2023-06-13 09:35:40 +00:00
|
|
|
package frostfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-07-15 12:47:19 +00:00
|
|
|
"io"
|
2023-06-13 09:35:40 +00:00
|
|
|
"strconv"
|
2024-08-30 12:05:32 +00:00
|
|
|
"strings"
|
2023-06-13 09:35:40 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
objectv2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2024-07-15 12:47:19 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-06-13 09:35:40 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/authmate"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/tokens"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/crdt"
|
2024-07-15 12:47:19 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-06-13 09:35:40 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2024-04-16 08:20:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-06-13 09:35:40 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2024-07-15 12:47:19 +00:00
|
|
|
"go.uber.org/zap"
|
2023-06-13 09:35:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
accessBoxCRDTNameAttr = "S3-Access-Box-CRDT-Name"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AuthmateFrostFS is a mediator which implements authmate.FrostFS through pool.Pool.
|
|
|
|
type AuthmateFrostFS struct {
|
2024-09-27 08:14:45 +00:00
|
|
|
frostFS frostfs.FrostFS
|
2024-07-15 12:47:19 +00:00
|
|
|
log *zap.Logger
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAuthmateFrostFS creates new AuthmateFrostFS using provided pool.Pool.
|
2024-09-27 08:14:45 +00:00
|
|
|
func NewAuthmateFrostFS(frostFS frostfs.FrostFS, log *zap.Logger) *AuthmateFrostFS {
|
2024-07-15 12:47:19 +00:00
|
|
|
return &AuthmateFrostFS{frostFS: frostFS, log: log}
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerExists implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) ContainerExists(ctx context.Context, idCnr cid.ID) error {
|
2024-09-27 08:14:45 +00:00
|
|
|
_, err := x.frostFS.Container(ctx, frostfs.PrmContainer{ContainerID: idCnr})
|
2023-06-13 09:35:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get container via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimeToEpoch implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) TimeToEpoch(ctx context.Context, futureTime time.Time) (uint64, uint64, error) {
|
|
|
|
return x.frostFS.TimeToEpoch(ctx, time.Now(), futureTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateContainer implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) CreateContainer(ctx context.Context, prm authmate.PrmContainerCreate) (cid.ID, error) {
|
|
|
|
basicACL := acl.Private
|
|
|
|
// allow reading objects to OTHERS in order to provide read access to S3 gateways
|
|
|
|
basicACL.AllowOp(acl.OpObjectGet, acl.RoleOthers)
|
|
|
|
basicACL.AllowOp(acl.OpObjectHead, acl.RoleOthers)
|
|
|
|
basicACL.AllowOp(acl.OpObjectSearch, acl.RoleOthers)
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
res, err := x.frostFS.CreateContainer(ctx, frostfs.PrmContainerCreate{
|
2023-06-13 09:35:40 +00:00
|
|
|
Creator: prm.Owner,
|
|
|
|
Policy: prm.Policy,
|
|
|
|
Name: prm.FriendlyName,
|
|
|
|
BasicACL: basicACL,
|
|
|
|
})
|
2023-08-25 10:06:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.ID{}, err
|
|
|
|
}
|
|
|
|
return res.ContainerID, nil
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
// GetCredsObject implements authmate.FrostFS interface method.
|
2024-08-30 12:05:32 +00:00
|
|
|
func (x *AuthmateFrostFS) GetCredsObject(ctx context.Context, prm tokens.PrmGetCredsObject) (*object.Object, error) {
|
|
|
|
versions, err := x.getCredVersions(ctx, prm.Container, prm.AccessKeyID)
|
2023-06-13 09:35:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
var addr oid.Address
|
|
|
|
isCustom := addr.DecodeString(strings.ReplaceAll(prm.AccessKeyID, "0", "/")) != nil
|
|
|
|
|
|
|
|
var credObjID oid.ID
|
2023-06-13 09:35:40 +00:00
|
|
|
if last := versions.GetLast(); last != nil {
|
2024-05-24 18:31:11 +00:00
|
|
|
credObjID = last.ObjID
|
2024-08-30 12:05:32 +00:00
|
|
|
} else if !isCustom {
|
|
|
|
credObjID = addr.Object()
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("%w: '%s'", tokens.ErrCustomAccessKeyIDNotFound, prm.AccessKeyID)
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
res, err := x.frostFS.GetObject(ctx, frostfs.PrmObjectGet{
|
2024-08-30 12:05:32 +00:00
|
|
|
Container: prm.Container,
|
2024-07-15 12:47:19 +00:00
|
|
|
Object: credObjID,
|
2023-06-13 09:35:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-15 12:47:19 +00:00
|
|
|
defer func() {
|
|
|
|
if closeErr := res.Payload.Close(); closeErr != nil {
|
|
|
|
x.reqLogger(ctx).Warn(logs.CloseCredsObjectPayload, zap.Error(closeErr))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
data, err := io.ReadAll(res.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Header.SetPayload(data)
|
|
|
|
|
|
|
|
return &res.Header, err
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateObject implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) CreateObject(ctx context.Context, prm tokens.PrmObjectCreate) (oid.ID, error) {
|
|
|
|
attributes := [][2]string{{objectv2.SysAttributeExpEpoch, strconv.FormatUint(prm.ExpirationEpoch, 10)}}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
if prm.NewVersionForAccessKeyID != "" {
|
|
|
|
versions, err := x.getCredVersions(ctx, prm.Container, prm.NewVersionForAccessKeyID)
|
2023-06-13 09:35:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return oid.ID{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if versions.GetLast() == nil {
|
2024-08-30 12:05:32 +00:00
|
|
|
var addr oid.Address
|
|
|
|
isCustom := addr.DecodeString(strings.ReplaceAll(prm.NewVersionForAccessKeyID, "0", "/")) != nil
|
|
|
|
|
|
|
|
if isCustom {
|
|
|
|
return oid.ID{}, fmt.Errorf("creds object for accessKeyId '%s' not found", prm.NewVersionForAccessKeyID)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:31:11 +00:00
|
|
|
versions.AppendVersion(&crdt.ObjectVersion{ObjID: addr.Object()})
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range versions.GetCRDTHeaders() {
|
|
|
|
attributes = append(attributes, [2]string{key, val})
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes = append(attributes, [2]string{accessBoxCRDTNameAttr, versions.Name()})
|
2024-08-30 12:05:32 +00:00
|
|
|
} else if prm.CustomAccessKey != "" {
|
|
|
|
attributes = append(attributes, [2]string{accessBoxCRDTNameAttr, prm.CustomAccessKey})
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 07:00:04 +00:00
|
|
|
for _, attr := range prm.CustomAttributes {
|
|
|
|
// we don't check attribute duplication since storage node does this
|
|
|
|
attributes = append(attributes, [2]string{attr.Key(), attr.Value()})
|
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
res, err := x.frostFS.CreateObject(ctx, frostfs.PrmObjectCreate{
|
2023-06-13 09:35:40 +00:00
|
|
|
Container: prm.Container,
|
|
|
|
Filepath: prm.Filepath,
|
|
|
|
Attributes: attributes,
|
|
|
|
Payload: bytes.NewReader(prm.Payload),
|
|
|
|
})
|
2024-07-22 09:00:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return oid.ID{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.ObjectID, nil
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (x *AuthmateFrostFS) getCredVersions(ctx context.Context, cnrID cid.ID, accessKeyID string) (*crdt.ObjectVersions, error) {
|
2024-09-27 08:14:45 +00:00
|
|
|
credVersions, err := x.frostFS.SearchObjects(ctx, frostfs.PrmObjectSearch{
|
2024-08-30 12:05:32 +00:00
|
|
|
Container: cnrID,
|
|
|
|
ExactAttribute: [2]string{accessBoxCRDTNameAttr, accessKeyID},
|
2023-06-13 09:35:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("search s3 access boxes: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
versions := crdt.NewObjectVersions(accessKeyID)
|
2023-06-13 09:35:40 +00:00
|
|
|
|
|
|
|
for _, id := range credVersions {
|
2024-09-27 08:14:45 +00:00
|
|
|
objVersion, err := x.frostFS.HeadObject(ctx, frostfs.PrmObjectHead{
|
2024-08-30 12:05:32 +00:00
|
|
|
Container: cnrID,
|
2024-07-15 12:47:19 +00:00
|
|
|
Object: id,
|
2023-06-13 09:35:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("head crdt access box '%s': %w", id.EncodeToString(), err)
|
|
|
|
}
|
|
|
|
|
2024-07-15 12:47:19 +00:00
|
|
|
versions.AppendVersion(crdt.NewObjectVersion(objVersion))
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return versions, nil
|
|
|
|
}
|
|
|
|
|
2024-07-15 12:47:19 +00:00
|
|
|
func (x *AuthmateFrostFS) reqLogger(ctx context.Context) *zap.Logger {
|
|
|
|
reqLogger := middleware.GetReqLog(ctx)
|
|
|
|
if reqLogger != nil {
|
|
|
|
return reqLogger
|
|
|
|
}
|
|
|
|
return x.log
|
|
|
|
}
|