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.

317 lines
8.2 KiB

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