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
|
|
|
"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"
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2023-11-16 12:10:51 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
2020-08-03 11:48:33 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
func (n *Layer) containerInfo(ctx context.Context, prm frostfs.PrmContainer) (*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
|
2024-03-01 14:11:07 +00:00
|
|
|
log = n.reqLogger(ctx).With(zap.Stringer("cid", prm.ContainerID))
|
2020-10-22 00:19:16 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
info = &data.BucketInfo{
|
2024-03-01 14:11:07 +00:00
|
|
|
CID: prm.ContainerID,
|
|
|
|
Name: prm.ContainerID.EncodeToString(),
|
2020-10-22 00:19:16 +00:00
|
|
|
}
|
2023-11-16 12:10:51 +00:00
|
|
|
|
|
|
|
reqInfo = middleware.GetReqInfo(ctx)
|
2020-10-22 00:19:16 +00:00
|
|
|
)
|
2024-03-01 14:11:07 +00:00
|
|
|
|
|
|
|
res, err = n.frostFS.Container(ctx, prm)
|
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)
|
2023-08-25 10:06:43 +00:00
|
|
|
info.HomomorphicHashDisabled = container.IsHomomorphicHashingDisabled(cnr)
|
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 {
|
2023-08-23 11:07:52 +00:00
|
|
|
log.Error(logs.CouldNotParseContainerObjectLockEnabledAttribute,
|
2022-07-14 10:23:16 +00:00
|
|
|
zap.String("lock_enabled", attrLockEnabled),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
2020-10-22 00:19:16 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2024-09-03 14:50:51 +00:00
|
|
|
zone := n.features.FormContainerZone(reqInfo.Namespace)
|
2023-11-23 07:44:50 +00:00
|
|
|
if zone != info.Zone {
|
2024-03-01 14:11:07 +00:00
|
|
|
return nil, fmt.Errorf("ns '%s' and zone '%s' are mismatched for container '%s'", zone, info.Zone, prm.ContainerID)
|
2023-11-23 07:44:50 +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
|
|
|
}
|
|
|
|
|
2024-06-25 12:57:55 +00:00
|
|
|
func (n *Layer) containerList(ctx context.Context) ([]*data.BucketInfo, error) {
|
2024-03-01 14:11:07 +00:00
|
|
|
stoken := n.SessionTokenForRead(ctx)
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
prm := frostfs.PrmUserContainers{
|
2024-03-01 14:11:07 +00:00
|
|
|
UserID: n.BearerOwner(ctx),
|
|
|
|
SessionToken: stoken,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := n.frostFS.UserContainers(ctx, prm)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
n.reqLogger(ctx).Error(logs.CouldNotListUserContainers, 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 {
|
2024-09-27 08:14:45 +00:00
|
|
|
getPrm := frostfs.PrmContainer{
|
2024-03-01 14:11:07 +00:00
|
|
|
ContainerID: res[i],
|
|
|
|
SessionToken: stoken,
|
|
|
|
}
|
|
|
|
info, err := n.containerInfo(ctx, getPrm)
|
2020-08-03 11:48:33 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
n.reqLogger(ctx).Error(logs.CouldNotFetchContainerInfo, 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
|
|
|
|
2024-06-25 12:57:55 +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
|
|
|
}
|
2023-11-23 07:44:50 +00:00
|
|
|
|
2024-09-03 14:50:51 +00:00
|
|
|
zone := n.features.FormContainerZone(p.Namespace)
|
2023-11-23 07:44:50 +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-11-23 07:44:50 +00:00
|
|
|
Zone: zone,
|
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
|
|
|
|
2024-02-12 08:00:04 +00:00
|
|
|
attributes := [][2]string{
|
|
|
|
{attributeLocationConstraint, p.LocationConstraint},
|
|
|
|
}
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
if p.ObjectLockEnabled {
|
|
|
|
attributes = append(attributes, [2]string{
|
|
|
|
AttributeLockEnabled, "true",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
res, err := n.frostFS.CreateContainer(ctx, frostfs.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,
|
2023-11-23 07:44:50 +00:00
|
|
|
Zone: zone,
|
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,
|
2024-05-28 12:50:34 +00:00
|
|
|
BasicACL: 0, // means APE
|
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
|
|
|
}
|
|
|
|
|
2023-08-25 10:06:43 +00:00
|
|
|
bktInfo.CID = res.ContainerID
|
|
|
|
bktInfo.HomomorphicHashDisabled = res.HomomorphicHashDisabled
|
2022-05-25 17:25:43 +00:00
|
|
|
|
2023-11-23 07:44:50 +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
|
|
|
}
|