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.

304 lines
8.4 KiB

3 years ago
7 years ago
7 years ago
7 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
7 years ago
4 years ago
7 years ago
4 years ago
4 years ago
4 years ago
7 years ago
7 years ago
3 years ago
3 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "math"
  10. "net/http"
  11. "time"
  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, s3Err, r)
  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, s3err.ErrInternalError, r)
  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, response)
  62. }
  63. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  64. bucket, _ := getBucketAndObject(r)
  65. glog.V(3).Infof("PutBucketHandler %s", bucket)
  66. // avoid duplicated buckets
  67. errCode := s3err.ErrNone
  68. if err := s3a.WithFilerClient(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, s3err.ErrInternalError, r)
  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, errCode, r)
  93. return
  94. }
  95. fn := func(entry *filer_pb.Entry) {
  96. if identityId := r.Header.Get(xhttp.AmzIdentityId); identityId != "" {
  97. if entry.Extended == nil {
  98. entry.Extended = make(map[string][]byte)
  99. }
  100. entry.Extended[xhttp.AmzIdentityId] = []byte(identityId)
  101. }
  102. }
  103. // create the folder for bucket, but lazily create actual collection
  104. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
  105. glog.Errorf("PutBucketHandler mkdir: %v", err)
  106. s3err.WriteErrorResponse(w, s3err.ErrInternalError, r)
  107. return
  108. }
  109. writeSuccessResponseEmpty(w)
  110. }
  111. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  112. bucket, _ := getBucketAndObject(r)
  113. glog.V(3).Infof("DeleteBucketHandler %s", bucket)
  114. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  115. s3err.WriteErrorResponse(w, err, r)
  116. return
  117. }
  118. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  119. // delete collection
  120. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  121. Collection: bucket,
  122. }
  123. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  124. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  125. return fmt.Errorf("delete collection %s: %v", bucket, err)
  126. }
  127. return nil
  128. })
  129. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  130. if err != nil {
  131. s3err.WriteErrorResponse(w, s3err.ErrInternalError, r)
  132. return
  133. }
  134. s3err.WriteEmptyResponse(w, http.StatusNoContent)
  135. }
  136. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  137. bucket, _ := getBucketAndObject(r)
  138. glog.V(3).Infof("HeadBucketHandler %s", bucket)
  139. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  140. s3err.WriteErrorResponse(w, err, r)
  141. return
  142. }
  143. writeSuccessResponseEmpty(w)
  144. }
  145. func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
  146. entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  147. if entry == nil || err == filer_pb.ErrNotFound {
  148. return s3err.ErrNoSuchBucket
  149. }
  150. if !s3a.hasAccess(r, entry) {
  151. return s3err.ErrAccessDenied
  152. }
  153. return s3err.ErrNone
  154. }
  155. func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
  156. isAdmin := r.Header.Get(xhttp.AmzIsAdmin) != ""
  157. if isAdmin {
  158. return true
  159. }
  160. if entry.Extended == nil {
  161. return true
  162. }
  163. identityId := r.Header.Get(xhttp.AmzIdentityId)
  164. if id, ok := entry.Extended[xhttp.AmzIdentityId]; ok {
  165. if identityId != string(id) {
  166. return false
  167. }
  168. }
  169. return true
  170. }
  171. // GetBucketAclHandler Get Bucket ACL
  172. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html
  173. func (s3a *S3ApiServer) GetBucketAclHandler(w http.ResponseWriter, r *http.Request) {
  174. // collect parameters
  175. bucket, _ := getBucketAndObject(r)
  176. glog.V(3).Infof("GetBucketAclHandler %s", bucket)
  177. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  178. s3err.WriteErrorResponse(w, err, r)
  179. return
  180. }
  181. response := AccessControlPolicy{}
  182. for _, ident := range s3a.iam.identities {
  183. if len(ident.Credentials) == 0 {
  184. continue
  185. }
  186. for _, action := range ident.Actions {
  187. if !action.overBucket(bucket) || action.getPermission() == "" {
  188. continue
  189. }
  190. id := ident.Credentials[0].AccessKey
  191. if response.Owner.DisplayName == "" && action.isOwner(bucket) && len(ident.Credentials) > 0 {
  192. response.Owner.DisplayName = ident.Name
  193. response.Owner.ID = id
  194. }
  195. response.AccessControlList.Grant = append(response.AccessControlList.Grant, Grant{
  196. Grantee: Grantee{
  197. ID: id,
  198. DisplayName: ident.Name,
  199. Type: "CanonicalUser",
  200. XMLXSI: "CanonicalUser",
  201. XMLNS: "http://www.w3.org/2001/XMLSchema-instance"},
  202. Permission: action.getPermission(),
  203. })
  204. }
  205. }
  206. writeSuccessResponseXML(w, response)
  207. }
  208. // GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration
  209. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html
  210. func (s3a *S3ApiServer) GetBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  211. // collect parameters
  212. bucket, _ := getBucketAndObject(r)
  213. glog.V(3).Infof("GetBucketAclHandler %s", bucket)
  214. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  215. s3err.WriteErrorResponse(w, err, r)
  216. return
  217. }
  218. fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
  219. if err != nil {
  220. glog.Errorf("GetBucketLifecycleConfigurationHandler: %s", err)
  221. s3err.WriteErrorResponse(w, s3err.ErrInternalError, r)
  222. return
  223. }
  224. ttls := fc.GetCollectionTtls(bucket)
  225. if len(ttls) == 0 {
  226. s3err.WriteErrorResponse(w, s3err.ErrNoSuchLifecycleConfiguration, r)
  227. }
  228. response := Lifecycle{}
  229. for prefix, internalTtl := range ttls {
  230. ttl, _ := needle.ReadTTL(internalTtl)
  231. days := int(ttl.Minutes() / 60 / 24)
  232. if days == 0 {
  233. continue
  234. }
  235. response.Rules = append(response.Rules, Rule{
  236. Status: Enabled, Filter: Filter{
  237. Prefix: Prefix{string: prefix, set: true},
  238. set: true,
  239. },
  240. Expiration: Expiration{Days: days, set: true},
  241. })
  242. }
  243. writeSuccessResponseXML(w, response)
  244. }
  245. // PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration
  246. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketLifecycleConfiguration.html
  247. func (s3a *S3ApiServer) PutBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  248. s3err.WriteErrorResponse(w, s3err.ErrNotImplemented, r)
  249. }
  250. // DeleteBucketMetricsConfiguration Delete Bucket Lifecycle
  251. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html
  252. func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) {
  253. s3err.WriteErrorResponse(w, s3err.ErrNotImplemented, r)
  254. }