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