From 929c9851a632817fc734f5f3023b97bd1274b6d0 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 18 Mar 2022 11:46:07 +0300 Subject: [PATCH] [#1255] node/session: Create separate dir for in-memory storage Move in-memory session storage to the separate directory of `storage`. It is done for future support of different kind of session storages. Signed-off-by: Pavel Karpy --- cmd/neofs-node/config.go | 2 +- cmd/neofs-node/session.go | 4 ++-- pkg/services/object/util/key.go | 6 +++--- pkg/services/object/util/key_test.go | 4 ++-- .../session/storage/{ => temporary}/executor.go | 12 +++++++----- .../session/storage/{ => temporary}/storage.go | 17 +++++++++++------ pkg/services/session/storage/types.go | 10 ++++++++++ 7 files changed, 36 insertions(+), 19 deletions(-) rename pkg/services/session/storage/{ => temporary}/executor.go (80%) rename pkg/services/session/storage/{ => temporary}/storage.go (61%) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 2b2ee85cd..eae65314e 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -37,7 +37,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/services/object/acl/eacl" trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller" truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage" - tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" + tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage/temporary" "github.com/nspcc-dev/neofs-node/pkg/services/util/response" "github.com/nspcc-dev/neofs-node/pkg/util" "github.com/nspcc-dev/neofs-node/pkg/util/logger" diff --git a/cmd/neofs-node/session.go b/cmd/neofs-node/session.go index 39c4bb681..076bcf25a 100644 --- a/cmd/neofs-node/session.go +++ b/cmd/neofs-node/session.go @@ -6,11 +6,11 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" sessionTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc" sessionSvc "github.com/nspcc-dev/neofs-node/pkg/services/session" - "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" + "github.com/nspcc-dev/neofs-node/pkg/services/session/storage/temporary" ) func initSessionService(c *cfg) { - c.privateTokenStore = storage.New() + c.privateTokenStore = temporary.NewTokenStore() addNewEpochNotificationHandler(c, func(ev event.Event) { c.privateTokenStore.RemoveOld(ev.(netmap.NewEpoch).EpochNumber()) }) diff --git a/pkg/services/object/util/key.go b/pkg/services/object/util/key.go index c92d8c120..f7a8d1880 100644 --- a/pkg/services/object/util/key.go +++ b/pkg/services/object/util/key.go @@ -4,7 +4,7 @@ import ( "crypto/ecdsa" "github.com/nspcc-dev/neofs-node/pkg/core/netmap" - "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" + "github.com/nspcc-dev/neofs-node/pkg/services/session/storage/temporary" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "github.com/nspcc-dev/neofs-sdk-go/session" ) @@ -13,13 +13,13 @@ import ( type KeyStorage struct { key *ecdsa.PrivateKey - tokenStore *storage.TokenStore + tokenStore *temporary.TokenStore networkState netmap.State } // NewKeyStorage creates, initializes and returns new KeyStorage instance. -func NewKeyStorage(localKey *ecdsa.PrivateKey, tokenStore *storage.TokenStore, net netmap.State) *KeyStorage { +func NewKeyStorage(localKey *ecdsa.PrivateKey, tokenStore *temporary.TokenStore, net netmap.State) *KeyStorage { return &KeyStorage{ key: localKey, tokenStore: tokenStore, diff --git a/pkg/services/object/util/key_test.go b/pkg/services/object/util/key_test.go index 20b12bcac..0e42b70f6 100644 --- a/pkg/services/object/util/key_test.go +++ b/pkg/services/object/util/key_test.go @@ -9,7 +9,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" sessionV2 "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-node/pkg/services/object/util" - tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" + tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage/temporary" "github.com/nspcc-dev/neofs-sdk-go/session" "github.com/stretchr/testify/require" ) @@ -18,7 +18,7 @@ func TestNewKeyStorage(t *testing.T) { nodeKey, err := keys.NewPrivateKey() require.NoError(t, err) - tokenStor := tokenStorage.New() + tokenStor := tokenStorage.NewTokenStore() stor := util.NewKeyStorage(&nodeKey.PrivateKey, tokenStor, mockedNetworkState{42}) t.Run("node key", func(t *testing.T) { diff --git a/pkg/services/session/storage/executor.go b/pkg/services/session/storage/temporary/executor.go similarity index 80% rename from pkg/services/session/storage/executor.go rename to pkg/services/session/storage/temporary/executor.go index 5edbdf95a..30e5221a7 100644 --- a/pkg/services/session/storage/executor.go +++ b/pkg/services/session/storage/temporary/executor.go @@ -1,4 +1,4 @@ -package storage +package temporary import ( "context" @@ -8,6 +8,7 @@ import ( "github.com/mr-tron/base58" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neofs-api-go/v2/session" + "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" "github.com/nspcc-dev/neofs-sdk-go/owner" ) @@ -32,14 +33,15 @@ func (s *TokenStore) Create(ctx context.Context, body *session.CreateRequestBody return nil, err } + privateToken := new(storage.PrivateToken) + privateToken.SetSessionKey(&sk.PrivateKey) + privateToken.SetExpiredAt(body.GetExpiration()) + s.mtx.Lock() s.tokens[key{ tokenID: base58.Encode(uidBytes), ownerID: base58.Encode(ownerBytes), - }] = &PrivateToken{ - sessionKey: &sk.PrivateKey, - exp: body.GetExpiration(), - } + }] = privateToken s.mtx.Unlock() res := new(session.CreateResponseBody) diff --git a/pkg/services/session/storage/storage.go b/pkg/services/session/storage/temporary/storage.go similarity index 61% rename from pkg/services/session/storage/storage.go rename to pkg/services/session/storage/temporary/storage.go index f045e93bf..7ede70ce2 100644 --- a/pkg/services/session/storage/storage.go +++ b/pkg/services/session/storage/temporary/storage.go @@ -1,9 +1,10 @@ -package storage +package temporary import ( "sync" "github.com/mr-tron/base58" + "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" "github.com/nspcc-dev/neofs-sdk-go/owner" ) @@ -12,26 +13,30 @@ type key struct { ownerID string } +// TokenStore is an in-memory session token store. +// It allows creating (storing), retrieving and +// expiring (removing) session tokens. +// Must be created only via calling NewTokenStore. type TokenStore struct { mtx *sync.RWMutex - tokens map[key]*PrivateToken + tokens map[key]*storage.PrivateToken } -// New creates, initializes and returns a new TokenStore instance. +// NewTokenStore creates, initializes and returns a new TokenStore instance. // // The elements of the instance are stored in the map. -func New() *TokenStore { +func NewTokenStore() *TokenStore { return &TokenStore{ mtx: new(sync.RWMutex), - tokens: make(map[key]*PrivateToken), + tokens: make(map[key]*storage.PrivateToken), } } // Get returns private token corresponding to the given identifiers. // // Returns nil is there is no element in storage. -func (s *TokenStore) Get(ownerID *owner.ID, tokenID []byte) *PrivateToken { +func (s *TokenStore) Get(ownerID *owner.ID, tokenID []byte) *storage.PrivateToken { ownerBytes, err := ownerID.Marshal() if err != nil { panic(err) diff --git a/pkg/services/session/storage/types.go b/pkg/services/session/storage/types.go index 76d61afe3..4d7ff8375 100644 --- a/pkg/services/session/storage/types.go +++ b/pkg/services/session/storage/types.go @@ -11,6 +11,16 @@ type PrivateToken struct { exp uint64 } +// SetSessionKey sets a private session key. +func (t *PrivateToken) SetSessionKey(sessionKey *ecdsa.PrivateKey) { + t.sessionKey = sessionKey +} + +// SetExpiredAt sets epoch number until token is valid. +func (t *PrivateToken) SetExpiredAt(exp uint64) { + t.exp = exp +} + // SessionKey returns the private session key. func (t *PrivateToken) SessionKey() *ecdsa.PrivateKey { return t.sessionKey