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.

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