forked from TrueCloudLab/frostfs-node
[#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 <carpawell@nspcc.ru>
This commit is contained in:
parent
2a69aaf976
commit
929c9851a6
7 changed files with 36 additions and 19 deletions
|
@ -37,7 +37,7 @@ import (
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/services/object/acl/eacl"
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/acl/eacl"
|
||||||
trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
|
trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
|
||||||
truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage"
|
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/services/util/response"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/util"
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
||||||
sessionTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc"
|
sessionTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc"
|
||||||
sessionSvc "github.com/nspcc-dev/neofs-node/pkg/services/session"
|
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) {
|
func initSessionService(c *cfg) {
|
||||||
c.privateTokenStore = storage.New()
|
c.privateTokenStore = temporary.NewTokenStore()
|
||||||
addNewEpochNotificationHandler(c, func(ev event.Event) {
|
addNewEpochNotificationHandler(c, func(ev event.Event) {
|
||||||
c.privateTokenStore.RemoveOld(ev.(netmap.NewEpoch).EpochNumber())
|
c.privateTokenStore.RemoveOld(ev.(netmap.NewEpoch).EpochNumber())
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
"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"
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/session"
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
||||||
)
|
)
|
||||||
|
@ -13,13 +13,13 @@ import (
|
||||||
type KeyStorage struct {
|
type KeyStorage struct {
|
||||||
key *ecdsa.PrivateKey
|
key *ecdsa.PrivateKey
|
||||||
|
|
||||||
tokenStore *storage.TokenStore
|
tokenStore *temporary.TokenStore
|
||||||
|
|
||||||
networkState netmap.State
|
networkState netmap.State
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewKeyStorage creates, initializes and returns new KeyStorage instance.
|
// 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{
|
return &KeyStorage{
|
||||||
key: localKey,
|
key: localKey,
|
||||||
tokenStore: tokenStore,
|
tokenStore: tokenStore,
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
sessionV2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
sessionV2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
"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/nspcc-dev/neofs-sdk-go/session"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -18,7 +18,7 @@ func TestNewKeyStorage(t *testing.T) {
|
||||||
nodeKey, err := keys.NewPrivateKey()
|
nodeKey, err := keys.NewPrivateKey()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
tokenStor := tokenStorage.New()
|
tokenStor := tokenStorage.NewTokenStore()
|
||||||
stor := util.NewKeyStorage(&nodeKey.PrivateKey, tokenStor, mockedNetworkState{42})
|
stor := util.NewKeyStorage(&nodeKey.PrivateKey, tokenStor, mockedNetworkState{42})
|
||||||
|
|
||||||
t.Run("node key", func(t *testing.T) {
|
t.Run("node key", func(t *testing.T) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package storage
|
package temporary
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/mr-tron/base58"
|
"github.com/mr-tron/base58"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
"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"
|
"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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
privateToken := new(storage.PrivateToken)
|
||||||
|
privateToken.SetSessionKey(&sk.PrivateKey)
|
||||||
|
privateToken.SetExpiredAt(body.GetExpiration())
|
||||||
|
|
||||||
s.mtx.Lock()
|
s.mtx.Lock()
|
||||||
s.tokens[key{
|
s.tokens[key{
|
||||||
tokenID: base58.Encode(uidBytes),
|
tokenID: base58.Encode(uidBytes),
|
||||||
ownerID: base58.Encode(ownerBytes),
|
ownerID: base58.Encode(ownerBytes),
|
||||||
}] = &PrivateToken{
|
}] = privateToken
|
||||||
sessionKey: &sk.PrivateKey,
|
|
||||||
exp: body.GetExpiration(),
|
|
||||||
}
|
|
||||||
s.mtx.Unlock()
|
s.mtx.Unlock()
|
||||||
|
|
||||||
res := new(session.CreateResponseBody)
|
res := new(session.CreateResponseBody)
|
|
@ -1,9 +1,10 @@
|
||||||
package storage
|
package temporary
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/mr-tron/base58"
|
"github.com/mr-tron/base58"
|
||||||
|
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,26 +13,30 @@ type key struct {
|
||||||
ownerID string
|
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 {
|
type TokenStore struct {
|
||||||
mtx *sync.RWMutex
|
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.
|
// The elements of the instance are stored in the map.
|
||||||
func New() *TokenStore {
|
func NewTokenStore() *TokenStore {
|
||||||
return &TokenStore{
|
return &TokenStore{
|
||||||
mtx: new(sync.RWMutex),
|
mtx: new(sync.RWMutex),
|
||||||
tokens: make(map[key]*PrivateToken),
|
tokens: make(map[key]*storage.PrivateToken),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns private token corresponding to the given identifiers.
|
// Get returns private token corresponding to the given identifiers.
|
||||||
//
|
//
|
||||||
// Returns nil is there is no element in storage.
|
// 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()
|
ownerBytes, err := ownerID.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
|
@ -11,6 +11,16 @@ type PrivateToken struct {
|
||||||
exp uint64
|
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.
|
// SessionKey returns the private session key.
|
||||||
func (t *PrivateToken) SessionKey() *ecdsa.PrivateKey {
|
func (t *PrivateToken) SessionKey() *ecdsa.PrivateKey {
|
||||||
return t.sessionKey
|
return t.sessionKey
|
||||||
|
|
Loading…
Reference in a new issue