2020-08-03 11:48:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2021-07-09 14:06:35 +00:00
|
|
|
"bytes"
|
2020-08-03 11:48:33 +00:00
|
|
|
"context"
|
2021-06-23 20:21:15 +00:00
|
|
|
"fmt"
|
2020-10-22 00:19:16 +00:00
|
|
|
"strconv"
|
2021-07-09 08:57:44 +00:00
|
|
|
"strings"
|
2020-08-03 11:48:33 +00:00
|
|
|
"time"
|
|
|
|
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-09-10 06:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2021-08-09 08:53:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2022-01-25 14:41:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/acl"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
2021-08-27 12:48:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
2020-08-03 11:48:33 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-07-21 11:59:46 +00:00
|
|
|
// BucketACL extends BucketInfo by eacl.Table.
|
|
|
|
BucketACL struct {
|
2021-09-10 06:56:56 +00:00
|
|
|
Info *data.BucketInfo
|
2021-07-21 11:59:46 +00:00
|
|
|
EACL *eacl.Table
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-01-11 12:33:09 +00:00
|
|
|
const locationConstraintAttr = ".s3-location-constraint"
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*data.BucketInfo, error) {
|
2020-10-22 00:19:16 +00:00
|
|
|
var (
|
2021-10-19 15:08:07 +00:00
|
|
|
err error
|
|
|
|
res *container.Container
|
|
|
|
rid = api.GetRequestID(ctx)
|
2020-10-22 00:19:16 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
info = &data.BucketInfo{
|
2020-10-22 00:19:16 +00:00
|
|
|
CID: cid,
|
|
|
|
Name: cid.String(),
|
|
|
|
}
|
|
|
|
)
|
2021-10-19 15:08:07 +00:00
|
|
|
res, err = n.pool.GetContainer(ctx, cid, n.CallOptions(ctx)...)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
n.log.Error("could not fetch container",
|
|
|
|
zap.Stringer("cid", cid),
|
2020-08-11 11:32:04 +00:00
|
|
|
zap.String("request_id", rid),
|
2020-08-03 11:48:33 +00:00
|
|
|
zap.Error(err))
|
2020-10-19 01:04:37 +00:00
|
|
|
|
2021-07-09 08:57:44 +00:00
|
|
|
if strings.Contains(err.Error(), "container not found") {
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchBucket)
|
2021-07-09 08:57:44 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:01:38 +00:00
|
|
|
info.Owner = res.OwnerID()
|
2021-07-21 11:59:46 +00:00
|
|
|
info.BasicACL = res.BasicACL()
|
2020-10-23 00:12:37 +00:00
|
|
|
|
2020-11-24 07:01:38 +00:00
|
|
|
for _, attr := range res.Attributes() {
|
|
|
|
switch key, val := attr.Key(), attr.Value(); key {
|
2020-10-23 00:12:37 +00:00
|
|
|
case container.AttributeName:
|
2020-10-22 00:19:16 +00:00
|
|
|
info.Name = val
|
2020-10-23 00:12:37 +00:00
|
|
|
case container.AttributeTimestamp:
|
2020-11-24 07:01:38 +00:00
|
|
|
unix, err := strconv.ParseInt(attr.Value(), 10, 64)
|
2020-10-22 00:19:16 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not parse container creation time",
|
|
|
|
zap.Stringer("cid", cid),
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.String("created_at", val),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Created = time.Unix(unix, 0)
|
2022-01-11 12:33:09 +00:00
|
|
|
case locationConstraintAttr:
|
|
|
|
info.LocationConstraint = val
|
2020-10-22 00:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
if err := n.bucketCache.Put(info); err != nil {
|
|
|
|
n.log.Warn("could not put bucket info into cache",
|
|
|
|
zap.Stringer("cid", cid),
|
|
|
|
zap.String("bucket_name", info.Name),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2020-10-22 00:19:16 +00:00
|
|
|
return info, nil
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) containerList(ctx context.Context) ([]*data.BucketInfo, error) {
|
2020-11-24 07:01:38 +00:00
|
|
|
var (
|
2021-10-19 15:08:07 +00:00
|
|
|
err error
|
|
|
|
own = n.Owner(ctx)
|
|
|
|
res []*cid.ID
|
|
|
|
rid = api.GetRequestID(ctx)
|
2020-11-24 07:01:38 +00:00
|
|
|
)
|
2021-10-19 15:08:07 +00:00
|
|
|
res, err = n.pool.ListContainers(ctx, own, n.CallOptions(ctx)...)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
n.log.Error("could not fetch container",
|
2020-08-11 11:32:04 +00:00
|
|
|
zap.String("request_id", rid),
|
2020-08-03 11:48:33 +00:00
|
|
|
zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
list := make([]*data.BucketInfo, 0, len(res))
|
2020-10-19 01:04:37 +00:00
|
|
|
for _, cid := range res {
|
2020-08-03 11:48:33 +00:00
|
|
|
info, err := n.containerInfo(ctx, cid)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not fetch container info",
|
2020-08-11 11:32:04 +00:00
|
|
|
zap.String("request_id", rid),
|
2020-08-03 11:48:33 +00:00
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
list = append(list, info)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
|
|
|
func (n *layer) createContainer(ctx context.Context, p *CreateBucketParams) (*cid.ID, error) {
|
2021-08-19 06:55:22 +00:00
|
|
|
var err error
|
2021-09-10 06:56:56 +00:00
|
|
|
bktInfo := &data.BucketInfo{
|
2022-01-11 12:33:09 +00:00
|
|
|
Name: p.Name,
|
|
|
|
Owner: n.Owner(ctx),
|
|
|
|
Created: time.Now(),
|
|
|
|
BasicACL: p.ACL,
|
|
|
|
LocationConstraint: p.LocationConstraint,
|
2021-08-19 06:55:22 +00:00
|
|
|
}
|
2022-01-11 12:33:09 +00:00
|
|
|
|
|
|
|
options := []container.Option{
|
2021-06-23 20:21:15 +00:00
|
|
|
container.WithPolicy(p.Policy),
|
2022-01-25 14:41:01 +00:00
|
|
|
container.WithCustomBasicACL(acl.BasicACL(p.ACL)),
|
2021-06-23 20:21:15 +00:00
|
|
|
container.WithAttribute(container.AttributeName, p.Name),
|
2022-01-11 12:33:09 +00:00
|
|
|
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...)
|
2021-12-30 12:23:41 +00:00
|
|
|
container.SetNativeName(cnr, p.Name)
|
2021-06-23 20:21:15 +00:00
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
cnr.SetSessionToken(p.SessionToken)
|
2021-08-19 06:55:22 +00:00
|
|
|
cnr.SetOwnerID(bktInfo.Owner)
|
2021-06-23 20:21:15 +00:00
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if bktInfo.CID, err = n.pool.PutContainer(ctx, cnr); err != nil {
|
|
|
|
return nil, err
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if err = n.pool.WaitForContainerPresence(ctx, bktInfo.CID, pool.DefaultPollingParams()); err != nil {
|
2021-07-08 10:10:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if err = n.setContainerEACLTable(ctx, bktInfo.CID, p.EACL); err != nil {
|
2021-06-23 20:21:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if err = n.bucketCache.Put(bktInfo); err != nil {
|
|
|
|
n.log.Warn("couldn't put bucket info into cache",
|
|
|
|
zap.String("bucket name", bktInfo.Name),
|
|
|
|
zap.Stringer("bucket cid", bktInfo.CID),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bktInfo.CID, nil
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
2021-06-23 20:25:00 +00:00
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
func (n *layer) setContainerEACLTable(ctx context.Context, cid *cid.ID, table *eacl.Table) error {
|
|
|
|
table.SetCID(cid)
|
2021-07-08 10:10:46 +00:00
|
|
|
if err := n.pool.SetEACL(ctx, table, n.SessionOpt(ctx)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-30 18:05:27 +00:00
|
|
|
return n.waitEACLPresence(ctx, cid, table, defaultWaitParams())
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
func (n *layer) GetContainerEACL(ctx context.Context, cid *cid.ID) (*eacl.Table, error) {
|
2021-12-29 13:38:23 +00:00
|
|
|
return n.pool.GetEACL(ctx, cid)
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type waitParams struct {
|
|
|
|
WaitTimeout time.Duration
|
|
|
|
PollInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultWaitParams() *waitParams {
|
|
|
|
return &waitParams{
|
|
|
|
WaitTimeout: 60 * time.Second,
|
|
|
|
PollInterval: 3 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:06:35 +00:00
|
|
|
func (n *layer) waitEACLPresence(ctx context.Context, cid *cid.ID, table *eacl.Table, params *waitParams) error {
|
|
|
|
exp, err := table.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal eacl: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:10:46 +00:00
|
|
|
wctx, cancel := context.WithTimeout(ctx, params.WaitTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ticker := time.NewTimer(params.PollInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
wdone := wctx.Done()
|
|
|
|
done := ctx.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return ctx.Err()
|
|
|
|
case <-wdone:
|
|
|
|
return wctx.Err()
|
|
|
|
case <-ticker.C:
|
2021-12-29 13:38:23 +00:00
|
|
|
eaclTable, err := n.pool.GetEACL(ctx, cid)
|
2021-07-09 14:06:35 +00:00
|
|
|
if err == nil {
|
2021-12-29 13:38:23 +00:00
|
|
|
got, err := eaclTable.Marshal()
|
2021-07-09 14:06:35 +00:00
|
|
|
if err == nil && bytes.Equal(exp, got) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|
|
|
|
ticker.Reset(params.PollInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:25:00 +00:00
|
|
|
func (n *layer) deleteContainer(ctx context.Context, cid *cid.ID) error {
|
|
|
|
return n.pool.DeleteContainer(ctx, cid, n.SessionOpt(ctx))
|
|
|
|
}
|