auth_credentials.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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) lookupByAccessKey(accessKey string) (identity *Identity, cred *Credential, found bool) {
  78. for _, ident := range iam.identities {
  79. for _, cred := range ident.Credentials {
  80. if cred.AccessKey == accessKey {
  81. return ident, cred, true
  82. }
  83. }
  84. }
  85. return nil, nil, false
  86. }
  87. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  88. if len(iam.identities) == 0 {
  89. return f
  90. }
  91. return func(w http.ResponseWriter, r *http.Request) {
  92. errCode := iam.authRequest(r, action)
  93. if errCode == ErrNone {
  94. f(w, r)
  95. return
  96. }
  97. writeErrorResponse(w, errCode, r.URL)
  98. }
  99. }
  100. // check whether the request has valid access keys
  101. func (iam *IdentityAccessManagement) authRequest(r *http.Request, action Action) ErrorCode {
  102. var identity *Identity
  103. var s3Err ErrorCode
  104. switch getRequestAuthType(r) {
  105. case authTypeStreamingSigned:
  106. return ErrNone
  107. case authTypeUnknown:
  108. glog.V(3).Infof("unknown auth type")
  109. return ErrAccessDenied
  110. case authTypePresignedV2, authTypeSignedV2:
  111. glog.V(3).Infof("v2 auth type")
  112. identity, s3Err = iam.isReqAuthenticatedV2(r)
  113. case authTypeSigned, authTypePresigned:
  114. glog.V(3).Infof("v4 auth type")
  115. identity, s3Err = iam.reqSignatureV4Verify(r)
  116. case authTypePostPolicy:
  117. glog.V(3).Infof("post policy auth type")
  118. return ErrNotImplemented
  119. case authTypeJWT:
  120. glog.V(3).Infof("jwt auth type")
  121. return ErrNotImplemented
  122. case authTypeAnonymous:
  123. return ErrAccessDenied
  124. default:
  125. return ErrNotImplemented
  126. }
  127. glog.V(3).Infof("auth error: %v", s3Err)
  128. if s3Err != ErrNone {
  129. return s3Err
  130. }
  131. glog.V(3).Infof("user name: %v actions: %v", identity.Name, identity.Actions)
  132. vars := mux.Vars(r)
  133. bucket := vars["bucket"]
  134. if !identity.canDo(action, bucket) {
  135. return ErrAccessDenied
  136. }
  137. return ErrNone
  138. }
  139. func (identity *Identity) canDo(action Action, bucket string) bool {
  140. for _, a := range identity.Actions {
  141. if a == "Admin" {
  142. return true
  143. }
  144. }
  145. for _, a := range identity.Actions {
  146. if a == action {
  147. return true
  148. }
  149. }
  150. if bucket == "" {
  151. return false
  152. }
  153. limitedByBucket := string(action) + ":" + bucket
  154. for _, a := range identity.Actions {
  155. if string(a) == limitedByBucket {
  156. return true
  157. }
  158. }
  159. return false
  160. }