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.

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