2023-10-31 08:55:42 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2024-01-25 17:25:23 +00:00
|
|
|
"fmt"
|
2023-10-31 08:55:42 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
policyengine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
2023-11-10 08:40:06 +00:00
|
|
|
nativeschema "git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
2023-10-31 08:55:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseAPERule(t *testing.T) {
|
|
|
|
tests := [...]struct {
|
|
|
|
name string
|
|
|
|
rule string
|
|
|
|
expectErr error
|
|
|
|
expectRule policyengine.Rule
|
|
|
|
}{
|
|
|
|
{
|
2024-01-25 17:25:23 +00:00
|
|
|
name: "Valid allow rule for all objects",
|
2023-10-31 08:55:42 +00:00
|
|
|
rule: "allow Object.Put *",
|
2024-01-25 17:25:23 +00:00
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatAllObjects}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for all objects in root namespace",
|
|
|
|
rule: "allow Object.Put /*",
|
2023-10-31 08:55:42 +00:00
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
2023-11-10 08:40:06 +00:00
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatRootObjects}},
|
2024-01-25 17:25:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for all objects in root namespace and container",
|
|
|
|
rule: "allow Object.Put /cid/*",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatRootContainerObjects, "cid"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for object in root namespace and container",
|
|
|
|
rule: "allow Object.Put /cid/oid",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatRootContainerObject, "cid", "oid"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for all objects in namespace",
|
|
|
|
rule: "allow Object.Put ns/*",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatNamespaceObjects, "ns"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for all objects in namespace and container",
|
|
|
|
rule: "allow Object.Put ns/cid/*",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatNamespaceContainerObjects, "ns", "cid"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for object in namespace and container",
|
|
|
|
rule: "allow Object.Put ns/cid/oid",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatNamespaceContainerObject, "ns", "cid", "oid"),
|
|
|
|
}},
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid deny rule",
|
|
|
|
rule: "deny Object.Put *",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.AccessDenied,
|
2023-11-10 08:40:06 +00:00
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
2024-01-25 17:25:23 +00:00
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatAllObjects}},
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid deny rule with action detail",
|
|
|
|
rule: "deny:QuotaLimitReached Object.Put *",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.QuotaLimitReached,
|
2023-11-10 08:40:06 +00:00
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutObject}},
|
2024-01-25 17:25:23 +00:00
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatAllObjects}},
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid allow rule with conditions",
|
2024-05-14 09:23:26 +00:00
|
|
|
rule: "allow Object.Get ResourceCondition:Department=HR RequestCondition:Actor!=ownerA *",
|
2023-10-31 08:55:42 +00:00
|
|
|
expectRule: policyengine.Rule{
|
2023-11-10 08:40:06 +00:00
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodGetObject}},
|
2024-01-25 17:25:23 +00:00
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatAllObjects}},
|
2023-10-31 08:55:42 +00:00
|
|
|
Condition: []policyengine.Condition{
|
|
|
|
{
|
2024-05-14 09:23:26 +00:00
|
|
|
Op: policyengine.CondStringEquals,
|
|
|
|
Kind: policyengine.KindResource,
|
|
|
|
Key: "Department",
|
|
|
|
Value: "HR",
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-14 09:23:26 +00:00
|
|
|
Op: policyengine.CondStringNotEquals,
|
|
|
|
Kind: policyengine.KindRequest,
|
|
|
|
Key: "Actor",
|
|
|
|
Value: "ownerA",
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-01-25 17:25:23 +00:00
|
|
|
name: "Valid rule for object with conditions with action detail",
|
2024-05-14 09:23:26 +00:00
|
|
|
rule: "deny:QuotaLimitReached Object.Get ResourceCondition:Department=HR RequestCondition:Actor!=ownerA *",
|
2023-10-31 08:55:42 +00:00
|
|
|
expectRule: policyengine.Rule{
|
2023-11-10 08:40:06 +00:00
|
|
|
Status: policyengine.QuotaLimitReached,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodGetObject}},
|
2024-01-25 17:25:23 +00:00
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatAllObjects}},
|
2023-10-31 08:55:42 +00:00
|
|
|
Condition: []policyengine.Condition{
|
|
|
|
{
|
2024-05-14 09:23:26 +00:00
|
|
|
Op: policyengine.CondStringEquals,
|
|
|
|
Kind: policyengine.KindResource,
|
|
|
|
Key: "Department",
|
|
|
|
Value: "HR",
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-14 09:23:26 +00:00
|
|
|
Op: policyengine.CondStringNotEquals,
|
|
|
|
Kind: policyengine.KindRequest,
|
|
|
|
Key: "Actor",
|
|
|
|
Value: "ownerA",
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-01-25 17:25:23 +00:00
|
|
|
name: "Invalid rule with unknown status",
|
2023-10-31 08:55:42 +00:00
|
|
|
rule: "permit Object.Put *",
|
2024-01-25 17:25:23 +00:00
|
|
|
expectErr: errUnknownStatus,
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-01-25 17:25:23 +00:00
|
|
|
name: "Invalid rule with unknown action",
|
2023-10-31 08:55:42 +00:00
|
|
|
rule: "allow Object.PutOut *",
|
2024-01-25 17:25:23 +00:00
|
|
|
expectErr: errUnknownAction,
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
{
|
2024-01-25 17:25:23 +00:00
|
|
|
name: "Invalid rule with unknown status detail",
|
2023-10-31 08:55:42 +00:00
|
|
|
rule: "deny:UnknownActionDetail Object.Put *",
|
2024-01-25 17:25:23 +00:00
|
|
|
expectErr: errUnknownStatusDetail,
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with unknown condition binary operator",
|
2024-05-14 09:23:26 +00:00
|
|
|
rule: "deny Object.Put ResourceCondition:Department<HR *",
|
2023-10-31 08:55:42 +00:00
|
|
|
expectErr: errUnknownBinaryOperator,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with unknown condition object type",
|
2024-05-14 09:23:26 +00:00
|
|
|
rule: "deny Object.Put ResourSeCondiDion:Department=HR *",
|
2023-10-31 08:55:42 +00:00
|
|
|
expectErr: errUnknownCondObjectType,
|
|
|
|
},
|
2024-01-25 17:25:23 +00:00
|
|
|
{
|
|
|
|
name: "Invalid rule with mixed types of actions",
|
|
|
|
rule: "allow Object.Put Container.Put *",
|
|
|
|
expectErr: errMixedTypesInRule,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with no actions",
|
2024-05-14 09:23:26 +00:00
|
|
|
rule: "allow ResourceCondition:A=B *",
|
2024-01-25 17:25:23 +00:00
|
|
|
expectErr: errNoActionsInRule,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with invalid resource for object nm/cnt/obj/err",
|
|
|
|
rule: "allow Object.Put nm/cnt/obj/err",
|
|
|
|
expectErr: errUnsupportedResourceFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with invalid resource for container nm/cnt/err",
|
|
|
|
rule: "allow Container.Put nm/cnt/err",
|
|
|
|
expectErr: errUnsupportedResourceFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with invalid resource for container /nm/cnt/err",
|
|
|
|
rule: "allow Container.Put /nm/cnt/err",
|
|
|
|
expectErr: errUnsupportedResourceFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with invalid resource for container /nm/cnt/",
|
|
|
|
rule: "allow Container.Put /nm/cnt/",
|
|
|
|
expectErr: errUnsupportedResourceFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with invalid resource for container /nm/cnt",
|
|
|
|
rule: "allow Container.Put /nm/cnt",
|
|
|
|
expectErr: errUnsupportedResourceFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid rule with invalid resource for container /nm/",
|
|
|
|
rule: "allow Container.Put /nm/",
|
|
|
|
expectErr: errUnsupportedResourceFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for all containers",
|
|
|
|
rule: "allow Container.Put *",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutContainer}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatAllContainers}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for all containers in root namespace",
|
|
|
|
rule: "allow Container.Put /*",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutContainer}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{nativeschema.ResourceFormatRootContainers}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for container in root namespace",
|
|
|
|
rule: "allow Container.Put /cid",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutContainer}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatRootContainer, "cid"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for all container in namespace",
|
|
|
|
rule: "allow Container.Put ns/*",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutContainer}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatNamespaceContainers, "ns"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for container in namespace",
|
|
|
|
rule: "allow Container.Put ns/cid",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodPutContainer}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatNamespaceContainer, "ns", "cid"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid rule for container with conditions with action detail",
|
2024-05-14 09:23:26 +00:00
|
|
|
rule: "allow Container.Get ResourceCondition:A=B Container.Put RequestCondition:C!=D " +
|
2024-01-25 17:25:23 +00:00
|
|
|
"* /cnt_id",
|
|
|
|
expectRule: policyengine.Rule{
|
|
|
|
Status: policyengine.Allow,
|
|
|
|
Actions: policyengine.Actions{Names: []string{nativeschema.MethodGetContainer, nativeschema.MethodPutContainer}},
|
|
|
|
Resources: policyengine.Resources{Names: []string{
|
|
|
|
nativeschema.ResourceFormatAllContainers,
|
|
|
|
fmt.Sprintf(nativeschema.ResourceFormatRootContainer, "cnt_id"),
|
|
|
|
}},
|
|
|
|
Condition: []policyengine.Condition{
|
|
|
|
{
|
2024-05-14 09:23:26 +00:00
|
|
|
Op: policyengine.CondStringEquals,
|
|
|
|
Kind: policyengine.KindResource,
|
|
|
|
Key: "A",
|
|
|
|
Value: "B",
|
2024-01-25 17:25:23 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-14 09:23:26 +00:00
|
|
|
Op: policyengine.CondStringNotEquals,
|
|
|
|
Kind: policyengine.KindRequest,
|
|
|
|
Key: "C",
|
|
|
|
Value: "D",
|
2024-01-25 17:25:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
r := new(policyengine.Rule)
|
|
|
|
err := ParseAPERule(r, test.rule)
|
|
|
|
require.ErrorIs(t, err, test.expectErr)
|
|
|
|
if test.expectErr == nil {
|
|
|
|
require.Equal(t, test.expectRule, *r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|