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"
|
2023-10-17 14:10:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{{
|
|
|
|
Op: CondSliceContains,
|
|
|
|
Object: ObjectRequest,
|
|
|
|
Key: propKey,
|
|
|
|
Value: groupID,
|
|
|
|
}},
|
|
|
|
}}}
|
|
|
|
|
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|