Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/securitypolicy/api_test.rego
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package api

version := "0.0.2"
version := "0.7.0"

enforcement_points := {
"__fixture_for_future_test__": {"introducedVersion": "100.0.0", "default_results": {"allowed": true}, "use_framework": false},
"__fixture_for_allowed_test_true__": {"introducedVersion": "0.0.2", "default_results": {"allowed": true}, "use_framework": false},
"__fixture_for_allowed_test_false__": {"introducedVersion": "0.0.2", "default_results": {"allowed": false}, "use_framework": false},
"__fixture_for_allowed_extra__": {"introducedVersion": "0.0.1", "default_results": {"allowed": false, "__test__": "test"}, "use_framework": false}
"__fixture_for_allowed_test_true__": {"introducedVersion": "0.7.0", "default_results": {"allowed": true}, "use_framework": false},
"__fixture_for_allowed_test_false__": {"introducedVersion": "0.7.0", "default_results": {"allowed": false}, "use_framework": false},
"__fixture_for_allowed_extra__": {"introducedVersion": "0.6.0", "default_results": {"allowed": false, "__test__": "test"}, "use_framework": false}
}
7 changes: 7 additions & 0 deletions pkg/securitypolicy/framework.rego
Original file line number Diff line number Diff line change
Expand Up @@ -3311,6 +3311,13 @@ default policy_api_version := null
policy_framework_version := data.policy.framework_version
policy_api_version := data.policy.api_version

default is_unsupported_api_version := false

is_unsupported_api_version if {
policy_api_version != null
semver.compare(policy_api_version, "0.6.0") < 0
}

# deprecated
policy_framework_version := data.policy.framework_svn
policy_api_version := data.policy.api_svn
Expand Down
65 changes: 60 additions & 5 deletions pkg/securitypolicy/regopolicy_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2472,12 +2472,67 @@ func Test_Rego_Version_Future_Enforcement_Point(t *testing.T) {
}
}

func Test_Rego_Unsupported_APIVersion(t *testing.T) {
tests := []struct {
name string
versionKey string
version string
supported bool
}{
{
name: "below minimum",
versionKey: "api_version",
version: "0.5.9",
},
{
name: "minimum",
versionKey: "api_version",
version: "0.6.0",
supported: true,
},
{
name: "above minimum",
versionKey: "api_version",
version: "0.7.0",
supported: true,
},
{
name: "deprecated key below minimum",
versionKey: "api_svn",
version: "0.5.9",
},
{
name: "deprecated key at minimum",
versionKey: "api_svn",
version: "0.6.0",
supported: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
code := fmt.Sprintf("package policy\n\n%s := %q", test.versionKey, test.version)
_, err := newRegoPolicy(code, []oci.Mount{}, []oci.Mount{}, testOSType)
if test.supported {
if err != nil {
t.Fatalf("supported policy version %s was rejected: %v", test.version, err)
}
return
}

if err == nil {
t.Fatalf("unsupported policy version %s was accepted", test.version)
}
})
}
}

// Tests whether the enforcement point logic returns the default behavior
// and "unsupported" when the enforcement point was added in a version of the
// framework that was released after the policy was authored as indicated
// by their respective version information.
func Test_Rego_Version_Unavailable_Enforcement_Point(t *testing.T) {
code := "package policy\n\napi_version := \"0.0.1\""
code := "package policy\n\napi_version := \"0.6.0\""
policy, err := newRegoPolicy(code, []oci.Mount{}, []oci.Mount{}, testOSType)

if err != nil {
Expand Down Expand Up @@ -2511,7 +2566,7 @@ func Test_Rego_Version_Unavailable_Enforcement_Point(t *testing.T) {
}

func Test_Rego_Enforcement_Point_Allowed(t *testing.T) {
code := "package policy\n\napi_version := \"0.0.1\""
code := "package policy\n\napi_version := \"0.6.0\""
policy, err := newRegoPolicy(code, []oci.Mount{}, []oci.Mount{}, testOSType)

if err != nil {
Expand Down Expand Up @@ -2560,7 +2615,7 @@ func Test_Rego_Enforcement_Point_Extra(t *testing.T) {
ctx := context.Background()
code := `package policy

api_version := "0.0.1"
api_version := "0.6.0"

__fixture_for_allowed_extra__ := {"allowed": true}
`
Expand Down Expand Up @@ -8921,7 +8976,7 @@ func Test_Rego_APISVN(t *testing.T) {
func Test_Rego_NoReason(t *testing.T) {
code := `package policy

api_version := "0.0.1"
api_version := "0.6.0"

mount_device := {"allowed": false}
`
Expand Down Expand Up @@ -9013,7 +9068,7 @@ func Test_Rego_ErrorTruncation_Unable(t *testing.T) {
func Test_Rego_ErrorTruncation_CustomPolicy(t *testing.T) {
code := fmt.Sprintf(`package policy

api_version := "0.1.0"
api_version := "0.6.0"

mount_device := {"allowed": false}

Expand Down
15 changes: 15 additions & 0 deletions pkg/securitypolicy/securitypolicyenforcer_rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ func newRegoPolicy(code string, defaultMounts []oci.Mount, privilegedMounts []oc
return nil, fmt.Errorf("rego compilation failed: %w", err)
}

result, err := policy.rego.RawQuery("data.framework.is_unsupported_api_version", map[string]interface{}{})
if err != nil {
return nil, fmt.Errorf("error checking policy api_version: %w", err)
}
if len(result) != 1 || len(result[0].Expressions) != 1 {
return nil, errors.New("error checking policy api_version: unexpected result")
}
unsupported, ok := result[0].Expressions[0].Value.(bool)
if !ok {
return nil, errors.New("error checking policy api_version: result is not a boolean")
}
if unsupported {
return nil, errors.New("policy api_version is unsupported: minimum supported version is 0.6.0")
}

// by default we do not perform message truncation
policy.maxErrorMessageLength = 0

Expand Down
Loading