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.

516 lines
16 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
3 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
3 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
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. // 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. existedActions := make(map[string]bool, len(ident.Actions))
  201. for _, action := range ident.Actions {
  202. existedActions[action] = true
  203. }
  204. for _, action := range actions {
  205. if !existedActions[action] {
  206. ident.Actions = append(ident.Actions, action)
  207. }
  208. }
  209. return resp, nil
  210. }
  211. return resp, fmt.Errorf("%s: the user with name %s cannot be found", iam.ErrCodeNoSuchEntityException, userName)
  212. }
  213. func (iama *IamApiServer) GetUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp GetUserPolicyResponse, err error) {
  214. userName := values.Get("UserName")
  215. policyName := values.Get("PolicyName")
  216. for _, ident := range s3cfg.Identities {
  217. if userName != ident.Name {
  218. continue
  219. }
  220. resp.GetUserPolicyResult.UserName = userName
  221. resp.GetUserPolicyResult.PolicyName = policyName
  222. if len(ident.Actions) == 0 {
  223. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  224. }
  225. policyDocument := PolicyDocument{Version: policyDocumentVersion}
  226. statements := make(map[string][]string)
  227. for _, action := range ident.Actions {
  228. // parse "Read:EXAMPLE-BUCKET"
  229. act := strings.Split(action, ":")
  230. resource := "*"
  231. if len(act) == 2 {
  232. resource = fmt.Sprintf("arn:aws:s3:::%s/*", act[1])
  233. }
  234. statements[resource] = append(statements[resource],
  235. fmt.Sprintf("s3:%s", MapToIdentitiesAction(act[0])),
  236. )
  237. }
  238. for resource, actions := range statements {
  239. isEqAction := false
  240. for i, statement := range policyDocument.Statement {
  241. if reflect.DeepEqual(statement.Action, actions) {
  242. policyDocument.Statement[i].Resource = append(
  243. policyDocument.Statement[i].Resource, resource)
  244. isEqAction = true
  245. break
  246. }
  247. }
  248. if isEqAction {
  249. continue
  250. }
  251. policyDocumentStatement := Statement{
  252. Effect: "Allow",
  253. Action: actions,
  254. }
  255. policyDocumentStatement.Resource = append(policyDocumentStatement.Resource, resource)
  256. policyDocument.Statement = append(policyDocument.Statement, &policyDocumentStatement)
  257. }
  258. resp.GetUserPolicyResult.PolicyDocument = policyDocument.String()
  259. return resp, nil
  260. }
  261. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  262. }
  263. func (iama *IamApiServer) DeleteUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp PutUserPolicyResponse, err error) {
  264. userName := values.Get("UserName")
  265. for i, ident := range s3cfg.Identities {
  266. if ident.Name == userName {
  267. s3cfg.Identities = append(s3cfg.Identities[:i], s3cfg.Identities[i+1:]...)
  268. return resp, nil
  269. }
  270. }
  271. return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
  272. }
  273. func GetActions(policy *PolicyDocument) (actions []string) {
  274. for _, statement := range policy.Statement {
  275. if statement.Effect != "Allow" {
  276. continue
  277. }
  278. for _, resource := range statement.Resource {
  279. // Parse "arn:aws:s3:::my-bucket/shared/*"
  280. res := strings.Split(resource, ":")
  281. if len(res) != 6 || res[0] != "arn" || res[1] != "aws" || res[2] != "s3" {
  282. glog.Infof("not match resource: %s", res)
  283. continue
  284. }
  285. for _, action := range statement.Action {
  286. // Parse "s3:Get*"
  287. act := strings.Split(action, ":")
  288. if len(act) != 2 || act[0] != "s3" {
  289. glog.Infof("not match action: %s", act)
  290. continue
  291. }
  292. statementAction := MapToStatementAction(act[1])
  293. if res[5] == "*" {
  294. actions = append(actions, statementAction)
  295. continue
  296. }
  297. // Parse my-bucket/shared/*
  298. path := strings.Split(res[5], "/")
  299. if len(path) != 2 || path[1] != "*" {
  300. glog.Infof("not match bucket: %s", path)
  301. continue
  302. }
  303. actions = append(actions, fmt.Sprintf("%s:%s", statementAction, path[0]))
  304. }
  305. }
  306. }
  307. return actions
  308. }
  309. func (iama *IamApiServer) CreateAccessKey(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp CreateAccessKeyResponse) {
  310. userName := values.Get("UserName")
  311. status := iam.StatusTypeActive
  312. accessKeyId := StringWithCharset(21, charsetUpper)
  313. secretAccessKey := StringWithCharset(42, charset)
  314. resp.CreateAccessKeyResult.AccessKey.AccessKeyId = &accessKeyId
  315. resp.CreateAccessKeyResult.AccessKey.SecretAccessKey = &secretAccessKey
  316. resp.CreateAccessKeyResult.AccessKey.UserName = &userName
  317. resp.CreateAccessKeyResult.AccessKey.Status = &status
  318. changed := false
  319. for _, ident := range s3cfg.Identities {
  320. if userName == ident.Name {
  321. ident.Credentials = append(ident.Credentials,
  322. &iam_pb.Credential{AccessKey: accessKeyId, SecretKey: secretAccessKey})
  323. changed = true
  324. break
  325. }
  326. }
  327. if !changed {
  328. s3cfg.Identities = append(s3cfg.Identities,
  329. &iam_pb.Identity{
  330. Name: userName,
  331. Credentials: []*iam_pb.Credential{
  332. {
  333. AccessKey: accessKeyId,
  334. SecretKey: secretAccessKey,
  335. },
  336. },
  337. },
  338. )
  339. }
  340. return resp
  341. }
  342. func (iama *IamApiServer) DeleteAccessKey(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp DeleteAccessKeyResponse) {
  343. userName := values.Get("UserName")
  344. accessKeyId := values.Get("AccessKeyId")
  345. for _, ident := range s3cfg.Identities {
  346. if userName == ident.Name {
  347. for i, cred := range ident.Credentials {
  348. if cred.AccessKey == accessKeyId {
  349. ident.Credentials = append(ident.Credentials[:i], ident.Credentials[i+1:]...)
  350. break
  351. }
  352. }
  353. break
  354. }
  355. }
  356. return resp
  357. }
  358. // handleImplicitUsername adds username who signs the request to values if 'username' is not specified
  359. // According to https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/create-access-key.html/
  360. // "If you do not specify a user name, IAM determines the user name implicitly based on the Amazon Web
  361. // Services access key ID signing the request."
  362. func handleImplicitUsername(r *http.Request, values url.Values) {
  363. if len(r.Header["Authorization"]) == 0 || values.Get("UserName") != "" {
  364. return
  365. }
  366. // get username who signs the request. For a typical Authorization:
  367. // "AWS4-HMAC-SHA256 Credential=197FSAQ7HHTA48X64O3A/20220420/test1/iam/aws4_request, SignedHeaders=content-type;
  368. // host;x-amz-date, Signature=6757dc6b3d7534d67e17842760310e99ee695408497f6edc4fdb84770c252dc8",
  369. // the "test1" will be extracted as the username
  370. glog.V(4).Infof("Authorization field: %v", r.Header["Authorization"][0])
  371. s := strings.Split(r.Header["Authorization"][0], "Credential=")
  372. if len(s) < 2 {
  373. return
  374. }
  375. s = strings.Split(s[1], ",")
  376. if len(s) < 2 {
  377. return
  378. }
  379. s = strings.Split(s[0], "/")
  380. if len(s) < 5 {
  381. return
  382. }
  383. userName := s[2]
  384. values.Set("UserName", userName)
  385. }
  386. func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
  387. if err := r.ParseForm(); err != nil {
  388. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  389. return
  390. }
  391. values := r.PostForm
  392. s3cfg := &iam_pb.S3ApiConfiguration{}
  393. if err := iama.s3ApiConfig.GetS3ApiConfiguration(s3cfg); err != nil {
  394. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  395. return
  396. }
  397. glog.V(4).Infof("DoActions: %+v", values)
  398. var response interface{}
  399. var err error
  400. changed := true
  401. switch r.Form.Get("Action") {
  402. case "ListUsers":
  403. response = iama.ListUsers(s3cfg, values)
  404. changed = false
  405. case "ListAccessKeys":
  406. handleImplicitUsername(r, values)
  407. response = iama.ListAccessKeys(s3cfg, values)
  408. changed = false
  409. case "CreateUser":
  410. response = iama.CreateUser(s3cfg, values)
  411. case "GetUser":
  412. userName := values.Get("UserName")
  413. response, err = iama.GetUser(s3cfg, userName)
  414. if err != nil {
  415. writeIamErrorResponse(w, r, err, "user", userName, nil)
  416. return
  417. }
  418. changed = false
  419. case "UpdateUser":
  420. response, err = iama.UpdateUser(s3cfg, values)
  421. if err != nil {
  422. glog.Errorf("UpdateUser: %+v", err)
  423. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  424. return
  425. }
  426. case "DeleteUser":
  427. userName := values.Get("UserName")
  428. response, err = iama.DeleteUser(s3cfg, userName)
  429. if err != nil {
  430. writeIamErrorResponse(w, r, err, "user", userName, nil)
  431. return
  432. }
  433. case "CreateAccessKey":
  434. handleImplicitUsername(r, values)
  435. response = iama.CreateAccessKey(s3cfg, values)
  436. case "DeleteAccessKey":
  437. handleImplicitUsername(r, values)
  438. response = iama.DeleteAccessKey(s3cfg, values)
  439. case "CreatePolicy":
  440. response, err = iama.CreatePolicy(s3cfg, values)
  441. if err != nil {
  442. glog.Errorf("CreatePolicy: %+v", err)
  443. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  444. return
  445. }
  446. case "PutUserPolicy":
  447. response, err = iama.PutUserPolicy(s3cfg, values)
  448. if err != nil {
  449. glog.Errorf("PutUserPolicy: %+v", err)
  450. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  451. return
  452. }
  453. case "GetUserPolicy":
  454. response, err = iama.GetUserPolicy(s3cfg, values)
  455. if err != nil {
  456. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  457. return
  458. }
  459. changed = false
  460. case "DeleteUserPolicy":
  461. if response, err = iama.DeleteUserPolicy(s3cfg, values); err != nil {
  462. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  463. return
  464. }
  465. default:
  466. errNotImplemented := s3err.GetAPIError(s3err.ErrNotImplemented)
  467. errorResponse := ErrorResponse{}
  468. errorResponse.Error.Code = &errNotImplemented.Code
  469. errorResponse.Error.Message = &errNotImplemented.Description
  470. s3err.WriteXMLResponse(w, r, errNotImplemented.HTTPStatusCode, errorResponse)
  471. return
  472. }
  473. if changed {
  474. err := iama.s3ApiConfig.PutS3ApiConfiguration(s3cfg)
  475. if err != nil {
  476. writeIamErrorResponse(w, r, fmt.Errorf(iam.ErrCodeServiceFailureException), "", "", err)
  477. return
  478. }
  479. }
  480. s3err.WriteXMLResponse(w, r, http.StatusOK, response)
  481. }