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.

356 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. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  14. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  15. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  16. )
  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. Credentials []*Credential
  30. Actions []Action
  31. }
  32. type Credential struct {
  33. AccessKey string
  34. SecretKey string
  35. }
  36. func (action Action) isAdmin() bool {
  37. return strings.HasPrefix(string(action), s3_constants.ACTION_ADMIN)
  38. }
  39. func (action Action) isOwner(bucket string) bool {
  40. return string(action) == s3_constants.ACTION_ADMIN+":"+bucket
  41. }
  42. func (action Action) overBucket(bucket string) bool {
  43. return strings.HasSuffix(string(action), ":"+bucket) || strings.HasSuffix(string(action), ":*")
  44. }
  45. func (action Action) getPermission() Permission {
  46. switch act := strings.Split(string(action), ":")[0]; act {
  47. case s3_constants.ACTION_ADMIN:
  48. return Permission("FULL_CONTROL")
  49. case s3_constants.ACTION_WRITE:
  50. return Permission("WRITE")
  51. case s3_constants.ACTION_READ:
  52. return Permission("READ")
  53. default:
  54. return Permission("")
  55. }
  56. }
  57. func NewIdentityAccessManagement(option *S3ApiServerOption) *IdentityAccessManagement {
  58. iam := &IdentityAccessManagement{
  59. domain: option.DomainName,
  60. }
  61. if option.Config != "" {
  62. if err := iam.loadS3ApiConfigurationFromFile(option.Config); err != nil {
  63. glog.Fatalf("fail to load config file %s: %v", option.Config, err)
  64. }
  65. } else {
  66. if err := iam.loadS3ApiConfigurationFromFiler(option); err != nil {
  67. glog.Warningf("fail to load config: %v", err)
  68. }
  69. }
  70. return iam
  71. }
  72. func (iam *IdentityAccessManagement) loadS3ApiConfigurationFromFiler(option *S3ApiServerOption) (err error) {
  73. var content []byte
  74. err = pb.WithFilerClient(false, option.Filer, option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  75. content, err = filer.ReadInsideFiler(client, filer.IamConfigDirecotry, filer.IamIdentityFile)
  76. return err
  77. })
  78. if err != nil {
  79. return fmt.Errorf("read S3 config: %v", err)
  80. }
  81. return iam.LoadS3ApiConfigurationFromBytes(content)
  82. }
  83. func (iam *IdentityAccessManagement) loadS3ApiConfigurationFromFile(fileName string) error {
  84. content, readErr := os.ReadFile(fileName)
  85. if readErr != nil {
  86. glog.Warningf("fail to read %s : %v", fileName, readErr)
  87. return fmt.Errorf("fail to read %s : %v", fileName, readErr)
  88. }
  89. return iam.LoadS3ApiConfigurationFromBytes(content)
  90. }
  91. func (iam *IdentityAccessManagement) LoadS3ApiConfigurationFromBytes(content []byte) error {
  92. s3ApiConfiguration := &iam_pb.S3ApiConfiguration{}
  93. if err := filer.ParseS3ConfigurationFromBytes(content, s3ApiConfiguration); err != nil {
  94. glog.Warningf("unmarshal error: %v", err)
  95. return fmt.Errorf("unmarshal error: %v", err)
  96. }
  97. if err := iam.loadS3ApiConfiguration(s3ApiConfiguration); err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func (iam *IdentityAccessManagement) loadS3ApiConfiguration(config *iam_pb.S3ApiConfiguration) error {
  103. var identities []*Identity
  104. for _, ident := range config.Identities {
  105. t := &Identity{
  106. Name: ident.Name,
  107. Credentials: nil,
  108. Actions: nil,
  109. }
  110. for _, action := range ident.Actions {
  111. t.Actions = append(t.Actions, Action(action))
  112. }
  113. for _, cred := range ident.Credentials {
  114. t.Credentials = append(t.Credentials, &Credential{
  115. AccessKey: cred.AccessKey,
  116. SecretKey: cred.SecretKey,
  117. })
  118. }
  119. identities = append(identities, t)
  120. }
  121. iam.m.Lock()
  122. // atomically switch
  123. iam.identities = identities
  124. if !iam.isAuthEnabled { // one-directional, no toggling
  125. iam.isAuthEnabled = len(identities) > 0
  126. }
  127. iam.m.Unlock()
  128. return nil
  129. }
  130. func (iam *IdentityAccessManagement) isEnabled() bool {
  131. return iam.isAuthEnabled
  132. }
  133. func (iam *IdentityAccessManagement) lookupByAccessKey(accessKey string) (identity *Identity, cred *Credential, found bool) {
  134. iam.m.RLock()
  135. defer iam.m.RUnlock()
  136. for _, ident := range iam.identities {
  137. for _, cred := range ident.Credentials {
  138. // println("checking", ident.Name, cred.AccessKey)
  139. if cred.AccessKey == accessKey {
  140. return ident, cred, true
  141. }
  142. }
  143. }
  144. glog.V(1).Infof("could not find accessKey %s", accessKey)
  145. return nil, nil, false
  146. }
  147. func (iam *IdentityAccessManagement) lookupAnonymous() (identity *Identity, found bool) {
  148. iam.m.RLock()
  149. defer iam.m.RUnlock()
  150. for _, ident := range iam.identities {
  151. if ident.Name == "anonymous" {
  152. return ident, true
  153. }
  154. }
  155. return nil, false
  156. }
  157. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  158. if !iam.isEnabled() {
  159. return f
  160. }
  161. return func(w http.ResponseWriter, r *http.Request) {
  162. identity, errCode := iam.authRequest(r, action)
  163. if errCode == s3err.ErrNone {
  164. if identity != nil && identity.Name != "" {
  165. r.Header.Set(xhttp.AmzIdentityId, identity.Name)
  166. if identity.isAdmin() {
  167. r.Header.Set(xhttp.AmzIsAdmin, "true")
  168. } else if _, ok := r.Header[xhttp.AmzIsAdmin]; ok {
  169. r.Header.Del(xhttp.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(xhttp.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(xhttp.AmzAuthType, "PostPolicy")
  202. return identity, s3err.ErrNone
  203. case authTypeJWT:
  204. glog.V(3).Infof("jwt auth type")
  205. r.Header.Set(xhttp.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(xhttp.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(xhttp.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 := xhttp.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(xhttp.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(xhttp.AmzAuthType, "PostPolicy")
  253. return identity, s3err.ErrNone
  254. case authTypeJWT:
  255. glog.V(3).Infof("jwt auth type")
  256. r.Header.Set(xhttp.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(xhttp.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(xhttp.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. }