[#294] aclsvc: Refactor service constructor

Pass required deps as args.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-07-06 11:28:36 +03:00 committed by Evgenii Stratonikov
parent 61541eaec2
commit 18d8898b00
6 changed files with 39 additions and 94 deletions

View file

@ -1,10 +1,12 @@
package acl package acl
import ( import (
"context"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"errors" "errors"
"fmt" "fmt"
"io"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
@ -17,6 +19,8 @@ import (
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa" frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
eaclSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl" 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" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
) )
@ -30,6 +34,18 @@ type Checker struct {
state netmap.State 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. // Various EACL check errors.
var ( var (
errEACLDeniedByRule = errors.New("denied by rule") 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) { func (c *Checker) getHeaderSource(cnr cid.ID, msg any, reqInfo v2.RequestInfo) (eaclSDK.TypedHeaderSource, error) {
hdrSrcOpts := make([]eaclV2.Option, 0, 3) var xHeaderSource eaclV2.XHeaderSource
hdrSrcOpts = append(hdrSrcOpts,
eaclV2.WithLocalObjectStorage(c.localStorage),
eaclV2.WithCID(cnr),
eaclV2.WithOID(reqInfo.ObjectID()),
)
if req, ok := msg.(eaclV2.Request); ok { if req, ok := msg.(eaclV2.Request); ok {
hdrSrcOpts = append(hdrSrcOpts, eaclV2.WithServiceRequest(req)) xHeaderSource = eaclV2.NewRequestXHeaderSource(req)
} else { } else {
hdrSrcOpts = append(hdrSrcOpts, xHeaderSource = eaclV2.NewResponseXHeaderSource(msg.(eaclV2.Response), reqInfo.Request().(eaclV2.Request))
eaclV2.WithServiceResponse(
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 { if err != nil {
return nil, fmt.Errorf("can't parse headers: %w", err) return nil, fmt.Errorf("can't parse headers: %w", err)
} }

View file

@ -103,9 +103,9 @@ func TestHeadRequest(t *testing.T) {
newSource := func(t *testing.T) eaclSDK.TypedHeaderSource { newSource := func(t *testing.T) eaclSDK.TypedHeaderSource {
hdrSrc, err := NewMessageHeaderSource( hdrSrc, err := NewMessageHeaderSource(
WithObjectStorage(lStorage), lStorage,
WithServiceRequest(req), NewRequestXHeaderSource(req),
WithCID(addr.Container()), addr.Container(),
WithOID(&id)) WithOID(&id))
require.NoError(t, err) require.NoError(t, err)
return hdrSrc return hdrSrc

View file

@ -21,7 +21,7 @@ type Option func(*cfg)
type cfg struct { type cfg struct {
storage ObjectStorage storage ObjectStorage
msg xHeaderSource msg XHeaderSource
cnr cid.ID cnr cid.ID
obj *oid.ID obj *oid.ID
@ -46,14 +46,12 @@ type headerSource struct {
incompleteObjectHeaders bool incompleteObjectHeaders bool
} }
func defaultCfg() *cfg { func NewMessageHeaderSource(os ObjectStorage, xhs XHeaderSource, cnrID cid.ID, opts ...Option) (eaclSDK.TypedHeaderSource, error) {
return &cfg{ cfg := &cfg{
storage: new(localStorage), storage: os,
cnr: cnrID,
msg: xhs,
} }
}
func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) {
cfg := defaultCfg()
for i := range opts { for i := range opts {
opts[i](cfg) opts[i](cfg)
@ -70,7 +68,7 @@ func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) {
return nil, err return nil, err
} }
res.requestHeaders = requestHeaders(cfg.msg) res.requestHeaders = cfg.msg.GetXHeaders()
return res, nil return res, nil
} }
@ -96,10 +94,6 @@ func (x xHeader) Value() string {
return (*session.XHeader)(&x).GetValue() return (*session.XHeader)(&x).GetValue()
} }
func requestHeaders(msg xHeaderSource) []eaclSDK.Header {
return msg.GetXHeaders()
}
var errMissingOID = errors.New("object ID is missing") var errMissingOID = errors.New("object ID is missing")
func (h *cfg) readObjectHeaders(dst *headerSource) error { func (h *cfg) readObjectHeaders(dst *headerSource) error {

View file

@ -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)
}

View file

@ -1,48 +1,9 @@
package v2 package v2
import ( 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" 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 { func WithOID(v *oid.ID) Option {
return func(c *cfg) { return func(c *cfg) {
c.obj = v c.obj = v

View file

@ -5,7 +5,7 @@ import (
eaclSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl" eaclSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
) )
type xHeaderSource interface { type XHeaderSource interface {
GetXHeaders() []eaclSDK.Header GetXHeaders() []eaclSDK.Header
} }
@ -13,12 +13,20 @@ type requestXHeaderSource struct {
req Request req Request
} }
func NewRequestXHeaderSource(req Request) XHeaderSource {
return requestXHeaderSource{req: req}
}
type responseXHeaderSource struct { type responseXHeaderSource struct {
resp Response resp Response
req Request req Request
} }
func NewResponseXHeaderSource(resp Response, req Request) XHeaderSource {
return responseXHeaderSource{resp: resp, req: req}
}
func (s requestXHeaderSource) GetXHeaders() []eaclSDK.Header { func (s requestXHeaderSource) GetXHeaders() []eaclSDK.Header {
ln := 0 ln := 0