You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

477 lines
14 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
  1. package iamapi
  2. import (
  3. "crypto/sha1"
  4. "encoding/json"
  5. "fmt"
  6. "math/rand"
  7. "net/http"
  8. "net/url"
  9. "reflect"
  10. "strings"
  11. "sync"
  12. "time"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  15. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  16. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  17. "github.com/aws/aws-sdk-go/service/iam"
  18. )
  19. const (
  20. charsetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  21. charset = charsetUpper + "abcdefghijklmnopqrstuvwxyz/"
  22. policyDocumentVersion = "2012-10-17"
  23. StatementActionAdmin = "*"
  24. StatementActionWrite = "Put*"
  25. StatementActionRead = "Get*"
  26. StatementActionList = "List*"
  27. StatementActionTagging = "Tagging*"
  28. )
  29. var (
  30. seededRand *rand.Rand = rand.New(
  31. rand.NewSource(time.Now().UnixNano()))
  32. policyDocuments = map[string]*PolicyDocument{}
  33. policyLock = sync.RWMutex{}
  34. )
  35. func MapToStatementAction(action string) string {
  36. switch action {
  37. case StatementActionAdmin:
  38. return s3_constants.ACTION_ADMIN
  39. case StatementActionWrite:
  40. return s3_constants.ACTION_WRITE
  41. case StatementActionRead:
  42. return s3_constants.ACTION_READ
  43. case StatementActionList:
  44. return s3_constants.ACTION_LIST
  45. case StatementActionTagging:
  46. return s3_constants.ACTION_TAGGING
  47. default:
  48. return ""
  49. }
  50. }
  51. func MapToIdentitiesAction(action string) string {
  52. switch action {
  53. case s3_constants.ACTION_ADMIN:
  54. return StatementActionAdmin
  55. case s3_constants.ACTION_WRITE:
  56. return StatementActionWrite
  57. case s3_constants.ACTION_READ:
  58. return StatementActionRead
  59. case s3_constants.ACTION_LIST:
  60. return StatementActionList
  61. case s3_constants.ACTION_TAGGING:
  62. return StatementActionTagging
  63. default:
  64. return ""
  65. }
  66. }
  67. type Statement struct {
  68. Effect string `json:"Effect"`
  69. Action []string `json:"Action"`
  70. Resource []string `json:"Resource"`
  71. }
  72. type Policies struct {
  73. Policies map[string]PolicyDocument `json:"policies"`
  74. }
  75. type PolicyDocument struct {
  76. Version string `json:"Version"`
  77. Statement []*Statement `json:"Statement"`
  78. }
  79. func (p PolicyDocument) String() string {
  80. b, _ := json.Marshal(p)
  81. return string(b)
  82. }
  83. func Hash(s *string) string {
  84. h := sha1.New()
  85. h.Write([]byte(*s))
  86. return fmt.Sprintf("%x", h.Sum(nil))
  87. }
  88. func StringWithCharset(length int, charset string) string {
  89. b := make([]byte, length)
  90. for i := range b {
  91. b[i] = charset[seededRand.Intn(len(charset))]
  92. }
  93. return string(b)
  94. }
  95. func (iama *IamApiServer) ListUsers(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp ListUsersResponse) {
  96. for _, ident := range s3cfg.Identities {
  97. resp.ListUsersResult.Users = append(resp.ListUsersResult.Users, &iam.User{UserName: &ident.Name})
  98. }
  99. return resp
  100. }
  101. func (iama *IamApiServer) ListAccessKeys(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp ListAccessKeysResponse) {
  102. status := iam.StatusTypeActive
  103. userName := values.Get("UserName")
  104. for _, ident := range s3cfg.Identities {
  105. if userName != "" && userName != ident.Name {
  106. continue
  107. }
  108. for _, cred := range ident.Credentials {
  109. resp.ListAccessKeysResult.AccessKeyMetadata = append(resp.ListAccessKeysResult.AccessKeyMetadata,
  110. &iam.AccessKeyMetadata{UserName: &ident.Name, AccessKeyId: &cred.AccessKey, Status: &status},
  111. )
  112. }
  113. }
  114. return resp
  115. }
  116. func (iama *IamApiServer) CreateUser(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp CreateUserResponse) {
  117. userName := values.Get("UserName")
  118. resp.CreateUserResult.User.UserName = &userName
  119. s3cfg.Identities = append(s3cfg.Identities, &iam_pb.Identity{Name: userName})
  120. return resp
  121. }
  122. func (iama *IamApiServer) DeleteUser(s3cfg *iam_pb.S3ApiConfiguration, userName string) (resp DeleteUserResponse, err error) {
  123. for i, ident := range s3cfg.Identities {
  124. if userName == ident.Name {
  125. s3cfg.Identities = append(s3cfg.Identities[:i], s3cfg.Identities[i+1:]...)
  126. return resp, nil
  127. }
  128. }
  129. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  130. }
  131. func (iama *IamApiServer) GetUser(s3cfg *iam_pb.S3ApiConfiguration, userName string) (resp GetUserResponse, err error) {
  132. for _, ident := range s3cfg.Identities {
  133. if userName == ident.Name {
  134. resp.GetUserResult.User = iam.User{UserName: &ident.Name}
  135. return resp, nil
  136. }
  137. }
  138. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  139. }
  140. func (iama *IamApiServer) UpdateUser(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp UpdateUserResponse, err error) {
  141. userName := values.Get("UserName")
  142. newUserName := values.Get("NewUserName")
  143. if newUserName != "" {
  144. for _, ident := range s3cfg.Identities {
  145. if userName == ident.Name {
  146. ident.Name = newUserName
  147. return resp, nil
  148. }
  149. }
  150. } else {
  151. return resp, nil
  152. }
  153. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  154. }
  155. func GetPolicyDocument(policy *string) (policyDocument PolicyDocument, err error) {
  156. if err = json.Unmarshal([]byte(*policy), &policyDocument); err != nil {
  157. return PolicyDocument{}, err
  158. }
  159. return policyDocument, err
  160. }
  161. func (iama *IamApiServer) CreatePolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp CreatePolicyResponse, err error) {
  162. policyName := values.Get("PolicyName")
  163. policyDocumentString := values.Get("PolicyDocument")
  164. policyDocument, err := GetPolicyDocument(&policyDocumentString)
  165. if err != nil {
  166. return CreatePolicyResponse{}, err
  167. }
  168. policyId := Hash(&policyDocumentString)
  169. arn := fmt.Sprintf("arn:aws:iam:::policy/%s", policyName)
  170. resp.CreatePolicyResult.Policy.PolicyName = &policyName
  171. resp.CreatePolicyResult.Policy.Arn = &arn
  172. resp.CreatePolicyResult.Policy.PolicyId = &policyId
  173. policies := Policies{}
  174. policyLock.Lock()
  175. defer policyLock.Unlock()
  176. if err = iama.s3ApiConfig.GetPolicies(&policies); err != nil {
  177. return resp, err
  178. }
  179. policies.Policies[policyName] = policyDocument
  180. if err = iama.s3ApiConfig.PutPolicies(&policies); err != nil {
  181. return resp, err
  182. }
  183. return resp, nil
  184. }
  185. func (iama *IamApiServer) PutUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp PutUserPolicyResponse, err error) {
  186. userName := values.Get("UserName")
  187. policyName := values.Get("PolicyName")
  188. policyDocumentString := values.Get("PolicyDocument")
  189. policyDocument, err := GetPolicyDocument(&policyDocumentString)
  190. if err != nil {
  191. return PutUserPolicyResponse{}, err
  192. }
  193. policyDocuments[policyName] = &policyDocument
  194. actions := GetActions(&policyDocument)
  195. for _, ident := range s3cfg.Identities {
  196. if userName == ident.Name {
  197. for _, action := range actions {
  198. ident.Actions = append(ident.Actions, action)
  199. }
  200. break
  201. }
  202. }
  203. return resp, nil
  204. }
  205. func (iama *IamApiServer) GetUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp GetUserPolicyResponse, err error) {
  206. userName := values.Get("UserName")
  207. policyName := values.Get("PolicyName")
  208. for _, ident := range s3cfg.Identities {
  209. if userName != ident.Name {
  210. continue
  211. }
  212. resp.GetUserPolicyResult.UserName = userName
  213. resp.GetUserPolicyResult.PolicyName = policyName
  214. if len(ident.Actions) == 0 {
  215. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  216. }
  217. policyDocument := PolicyDocument{Version: policyDocumentVersion}
  218. statements := make(map[string][]string)
  219. for _, action := range ident.Actions {
  220. // parse "Read:EXAMPLE-BUCKET"
  221. act := strings.Split(action, ":")
  222. resource := "*"
  223. if len(act) == 2 {
  224. resource = fmt.Sprintf("arn:aws:s3:::%s/*", act[1])
  225. }
  226. statements[resource] = append(statements[resource],
  227. fmt.Sprintf("s3:%s", MapToIdentitiesAction(act[0])),
  228. )
  229. }
  230. for resource, actions := range statements {
  231. isEqAction := false
  232. for i, statement := range policyDocument.Statement {
  233. if reflect.DeepEqual(statement.Action, actions) {
  234. policyDocument.Statement[i].Resource = append(
  235. policyDocument.Statement[i].Resource, resource)
  236. isEqAction = true
  237. break
  238. }
  239. }
  240. if isEqAction {
  241. continue
  242. }
  243. policyDocumentStatement := Statement{
  244. Effect: "Allow",
  245. Action: actions,
  246. }
  247. policyDocumentStatement.Resource = append(policyDocumentStatement.Resource, resource)
  248. policyDocument.Statement = append(policyDocument.Statement, &policyDocumentStatement)
  249. }
  250. resp.GetUserPolicyResult.PolicyDocument = policyDocument.String()
  251. return resp, nil
  252. }
  253. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  254. }
  255. func (iama *IamApiServer) DeleteUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp PutUserPolicyResponse, err error) {
  256. userName := values.Get("UserName")
  257. for i, ident := range s3cfg.Identities {
  258. if ident.Name == userName {
  259. s3cfg.Identities = append(s3cfg.Identities[:i], s3cfg.Identities[i+1:]...)
  260. return resp, nil
  261. }
  262. }
  263. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  264. }
  265. func GetActions(policy *PolicyDocument) (actions []string) {
  266. for _, statement := range policy.Statement {
  267. if statement.Effect != "Allow" {
  268. continue
  269. }
  270. for _, resource := range statement.Resource {
  271. // Parse "arn:aws:s3:::my-bucket/shared/*"
  272. res := strings.Split(resource, ":")
  273. if len(res) != 6 || res[0] != "arn" || res[1] != "aws" || res[2] != "s3" {
  274. glog.Infof("not match resource: %s", res)
  275. continue
  276. }
  277. for _, action := range statement.Action {
  278. // Parse "s3:Get*"
  279. act := strings.Split(action, ":")
  280. if len(act) != 2 || act[0] != "s3" {
  281. glog.Infof("not match action: %s", act)
  282. continue
  283. }
  284. statementAction := MapToStatementAction(act[1])
  285. if res[5] == "*" {
  286. actions = append(actions, statementAction)
  287. continue
  288. }
  289. // Parse my-bucket/shared/*
  290. path := strings.Split(res[5], "/")
  291. if len(path) != 2 || path[1] != "*" {
  292. glog.Infof("not match bucket: %s", path)
  293. continue
  294. }
  295. actions = append(actions, fmt.Sprintf("%s:%s", statementAction, path[0]))
  296. }
  297. }
  298. }
  299. return actions
  300. }
  301. func (iama *IamApiServer) CreateAccessKey(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp CreateAccessKeyResponse) {
  302. userName := values.Get("UserName")
  303. status := iam.StatusTypeActive
  304. accessKeyId := StringWithCharset(21, charsetUpper)
  305. secretAccessKey := StringWithCharset(42, charset)
  306. resp.CreateAccessKeyResult.AccessKey.AccessKeyId = &accessKeyId
  307. resp.CreateAccessKeyResult.AccessKey.SecretAccessKey = &secretAccessKey
  308. resp.CreateAccessKeyResult.AccessKey.UserName = &userName
  309. resp.CreateAccessKeyResult.AccessKey.Status = &status
  310. changed := false
  311. for _, ident := range s3cfg.Identities {
  312. if userName == ident.Name {
  313. ident.Credentials = append(ident.Credentials,
  314. &iam_pb.Credential{AccessKey: accessKeyId, SecretKey: secretAccessKey})
  315. changed = true
  316. break
  317. }
  318. }
  319. if !changed {
  320. s3cfg.Identities = append(s3cfg.Identities,
  321. &iam_pb.Identity{Name: userName,
  322. Credentials: []*iam_pb.Credential{
  323. {
  324. AccessKey: accessKeyId,
  325. SecretKey: secretAccessKey,
  326. },
  327. },
  328. },
  329. )
  330. }
  331. return resp
  332. }
  333. func (iama *IamApiServer) DeleteAccessKey(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp DeleteAccessKeyResponse) {
  334. userName := values.Get("UserName")
  335. accessKeyId := values.Get("AccessKeyId")
  336. for _, ident := range s3cfg.Identities {
  337. if userName == ident.Name {
  338. for i, cred := range ident.Credentials {
  339. if cred.AccessKey == accessKeyId {
  340. ident.Credentials = append(ident.Credentials[:i], ident.Credentials[i+1:]...)
  341. break
  342. }
  343. }
  344. break
  345. }
  346. }
  347. return resp
  348. }
  349. func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
  350. if err := r.ParseForm(); err != nil {
  351. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  352. return
  353. }
  354. values := r.PostForm
  355. var s3cfgLock sync.RWMutex
  356. s3cfgLock.RLock()
  357. s3cfg := &iam_pb.S3ApiConfiguration{}
  358. if err := iama.s3ApiConfig.GetS3ApiConfiguration(s3cfg); err != nil {
  359. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  360. return
  361. }
  362. s3cfgLock.RUnlock()
  363. glog.V(4).Infof("DoActions: %+v", values)
  364. var response interface{}
  365. var err error
  366. changed := true
  367. switch r.Form.Get("Action") {
  368. case "ListUsers":
  369. response = iama.ListUsers(s3cfg, values)
  370. changed = false
  371. case "ListAccessKeys":
  372. response = iama.ListAccessKeys(s3cfg, values)
  373. changed = false
  374. case "CreateUser":
  375. response = iama.CreateUser(s3cfg, values)
  376. case "GetUser":
  377. userName := values.Get("UserName")
  378. response, err = iama.GetUser(s3cfg, userName)
  379. if err != nil {
  380. writeIamErrorResponse(w, r, err, "user", userName, nil)
  381. return
  382. }
  383. changed = false
  384. case "UpdateUser":
  385. response, err = iama.UpdateUser(s3cfg, values)
  386. if err != nil {
  387. glog.Errorf("UpdateUser: %+v", err)
  388. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  389. return
  390. }
  391. case "DeleteUser":
  392. userName := values.Get("UserName")
  393. response, err = iama.DeleteUser(s3cfg, userName)
  394. if err != nil {
  395. writeIamErrorResponse(w, r, err, "user", userName, nil)
  396. return
  397. }
  398. case "CreateAccessKey":
  399. response = iama.CreateAccessKey(s3cfg, values)
  400. case "DeleteAccessKey":
  401. response = iama.DeleteAccessKey(s3cfg, values)
  402. case "CreatePolicy":
  403. response, err = iama.CreatePolicy(s3cfg, values)
  404. if err != nil {
  405. glog.Errorf("CreatePolicy: %+v", err)
  406. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  407. return
  408. }
  409. case "PutUserPolicy":
  410. response, err = iama.PutUserPolicy(s3cfg, values)
  411. if err != nil {
  412. glog.Errorf("PutUserPolicy: %+v", err)
  413. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  414. return
  415. }
  416. case "GetUserPolicy":
  417. response, err = iama.GetUserPolicy(s3cfg, values)
  418. if err != nil {
  419. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  420. return
  421. }
  422. changed = false
  423. case "DeleteUserPolicy":
  424. if response, err = iama.DeleteUserPolicy(s3cfg, values); err != nil {
  425. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  426. }
  427. default:
  428. errNotImplemented := s3err.GetAPIError(s3err.ErrNotImplemented)
  429. errorResponse := ErrorResponse{}
  430. errorResponse.Error.Code = &errNotImplemented.Code
  431. errorResponse.Error.Message = &errNotImplemented.Description
  432. s3err.WriteXMLResponse(w, r, errNotImplemented.HTTPStatusCode, errorResponse)
  433. return
  434. }
  435. if changed {
  436. s3cfgLock.Lock()
  437. err := iama.s3ApiConfig.PutS3ApiConfiguration(s3cfg)
  438. s3cfgLock.Unlock()
  439. if err != nil {
  440. writeIamErrorResponse(w, r, fmt.Errorf(iam.ErrCodeServiceFailureException), "", "", err)
  441. return
  442. }
  443. }
  444. s3err.WriteXMLResponse(w, r, http.StatusOK, response)
  445. }