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.

209 lines
4.7 KiB

5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
  1. package s3api
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  6. "io/ioutil"
  7. "net/http"
  8. "github.com/golang/protobuf/jsonpb"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  11. )
  12. type Action string
  13. const (
  14. ACTION_READ = "Read"
  15. ACTION_WRITE = "Write"
  16. ACTION_ADMIN = "Admin"
  17. ACTION_TAGGING = "Tagging"
  18. ACTION_LIST = "List"
  19. )
  20. type Iam interface {
  21. Check(f http.HandlerFunc, actions ...Action) http.HandlerFunc
  22. }
  23. type IdentityAccessManagement struct {
  24. identities []*Identity
  25. domain string
  26. }
  27. type Identity struct {
  28. Name string
  29. Credentials []*Credential
  30. Actions []Action
  31. }
  32. type Credential struct {
  33. AccessKey string
  34. SecretKey string
  35. }
  36. func NewIdentityAccessManagement(fileName string, domain string) *IdentityAccessManagement {
  37. iam := &IdentityAccessManagement{
  38. domain: domain,
  39. }
  40. if fileName == "" {
  41. return iam
  42. }
  43. if err := iam.loadS3ApiConfiguration(fileName); err != nil {
  44. glog.Fatalf("fail to load config file %s: %v", fileName, err)
  45. }
  46. return iam
  47. }
  48. func (iam *IdentityAccessManagement) loadS3ApiConfiguration(fileName string) error {
  49. s3ApiConfiguration := &iam_pb.S3ApiConfiguration{}
  50. rawData, readErr := ioutil.ReadFile(fileName)
  51. if readErr != nil {
  52. glog.Warningf("fail to read %s : %v", fileName, readErr)
  53. return fmt.Errorf("fail to read %s : %v", fileName, readErr)
  54. }
  55. glog.V(1).Infof("load s3 config: %v", fileName)
  56. if err := jsonpb.Unmarshal(bytes.NewReader(rawData), s3ApiConfiguration); err != nil {
  57. glog.Warningf("unmarshal error: %v", err)
  58. return fmt.Errorf("unmarshal %s error: %v", fileName, err)
  59. }
  60. for _, ident := range s3ApiConfiguration.Identities {
  61. t := &Identity{
  62. Name: ident.Name,
  63. Credentials: nil,
  64. Actions: nil,
  65. }
  66. for _, action := range ident.Actions {
  67. t.Actions = append(t.Actions, Action(action))
  68. }
  69. for _, cred := range ident.Credentials {
  70. t.Credentials = append(t.Credentials, &Credential{
  71. AccessKey: cred.AccessKey,
  72. SecretKey: cred.SecretKey,
  73. })
  74. }
  75. iam.identities = append(iam.identities, t)
  76. }
  77. return nil
  78. }
  79. func (iam *IdentityAccessManagement) isEnabled() bool {
  80. return len(iam.identities) > 0
  81. }
  82. func (iam *IdentityAccessManagement) lookupByAccessKey(accessKey string) (identity *Identity, cred *Credential, found bool) {
  83. for _, ident := range iam.identities {
  84. for _, cred := range ident.Credentials {
  85. if cred.AccessKey == accessKey {
  86. return ident, cred, true
  87. }
  88. }
  89. }
  90. return nil, nil, false
  91. }
  92. func (iam *IdentityAccessManagement) lookupAnonymous() (identity *Identity, found bool) {
  93. for _, ident := range iam.identities {
  94. if ident.Name == "anonymous" {
  95. return ident, true
  96. }
  97. }
  98. return nil, false
  99. }
  100. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  101. if !iam.isEnabled() {
  102. return f
  103. }
  104. return func(w http.ResponseWriter, r *http.Request) {
  105. errCode := iam.authRequest(r, action)
  106. if errCode == s3err.ErrNone {
  107. f(w, r)
  108. return
  109. }
  110. writeErrorResponse(w, errCode, r.URL)
  111. }
  112. }
  113. // check whether the request has valid access keys
  114. func (iam *IdentityAccessManagement) authRequest(r *http.Request, action Action) s3err.ErrorCode {
  115. var identity *Identity
  116. var s3Err s3err.ErrorCode
  117. var found bool
  118. switch getRequestAuthType(r) {
  119. case authTypeStreamingSigned:
  120. return s3err.ErrNone
  121. case authTypeUnknown:
  122. glog.V(3).Infof("unknown auth type")
  123. return s3err.ErrAccessDenied
  124. case authTypePresignedV2, authTypeSignedV2:
  125. glog.V(3).Infof("v2 auth type")
  126. identity, s3Err = iam.isReqAuthenticatedV2(r)
  127. case authTypeSigned, authTypePresigned:
  128. glog.V(3).Infof("v4 auth type")
  129. identity, s3Err = iam.reqSignatureV4Verify(r)
  130. case authTypePostPolicy:
  131. glog.V(3).Infof("post policy auth type")
  132. return s3err.ErrNone
  133. case authTypeJWT:
  134. glog.V(3).Infof("jwt auth type")
  135. return s3err.ErrNotImplemented
  136. case authTypeAnonymous:
  137. identity, found = iam.lookupAnonymous()
  138. if !found {
  139. return s3err.ErrAccessDenied
  140. }
  141. default:
  142. return s3err.ErrNotImplemented
  143. }
  144. glog.V(3).Infof("auth error: %v", s3Err)
  145. if s3Err != s3err.ErrNone {
  146. return s3Err
  147. }
  148. glog.V(3).Infof("user name: %v actions: %v", identity.Name, identity.Actions)
  149. bucket, _ := getBucketAndObject(r)
  150. if !identity.canDo(action, bucket) {
  151. return s3err.ErrAccessDenied
  152. }
  153. return s3err.ErrNone
  154. }
  155. func (identity *Identity) canDo(action Action, bucket string) bool {
  156. for _, a := range identity.Actions {
  157. if a == "Admin" {
  158. return true
  159. }
  160. }
  161. for _, a := range identity.Actions {
  162. if a == action {
  163. return true
  164. }
  165. }
  166. if bucket == "" {
  167. return false
  168. }
  169. limitedByBucket := string(action) + ":" + bucket
  170. for _, a := range identity.Actions {
  171. if string(a) == limitedByBucket {
  172. return true
  173. }
  174. }
  175. return false
  176. }