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.

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