iamapi_management_handlers.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/seaweedfs/seaweedfs/weed/glog"
  14. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  15. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  16. "github.com/seaweedfs/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. // https://docs.aws.amazon.com/IAM/latest/APIReference/API_PutUserPolicy.html
  186. func (iama *IamApiServer) PutUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp PutUserPolicyResponse, err error) {
  187. userName := values.Get("UserName")
  188. policyName := values.Get("PolicyName")
  189. policyDocumentString := values.Get("PolicyDocument")
  190. policyDocument, err := GetPolicyDocument(&policyDocumentString)
  191. if err != nil {
  192. return PutUserPolicyResponse{}, err
  193. }
  194. policyDocuments[policyName] = &policyDocument
  195. actions := GetActions(&policyDocument)
  196. for _, ident := range s3cfg.Identities {
  197. if userName != ident.Name {
  198. continue
  199. }
  200. ident.Actions = actions
  201. return resp, nil
  202. }
  203. return resp, fmt.Errorf("%s: the user with name %s cannot be found", iam.ErrCodeNoSuchEntityException, userName)
  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{
  322. Name: userName,
  323. Credentials: []*iam_pb.Credential{
  324. {
  325. AccessKey: accessKeyId,
  326. SecretKey: secretAccessKey,
  327. },
  328. },
  329. },
  330. )
  331. }
  332. return resp
  333. }
  334. func (iama *IamApiServer) DeleteAccessKey(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp DeleteAccessKeyResponse) {
  335. userName := values.Get("UserName")
  336. accessKeyId := values.Get("AccessKeyId")
  337. for _, ident := range s3cfg.Identities {
  338. if userName == ident.Name {
  339. for i, cred := range ident.Credentials {
  340. if cred.AccessKey == accessKeyId {
  341. ident.Credentials = append(ident.Credentials[:i], ident.Credentials[i+1:]...)
  342. break
  343. }
  344. }
  345. break
  346. }
  347. }
  348. return resp
  349. }
  350. // handleImplicitUsername adds username who signs the request to values if 'username' is not specified
  351. // According to https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/create-access-key.html/
  352. // "If you do not specify a user name, IAM determines the user name implicitly based on the Amazon Web
  353. // Services access key ID signing the request."
  354. func handleImplicitUsername(r *http.Request, values url.Values) {
  355. if len(r.Header["Authorization"]) == 0 || values.Get("UserName") != "" {
  356. return
  357. }
  358. // get username who signs the request. For a typical Authorization:
  359. // "AWS4-HMAC-SHA256 Credential=197FSAQ7HHTA48X64O3A/20220420/test1/iam/aws4_request, SignedHeaders=content-type;
  360. // host;x-amz-date, Signature=6757dc6b3d7534d67e17842760310e99ee695408497f6edc4fdb84770c252dc8",
  361. // the "test1" will be extracted as the username
  362. glog.V(4).Infof("Authorization field: %v", r.Header["Authorization"][0])
  363. s := strings.Split(r.Header["Authorization"][0], "Credential=")
  364. if len(s) < 2 {
  365. return
  366. }
  367. s = strings.Split(s[1], ",")
  368. if len(s) < 2 {
  369. return
  370. }
  371. s = strings.Split(s[0], "/")
  372. if len(s) < 5 {
  373. return
  374. }
  375. userName := s[2]
  376. values.Set("UserName", userName)
  377. }
  378. func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
  379. if err := r.ParseForm(); err != nil {
  380. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  381. return
  382. }
  383. values := r.PostForm
  384. s3cfg := &iam_pb.S3ApiConfiguration{}
  385. if err := iama.s3ApiConfig.GetS3ApiConfiguration(s3cfg); err != nil {
  386. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  387. return
  388. }
  389. glog.V(4).Infof("DoActions: %+v", values)
  390. var response interface{}
  391. var err error
  392. changed := true
  393. switch r.Form.Get("Action") {
  394. case "ListUsers":
  395. response = iama.ListUsers(s3cfg, values)
  396. changed = false
  397. case "ListAccessKeys":
  398. handleImplicitUsername(r, values)
  399. response = iama.ListAccessKeys(s3cfg, values)
  400. changed = false
  401. case "CreateUser":
  402. response = iama.CreateUser(s3cfg, values)
  403. case "GetUser":
  404. userName := values.Get("UserName")
  405. response, err = iama.GetUser(s3cfg, userName)
  406. if err != nil {
  407. writeIamErrorResponse(w, r, err, "user", userName, nil)
  408. return
  409. }
  410. changed = false
  411. case "UpdateUser":
  412. response, err = iama.UpdateUser(s3cfg, values)
  413. if err != nil {
  414. glog.Errorf("UpdateUser: %+v", err)
  415. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  416. return
  417. }
  418. case "DeleteUser":
  419. userName := values.Get("UserName")
  420. response, err = iama.DeleteUser(s3cfg, userName)
  421. if err != nil {
  422. writeIamErrorResponse(w, r, err, "user", userName, nil)
  423. return
  424. }
  425. case "CreateAccessKey":
  426. handleImplicitUsername(r, values)
  427. response = iama.CreateAccessKey(s3cfg, values)
  428. case "DeleteAccessKey":
  429. handleImplicitUsername(r, values)
  430. response = iama.DeleteAccessKey(s3cfg, values)
  431. case "CreatePolicy":
  432. response, err = iama.CreatePolicy(s3cfg, values)
  433. if err != nil {
  434. glog.Errorf("CreatePolicy: %+v", err)
  435. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  436. return
  437. }
  438. case "PutUserPolicy":
  439. response, err = iama.PutUserPolicy(s3cfg, values)
  440. if err != nil {
  441. glog.Errorf("PutUserPolicy: %+v", err)
  442. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  443. return
  444. }
  445. case "GetUserPolicy":
  446. response, err = iama.GetUserPolicy(s3cfg, values)
  447. if err != nil {
  448. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  449. return
  450. }
  451. changed = false
  452. case "DeleteUserPolicy":
  453. if response, err = iama.DeleteUserPolicy(s3cfg, values); err != nil {
  454. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  455. return
  456. }
  457. default:
  458. errNotImplemented := s3err.GetAPIError(s3err.ErrNotImplemented)
  459. errorResponse := ErrorResponse{}
  460. errorResponse.Error.Code = &errNotImplemented.Code
  461. errorResponse.Error.Message = &errNotImplemented.Description
  462. s3err.WriteXMLResponse(w, r, errNotImplemented.HTTPStatusCode, errorResponse)
  463. return
  464. }
  465. if changed {
  466. err := iama.s3ApiConfig.PutS3ApiConfiguration(s3cfg)
  467. if err != nil {
  468. writeIamErrorResponse(w, r, fmt.Errorf(iam.ErrCodeServiceFailureException), "", "", err)
  469. return
  470. }
  471. }
  472. s3err.WriteXMLResponse(w, r, http.StatusOK, response)
  473. }