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.

210 lines
4.8 KiB

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
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
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
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
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
5 years ago
5 years ago
5 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(option *S3ApiServerOption) *IdentityAccessManagement {
  37. iam := &IdentityAccessManagement{
  38. domain: option.DomainName,
  39. }
  40. if err := loadS3config(iam, option); err != nil {
  41. glog.Warningf("fail to load config %v", err)
  42. }
  43. if len(iam.identities) == 0 && option.Config != "" {
  44. if err := iam.loadS3ApiConfiguration(option.Config); err != nil {
  45. glog.Fatalf("fail to load config file %s: %v", option.Config, err)
  46. }
  47. }
  48. return iam
  49. }
  50. func (iam *IdentityAccessManagement) loadS3ApiConfiguration(fileName string) error {
  51. s3ApiConfiguration := &iam_pb.S3ApiConfiguration{}
  52. rawData, readErr := ioutil.ReadFile(fileName)
  53. if readErr != nil {
  54. glog.Warningf("fail to read %s : %v", fileName, readErr)
  55. return fmt.Errorf("fail to read %s : %v", fileName, readErr)
  56. }
  57. glog.V(1).Infof("load s3 config: %v", fileName)
  58. if err := jsonpb.Unmarshal(bytes.NewReader(rawData), s3ApiConfiguration); err != nil {
  59. glog.Warningf("unmarshal error: %v", err)
  60. return fmt.Errorf("unmarshal %s error: %v", fileName, err)
  61. }
  62. for _, ident := range s3ApiConfiguration.Identities {
  63. t := &Identity{
  64. Name: ident.Name,
  65. Credentials: nil,
  66. Actions: nil,
  67. }
  68. for _, action := range ident.Actions {
  69. t.Actions = append(t.Actions, Action(action))
  70. }
  71. for _, cred := range ident.Credentials {
  72. t.Credentials = append(t.Credentials, &Credential{
  73. AccessKey: cred.AccessKey,
  74. SecretKey: cred.SecretKey,
  75. })
  76. }
  77. iam.identities = append(iam.identities, t)
  78. }
  79. return nil
  80. }
  81. func (iam *IdentityAccessManagement) isEnabled() bool {
  82. return len(iam.identities) > 0
  83. }
  84. func (iam *IdentityAccessManagement) lookupByAccessKey(accessKey string) (identity *Identity, cred *Credential, found bool) {
  85. for _, ident := range iam.identities {
  86. for _, cred := range ident.Credentials {
  87. if cred.AccessKey == accessKey {
  88. return ident, cred, true
  89. }
  90. }
  91. }
  92. return nil, nil, false
  93. }
  94. func (iam *IdentityAccessManagement) lookupAnonymous() (identity *Identity, found bool) {
  95. for _, ident := range iam.identities {
  96. if ident.Name == "anonymous" {
  97. return ident, true
  98. }
  99. }
  100. return nil, false
  101. }
  102. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  103. if !iam.isEnabled() {
  104. return f
  105. }
  106. return func(w http.ResponseWriter, r *http.Request) {
  107. errCode := iam.authRequest(r, action)
  108. if errCode == s3err.ErrNone {
  109. f(w, r)
  110. return
  111. }
  112. writeErrorResponse(w, errCode, r.URL)
  113. }
  114. }
  115. // check whether the request has valid access keys
  116. func (iam *IdentityAccessManagement) authRequest(r *http.Request, action Action) s3err.ErrorCode {
  117. var identity *Identity
  118. var s3Err s3err.ErrorCode
  119. var found bool
  120. switch getRequestAuthType(r) {
  121. case authTypeStreamingSigned:
  122. return s3err.ErrNone
  123. case authTypeUnknown:
  124. glog.V(3).Infof("unknown auth type")
  125. return s3err.ErrAccessDenied
  126. case authTypePresignedV2, authTypeSignedV2:
  127. glog.V(3).Infof("v2 auth type")
  128. identity, s3Err = iam.isReqAuthenticatedV2(r)
  129. case authTypeSigned, authTypePresigned:
  130. glog.V(3).Infof("v4 auth type")
  131. identity, s3Err = iam.reqSignatureV4Verify(r)
  132. case authTypePostPolicy:
  133. glog.V(3).Infof("post policy auth type")
  134. return s3err.ErrNone
  135. case authTypeJWT:
  136. glog.V(3).Infof("jwt auth type")
  137. return s3err.ErrNotImplemented
  138. case authTypeAnonymous:
  139. identity, found = iam.lookupAnonymous()
  140. if !found {
  141. return s3err.ErrAccessDenied
  142. }
  143. default:
  144. return s3err.ErrNotImplemented
  145. }
  146. glog.V(3).Infof("auth error: %v", s3Err)
  147. if s3Err != s3err.ErrNone {
  148. return s3Err
  149. }
  150. glog.V(3).Infof("user name: %v actions: %v", identity.Name, identity.Actions)
  151. bucket, _ := getBucketAndObject(r)
  152. if !identity.canDo(action, bucket) {
  153. return s3err.ErrAccessDenied
  154. }
  155. return s3err.ErrNone
  156. }
  157. func (identity *Identity) canDo(action Action, bucket string) bool {
  158. for _, a := range identity.Actions {
  159. if a == "Admin" {
  160. return true
  161. }
  162. }
  163. for _, a := range identity.Actions {
  164. if a == action {
  165. return true
  166. }
  167. }
  168. if bucket == "" {
  169. return false
  170. }
  171. limitedByBucket := string(action) + ":" + bucket
  172. for _, a := range identity.Actions {
  173. if string(a) == limitedByBucket {
  174. return true
  175. }
  176. }
  177. return false
  178. }