2024-02-06 13:44:49 +00:00
|
|
|
package tokens
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2024-04-16 08:20:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2024-02-06 13:44:49 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type frostfsMock struct {
|
|
|
|
objects map[oid.Address][]byte
|
|
|
|
errors map[oid.Address]error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *frostfsMock) CreateObject(context.Context, PrmObjectCreate) (oid.ID, error) {
|
|
|
|
panic("implement me for test")
|
|
|
|
}
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
func (f *frostfsMock) GetCredsObject(_ context.Context, address oid.Address) (*object.Object, error) {
|
2024-02-06 13:44:49 +00:00
|
|
|
if err := f.errors[address]; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, ok := f.objects[address]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
2024-04-16 08:20:35 +00:00
|
|
|
|
|
|
|
var obj object.Object
|
|
|
|
obj.SetPayload(data)
|
|
|
|
return &obj, nil
|
2024-02-06 13:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovingAccessBox(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gateData := []*accessbox.GateData{{
|
|
|
|
BearerToken: &bearer.Token{},
|
|
|
|
GateKey: key.PublicKey(),
|
|
|
|
}}
|
|
|
|
|
|
|
|
secretKey := "713d0a0b9efc7d22923e17b0402a6a89b4273bc711c8bacb2da1b643d0006aeb"
|
|
|
|
sk, err := hex.DecodeString(secretKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
accessBox, _, err := accessbox.PackTokens(gateData, sk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
data, err := accessBox.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
addr := oidtest.Address()
|
|
|
|
frostfs := &frostfsMock{
|
|
|
|
objects: map[oid.Address][]byte{addr: data},
|
|
|
|
errors: map[oid.Address]error{},
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := Config{
|
|
|
|
FrostFS: frostfs,
|
|
|
|
Key: key,
|
|
|
|
CacheConfig: &cache.Config{
|
|
|
|
Size: 10,
|
|
|
|
Lifetime: 24 * time.Hour,
|
|
|
|
Logger: zaptest.NewLogger(t),
|
|
|
|
},
|
|
|
|
RemovingCheckAfterDurations: 0, // means check always
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := New(cfg)
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
_, _, err = creds.GetBox(ctx, addr)
|
2024-02-06 13:44:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
frostfs.errors[addr] = errors.New("network error")
|
2024-04-16 08:20:35 +00:00
|
|
|
_, _, err = creds.GetBox(ctx, addr)
|
2024-02-06 13:44:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
frostfs.errors[addr] = &apistatus.ObjectAlreadyRemoved{}
|
2024-04-16 08:20:35 +00:00
|
|
|
_, _, err = creds.GetBox(ctx, addr)
|
2024-02-06 13:44:49 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|