From 18d8898b00d94b33fd2c3bddee83dbadbd6ce15b Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Thu, 6 Jul 2023 11:28:36 +0300 Subject: [PATCH] [#294] aclsvc: Refactor service constructor Pass required deps as args. Signed-off-by: Dmitrii Stepanov --- pkg/services/object/acl/acl.go | 36 +++++++++-------- pkg/services/object/acl/eacl/v2/eacl_test.go | 6 +-- pkg/services/object/acl/eacl/v2/headers.go | 20 ++++------ pkg/services/object/acl/eacl/v2/localstore.go | 22 ----------- pkg/services/object/acl/eacl/v2/opts.go | 39 ------------------- pkg/services/object/acl/eacl/v2/xheader.go | 10 ++++- 6 files changed, 39 insertions(+), 94 deletions(-) delete mode 100644 pkg/services/object/acl/eacl/v2/localstore.go diff --git a/pkg/services/object/acl/acl.go b/pkg/services/object/acl/acl.go index cfe49396e..921545c8b 100644 --- a/pkg/services/object/acl/acl.go +++ b/pkg/services/object/acl/acl.go @@ -1,10 +1,12 @@ package acl import ( + "context" "crypto/ecdsa" "crypto/elliptic" "errors" "fmt" + "io" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap" @@ -17,6 +19,8 @@ import ( cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa" eaclSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl" + objectSDK "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/user" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" ) @@ -30,6 +34,18 @@ type Checker struct { state netmap.State } +type localStorage struct { + ls *engine.StorageEngine +} + +func (s *localStorage) Head(ctx context.Context, addr oid.Address) (*objectSDK.Object, error) { + if s.ls == nil { + return nil, io.ErrUnexpectedEOF + } + + return engine.Head(ctx, s.ls, addr) +} + // Various EACL check errors. var ( errEACLDeniedByRule = errors.New("denied by rule") @@ -158,26 +174,14 @@ func getRole(reqInfo v2.RequestInfo) eaclSDK.Role { } func (c *Checker) getHeaderSource(cnr cid.ID, msg any, reqInfo v2.RequestInfo) (eaclSDK.TypedHeaderSource, error) { - hdrSrcOpts := make([]eaclV2.Option, 0, 3) - - hdrSrcOpts = append(hdrSrcOpts, - eaclV2.WithLocalObjectStorage(c.localStorage), - eaclV2.WithCID(cnr), - eaclV2.WithOID(reqInfo.ObjectID()), - ) - + var xHeaderSource eaclV2.XHeaderSource if req, ok := msg.(eaclV2.Request); ok { - hdrSrcOpts = append(hdrSrcOpts, eaclV2.WithServiceRequest(req)) + xHeaderSource = eaclV2.NewRequestXHeaderSource(req) } else { - hdrSrcOpts = append(hdrSrcOpts, - eaclV2.WithServiceResponse( - msg.(eaclV2.Response), - reqInfo.Request().(eaclV2.Request), - ), - ) + xHeaderSource = eaclV2.NewResponseXHeaderSource(msg.(eaclV2.Response), reqInfo.Request().(eaclV2.Request)) } - hdrSrc, err := eaclV2.NewMessageHeaderSource(hdrSrcOpts...) + hdrSrc, err := eaclV2.NewMessageHeaderSource(&localStorage{ls: c.localStorage}, xHeaderSource, cnr, eaclV2.WithOID(reqInfo.ObjectID())) if err != nil { return nil, fmt.Errorf("can't parse headers: %w", err) } diff --git a/pkg/services/object/acl/eacl/v2/eacl_test.go b/pkg/services/object/acl/eacl/v2/eacl_test.go index 6f879123b..023b99239 100644 --- a/pkg/services/object/acl/eacl/v2/eacl_test.go +++ b/pkg/services/object/acl/eacl/v2/eacl_test.go @@ -103,9 +103,9 @@ func TestHeadRequest(t *testing.T) { newSource := func(t *testing.T) eaclSDK.TypedHeaderSource { hdrSrc, err := NewMessageHeaderSource( - WithObjectStorage(lStorage), - WithServiceRequest(req), - WithCID(addr.Container()), + lStorage, + NewRequestXHeaderSource(req), + addr.Container(), WithOID(&id)) require.NoError(t, err) return hdrSrc diff --git a/pkg/services/object/acl/eacl/v2/headers.go b/pkg/services/object/acl/eacl/v2/headers.go index c7a1b7729..7408c96e9 100644 --- a/pkg/services/object/acl/eacl/v2/headers.go +++ b/pkg/services/object/acl/eacl/v2/headers.go @@ -21,7 +21,7 @@ type Option func(*cfg) type cfg struct { storage ObjectStorage - msg xHeaderSource + msg XHeaderSource cnr cid.ID obj *oid.ID @@ -46,14 +46,12 @@ type headerSource struct { incompleteObjectHeaders bool } -func defaultCfg() *cfg { - return &cfg{ - storage: new(localStorage), +func NewMessageHeaderSource(os ObjectStorage, xhs XHeaderSource, cnrID cid.ID, opts ...Option) (eaclSDK.TypedHeaderSource, error) { + cfg := &cfg{ + storage: os, + cnr: cnrID, + msg: xhs, } -} - -func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) { - cfg := defaultCfg() for i := range opts { opts[i](cfg) @@ -70,7 +68,7 @@ func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) { return nil, err } - res.requestHeaders = requestHeaders(cfg.msg) + res.requestHeaders = cfg.msg.GetXHeaders() return res, nil } @@ -96,10 +94,6 @@ func (x xHeader) Value() string { return (*session.XHeader)(&x).GetValue() } -func requestHeaders(msg xHeaderSource) []eaclSDK.Header { - return msg.GetXHeaders() -} - var errMissingOID = errors.New("object ID is missing") func (h *cfg) readObjectHeaders(dst *headerSource) error { diff --git a/pkg/services/object/acl/eacl/v2/localstore.go b/pkg/services/object/acl/eacl/v2/localstore.go deleted file mode 100644 index 0f23e9881..000000000 --- a/pkg/services/object/acl/eacl/v2/localstore.go +++ /dev/null @@ -1,22 +0,0 @@ -package v2 - -import ( - "context" - "io" - - "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine" - objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" - oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" -) - -type localStorage struct { - ls *engine.StorageEngine -} - -func (s *localStorage) Head(ctx context.Context, addr oid.Address) (*objectSDK.Object, error) { - if s.ls == nil { - return nil, io.ErrUnexpectedEOF - } - - return engine.Head(ctx, s.ls, addr) -} diff --git a/pkg/services/object/acl/eacl/v2/opts.go b/pkg/services/object/acl/eacl/v2/opts.go index 7657e8780..d91a21c75 100644 --- a/pkg/services/object/acl/eacl/v2/opts.go +++ b/pkg/services/object/acl/eacl/v2/opts.go @@ -1,48 +1,9 @@ package v2 import ( - "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine" - cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" ) -func WithObjectStorage(v ObjectStorage) Option { - return func(c *cfg) { - c.storage = v - } -} - -func WithLocalObjectStorage(v *engine.StorageEngine) Option { - return func(c *cfg) { - c.storage = &localStorage{ - ls: v, - } - } -} - -func WithServiceRequest(v Request) Option { - return func(c *cfg) { - c.msg = requestXHeaderSource{ - req: v, - } - } -} - -func WithServiceResponse(resp Response, req Request) Option { - return func(c *cfg) { - c.msg = responseXHeaderSource{ - resp: resp, - req: req, - } - } -} - -func WithCID(v cid.ID) Option { - return func(c *cfg) { - c.cnr = v - } -} - func WithOID(v *oid.ID) Option { return func(c *cfg) { c.obj = v diff --git a/pkg/services/object/acl/eacl/v2/xheader.go b/pkg/services/object/acl/eacl/v2/xheader.go index 246714af7..c1fdea9d8 100644 --- a/pkg/services/object/acl/eacl/v2/xheader.go +++ b/pkg/services/object/acl/eacl/v2/xheader.go @@ -5,7 +5,7 @@ import ( eaclSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl" ) -type xHeaderSource interface { +type XHeaderSource interface { GetXHeaders() []eaclSDK.Header } @@ -13,12 +13,20 @@ type requestXHeaderSource struct { req Request } +func NewRequestXHeaderSource(req Request) XHeaderSource { + return requestXHeaderSource{req: req} +} + type responseXHeaderSource struct { resp Response req Request } +func NewResponseXHeaderSource(resp Response, req Request) XHeaderSource { + return responseXHeaderSource{resp: resp, req: req} +} + func (s requestXHeaderSource) GetXHeaders() []eaclSDK.Header { ln := 0