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.

510 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
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
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. // handleImplicitUsername adds username who signs the request to values if 'username' is not specified
  350. // According to https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/create-access-key.html/
  351. // "If you do not specify a user name, IAM determines the user name implicitly based on the Amazon Web
  352. // Services access key ID signing the request."
  353. func handleImplicitUsername(r *http.Request, values url.Values) {
  354. if len(r.Header["Authorization"]) == 0 || values.Get("UserName") != "" {
  355. return
  356. }
  357. // get username who signs the request. For a typical Authorization:
  358. // "AWS4-HMAC-SHA256 Credential=197FSAQ7HHTA48X64O3A/20220420/test1/iam/aws4_request, SignedHeaders=content-type;
  359. // host;x-amz-date, Signature=6757dc6b3d7534d67e17842760310e99ee695408497f6edc4fdb84770c252dc8",
  360. // the "test1" will be extracted as the username
  361. glog.V(4).Infof("Authorization field: %v", r.Header["Authorization"][0])
  362. s := strings.Split(r.Header["Authorization"][0], "Credential=")
  363. if len(s) < 2 {
  364. return
  365. }
  366. s = strings.Split(s[1], ",")
  367. if len(s) < 2 {
  368. return
  369. }
  370. s = strings.Split(s[0], "/")
  371. if len(s) < 5 {
  372. return
  373. }
  374. userName := s[2]
  375. values.Set("UserName", userName)
  376. }
  377. func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
  378. if err := r.ParseForm(); err != nil {
  379. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  380. return
  381. }
  382. values := r.PostForm
  383. var s3cfgLock sync.RWMutex
  384. s3cfgLock.RLock()
  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. s3cfgLock.RUnlock()
  391. glog.V(4).Infof("DoActions: %+v", values)
  392. var response interface{}
  393. var err error
  394. changed := true
  395. switch r.Form.Get("Action") {
  396. case "ListUsers":
  397. response = iama.ListUsers(s3cfg, values)
  398. changed = false
  399. case "ListAccessKeys":
  400. handleImplicitUsername(r, values)
  401. response = iama.ListAccessKeys(s3cfg, values)
  402. changed = false
  403. case "CreateUser":
  404. response = iama.CreateUser(s3cfg, values)
  405. case "GetUser":
  406. userName := values.Get("UserName")
  407. response, err = iama.GetUser(s3cfg, userName)
  408. if err != nil {
  409. writeIamErrorResponse(w, r, err, "user", userName, nil)
  410. return
  411. }
  412. changed = false
  413. case "UpdateUser":
  414. response, err = iama.UpdateUser(s3cfg, values)
  415. if err != nil {
  416. glog.Errorf("UpdateUser: %+v", err)
  417. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  418. return
  419. }
  420. case "DeleteUser":
  421. userName := values.Get("UserName")
  422. response, err = iama.DeleteUser(s3cfg, userName)
  423. if err != nil {
  424. writeIamErrorResponse(w, r, err, "user", userName, nil)
  425. return
  426. }
  427. case "CreateAccessKey":
  428. handleImplicitUsername(r, values)
  429. response = iama.CreateAccessKey(s3cfg, values)
  430. case "DeleteAccessKey":
  431. handleImplicitUsername(r, values)
  432. response = iama.DeleteAccessKey(s3cfg, values)
  433. case "CreatePolicy":
  434. response, err = iama.CreatePolicy(s3cfg, values)
  435. if err != nil {
  436. glog.Errorf("CreatePolicy: %+v", err)
  437. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  438. return
  439. }
  440. case "PutUserPolicy":
  441. response, err = iama.PutUserPolicy(s3cfg, values)
  442. if err != nil {
  443. glog.Errorf("PutUserPolicy: %+v", err)
  444. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  445. return
  446. }
  447. case "GetUserPolicy":
  448. response, err = iama.GetUserPolicy(s3cfg, values)
  449. if err != nil {
  450. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  451. return
  452. }
  453. changed = false
  454. case "DeleteUserPolicy":
  455. if response, err = iama.DeleteUserPolicy(s3cfg, values); err != nil {
  456. writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
  457. return
  458. }
  459. default:
  460. errNotImplemented := s3err.GetAPIError(s3err.ErrNotImplemented)
  461. errorResponse := ErrorResponse{}
  462. errorResponse.Error.Code = &errNotImplemented.Code
  463. errorResponse.Error.Message = &errNotImplemented.Description
  464. s3err.WriteXMLResponse(w, r, errNotImplemented.HTTPStatusCode, errorResponse)
  465. return
  466. }
  467. if changed {
  468. s3cfgLock.Lock()
  469. err := iama.s3ApiConfig.PutS3ApiConfiguration(s3cfg)
  470. s3cfgLock.Unlock()
  471. if err != nil {
  472. writeIamErrorResponse(w, r, fmt.Errorf(iam.ErrCodeServiceFailureException), "", "", err)
  473. return
  474. }
  475. }
  476. s3err.WriteXMLResponse(w, r, http.StatusOK, response)
  477. }