2022-12-20 08:38:58 +00:00
|
|
|
package frostfs
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-03-05 08:53:01 +00:00
|
|
|
"math"
|
2022-03-01 19:02:24 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
objectv2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/authmate"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/tokens"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
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/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-03-01 19:02:24 +00:00
|
|
|
)
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// FrostFS represents virtual connection to the FrostFS network.
|
2022-03-01 19:02:24 +00:00
|
|
|
// It is used to provide an interface to dependent packages
|
2022-12-20 08:38:58 +00:00
|
|
|
// which work with FrostFS.
|
|
|
|
type FrostFS struct {
|
2022-05-04 10:00:14 +00:00
|
|
|
pool *pool.Pool
|
|
|
|
await pool.WaitParams
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 10:00:14 +00:00
|
|
|
const (
|
|
|
|
defaultPollInterval = time.Second // overrides default value from pool
|
|
|
|
defaultPollTimeout = 120 * time.Second // same as default value from pool
|
|
|
|
)
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// NewFrostFS creates new FrostFS using provided pool.Pool.
|
|
|
|
func NewFrostFS(p *pool.Pool) *FrostFS {
|
2022-05-04 10:00:14 +00:00
|
|
|
var await pool.WaitParams
|
|
|
|
await.SetPollInterval(defaultPollInterval)
|
|
|
|
await.SetTimeout(defaultPollTimeout)
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
return &FrostFS{
|
2022-05-04 10:00:14 +00:00
|
|
|
pool: p,
|
|
|
|
await: await,
|
|
|
|
}
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// TimeToEpoch implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) TimeToEpoch(ctx context.Context, now, futureTime time.Time) (uint64, uint64, error) {
|
2022-03-05 08:53:01 +00:00
|
|
|
dur := futureTime.Sub(now)
|
|
|
|
if dur < 0 {
|
|
|
|
return 0, 0, fmt.Errorf("time '%s' must be in the future (after %s)",
|
|
|
|
futureTime.Format(time.RFC3339), now.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
networkInfo, err := x.pool.NetworkInfo(ctx)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-03-05 08:53:01 +00:00
|
|
|
return 0, 0, fmt.Errorf("get network info via client: %w", err)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 19:31:41 +00:00
|
|
|
durEpoch := networkInfo.EpochDuration()
|
2022-03-01 19:02:24 +00:00
|
|
|
if durEpoch == 0 {
|
2022-03-05 08:53:01 +00:00
|
|
|
return 0, 0, errors.New("epoch duration is missing or zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
curr := networkInfo.CurrentEpoch()
|
|
|
|
msPerEpoch := durEpoch * uint64(networkInfo.MsPerBlock())
|
|
|
|
|
|
|
|
epochLifetime := uint64(dur.Milliseconds()) / msPerEpoch
|
|
|
|
if uint64(dur.Milliseconds())%msPerEpoch != 0 {
|
|
|
|
epochLifetime++
|
|
|
|
}
|
|
|
|
|
|
|
|
var epoch uint64
|
|
|
|
if epochLifetime >= math.MaxUint64-curr {
|
|
|
|
epoch = math.MaxUint64
|
|
|
|
} else {
|
|
|
|
epoch = curr + epochLifetime
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 08:53:01 +00:00
|
|
|
return curr, epoch, nil
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// Container implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) Container(ctx context.Context, idCnr cid.ID) (*container.Container, error) {
|
2022-04-07 14:36:44 +00:00
|
|
|
var prm pool.PrmContainerGet
|
|
|
|
prm.SetContainerID(idCnr)
|
|
|
|
|
|
|
|
res, err := x.pool.GetContainer(ctx, prm)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read container via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:03:48 +00:00
|
|
|
return &res, nil
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:43:40 +00:00
|
|
|
var basicACLZero acl.Basic
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// CreateContainer implements frostfs.FrostFS interface method.
|
2022-06-10 01:59:17 +00:00
|
|
|
//
|
|
|
|
// If prm.BasicACL is zero, 'eacl-public-read-write' is used.
|
2022-12-20 08:38:58 +00:00
|
|
|
func (x *FrostFS) CreateContainer(ctx context.Context, prm layer.PrmContainerCreate) (cid.ID, error) {
|
2022-06-17 14:43:40 +00:00
|
|
|
if prm.BasicACL == basicACLZero {
|
|
|
|
prm.BasicACL = acl.PublicRWExtended
|
2022-06-10 01:59:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 14:43:52 +00:00
|
|
|
var cnr container.Container
|
|
|
|
cnr.Init()
|
|
|
|
cnr.SetPlacementPolicy(prm.Policy)
|
|
|
|
cnr.SetOwner(prm.Creator)
|
|
|
|
cnr.SetBasicACL(prm.BasicACL)
|
2022-11-08 09:12:55 +00:00
|
|
|
|
|
|
|
creationTime := prm.CreationTime
|
|
|
|
if creationTime.IsZero() {
|
|
|
|
creationTime = time.Now()
|
|
|
|
}
|
|
|
|
container.SetCreationTime(&cnr, creationTime)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
if prm.Name != "" {
|
2022-06-30 08:58:51 +00:00
|
|
|
var d container.Domain
|
|
|
|
d.SetName(prm.Name)
|
|
|
|
|
|
|
|
container.WriteDomain(&cnr, d)
|
2022-06-29 14:43:52 +00:00
|
|
|
container.SetName(&cnr, prm.Name)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 14:43:52 +00:00
|
|
|
for i := range prm.AdditionalAttributes {
|
|
|
|
cnr.SetAttribute(prm.AdditionalAttributes[i][0], prm.AdditionalAttributes[i][1])
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 12:47:21 +00:00
|
|
|
err := pool.SyncContainerWithNetwork(ctx, &cnr, x.pool)
|
|
|
|
if err != nil {
|
2022-06-27 09:08:26 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("sync container with the network state: %w", err)
|
2022-05-16 07:46:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
var prmPut pool.PrmContainerPut
|
2022-06-29 14:43:52 +00:00
|
|
|
prmPut.SetContainer(cnr)
|
2022-05-04 10:00:14 +00:00
|
|
|
prmPut.SetWaitParams(x.await)
|
2022-04-07 14:36:44 +00:00
|
|
|
|
2022-06-21 15:21:20 +00:00
|
|
|
if prm.SessionToken != nil {
|
|
|
|
prmPut.WithinSession(*prm.SessionToken)
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
// send request to save the container
|
2022-04-07 14:36:44 +00:00
|
|
|
idCnr, err := x.pool.PutContainer(ctx, prmPut)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-06-27 09:08:26 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("save container via connection pool: %w", err)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:03:48 +00:00
|
|
|
return idCnr, nil
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// UserContainers implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) UserContainers(ctx context.Context, id user.ID) ([]cid.ID, error) {
|
2022-04-07 14:36:44 +00:00
|
|
|
var prm pool.PrmContainerList
|
|
|
|
prm.SetOwnerID(id)
|
|
|
|
|
|
|
|
r, err := x.pool.ListContainers(ctx, prm)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("list user containers via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-16 09:02:16 +00:00
|
|
|
return r, nil
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// SetContainerEACL implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) SetContainerEACL(ctx context.Context, table eacl.Table, sessionToken *session.Container) error {
|
2022-04-07 14:36:44 +00:00
|
|
|
var prm pool.PrmContainerSetEACL
|
|
|
|
prm.SetTable(table)
|
2022-05-04 10:00:14 +00:00
|
|
|
prm.SetWaitParams(x.await)
|
2022-04-07 14:36:44 +00:00
|
|
|
|
2022-06-21 15:21:20 +00:00
|
|
|
if sessionToken != nil {
|
|
|
|
prm.WithinSession(*sessionToken)
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
err := x.pool.SetEACL(ctx, prm)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("save eACL via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// ContainerEACL implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) ContainerEACL(ctx context.Context, id cid.ID) (*eacl.Table, error) {
|
2022-04-07 14:36:44 +00:00
|
|
|
var prm pool.PrmContainerEACL
|
|
|
|
prm.SetContainerID(id)
|
|
|
|
|
|
|
|
res, err := x.pool.GetEACL(ctx, prm)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read eACL via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:03:48 +00:00
|
|
|
return &res, nil
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// DeleteContainer implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) DeleteContainer(ctx context.Context, id cid.ID, token *session.Container) error {
|
2022-04-07 14:36:44 +00:00
|
|
|
var prm pool.PrmContainerDelete
|
|
|
|
prm.SetContainerID(id)
|
2022-05-04 10:00:14 +00:00
|
|
|
prm.SetWaitParams(x.await)
|
2022-04-07 14:36:44 +00:00
|
|
|
|
2022-06-22 13:15:45 +00:00
|
|
|
if token != nil {
|
|
|
|
prm.SetSessionToken(*token)
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
err := x.pool.DeleteContainer(ctx, prm)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete container via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// CreateObject implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) CreateObject(ctx context.Context, prm layer.PrmObjectCreate) (oid.ID, error) {
|
2022-03-01 19:02:24 +00:00
|
|
|
attrNum := len(prm.Attributes) + 1 // + creation time
|
|
|
|
|
2022-09-07 06:59:24 +00:00
|
|
|
if prm.Filepath != "" {
|
2022-03-01 19:02:24 +00:00
|
|
|
attrNum++
|
|
|
|
}
|
|
|
|
|
2022-03-16 09:02:16 +00:00
|
|
|
attrs := make([]object.Attribute, 0, attrNum)
|
2022-03-01 19:02:24 +00:00
|
|
|
var a *object.Attribute
|
|
|
|
|
|
|
|
a = object.NewAttribute()
|
|
|
|
a.SetKey(object.AttributeTimestamp)
|
2022-11-08 09:12:55 +00:00
|
|
|
|
|
|
|
creationTime := prm.CreationTime
|
|
|
|
if creationTime.IsZero() {
|
|
|
|
creationTime = time.Now()
|
|
|
|
}
|
|
|
|
a.SetValue(strconv.FormatInt(creationTime.Unix(), 10))
|
|
|
|
|
2022-03-16 09:02:16 +00:00
|
|
|
attrs = append(attrs, *a)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
for i := range prm.Attributes {
|
|
|
|
a = object.NewAttribute()
|
|
|
|
a.SetKey(prm.Attributes[i][0])
|
|
|
|
a.SetValue(prm.Attributes[i][1])
|
2022-03-16 09:02:16 +00:00
|
|
|
attrs = append(attrs, *a)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:59:24 +00:00
|
|
|
if prm.Filepath != "" {
|
2022-03-01 19:02:24 +00:00
|
|
|
a = object.NewAttribute()
|
2022-09-07 06:59:24 +00:00
|
|
|
a.SetKey(object.AttributeFilePath)
|
|
|
|
a.SetValue(prm.Filepath)
|
2022-03-16 09:02:16 +00:00
|
|
|
attrs = append(attrs, *a)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 14:36:30 +00:00
|
|
|
obj := object.New()
|
2022-04-25 09:57:58 +00:00
|
|
|
obj.SetContainerID(prm.Container)
|
2022-03-04 14:36:30 +00:00
|
|
|
obj.SetOwnerID(&prm.Creator)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
obj.SetPayloadSize(prm.PayloadSize)
|
|
|
|
|
|
|
|
if len(prm.Locks) > 0 {
|
|
|
|
lock := new(object.Lock)
|
|
|
|
lock.WriteMembers(prm.Locks)
|
|
|
|
objectv2.WriteLock(obj.ToV2(), (objectv2.Lock)(*lock))
|
|
|
|
}
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
var prmPut pool.PrmObjectPut
|
|
|
|
prmPut.SetHeader(*obj)
|
|
|
|
prmPut.SetPayload(prm.Payload)
|
2023-04-24 23:49:12 +00:00
|
|
|
prmPut.SetCopiesNumberVector(prm.CopiesNumber)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
2022-04-25 09:57:58 +00:00
|
|
|
prmPut.UseBearer(*prm.BearerToken)
|
2022-03-01 19:02:24 +00:00
|
|
|
} else {
|
2022-04-07 14:36:44 +00:00
|
|
|
prmPut.UseKey(prm.PrivateKey)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
idObj, err := x.pool.PutObject(ctx, prmPut)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-06-08 06:05:07 +00:00
|
|
|
reason, ok := isErrAccessDenied(err)
|
|
|
|
if ok {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("%w: %s", layer.ErrAccessDenied, reason)
|
2022-06-08 06:05:07 +00:00
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("save object via connection pool: %w", err)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:03:48 +00:00
|
|
|
return idObj, nil
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wraps io.ReadCloser and transforms Read errors related to access violation
|
2022-12-20 08:38:58 +00:00
|
|
|
// to frostfs.ErrAccessDenied.
|
2022-03-01 19:02:24 +00:00
|
|
|
type payloadReader struct {
|
|
|
|
io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x payloadReader) Read(p []byte) (int, error) {
|
|
|
|
n, err := x.ReadCloser.Read(p)
|
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if reason, ok := isErrAccessDenied(err); ok {
|
2022-06-06 11:09:09 +00:00
|
|
|
return n, fmt.Errorf("%w: %s", layer.ErrAccessDenied, reason)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// ReadObject implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) ReadObject(ctx context.Context, prm layer.PrmObjectRead) (*layer.ObjectPart, error) {
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(prm.Container)
|
|
|
|
addr.SetObject(prm.Object)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
var prmGet pool.PrmObjectGet
|
|
|
|
prmGet.SetAddress(addr)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
2022-04-25 09:57:58 +00:00
|
|
|
prmGet.UseBearer(*prm.BearerToken)
|
2022-03-01 19:02:24 +00:00
|
|
|
} else {
|
2022-04-07 14:36:44 +00:00
|
|
|
prmGet.UseKey(prm.PrivateKey)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if prm.WithHeader {
|
|
|
|
if prm.WithPayload {
|
2022-04-07 14:36:44 +00:00
|
|
|
res, err := x.pool.GetObject(ctx, prmGet)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if reason, ok := isErrAccessDenied(err); ok {
|
2022-06-06 11:09:09 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", layer.ErrAccessDenied, reason)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("init full object reading via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-02 14:27:32 +00:00
|
|
|
defer res.Payload.Close()
|
|
|
|
|
|
|
|
payload, err := io.ReadAll(res.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read full object payload: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:37:48 +00:00
|
|
|
res.Header.SetPayload(payload)
|
2022-03-02 14:27:32 +00:00
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
return &layer.ObjectPart{
|
2022-03-02 14:27:32 +00:00
|
|
|
Head: &res.Header,
|
2022-03-01 19:02:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
var prmHead pool.PrmObjectHead
|
|
|
|
prmHead.SetAddress(addr)
|
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
2022-04-25 09:57:58 +00:00
|
|
|
prmHead.UseBearer(*prm.BearerToken)
|
2022-04-07 14:36:44 +00:00
|
|
|
} else {
|
|
|
|
prmHead.UseKey(prm.PrivateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr, err := x.pool.HeadObject(ctx, prmHead)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if reason, ok := isErrAccessDenied(err); ok {
|
2022-06-06 11:09:09 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", layer.ErrAccessDenied, reason)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("read object header via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
return &layer.ObjectPart{
|
2022-08-24 16:03:48 +00:00
|
|
|
Head: &hdr,
|
2022-03-01 19:02:24 +00:00
|
|
|
}, nil
|
|
|
|
} else if prm.PayloadRange[0]+prm.PayloadRange[1] == 0 {
|
2022-04-07 14:36:44 +00:00
|
|
|
res, err := x.pool.GetObject(ctx, prmGet)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if reason, ok := isErrAccessDenied(err); ok {
|
2022-06-06 11:09:09 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", layer.ErrAccessDenied, reason)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("init full payload range reading via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
return &layer.ObjectPart{
|
2022-03-01 19:02:24 +00:00
|
|
|
Payload: res.Payload,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
var prmRange pool.PrmObjectRange
|
|
|
|
prmRange.SetAddress(addr)
|
|
|
|
prmRange.SetOffset(prm.PayloadRange[0])
|
|
|
|
prmRange.SetLength(prm.PayloadRange[1])
|
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
2022-04-25 09:57:58 +00:00
|
|
|
prmRange.UseBearer(*prm.BearerToken)
|
2022-04-07 14:36:44 +00:00
|
|
|
} else {
|
|
|
|
prmRange.UseKey(prm.PrivateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := x.pool.ObjectRange(ctx, prmRange)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if reason, ok := isErrAccessDenied(err); ok {
|
2022-06-06 11:09:09 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", layer.ErrAccessDenied, reason)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("init payload range reading via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
return &layer.ObjectPart{
|
2022-08-24 16:03:48 +00:00
|
|
|
Payload: payloadReader{&res},
|
2022-03-01 19:02:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// DeleteObject implements frostfs.FrostFS interface method.
|
|
|
|
func (x *FrostFS) DeleteObject(ctx context.Context, prm layer.PrmObjectDelete) error {
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(prm.Container)
|
|
|
|
addr.SetObject(prm.Object)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
var prmDelete pool.PrmObjectDelete
|
|
|
|
prmDelete.SetAddress(addr)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
2022-04-25 09:57:58 +00:00
|
|
|
prmDelete.UseBearer(*prm.BearerToken)
|
2022-03-01 19:02:24 +00:00
|
|
|
} else {
|
2022-04-07 14:36:44 +00:00
|
|
|
prmDelete.UseKey(prm.PrivateKey)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 14:36:44 +00:00
|
|
|
err := x.pool.DeleteObject(ctx, prmDelete)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
2022-04-21 08:49:56 +00:00
|
|
|
if reason, ok := isErrAccessDenied(err); ok {
|
2022-06-06 11:09:09 +00:00
|
|
|
return fmt.Errorf("%w: %s", layer.ErrAccessDenied, reason)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("mark object removal via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:49:56 +00:00
|
|
|
func isErrAccessDenied(err error) (string, bool) {
|
2022-06-08 06:05:07 +00:00
|
|
|
unwrappedErr := errors.Unwrap(err)
|
|
|
|
for unwrappedErr != nil {
|
|
|
|
err = unwrappedErr
|
|
|
|
unwrappedErr = errors.Unwrap(err)
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:49:56 +00:00
|
|
|
switch err := err.(type) {
|
|
|
|
default:
|
|
|
|
return "", false
|
|
|
|
case apistatus.ObjectAccessDenied:
|
|
|
|
return err.Reason(), true
|
|
|
|
case *apistatus.ObjectAccessDenied:
|
|
|
|
return err.Reason(), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// ResolverFrostFS represents virtual connection to the FrostFS network.
|
|
|
|
// It implements resolver.FrostFS.
|
|
|
|
type ResolverFrostFS struct {
|
2022-03-25 10:28:39 +00:00
|
|
|
pool *pool.Pool
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// NewResolverFrostFS creates new ResolverFrostFS using provided pool.Pool.
|
|
|
|
func NewResolverFrostFS(p *pool.Pool) *ResolverFrostFS {
|
|
|
|
return &ResolverFrostFS{pool: p}
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// SystemDNS implements resolver.FrostFS interface method.
|
|
|
|
func (x *ResolverFrostFS) SystemDNS(ctx context.Context) (string, error) {
|
2022-04-07 14:36:44 +00:00
|
|
|
networkInfo, err := x.pool.NetworkInfo(ctx)
|
2022-03-01 19:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("read network info via client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:31:41 +00:00
|
|
|
domain := networkInfo.RawNetworkParameter("SystemDNS")
|
|
|
|
if domain == nil {
|
2022-03-01 19:02:24 +00:00
|
|
|
return "", errors.New("system DNS parameter not found or empty")
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:31:41 +00:00
|
|
|
return string(domain), nil
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
2022-03-25 10:28:39 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// AuthmateFrostFS is a mediator which implements authmate.FrostFS through pool.Pool.
|
|
|
|
type AuthmateFrostFS struct {
|
|
|
|
frostFS *FrostFS
|
2022-03-25 10:28:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// NewAuthmateFrostFS creates new AuthmateFrostFS using provided pool.Pool.
|
|
|
|
func NewAuthmateFrostFS(p *pool.Pool) *AuthmateFrostFS {
|
|
|
|
return &AuthmateFrostFS{frostFS: NewFrostFS(p)}
|
2022-03-25 10:28:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// ContainerExists implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) ContainerExists(ctx context.Context, idCnr cid.ID) error {
|
|
|
|
_, err := x.frostFS.Container(ctx, idCnr)
|
2022-03-25 10:28:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get container via connection pool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// 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)
|
2022-03-25 10:28:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// CreateContainer implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) CreateContainer(ctx context.Context, prm authmate.PrmContainerCreate) (cid.ID, error) {
|
2022-06-17 14:43:40 +00:00
|
|
|
basicACL := acl.Private
|
|
|
|
// allow reading objects to OTHERS in order to provide read access to S3 gateways
|
|
|
|
basicACL.AllowOp(acl.OpObjectGet, acl.RoleOthers)
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
return x.frostFS.CreateContainer(ctx, layer.PrmContainerCreate{
|
2022-03-25 10:28:39 +00:00
|
|
|
Creator: prm.Owner,
|
|
|
|
Policy: prm.Policy,
|
|
|
|
Name: prm.FriendlyName,
|
2022-06-17 14:43:40 +00:00
|
|
|
BasicACL: basicACL,
|
2022-03-25 10:28:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// ReadObjectPayload implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) ReadObjectPayload(ctx context.Context, addr oid.Address) ([]byte, error) {
|
|
|
|
res, err := x.frostFS.ReadObject(ctx, layer.PrmObjectRead{
|
2022-05-25 17:25:43 +00:00
|
|
|
Container: addr.Container(),
|
|
|
|
Object: addr.Object(),
|
2022-03-25 10:28:39 +00:00
|
|
|
WithPayload: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Payload.Close()
|
|
|
|
|
|
|
|
return io.ReadAll(res.Payload)
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// CreateObject implements authmate.FrostFS interface method.
|
|
|
|
func (x *AuthmateFrostFS) CreateObject(ctx context.Context, prm tokens.PrmObjectCreate) (oid.ID, error) {
|
|
|
|
return x.frostFS.CreateObject(ctx, layer.PrmObjectCreate{
|
2022-03-25 10:28:39 +00:00
|
|
|
Creator: prm.Creator,
|
|
|
|
Container: prm.Container,
|
2022-09-07 06:59:24 +00:00
|
|
|
Filepath: prm.Filepath,
|
2022-03-25 10:28:39 +00:00
|
|
|
Attributes: [][2]string{
|
2023-03-22 06:53:14 +00:00
|
|
|
{objectv2.SysAttributeExpEpoch, strconv.FormatUint(prm.ExpirationEpoch, 10)}},
|
2022-03-25 10:28:39 +00:00
|
|
|
Payload: bytes.NewReader(prm.Payload),
|
|
|
|
})
|
|
|
|
}
|
2022-07-27 14:52:25 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// PoolStatistic is a mediator which implements authmate.FrostFS through pool.Pool.
|
2022-07-27 14:52:25 +00:00
|
|
|
type PoolStatistic struct {
|
|
|
|
pool *pool.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPoolStatistic creates new PoolStatistic using provided pool.Pool.
|
|
|
|
func NewPoolStatistic(p *pool.Pool) *PoolStatistic {
|
|
|
|
return &PoolStatistic{pool: p}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statistic implements interface method.
|
|
|
|
func (x *PoolStatistic) Statistic() pool.Statistic {
|
|
|
|
return x.pool.Statistic()
|
|
|
|
}
|