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.

343 lines
9.9 KiB

7 years ago
7 years ago
7 years ago
3 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
4 years ago
3 years ago
3 years ago
6 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
7 years ago
7 years ago
3 years ago
4 years ago
4 years ago
7 years ago
7 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "math"
  8. "net/http"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/filer"
  11. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  14. "github.com/aws/aws-sdk-go/aws"
  15. "github.com/aws/aws-sdk-go/service/s3"
  16. "github.com/chrislusf/seaweedfs/weed/glog"
  17. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  18. )
  19. type ListAllMyBucketsResult struct {
  20. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
  21. Owner *s3.Owner
  22. Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
  23. }
  24. func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
  25. glog.V(3).Infof("ListBucketsHandler")
  26. var identity *Identity
  27. var s3Err s3err.ErrorCode
  28. if s3a.iam.isEnabled() {
  29. identity, s3Err = s3a.iam.authUser(r)
  30. if s3Err != s3err.ErrNone {
  31. s3err.WriteErrorResponse(w, r, s3Err)
  32. return
  33. }
  34. }
  35. var response ListAllMyBucketsResult
  36. entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
  37. if err != nil {
  38. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  39. return
  40. }
  41. identityId := r.Header.Get(s3_constants.AmzIdentityId)
  42. var buckets []*s3.Bucket
  43. for _, entry := range entries {
  44. if entry.IsDirectory {
  45. if identity != nil && !identity.canDo(s3_constants.ACTION_LIST, entry.Name, "") {
  46. continue
  47. }
  48. buckets = append(buckets, &s3.Bucket{
  49. Name: aws.String(entry.Name),
  50. CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
  51. })
  52. }
  53. }
  54. response = ListAllMyBucketsResult{
  55. Owner: &s3.Owner{
  56. ID: aws.String(identityId),
  57. DisplayName: aws.String(identityId),
  58. },
  59. Buckets: buckets,
  60. }
  61. writeSuccessResponseXML(w, r, response)
  62. }
  63. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  64. bucket, _ := s3_constants.GetBucketAndObject(r)
  65. glog.V(3).Infof("PutBucketHandler %s", bucket)
  66. // avoid duplicated buckets
  67. errCode := s3err.ErrNone
  68. if err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  69. if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
  70. IncludeEcVolumes: true,
  71. IncludeNormalVolumes: true,
  72. }); err != nil {
  73. glog.Errorf("list collection: %v", err)
  74. return fmt.Errorf("list collections: %v", err)
  75. } else {
  76. for _, c := range resp.Collections {
  77. if bucket == c.Name {
  78. errCode = s3err.ErrBucketAlreadyExists
  79. break
  80. }
  81. }
  82. }
  83. return nil
  84. }); err != nil {
  85. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  86. return
  87. }
  88. if exist, err := s3a.exists(s3a.option.BucketsPath, bucket, true); err == nil && exist {
  89. errCode = s3err.ErrBucketAlreadyExists
  90. }
  91. if errCode != s3err.ErrNone {
  92. s3err.WriteErrorResponse(w, r, errCode)
  93. return
  94. }
  95. if s3a.iam.isEnabled() {
  96. if _, errCode = s3a.iam.authRequest(r, s3_constants.ACTION_ADMIN); errCode != s3err.ErrNone {
  97. s3err.WriteErrorResponse(w, r, errCode)
  98. return
  99. }
  100. }
  101. fn := func(entry *filer_pb.Entry) {
  102. if identityId := r.Header.Get(s3_constants.AmzIdentityId); identityId != "" {
  103. if entry.Extended == nil {
  104. entry.Extended = make(map[string][]byte)
  105. }
  106. entry.Extended[s3_constants.AmzIdentityId] = []byte(identityId)
  107. }
  108. }
  109. // create the folder for bucket, but lazily create actual collection
  110. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
  111. glog.Errorf("PutBucketHandler mkdir: %v", err)
  112. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  113. return
  114. }
  115. w.Header().Set("Location", "/"+bucket)
  116. writeSuccessResponseEmpty(w, r)
  117. }
  118. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  119. bucket, _ := s3_constants.GetBucketAndObject(r)
  120. glog.V(3).Infof("DeleteBucketHandler %s", bucket)
  121. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  122. s3err.WriteErrorResponse(w, r, err)
  123. return
  124. }
  125. err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  126. if !s3a.option.AllowDeleteBucketNotEmpty {
  127. entries, _, err := s3a.list(s3a.option.BucketsPath+"/"+bucket, "", "", false, 1)
  128. if err != nil {
  129. return fmt.Errorf("failed to list bucket %s: %v", bucket, err)
  130. }
  131. if len(entries) > 0 {
  132. return errors.New(s3err.GetAPIError(s3err.ErrBucketNotEmpty).Code)
  133. }
  134. }
  135. // delete collection
  136. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  137. Collection: bucket,
  138. }
  139. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  140. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  141. return fmt.Errorf("delete collection %s: %v", bucket, err)
  142. }
  143. return nil
  144. })
  145. if err != nil {
  146. s3ErrorCode := s3err.ErrInternalError
  147. if err.Error() == s3err.GetAPIError(s3err.ErrBucketNotEmpty).Code {
  148. s3ErrorCode = s3err.ErrBucketNotEmpty
  149. }
  150. s3err.WriteErrorResponse(w, r, s3ErrorCode)
  151. return
  152. }
  153. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  154. if err != nil {
  155. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  156. return
  157. }
  158. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  159. }
  160. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  161. bucket, _ := s3_constants.GetBucketAndObject(r)
  162. glog.V(3).Infof("HeadBucketHandler %s", bucket)
  163. if entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket); entry == nil || err == filer_pb.ErrNotFound {
  164. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  165. return
  166. }
  167. writeSuccessResponseEmpty(w, r)
  168. }
  169. func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
  170. entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  171. if entry == nil || err == filer_pb.ErrNotFound {
  172. return s3err.ErrNoSuchBucket
  173. }
  174. if !s3a.hasAccess(r, entry) {
  175. return s3err.ErrAccessDenied
  176. }
  177. return s3err.ErrNone
  178. }
  179. func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
  180. isAdmin := r.Header.Get(s3_constants.AmzIsAdmin) != ""
  181. if isAdmin {
  182. return true
  183. }
  184. if entry.Extended == nil {
  185. return true
  186. }
  187. identityId := r.Header.Get(s3_constants.AmzIdentityId)
  188. if id, ok := entry.Extended[s3_constants.AmzIdentityId]; ok {
  189. if identityId != string(id) {
  190. return false
  191. }
  192. }
  193. return true
  194. }
  195. // GetBucketAclHandler Get Bucket ACL
  196. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html
  197. func (s3a *S3ApiServer) GetBucketAclHandler(w http.ResponseWriter, r *http.Request) {
  198. // collect parameters
  199. bucket, _ := s3_constants.GetBucketAndObject(r)
  200. glog.V(3).Infof("GetBucketAclHandler %s", bucket)
  201. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  202. s3err.WriteErrorResponse(w, r, err)
  203. return
  204. }
  205. response := AccessControlPolicy{}
  206. for _, ident := range s3a.iam.identities {
  207. if len(ident.Credentials) == 0 {
  208. continue
  209. }
  210. for _, action := range ident.Actions {
  211. if !action.overBucket(bucket) || action.getPermission() == "" {
  212. continue
  213. }
  214. id := ident.Credentials[0].AccessKey
  215. if response.Owner.DisplayName == "" && action.isOwner(bucket) && len(ident.Credentials) > 0 {
  216. response.Owner.DisplayName = ident.Name
  217. response.Owner.ID = id
  218. }
  219. response.AccessControlList.Grant = append(response.AccessControlList.Grant, Grant{
  220. Grantee: Grantee{
  221. ID: id,
  222. DisplayName: ident.Name,
  223. Type: "CanonicalUser",
  224. XMLXSI: "CanonicalUser",
  225. XMLNS: "http://www.w3.org/2001/XMLSchema-instance"},
  226. Permission: action.getPermission(),
  227. })
  228. }
  229. }
  230. writeSuccessResponseXML(w, r, response)
  231. }
  232. // GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration
  233. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html
  234. func (s3a *S3ApiServer) GetBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  235. // collect parameters
  236. bucket, _ := s3_constants.GetBucketAndObject(r)
  237. glog.V(3).Infof("GetBucketLifecycleConfigurationHandler %s", bucket)
  238. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  239. s3err.WriteErrorResponse(w, r, err)
  240. return
  241. }
  242. fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
  243. if err != nil {
  244. glog.Errorf("GetBucketLifecycleConfigurationHandler: %s", err)
  245. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  246. return
  247. }
  248. ttls := fc.GetCollectionTtls(bucket)
  249. if len(ttls) == 0 {
  250. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchLifecycleConfiguration)
  251. return
  252. }
  253. response := Lifecycle{}
  254. for prefix, internalTtl := range ttls {
  255. ttl, _ := needle.ReadTTL(internalTtl)
  256. days := int(ttl.Minutes() / 60 / 24)
  257. if days == 0 {
  258. continue
  259. }
  260. response.Rules = append(response.Rules, Rule{
  261. Status: Enabled, Filter: Filter{
  262. Prefix: Prefix{string: prefix, set: true},
  263. set: true,
  264. },
  265. Expiration: Expiration{Days: days, set: true},
  266. })
  267. }
  268. writeSuccessResponseXML(w, r, response)
  269. }
  270. // PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration
  271. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketLifecycleConfiguration.html
  272. func (s3a *S3ApiServer) PutBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  273. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  274. }
  275. // DeleteBucketMetricsConfiguration Delete Bucket Lifecycle
  276. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html
  277. func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) {
  278. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  279. }
  280. // GetBucketLocationHandler Get bucket location
  281. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html
  282. func (s3a *S3ApiServer) GetBucketLocationHandler(w http.ResponseWriter, r *http.Request) {
  283. writeSuccessResponseXML(w, r, LocationConstraint{})
  284. }
  285. // GetBucketRequestPaymentHandler Get bucket location
  286. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketRequestPayment.html
  287. func (s3a *S3ApiServer) GetBucketRequestPaymentHandler(w http.ResponseWriter, r *http.Request) {
  288. writeSuccessResponseXML(w, r, RequestPaymentConfiguration{Payer: "BucketOwner"})
  289. }