2023-11-07 17:20:54 +00:00
|
|
|
package chain
|
2023-10-17 14:10:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-12-08 11:07:39 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/resource/testutil"
|
2023-12-20 10:17:15 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/common"
|
2023-12-08 11:07:39 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
2024-04-01 14:27:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/s3"
|
2023-10-17 14:10:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-01-12 07:49:12 +00:00
|
|
|
func TestChainIDSerialization(t *testing.T) {
|
|
|
|
chainIDBytes := []byte{93, 236, 80, 138, 168, 3, 144, 92, 173, 141, 16, 42, 249, 90, 97, 109, 211, 169, 54, 163}
|
|
|
|
|
|
|
|
chain1 := &Chain{ID: ID(chainIDBytes)}
|
|
|
|
data := chain1.Bytes()
|
|
|
|
|
|
|
|
var chain2 Chain
|
|
|
|
err := chain2.DecodeBytes(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, chain1.ID, chain2.ID)
|
|
|
|
}
|
|
|
|
|
2023-10-17 14:10:48 +00:00
|
|
|
func TestEncodeDecode(t *testing.T) {
|
|
|
|
expected := Chain{
|
2023-12-08 11:07:39 +00:00
|
|
|
MatchType: MatchTypeFirstMatch,
|
2023-10-17 14:10:48 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Status: Allow,
|
2023-10-30 13:34:11 +00:00
|
|
|
Actions: Actions{Names: []string{
|
2023-10-17 14:10:48 +00:00
|
|
|
"native::PutObject",
|
2023-10-30 13:34:11 +00:00
|
|
|
}},
|
|
|
|
Resources: Resources{Names: []string{"*"}},
|
2023-10-17 14:10:48 +00:00
|
|
|
Condition: []Condition{
|
|
|
|
{
|
|
|
|
Op: CondStringEquals,
|
|
|
|
Key: "Name",
|
|
|
|
Value: "NNS",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
data := expected.Bytes()
|
|
|
|
|
|
|
|
var actual Chain
|
|
|
|
require.NoError(t, actual.DecodeBytes(data))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
2023-12-08 11:07:39 +00:00
|
|
|
|
|
|
|
func TestReturnFirstMatch(t *testing.T) {
|
|
|
|
ch := Chain{
|
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Status: Allow,
|
|
|
|
Actions: Actions{Names: []string{
|
|
|
|
native.MethodPutObject,
|
|
|
|
}},
|
|
|
|
Resources: Resources{Names: []string{native.ResourceFormatRootContainers}},
|
|
|
|
Condition: []Condition{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: AccessDenied,
|
|
|
|
Actions: Actions{Names: []string{
|
|
|
|
native.MethodPutObject,
|
|
|
|
}},
|
|
|
|
Resources: Resources{Names: []string{native.ResourceFormatRootContainers}},
|
|
|
|
Condition: []Condition{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resource := testutil.NewResource(native.ResourceFormatRootContainers, nil)
|
|
|
|
request := testutil.NewRequest(native.MethodPutObject, resource, nil)
|
|
|
|
|
|
|
|
t.Run("default match", func(t *testing.T) {
|
|
|
|
st, found := ch.Match(request)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, AccessDenied, st)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("return first match", func(t *testing.T) {
|
|
|
|
ch.MatchType = MatchTypeFirstMatch
|
|
|
|
st, found := ch.Match(request)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, Allow, st)
|
|
|
|
})
|
|
|
|
}
|
2023-12-20 10:17:15 +00:00
|
|
|
|
|
|
|
func TestCondSliceContainsMatch(t *testing.T) {
|
|
|
|
propKey := common.PropertyKeyFrostFSIDGroupID
|
|
|
|
groupID := "1"
|
|
|
|
|
|
|
|
ch := Chain{Rules: []Rule{{
|
|
|
|
Status: Allow,
|
|
|
|
Actions: Actions{Names: []string{native.MethodPutObject}},
|
|
|
|
Resources: Resources{Names: []string{native.ResourceFormatRootContainers}},
|
|
|
|
Condition: []Condition{{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondSliceContains,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: groupID,
|
2023-12-20 10:17:15 +00:00
|
|
|
}},
|
|
|
|
}}}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
status Status
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple value",
|
|
|
|
value: groupID,
|
|
|
|
status: Allow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple value by func",
|
|
|
|
value: FormCondSliceContainsValue([]string{groupID}),
|
|
|
|
status: Allow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple values by func",
|
|
|
|
value: FormCondSliceContainsValue([]string{groupID, "2", "3"}),
|
|
|
|
status: Allow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple mismatched",
|
|
|
|
value: "3",
|
|
|
|
status: NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple mismatched",
|
|
|
|
value: FormCondSliceContainsValue([]string{"11", "12"}),
|
|
|
|
status: NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "comma correct handling mismatched",
|
|
|
|
value: "1,11",
|
|
|
|
status: NoRuleFound,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
resource := testutil.NewResource(native.ResourceFormatRootContainers, nil)
|
|
|
|
request := testutil.NewRequest(native.MethodPutObject, resource, map[string]string{propKey: tc.value})
|
|
|
|
|
|
|
|
st, _ := ch.Match(request)
|
|
|
|
require.Equal(t, tc.status.String(), st.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-04-01 14:27:45 +00:00
|
|
|
|
|
|
|
func TestNumericConditionsMatch(t *testing.T) {
|
|
|
|
propKey := s3.PropertyKeyMaxKeys
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
conditions []Condition
|
|
|
|
value string
|
|
|
|
status Status
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "value from interval",
|
|
|
|
conditions: []Condition{
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondNumericLessThan,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: "100",
|
2024-04-01 14:27:45 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondNumericGreaterThan,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: "80",
|
2024-04-01 14:27:45 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondNumericNotEquals,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: "91",
|
2024-04-01 14:27:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
value: "90",
|
|
|
|
status: Allow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "border value",
|
|
|
|
conditions: []Condition{
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondNumericEquals,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: "50",
|
2024-04-01 14:27:45 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondNumericLessThanEquals,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: "50",
|
2024-04-01 14:27:45 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondNumericGreaterThanEquals,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: "50",
|
2024-04-01 14:27:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
value: "50",
|
|
|
|
status: Allow,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
resource := testutil.NewResource(native.ResourceFormatRootContainers, nil)
|
|
|
|
request := testutil.NewRequest(native.MethodPutObject, resource, map[string]string{propKey: tc.value})
|
|
|
|
|
|
|
|
ch := Chain{Rules: []Rule{{
|
|
|
|
Status: Allow,
|
|
|
|
Actions: Actions{Names: []string{native.MethodPutObject}},
|
|
|
|
Resources: Resources{Names: []string{native.ResourceFormatRootContainers}},
|
|
|
|
Condition: tc.conditions,
|
|
|
|
}}}
|
|
|
|
st, _ := ch.Match(request)
|
|
|
|
require.Equal(t, tc.status.String(), st.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidNumericValues(t *testing.T) {
|
|
|
|
propKey := s3.PropertyKeyMaxKeys
|
|
|
|
propValues := []string{"", "invalid"}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
conditionType ConditionType
|
|
|
|
match bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "NumericEquals condition",
|
|
|
|
conditionType: CondNumericEquals,
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NumericNotEquals condition",
|
|
|
|
conditionType: CondNumericNotEquals,
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NumericLessThan condition",
|
|
|
|
conditionType: CondNumericLessThan,
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NumericLessThanEquals condition",
|
|
|
|
conditionType: CondNumericLessThanEquals,
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NumericGreaterThan condition",
|
|
|
|
conditionType: CondNumericGreaterThan,
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NumericGreaterThanEquals condition",
|
|
|
|
conditionType: CondNumericGreaterThanEquals,
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
resource := testutil.NewResource(native.ResourceFormatRootContainers, nil)
|
|
|
|
condition := Condition{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: tc.conditionType,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: "50",
|
2024-04-01 14:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, propValue := range propValues {
|
|
|
|
request := testutil.NewRequest(native.MethodPutObject, resource, map[string]string{propKey: propValue})
|
|
|
|
|
|
|
|
match := condition.Match(request)
|
|
|
|
require.Equal(t, tc.match, match)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|