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.

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