From b9f77b3d963259a88a664ff962fa8086e78ced1d Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Tue, 11 Jan 2022 15:33:09 +0300 Subject: [PATCH] [#301] Implement GetBucketLocation Signed-off-by: Denis Kirillov --- api/data/info.go | 11 ++++++----- api/handler/info.go | 14 ++++++++++---- api/handler/put.go | 1 + api/layer/container.go | 25 +++++++++++++++++++------ api/layer/layer.go | 11 ++++++----- docs/aws_s3_compat.md | 2 +- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/api/data/info.go b/api/data/info.go index cd330f0cf..5ccbe2117 100644 --- a/api/data/info.go +++ b/api/data/info.go @@ -17,11 +17,12 @@ const ( type ( // BucketInfo stores basic bucket data. BucketInfo struct { - Name string - CID *cid.ID - Owner *owner.ID - Created time.Time - BasicACL uint32 + Name string + CID *cid.ID + Owner *owner.ID + Created time.Time + BasicACL uint32 + LocationConstraint string } // ObjectInfo holds S3 object data. diff --git a/api/handler/info.go b/api/handler/info.go index 0becc3879..f14309542 100644 --- a/api/handler/info.go +++ b/api/handler/info.go @@ -9,12 +9,18 @@ import ( func (h *handler) GetBucketLocationHandler(w http.ResponseWriter, r *http.Request) { reqInfo := api.GetReqInfo(r.Context()) - if _, err := h.obj.GetBucketInfo(r.Context(), reqInfo.BucketName); err != nil { - h.logAndSendError(w, "something went wrong", reqInfo, err) + bktInfo, err := h.obj.GetBucketInfo(r.Context(), reqInfo.BucketName) + if err != nil { + h.logAndSendError(w, "could not get bucket info", reqInfo, err) return } - if err := api.EncodeToResponse(w, LocationResponse{Location: ""}); err != nil { - h.logAndSendError(w, "something went wrong", reqInfo, err) + if err = checkOwner(bktInfo, r.Header.Get(api.AmzExpectedBucketOwner)); err != nil { + h.logAndSendError(w, "expected owner doesn't match", reqInfo, err) + return + } + + if err = api.EncodeToResponse(w, LocationResponse{Location: bktInfo.LocationConstraint}); err != nil { + h.logAndSendError(w, "couldn't encode bucket location response", reqInfo, err) } } diff --git a/api/handler/put.go b/api/handler/put.go index 3aa416658..38574d2f3 100644 --- a/api/handler/put.go +++ b/api/handler/put.go @@ -549,6 +549,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) { for _, placementPolicy := range policies { if placementPolicy.LocationConstraint == createParams.LocationConstraint { p.Policy = placementPolicy.Policy + p.LocationConstraint = createParams.LocationConstraint break } } diff --git a/api/layer/container.go b/api/layer/container.go index 8b816162f..a807f1ace 100644 --- a/api/layer/container.go +++ b/api/layer/container.go @@ -26,6 +26,8 @@ type ( } ) +const locationConstraintAttr = ".s3-location-constraint" + func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*data.BucketInfo, error) { var ( err error @@ -70,6 +72,8 @@ func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*data.BucketInf } info.Created = time.Unix(unix, 0) + case locationConstraintAttr: + info.LocationConstraint = val } } @@ -117,16 +121,25 @@ func (n *layer) containerList(ctx context.Context) ([]*data.BucketInfo, error) { func (n *layer) createContainer(ctx context.Context, p *CreateBucketParams) (*cid.ID, error) { var err error bktInfo := &data.BucketInfo{ - Name: p.Name, - Owner: n.Owner(ctx), - Created: time.Now(), - BasicACL: p.ACL, + Name: p.Name, + Owner: n.Owner(ctx), + Created: time.Now(), + BasicACL: p.ACL, + LocationConstraint: p.LocationConstraint, } - cnr := container.New( + + options := []container.Option{ container.WithPolicy(p.Policy), container.WithCustomBasicACL(p.ACL), container.WithAttribute(container.AttributeName, p.Name), - container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(bktInfo.Created.Unix(), 10))) + container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(bktInfo.Created.Unix(), 10)), + } + + if p.LocationConstraint != "" { + options = append(options, container.WithAttribute(locationConstraintAttr, p.LocationConstraint)) + } + + cnr := container.New(options...) container.SetNativeName(cnr, p.Name) cnr.SetSessionToken(p.SessionToken) diff --git a/api/layer/layer.go b/api/layer/layer.go index 056ceb9d9..f5e382e8c 100644 --- a/api/layer/layer.go +++ b/api/layer/layer.go @@ -131,11 +131,12 @@ type ( } // CreateBucketParams stores bucket create request parameters. CreateBucketParams struct { - Name string - ACL uint32 - Policy *netmap.PlacementPolicy - EACL *eacl.Table - SessionToken *session.Token + Name string + ACL uint32 + Policy *netmap.PlacementPolicy + EACL *eacl.Table + SessionToken *session.Token + LocationConstraint string } // PutBucketACLParams stores put bucket acl request parameters. PutBucketACLParams struct { diff --git a/docs/aws_s3_compat.md b/docs/aws_s3_compat.md index b62feef32..ce2340b24 100644 --- a/docs/aws_s3_compat.md +++ b/docs/aws_s3_compat.md @@ -87,7 +87,7 @@ See also `GetObject` and other method parameters. |----|----------------------|-----------| | 🟢 | CreateBucket | PutBucket | | 🟢 | DeleteBucket | | -| 🔵 | GetBucketLocation | | +| 🟢 | GetBucketLocation | | | 🟢 | HeadBucket | | | 🟢 | ListBuckets | | | 🔵 | PutPublicAccessBlock | |