[#243] services/object: Share common parameters across services

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-12-11 14:59:16 +03:00 committed by Alex Vanin
parent a01262d8bd
commit fb50362dcc
12 changed files with 89 additions and 161 deletions

BIN
go.sum

Binary file not shown.

View file

@ -60,7 +60,7 @@ func (exec execCtx) isLocal() bool {
} }
func (exec *execCtx) key() *ecdsa.PrivateKey { func (exec *execCtx) key() *ecdsa.PrivateKey {
return exec.prm.key return exec.prm.common.PrivateKey()
} }
func (exec *execCtx) address() *objectSDK.Address { func (exec *execCtx) address() *objectSDK.Address {
@ -76,7 +76,7 @@ func (exec *execCtx) commonParameters() *util.CommonPrm {
} }
func (exec execCtx) callOptions() []client.CallOption { func (exec execCtx) callOptions() []client.CallOption {
return exec.prm.callOpts return exec.prm.common.RemoteCallOptions()
} }
func (exec *execCtx) newAddress(id *objectSDK.ID) *objectSDK.Address { func (exec *execCtx) newAddress(id *objectSDK.ID) *objectSDK.Address {

View file

@ -1,20 +1,14 @@
package deletesvc package deletesvc
import ( import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
) )
// Prm groups parameters of Delete service call. // Prm groups parameters of Delete service call.
type Prm struct { type Prm struct {
key *ecdsa.PrivateKey
common *util.CommonPrm common *util.CommonPrm
callOpts []client.CallOption
client.DeleteObjectParams client.DeleteObjectParams
} }
@ -22,13 +16,3 @@ type Prm struct {
func (p *Prm) SetCommonParameters(common *util.CommonPrm) { func (p *Prm) SetCommonParameters(common *util.CommonPrm) {
p.common = common 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
}

View file

@ -25,12 +25,10 @@ func (w *headSvcWrapper) headAddress(exec *execCtx, addr *objectSDK.Address) (*o
wr := getsvc.NewSimpleObjectWriter() wr := getsvc.NewSimpleObjectWriter()
p := getsvc.HeadPrm{} p := getsvc.HeadPrm{}
p.SetPrivateKey(exec.key())
p.SetCommonParameters(exec.commonParameters()) p.SetCommonParameters(exec.commonParameters())
p.SetHeaderWriter(wr) p.SetHeaderWriter(wr)
p.WithRawFlag(true) p.WithRawFlag(true)
p.WithAddress(addr) p.WithAddress(addr)
p.SetRemoteCallOptions(exec.callOptions()...)
err := (*getsvc.Service)(w).Head(exec.context(), p) err := (*getsvc.Service)(w).Head(exec.context(), p)
if err != nil { if err != nil {
@ -86,10 +84,8 @@ func (w *searchSvcWrapper) splitMembers(exec *execCtx) ([]*objectSDK.ID, error)
p := searchsvc.Prm{} p := searchsvc.Prm{}
p.SetWriter(wr) p.SetWriter(wr)
p.SetCommonParameters(exec.commonParameters()) p.SetCommonParameters(exec.commonParameters())
p.SetPrivateKey(exec.key())
p.WithContainerID(exec.containerID()) p.WithContainerID(exec.containerID())
p.WithSearchFilters(fs) p.WithSearchFilters(fs)
p.SetRemoteCallOptions(exec.callOptions()...)
err := (*searchsvc.Service)(w).Search(exec.context(), p) err := (*searchsvc.Service)(w).Search(exec.context(), p)
if err != nil { if err != nil {

View file

@ -1,11 +1,9 @@
package deletesvc package deletesvc
import ( 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/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-api-go/pkg/token"
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" 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" deletesvc "github.com/nspcc-dev/neofs-node/pkg/services/object/delete"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "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 := new(deletesvc.Prm)
p.SetPrivateKey(key) p.SetCommonParameters(util.CommonPrmFromV2(req).
p.SetCommonParameters(commonParameters(meta)) WithPrivateKey(key),
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) )
body := req.GetBody() body := req.GetBody()
p.WithAddress(object.NewAddressFromV2(body.GetAddress())) 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) { func (w *tombstoneBodyWriter) SetAddress(addr *object.Address) {
w.body.SetTombstone(addr.ToV2()) 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
}

View file

@ -96,11 +96,11 @@ func (exec execCtx) address() *objectSDK.Address {
} }
func (exec execCtx) key() *ecdsa.PrivateKey { func (exec execCtx) key() *ecdsa.PrivateKey {
return exec.prm.key return exec.prm.common.PrivateKey()
} }
func (exec execCtx) callOptions() []client.CallOption { func (exec execCtx) callOptions() []client.CallOption {
return exec.prm.callOpts return exec.prm.common.RemoteCallOptions()
} }
func (exec execCtx) remotePrm() *client.GetObjectParams { func (exec execCtx) remotePrm() *client.GetObjectParams {

View file

@ -1,7 +1,6 @@
package getsvc package getsvc
import ( import (
"crypto/ecdsa"
"hash" "hash"
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
@ -39,11 +38,6 @@ type HeadPrm struct {
type commonPrm struct { type commonPrm struct {
objWriter ObjectWriter objWriter ObjectWriter
// TODO: replace key and callOpts to CommonPrm
key *ecdsa.PrivateKey
callOpts []client.CallOption
common *util.CommonPrm common *util.CommonPrm
client.GetObjectParams client.GetObjectParams
@ -72,16 +66,6 @@ func (p *Prm) SetObjectWriter(w ObjectWriter) {
p.objWriter = w 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. // SetChunkWriter sets target component to write the object payload range.
func (p *RangePrm) SetChunkWriter(w ChunkWriter) { func (p *RangePrm) SetChunkWriter(w ChunkWriter) {
p.objWriter = &partWriter{ p.objWriter = &partWriter{

View file

@ -4,12 +4,10 @@ import (
"crypto/sha256" "crypto/sha256"
"hash" "hash"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-api-go/pkg/token"
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" 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/refs"
"github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object" objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
getsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/get" 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 := new(getsvc.Prm)
p.SetPrivateKey(key) p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody() body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.WithRawFlag(body.GetRaw()) p.WithRawFlag(body.GetRaw())
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetObjectWriter(&streamObjectWriter{stream}) p.SetObjectWriter(&streamObjectWriter{stream})
p.SetCommonParameters(commonParameters(meta))
return p, nil return p, nil
} }
@ -48,15 +46,15 @@ func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.Get
} }
p := new(getsvc.RangePrm) p := new(getsvc.RangePrm)
p.SetPrivateKey(key) p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody() body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.WithRawFlag(body.GetRaw()) p.WithRawFlag(body.GetRaw())
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetChunkWriter(&streamObjectRangeWriter{stream}) p.SetChunkWriter(&streamObjectRangeWriter{stream})
p.SetRange(objectSDK.NewRangeFromV2(body.GetRange())) p.SetRange(objectSDK.NewRangeFromV2(body.GetRange()))
p.SetCommonParameters(commonParameters(meta))
return p, nil return p, nil
} }
@ -70,12 +68,12 @@ func (s *Service) toHashRangePrm(req *objectV2.GetRangeHashRequest) (*getsvc.Ran
} }
p := new(getsvc.RangeHashPrm) p := new(getsvc.RangeHashPrm)
p.SetPrivateKey(key) p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody() body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetCommonParameters(commonParameters(meta))
rngsV2 := body.GetRanges() rngsV2 := body.GetRanges()
rngs := make([]*objectSDK.Range, 0, len(rngsV2)) 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 := new(getsvc.HeadPrm)
p.SetPrivateKey(key) p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody() body := req.GetBody()
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress())) p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
p.WithRawFlag(body.GetRaw()) p.WithRawFlag(body.GetRaw())
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetHeaderWriter(&headResponseWriter{ p.SetHeaderWriter(&headResponseWriter{
mainOnly: body.GetMainOnly(), mainOnly: body.GetMainOnly(),
body: resp.GetBody(), body: resp.GetBody(),
}) })
p.SetCommonParameters(commonParameters(meta))
return p, nil 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 { func splitInfoResponse(info *objectSDK.SplitInfo) *objectV2.GetResponse {
resp := new(objectV2.GetResponse) resp := new(objectV2.GetResponse)

View file

@ -60,11 +60,11 @@ func (exec execCtx) isLocal() bool {
} }
func (exec execCtx) key() *ecdsa.PrivateKey { func (exec execCtx) key() *ecdsa.PrivateKey {
return exec.prm.key return exec.prm.common.PrivateKey()
} }
func (exec execCtx) callOptions() []client.CallOption { func (exec execCtx) callOptions() []client.CallOption {
return exec.prm.callOpts return exec.prm.common.RemoteCallOptions()
} }
func (exec execCtx) remotePrm() *client.SearchObjectParams { func (exec execCtx) remotePrm() *client.SearchObjectParams {

View file

@ -1,8 +1,6 @@
package searchsvc package searchsvc
import ( import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
@ -12,11 +10,6 @@ import (
type Prm struct { type Prm struct {
writer IDListWriter writer IDListWriter
// TODO: replace key and callOpts to CommonPrm
key *ecdsa.PrivateKey
callOpts []client.CallOption
common *util.CommonPrm common *util.CommonPrm
client.SearchObjectParams client.SearchObjectParams
@ -28,16 +21,6 @@ type IDListWriter interface {
WriteIDs([]*objectSDK.ID) error 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. // SetCommonParameters sets common parameters of the operation.
func (p *Prm) SetCommonParameters(common *util.CommonPrm) { func (p *Prm) SetCommonParameters(common *util.CommonPrm) {
p.common = common p.common = common

View file

@ -1,12 +1,10 @@
package searchsvc package searchsvc
import ( import (
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/container"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-api-go/pkg/token"
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" 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" objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search" searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "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 := new(searchsvc.Prm)
p.SetPrivateKey(key) p.SetCommonParameters(util.CommonPrmFromV2(req).
p.SetCommonParameters(commonParameters(meta)) WithPrivateKey(key),
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...) )
p.SetWriter(&streamWriter{ p.SetWriter(&streamWriter{
stream: stream, stream: stream,
}) })
@ -34,25 +33,3 @@ func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStre
return p, nil 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)
}

View file

@ -1,6 +1,9 @@
package util package util
import ( 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/pkg/token"
"github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-api-go/v2/session"
) )
@ -8,8 +11,13 @@ import (
type CommonPrm struct { type CommonPrm struct {
local bool local bool
token *token.SessionToken token *token.SessionToken
bearer *token.BearerToken bearer *token.BearerToken
key *ecdsa.PrivateKey
callOpts []client.CallOption
} }
func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm { func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm {
@ -44,6 +52,42 @@ func (p *CommonPrm) WithBearerToken(token *token.BearerToken) *CommonPrm {
return p 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 { func (p *CommonPrm) SessionToken() *token.SessionToken {
if p != nil { if p != nil {
return p.token return p.token
@ -65,9 +109,24 @@ func CommonPrmFromV2(req interface {
}) *CommonPrm { }) *CommonPrm {
meta := req.GetMetaHeader() meta := req.GetMetaHeader()
return &CommonPrm{ prm := &CommonPrm{
local: meta.GetTTL() <= 1, // FIXME: use constant local: meta.GetTTL() <= 1, // FIXME: use constant
token: token.NewSessionTokenFromV2(meta.GetSessionToken()), token: nil,
bearer: token.NewBearerTokenFromV2(meta.GetBearerToken()), 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
} }