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.

247 lines
6.2 KiB

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