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-03-04 13:07:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer/neofs"
|
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"
|
2022-01-26 09:09:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
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-02-25 09:06:40 +00:00
|
|
|
const (
|
|
|
|
attributeLocationConstraint = ".s3-location-constraint"
|
2022-03-01 15:07:15 +00:00
|
|
|
AttributeLockEnabled = "LockEnabled"
|
2022-02-25 09:06:40 +00:00
|
|
|
)
|
2022-01-11 12:33:09 +00:00
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
func (n *layer) containerInfo(ctx context.Context, idCnr *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)
|
2022-02-25 09:06:40 +00:00
|
|
|
log = n.log.With(zap.Stringer("cid", idCnr), zap.String("request_id", rid))
|
2020-10-22 00:19:16 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
info = &data.BucketInfo{
|
2022-03-01 19:02:24 +00:00
|
|
|
CID: idCnr,
|
|
|
|
Name: idCnr.String(),
|
2020-10-22 00:19:16 +00:00
|
|
|
}
|
|
|
|
)
|
2022-03-01 19:02:24 +00:00
|
|
|
res, err = n.neoFS.Container(ctx, *idCnr)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2022-02-25 09:06:40 +00:00
|
|
|
log.Error("could not fetch container", 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 {
|
2022-02-25 09:06:40 +00:00
|
|
|
log.Error("could not parse container creation time",
|
|
|
|
zap.String("created_at", val), zap.Error(err))
|
2020-10-22 00:19:16 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Created = time.Unix(unix, 0)
|
2022-02-25 09:06:40 +00:00
|
|
|
case attributeLocationConstraint:
|
2022-01-11 12:33:09 +00:00
|
|
|
info.LocationConstraint = val
|
2022-03-01 15:07:15 +00:00
|
|
|
case AttributeLockEnabled:
|
2022-02-25 09:06:40 +00:00
|
|
|
info.ObjectLockEnabled, err = strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not parse container object lock enabled attribute",
|
|
|
|
zap.String("lock_enabled", val), zap.Error(err))
|
|
|
|
}
|
2020-10-22 00:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2022-02-25 09:06:40 +00:00
|
|
|
if err = n.bucketCache.Put(info); err != nil {
|
|
|
|
log.Warn("could not put bucket info into cache",
|
|
|
|
zap.String("bucket_name", info.Name), zap.Error(err))
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-03-01 19:02:24 +00:00
|
|
|
res []cid.ID
|
2021-10-19 15:08:07 +00:00
|
|
|
rid = api.GetRequestID(ctx)
|
2020-11-24 07:01:38 +00:00
|
|
|
)
|
2022-03-01 19:02:24 +00:00
|
|
|
res, err = n.neoFS.UserContainers(ctx, *own)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2022-03-01 19:02:24 +00:00
|
|
|
n.log.Error("could not list user containers",
|
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))
|
2022-03-01 19:02:24 +00:00
|
|
|
for i := range res {
|
|
|
|
info, err := n.containerInfo(ctx, &res[i])
|
2020-08-03 11:48:33 +00:00
|
|
|
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
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
func (n *layer) createContainer(ctx context.Context, p *CreateBucketParams) (*data.BucketInfo, 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),
|
2022-03-25 10:28:39 +00:00
|
|
|
Created: time.Now(), // this can be a little incorrect since the real time is set later
|
2022-01-11 12:33:09 +00:00
|
|
|
BasicACL: p.ACL,
|
|
|
|
LocationConstraint: p.LocationConstraint,
|
2022-03-18 13:04:09 +00:00
|
|
|
ObjectLockEnabled: p.ObjectLockEnabled,
|
2021-08-19 06:55:22 +00:00
|
|
|
}
|
2022-01-11 12:33:09 +00:00
|
|
|
|
2022-03-04 13:07:27 +00:00
|
|
|
var attributes [][2]string
|
2022-01-11 12:33:09 +00:00
|
|
|
|
|
|
|
if p.LocationConstraint != "" {
|
2022-03-04 13:07:27 +00:00
|
|
|
attributes = append(attributes, [2]string{
|
|
|
|
attributeLocationConstraint, p.LocationConstraint,
|
|
|
|
})
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
if p.ObjectLockEnabled {
|
|
|
|
attributes = append(attributes, [2]string{
|
|
|
|
AttributeLockEnabled, "true",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if bktInfo.CID, err = n.neoFS.CreateContainer(ctx, neofs.PrmContainerCreate{
|
|
|
|
Creator: *bktInfo.Owner,
|
|
|
|
Policy: *p.Policy,
|
|
|
|
Name: p.Name,
|
|
|
|
SessionToken: p.SessionToken,
|
|
|
|
BasicACL: acl.BasicACL(p.ACL),
|
|
|
|
AdditionalAttributes: attributes,
|
2022-03-01 19:02:24 +00:00
|
|
|
}); 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))
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
return bktInfo, nil
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
2021-06-23 20:25:00 +00:00
|
|
|
|
2022-03-03 13:34:47 +00:00
|
|
|
func (n *layer) setContainerEACLTable(ctx context.Context, idCnr *cid.ID, table *eacl.Table) error {
|
|
|
|
table.SetCID(idCnr)
|
2022-01-26 09:09:28 +00:00
|
|
|
|
|
|
|
boxData, err := GetBoxData(ctx)
|
|
|
|
if err == nil {
|
2022-03-01 19:02:24 +00:00
|
|
|
table.SetSessionToken(boxData.Gate.SessionTokenForSetEACL())
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
if err := n.neoFS.SetContainerEACL(ctx, *table); err != nil {
|
2021-07-08 10:10:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-03 13:34:47 +00:00
|
|
|
return n.waitEACLPresence(ctx, *idCnr, table, defaultWaitParams())
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 13:34:47 +00:00
|
|
|
func (n *layer) GetContainerEACL(ctx context.Context, idCnr *cid.ID) (*eacl.Table, error) {
|
|
|
|
return n.neoFS.ContainerEACL(ctx, *idCnr)
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 13:34:47 +00:00
|
|
|
func (n *layer) waitEACLPresence(ctx context.Context, idCnr cid.ID, table *eacl.Table, params *waitParams) error {
|
2021-07-09 14:06:35 +00:00
|
|
|
exp, err := table.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal eacl: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 13:34:47 +00:00
|
|
|
return waitFor(ctx, params, func(ctx context.Context) bool {
|
|
|
|
eaclTable, err := n.neoFS.ContainerEACL(ctx, idCnr)
|
|
|
|
if err == nil {
|
|
|
|
got, err := eaclTable.Marshal()
|
|
|
|
if err == nil && bytes.Equal(exp, got) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) deleteContainer(ctx context.Context, idCnr *cid.ID) error {
|
|
|
|
var sessionToken *session.Token
|
|
|
|
boxData, err := GetBoxData(ctx)
|
|
|
|
if err == nil {
|
|
|
|
sessionToken = boxData.Gate.SessionTokenForDelete()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = n.neoFS.DeleteContainer(ctx, *idCnr, sessionToken); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.waitForContainerRemoved(ctx, idCnr, defaultWaitParams())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) waitForContainerRemoved(ctx context.Context, idCnr *cid.ID, params *waitParams) error {
|
|
|
|
return waitFor(ctx, params, func(ctx context.Context) bool {
|
|
|
|
_, err := n.neoFS.Container(ctx, *idCnr)
|
|
|
|
// TODO: (neofs-s3-gw#367) handle NeoFS API status error
|
|
|
|
if err != nil && strings.Contains(err.Error(), "container not found") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitFor await that given condition will be met in waitParams time.
|
|
|
|
func waitFor(ctx context.Context, params *waitParams, condition func(context.Context) bool) error {
|
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:
|
2022-03-03 13:34:47 +00:00
|
|
|
if condition(ctx) {
|
|
|
|
return nil
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|
|
|
|
ticker.Reset(params.PollInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|