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.

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