2020-08-24 14:07:08 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-16 15:26:13 +00:00
|
|
|
"errors"
|
2022-05-12 16:37:46 +00:00
|
|
|
"fmt"
|
2020-08-24 14:07:08 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/container"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-05-26 10:34:06 +00:00
|
|
|
containercore "github.com/nspcc-dev/neofs-node/pkg/core/container"
|
2020-08-24 14:07:08 +00:00
|
|
|
containerSvc "github.com/nspcc-dev/neofs-node/pkg/services/container"
|
2021-08-30 11:16:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/acl/eacl"
|
2021-11-10 07:08:33 +00:00
|
|
|
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-16 13:15:31 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
2021-11-10 07:08:33 +00:00
|
|
|
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2020-08-24 14:07:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type morphExecutor struct {
|
2021-08-30 11:16:41 +00:00
|
|
|
rdr Reader
|
2021-09-08 17:24:16 +00:00
|
|
|
wrt Writer
|
2021-08-30 11:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reader is an interface of read-only container storage.
|
|
|
|
type Reader interface {
|
|
|
|
containercore.Source
|
|
|
|
eacl.Source
|
|
|
|
|
|
|
|
// List returns a list of container identifiers belonging
|
2022-05-17 13:59:46 +00:00
|
|
|
// to the specified user of NeoFS system. Returns the identifiers
|
2021-08-30 11:16:41 +00:00
|
|
|
// of all NeoFS containers if pointer to owner identifier is nil.
|
2022-05-17 13:59:46 +00:00
|
|
|
List(*user.ID) ([]*cid.ID, error)
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 17:24:16 +00:00
|
|
|
// Writer is an interface of container storage updater.
|
|
|
|
type Writer interface {
|
|
|
|
// Put stores specified container in the side chain.
|
|
|
|
Put(*containerSDK.Container) (*cid.ID, error)
|
|
|
|
// Delete removes specified container from the side chain.
|
|
|
|
Delete(containercore.RemovalWitness) error
|
|
|
|
// PutEACL updates extended ACL table of specified container in the side chain.
|
|
|
|
PutEACL(*eaclSDK.Table) error
|
2021-09-08 13:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 15:26:13 +00:00
|
|
|
// ErrInvalidContext is thrown by morph ServiceExecutor when provided session
|
|
|
|
// token does not contain expected container context.
|
|
|
|
var ErrInvalidContext = errors.New("session token does not contain container context")
|
|
|
|
|
2021-09-08 17:24:16 +00:00
|
|
|
func NewExecutor(rdr Reader, wrt Writer) containerSvc.ServiceExecutor {
|
2020-08-24 14:07:08 +00:00
|
|
|
return &morphExecutor{
|
2021-09-08 17:24:16 +00:00
|
|
|
rdr: rdr,
|
|
|
|
wrt: wrt,
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:23:11 +00:00
|
|
|
func (s *morphExecutor) Put(ctx containerSvc.ContextWithToken, body *container.PutRequestBody) (*container.PutResponseBody, error) {
|
2022-05-16 13:15:31 +00:00
|
|
|
sigV2 := body.GetSignature()
|
|
|
|
if sigV2 == nil {
|
|
|
|
// TODO(@cthulhu-rider): #1387 use "const" error
|
|
|
|
return nil, errors.New("missing signature")
|
|
|
|
}
|
|
|
|
|
2021-07-02 05:59:02 +00:00
|
|
|
cnr := containerSDK.NewContainerFromV2(body.GetContainer())
|
2020-11-03 09:08:24 +00:00
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
var sig neofscrypto.Signature
|
|
|
|
sig.ReadFromV2(*sigV2)
|
|
|
|
|
|
|
|
cnr.SetSignature(&sig)
|
2020-12-24 10:20:20 +00:00
|
|
|
|
2021-12-16 15:26:13 +00:00
|
|
|
tok := session.NewTokenFromV2(ctx.SessionToken)
|
|
|
|
if ctx.SessionToken != nil && session.GetContainerContext(tok) == nil {
|
|
|
|
return nil, ErrInvalidContext
|
|
|
|
}
|
|
|
|
|
|
|
|
cnr.SetSessionToken(tok)
|
2021-05-25 15:46:50 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
idCnr, err := s.wrt.Put(cnr)
|
2020-08-24 14:07:08 +00:00
|
|
|
if err != nil {
|
2020-10-07 16:17:50 +00:00
|
|
|
return nil, err
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var idCnrV2 refs.ContainerID
|
|
|
|
idCnr.WriteToV2(&idCnrV2)
|
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
res := new(container.PutResponseBody)
|
2022-05-12 16:37:46 +00:00
|
|
|
res.SetContainerID(&idCnrV2)
|
2020-08-24 14:07:08 +00:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 11:08:24 +00:00
|
|
|
func (s *morphExecutor) Delete(ctx containerSvc.ContextWithToken, body *container.DeleteRequestBody) (*container.DeleteResponseBody, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 := body.GetContainerID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return nil, errors.New("missing container ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id cid.ID
|
|
|
|
|
|
|
|
err := id.ReadFromV2(*idV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid container ID: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-26 10:34:06 +00:00
|
|
|
sig := body.GetSignature().GetSign()
|
2021-12-16 15:26:13 +00:00
|
|
|
|
2021-05-26 11:08:24 +00:00
|
|
|
tok := session.NewTokenFromV2(ctx.SessionToken)
|
2021-12-16 15:26:13 +00:00
|
|
|
if ctx.SessionToken != nil && session.GetContainerContext(tok) == nil {
|
|
|
|
return nil, ErrInvalidContext
|
|
|
|
}
|
2020-08-24 14:07:08 +00:00
|
|
|
|
2021-05-26 10:34:06 +00:00
|
|
|
var rmWitness containercore.RemovalWitness
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
rmWitness.SetContainerID(&id)
|
2021-05-26 10:34:06 +00:00
|
|
|
rmWitness.SetSignature(sig)
|
2021-05-26 11:08:24 +00:00
|
|
|
rmWitness.SetSessionToken(tok)
|
2021-05-26 10:34:06 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err = s.wrt.Delete(rmWitness)
|
2020-10-07 16:17:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new(container.DeleteResponseBody), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *morphExecutor) Get(ctx context.Context, body *container.GetRequestBody) (*container.GetResponseBody, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 := body.GetContainerID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return nil, errors.New("missing container ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id cid.ID
|
2020-08-24 14:07:08 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err := id.ReadFromV2(*idV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid container ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cnr, err := s.rdr.Get(&id)
|
2020-08-24 14:07:08 +00:00
|
|
|
if err != nil {
|
2020-10-07 16:17:50 +00:00
|
|
|
return nil, err
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
var sigV2 *refs.Signature
|
|
|
|
|
|
|
|
if sig := cnr.Signature(); sig != nil {
|
|
|
|
sigV2 = new(refs.Signature)
|
|
|
|
sig.WriteToV2(sigV2)
|
|
|
|
}
|
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
res := new(container.GetResponseBody)
|
2020-10-07 16:17:50 +00:00
|
|
|
res.SetContainer(cnr.ToV2())
|
2022-05-16 13:15:31 +00:00
|
|
|
res.SetSignature(sigV2)
|
2021-05-25 15:10:34 +00:00
|
|
|
res.SetSessionToken(cnr.SessionToken().ToV2())
|
2020-08-24 14:07:08 +00:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *morphExecutor) List(ctx context.Context, body *container.ListRequestBody) (*container.ListResponseBody, error) {
|
2022-05-17 13:59:46 +00:00
|
|
|
idV2 := body.GetOwnerID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return nil, fmt.Errorf("missing user ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id user.ID
|
|
|
|
|
|
|
|
err := id.ReadFromV2(*idV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid user ID: %w", err)
|
|
|
|
}
|
2020-08-24 14:07:08 +00:00
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
cnrs, err := s.rdr.List(&id)
|
2020-08-24 14:07:08 +00:00
|
|
|
if err != nil {
|
2020-10-07 16:17:50 +00:00
|
|
|
return nil, err
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
cidList := make([]refs.ContainerID, len(cnrs))
|
2020-10-07 16:17:50 +00:00
|
|
|
for i := range cnrs {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnrs[i].WriteToV2(&cidList[i])
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := new(container.ListResponseBody)
|
|
|
|
res.SetContainerIDs(cidList)
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:25:14 +00:00
|
|
|
func (s *morphExecutor) SetExtendedACL(ctx containerSvc.ContextWithToken, body *container.SetExtendedACLRequestBody) (*container.SetExtendedACLResponseBody, error) {
|
2022-05-16 13:15:31 +00:00
|
|
|
sigV2 := body.GetSignature()
|
|
|
|
if sigV2 == nil {
|
|
|
|
// TODO(@cthulhu-rider): #1387 use "const" error
|
|
|
|
return nil, errors.New("missing signature")
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:17:50 +00:00
|
|
|
table := eaclSDK.NewTableFromV2(body.GetEACL())
|
2020-08-24 14:07:08 +00:00
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
var sig neofscrypto.Signature
|
|
|
|
sig.ReadFromV2(*sigV2)
|
|
|
|
|
|
|
|
table.SetSignature(&sig)
|
2021-05-25 15:27:12 +00:00
|
|
|
|
2021-12-16 15:26:13 +00:00
|
|
|
tok := session.NewTokenFromV2(ctx.SessionToken)
|
|
|
|
if ctx.SessionToken != nil && session.GetContainerContext(tok) == nil {
|
|
|
|
return nil, ErrInvalidContext
|
|
|
|
}
|
|
|
|
|
|
|
|
table.SetSessionToken(tok)
|
2021-05-25 16:25:14 +00:00
|
|
|
|
2021-09-08 17:24:16 +00:00
|
|
|
err := s.wrt.PutEACL(table)
|
2021-09-08 13:31:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return new(container.SetExtendedACLResponseBody), nil
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:17:50 +00:00
|
|
|
func (s *morphExecutor) GetExtendedACL(ctx context.Context, body *container.GetExtendedACLRequestBody) (*container.GetExtendedACLResponseBody, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 := body.GetContainerID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return nil, errors.New("missing container ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id cid.ID
|
|
|
|
|
|
|
|
err := id.ReadFromV2(*idV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid container ID: %w", err)
|
|
|
|
}
|
2020-08-24 14:07:08 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
table, err := s.rdr.GetEACL(&id)
|
2020-08-24 14:07:08 +00:00
|
|
|
if err != nil {
|
2020-10-07 16:17:50 +00:00
|
|
|
return nil, err
|
2020-08-24 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
var sigV2 *refs.Signature
|
|
|
|
|
|
|
|
if sig := table.Signature(); sig != nil {
|
|
|
|
sigV2 = new(refs.Signature)
|
|
|
|
sig.WriteToV2(sigV2)
|
|
|
|
}
|
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
res := new(container.GetExtendedACLResponseBody)
|
2020-10-07 16:17:50 +00:00
|
|
|
res.SetEACL(table.ToV2())
|
2022-05-16 13:15:31 +00:00
|
|
|
res.SetSignature(sigV2)
|
2021-05-25 15:12:23 +00:00
|
|
|
res.SetSessionToken(table.SessionToken().ToV2())
|
2020-09-04 12:18:47 +00:00
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
return res, nil
|
|
|
|
}
|