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.

329 lines
8.6 KiB

5 years ago
4 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
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
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. 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(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. iam.m.Unlock()
  124. return nil
  125. }
  126. func (iam *IdentityAccessManagement) isEnabled() bool {
  127. iam.m.RLock()
  128. defer iam.m.RUnlock()
  129. return len(iam.identities) > 0
  130. }
  131. func (iam *IdentityAccessManagement) lookupByAccessKey(accessKey string) (identity *Identity, cred *Credential, found bool) {
  132. iam.m.RLock()
  133. defer iam.m.RUnlock()
  134. for _, ident := range iam.identities {
  135. for _, cred := range ident.Credentials {
  136. // println("checking", ident.Name, cred.AccessKey)
  137. if cred.AccessKey == accessKey {
  138. return ident, cred, true
  139. }
  140. }
  141. }
  142. glog.V(1).Infof("could not find accessKey %s", accessKey)
  143. return nil, nil, false
  144. }
  145. func (iam *IdentityAccessManagement) lookupAnonymous() (identity *Identity, found bool) {
  146. iam.m.RLock()
  147. defer iam.m.RUnlock()
  148. for _, ident := range iam.identities {
  149. if ident.Name == "anonymous" {
  150. return ident, true
  151. }
  152. }
  153. return nil, false
  154. }
  155. func (iam *IdentityAccessManagement) Auth(f http.HandlerFunc, action Action) http.HandlerFunc {
  156. if !iam.isEnabled() {
  157. return f
  158. }
  159. return func(w http.ResponseWriter, r *http.Request) {
  160. identity, errCode := iam.authRequest(r, action)
  161. if errCode == s3err.ErrNone {
  162. if identity != nil && identity.Name != "" {
  163. r.Header.Set(xhttp.AmzIdentityId, identity.Name)
  164. if identity.isAdmin() {
  165. r.Header.Set(xhttp.AmzIsAdmin, "true")
  166. } else if _, ok := r.Header[xhttp.AmzIsAdmin]; ok {
  167. r.Header.Del(xhttp.AmzIsAdmin)
  168. }
  169. }
  170. f(w, r)
  171. return
  172. }
  173. s3err.WriteErrorResponse(w, r, errCode)
  174. }
  175. }
  176. // check whether the request has valid access keys
  177. func (iam *IdentityAccessManagement) authRequest(r *http.Request, action Action) (*Identity, s3err.ErrorCode) {
  178. var identity *Identity
  179. var s3Err s3err.ErrorCode
  180. var found bool
  181. switch getRequestAuthType(r) {
  182. case authTypeStreamingSigned:
  183. return identity, s3err.ErrNone
  184. case authTypeUnknown:
  185. glog.V(3).Infof("unknown auth type")
  186. return identity, s3err.ErrAccessDenied
  187. case authTypePresignedV2, authTypeSignedV2:
  188. glog.V(3).Infof("v2 auth type")
  189. identity, s3Err = iam.isReqAuthenticatedV2(r)
  190. case authTypeSigned, authTypePresigned:
  191. glog.V(3).Infof("v4 auth type")
  192. identity, s3Err = iam.reqSignatureV4Verify(r)
  193. case authTypePostPolicy:
  194. glog.V(3).Infof("post policy auth type")
  195. return identity, s3err.ErrNone
  196. case authTypeJWT:
  197. glog.V(3).Infof("jwt auth type")
  198. return identity, s3err.ErrNotImplemented
  199. case authTypeAnonymous:
  200. identity, found = iam.lookupAnonymous()
  201. if !found {
  202. return identity, s3err.ErrAccessDenied
  203. }
  204. default:
  205. return identity, s3err.ErrNotImplemented
  206. }
  207. if s3Err != s3err.ErrNone {
  208. return identity, s3Err
  209. }
  210. glog.V(3).Infof("user name: %v actions: %v, action: %v", identity.Name, identity.Actions, action)
  211. bucket, _ := getBucketAndObject(r)
  212. if !identity.canDo(action, bucket) {
  213. return identity, s3err.ErrAccessDenied
  214. }
  215. return identity, s3err.ErrNone
  216. }
  217. func (iam *IdentityAccessManagement) authUser(r *http.Request) (*Identity, s3err.ErrorCode) {
  218. var identity *Identity
  219. var s3Err s3err.ErrorCode
  220. var found bool
  221. switch getRequestAuthType(r) {
  222. case authTypeStreamingSigned:
  223. return identity, s3err.ErrNone
  224. case authTypeUnknown:
  225. glog.V(3).Infof("unknown auth type")
  226. return identity, s3err.ErrAccessDenied
  227. case authTypePresignedV2, authTypeSignedV2:
  228. glog.V(3).Infof("v2 auth type")
  229. identity, s3Err = iam.isReqAuthenticatedV2(r)
  230. case authTypeSigned, authTypePresigned:
  231. glog.V(3).Infof("v4 auth type")
  232. identity, s3Err = iam.reqSignatureV4Verify(r)
  233. case authTypePostPolicy:
  234. glog.V(3).Infof("post policy auth type")
  235. return identity, s3err.ErrNone
  236. case authTypeJWT:
  237. glog.V(3).Infof("jwt auth type")
  238. return identity, s3err.ErrNotImplemented
  239. case authTypeAnonymous:
  240. identity, found = iam.lookupAnonymous()
  241. if !found {
  242. return identity, s3err.ErrAccessDenied
  243. }
  244. default:
  245. return identity, s3err.ErrNotImplemented
  246. }
  247. glog.V(3).Infof("auth error: %v", s3Err)
  248. if s3Err != s3err.ErrNone {
  249. return identity, s3Err
  250. }
  251. return identity, s3err.ErrNone
  252. }
  253. func (identity *Identity) canDo(action Action, bucket string) bool {
  254. if identity.isAdmin() {
  255. return true
  256. }
  257. for _, a := range identity.Actions {
  258. if a == action {
  259. return true
  260. }
  261. }
  262. if bucket == "" {
  263. return false
  264. }
  265. limitedByBucket := string(action) + ":" + bucket
  266. adminLimitedByBucket := s3_constants.ACTION_ADMIN + ":" + bucket
  267. for _, a := range identity.Actions {
  268. act := string(a)
  269. if strings.HasSuffix(act, "*") {
  270. if strings.HasPrefix(limitedByBucket, act[:len(act)-1]) {
  271. return true
  272. }
  273. if strings.HasPrefix(adminLimitedByBucket, act[:len(act)-1]) {
  274. return true
  275. }
  276. } else {
  277. if act == limitedByBucket {
  278. return true
  279. }
  280. if act == adminLimitedByBucket {
  281. return true
  282. }
  283. }
  284. }
  285. return false
  286. }
  287. func (identity *Identity) isAdmin() bool {
  288. for _, a := range identity.Actions {
  289. if a == "Admin" {
  290. return true
  291. }
  292. }
  293. return false
  294. }