2022-06-06 11:09:09 +00:00
|
|
|
package layer
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2022-09-06 07:43:04 +00:00
|
|
|
"errors"
|
2022-03-04 13:07:27 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-03-05 08:53:01 +00:00
|
|
|
"time"
|
2022-03-04 13:07:27 +00:00
|
|
|
|
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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum"
|
2023-04-14 12:36:22 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2023-03-07 14:38:08 +00:00
|
|
|
"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/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-03-04 13:07:27 +00:00
|
|
|
)
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
type TestFrostFS struct {
|
|
|
|
FrostFS
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
objects map[string]*object.Object
|
2023-04-24 16:40:18 +00:00
|
|
|
objectErrors map[string]error
|
2022-03-04 13:07:27 +00:00
|
|
|
containers map[string]*container.Container
|
2022-09-06 07:43:04 +00:00
|
|
|
eaclTables map[string]*eacl.Table
|
2022-03-04 13:07:27 +00:00
|
|
|
currentEpoch uint64
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func NewTestFrostFS() *TestFrostFS {
|
|
|
|
return &TestFrostFS{
|
2023-04-24 16:40:18 +00:00
|
|
|
objects: make(map[string]*object.Object),
|
|
|
|
objectErrors: make(map[string]error),
|
|
|
|
containers: make(map[string]*container.Container),
|
|
|
|
eaclTables: make(map[string]*eacl.Table),
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) CurrentEpoch() uint64 {
|
2022-03-04 13:07:27 +00:00
|
|
|
return t.currentEpoch
|
|
|
|
}
|
|
|
|
|
2023-04-24 16:40:18 +00:00
|
|
|
func (t *TestFrostFS) SetObjectError(addr oid.Address, err error) {
|
|
|
|
if err == nil {
|
|
|
|
delete(t.objectErrors, addr.EncodeToString())
|
|
|
|
} else {
|
|
|
|
t.objectErrors[addr.EncodeToString()] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) Objects() []*object.Object {
|
2022-03-04 13:07:27 +00:00
|
|
|
res := make([]*object.Object, 0, len(t.objects))
|
|
|
|
|
|
|
|
for _, obj := range t.objects {
|
|
|
|
res = append(res, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-04-14 12:36:22 +00:00
|
|
|
func (t *TestFrostFS) ObjectExists(objID oid.ID) bool {
|
|
|
|
for _, obj := range t.objects {
|
|
|
|
if id, _ := obj.ID(); id.Equals(objID) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) AddObject(key string, obj *object.Object) {
|
2022-03-04 13:07:27 +00:00
|
|
|
t.objects[key] = obj
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) ContainerID(name string) (cid.ID, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
for id, cnr := range t.containers {
|
2022-06-29 14:43:52 +00:00
|
|
|
if container.Name(*cnr) == name {
|
|
|
|
var cnrID cid.ID
|
2022-06-27 09:08:26 +00:00
|
|
|
return cnrID, cnrID.DecodeString(id)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 09:08:26 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("not found")
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) CreateContainer(_ context.Context, prm PrmContainerCreate) (cid.ID, error) {
|
2022-06-29 14:43:52 +00:00
|
|
|
var cnr container.Container
|
|
|
|
cnr.Init()
|
|
|
|
cnr.SetOwner(prm.Creator)
|
|
|
|
cnr.SetPlacementPolicy(prm.Policy)
|
|
|
|
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-04 13:07:27 +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-04 13:07:27 +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-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
2022-06-27 09:08:26 +00:00
|
|
|
return cid.ID{}, err
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var id cid.ID
|
2022-03-04 13:07:27 +00:00
|
|
|
id.SetSHA256(sha256.Sum256(b))
|
2022-06-29 14:43:52 +00:00
|
|
|
t.containers[id.EncodeToString()] = &cnr
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
return id, nil
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) DeleteContainer(_ context.Context, cnrID cid.ID, _ *session.Container) error {
|
2022-07-21 09:05:47 +00:00
|
|
|
delete(t.containers, cnrID.EncodeToString())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) Container(_ context.Context, id cid.ID) (*container.Container, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
for k, v := range t.containers {
|
2022-05-25 17:25:43 +00:00
|
|
|
if k == id.EncodeToString() {
|
2022-03-04 13:07:27 +00:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
return nil, fmt.Errorf("container not found %s", id)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) UserContainers(_ context.Context, _ user.ID) ([]cid.ID, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
var res []cid.ID
|
|
|
|
for k := range t.containers {
|
|
|
|
var idCnr cid.ID
|
2022-04-25 09:57:58 +00:00
|
|
|
if err := idCnr.DecodeString(k); err != nil {
|
2022-03-04 13:07:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, idCnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) ReadObject(ctx context.Context, prm PrmObjectRead) (*ObjectPart, error) {
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(prm.Container)
|
|
|
|
addr.SetObject(prm.Object)
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
sAddr := addr.EncodeToString()
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2023-04-24 16:40:18 +00:00
|
|
|
if err := t.objectErrors[sAddr]; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-04 13:07:27 +00:00
|
|
|
if obj, ok := t.objects[sAddr]; ok {
|
2022-10-04 08:31:09 +00:00
|
|
|
owner := getOwner(ctx)
|
2023-02-20 09:18:29 +00:00
|
|
|
if !obj.OwnerID().Equals(owner) && !t.isPublicRead(prm.Container) {
|
2022-10-04 08:31:09 +00:00
|
|
|
return nil, ErrAccessDenied
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
payload := obj.Payload()
|
|
|
|
|
|
|
|
if prm.PayloadRange[0]+prm.PayloadRange[1] > 0 {
|
|
|
|
off := prm.PayloadRange[0]
|
|
|
|
payload = payload[off : off+prm.PayloadRange[1]]
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
return &ObjectPart{
|
2022-03-04 13:07:27 +00:00
|
|
|
Head: obj,
|
2022-08-01 16:52:09 +00:00
|
|
|
Payload: io.NopCloser(bytes.NewReader(payload)),
|
2022-03-04 13:07:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-14 12:36:22 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apistatus.ObjectNotFound{}, addr)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:39:35 +00:00
|
|
|
func (t *TestFrostFS) CreateObject(_ context.Context, prm PrmObjectCreate) (oid.ID, error) {
|
2022-04-25 09:57:58 +00:00
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
2022-04-25 09:57:58 +00:00
|
|
|
}
|
|
|
|
var id oid.ID
|
|
|
|
id.SetSHA256(sha256.Sum256(b))
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-03-17 14:03:06 +00:00
|
|
|
attrs := make([]object.Attribute, 0)
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2022-09-07 06:59:24 +00:00
|
|
|
if prm.Filepath != "" {
|
2022-03-04 13:07:27 +00:00
|
|
|
a := object.NewAttribute()
|
2022-09-07 06:59:24 +00:00
|
|
|
a.SetKey(object.AttributeFilePath)
|
|
|
|
a.SetValue(prm.Filepath)
|
2022-03-17 14:03:06 +00:00
|
|
|
attrs = append(attrs, *a)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range prm.Attributes {
|
|
|
|
a := object.NewAttribute()
|
|
|
|
a.SetKey(prm.Attributes[i][0])
|
|
|
|
a.SetValue(prm.Attributes[i][1])
|
2022-03-17 14:03:06 +00:00
|
|
|
attrs = append(attrs, *a)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj := object.New()
|
2022-04-25 09:57:58 +00:00
|
|
|
obj.SetContainerID(prm.Container)
|
2022-03-04 13:07:27 +00:00
|
|
|
obj.SetID(id)
|
|
|
|
obj.SetPayloadSize(prm.PayloadSize)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
obj.SetCreationEpoch(t.currentEpoch)
|
2022-10-04 08:31:09 +00:00
|
|
|
obj.SetOwnerID(&prm.Creator)
|
2022-03-04 13:07:27 +00:00
|
|
|
t.currentEpoch++
|
|
|
|
|
2022-03-04 14:36:30 +00:00
|
|
|
if len(prm.Locks) > 0 {
|
|
|
|
lock := new(object.Lock)
|
|
|
|
lock.WriteMembers(prm.Locks)
|
|
|
|
objectv2.WriteLock(obj.ToV2(), (objectv2.Lock)(*lock))
|
|
|
|
}
|
|
|
|
|
2022-03-04 13:07:27 +00:00
|
|
|
if prm.Payload != nil {
|
|
|
|
all, err := io.ReadAll(prm.Payload)
|
|
|
|
if err != nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
obj.SetPayload(all)
|
|
|
|
obj.SetPayloadSize(uint64(len(all)))
|
2022-06-01 14:50:30 +00:00
|
|
|
var hash checksum.Checksum
|
|
|
|
checksum.Calculate(&hash, checksum.SHA256, all)
|
|
|
|
obj.SetPayloadChecksum(hash)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
cnrID, _ := obj.ContainerID()
|
|
|
|
objID, _ := obj.ID()
|
|
|
|
|
|
|
|
addr := newAddress(cnrID, objID)
|
2022-05-25 17:25:43 +00:00
|
|
|
t.objects[addr.EncodeToString()] = obj
|
2022-06-27 09:33:36 +00:00
|
|
|
return objID, nil
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) DeleteObject(ctx context.Context, prm PrmObjectDelete) error {
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(prm.Container)
|
|
|
|
addr.SetObject(prm.Object)
|
2022-03-04 13:07:27 +00:00
|
|
|
|
2023-04-24 16:40:18 +00:00
|
|
|
if err := t.objectErrors[addr.EncodeToString()]; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
if obj, ok := t.objects[addr.EncodeToString()]; ok {
|
|
|
|
owner := getOwner(ctx)
|
|
|
|
if !obj.OwnerID().Equals(owner) {
|
|
|
|
return ErrAccessDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(t.objects, addr.EncodeToString())
|
|
|
|
}
|
2022-03-04 13:07:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) TimeToEpoch(_ context.Context, now, futureTime time.Time) (uint64, uint64, error) {
|
2022-11-08 09:12:55 +00:00
|
|
|
return t.currentEpoch, t.currentEpoch + uint64(futureTime.Sub(now).Seconds()), nil
|
2022-03-05 08:53:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) AllObjects(cnrID cid.ID) []oid.ID {
|
2022-08-22 08:02:58 +00:00
|
|
|
result := make([]oid.ID, 0, len(t.objects))
|
|
|
|
|
|
|
|
for _, val := range t.objects {
|
|
|
|
objCnrID, _ := val.ContainerID()
|
|
|
|
objObjID, _ := val.ID()
|
|
|
|
if cnrID.Equals(objCnrID) {
|
|
|
|
result = append(result, objObjID)
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 08:02:58 +00:00
|
|
|
return result
|
2022-03-04 13:07:27 +00:00
|
|
|
}
|
2022-09-06 07:43:04 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) SetContainerEACL(_ context.Context, table eacl.Table, _ *session.Container) error {
|
2022-09-06 07:43:04 +00:00
|
|
|
cnrID, ok := table.CID()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid cid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok = t.containers[cnrID.EncodeToString()]; !ok {
|
|
|
|
return errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.eaclTables[cnrID.EncodeToString()] = &table
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
func (t *TestFrostFS) ContainerEACL(_ context.Context, cnrID cid.ID) (*eacl.Table, error) {
|
2022-09-06 07:43:04 +00:00
|
|
|
table, ok := t.eaclTables[cnrID.EncodeToString()]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return table, nil
|
|
|
|
}
|
2022-10-04 08:31:09 +00:00
|
|
|
|
2023-02-20 09:18:29 +00:00
|
|
|
func (t *TestFrostFS) isPublicRead(cnrID cid.ID) bool {
|
|
|
|
table, ok := t.eaclTables[cnrID.EncodeToString()]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rec := range table.Records() {
|
|
|
|
if rec.Operation() == eacl.OperationGet && len(rec.Filters()) == 0 {
|
|
|
|
for _, trgt := range rec.Targets() {
|
|
|
|
if trgt.Role() == eacl.RoleOthers {
|
|
|
|
return rec.Action() == eacl.ActionAllow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func getOwner(ctx context.Context) user.ID {
|
|
|
|
if bd, ok := ctx.Value(api.BoxData).(*accessbox.Box); ok && bd != nil && bd.Gate != nil && bd.Gate.BearerToken != nil {
|
|
|
|
return bearer.ResolveIssuer(*bd.Gate.BearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.ID{}
|
|
|
|
}
|