[#525] v2/container: Write session token from header to container

If container is created via session, then session token should be written to
it.

Write session token from request meta header to `Container` structure which
is passed to `wrapper.Put` function.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-25 18:46:50 +03:00 committed by Leonard Lyubich
parent db67a117f0
commit 98cc685a9b
2 changed files with 25 additions and 3 deletions

View file

@ -5,10 +5,18 @@ import (
"fmt" "fmt"
"github.com/nspcc-dev/neofs-api-go/v2/container" "github.com/nspcc-dev/neofs-api-go/v2/container"
"github.com/nspcc-dev/neofs-api-go/v2/session"
) )
// FIXME: (temp solution) we need to pass session token from header
type PutContext struct {
context.Context
SessionToken *session.SessionToken
}
type ServiceExecutor interface { type ServiceExecutor interface {
Put(context.Context, *container.PutRequestBody) (*container.PutResponseBody, error) Put(PutContext, *container.PutRequestBody) (*container.PutResponseBody, error)
Delete(context.Context, *container.DeleteRequestBody) (*container.DeleteResponseBody, error) Delete(context.Context, *container.DeleteRequestBody) (*container.DeleteResponseBody, error)
Get(context.Context, *container.GetRequestBody) (*container.GetResponseBody, error) Get(context.Context, *container.GetRequestBody) (*container.GetResponseBody, error)
List(context.Context, *container.ListRequestBody) (*container.ListResponseBody, error) List(context.Context, *container.ListRequestBody) (*container.ListResponseBody, error)
@ -30,7 +38,16 @@ func NewExecutionService(exec ServiceExecutor) Server {
} }
func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) { func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) {
respBody, err := s.exec.Put(ctx, req.GetBody()) var tok *session.SessionToken
for meta := req.GetMetaHeader(); meta != nil; meta = meta.GetOrigin() {
tok = meta.GetSessionToken()
}
respBody, err := s.exec.Put(PutContext{
Context: ctx,
SessionToken: tok,
}, req.GetBody())
if err != nil { if err != nil {
return nil, fmt.Errorf("could not execute Put request: %w", err) return nil, fmt.Errorf("could not execute Put request: %w", err)
} }

View file

@ -8,6 +8,7 @@ import (
eaclSDK "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl" eaclSDK "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
containerSDK "github.com/nspcc-dev/neofs-api-go/pkg/container" containerSDK "github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/session"
"github.com/nspcc-dev/neofs-api-go/v2/container" "github.com/nspcc-dev/neofs-api-go/v2/container"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
@ -24,7 +25,7 @@ func NewExecutor(w *wrapper.Wrapper) containerSvc.ServiceExecutor {
} }
} }
func (s *morphExecutor) Put(ctx context.Context, body *container.PutRequestBody) (*container.PutResponseBody, error) { func (s *morphExecutor) Put(ctx containerSvc.PutContext, body *container.PutRequestBody) (*container.PutResponseBody, error) {
cnr, err := containerSDK.NewVerifiedFromV2(body.GetContainer()) cnr, err := containerSDK.NewVerifiedFromV2(body.GetContainer())
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid format of the container structure: %w", err) return nil, fmt.Errorf("invalid format of the container structure: %w", err)
@ -34,6 +35,10 @@ func (s *morphExecutor) Put(ctx context.Context, body *container.PutRequestBody)
pkg.NewSignatureFromV2(body.GetSignature()), pkg.NewSignatureFromV2(body.GetSignature()),
) )
cnr.SetSessionToken(
session.NewTokenFromV2(ctx.SessionToken),
)
cid, err := wrapper.Put(s.wrapper, cnr) cid, err := wrapper.Put(s.wrapper, cnr)
if err != nil { if err != nil {
return nil, err return nil, err