[#70] pool: Ignore default session token in non object service requests

Default session token is created for object service requests
and should not be reused in container or any other service requests.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-12-16 18:46:38 +03:00 committed by Angira Kekteeva
parent 2806d90089
commit b1770ecb92

View file

@ -158,7 +158,8 @@ type clientPack struct {
type CallOption func(config *callConfig) type CallOption func(config *callConfig)
type callConfig struct { type callConfig struct {
isRetry bool isRetry bool
useDefaultSession bool
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
btoken *token.BearerToken btoken *token.BearerToken
@ -189,6 +190,12 @@ func retry() CallOption {
} }
} }
func useDefaultSession() CallOption {
return func(config *callConfig) {
config.useDefaultSession = true
}
}
func cfgFromOpts(opts ...CallOption) *callConfig { func cfgFromOpts(opts ...CallOption) *callConfig {
var cfg = new(callConfig) var cfg = new(callConfig)
for _, opt := range opts { for _, opt := range opts {
@ -449,7 +456,7 @@ func (p *pool) conn(ctx context.Context, cfg *callConfig) (*clientPack, []client
clientCallOptions = append(clientCallOptions, client.WithKey(key)) clientCallOptions = append(clientCallOptions, client.WithKey(key))
sessionToken := cfg.stoken sessionToken := cfg.stoken
if sessionToken == nil { if sessionToken == nil && cfg.useDefaultSession {
cacheKey := formCacheKey(cp.address, key) cacheKey := formCacheKey(cp.address, key)
sessionToken = p.cache.Get(cacheKey) sessionToken = p.cache.Get(cacheKey)
if sessionToken == nil { if sessionToken == nil {
@ -486,7 +493,7 @@ func (p *pool) checkSessionTokenErr(err error, address string) bool {
} }
func (p *pool) PutObject(ctx context.Context, params *client.PutObjectParams, opts ...CallOption) (*object.ID, error) { func (p *pool) PutObject(ctx context.Context, params *client.PutObjectParams, opts ...CallOption) (*object.ID, error) {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -510,7 +517,7 @@ func (p *pool) PutObject(ctx context.Context, params *client.PutObjectParams, op
} }
func (p *pool) DeleteObject(ctx context.Context, params *client.DeleteObjectParams, opts ...CallOption) error { func (p *pool) DeleteObject(ctx context.Context, params *client.DeleteObjectParams, opts ...CallOption) error {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return err return err
@ -533,7 +540,7 @@ func (p *pool) DeleteObject(ctx context.Context, params *client.DeleteObjectPara
} }
func (p *pool) GetObject(ctx context.Context, params *client.GetObjectParams, opts ...CallOption) (*object.Object, error) { func (p *pool) GetObject(ctx context.Context, params *client.GetObjectParams, opts ...CallOption) (*object.Object, error) {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -557,7 +564,7 @@ func (p *pool) GetObject(ctx context.Context, params *client.GetObjectParams, op
} }
func (p *pool) GetObjectHeader(ctx context.Context, params *client.ObjectHeaderParams, opts ...CallOption) (*object.Object, error) { func (p *pool) GetObjectHeader(ctx context.Context, params *client.ObjectHeaderParams, opts ...CallOption) (*object.Object, error) {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -581,7 +588,7 @@ func (p *pool) GetObjectHeader(ctx context.Context, params *client.ObjectHeaderP
} }
func (p *pool) ObjectPayloadRangeData(ctx context.Context, params *client.RangeDataParams, opts ...CallOption) ([]byte, error) { func (p *pool) ObjectPayloadRangeData(ctx context.Context, params *client.RangeDataParams, opts ...CallOption) ([]byte, error) {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -615,7 +622,7 @@ func copyRangeChecksumParams(prm *client.RangeChecksumParams) *client.RangeCheck
} }
func (p *pool) ObjectPayloadRangeSHA256(ctx context.Context, params *client.RangeChecksumParams, opts ...CallOption) ([][32]byte, error) { func (p *pool) ObjectPayloadRangeSHA256(ctx context.Context, params *client.RangeChecksumParams, opts ...CallOption) ([][32]byte, error) {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -657,7 +664,7 @@ func (p *pool) ObjectPayloadRangeSHA256(ctx context.Context, params *client.Rang
} }
func (p *pool) ObjectPayloadRangeTZ(ctx context.Context, params *client.RangeChecksumParams, opts ...CallOption) ([][64]byte, error) { func (p *pool) ObjectPayloadRangeTZ(ctx context.Context, params *client.RangeChecksumParams, opts ...CallOption) ([][64]byte, error) {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -699,7 +706,7 @@ func (p *pool) ObjectPayloadRangeTZ(ctx context.Context, params *client.RangeChe
} }
func (p *pool) SearchObject(ctx context.Context, params *client.SearchObjectParams, opts ...CallOption) ([]*object.ID, error) { func (p *pool) SearchObject(ctx context.Context, params *client.SearchObjectParams, opts ...CallOption) ([]*object.ID, error) {
cfg := cfgFromOpts(opts...) cfg := cfgFromOpts(append(opts, useDefaultSession())...)
cp, options, err := p.conn(ctx, cfg) cp, options, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err