2024-04-26 14:32:58 +00:00
|
|
|
package frostfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-11 08:32:36 +00:00
|
|
|
"strings"
|
2024-04-26 14:32:58 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/authmate"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/tokens"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-07-15 12:47:19 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2024-04-26 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetCredsObject(t *testing.T) {
|
|
|
|
ctx, bktName, payload, newPayload := context.Background(), "bucket", []byte("payload"), []byte("new-payload")
|
|
|
|
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var userID user.ID
|
|
|
|
userID.SetScriptHash(key.PublicKey().GetScriptHash())
|
|
|
|
|
|
|
|
var token bearer.Token
|
|
|
|
err = token.Sign(key.PrivateKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx = middleware.SetBox(ctx, &middleware.Box{AccessBox: &accessbox.Box{
|
|
|
|
Gate: &accessbox.GateData{
|
|
|
|
BearerToken: &token,
|
|
|
|
},
|
|
|
|
}})
|
|
|
|
|
2024-07-15 12:47:19 +00:00
|
|
|
frostfs := NewAuthmateFrostFS(layer.NewTestFrostFS(key), zaptest.NewLogger(t))
|
2024-04-26 14:32:58 +00:00
|
|
|
|
2024-10-11 08:32:36 +00:00
|
|
|
cnrID, err := frostfs.CreateContainer(ctx, authmate.PrmContainerCreate{
|
2024-04-26 14:32:58 +00:00
|
|
|
FriendlyName: bktName,
|
|
|
|
Owner: userID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
objID, err := frostfs.CreateObject(ctx, tokens.PrmObjectCreate{
|
2024-10-11 08:32:36 +00:00
|
|
|
Container: cnrID,
|
2024-04-26 14:32:58 +00:00
|
|
|
Payload: payload,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var addr oid.Address
|
2024-10-11 08:32:36 +00:00
|
|
|
addr.SetContainer(cnrID)
|
2024-04-26 14:32:58 +00:00
|
|
|
addr.SetObject(objID)
|
|
|
|
|
2024-10-11 08:32:36 +00:00
|
|
|
accessKeyID := getAccessKeyID(addr)
|
|
|
|
|
|
|
|
obj, err := frostfs.GetCredsObject(ctx, tokens.PrmGetCredsObject{
|
|
|
|
Container: cnrID,
|
|
|
|
AccessKeyID: accessKeyID,
|
|
|
|
})
|
2024-04-26 14:32:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, payload, obj.Payload())
|
|
|
|
|
|
|
|
_, err = frostfs.CreateObject(ctx, tokens.PrmObjectCreate{
|
2024-10-11 08:32:36 +00:00
|
|
|
Container: cnrID,
|
|
|
|
Payload: newPayload,
|
|
|
|
NewVersionForAccessKeyID: accessKeyID,
|
2024-04-26 14:32:58 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-10-11 08:32:36 +00:00
|
|
|
obj, err = frostfs.GetCredsObject(ctx, tokens.PrmGetCredsObject{
|
|
|
|
Container: cnrID,
|
|
|
|
AccessKeyID: getAccessKeyID(addr),
|
|
|
|
})
|
2024-04-26 14:32:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newPayload, obj.Payload())
|
|
|
|
}
|
2024-10-11 08:32:36 +00:00
|
|
|
|
|
|
|
func getAccessKeyID(addr oid.Address) string {
|
|
|
|
return strings.ReplaceAll(addr.EncodeToString(), "/", "0")
|
|
|
|
}
|