2022-02-11 12:25:05 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2022-06-08 08:53:15 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2022-02-11 12:25:05 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
bearertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer/test"
|
|
|
|
aclsdk "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
|
|
|
sessionSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
sessiontest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session/test"
|
2022-02-11 12:25:05 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOriginalTokens(t *testing.T) {
|
2022-05-18 15:20:08 +00:00
|
|
|
sToken := sessiontest.ObjectSigned()
|
2022-06-08 08:53:15 +00:00
|
|
|
bToken := bearertest.Token()
|
2022-05-12 07:22:02 +00:00
|
|
|
|
2022-06-08 08:53:15 +00:00
|
|
|
pk, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.NoError(t, bToken.Sign(*pk))
|
|
|
|
|
|
|
|
var bTokenV2 acl.BearerToken
|
|
|
|
bToken.WriteToV2(&bTokenV2)
|
|
|
|
// This line is needed because SDK uses some custom format for
|
|
|
|
// reserved filters, so `cid.ID` is not converted to string immediately.
|
|
|
|
require.NoError(t, bToken.ReadFromV2(bTokenV2))
|
2022-02-11 12:25:05 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var sTokenV2 session.Token
|
|
|
|
sToken.WriteToV2(&sTokenV2)
|
|
|
|
|
2024-08-30 16:20:55 +00:00
|
|
|
for i := range 10 {
|
2022-06-08 08:53:15 +00:00
|
|
|
metaHeaders := testGenerateMetaHeader(uint32(i), &bTokenV2, &sTokenV2)
|
2022-05-18 15:20:08 +00:00
|
|
|
res, err := originalSessionToken(metaHeaders)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sToken, res, i)
|
2022-06-08 08:53:15 +00:00
|
|
|
|
|
|
|
bTok, err := originalBearerToken(metaHeaders)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &bToken, bTok, i)
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 08:49:19 +00:00
|
|
|
func testGenerateMetaHeader(depth uint32, b *acl.BearerToken, s *session.Token) *session.RequestMetaHeader {
|
2022-02-11 12:25:05 +00:00
|
|
|
metaHeader := new(session.RequestMetaHeader)
|
|
|
|
metaHeader.SetBearerToken(b)
|
|
|
|
metaHeader.SetSessionToken(s)
|
|
|
|
|
2024-10-30 11:42:09 +00:00
|
|
|
for range depth {
|
2022-02-11 12:25:05 +00:00
|
|
|
link := metaHeader
|
|
|
|
metaHeader = new(session.RequestMetaHeader)
|
|
|
|
metaHeader.SetOrigin(link)
|
|
|
|
}
|
|
|
|
|
|
|
|
return metaHeader
|
|
|
|
}
|
2022-02-28 12:35:10 +00:00
|
|
|
|
|
|
|
func TestIsVerbCompatible(t *testing.T) {
|
2022-12-23 17:35:35 +00:00
|
|
|
// Source: https://nspcc.ru/upload/frostfs-spec-latest.pdf#page=28
|
2022-06-17 13:40:51 +00:00
|
|
|
table := map[aclsdk.Op][]sessionSDK.ObjectVerb{
|
|
|
|
aclsdk.OpObjectPut: {sessionSDK.VerbObjectPut, sessionSDK.VerbObjectDelete},
|
|
|
|
aclsdk.OpObjectDelete: {sessionSDK.VerbObjectDelete},
|
|
|
|
aclsdk.OpObjectGet: {sessionSDK.VerbObjectGet},
|
|
|
|
aclsdk.OpObjectHead: {
|
2022-05-18 15:20:08 +00:00
|
|
|
sessionSDK.VerbObjectHead,
|
|
|
|
sessionSDK.VerbObjectGet,
|
|
|
|
sessionSDK.VerbObjectDelete,
|
|
|
|
sessionSDK.VerbObjectRange,
|
|
|
|
sessionSDK.VerbObjectRangeHash,
|
|
|
|
},
|
2022-06-17 13:40:51 +00:00
|
|
|
aclsdk.OpObjectRange: {sessionSDK.VerbObjectRange, sessionSDK.VerbObjectRangeHash},
|
|
|
|
aclsdk.OpObjectHash: {sessionSDK.VerbObjectRangeHash},
|
|
|
|
aclsdk.OpObjectSearch: {sessionSDK.VerbObjectSearch, sessionSDK.VerbObjectDelete},
|
2022-02-28 12:35:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
verbs := []sessionSDK.ObjectVerb{
|
|
|
|
sessionSDK.VerbObjectPut,
|
|
|
|
sessionSDK.VerbObjectDelete,
|
|
|
|
sessionSDK.VerbObjectHead,
|
|
|
|
sessionSDK.VerbObjectRange,
|
|
|
|
sessionSDK.VerbObjectRangeHash,
|
|
|
|
sessionSDK.VerbObjectGet,
|
|
|
|
sessionSDK.VerbObjectSearch,
|
2022-02-28 12:35:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var tok sessionSDK.Object
|
|
|
|
|
|
|
|
for op, list := range table {
|
|
|
|
for _, verb := range verbs {
|
2022-02-28 12:35:10 +00:00
|
|
|
var contains bool
|
2022-05-18 15:20:08 +00:00
|
|
|
for _, v := range list {
|
|
|
|
if v == verb {
|
2022-02-28 12:35:10 +00:00
|
|
|
contains = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
tok.ForVerb(verb)
|
|
|
|
|
|
|
|
require.Equal(t, contains, assertVerb(tok, op),
|
|
|
|
"%v in token, %s executing", verb, op)
|
2022-02-28 12:35:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 10:22:09 +00:00
|
|
|
|
|
|
|
func TestAssertSessionRelation(t *testing.T) {
|
|
|
|
var tok sessionSDK.Object
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
cnrOther := cidtest.ID()
|
|
|
|
obj := oidtest.ID()
|
|
|
|
objOther := oidtest.ID()
|
|
|
|
|
|
|
|
// make sure ids differ, otherwise test won't work correctly
|
|
|
|
require.False(t, cnrOther.Equals(cnr))
|
|
|
|
require.False(t, objOther.Equals(obj))
|
|
|
|
|
|
|
|
// bind session to the container (required)
|
|
|
|
tok.BindContainer(cnr)
|
|
|
|
|
|
|
|
// test container-global session
|
|
|
|
require.NoError(t, assertSessionRelation(tok, cnr, nil))
|
|
|
|
require.NoError(t, assertSessionRelation(tok, cnr, &obj))
|
|
|
|
require.Error(t, assertSessionRelation(tok, cnrOther, nil))
|
|
|
|
require.Error(t, assertSessionRelation(tok, cnrOther, &obj))
|
|
|
|
|
|
|
|
// limit the session to the particular object
|
|
|
|
tok.LimitByObjects(obj)
|
|
|
|
|
|
|
|
// test fixed object session (here obj arg must be non-nil everywhere)
|
|
|
|
require.NoError(t, assertSessionRelation(tok, cnr, &obj))
|
|
|
|
require.Error(t, assertSessionRelation(tok, cnr, &objOther))
|
|
|
|
}
|