[#1159] services/container: Remove ContainerWithContext struct

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-30 17:19:14 +03:00 committed by LeL
parent 2bcc0051ab
commit 855cbf5a3a
3 changed files with 26 additions and 53 deletions

View file

@ -8,19 +8,12 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/session"
)
// FIXME: #1159 (temp solution) we need to pass session token from header
type ContextWithToken struct {
context.Context
SessionToken *session.Token
}
type ServiceExecutor interface {
Put(ContextWithToken, *container.PutRequestBody) (*container.PutResponseBody, error)
Delete(ContextWithToken, *container.DeleteRequestBody) (*container.DeleteResponseBody, error)
Put(context.Context, *session.Token, *container.PutRequestBody) (*container.PutResponseBody, error)
Delete(context.Context, *session.Token, *container.DeleteRequestBody) (*container.DeleteResponseBody, error)
Get(context.Context, *container.GetRequestBody) (*container.GetResponseBody, error)
List(context.Context, *container.ListRequestBody) (*container.ListResponseBody, error)
SetExtendedACL(ContextWithToken, *container.SetExtendedACLRequestBody) (*container.SetExtendedACLResponseBody, error)
SetExtendedACL(context.Context, *session.Token, *container.SetExtendedACLRequestBody) (*container.SetExtendedACLResponseBody, error)
GetExtendedACL(context.Context, *container.GetExtendedACLRequestBody) (*container.GetExtendedACLResponseBody, error)
}
@ -37,23 +30,8 @@ func NewExecutionService(exec ServiceExecutor) Server {
}
}
func contextWithTokenFromRequest(ctx context.Context, req interface {
GetMetaHeader() *session.RequestMetaHeader
}) ContextWithToken {
var tok *session.Token
for meta := req.GetMetaHeader(); meta != nil; meta = meta.GetOrigin() {
tok = meta.GetSessionToken()
}
return ContextWithToken{
Context: ctx,
SessionToken: tok,
}
}
func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) {
respBody, err := s.exec.Put(contextWithTokenFromRequest(ctx, req), req.GetBody())
respBody, err := s.exec.Put(ctx, req.GetMetaHeader().GetOrigin().GetSessionToken(), req.GetBody())
if err != nil {
return nil, fmt.Errorf("could not execute Put request: %w", err)
}
@ -65,7 +43,7 @@ func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*cont
}
func (s *executorSvc) Delete(ctx context.Context, req *container.DeleteRequest) (*container.DeleteResponse, error) {
respBody, err := s.exec.Delete(contextWithTokenFromRequest(ctx, req), req.GetBody())
respBody, err := s.exec.Delete(ctx, req.GetMetaHeader().GetOrigin().GetSessionToken(), req.GetBody())
if err != nil {
return nil, fmt.Errorf("could not execute Delete request: %w", err)
}
@ -101,7 +79,7 @@ func (s *executorSvc) List(ctx context.Context, req *container.ListRequest) (*co
}
func (s *executorSvc) SetExtendedACL(ctx context.Context, req *container.SetExtendedACLRequest) (*container.SetExtendedACLResponse, error) {
respBody, err := s.exec.SetExtendedACL(contextWithTokenFromRequest(ctx, req), req.GetBody())
respBody, err := s.exec.SetExtendedACL(ctx, req.GetMetaHeader().GetOrigin().GetSessionToken(), req.GetBody())
if err != nil {
return nil, fmt.Errorf("could not execute SetEACL request: %w", err)
}

View file

@ -52,7 +52,7 @@ func NewExecutor(rdr Reader, wrt Writer) containerSvc.ServiceExecutor {
}
}
func (s *morphExecutor) Put(ctx containerSvc.ContextWithToken, body *container.PutRequestBody) (*container.PutResponseBody, error) {
func (s *morphExecutor) Put(_ context.Context, tokV2 *sessionV2.Token, body *container.PutRequestBody) (*container.PutResponseBody, error) {
sigV2 := body.GetSignature()
if sigV2 == nil {
// TODO(@cthulhu-rider): #1387 use "const" error
@ -66,10 +66,10 @@ func (s *morphExecutor) Put(ctx containerSvc.ContextWithToken, body *container.P
cnr.SetSignature(&sig)
if ctx.SessionToken != nil {
if tokV2 != nil {
var tok session.Container
err := tok.ReadFromV2(*ctx.SessionToken)
err := tok.ReadFromV2(*tokV2)
if err != nil {
return nil, fmt.Errorf("invalid session token: %w", err)
}
@ -91,7 +91,7 @@ func (s *morphExecutor) Put(ctx containerSvc.ContextWithToken, body *container.P
return res, nil
}
func (s *morphExecutor) Delete(ctx containerSvc.ContextWithToken, body *container.DeleteRequestBody) (*container.DeleteResponseBody, error) {
func (s *morphExecutor) Delete(_ context.Context, tokV2 *sessionV2.Token, body *container.DeleteRequestBody) (*container.DeleteResponseBody, error) {
idV2 := body.GetContainerID()
if idV2 == nil {
return nil, errors.New("missing container ID")
@ -108,10 +108,10 @@ func (s *morphExecutor) Delete(ctx containerSvc.ContextWithToken, body *containe
var tok *session.Container
if ctx.SessionToken != nil {
if tokV2 != nil {
tok = new(session.Container)
err := tok.ReadFromV2(*ctx.SessionToken)
err := tok.ReadFromV2(*tokV2)
if err != nil {
return nil, fmt.Errorf("invalid session token: %w", err)
}
@ -201,7 +201,7 @@ func (s *morphExecutor) List(ctx context.Context, body *container.ListRequestBod
return res, nil
}
func (s *morphExecutor) SetExtendedACL(ctx containerSvc.ContextWithToken, body *container.SetExtendedACLRequestBody) (*container.SetExtendedACLResponseBody, error) {
func (s *morphExecutor) SetExtendedACL(ctx context.Context, tokV2 *sessionV2.Token, body *container.SetExtendedACLRequestBody) (*container.SetExtendedACLResponseBody, error) {
sigV2 := body.GetSignature()
if sigV2 == nil {
// TODO(@cthulhu-rider): #1387 use "const" error
@ -215,10 +215,10 @@ func (s *morphExecutor) SetExtendedACL(ctx containerSvc.ContextWithToken, body *
table.SetSignature(&sig)
if ctx.SessionToken != nil {
if tokV2 != nil {
var tok session.Container
err := tok.ReadFromV2(*ctx.SessionToken)
err := tok.ReadFromV2(*tokV2)
if err != nil {
return nil, fmt.Errorf("invalid session token: %w", err)
}

View file

@ -48,35 +48,35 @@ func TestInvalidToken(t *testing.T) {
tests := []struct {
name string
op func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) error
op func(e containerSvc.ServiceExecutor, tokV2 *session.Token) error
}{
{
name: "put",
op: func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) (err error) {
op: func(e containerSvc.ServiceExecutor, tokV2 *session.Token) (err error) {
var reqBody container.PutRequestBody
reqBody.SetSignature(new(refs.Signature))
_, err = e.Put(ctx, &reqBody)
_, err = e.Put(context.TODO(), tokV2, &reqBody)
return
},
},
{
name: "delete",
op: func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) (err error) {
op: func(e containerSvc.ServiceExecutor, tokV2 *session.Token) (err error) {
var reqBody container.DeleteRequestBody
reqBody.SetContainerID(&cnrV2)
_, err = e.Delete(ctx, &reqBody)
_, err = e.Delete(context.TODO(), tokV2, &reqBody)
return
},
},
{
name: "setEACL",
op: func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) (err error) {
op: func(e containerSvc.ServiceExecutor, tokV2 *session.Token) (err error) {
var reqBody container.SetExtendedACLRequestBody
reqBody.SetSignature(new(refs.Signature))
_, err = e.SetExtendedACL(ctx, &reqBody)
_, err = e.SetExtendedACL(context.TODO(), tokV2, &reqBody)
return
},
},
@ -84,17 +84,12 @@ func TestInvalidToken(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := containerSvc.ContextWithToken{
Context: context.Background(),
SessionToken: generateToken(new(session.ObjectSessionContext)),
}
require.Error(t, test.op(e, ctx))
tok := generateToken(new(session.ObjectSessionContext))
require.Error(t, test.op(e, tok))
ctx.SessionToken = &tokV2
require.NoError(t, test.op(e, ctx))
require.NoError(t, test.op(e, &tokV2))
ctx.SessionToken = nil
require.NoError(t, test.op(e, ctx))
require.NoError(t, test.op(e, nil))
})
}
}