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.

319 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
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. "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. // println("checking", ident.Name, cred.AccessKey)
  129. if cred.AccessKey == accessKey {
  130. return ident, cred, true
  131. }
  132. }
  133. }
  134. glog.V(1).Infof("could not find accessKey %s", accessKey)
  135. return nil, nil, false
  136. }
  137. func (iam *IdentityAccessManagement) lookupAnonymous() (identity *Identity, found bool) {
  138. for _, ident := range iam.identities {
  139. if ident.Name == "anonymous" {
  140. return ident, true
  141. }
  142. }
  143. return nil, false
  144. }
  145. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  146. if !iam.isEnabled() {
  147. return f
  148. }
  149. return func(w http.ResponseWriter, r *http.Request) {
  150. identity, errCode := iam.authRequest(r, action)
  151. if errCode == s3err.ErrNone {
  152. if identity != nil && identity.Name != "" {
  153. r.Header.Set(xhttp.AmzIdentityId, identity.Name)
  154. if identity.isAdmin() {
  155. r.Header.Set(xhttp.AmzIsAdmin, "true")
  156. }
  157. }
  158. f(w, r)
  159. return
  160. }
  161. s3err.WriteErrorResponse(w, r, errCode)
  162. }
  163. }
  164. // check whether the request has valid access keys
  165. func (iam *IdentityAccessManagement) authRequest(r *http.Request, action Action) (*Identity, s3err.ErrorCode) {
  166. var identity *Identity
  167. var s3Err s3err.ErrorCode
  168. var found bool
  169. switch getRequestAuthType(r) {
  170. case authTypeStreamingSigned:
  171. return identity, s3err.ErrNone
  172. case authTypeUnknown:
  173. glog.V(3).Infof("unknown auth type")
  174. return identity, s3err.ErrAccessDenied
  175. case authTypePresignedV2, authTypeSignedV2:
  176. glog.V(3).Infof("v2 auth type")
  177. identity, s3Err = iam.isReqAuthenticatedV2(r)
  178. case authTypeSigned, authTypePresigned:
  179. glog.V(3).Infof("v4 auth type")
  180. identity, s3Err = iam.reqSignatureV4Verify(r)
  181. case authTypePostPolicy:
  182. glog.V(3).Infof("post policy auth type")
  183. return identity, s3err.ErrNone
  184. case authTypeJWT:
  185. glog.V(3).Infof("jwt auth type")
  186. return identity, s3err.ErrNotImplemented
  187. case authTypeAnonymous:
  188. identity, found = iam.lookupAnonymous()
  189. if !found {
  190. return identity, s3err.ErrAccessDenied
  191. }
  192. default:
  193. return identity, s3err.ErrNotImplemented
  194. }
  195. if s3Err != s3err.ErrNone {
  196. return identity, s3Err
  197. }
  198. glog.V(3).Infof("user name: %v actions: %v, action: %v", identity.Name, identity.Actions, action)
  199. bucket, _ := getBucketAndObject(r)
  200. if !identity.canDo(action, bucket) {
  201. return identity, s3err.ErrAccessDenied
  202. }
  203. return identity, s3err.ErrNone
  204. }
  205. func (iam *IdentityAccessManagement) authUser(r *http.Request) (*Identity, s3err.ErrorCode) {
  206. var identity *Identity
  207. var s3Err s3err.ErrorCode
  208. var found bool
  209. switch getRequestAuthType(r) {
  210. case authTypeStreamingSigned:
  211. return identity, s3err.ErrNone
  212. case authTypeUnknown:
  213. glog.V(3).Infof("unknown auth type")
  214. return identity, s3err.ErrAccessDenied
  215. case authTypePresignedV2, authTypeSignedV2:
  216. glog.V(3).Infof("v2 auth type")
  217. identity, s3Err = iam.isReqAuthenticatedV2(r)
  218. case authTypeSigned, authTypePresigned:
  219. glog.V(3).Infof("v4 auth type")
  220. identity, s3Err = iam.reqSignatureV4Verify(r)
  221. case authTypePostPolicy:
  222. glog.V(3).Infof("post policy auth type")
  223. return identity, s3err.ErrNone
  224. case authTypeJWT:
  225. glog.V(3).Infof("jwt auth type")
  226. return identity, s3err.ErrNotImplemented
  227. case authTypeAnonymous:
  228. identity, found = iam.lookupAnonymous()
  229. if !found {
  230. return identity, s3err.ErrAccessDenied
  231. }
  232. default:
  233. return identity, s3err.ErrNotImplemented
  234. }
  235. glog.V(3).Infof("auth error: %v", s3Err)
  236. if s3Err != s3err.ErrNone {
  237. return identity, s3Err
  238. }
  239. return identity, s3err.ErrNone
  240. }
  241. func (identity *Identity) canDo(action Action, bucket string) bool {
  242. if identity.isAdmin() {
  243. return true
  244. }
  245. for _, a := range identity.Actions {
  246. if a == action {
  247. return true
  248. }
  249. }
  250. if bucket == "" {
  251. return false
  252. }
  253. limitedByBucket := string(action) + ":" + bucket
  254. adminLimitedByBucket := s3_constants.ACTION_ADMIN + ":" + bucket
  255. for _, a := range identity.Actions {
  256. act := string(a)
  257. if strings.HasSuffix(act, "*") {
  258. if strings.HasPrefix(limitedByBucket, act[:len(act)-1]) {
  259. return true
  260. }
  261. if strings.HasPrefix(adminLimitedByBucket, act[:len(act)-1]) {
  262. return true
  263. }
  264. } else {
  265. if act == limitedByBucket {
  266. return true
  267. }
  268. if act == adminLimitedByBucket {
  269. return true
  270. }
  271. }
  272. }
  273. return false
  274. }
  275. func (identity *Identity) isAdmin() bool {
  276. for _, a := range identity.Actions {
  277. if a == "Admin" {
  278. return true
  279. }
  280. }
  281. return false
  282. }