2020-08-03 11:48:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-22 00:19:16 +00:00
|
|
|
"strconv"
|
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"
|
2022-04-21 08:49:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
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-05-27 11:53:40 +00:00
|
|
|
defaultLocationConstraint = "default"
|
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-05-25 17:25:43 +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,
|
2022-05-25 17:25:43 +00:00
|
|
|
Name: idCnr.EncodeToString(),
|
2020-10-22 00:19:16 +00:00
|
|
|
}
|
|
|
|
)
|
2022-05-25 17:25:43 +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
|
|
|
|
2022-04-21 08:49:56 +00:00
|
|
|
if client.IsErrContainerNotFound(err) {
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +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-04-25 09:57:58 +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 {
|
2022-05-25 17:25:43 +00:00
|
|
|
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
|
2022-04-25 09:57:58 +00:00
|
|
|
ownerID := n.Owner(ctx)
|
2022-05-27 11:53:40 +00:00
|
|
|
if p.LocationConstraint == "" {
|
|
|
|
p.LocationConstraint = defaultLocationConstraint // s3tests_boto3.functional.test_s3:test_bucket_get_location
|
|
|
|
}
|
2021-09-10 06:56:56 +00:00
|
|
|
bktInfo := &data.BucketInfo{
|
2022-01-11 12:33:09 +00:00
|
|
|
Name: p.Name,
|
2022-05-25 17:25:43 +00:00
|
|
|
Owner: ownerID,
|
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
|
|
|
|
2022-05-27 11:53:40 +00:00
|
|
|
attributes = append(attributes, [2]string{
|
|
|
|
attributeLocationConstraint, p.LocationConstraint,
|
|
|
|
})
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
if p.ObjectLockEnabled {
|
|
|
|
attributes = append(attributes, [2]string{
|
|
|
|
AttributeLockEnabled, "true",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
idCnr, err := n.neoFS.CreateContainer(ctx, neofs.PrmContainerCreate{
|
|
|
|
Creator: bktInfo.Owner,
|
2022-03-04 13:07:27 +00:00
|
|
|
Policy: *p.Policy,
|
|
|
|
Name: p.Name,
|
|
|
|
SessionToken: p.SessionToken,
|
|
|
|
BasicACL: acl.BasicACL(p.ACL),
|
|
|
|
AdditionalAttributes: attributes,
|
2022-05-25 17:25:43 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-08 10:10:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
bktInfo.CID = *idCnr
|
|
|
|
|
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-05-25 17:25:43 +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-04-07 14:36:44 +00:00
|
|
|
return n.neoFS.SetContainerEACL(ctx, *table)
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +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
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func (n *layer) deleteContainer(ctx context.Context, idCnr cid.ID) error {
|
2022-05-04 12:29:11 +00:00
|
|
|
var sessionToken *session.Container
|
2022-03-03 13:34:47 +00:00
|
|
|
boxData, err := GetBoxData(ctx)
|
|
|
|
if err == nil {
|
|
|
|
sessionToken = boxData.Gate.SessionTokenForDelete()
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
return n.neoFS.DeleteContainer(ctx, idCnr, sessionToken)
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|