2021-05-25 19:59:21 +00:00
|
|
|
package accessbox
|
|
|
|
|
|
|
|
import (
|
2024-04-24 15:20:51 +00:00
|
|
|
"encoding/hex"
|
2021-05-25 19:59:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2021-06-18 15:15:58 +00:00
|
|
|
"github.com/google/uuid"
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-05-25 19:59:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-01 14:00:30 +00:00
|
|
|
func assertBearerToken(t *testing.T, exp, act bearer.Token) {
|
|
|
|
// compare binary representations since deep equal is not guaranteed
|
|
|
|
require.Equal(t, exp.Marshal(), act.Marshal())
|
|
|
|
}
|
|
|
|
|
2022-10-25 09:36:31 +00:00
|
|
|
func TestTokensEncryptDecrypt(t *testing.T) {
|
2021-06-14 13:39:25 +00:00
|
|
|
var (
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn bearer.Token
|
|
|
|
tkn2 bearer.Token
|
2021-06-14 13:39:25 +00:00
|
|
|
)
|
2021-06-24 15:21:34 +00:00
|
|
|
sec, err := keys.NewPrivateKey()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
cred, err := keys.NewPrivateKey()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn.SetEACLTable(*eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.Sign(sec.PrivateKey))
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
data, err := encrypt(cred, cred.PublicKey(), tkn.Marshal())
|
2021-06-17 16:45:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
rawTkn2, err := decrypt(cred, cred.PublicKey(), data)
|
2021-06-17 16:45:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = tkn2.Unmarshal(rawTkn2)
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-01 14:00:30 +00:00
|
|
|
assertBearerToken(t, tkn, tkn2)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 09:36:31 +00:00
|
|
|
func TestBearerTokenInAccessBox(t *testing.T) {
|
2021-06-14 13:39:25 +00:00
|
|
|
var (
|
2021-06-16 14:07:31 +00:00
|
|
|
box *AccessBox
|
|
|
|
box2 AccessBox
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn bearer.Token
|
2021-06-14 13:39:25 +00:00
|
|
|
)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
sec, err := keys.NewPrivateKey()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
cred, err := keys.NewPrivateKey()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn.SetEACLTable(*eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.Sign(sec.PrivateKey))
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
gate := NewGateData(cred.PublicKey(), &tkn)
|
2023-06-13 09:35:40 +00:00
|
|
|
box, _, err = PackTokens([]*GateData{gate}, nil)
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
data, err := box.Marshal()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
err = box2.Unmarshal(data)
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
tkns, err := box2.GetTokens(cred)
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-01 14:00:30 +00:00
|
|
|
assertBearerToken(t, tkn, *tkns.BearerToken)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 09:36:31 +00:00
|
|
|
func TestSessionTokenInAccessBox(t *testing.T) {
|
2021-06-18 15:15:58 +00:00
|
|
|
var (
|
|
|
|
box *AccessBox
|
|
|
|
box2 AccessBox
|
2022-05-04 12:29:11 +00:00
|
|
|
tkn = new(session.Container)
|
2021-06-18 15:15:58 +00:00
|
|
|
)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
sec, err := keys.NewPrivateKey()
|
2021-06-18 15:15:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
cred, err := keys.NewPrivateKey()
|
2021-06-18 15:15:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-04 12:29:11 +00:00
|
|
|
tkn.SetID(uuid.New())
|
2022-12-20 08:38:58 +00:00
|
|
|
tkn.SetAuthKey((*frostfsecdsa.PublicKey)(sec.PublicKey()))
|
2022-05-04 12:29:11 +00:00
|
|
|
require.NoError(t, tkn.Sign(sec.PrivateKey))
|
2021-06-18 15:15:58 +00:00
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var newTkn bearer.Token
|
|
|
|
gate := NewGateData(cred.PublicKey(), &newTkn)
|
2022-05-04 12:29:11 +00:00
|
|
|
gate.SessionTokens = []*session.Container{tkn}
|
2023-06-13 09:35:40 +00:00
|
|
|
box, _, err = PackTokens([]*GateData{gate}, nil)
|
2021-06-18 15:15:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
data, err := box.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = box2.Unmarshal(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tkns, err := box2.GetTokens(cred)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-04 12:29:11 +00:00
|
|
|
require.Equal(t, []*session.Container{tkn}, tkns.SessionTokens)
|
2021-06-18 15:15:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 09:36:31 +00:00
|
|
|
func TestAccessboxMultipleKeys(t *testing.T) {
|
2021-06-14 13:39:25 +00:00
|
|
|
var (
|
2021-06-16 14:07:31 +00:00
|
|
|
box *AccessBox
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn bearer.Token
|
2021-06-14 13:39:25 +00:00
|
|
|
)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
sec, err := keys.NewPrivateKey()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn.SetEACLTable(*eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.Sign(sec.PrivateKey))
|
2021-05-25 19:59:21 +00:00
|
|
|
|
|
|
|
count := 10
|
2021-06-17 16:45:50 +00:00
|
|
|
gates := make([]*GateData, 0, count)
|
2021-06-24 15:21:34 +00:00
|
|
|
privateKeys := make([]*keys.PrivateKey, 0, count)
|
2021-05-25 19:59:21 +00:00
|
|
|
{ // generate keys
|
|
|
|
for i := 0; i < count; i++ {
|
2021-06-24 15:21:34 +00:00
|
|
|
cred, err := keys.NewPrivateKey()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
gates = append(gates, NewGateData(cred.PublicKey(), &tkn))
|
2021-06-24 15:21:34 +00:00
|
|
|
privateKeys = append(privateKeys, cred)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:35:40 +00:00
|
|
|
box, _, err = PackTokens(gates, nil)
|
2021-06-14 13:39:25 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
for i, k := range privateKeys {
|
2021-06-17 16:45:50 +00:00
|
|
|
tkns, err := box.GetTokens(k)
|
2021-06-14 13:39:25 +00:00
|
|
|
require.NoError(t, err, "key #%d: %s failed", i, k)
|
2022-06-01 14:00:30 +00:00
|
|
|
assertBearerToken(t, tkn, *tkns.BearerToken)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 09:36:31 +00:00
|
|
|
func TestUnknownKey(t *testing.T) {
|
2021-06-14 13:39:25 +00:00
|
|
|
var (
|
2021-06-16 14:07:31 +00:00
|
|
|
box *AccessBox
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn bearer.Token
|
2021-06-14 13:39:25 +00:00
|
|
|
)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
sec, err := keys.NewPrivateKey()
|
2021-06-14 13:39:25 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
cred, err := keys.NewPrivateKey()
|
2021-06-14 13:39:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
wrongCred, err := keys.NewPrivateKey()
|
2021-06-14 13:39:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
tkn.SetEACLTable(*eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.Sign(sec.PrivateKey))
|
2021-06-14 13:39:25 +00:00
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
gate := NewGateData(cred.PublicKey(), &tkn)
|
2023-06-13 09:35:40 +00:00
|
|
|
box, _, err = PackTokens([]*GateData{gate}, nil)
|
2021-06-14 13:39:25 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
_, err = box.GetTokens(wrongCred)
|
2021-06-14 13:39:25 +00:00
|
|
|
require.Error(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2024-04-24 15:20:51 +00:00
|
|
|
|
|
|
|
func TestGateDataSessionToken(t *testing.T) {
|
|
|
|
cred, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var tkn bearer.Token
|
|
|
|
gate := NewGateData(cred.PublicKey(), &tkn)
|
|
|
|
require.Equal(t, cred.PublicKey(), gate.GateKey)
|
|
|
|
assertBearerToken(t, tkn, *gate.BearerToken)
|
|
|
|
|
|
|
|
t.Run("session token for put", func(t *testing.T) {
|
|
|
|
gate.SessionTokens = []*session.Container{}
|
|
|
|
sessionTkn := gate.SessionTokenForPut()
|
|
|
|
require.Nil(t, sessionTkn)
|
|
|
|
|
|
|
|
sessionTknPut := new(session.Container)
|
|
|
|
sessionTknPut.ForVerb(session.VerbContainerPut)
|
|
|
|
gate.SessionTokens = []*session.Container{sessionTknPut}
|
|
|
|
sessionTkn = gate.SessionTokenForPut()
|
|
|
|
require.Equal(t, sessionTknPut, sessionTkn)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("session token for delete", func(t *testing.T) {
|
|
|
|
gate.SessionTokens = []*session.Container{}
|
|
|
|
sessionTkn := gate.SessionTokenForDelete()
|
|
|
|
require.Nil(t, sessionTkn)
|
|
|
|
|
|
|
|
sessionTknDelete := new(session.Container)
|
|
|
|
sessionTknDelete.ForVerb(session.VerbContainerDelete)
|
|
|
|
gate.SessionTokens = []*session.Container{sessionTknDelete}
|
|
|
|
sessionTkn = gate.SessionTokenForDelete()
|
|
|
|
require.Equal(t, sessionTknDelete, sessionTkn)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("session token", func(t *testing.T) {
|
|
|
|
gate.SessionTokens = []*session.Container{}
|
|
|
|
sessionTkn := gate.SessionToken()
|
|
|
|
require.Nil(t, sessionTkn)
|
|
|
|
|
|
|
|
sessionTknPut := new(session.Container)
|
|
|
|
sessionTknPut.ForVerb(session.VerbContainerPut)
|
|
|
|
gate.SessionTokens = []*session.Container{sessionTknPut}
|
|
|
|
sessionTkn = gate.SessionToken()
|
|
|
|
require.Equal(t, sessionTkn, sessionTknPut)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBox(t *testing.T) {
|
|
|
|
cred, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var tkn bearer.Token
|
|
|
|
gate := NewGateData(cred.PublicKey(), &tkn)
|
|
|
|
|
|
|
|
secret := []byte("secret")
|
|
|
|
accessBox, _, err := PackTokens([]*GateData{gate}, secret)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
box, err := accessBox.GetBox(cred)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, hex.EncodeToString(secret), box.Gate.SecretKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessBox(t *testing.T) {
|
|
|
|
cred, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var tkn bearer.Token
|
|
|
|
gate := NewGateData(cred.PublicKey(), &tkn)
|
|
|
|
|
|
|
|
accessBox, _, err := PackTokens([]*GateData{gate}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("invalid owner", func(t *testing.T) {
|
|
|
|
randomKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = accessBox.GetTokens(randomKey)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
_, err = accessBox.GetBox(randomKey)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty placement policy", func(t *testing.T) {
|
|
|
|
policy, err := accessBox.GetPlacementPolicy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, policy)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get correct placement policy", func(t *testing.T) {
|
|
|
|
policy := &AccessBox_ContainerPolicy{LocationConstraint: "locationConstraint"}
|
|
|
|
accessBox.ContainerPolicy = []*AccessBox_ContainerPolicy{policy}
|
|
|
|
|
|
|
|
policies, err := accessBox.GetPlacementPolicy()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, policies, 1)
|
|
|
|
require.Equal(t, policy.LocationConstraint, policies[0].LocationConstraint)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get incorrect placement policy", func(t *testing.T) {
|
|
|
|
policy := &AccessBox_ContainerPolicy{
|
|
|
|
LocationConstraint: "locationConstraint",
|
|
|
|
Policy: []byte("policy"),
|
|
|
|
}
|
|
|
|
accessBox.ContainerPolicy = []*AccessBox_ContainerPolicy{policy}
|
|
|
|
|
|
|
|
_, err = accessBox.GetPlacementPolicy()
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
_, err = accessBox.GetBox(cred)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty seed key", func(t *testing.T) {
|
|
|
|
accessBox.SeedKey = nil
|
|
|
|
|
|
|
|
_, err = accessBox.GetTokens(cred)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
_, err = accessBox.GetBox(cred)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid gate key", func(t *testing.T) {
|
|
|
|
gate = &GateData{
|
|
|
|
BearerToken: &tkn,
|
|
|
|
GateKey: &keys.PublicKey{},
|
|
|
|
}
|
|
|
|
_, _, err = PackTokens([]*GateData{gate}, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|