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.

414 lines
12 KiB

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
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
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. "github.com/seaweedfs/seaweedfs/weed/s3api/s3account"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "sync"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/pb"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  15. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  16. )
  17. var IdentityAnonymous = &Identity{
  18. Name: s3account.AccountAnonymous.Name,
  19. AccountId: s3account.AccountAnonymous.Id,
  20. }
  21. type Action string
  22. type Iam interface {
  23. Check(f http.HandlerFunc, actions ...Action) http.HandlerFunc
  24. }
  25. type IdentityAccessManagement struct {
  26. m sync.RWMutex
  27. identities []*Identity
  28. isAuthEnabled bool
  29. domain string
  30. }
  31. type Identity struct {
  32. Name string
  33. AccountId string
  34. Credentials []*Credential
  35. Actions []Action
  36. }
  37. func (i *Identity) isAnonymous() bool {
  38. return i.Name == s3account.AccountAnonymous.Name
  39. }
  40. type Credential struct {
  41. AccessKey string
  42. SecretKey string
  43. }
  44. func (action Action) isAdmin() bool {
  45. return strings.HasPrefix(string(action), s3_constants.ACTION_ADMIN)
  46. }
  47. func (action Action) isOwner(bucket string) bool {
  48. return string(action) == s3_constants.ACTION_ADMIN+":"+bucket
  49. }
  50. func (action Action) overBucket(bucket string) bool {
  51. return strings.HasSuffix(string(action), ":"+bucket) || strings.HasSuffix(string(action), ":*")
  52. }
  53. func (action Action) getPermission() Permission {
  54. switch act := strings.Split(string(action), ":")[0]; act {
  55. case s3_constants.ACTION_ADMIN:
  56. return Permission("FULL_CONTROL")
  57. case s3_constants.ACTION_WRITE:
  58. return Permission("WRITE")
  59. case s3_constants.ACTION_READ:
  60. return Permission("READ")
  61. default:
  62. return Permission("")
  63. }
  64. }
  65. func NewIdentityAccessManagement(option *S3ApiServerOption) *IdentityAccessManagement {
  66. iam := &IdentityAccessManagement{
  67. domain: option.DomainName,
  68. }
  69. if option.Config != "" {
  70. if err := iam.loadS3ApiConfigurationFromFile(option.Config); err != nil {
  71. glog.Fatalf("fail to load config file %s: %v", option.Config, err)
  72. }
  73. } else {
  74. if err := iam.loadS3ApiConfigurationFromFiler(option); err != nil {
  75. glog.Warningf("fail to load config: %v", err)
  76. }
  77. }
  78. return iam
  79. }
  80. func (iam *IdentityAccessManagement) loadS3ApiConfigurationFromFiler(option *S3ApiServerOption) (err error) {
  81. var content []byte
  82. err = pb.WithFilerClient(false, 0, option.Filer, option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  83. content, err = filer.ReadInsideFiler(client, filer.IamConfigDirectory, filer.IamIdentityFile)
  84. return err
  85. })
  86. if err != nil {
  87. return fmt.Errorf("read S3 config: %v", err)
  88. }
  89. return iam.LoadS3ApiConfigurationFromBytes(content)
  90. }
  91. func (iam *IdentityAccessManagement) loadS3ApiConfigurationFromFile(fileName string) error {
  92. content, readErr := os.ReadFile(fileName)
  93. if readErr != nil {
  94. glog.Warningf("fail to read %s : %v", fileName, readErr)
  95. return fmt.Errorf("fail to read %s : %v", fileName, readErr)
  96. }
  97. return iam.LoadS3ApiConfigurationFromBytes(content)
  98. }
  99. func (iam *IdentityAccessManagement) LoadS3ApiConfigurationFromBytes(content []byte) error {
  100. s3ApiConfiguration := &iam_pb.S3ApiConfiguration{}
  101. if err := filer.ParseS3ConfigurationFromBytes(content, s3ApiConfiguration); err != nil {
  102. glog.Warningf("unmarshal error: %v", err)
  103. return fmt.Errorf("unmarshal error: %v", err)
  104. }
  105. if err := filer.CheckDuplicateAccessKey(s3ApiConfiguration); err != nil {
  106. return err
  107. }
  108. if err := iam.loadS3ApiConfiguration(s3ApiConfiguration); err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. func (iam *IdentityAccessManagement) loadS3ApiConfiguration(config *iam_pb.S3ApiConfiguration) error {
  114. var identities []*Identity
  115. for _, ident := range config.Identities {
  116. t := &Identity{
  117. Name: ident.Name,
  118. AccountId: s3account.AccountAdmin.Id,
  119. Credentials: nil,
  120. Actions: nil,
  121. }
  122. if ident.Name == s3account.AccountAnonymous.Name {
  123. if ident.AccountId != "" && ident.AccountId != s3account.AccountAnonymous.Id {
  124. glog.Warningf("anonymous identity is associated with a non-anonymous account ID, the association is invalid")
  125. }
  126. t.AccountId = s3account.AccountAnonymous.Id
  127. IdentityAnonymous = t
  128. } else {
  129. if len(ident.AccountId) > 0 {
  130. t.AccountId = ident.AccountId
  131. }
  132. }
  133. for _, action := range ident.Actions {
  134. t.Actions = append(t.Actions, Action(action))
  135. }
  136. for _, cred := range ident.Credentials {
  137. t.Credentials = append(t.Credentials, &Credential{
  138. AccessKey: cred.AccessKey,
  139. SecretKey: cred.SecretKey,
  140. })
  141. }
  142. identities = append(identities, t)
  143. }
  144. iam.m.Lock()
  145. // atomically switch
  146. iam.identities = identities
  147. if !iam.isAuthEnabled { // one-directional, no toggling
  148. iam.isAuthEnabled = len(identities) > 0
  149. }
  150. iam.m.Unlock()
  151. return nil
  152. }
  153. func (iam *IdentityAccessManagement) isEnabled() bool {
  154. return iam.isAuthEnabled
  155. }
  156. func (iam *IdentityAccessManagement) lookupByAccessKey(accessKey string) (identity *Identity, cred *Credential, found bool) {
  157. iam.m.RLock()
  158. defer iam.m.RUnlock()
  159. for _, ident := range iam.identities {
  160. for _, cred := range ident.Credentials {
  161. // println("checking", ident.Name, cred.AccessKey)
  162. if cred.AccessKey == accessKey {
  163. return ident, cred, true
  164. }
  165. }
  166. }
  167. glog.V(1).Infof("could not find accessKey %s", accessKey)
  168. return nil, nil, false
  169. }
  170. func (iam *IdentityAccessManagement) lookupAnonymous() (identity *Identity, found bool) {
  171. iam.m.RLock()
  172. defer iam.m.RUnlock()
  173. for _, ident := range iam.identities {
  174. if ident.isAnonymous() {
  175. return ident, true
  176. }
  177. }
  178. return nil, false
  179. }
  180. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  181. return Auth(iam, nil, f, action, false)
  182. }
  183. func (s3a *S3ApiServer) Auth(f http.HandlerFunc, action Action, supportAcl bool) http.HandlerFunc {
  184. return Auth(s3a.iam, s3a.bucketRegistry, f, action, supportAcl)
  185. }
  186. func Auth(iam *IdentityAccessManagement, br *BucketRegistry, f http.HandlerFunc, action Action, supportAcl bool) http.HandlerFunc {
  187. return func(w http.ResponseWriter, r *http.Request) {
  188. //unset predefined headers
  189. delete(r.Header, s3_constants.AmzAccountId)
  190. delete(r.Header, s3_constants.ExtAmzOwnerKey)
  191. delete(r.Header, s3_constants.ExtAmzAclKey)
  192. if !iam.isEnabled() {
  193. f(w, r)
  194. return
  195. }
  196. identity, errCode := authRequest(iam, br, r, action, supportAcl)
  197. if errCode == s3err.ErrNone {
  198. if identity != nil && identity.Name != "" {
  199. r.Header.Set(s3_constants.AmzIdentityId, identity.Name)
  200. if identity.isAdmin() {
  201. r.Header.Set(s3_constants.AmzIsAdmin, "true")
  202. } else if _, ok := r.Header[s3_constants.AmzIsAdmin]; ok {
  203. r.Header.Del(s3_constants.AmzIsAdmin)
  204. }
  205. }
  206. f(w, r)
  207. return
  208. }
  209. s3err.WriteErrorResponse(w, r, errCode)
  210. }
  211. }
  212. func (iam *IdentityAccessManagement) authRequest(r *http.Request, action Action) (*Identity, s3err.ErrorCode) {
  213. return authRequest(iam, nil, r, action, false)
  214. }
  215. func authRequest(iam *IdentityAccessManagement, br *BucketRegistry, r *http.Request, action Action, supportAcl bool) (*Identity, s3err.ErrorCode) {
  216. var identity *Identity
  217. var s3Err s3err.ErrorCode
  218. var authType string
  219. switch getRequestAuthType(r) {
  220. case authTypeStreamingSigned:
  221. return identity, s3err.ErrNone
  222. case authTypeUnknown:
  223. glog.V(3).Infof("unknown auth type")
  224. r.Header.Set(s3_constants.AmzAuthType, "Unknown")
  225. return identity, s3err.ErrAccessDenied
  226. case authTypePresignedV2, authTypeSignedV2:
  227. glog.V(3).Infof("v2 auth type")
  228. identity, s3Err = iam.isReqAuthenticatedV2(r)
  229. authType = "SigV2"
  230. case authTypeSigned, authTypePresigned:
  231. glog.V(3).Infof("v4 auth type")
  232. identity, s3Err = iam.reqSignatureV4Verify(r)
  233. authType = "SigV4"
  234. case authTypePostPolicy:
  235. glog.V(3).Infof("post policy auth type")
  236. r.Header.Set(s3_constants.AmzAuthType, "PostPolicy")
  237. return identity, s3err.ErrNone
  238. case authTypeJWT:
  239. glog.V(3).Infof("jwt auth type")
  240. r.Header.Set(s3_constants.AmzAuthType, "Jwt")
  241. return identity, s3err.ErrNotImplemented
  242. case authTypeAnonymous:
  243. if supportAcl && br != nil {
  244. bucket, _ := s3_constants.GetBucketAndObject(r)
  245. bucketMetadata, errorCode := br.GetBucketMetadata(bucket)
  246. if errorCode != s3err.ErrNone {
  247. return nil, errorCode
  248. }
  249. if bucketMetadata.ObjectOwnership != s3_constants.OwnershipBucketOwnerEnforced {
  250. return IdentityAnonymous, s3err.ErrNone
  251. }
  252. }
  253. authType = "Anonymous"
  254. identity = IdentityAnonymous
  255. if len(identity.Actions) == 0 {
  256. r.Header.Set(s3_constants.AmzAuthType, authType)
  257. return identity, s3err.ErrAccessDenied
  258. }
  259. default:
  260. return identity, s3err.ErrNotImplemented
  261. }
  262. if len(authType) > 0 {
  263. r.Header.Set(s3_constants.AmzAuthType, authType)
  264. }
  265. if s3Err != s3err.ErrNone {
  266. return identity, s3Err
  267. }
  268. glog.V(3).Infof("user name: %v account id: %v actions: %v, action: %v", identity.Name, identity.AccountId, identity.Actions, action)
  269. bucket, object := s3_constants.GetBucketAndObject(r)
  270. if !identity.canDo(action, bucket, object) {
  271. return identity, s3err.ErrAccessDenied
  272. }
  273. if !identity.isAnonymous() {
  274. r.Header.Set(s3_constants.AmzAccountId, identity.AccountId)
  275. }
  276. return identity, s3err.ErrNone
  277. }
  278. func (iam *IdentityAccessManagement) authUser(r *http.Request) (*Identity, s3err.ErrorCode) {
  279. var identity *Identity
  280. var s3Err s3err.ErrorCode
  281. var found bool
  282. var authType string
  283. switch getRequestAuthType(r) {
  284. case authTypeStreamingSigned:
  285. return identity, s3err.ErrNone
  286. case authTypeUnknown:
  287. glog.V(3).Infof("unknown auth type")
  288. r.Header.Set(s3_constants.AmzAuthType, "Unknown")
  289. return identity, s3err.ErrAccessDenied
  290. case authTypePresignedV2, authTypeSignedV2:
  291. glog.V(3).Infof("v2 auth type")
  292. identity, s3Err = iam.isReqAuthenticatedV2(r)
  293. authType = "SigV2"
  294. case authTypeSigned, authTypePresigned:
  295. glog.V(3).Infof("v4 auth type")
  296. identity, s3Err = iam.reqSignatureV4Verify(r)
  297. authType = "SigV4"
  298. case authTypePostPolicy:
  299. glog.V(3).Infof("post policy auth type")
  300. r.Header.Set(s3_constants.AmzAuthType, "PostPolicy")
  301. return identity, s3err.ErrNone
  302. case authTypeJWT:
  303. glog.V(3).Infof("jwt auth type")
  304. r.Header.Set(s3_constants.AmzAuthType, "Jwt")
  305. return identity, s3err.ErrNotImplemented
  306. case authTypeAnonymous:
  307. authType = "Anonymous"
  308. identity, found = iam.lookupAnonymous()
  309. if !found {
  310. r.Header.Set(s3_constants.AmzAuthType, authType)
  311. return identity, s3err.ErrAccessDenied
  312. }
  313. default:
  314. return identity, s3err.ErrNotImplemented
  315. }
  316. if len(authType) > 0 {
  317. r.Header.Set(s3_constants.AmzAuthType, authType)
  318. }
  319. glog.V(3).Infof("auth error: %v", s3Err)
  320. if s3Err != s3err.ErrNone {
  321. return identity, s3Err
  322. }
  323. return identity, s3err.ErrNone
  324. }
  325. func (identity *Identity) canDo(action Action, bucket string, objectKey string) bool {
  326. if identity.isAdmin() {
  327. return true
  328. }
  329. for _, a := range identity.Actions {
  330. if a == action {
  331. return true
  332. }
  333. }
  334. if bucket == "" {
  335. return false
  336. }
  337. target := string(action) + ":" + bucket + objectKey
  338. adminTarget := s3_constants.ACTION_ADMIN + ":" + bucket + objectKey
  339. limitedByBucket := string(action) + ":" + bucket
  340. adminLimitedByBucket := s3_constants.ACTION_ADMIN + ":" + bucket
  341. for _, a := range identity.Actions {
  342. act := string(a)
  343. if strings.HasSuffix(act, "*") {
  344. if strings.HasPrefix(target, act[:len(act)-1]) {
  345. return true
  346. }
  347. if strings.HasPrefix(adminTarget, act[:len(act)-1]) {
  348. return true
  349. }
  350. } else {
  351. if act == limitedByBucket {
  352. return true
  353. }
  354. if act == adminLimitedByBucket {
  355. return true
  356. }
  357. }
  358. }
  359. return false
  360. }
  361. func (identity *Identity) isAdmin() bool {
  362. for _, a := range identity.Actions {
  363. if a == "Admin" {
  364. return true
  365. }
  366. }
  367. return false
  368. }