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.

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