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.

342 lines
9.8 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. "fmt"
  6. "math"
  7. "net/http"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  10. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  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(xhttp.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, _ := xhttp.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(xhttp.AmzIdentityId); identityId != "" {
  103. if entry.Extended == nil {
  104. entry.Extended = make(map[string][]byte)
  105. }
  106. entry.Extended[xhttp.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. writeSuccessResponseEmpty(w, r)
  116. }
  117. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  118. bucket, _ := xhttp.GetBucketAndObject(r)
  119. glog.V(3).Infof("DeleteBucketHandler %s", bucket)
  120. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  121. s3err.WriteErrorResponse(w, r, err)
  122. return
  123. }
  124. err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  125. if !s3a.option.AllowDeleteBucketNotEmpty {
  126. entries, _, err := s3a.list(s3a.option.BucketsPath+"/"+bucket, "", "", false, 1)
  127. if err != nil {
  128. return fmt.Errorf("failed to list bucket %s: %v", bucket, err)
  129. }
  130. if len(entries) > 0 {
  131. return fmt.Errorf("BucketNotEmpty")
  132. }
  133. }
  134. // delete collection
  135. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  136. Collection: bucket,
  137. }
  138. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  139. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  140. return fmt.Errorf("delete collection %s: %v", bucket, err)
  141. }
  142. return nil
  143. })
  144. if err != nil {
  145. s3ErrorCode := s3err.ErrInternalError
  146. if err.Error() == "BucketNotEmpty" {
  147. s3ErrorCode = s3err.ErrBucketNotEmpty
  148. }
  149. s3err.WriteErrorResponse(w, r, s3ErrorCode)
  150. return
  151. }
  152. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  153. if err != nil {
  154. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  155. return
  156. }
  157. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  158. }
  159. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  160. bucket, _ := xhttp.GetBucketAndObject(r)
  161. glog.V(3).Infof("HeadBucketHandler %s", bucket)
  162. if entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket); entry == nil || err == filer_pb.ErrNotFound {
  163. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  164. return
  165. }
  166. writeSuccessResponseEmpty(w, r)
  167. }
  168. func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
  169. entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  170. if entry == nil || err == filer_pb.ErrNotFound {
  171. return s3err.ErrNoSuchBucket
  172. }
  173. if !s3a.hasAccess(r, entry) {
  174. return s3err.ErrAccessDenied
  175. }
  176. return s3err.ErrNone
  177. }
  178. func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
  179. isAdmin := r.Header.Get(xhttp.AmzIsAdmin) != ""
  180. if isAdmin {
  181. return true
  182. }
  183. if entry.Extended == nil {
  184. return true
  185. }
  186. identityId := r.Header.Get(xhttp.AmzIdentityId)
  187. if id, ok := entry.Extended[xhttp.AmzIdentityId]; ok {
  188. if identityId != string(id) {
  189. return false
  190. }
  191. }
  192. return true
  193. }
  194. // GetBucketAclHandler Get Bucket ACL
  195. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html
  196. func (s3a *S3ApiServer) GetBucketAclHandler(w http.ResponseWriter, r *http.Request) {
  197. // collect parameters
  198. bucket, _ := xhttp.GetBucketAndObject(r)
  199. glog.V(3).Infof("GetBucketAclHandler %s", bucket)
  200. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  201. s3err.WriteErrorResponse(w, r, err)
  202. return
  203. }
  204. response := AccessControlPolicy{}
  205. for _, ident := range s3a.iam.identities {
  206. if len(ident.Credentials) == 0 {
  207. continue
  208. }
  209. for _, action := range ident.Actions {
  210. if !action.overBucket(bucket) || action.getPermission() == "" {
  211. continue
  212. }
  213. id := ident.Credentials[0].AccessKey
  214. if response.Owner.DisplayName == "" && action.isOwner(bucket) && len(ident.Credentials) > 0 {
  215. response.Owner.DisplayName = ident.Name
  216. response.Owner.ID = id
  217. }
  218. response.AccessControlList.Grant = append(response.AccessControlList.Grant, Grant{
  219. Grantee: Grantee{
  220. ID: id,
  221. DisplayName: ident.Name,
  222. Type: "CanonicalUser",
  223. XMLXSI: "CanonicalUser",
  224. XMLNS: "http://www.w3.org/2001/XMLSchema-instance"},
  225. Permission: action.getPermission(),
  226. })
  227. }
  228. }
  229. writeSuccessResponseXML(w, r, response)
  230. }
  231. // GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration
  232. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html
  233. func (s3a *S3ApiServer) GetBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  234. // collect parameters
  235. bucket, _ := xhttp.GetBucketAndObject(r)
  236. glog.V(3).Infof("GetBucketLifecycleConfigurationHandler %s", bucket)
  237. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  238. s3err.WriteErrorResponse(w, r, err)
  239. return
  240. }
  241. fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
  242. if err != nil {
  243. glog.Errorf("GetBucketLifecycleConfigurationHandler: %s", err)
  244. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  245. return
  246. }
  247. ttls := fc.GetCollectionTtls(bucket)
  248. if len(ttls) == 0 {
  249. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchLifecycleConfiguration)
  250. return
  251. }
  252. response := Lifecycle{}
  253. for prefix, internalTtl := range ttls {
  254. ttl, _ := needle.ReadTTL(internalTtl)
  255. days := int(ttl.Minutes() / 60 / 24)
  256. if days == 0 {
  257. continue
  258. }
  259. response.Rules = append(response.Rules, Rule{
  260. Status: Enabled, Filter: Filter{
  261. Prefix: Prefix{string: prefix, set: true},
  262. set: true,
  263. },
  264. Expiration: Expiration{Days: days, set: true},
  265. })
  266. }
  267. writeSuccessResponseXML(w, r, response)
  268. }
  269. // PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration
  270. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketLifecycleConfiguration.html
  271. func (s3a *S3ApiServer) PutBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  272. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  273. }
  274. // DeleteBucketMetricsConfiguration Delete Bucket Lifecycle
  275. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html
  276. func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) {
  277. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  278. }
  279. // GetBucketLocationHandler Get bucket location
  280. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html
  281. func (s3a *S3ApiServer) GetBucketLocationHandler(w http.ResponseWriter, r *http.Request) {
  282. writeSuccessResponseXML(w, r, LocationConstraint{})
  283. }
  284. // GetBucketRequestPaymentHandler Get bucket location
  285. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketRequestPayment.html
  286. func (s3a *S3ApiServer) GetBucketRequestPaymentHandler(w http.ResponseWriter, r *http.Request) {
  287. writeSuccessResponseXML(w, r, RequestPaymentConfiguration{Payer: "BucketOwner"})
  288. }