2020-08-03 11:48:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-22 19:40:52 +00:00
|
|
|
"fmt"
|
2020-10-22 00:19:16 +00:00
|
|
|
"strconv"
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
v2container "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-06-30 09:03:55 +00:00
|
|
|
s3errors "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-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-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
|
2023-06-09 13:19:23 +00:00
|
|
|
log = n.reqLogger(ctx).With(zap.Stringer("cid", idCnr))
|
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-12-20 08:38:58 +00:00
|
|
|
res, err = n.frostFS.Container(ctx, idCnr)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if client.IsErrContainerNotFound(err) {
|
2023-06-30 09:03:55 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", s3errors.GetAPIError(s3errors.ErrNoSuchBucket), err.Error())
|
2021-07-09 08:57:44 +00:00
|
|
|
}
|
2022-12-20 08:38:58 +00:00
|
|
|
return nil, fmt.Errorf("get frostfs container: %w", err)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 14:43:52 +00:00
|
|
|
cnr := *res
|
2020-10-23 00:12:37 +00:00
|
|
|
|
2022-06-29 14:43:52 +00:00
|
|
|
info.Owner = cnr.Owner()
|
2022-10-26 13:56:01 +00:00
|
|
|
if domain := container.ReadDomain(cnr); domain.Name() != "" {
|
|
|
|
info.Name = domain.Name()
|
2023-02-10 12:09:32 +00:00
|
|
|
info.Zone = domain.Zone()
|
2022-10-26 13:56:01 +00:00
|
|
|
}
|
2022-06-29 14:43:52 +00:00
|
|
|
info.Created = container.CreatedAt(cnr)
|
|
|
|
info.LocationConstraint = cnr.Attribute(attributeLocationConstraint)
|
2020-10-22 00:19:16 +00:00
|
|
|
|
2022-06-29 14:43:52 +00:00
|
|
|
attrLockEnabled := cnr.Attribute(AttributeLockEnabled)
|
2022-07-14 10:23:16 +00:00
|
|
|
if len(attrLockEnabled) > 0 {
|
|
|
|
info.ObjectLockEnabled, err = strconv.ParseBool(attrLockEnabled)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not parse container object lock enabled attribute",
|
|
|
|
zap.String("lock_enabled", attrLockEnabled),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
2020-10-22 00:19:16 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.PutBucket(info)
|
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) {
|
2023-08-03 12:08:22 +00:00
|
|
|
res, err := n.frostFS.UserContainers(ctx, n.BearerOwner(ctx))
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2023-06-09 13:19:23 +00:00
|
|
|
n.reqLogger(ctx).Error("could not list user containers", zap.Error(err))
|
2020-08-03 11:48:33 +00:00
|
|
|
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 {
|
2023-06-09 13:19:23 +00:00
|
|
|
n.reqLogger(ctx).Error("could not fetch container info", zap.Error(err))
|
2020-08-03 11:48:33 +00:00
|
|
|
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) {
|
2022-05-27 11:53:40 +00:00
|
|
|
if p.LocationConstraint == "" {
|
2022-05-30 11:33:46 +00:00
|
|
|
p.LocationConstraint = api.DefaultLocationConstraint // s3tests_boto3.functional.test_s3:test_bucket_get_location
|
2022-05-27 11:53:40 +00:00
|
|
|
}
|
2021-09-10 06:56:56 +00:00
|
|
|
bktInfo := &data.BucketInfo{
|
2022-01-11 12:33:09 +00:00
|
|
|
Name: p.Name,
|
2023-02-10 12:09:32 +00:00
|
|
|
Zone: v2container.SysAttributeZoneDefault,
|
2023-08-03 12:08:22 +00:00
|
|
|
Owner: n.BearerOwner(ctx),
|
2022-11-08 09:12:55 +00:00
|
|
|
Created: TimeNow(ctx),
|
2022-01-11 12:33:09 +00:00
|
|
|
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-12-20 08:38:58 +00:00
|
|
|
idCnr, err := n.frostFS.CreateContainer(ctx, PrmContainerCreate{
|
2022-05-25 17:25:43 +00:00
|
|
|
Creator: bktInfo.Owner,
|
2022-06-15 19:31:41 +00:00
|
|
|
Policy: p.Policy,
|
2022-03-04 13:07:27 +00:00
|
|
|
Name: p.Name,
|
2022-06-21 15:21:20 +00:00
|
|
|
SessionToken: p.SessionContainerCreation,
|
2022-11-08 09:12:55 +00:00
|
|
|
CreationTime: bktInfo.Created,
|
2022-03-04 13:07:27 +00:00
|
|
|
AdditionalAttributes: attributes,
|
2022-05-25 17:25:43 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("create container: %w", err)
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
bktInfo.CID = idCnr
|
2022-05-25 17:25:43 +00:00
|
|
|
|
2022-06-21 15:21:20 +00:00
|
|
|
if err = n.setContainerEACLTable(ctx, bktInfo.CID, p.EACL, p.SessionEACL); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("set container eacl: %w", err)
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.PutBucket(bktInfo)
|
2021-08-19 06:55:22 +00:00
|
|
|
|
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-06-21 15:21:20 +00:00
|
|
|
func (n *layer) setContainerEACLTable(ctx context.Context, idCnr cid.ID, table *eacl.Table, sessionToken *session.Container) error {
|
2022-05-25 17:25:43 +00:00
|
|
|
table.SetCID(idCnr)
|
2022-01-26 09:09:28 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
return n.frostFS.SetContainerEACL(ctx, *table, sessionToken)
|
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) {
|
2022-12-20 08:38:58 +00:00
|
|
|
return n.frostFS.ContainerEACL(ctx, idCnr)
|
2021-07-08 10:10:46 +00:00
|
|
|
}
|