2020-09-29 15:05:22 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-12-11 11:59:16 +00:00
|
|
|
"crypto/ecdsa"
|
2021-01-12 14:55:02 +00:00
|
|
|
"strconv"
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-12-28 15:55:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-12-11 11:59:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-05-31 11:03:17 +00:00
|
|
|
sessionsdk "github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2020-09-29 15:08:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-09-29 15:05:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommonPrm struct {
|
|
|
|
local bool
|
2020-09-29 15:08:16 +00:00
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
netmapEpoch, netmapLookupDepth uint64
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
token *sessionsdk.Token
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
bearer *token.BearerToken
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
keyStor *KeyStorage
|
2020-12-11 11:59:16 +00:00
|
|
|
|
|
|
|
callOpts []client.CallOption
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
type remoteCallOpts struct {
|
|
|
|
opts []client.CallOption
|
|
|
|
}
|
|
|
|
|
|
|
|
type DynamicCallOption func(*remoteCallOpts)
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.local = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) LocalOnly() bool {
|
|
|
|
if p != nil {
|
|
|
|
return p.local
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (p *CommonPrm) WithSessionToken(token *sessionsdk.Token) *CommonPrm {
|
2020-09-29 15:08:16 +00:00
|
|
|
if p != nil {
|
|
|
|
p.token = token
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
func (p *CommonPrm) WithBearerToken(token *token.BearerToken) *CommonPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.bearer = token
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
// WithKeyStorage sets private key storage to use during execution.
|
|
|
|
func (p *CommonPrm) WithKeyStorage(stor *KeyStorage) *CommonPrm {
|
2020-12-11 11:59:16 +00:00
|
|
|
if p != nil {
|
2021-10-26 12:07:28 +00:00
|
|
|
p.keyStor = stor
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
// KeyStorage returns private key storage to use during execution.
|
|
|
|
func (p *CommonPrm) KeyStorage() *KeyStorage {
|
2020-12-11 11:59:16 +00:00
|
|
|
if p != nil {
|
2021-10-26 12:07:28 +00:00
|
|
|
return p.keyStor
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-01-12 14:55:02 +00:00
|
|
|
func (p *CommonPrm) RemoteCallOptions(dynamic ...DynamicCallOption) []client.CallOption {
|
2020-12-11 11:59:16 +00:00
|
|
|
if p != nil {
|
2021-01-12 14:55:02 +00:00
|
|
|
o := &remoteCallOpts{
|
|
|
|
opts: p.callOpts,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, applier := range dynamic {
|
|
|
|
applier(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.opts
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
func WithNetmapEpoch(v uint64) DynamicCallOption {
|
|
|
|
return func(o *remoteCallOpts) {
|
|
|
|
xHdr := pkg.NewXHeader()
|
|
|
|
xHdr.SetKey(session.XHeaderNetmapEpoch)
|
|
|
|
xHdr.SetValue(strconv.FormatUint(v, 10))
|
|
|
|
|
|
|
|
o.opts = append(o.opts, client.WithXHeader(xHdr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 15:22:21 +00:00
|
|
|
// WithKey sets key to use for the request.
|
|
|
|
func WithKey(key *ecdsa.PrivateKey) DynamicCallOption {
|
|
|
|
return func(o *remoteCallOpts) {
|
|
|
|
o.opts = append(o.opts, client.WithKey(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (p *CommonPrm) SessionToken() *sessionsdk.Token {
|
2020-09-29 15:08:16 +00:00
|
|
|
if p != nil {
|
|
|
|
return p.token
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
func (p *CommonPrm) BearerToken() *token.BearerToken {
|
|
|
|
if p != nil {
|
|
|
|
return p.bearer
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
func (p *CommonPrm) NetmapEpoch() uint64 {
|
|
|
|
if p != nil {
|
|
|
|
return p.netmapEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) NetmapLookupDepth() uint64 {
|
|
|
|
if p != nil {
|
|
|
|
return p.netmapLookupDepth
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) SetNetmapLookupDepth(v uint64) {
|
|
|
|
if p != nil {
|
|
|
|
p.netmapLookupDepth = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
func CommonPrmFromV2(req interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
2021-01-12 14:55:02 +00:00
|
|
|
}) (*CommonPrm, error) {
|
2020-09-29 15:05:22 +00:00
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2020-12-28 15:55:16 +00:00
|
|
|
xHdrs := meta.GetXHeaders()
|
|
|
|
|
|
|
|
const staticOptNum = 3
|
|
|
|
|
2020-12-11 11:59:16 +00:00
|
|
|
prm := &CommonPrm{
|
|
|
|
local: meta.GetTTL() <= 1, // FIXME: use constant
|
|
|
|
token: nil,
|
|
|
|
bearer: nil,
|
2020-12-28 15:55:16 +00:00
|
|
|
callOpts: make([]client.CallOption, 0, staticOptNum+len(xHdrs)),
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prm.callOpts = append(prm.callOpts, client.WithTTL(meta.GetTTL()-1))
|
|
|
|
|
|
|
|
if tok := meta.GetSessionToken(); tok != nil {
|
2021-05-31 11:03:17 +00:00
|
|
|
prm.token = sessionsdk.NewTokenFromV2(tok)
|
2020-12-11 11:59:16 +00:00
|
|
|
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))
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-12-28 15:55:16 +00:00
|
|
|
for i := range xHdrs {
|
2021-01-12 14:55:02 +00:00
|
|
|
switch xHdrs[i].GetKey() {
|
|
|
|
case session.XHeaderNetmapEpoch:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
prm.netmapEpoch, err = strconv.ParseUint(xHdrs[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case session.XHeaderNetmapLookupDepth:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
prm.netmapLookupDepth, err = strconv.ParseUint(xHdrs[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
prm.callOpts = append(prm.callOpts,
|
|
|
|
client.WithXHeader(
|
|
|
|
pkg.NewXHeaderFromV2(xHdrs[i]),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2020-12-28 15:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
return prm, nil
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|