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.

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