iamapi_management_handlers.go 17 KB

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