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.

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