2021-10-25 12:10:13 +00:00
|
|
|
package util_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util"
|
|
|
|
tokenStorage "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/session/storage/temporary"
|
2024-11-07 14:32:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
sessionV2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
2023-03-07 13:38:26 +00:00
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
2021-10-25 12:10:13 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewKeyStorage(t *testing.T) {
|
|
|
|
nodeKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-18 08:46:07 +00:00
|
|
|
tokenStor := tokenStorage.NewTokenStore()
|
2021-10-25 12:19:50 +00:00
|
|
|
stor := util.NewKeyStorage(&nodeKey.PrivateKey, tokenStor, mockedNetworkState{42})
|
2021-10-25 12:10:13 +00:00
|
|
|
|
2023-11-21 08:42:30 +00:00
|
|
|
owner := usertest.ID()
|
2022-05-18 15:20:08 +00:00
|
|
|
|
2021-10-25 12:10:13 +00:00
|
|
|
t.Run("node key", func(t *testing.T) {
|
|
|
|
key, err := stor.GetKey(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, nodeKey.PrivateKey, *key)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown token", func(t *testing.T) {
|
2022-05-18 15:20:08 +00:00
|
|
|
_, err = stor.GetKey(&util.SessionInfo{
|
|
|
|
ID: uuid.New(),
|
2023-11-21 08:42:30 +00:00
|
|
|
Owner: usertest.ID(),
|
2022-05-18 15:20:08 +00:00
|
|
|
})
|
2021-10-25 12:10:13 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("known token", func(t *testing.T) {
|
2022-05-18 15:20:08 +00:00
|
|
|
tok := createToken(t, tokenStor, owner, 100)
|
2021-10-25 12:10:13 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
key, err := stor.GetKey(&util.SessionInfo{
|
|
|
|
ID: tok.ID(),
|
|
|
|
Owner: owner,
|
|
|
|
})
|
2021-10-25 12:10:13 +00:00
|
|
|
require.NoError(t, err)
|
2022-12-23 17:35:35 +00:00
|
|
|
require.True(t, tok.AssertAuthKey((*frostfsecdsa.PublicKey)(&key.PublicKey)))
|
2021-10-25 12:10:13 +00:00
|
|
|
})
|
2021-10-25 12:19:50 +00:00
|
|
|
|
|
|
|
t.Run("expired token", func(t *testing.T) {
|
2022-05-18 15:20:08 +00:00
|
|
|
tok := createToken(t, tokenStor, owner, 30)
|
|
|
|
_, err := stor.GetKey(&util.SessionInfo{
|
|
|
|
ID: tok.ID(),
|
|
|
|
Owner: owner,
|
|
|
|
})
|
2021-10-25 12:19:50 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2021-10-25 12:10:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
func createToken(t *testing.T, store *tokenStorage.TokenStore, owner user.ID, exp uint64) session.Object {
|
2022-05-17 13:59:46 +00:00
|
|
|
var ownerV2 refs.OwnerID
|
|
|
|
owner.WriteToV2(&ownerV2)
|
|
|
|
|
2021-10-25 12:10:13 +00:00
|
|
|
req := new(sessionV2.CreateRequestBody)
|
2022-05-17 13:59:46 +00:00
|
|
|
req.SetOwnerID(&ownerV2)
|
2021-10-25 12:10:13 +00:00
|
|
|
req.SetExpiration(exp)
|
|
|
|
|
|
|
|
resp, err := store.Create(context.Background(), req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
pub, err := keys.NewPublicKeyFromBytes(resp.GetSessionKey(), elliptic.P256())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var id uuid.UUID
|
|
|
|
require.NoError(t, id.UnmarshalBinary(resp.GetID()))
|
|
|
|
|
|
|
|
var tok session.Object
|
2022-12-23 17:35:35 +00:00
|
|
|
tok.SetAuthKey((*frostfsecdsa.PublicKey)(pub))
|
2022-05-18 15:20:08 +00:00
|
|
|
tok.SetID(id)
|
2021-10-25 12:10:13 +00:00
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|
2021-10-25 12:19:50 +00:00
|
|
|
|
|
|
|
type mockedNetworkState struct {
|
|
|
|
value uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockedNetworkState) CurrentEpoch() uint64 {
|
|
|
|
return m.value
|
|
|
|
}
|