2018-07-18 09:37:09 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-27 07:23:04 +00:00
|
|
|
"encoding/xml"
|
2018-07-18 09:37:09 +00:00
|
|
|
"fmt"
|
2019-01-02 19:36:29 +00:00
|
|
|
"math"
|
2018-07-22 00:39:10 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2018-07-24 08:38:08 +00:00
|
|
|
|
2022-02-03 14:17:05 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
|
2020-11-11 08:20:59 +00:00
|
|
|
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
|
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2020-02-25 19:13:06 +00:00
|
|
|
|
2018-07-24 08:38:08 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-07-19 08:21:44 +00:00
|
|
|
)
|
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
type ListAllMyBucketsResult struct {
|
2019-03-15 22:55:34 +00:00
|
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
|
2019-01-02 19:36:29 +00:00
|
|
|
Owner *s3.Owner
|
2019-02-27 07:23:04 +00:00
|
|
|
Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
|
2019-01-02 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 09:37:09 +00:00
|
|
|
func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-09-19 07:18:59 +00:00
|
|
|
glog.V(3).Infof("ListBucketsHandler")
|
|
|
|
|
2020-12-25 08:38:56 +00:00
|
|
|
var identity *Identity
|
|
|
|
var s3Err s3err.ErrorCode
|
|
|
|
if s3a.iam.isEnabled() {
|
|
|
|
identity, s3Err = s3a.iam.authUser(r)
|
|
|
|
if s3Err != s3err.ErrNone {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3Err)
|
2020-12-25 08:38:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
var response ListAllMyBucketsResult
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2020-09-11 21:53:50 +00:00
|
|
|
entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2018-09-03 20:03:16 +00:00
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
2018-09-03 20:03:16 +00:00
|
|
|
return
|
|
|
|
}
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2020-11-11 08:20:59 +00:00
|
|
|
identityId := r.Header.Get(xhttp.AmzIdentityId)
|
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
var buckets []*s3.Bucket
|
2018-09-03 20:03:16 +00:00
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDirectory {
|
2022-01-03 23:39:36 +00:00
|
|
|
if identity != nil && !identity.canDo(s3_constants.ACTION_LIST, entry.Name, "") {
|
2020-11-12 21:57:54 +00:00
|
|
|
continue
|
2020-11-11 08:20:59 +00:00
|
|
|
}
|
2019-01-02 19:36:29 +00:00
|
|
|
buckets = append(buckets, &s3.Bucket{
|
|
|
|
Name: aws.String(entry.Name),
|
2020-04-30 04:35:24 +00:00
|
|
|
CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
|
2018-09-03 20:03:16 +00:00
|
|
|
})
|
2018-07-18 09:37:09 +00:00
|
|
|
}
|
2018-09-03 20:03:16 +00:00
|
|
|
}
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
response = ListAllMyBucketsResult{
|
|
|
|
Owner: &s3.Owner{
|
2020-11-11 08:20:59 +00:00
|
|
|
ID: aws.String(identityId),
|
|
|
|
DisplayName: aws.String(identityId),
|
2018-09-03 20:03:16 +00:00
|
|
|
},
|
2019-01-02 19:36:29 +00:00
|
|
|
Buckets: buckets,
|
2018-07-18 09:37:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseXML(w, r, response)
|
2018-07-18 09:37:09 +00:00
|
|
|
}
|
2018-07-19 08:21:44 +00:00
|
|
|
|
|
|
|
func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-12-07 07:15:48 +00:00
|
|
|
bucket, _ := xhttp.GetBucketAndObject(r)
|
2021-09-19 07:18:59 +00:00
|
|
|
glog.V(3).Infof("PutBucketHandler %s", bucket)
|
2018-07-19 08:21:44 +00:00
|
|
|
|
2020-10-15 17:52:17 +00:00
|
|
|
// avoid duplicated buckets
|
|
|
|
errCode := s3err.ErrNone
|
2021-12-26 08:15:03 +00:00
|
|
|
if err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2020-10-15 17:52:17 +00:00
|
|
|
if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
|
|
|
|
IncludeEcVolumes: true,
|
|
|
|
IncludeNormalVolumes: true,
|
|
|
|
}); err != nil {
|
|
|
|
glog.Errorf("list collection: %v", err)
|
|
|
|
return fmt.Errorf("list collections: %v", err)
|
2020-10-25 03:12:04 +00:00
|
|
|
} else {
|
2020-10-15 17:52:17 +00:00
|
|
|
for _, c := range resp.Collections {
|
|
|
|
if bucket == c.Name {
|
|
|
|
errCode = s3err.ErrBucketAlreadyExists
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
2020-10-15 17:52:17 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-12 06:11:03 +00:00
|
|
|
if exist, err := s3a.exists(s3a.option.BucketsPath, bucket, true); err == nil && exist {
|
|
|
|
errCode = s3err.ErrBucketAlreadyExists
|
|
|
|
}
|
2020-10-15 17:52:17 +00:00
|
|
|
if errCode != s3err.ErrNone {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, errCode)
|
2020-10-15 17:52:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:11:36 +00:00
|
|
|
if s3a.iam.isEnabled() {
|
|
|
|
if _, errCode = s3a.iam.authRequest(r, s3_constants.ACTION_ADMIN); errCode != s3err.ErrNone {
|
|
|
|
s3err.WriteErrorResponse(w, r, errCode)
|
|
|
|
return
|
|
|
|
}
|
2021-11-02 20:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 08:20:59 +00:00
|
|
|
fn := func(entry *filer_pb.Entry) {
|
|
|
|
if identityId := r.Header.Get(xhttp.AmzIdentityId); identityId != "" {
|
2020-11-12 03:50:19 +00:00
|
|
|
if entry.Extended == nil {
|
|
|
|
entry.Extended = make(map[string][]byte)
|
|
|
|
}
|
2020-11-11 08:20:59 +00:00
|
|
|
entry.Extended[xhttp.AmzIdentityId] = []byte(identityId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 20:03:16 +00:00
|
|
|
// create the folder for bucket, but lazily create actual collection
|
2020-11-11 08:20:59 +00:00
|
|
|
if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
|
2020-10-15 17:52:17 +00:00
|
|
|
glog.Errorf("PutBucketHandler mkdir: %v", err)
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
2018-07-19 08:21:44 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseEmpty(w, r)
|
2018-07-19 08:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-12-07 07:15:48 +00:00
|
|
|
bucket, _ := xhttp.GetBucketAndObject(r)
|
2021-09-19 07:18:59 +00:00
|
|
|
glog.V(3).Infof("DeleteBucketHandler %s", bucket)
|
2018-07-19 08:21:44 +00:00
|
|
|
|
2020-11-13 09:13:20 +00:00
|
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, err)
|
2020-11-12 08:44:16 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-12 09:59:31 +00:00
|
|
|
|
2021-12-26 08:15:03 +00:00
|
|
|
err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2018-07-20 07:10:01 +00:00
|
|
|
|
|
|
|
// delete collection
|
|
|
|
deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
|
|
|
|
Collection: bucket,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
|
2020-02-26 06:23:59 +00:00
|
|
|
if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
|
2018-07-20 07:58:10 +00:00
|
|
|
return fmt.Errorf("delete collection %s: %v", bucket, err)
|
2018-07-20 07:10:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 08:21:44 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-03-20 21:17:31 +00:00
|
|
|
err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
|
2018-09-03 20:16:26 +00:00
|
|
|
|
2018-07-19 08:21:44 +00:00
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
2018-07-19 08:21:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:02:08 +00:00
|
|
|
s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
|
2018-07-19 08:21:44 +00:00
|
|
|
}
|
2018-07-19 08:43:27 +00:00
|
|
|
|
|
|
|
func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-12-07 07:15:48 +00:00
|
|
|
bucket, _ := xhttp.GetBucketAndObject(r)
|
2021-09-19 07:18:59 +00:00
|
|
|
glog.V(3).Infof("HeadBucketHandler %s", bucket)
|
2018-07-19 08:43:27 +00:00
|
|
|
|
2021-11-17 09:42:24 +00:00
|
|
|
if entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket); entry == nil || err == filer_pb.ErrNotFound {
|
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
|
2020-11-13 09:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseEmpty(w, r)
|
2020-11-13 09:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
|
2020-11-12 21:30:08 +00:00
|
|
|
entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
|
2020-11-12 21:30:46 +00:00
|
|
|
if entry == nil || err == filer_pb.ErrNotFound {
|
2020-11-13 09:13:20 +00:00
|
|
|
return s3err.ErrNoSuchBucket
|
2018-07-19 08:43:27 +00:00
|
|
|
}
|
2020-11-12 09:59:31 +00:00
|
|
|
|
2020-11-12 21:57:54 +00:00
|
|
|
if !s3a.hasAccess(r, entry) {
|
2020-11-13 09:13:20 +00:00
|
|
|
return s3err.ErrAccessDenied
|
2020-11-12 08:15:59 +00:00
|
|
|
}
|
2020-11-13 09:13:20 +00:00
|
|
|
return s3err.ErrNone
|
2018-07-19 08:43:27 +00:00
|
|
|
}
|
2020-11-12 21:57:54 +00:00
|
|
|
|
|
|
|
func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
|
|
|
|
isAdmin := r.Header.Get(xhttp.AmzIsAdmin) != ""
|
|
|
|
if isAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if entry.Extended == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
identityId := r.Header.Get(xhttp.AmzIdentityId)
|
|
|
|
if id, ok := entry.Extended[xhttp.AmzIdentityId]; ok {
|
|
|
|
if identityId != string(id) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2020-11-13 09:13:20 +00:00
|
|
|
}
|
2021-10-11 10:03:56 +00:00
|
|
|
|
|
|
|
// GetBucketAclHandler Get Bucket ACL
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html
|
|
|
|
func (s3a *S3ApiServer) GetBucketAclHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// collect parameters
|
2021-12-07 07:15:48 +00:00
|
|
|
bucket, _ := xhttp.GetBucketAndObject(r)
|
2021-10-11 10:03:56 +00:00
|
|
|
glog.V(3).Infof("GetBucketAclHandler %s", bucket)
|
|
|
|
|
|
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, err)
|
2021-10-11 10:03:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := AccessControlPolicy{}
|
|
|
|
for _, ident := range s3a.iam.identities {
|
|
|
|
if len(ident.Credentials) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, action := range ident.Actions {
|
|
|
|
if !action.overBucket(bucket) || action.getPermission() == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
id := ident.Credentials[0].AccessKey
|
|
|
|
if response.Owner.DisplayName == "" && action.isOwner(bucket) && len(ident.Credentials) > 0 {
|
|
|
|
response.Owner.DisplayName = ident.Name
|
|
|
|
response.Owner.ID = id
|
|
|
|
}
|
|
|
|
response.AccessControlList.Grant = append(response.AccessControlList.Grant, Grant{
|
|
|
|
Grantee: Grantee{
|
|
|
|
ID: id,
|
|
|
|
DisplayName: ident.Name,
|
|
|
|
Type: "CanonicalUser",
|
|
|
|
XMLXSI: "CanonicalUser",
|
|
|
|
XMLNS: "http://www.w3.org/2001/XMLSchema-instance"},
|
|
|
|
Permission: action.getPermission(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseXML(w, r, response)
|
2021-10-11 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html
|
|
|
|
func (s3a *S3ApiServer) GetBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// collect parameters
|
2021-12-07 07:15:48 +00:00
|
|
|
bucket, _ := xhttp.GetBucketAndObject(r)
|
2021-10-11 10:03:56 +00:00
|
|
|
glog.V(3).Infof("GetBucketAclHandler %s", bucket)
|
|
|
|
|
|
|
|
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, err)
|
2021-10-11 10:03:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("GetBucketLifecycleConfigurationHandler: %s", err)
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
2021-10-11 10:03:56 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-13 20:35:33 +00:00
|
|
|
ttls := fc.GetCollectionTtls(bucket)
|
|
|
|
if len(ttls) == 0 {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchLifecycleConfiguration)
|
2021-10-28 14:34:37 +00:00
|
|
|
return
|
2021-10-13 20:35:33 +00:00
|
|
|
}
|
|
|
|
response := Lifecycle{}
|
|
|
|
for prefix, internalTtl := range ttls {
|
|
|
|
ttl, _ := needle.ReadTTL(internalTtl)
|
|
|
|
days := int(ttl.Minutes() / 60 / 24)
|
|
|
|
if days == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
response.Rules = append(response.Rules, Rule{
|
|
|
|
Status: Enabled, Filter: Filter{
|
|
|
|
Prefix: Prefix{string: prefix, set: true},
|
|
|
|
set: true,
|
|
|
|
},
|
|
|
|
Expiration: Expiration{Days: days, set: true},
|
|
|
|
})
|
|
|
|
}
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseXML(w, r, response)
|
2021-10-11 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketLifecycleConfiguration.html
|
|
|
|
func (s3a *S3ApiServer) PutBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
|
2021-10-11 10:03:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucketMetricsConfiguration Delete Bucket Lifecycle
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html
|
|
|
|
func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-11-01 01:02:08 +00:00
|
|
|
s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
|
2021-10-11 10:03:56 +00:00
|
|
|
|
|
|
|
}
|
2022-02-03 14:17:05 +00:00
|
|
|
|
|
|
|
// GetBucketLocationHandler Get bucket location
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html
|
|
|
|
func (s3a *S3ApiServer) GetBucketLocationHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
writeSuccessResponseXML(w, r, LocationConstraint{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketRequestPaymentHandler Get bucket location
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketRequestPayment.html
|
|
|
|
func (s3a *S3ApiServer) GetBucketRequestPaymentHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
writeSuccessResponseXML(w, r, RequestPaymentConfiguration{Payer: "BucketOwner"})
|
|
|
|
}
|