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.

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