From fb50362dcc96d187939e0747d561cff83c43a766 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 11 Dec 2020 14:59:16 +0300 Subject: [PATCH] [#243] services/object: Share common parameters across services Signed-off-by: Leonard Lyubich --- go.sum | 2 - pkg/services/object/delete/exec.go | 4 +- pkg/services/object/delete/prm.go | 16 ------- pkg/services/object/delete/util.go | 4 -- pkg/services/object/delete/v2/util.go | 38 ++------------- pkg/services/object/get/exec.go | 4 +- pkg/services/object/get/prm.go | 16 ------- pkg/services/object/get/v2/util.go | 47 +++++------------- pkg/services/object/search/exec.go | 4 +- pkg/services/object/search/prm.go | 17 ------- pkg/services/object/search/v2/util.go | 31 ++---------- pkg/services/object/util/prm.go | 69 +++++++++++++++++++++++++-- 12 files changed, 89 insertions(+), 163 deletions(-) diff --git a/go.sum b/go.sum index e1b32a43f..702409893 100644 --- a/go.sum +++ b/go.sum @@ -283,8 +283,6 @@ github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1: github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc= github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b h1:gk5bZgpWOehaDVKI5vBDkcjXTpRkKqcvIb1h/vHnBH4= github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo= -github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211081945-1bc91466aa7b h1:0IdLNZHFrPKS7o2gvwo+dWO80k49nTrQq2YmzXN9I/4= -github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211081945-1bc91466aa7b/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211114827-2aa51d3add29 h1:5Th3BoSCbnKiHFzpoRh2+p3g3Hv1zTHC5dvHwfbIbBk= github.com/nspcc-dev/neofs-api-go v1.20.3-0.20201211114827-2aa51d3add29/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= diff --git a/pkg/services/object/delete/exec.go b/pkg/services/object/delete/exec.go index dbe665138..3da6249e8 100644 --- a/pkg/services/object/delete/exec.go +++ b/pkg/services/object/delete/exec.go @@ -60,7 +60,7 @@ func (exec execCtx) isLocal() bool { } func (exec *execCtx) key() *ecdsa.PrivateKey { - return exec.prm.key + return exec.prm.common.PrivateKey() } func (exec *execCtx) address() *objectSDK.Address { @@ -76,7 +76,7 @@ func (exec *execCtx) commonParameters() *util.CommonPrm { } func (exec execCtx) callOptions() []client.CallOption { - return exec.prm.callOpts + return exec.prm.common.RemoteCallOptions() } func (exec *execCtx) newAddress(id *objectSDK.ID) *objectSDK.Address { diff --git a/pkg/services/object/delete/prm.go b/pkg/services/object/delete/prm.go index 779cd2e77..227fd345b 100644 --- a/pkg/services/object/delete/prm.go +++ b/pkg/services/object/delete/prm.go @@ -1,20 +1,14 @@ package deletesvc import ( - "crypto/ecdsa" - "github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-node/pkg/services/object/util" ) // Prm groups parameters of Delete service call. type Prm struct { - key *ecdsa.PrivateKey - common *util.CommonPrm - callOpts []client.CallOption - client.DeleteObjectParams } @@ -22,13 +16,3 @@ type Prm struct { func (p *Prm) SetCommonParameters(common *util.CommonPrm) { p.common = common } - -// SetPrivateKey sets private key to use during execution. -func (p *Prm) SetPrivateKey(key *ecdsa.PrivateKey) { - p.key = key -} - -// SetRemoteCallOptions sets call options of remote client calls. -func (p *Prm) SetRemoteCallOptions(opts ...client.CallOption) { - p.callOpts = opts -} diff --git a/pkg/services/object/delete/util.go b/pkg/services/object/delete/util.go index 1a25e9a86..31a92ed2a 100644 --- a/pkg/services/object/delete/util.go +++ b/pkg/services/object/delete/util.go @@ -25,12 +25,10 @@ func (w *headSvcWrapper) headAddress(exec *execCtx, addr *objectSDK.Address) (*o wr := getsvc.NewSimpleObjectWriter() p := getsvc.HeadPrm{} - p.SetPrivateKey(exec.key()) p.SetCommonParameters(exec.commonParameters()) p.SetHeaderWriter(wr) p.WithRawFlag(true) p.WithAddress(addr) - p.SetRemoteCallOptions(exec.callOptions()...) err := (*getsvc.Service)(w).Head(exec.context(), p) if err != nil { @@ -86,10 +84,8 @@ func (w *searchSvcWrapper) splitMembers(exec *execCtx) ([]*objectSDK.ID, error) p := searchsvc.Prm{} p.SetWriter(wr) p.SetCommonParameters(exec.commonParameters()) - p.SetPrivateKey(exec.key()) p.WithContainerID(exec.containerID()) p.WithSearchFilters(fs) - p.SetRemoteCallOptions(exec.callOptions()...) err := (*searchsvc.Service)(w).Search(exec.context(), p) if err != nil { diff --git a/pkg/services/object/delete/v2/util.go b/pkg/services/object/delete/v2/util.go index c09cae632..4b6182906 100644 --- a/pkg/services/object/delete/v2/util.go +++ b/pkg/services/object/delete/v2/util.go @@ -1,11 +1,9 @@ package deletesvc import ( - "github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/token" objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" - "github.com/nspcc-dev/neofs-api-go/v2/session" deletesvc "github.com/nspcc-dev/neofs-node/pkg/services/object/delete" "github.com/nspcc-dev/neofs-node/pkg/services/object/util" ) @@ -23,9 +21,9 @@ func (s *Service) toPrm(req *objectV2.DeleteRequest, respBody *objectV2.DeleteRe } p := new(deletesvc.Prm) - p.SetPrivateKey(key) - p.SetCommonParameters(commonParameters(meta)) - p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) + p.SetCommonParameters(util.CommonPrmFromV2(req). + WithPrivateKey(key), + ) body := req.GetBody() p.WithAddress(object.NewAddressFromV2(body.GetAddress())) @@ -39,33 +37,3 @@ func (s *Service) toPrm(req *objectV2.DeleteRequest, respBody *objectV2.DeleteRe func (w *tombstoneBodyWriter) SetAddress(addr *object.Address) { w.body.SetTombstone(addr.ToV2()) } - -func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm { - prm := new(util.CommonPrm) - - if tok := meta.GetBearerToken(); tok != nil { - prm.WithBearerToken(token.NewBearerTokenFromV2(tok)) - } - - if tok := meta.GetSessionToken(); tok != nil { - prm.WithSessionToken(token.NewSessionTokenFromV2(tok)) - } - - return prm -} - -// can be shared accross all services -func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption { - opts := make([]client.CallOption, 0, 3) - opts = append(opts, client.WithTTL(meta.GetTTL()-1)) - - if tok := meta.GetBearerToken(); tok != nil { - opts = append(opts, client.WithBearer(token.NewBearerTokenFromV2(tok))) - } - - if tok := meta.GetSessionToken(); tok != nil { - opts = append(opts, client.WithSession(token.NewSessionTokenFromV2(tok))) - } - - return opts -} diff --git a/pkg/services/object/get/exec.go b/pkg/services/object/get/exec.go index fe96ac394..84a89cecb 100644 --- a/pkg/services/object/get/exec.go +++ b/pkg/services/object/get/exec.go @@ -96,11 +96,11 @@ func (exec execCtx) address() *objectSDK.Address { } func (exec execCtx) key() *ecdsa.PrivateKey { - return exec.prm.key + return exec.prm.common.PrivateKey() } func (exec execCtx) callOptions() []client.CallOption { - return exec.prm.callOpts + return exec.prm.common.RemoteCallOptions() } func (exec execCtx) remotePrm() *client.GetObjectParams { diff --git a/pkg/services/object/get/prm.go b/pkg/services/object/get/prm.go index aab3ec57a..8d859df0e 100644 --- a/pkg/services/object/get/prm.go +++ b/pkg/services/object/get/prm.go @@ -1,7 +1,6 @@ package getsvc import ( - "crypto/ecdsa" "hash" "github.com/nspcc-dev/neofs-api-go/pkg/client" @@ -39,11 +38,6 @@ type HeadPrm struct { type commonPrm struct { objWriter ObjectWriter - // TODO: replace key and callOpts to CommonPrm - key *ecdsa.PrivateKey - - callOpts []client.CallOption - common *util.CommonPrm client.GetObjectParams @@ -72,16 +66,6 @@ func (p *Prm) SetObjectWriter(w ObjectWriter) { p.objWriter = w } -// SetPrivateKey sets private key to use during execution. -func (p *commonPrm) SetPrivateKey(key *ecdsa.PrivateKey) { - p.key = key -} - -// SetRemoteCallOptions sets call options remote remote client calls. -func (p *commonPrm) SetRemoteCallOptions(opts ...client.CallOption) { - p.callOpts = opts -} - // SetChunkWriter sets target component to write the object payload range. func (p *RangePrm) SetChunkWriter(w ChunkWriter) { p.objWriter = &partWriter{ diff --git a/pkg/services/object/get/v2/util.go b/pkg/services/object/get/v2/util.go index 710c0ee14..a9b865574 100644 --- a/pkg/services/object/get/v2/util.go +++ b/pkg/services/object/get/v2/util.go @@ -4,12 +4,10 @@ import ( "crypto/sha256" "hash" - "github.com/nspcc-dev/neofs-api-go/pkg/client" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/token" objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" "github.com/nspcc-dev/neofs-api-go/v2/refs" - "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-node/pkg/core/object" objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object" getsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/get" @@ -27,14 +25,14 @@ func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStre } p := new(getsvc.Prm) - p.SetPrivateKey(key) + p.SetCommonParameters(util.CommonPrmFromV2(req). + WithPrivateKey(key), + ) body := req.GetBody() p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) p.WithRawFlag(body.GetRaw()) - p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) p.SetObjectWriter(&streamObjectWriter{stream}) - p.SetCommonParameters(commonParameters(meta)) return p, nil } @@ -48,15 +46,15 @@ func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.Get } p := new(getsvc.RangePrm) - p.SetPrivateKey(key) + p.SetCommonParameters(util.CommonPrmFromV2(req). + WithPrivateKey(key), + ) body := req.GetBody() p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) p.WithRawFlag(body.GetRaw()) - p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) p.SetChunkWriter(&streamObjectRangeWriter{stream}) p.SetRange(objectSDK.NewRangeFromV2(body.GetRange())) - p.SetCommonParameters(commonParameters(meta)) return p, nil } @@ -70,12 +68,12 @@ func (s *Service) toHashRangePrm(req *objectV2.GetRangeHashRequest) (*getsvc.Ran } p := new(getsvc.RangeHashPrm) - p.SetPrivateKey(key) + p.SetCommonParameters(util.CommonPrmFromV2(req). + WithPrivateKey(key), + ) body := req.GetBody() p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) - p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) - p.SetCommonParameters(commonParameters(meta)) rngsV2 := body.GetRanges() rngs := make([]*objectSDK.Range, 0, len(rngsV2)) @@ -127,42 +125,21 @@ func (s *Service) toHeadPrm(req *objectV2.HeadRequest, resp *objectV2.HeadRespon } p := new(getsvc.HeadPrm) - p.SetPrivateKey(key) + p.SetCommonParameters(util.CommonPrmFromV2(req). + WithPrivateKey(key), + ) body := req.GetBody() p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) p.WithRawFlag(body.GetRaw()) - p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) p.SetHeaderWriter(&headResponseWriter{ mainOnly: body.GetMainOnly(), body: resp.GetBody(), }) - p.SetCommonParameters(commonParameters(meta)) return p, nil } -// can be shared accross all services -func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption { - opts := make([]client.CallOption, 0, 3) - opts = append(opts, client.WithTTL(meta.GetTTL()-1)) - - if tok := meta.GetBearerToken(); tok != nil { - opts = append(opts, client.WithBearer(token.NewBearerTokenFromV2(tok))) - } - - if tok := meta.GetSessionToken(); tok != nil { - opts = append(opts, client.WithSession(token.NewSessionTokenFromV2(tok))) - } - - return opts -} - -func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm { - return new(util.CommonPrm). - WithLocalOnly(meta.GetTTL() <= 1) -} - func splitInfoResponse(info *objectSDK.SplitInfo) *objectV2.GetResponse { resp := new(objectV2.GetResponse) diff --git a/pkg/services/object/search/exec.go b/pkg/services/object/search/exec.go index 3469c1f01..c6306f7f6 100644 --- a/pkg/services/object/search/exec.go +++ b/pkg/services/object/search/exec.go @@ -60,11 +60,11 @@ func (exec execCtx) isLocal() bool { } func (exec execCtx) key() *ecdsa.PrivateKey { - return exec.prm.key + return exec.prm.common.PrivateKey() } func (exec execCtx) callOptions() []client.CallOption { - return exec.prm.callOpts + return exec.prm.common.RemoteCallOptions() } func (exec execCtx) remotePrm() *client.SearchObjectParams { diff --git a/pkg/services/object/search/prm.go b/pkg/services/object/search/prm.go index a458f9bae..d81cf4fba 100644 --- a/pkg/services/object/search/prm.go +++ b/pkg/services/object/search/prm.go @@ -1,8 +1,6 @@ package searchsvc import ( - "crypto/ecdsa" - "github.com/nspcc-dev/neofs-api-go/pkg/client" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-node/pkg/services/object/util" @@ -12,11 +10,6 @@ import ( type Prm struct { writer IDListWriter - // TODO: replace key and callOpts to CommonPrm - key *ecdsa.PrivateKey - - callOpts []client.CallOption - common *util.CommonPrm client.SearchObjectParams @@ -28,16 +21,6 @@ type IDListWriter interface { WriteIDs([]*objectSDK.ID) error } -// SetPrivateKey sets private key to use during execution. -func (p *Prm) SetPrivateKey(key *ecdsa.PrivateKey) { - p.key = key -} - -// SetRemoteCallOptions sets call options remote remote client calls. -func (p *Prm) SetRemoteCallOptions(opts ...client.CallOption) { - p.callOpts = opts -} - // SetCommonParameters sets common parameters of the operation. func (p *Prm) SetCommonParameters(common *util.CommonPrm) { p.common = common diff --git a/pkg/services/object/search/v2/util.go b/pkg/services/object/search/v2/util.go index c6ef8409b..38193879a 100644 --- a/pkg/services/object/search/v2/util.go +++ b/pkg/services/object/search/v2/util.go @@ -1,12 +1,10 @@ package searchsvc import ( - "github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/container" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/token" objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" - "github.com/nspcc-dev/neofs-api-go/v2/session" objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object" searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search" "github.com/nspcc-dev/neofs-node/pkg/services/object/util" @@ -21,9 +19,10 @@ func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStre } p := new(searchsvc.Prm) - p.SetPrivateKey(key) - p.SetCommonParameters(commonParameters(meta)) - p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) + p.SetCommonParameters(util.CommonPrmFromV2(req). + WithPrivateKey(key), + ) + p.SetWriter(&streamWriter{ stream: stream, }) @@ -34,25 +33,3 @@ func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStre return p, nil } - -// can be shared accross all services -func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption { - opts := make([]client.CallOption, 0, 3) - opts = append(opts, client.WithTTL(meta.GetTTL()-1)) - - if tok := meta.GetBearerToken(); tok != nil { - opts = append(opts, client.WithBearer(token.NewBearerTokenFromV2(tok))) - } - - if tok := meta.GetSessionToken(); tok != nil { - opts = append(opts, client.WithSession(token.NewSessionTokenFromV2(tok))) - } - - return opts -} - -func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm { - - return new(util.CommonPrm). - WithLocalOnly(meta.GetTTL() <= 1) -} diff --git a/pkg/services/object/util/prm.go b/pkg/services/object/util/prm.go index 14c957d02..995f5bcbe 100644 --- a/pkg/services/object/util/prm.go +++ b/pkg/services/object/util/prm.go @@ -1,6 +1,9 @@ package util import ( + "crypto/ecdsa" + + "github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-api-go/v2/session" ) @@ -8,8 +11,13 @@ import ( type CommonPrm struct { local bool - token *token.SessionToken + token *token.SessionToken + bearer *token.BearerToken + + key *ecdsa.PrivateKey + + callOpts []client.CallOption } func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm { @@ -44,6 +52,42 @@ func (p *CommonPrm) WithBearerToken(token *token.BearerToken) *CommonPrm { return p } +// WithPrivateKey sets private key to use during execution. +func (p *CommonPrm) WithPrivateKey(key *ecdsa.PrivateKey) *CommonPrm { + if p != nil { + p.key = key + } + + return p +} + +// PrivateKey returns private key to use during execution. +func (p *CommonPrm) PrivateKey() *ecdsa.PrivateKey { + if p != nil { + return p.key + } + + return nil +} + +// WithRemoteCallOptions sets call options remote remote client calls. +func (p *CommonPrm) WithRemoteCallOptions(opts ...client.CallOption) *CommonPrm { + if p != nil { + p.callOpts = opts + } + + return p +} + +// RemoteCallOptions return call options for remote client calls. +func (p *CommonPrm) RemoteCallOptions() []client.CallOption { + if p != nil { + return p.callOpts + } + + return nil +} + func (p *CommonPrm) SessionToken() *token.SessionToken { if p != nil { return p.token @@ -65,9 +109,24 @@ func CommonPrmFromV2(req interface { }) *CommonPrm { meta := req.GetMetaHeader() - return &CommonPrm{ - local: meta.GetTTL() <= 1, // FIXME: use constant - token: token.NewSessionTokenFromV2(meta.GetSessionToken()), - bearer: token.NewBearerTokenFromV2(meta.GetBearerToken()), + prm := &CommonPrm{ + local: meta.GetTTL() <= 1, // FIXME: use constant + token: nil, + bearer: nil, + callOpts: make([]client.CallOption, 0, 3), } + + prm.callOpts = append(prm.callOpts, client.WithTTL(meta.GetTTL()-1)) + + if tok := meta.GetSessionToken(); tok != nil { + prm.token = token.NewSessionTokenFromV2(tok) + prm.callOpts = append(prm.callOpts, client.WithSession(prm.token)) + } + + if tok := meta.GetBearerToken(); tok != nil { + prm.bearer = token.NewBearerTokenFromV2(tok) + prm.callOpts = append(prm.callOpts, client.WithBearer(prm.bearer)) + } + + return prm }