2023-12-27 14:18:15 +00:00
|
|
|
package ape
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-29 08:11:45 +00:00
|
|
|
"crypto/ecdsa"
|
2023-12-27 14:18:15 +00:00
|
|
|
"encoding/hex"
|
2024-05-08 07:02:54 +00:00
|
|
|
"errors"
|
2023-12-27 14:18:15 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2024-05-08 07:02:54 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
2024-07-12 09:02:20 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
2024-04-15 13:51:19 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
2024-05-08 07:02:54 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
2024-05-02 17:26:06 +00:00
|
|
|
frostfsidcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/frostfsid"
|
2024-05-29 08:11:45 +00:00
|
|
|
apeSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2023-12-27 14:18:15 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum"
|
2024-05-08 07:02:54 +00:00
|
|
|
containerSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
2023-12-27 14:18:15 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2024-05-08 07:02:54 +00:00
|
|
|
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2023-12-27 14:18:15 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2024-05-08 07:02:54 +00:00
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2023-12-27 14:18:15 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
policyengine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine/inmemory"
|
2024-06-20 12:39:39 +00:00
|
|
|
commonschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/common"
|
2023-12-27 14:18:15 +00:00
|
|
|
nativeschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
2024-04-11 10:51:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2024-04-15 13:51:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-12-27 14:18:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type headerProviderMock struct {
|
|
|
|
m map[oid.Address]*objectSDK.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ HeaderProvider = (*headerProviderMock)(nil)
|
|
|
|
|
|
|
|
func (h *headerProviderMock) addHeader(c cid.ID, o oid.ID, header *objectSDK.Object) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(c)
|
|
|
|
addr.SetObject(o)
|
|
|
|
h.m[addr] = header
|
|
|
|
}
|
|
|
|
|
2024-05-08 07:02:54 +00:00
|
|
|
func (h *headerProviderMock) GetHeader(_ context.Context, c cid.ID, o oid.ID, _ bool) (*objectSDK.Object, error) {
|
2023-12-27 14:18:15 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(c)
|
|
|
|
addr.SetObject(o)
|
|
|
|
obj, ok := h.m[addr]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("address not found")
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHeaderProviderMock() *headerProviderMock {
|
|
|
|
return &headerProviderMock{
|
|
|
|
m: make(map[oid.Address]*objectSDK.Object),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newContainerIDSDK(t *testing.T, encodedCID string) cid.ID {
|
|
|
|
var cnr cid.ID
|
|
|
|
require.NoError(t, cnr.DecodeString(encodedCID))
|
|
|
|
return cnr
|
|
|
|
}
|
|
|
|
|
|
|
|
func newObjectIDSDK(t *testing.T, encodedOID *string) *oid.ID {
|
|
|
|
if encodedOID == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
obj := new(oid.ID)
|
|
|
|
require.NoError(t, obj.DecodeString(*encodedOID))
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerObjectSDKParams struct {
|
|
|
|
majorVersion, minorVersion uint32
|
|
|
|
owner user.ID
|
|
|
|
epoch uint64
|
|
|
|
payloadSize uint64
|
|
|
|
typ objectSDK.Type
|
|
|
|
payloadChecksum checksum.Checksum
|
|
|
|
payloadHomomorphicHash checksum.Checksum
|
|
|
|
attributes []struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2024-03-01 08:58:17 +00:00
|
|
|
func newHeaderObjectSDK(cnr cid.ID, oid *oid.ID, headerObjSDK *headerObjectSDKParams) *objectSDK.Object {
|
2023-12-27 14:18:15 +00:00
|
|
|
objSDK := objectSDK.New()
|
|
|
|
objSDK.SetContainerID(cnr)
|
|
|
|
if oid != nil {
|
|
|
|
objSDK.SetID(*oid)
|
|
|
|
}
|
|
|
|
if headerObjSDK == nil {
|
|
|
|
return objSDK
|
|
|
|
}
|
|
|
|
ver := new(version.Version)
|
|
|
|
ver.SetMajor(headerObjSDK.majorVersion)
|
|
|
|
ver.SetMinor(headerObjSDK.minorVersion)
|
|
|
|
objSDK.SetVersion(ver)
|
|
|
|
objSDK.SetCreationEpoch(headerObjSDK.epoch)
|
|
|
|
objSDK.SetOwnerID(headerObjSDK.owner)
|
|
|
|
objSDK.SetPayloadSize(headerObjSDK.payloadSize)
|
|
|
|
objSDK.SetType(headerObjSDK.typ)
|
|
|
|
objSDK.SetPayloadChecksum(headerObjSDK.payloadChecksum)
|
|
|
|
objSDK.SetPayloadHomomorphicHash(headerObjSDK.payloadHomomorphicHash)
|
|
|
|
|
|
|
|
var attrs []objectSDK.Attribute
|
|
|
|
for _, attr := range headerObjSDK.attributes {
|
|
|
|
attrSDK := objectSDK.NewAttribute()
|
|
|
|
attrSDK.SetKey(attr.key)
|
|
|
|
attrSDK.SetValue(attr.val)
|
|
|
|
attrs = append(attrs, *attrSDK)
|
|
|
|
}
|
|
|
|
objSDK.SetAttributes(attrs...)
|
|
|
|
|
|
|
|
return objSDK
|
|
|
|
}
|
|
|
|
|
|
|
|
type testHeader struct {
|
|
|
|
headerObjSDK *headerObjectSDKParams
|
|
|
|
|
|
|
|
// If fromHeaderProvider is set, then running test should
|
|
|
|
// consider that a header is recieved from a header provider.
|
|
|
|
fromHeaderProvider bool
|
|
|
|
|
|
|
|
// If fromHeaderProvider is set, then running test should
|
|
|
|
// consider that a header is recieved from a message header.
|
|
|
|
fromRequestResponseHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
methodsRequiredOID = []string{
|
|
|
|
nativeschema.MethodGetObject,
|
|
|
|
nativeschema.MethodHeadObject,
|
|
|
|
nativeschema.MethodRangeObject,
|
|
|
|
nativeschema.MethodHashObject,
|
|
|
|
nativeschema.MethodDeleteObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
methodsOptionalOID = []string{
|
|
|
|
nativeschema.MethodSearchObject, nativeschema.MethodPutObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace = "test_namespace"
|
|
|
|
|
|
|
|
containerID = "73tQMTYyUkTgmvPR1HWib6pndbhSoBovbnMF7Pws8Rcy"
|
|
|
|
|
|
|
|
objectID = "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R"
|
|
|
|
|
2024-06-20 12:39:39 +00:00
|
|
|
groupID = "1"
|
|
|
|
|
2023-12-27 14:18:15 +00:00
|
|
|
role = "Container"
|
|
|
|
|
2024-04-11 10:51:39 +00:00
|
|
|
senderPrivateKey, _ = keys.NewPrivateKey()
|
|
|
|
|
|
|
|
senderKey = hex.EncodeToString(senderPrivateKey.PublicKey().Bytes())
|
2023-12-27 14:18:15 +00:00
|
|
|
)
|
|
|
|
|
2024-04-15 13:51:19 +00:00
|
|
|
type frostfsIDProviderMock struct {
|
2024-05-02 17:26:06 +00:00
|
|
|
subjects map[util.Uint160]*client.Subject
|
|
|
|
subjectsExtended map[util.Uint160]*client.SubjectExtended
|
2024-04-15 13:51:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 17:26:06 +00:00
|
|
|
var _ frostfsidcore.SubjectProvider = (*frostfsIDProviderMock)(nil)
|
2024-04-15 13:51:19 +00:00
|
|
|
|
|
|
|
func newFrostfsIDProviderMock(t *testing.T) *frostfsIDProviderMock {
|
|
|
|
return &frostfsIDProviderMock{
|
2024-05-02 17:26:06 +00:00
|
|
|
subjects: map[util.Uint160]*client.Subject{
|
2024-04-15 13:51:19 +00:00
|
|
|
scriptHashFromSenderKey(t, senderKey): {
|
|
|
|
Namespace: "testnamespace",
|
|
|
|
Name: "test",
|
|
|
|
KV: map[string]string{
|
|
|
|
"tag-attr1": "value1",
|
|
|
|
"tag-attr2": "value2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-02 17:26:06 +00:00
|
|
|
subjectsExtended: map[util.Uint160]*client.SubjectExtended{
|
|
|
|
scriptHashFromSenderKey(t, senderKey): {
|
|
|
|
Namespace: "testnamespace",
|
|
|
|
Name: "test",
|
|
|
|
KV: map[string]string{
|
|
|
|
"tag-attr1": "value1",
|
|
|
|
"tag-attr2": "value2",
|
|
|
|
},
|
|
|
|
Groups: []*client.Group{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
Name: "test",
|
|
|
|
Namespace: "testnamespace",
|
|
|
|
KV: map[string]string{
|
|
|
|
"attr1": "value1",
|
|
|
|
"attr2": "value2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-04-15 13:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func scriptHashFromSenderKey(t *testing.T, senderKey string) util.Uint160 {
|
|
|
|
pk, err := keys.NewPublicKeyFromString(senderKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return pk.GetScriptHash()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *frostfsIDProviderMock) GetSubject(key util.Uint160) (*client.Subject, error) {
|
2024-05-02 17:26:06 +00:00
|
|
|
v, ok := f.subjects[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s", frostfsidcore.SubjectNotFoundErrorMessage)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *frostfsIDProviderMock) GetSubjectExtended(key util.Uint160) (*client.SubjectExtended, error) {
|
|
|
|
v, ok := f.subjectsExtended[key]
|
2024-04-15 13:51:19 +00:00
|
|
|
if !ok {
|
2024-05-02 17:26:06 +00:00
|
|
|
return nil, fmt.Errorf("%s", frostfsidcore.SubjectNotFoundErrorMessage)
|
2024-04-15 13:51:19 +00:00
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 08:11:45 +00:00
|
|
|
var apeCheckTestCases = []struct {
|
|
|
|
name string
|
|
|
|
container string
|
|
|
|
object *string
|
|
|
|
methods []string
|
|
|
|
header testHeader
|
2024-07-12 09:02:20 +00:00
|
|
|
xHeaders []session.XHeader
|
2024-05-29 08:11:45 +00:00
|
|
|
containerRules []chain.Rule
|
2024-06-20 12:39:39 +00:00
|
|
|
groupidRules []chain.Rule
|
2024-05-29 08:11:45 +00:00
|
|
|
expectAPEErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "oid required requests are allowed",
|
|
|
|
container: containerID,
|
|
|
|
object: stringPtr(objectID),
|
|
|
|
methods: methodsRequiredOID,
|
|
|
|
containerRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.Allow,
|
|
|
|
Actions: chain.Actions{Names: methodsRequiredOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObject, containerID, objectID)},
|
2024-02-14 11:14:07 +00:00
|
|
|
},
|
|
|
|
},
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "oid optional requests are allowed",
|
|
|
|
container: containerID,
|
|
|
|
methods: methodsOptionalOID,
|
|
|
|
containerRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.Allow,
|
|
|
|
Actions: chain.Actions{Names: methodsOptionalOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObjects, containerID)},
|
2024-02-14 11:14:07 +00:00
|
|
|
},
|
|
|
|
},
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "oid required requests are denied",
|
|
|
|
container: containerID,
|
|
|
|
object: stringPtr(objectID),
|
|
|
|
methods: methodsRequiredOID,
|
|
|
|
containerRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.AccessDenied,
|
|
|
|
Actions: chain.Actions{Names: methodsRequiredOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObject, containerID, objectID)},
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
expectAPEErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "oid required requests are denied by an attribute",
|
|
|
|
container: containerID,
|
|
|
|
object: stringPtr(objectID),
|
|
|
|
methods: methodsRequiredOID,
|
|
|
|
header: testHeader{
|
|
|
|
headerObjSDK: &headerObjectSDKParams{
|
|
|
|
attributes: []struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "attr1",
|
|
|
|
val: "attribute_value",
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
fromHeaderProvider: true,
|
|
|
|
},
|
|
|
|
containerRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.AccessDenied,
|
|
|
|
Actions: chain.Actions{Names: methodsRequiredOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObject, containerID, objectID)},
|
|
|
|
},
|
|
|
|
Any: true,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringLike,
|
|
|
|
Kind: chain.KindResource,
|
|
|
|
Key: "attr1",
|
|
|
|
Value: "attribute*",
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
expectAPEErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "oid required requests are denied by sender",
|
|
|
|
container: containerID,
|
|
|
|
object: stringPtr(objectID),
|
|
|
|
methods: methodsRequiredOID,
|
|
|
|
header: testHeader{
|
|
|
|
headerObjSDK: &headerObjectSDKParams{
|
|
|
|
attributes: []struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "attr1",
|
|
|
|
val: "attribute_value",
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
fromHeaderProvider: true,
|
|
|
|
},
|
|
|
|
containerRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.AccessDenied,
|
|
|
|
Actions: chain.Actions{Names: methodsRequiredOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObject, containerID, objectID)},
|
|
|
|
},
|
|
|
|
Any: true,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringLike,
|
|
|
|
Kind: chain.KindRequest,
|
|
|
|
Key: nativeschema.PropertyKeyActorPublicKey,
|
|
|
|
Value: senderKey,
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
expectAPEErr: true,
|
|
|
|
},
|
2024-07-12 09:02:20 +00:00
|
|
|
{
|
|
|
|
name: "oid required requests are denied by xheader",
|
|
|
|
container: containerID,
|
|
|
|
object: stringPtr(objectID),
|
|
|
|
methods: methodsRequiredOID,
|
|
|
|
header: testHeader{
|
|
|
|
headerObjSDK: &headerObjectSDKParams{
|
|
|
|
attributes: []struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "attr1",
|
|
|
|
val: "attribute_value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fromHeaderProvider: true,
|
|
|
|
},
|
|
|
|
xHeaders: []session.XHeader{
|
|
|
|
func() (xhead session.XHeader) {
|
|
|
|
xhead.SetKey("X-Test-ID")
|
|
|
|
xhead.SetValue("aezakmi")
|
|
|
|
return
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
containerRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.AccessDenied,
|
|
|
|
Actions: chain.Actions{Names: methodsRequiredOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObject, containerID, objectID)},
|
|
|
|
},
|
|
|
|
Any: true,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringLike,
|
|
|
|
Kind: chain.KindRequest,
|
|
|
|
Key: fmt.Sprintf(commonschema.PropertyKeyFrostFSXHeader, "X-Test-ID"),
|
|
|
|
Value: "aezakmi",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectAPEErr: true,
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
{
|
|
|
|
name: "optional oid requests reached quota limit by an attribute",
|
|
|
|
container: containerID,
|
|
|
|
methods: methodsOptionalOID,
|
|
|
|
header: testHeader{
|
|
|
|
headerObjSDK: &headerObjectSDKParams{
|
|
|
|
payloadSize: 1000,
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
fromRequestResponseHeader: true,
|
|
|
|
},
|
|
|
|
containerRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.QuotaLimitReached,
|
|
|
|
Actions: chain.Actions{Names: methodsOptionalOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObjects, containerID)},
|
|
|
|
},
|
|
|
|
Any: true,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Kind: chain.KindResource,
|
|
|
|
Key: nativeschema.PropertyKeyObjectPayloadLength,
|
|
|
|
Value: "1000",
|
2023-12-27 14:18:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
expectAPEErr: true,
|
|
|
|
},
|
2024-06-20 12:39:39 +00:00
|
|
|
{
|
|
|
|
name: "optional oid requests reached quota limit by group-id",
|
|
|
|
container: containerID,
|
|
|
|
methods: methodsOptionalOID,
|
|
|
|
header: testHeader{
|
|
|
|
headerObjSDK: &headerObjectSDKParams{
|
|
|
|
payloadSize: 1000,
|
|
|
|
},
|
|
|
|
fromRequestResponseHeader: true,
|
|
|
|
},
|
|
|
|
groupidRules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.QuotaLimitReached,
|
|
|
|
Actions: chain.Actions{Names: methodsOptionalOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObjects, containerID)},
|
|
|
|
},
|
|
|
|
Any: true,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Kind: chain.KindRequest,
|
|
|
|
Key: commonschema.PropertyKeyFrostFSIDGroupID,
|
|
|
|
Value: groupID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectAPEErr: true,
|
|
|
|
},
|
2024-05-29 08:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type stMock struct{}
|
|
|
|
|
|
|
|
func (m *stMock) CurrentEpoch() uint64 {
|
|
|
|
return 8
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPECheck_BearerTokenOverrides(t *testing.T) {
|
|
|
|
for _, test := range apeCheckTestCases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
chain := chain.Chain{
|
|
|
|
Rules: test.containerRules,
|
|
|
|
MatchType: chain.MatchTypeFirstMatch,
|
|
|
|
}
|
|
|
|
chainSDK := apeSDK.Chain{
|
|
|
|
Raw: chain.Bytes(),
|
|
|
|
}
|
|
|
|
bt := new(bearer.Token)
|
|
|
|
bt.SetIat(1)
|
|
|
|
bt.SetExp(10)
|
|
|
|
bt.SetAPEOverride(bearer.APEOverride{
|
|
|
|
Target: apeSDK.ChainTarget{
|
|
|
|
TargetType: apeSDK.TargetTypeContainer,
|
|
|
|
Name: test.container,
|
|
|
|
},
|
|
|
|
Chains: []apeSDK.Chain{chainSDK},
|
|
|
|
})
|
|
|
|
bt.Sign(senderPrivateKey.PrivateKey)
|
|
|
|
var cnrOwner user.ID
|
|
|
|
user.IDFromKey(&cnrOwner, (ecdsa.PublicKey)(*senderPrivateKey.PublicKey()))
|
|
|
|
|
|
|
|
for _, method := range test.methods {
|
|
|
|
t.Run(method, func(t *testing.T) {
|
|
|
|
headerProvider := newHeaderProviderMock()
|
|
|
|
frostfsidProvider := newFrostfsIDProviderMock(t)
|
|
|
|
|
|
|
|
cnr := newContainerIDSDK(t, test.container)
|
|
|
|
obj := newObjectIDSDK(t, test.object)
|
|
|
|
|
|
|
|
ls := inmemory.NewInmemoryLocalStorage()
|
|
|
|
ms := inmemory.NewInmemoryMorphRuleChainStorage()
|
|
|
|
|
2024-08-14 11:38:01 +00:00
|
|
|
checker := NewChecker(ls, ms, headerProvider, frostfsidProvider, nil, &stMock{}, nil, nil)
|
2024-05-29 08:11:45 +00:00
|
|
|
|
|
|
|
prm := Prm{
|
|
|
|
Method: method,
|
|
|
|
Container: cnr,
|
|
|
|
Object: obj,
|
|
|
|
Role: role,
|
|
|
|
ContainerOwner: cnrOwner,
|
|
|
|
SenderKey: senderKey,
|
|
|
|
BearerToken: bt,
|
|
|
|
}
|
|
|
|
|
|
|
|
var headerObjSDK *objectSDK.Object
|
|
|
|
if test.header.headerObjSDK != nil {
|
|
|
|
headerObjSDK = newHeaderObjectSDK(cnr, obj, test.header.headerObjSDK)
|
|
|
|
if test.header.fromHeaderProvider {
|
|
|
|
require.NotNil(t, obj, "oid is required if a header is expected to be found in header provider")
|
|
|
|
headerProvider.addHeader(cnr, *obj, headerObjSDK)
|
|
|
|
} else if test.header.fromRequestResponseHeader {
|
|
|
|
prm.Header = headerObjSDK.ToV2().GetHeader()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 11:38:01 +00:00
|
|
|
err := checker.CheckAPE(context.Background(), prm)
|
2024-05-29 08:11:45 +00:00
|
|
|
if test.expectAPEErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPECheck(t *testing.T) {
|
|
|
|
for _, test := range apeCheckTestCases {
|
2023-12-27 14:18:15 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
for _, method := range test.methods {
|
|
|
|
t.Run(method, func(t *testing.T) {
|
|
|
|
headerProvider := newHeaderProviderMock()
|
2024-04-15 13:51:19 +00:00
|
|
|
frostfsidProvider := newFrostfsIDProviderMock(t)
|
2023-12-27 14:18:15 +00:00
|
|
|
|
|
|
|
cnr := newContainerIDSDK(t, test.container)
|
|
|
|
obj := newObjectIDSDK(t, test.object)
|
|
|
|
|
|
|
|
ls := inmemory.NewInmemoryLocalStorage()
|
|
|
|
ms := inmemory.NewInmemoryMorphRuleChainStorage()
|
|
|
|
|
2024-06-20 12:39:39 +00:00
|
|
|
if len(test.containerRules) > 0 {
|
|
|
|
ls.AddOverride(chain.Ingress, policyengine.ContainerTarget(test.container), &chain.Chain{
|
|
|
|
Rules: test.containerRules,
|
|
|
|
MatchType: chain.MatchTypeFirstMatch,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(test.groupidRules) > 0 {
|
|
|
|
ls.AddOverride(chain.Ingress, policyengine.GroupTarget(":"+groupID), &chain.Chain{
|
|
|
|
Rules: test.groupidRules,
|
|
|
|
MatchType: chain.MatchTypeFirstMatch,
|
|
|
|
})
|
|
|
|
}
|
2023-12-27 14:18:15 +00:00
|
|
|
|
2024-07-01 15:42:15 +00:00
|
|
|
checker := NewChecker(ls, ms, headerProvider, frostfsidProvider, nil, &stMock{}, nil, nil)
|
2023-12-27 14:18:15 +00:00
|
|
|
|
|
|
|
prm := Prm{
|
|
|
|
Method: method,
|
|
|
|
Container: cnr,
|
|
|
|
Object: obj,
|
|
|
|
Role: role,
|
|
|
|
SenderKey: senderKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
var headerObjSDK *objectSDK.Object
|
|
|
|
if test.header.headerObjSDK != nil {
|
2024-03-01 08:58:17 +00:00
|
|
|
headerObjSDK = newHeaderObjectSDK(cnr, obj, test.header.headerObjSDK)
|
2023-12-27 14:18:15 +00:00
|
|
|
if test.header.fromHeaderProvider {
|
|
|
|
require.NotNil(t, obj, "oid is required if a header is expected to be found in header provider")
|
|
|
|
headerProvider.addHeader(cnr, *obj, headerObjSDK)
|
|
|
|
} else if test.header.fromRequestResponseHeader {
|
|
|
|
prm.Header = headerObjSDK.ToV2().GetHeader()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := checker.CheckAPE(context.Background(), prm)
|
|
|
|
if test.expectAPEErr {
|
2024-01-18 11:35:52 +00:00
|
|
|
require.Error(t, err)
|
2023-12-27 14:18:15 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-05-08 07:02:54 +00:00
|
|
|
|
|
|
|
type netmapStub struct {
|
|
|
|
netmaps map[uint64]*netmapSDK.NetMap
|
|
|
|
currentEpoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *netmapStub) GetNetMap(diff uint64) (*netmapSDK.NetMap, error) {
|
|
|
|
if diff >= s.currentEpoch {
|
|
|
|
return nil, errors.New("invalid diff")
|
|
|
|
}
|
|
|
|
return s.GetNetMapByEpoch(s.currentEpoch - diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *netmapStub) GetNetMapByEpoch(epoch uint64) (*netmapSDK.NetMap, error) {
|
|
|
|
if nm, found := s.netmaps[epoch]; found {
|
|
|
|
return nm, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("netmap not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *netmapStub) Epoch() (uint64, error) {
|
|
|
|
return s.currentEpoch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testContainerSource struct {
|
|
|
|
containers map[cid.ID]*container.Container
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testContainerSource) Get(cnrID cid.ID) (*container.Container, error) {
|
|
|
|
if cnr, found := s.containers[cnrID]; found {
|
|
|
|
return cnr, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("container not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testContainerSource) DeletionInfo(cid.ID) (*container.DelInfo, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutECChunk(t *testing.T) {
|
|
|
|
headerProvider := newHeaderProviderMock()
|
|
|
|
frostfsidProvider := newFrostfsIDProviderMock(t)
|
|
|
|
|
|
|
|
cnr := newContainerIDSDK(t, containerID)
|
|
|
|
obj := newObjectIDSDK(t, &objectID)
|
|
|
|
|
|
|
|
ls := inmemory.NewInmemoryLocalStorage()
|
|
|
|
ms := inmemory.NewInmemoryMorphRuleChainStorage()
|
|
|
|
|
|
|
|
ls.AddOverride(chain.Ingress, policyengine.ContainerTarget(containerID), &chain.Chain{
|
|
|
|
Rules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: chain.AccessDenied,
|
|
|
|
Actions: chain.Actions{Names: methodsOptionalOID},
|
|
|
|
Resources: chain.Resources{
|
|
|
|
Names: []string{fmt.Sprintf(nativeschema.ResourceFormatRootContainerObjects, containerID)},
|
|
|
|
},
|
|
|
|
Any: true,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
2024-05-14 09:23:26 +00:00
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Kind: chain.KindResource,
|
|
|
|
Key: "attr1",
|
|
|
|
Value: "value",
|
2024-05-08 07:02:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MatchType: chain.MatchTypeFirstMatch,
|
|
|
|
})
|
|
|
|
|
|
|
|
node1Key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
node1 := netmapSDK.NodeInfo{}
|
|
|
|
node1.SetPublicKey(node1Key.PublicKey().Bytes())
|
|
|
|
netmap := &netmapSDK.NetMap{}
|
|
|
|
netmap.SetEpoch(100)
|
|
|
|
netmap.SetNodes([]netmapSDK.NodeInfo{node1})
|
|
|
|
|
|
|
|
nm := &netmapStub{
|
|
|
|
currentEpoch: 100,
|
|
|
|
netmaps: map[uint64]*netmapSDK.NetMap{
|
2024-10-28 12:46:38 +00:00
|
|
|
99: netmap,
|
2024-05-08 07:02:54 +00:00
|
|
|
100: netmap,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cont := containerSDK.Container{}
|
|
|
|
cont.Init()
|
|
|
|
pp := netmapSDK.PlacementPolicy{}
|
|
|
|
require.NoError(t, pp.DecodeString("REP 1"))
|
|
|
|
cont.SetPlacementPolicy(pp)
|
|
|
|
cs := &testContainerSource{
|
|
|
|
containers: map[cid.ID]*container.Container{
|
|
|
|
cnr: {
|
|
|
|
Value: cont,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:42:15 +00:00
|
|
|
checker := NewChecker(ls, ms, headerProvider, frostfsidProvider, nm, &stMock{}, cs, node1Key.PublicKey().Bytes())
|
2024-05-08 07:02:54 +00:00
|
|
|
|
|
|
|
ecParentID := oidtest.ID()
|
|
|
|
chunkHeader := newHeaderObjectSDK(cnr, obj, nil).ToV2().GetHeader()
|
|
|
|
ecHeader := object.ECHeader{
|
|
|
|
Index: 1,
|
|
|
|
Total: 5,
|
|
|
|
Parent: &refs.ObjectID{},
|
|
|
|
}
|
|
|
|
chunkHeader.SetEC(&ecHeader)
|
|
|
|
ecParentID.WriteToV2(ecHeader.Parent)
|
|
|
|
|
|
|
|
parentHeader := newHeaderObjectSDK(cnr, &ecParentID, &headerObjectSDKParams{
|
|
|
|
attributes: []struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "attr1",
|
|
|
|
val: "value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
headerProvider.addHeader(cnr, ecParentID, parentHeader)
|
|
|
|
|
|
|
|
t.Run("access denied for container node", func(t *testing.T) {
|
|
|
|
prm := Prm{
|
|
|
|
Method: nativeschema.MethodPutObject,
|
|
|
|
Container: cnr,
|
|
|
|
Object: obj,
|
|
|
|
Role: role,
|
|
|
|
SenderKey: senderKey,
|
|
|
|
Header: chunkHeader,
|
|
|
|
SoftAPECheck: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checker.CheckAPE(context.Background(), prm)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("access allowed for non container node", func(t *testing.T) {
|
|
|
|
otherKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
2024-07-01 15:42:15 +00:00
|
|
|
checker = NewChecker(ls, ms, headerProvider, frostfsidProvider, nm, &stMock{}, cs, otherKey.PublicKey().Bytes())
|
2024-05-08 07:02:54 +00:00
|
|
|
prm := Prm{
|
|
|
|
Method: nativeschema.MethodPutObject,
|
|
|
|
Container: cnr,
|
|
|
|
Object: obj,
|
|
|
|
Role: nativeschema.PropertyValueContainerRoleOthers,
|
|
|
|
SenderKey: senderKey,
|
|
|
|
Header: chunkHeader,
|
|
|
|
SoftAPECheck: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checker.CheckAPE(context.Background(), prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|