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.

325 lines
8.4 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
3 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 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
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
3 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
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "strings"
  7. "sync"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  13. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  14. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  15. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  16. )
  17. type Action string
  18. type Iam interface {
  19. Check(f http.HandlerFunc, actions ...Action) http.HandlerFunc
  20. }
  21. type IdentityAccessManagement struct {
  22. m sync.Mutex
  23. identities []*Identity
  24. domain string
  25. }
  26. type Identity struct {
  27. Name string
  28. Credentials []*Credential
  29. Actions []Action
  30. }
  31. type Credential struct {
  32. AccessKey string
  33. SecretKey string
  34. }
  35. func (action Action) isAdmin() bool {
  36. return strings.HasPrefix(string(action), s3_constants.ACTION_ADMIN)
  37. }
  38. func (action Action) isOwner(bucket string) bool {
  39. return string(action) == s3_constants.ACTION_ADMIN+":"+bucket
  40. }
  41. func (action Action) overBucket(bucket string) bool {
  42. return strings.HasSuffix(string(action), ":"+bucket) || strings.HasSuffix(string(action), ":*")
  43. }
  44. func (action Action) getPermission() Permission {
  45. switch act := strings.Split(string(action), ":")[0]; act {
  46. case s3_constants.ACTION_ADMIN:
  47. return Permission("FULL_CONTROL")
  48. case s3_constants.ACTION_WRITE:
  49. return Permission("WRITE")
  50. case s3_constants.ACTION_READ:
  51. return Permission("READ")
  52. default:
  53. return Permission("")
  54. }
  55. }
  56. func NewIdentityAccessManagement(option *S3ApiServerOption) *IdentityAccessManagement {
  57. iam := &IdentityAccessManagement{
  58. domain: option.DomainName,
  59. }
  60. if option.Config != "" {
  61. if err := iam.loadS3ApiConfigurationFromFile(option.Config); err != nil {
  62. glog.Fatalf("fail to load config file %s: %v", option.Config, err)
  63. }
  64. } else {
  65. if err := iam.loadS3ApiConfigurationFromFiler(option); err != nil {
  66. glog.Warningf("fail to load config: %v", err)
  67. }
  68. }
  69. return iam
  70. }
  71. func (iam *IdentityAccessManagement) loadS3ApiConfigurationFromFiler(option *S3ApiServerOption) (err error) {
  72. var content []byte
  73. err = pb.WithFilerClient(option.Filer, option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  74. content, err = filer.ReadInsideFiler(client, filer.IamConfigDirecotry, filer.IamIdentityFile)
  75. return err
  76. })
  77. if err != nil {
  78. return fmt.Errorf("read S3 config: %v", err)
  79. }
  80. return iam.loadS3ApiConfigurationFromBytes(content)
  81. }
  82. func (iam *IdentityAccessManagement) loadS3ApiConfigurationFromFile(fileName string) error {
  83. content, readErr := os.ReadFile(fileName)
  84. if readErr != nil {
  85. glog.Warningf("fail to read %s : %v", fileName, readErr)
  86. return fmt.Errorf("fail to read %s : %v", fileName, readErr)
  87. }
  88. return iam.loadS3ApiConfigurationFromBytes(content)
  89. }
  90. func (iam *IdentityAccessManagement) loadS3ApiConfigurationFromBytes(content []byte) error {
  91. s3ApiConfiguration := &iam_pb.S3ApiConfiguration{}
  92. if err := filer.ParseS3ConfigurationFromBytes(content, s3ApiConfiguration); err != nil {
  93. glog.Warningf("unmarshal error: %v", err)
  94. return fmt.Errorf("unmarshal error: %v", err)
  95. }
  96. if err := iam.loadS3ApiConfiguration(s3ApiConfiguration); err != nil {
  97. return err
  98. }
  99. return nil
  100. }
  101. func (iam *IdentityAccessManagement) loadS3ApiConfiguration(config *iam_pb.S3ApiConfiguration) error {
  102. var identities []*Identity
  103. for _, ident := range config.Identities {
  104. t := &Identity{
  105. Name: ident.Name,
  106. Credentials: nil,
  107. Actions: nil,
  108. }
  109. for _, action := range ident.Actions {
  110. t.Actions = append(t.Actions, Action(action))
  111. }
  112. for _, cred := range ident.Credentials {
  113. t.Credentials = append(t.Credentials, &Credential{
  114. AccessKey: cred.AccessKey,
  115. SecretKey: cred.SecretKey,
  116. })
  117. }
  118. identities = append(identities, t)
  119. }
  120. iam.m.Lock()
  121. // atomically switch
  122. iam.identities = identities
  123. iam.m.Unlock()
  124. return nil
  125. }
  126. func (iam *IdentityAccessManagement) isEnabled() bool {
  127. return len(iam.identities) > 0
  128. }
  129. func (iam *IdentityAccessManagement) lookupByAccessKey(accessKey string) (identity *Identity, cred *Credential, found bool) {
  130. for _, ident := range iam.identities {
  131. for _, cred := range ident.Credentials {
  132. // println("checking", ident.Name, cred.AccessKey)
  133. if cred.AccessKey == accessKey {
  134. return ident, cred, true
  135. }
  136. }
  137. }
  138. glog.V(1).Infof("could not find accessKey %s", accessKey)
  139. return nil, nil, false
  140. }
  141. func (iam *IdentityAccessManagement) lookupAnonymous() (identity *Identity, found bool) {
  142. for _, ident := range iam.identities {
  143. if ident.Name == "anonymous" {
  144. return ident, true
  145. }
  146. }
  147. return nil, false
  148. }
  149. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  150. if !iam.isEnabled() {
  151. return f
  152. }
  153. return func(w http.ResponseWriter, r *http.Request) {
  154. identity, errCode := iam.authRequest(r, action)
  155. if errCode == s3err.ErrNone {
  156. if identity != nil && identity.Name != "" {
  157. r.Header.Set(xhttp.AmzIdentityId, identity.Name)
  158. if identity.isAdmin() {
  159. r.Header.Set(xhttp.AmzIsAdmin, "true")
  160. }
  161. }
  162. f(w, r)
  163. return
  164. }
  165. s3err.WriteErrorResponse(w, r, errCode)
  166. }
  167. }
  168. // check whether the request has valid access keys
  169. func (iam *IdentityAccessManagement) authRequest(r *http.Request, action Action) (*Identity, s3err.ErrorCode) {
  170. var identity *Identity
  171. var s3Err s3err.ErrorCode
  172. var found bool
  173. switch getRequestAuthType(r) {
  174. case authTypeStreamingSigned:
  175. return identity, s3err.ErrNone
  176. case authTypeUnknown:
  177. glog.V(3).Infof("unknown auth type")
  178. return identity, s3err.ErrAccessDenied
  179. case authTypePresignedV2, authTypeSignedV2:
  180. glog.V(3).Infof("v2 auth type")
  181. identity, s3Err = iam.isReqAuthenticatedV2(r)
  182. case authTypeSigned, authTypePresigned:
  183. glog.V(3).Infof("v4 auth type")
  184. identity, s3Err = iam.reqSignatureV4Verify(r)
  185. case authTypePostPolicy:
  186. glog.V(3).Infof("post policy auth type")
  187. return identity, s3err.ErrNone
  188. case authTypeJWT:
  189. glog.V(3).Infof("jwt auth type")
  190. return identity, s3err.ErrNotImplemented
  191. case authTypeAnonymous:
  192. identity, found = iam.lookupAnonymous()
  193. if !found {
  194. return identity, s3err.ErrAccessDenied
  195. }
  196. default:
  197. return identity, s3err.ErrNotImplemented
  198. }
  199. if s3Err != s3err.ErrNone {
  200. return identity, s3Err
  201. }
  202. glog.V(3).Infof("user name: %v actions: %v, action: %v", identity.Name, identity.Actions, action)
  203. bucket, _ := getBucketAndObject(r)
  204. if !identity.canDo(action, bucket) {
  205. return identity, s3err.ErrAccessDenied
  206. }
  207. return identity, s3err.ErrNone
  208. }
  209. func (iam *IdentityAccessManagement) authUser(r *http.Request) (*Identity, s3err.ErrorCode) {
  210. var identity *Identity
  211. var s3Err s3err.ErrorCode
  212. var found bool
  213. switch getRequestAuthType(r) {
  214. case authTypeStreamingSigned:
  215. return identity, s3err.ErrNone
  216. case authTypeUnknown:
  217. glog.V(3).Infof("unknown auth type")
  218. return identity, s3err.ErrAccessDenied
  219. case authTypePresignedV2, authTypeSignedV2:
  220. glog.V(3).Infof("v2 auth type")
  221. identity, s3Err = iam.isReqAuthenticatedV2(r)
  222. case authTypeSigned, authTypePresigned:
  223. glog.V(3).Infof("v4 auth type")
  224. identity, s3Err = iam.reqSignatureV4Verify(r)
  225. case authTypePostPolicy:
  226. glog.V(3).Infof("post policy auth type")
  227. return identity, s3err.ErrNone
  228. case authTypeJWT:
  229. glog.V(3).Infof("jwt auth type")
  230. return identity, s3err.ErrNotImplemented
  231. case authTypeAnonymous:
  232. identity, found = iam.lookupAnonymous()
  233. if !found {
  234. return identity, s3err.ErrAccessDenied
  235. }
  236. default:
  237. return identity, s3err.ErrNotImplemented
  238. }
  239. glog.V(3).Infof("auth error: %v", s3Err)
  240. if s3Err != s3err.ErrNone {
  241. return identity, s3Err
  242. }
  243. return identity, s3err.ErrNone
  244. }
  245. func (identity *Identity) canDo(action Action, bucket string) bool {
  246. if identity.isAdmin() {
  247. return true
  248. }
  249. for _, a := range identity.Actions {
  250. if a == action {
  251. return true
  252. }
  253. }
  254. if bucket == "" {
  255. return false
  256. }
  257. limitedByBucket := string(action) + ":" + bucket
  258. adminLimitedByBucket := s3_constants.ACTION_ADMIN + ":" + bucket
  259. for _, a := range identity.Actions {
  260. act := string(a)
  261. if strings.HasSuffix(act, "*") {
  262. if strings.HasPrefix(limitedByBucket, act[:len(act)-1]) {
  263. return true
  264. }
  265. if strings.HasPrefix(adminLimitedByBucket, act[:len(act)-1]) {
  266. return true
  267. }
  268. } else {
  269. if act == limitedByBucket {
  270. return true
  271. }
  272. if act == adminLimitedByBucket {
  273. return true
  274. }
  275. }
  276. }
  277. return false
  278. }
  279. func (identity *Identity) isAdmin() bool {
  280. for _, a := range identity.Actions {
  281. if a == "Admin" {
  282. return true
  283. }
  284. }
  285. return false
  286. }