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.

355 lines
9.5 KiB

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