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.

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