Skip to content
Merged
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
13 changes: 13 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ on:
- '.github/workflow/release.yaml'

jobs:
test-policy-expressions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version-file: tools/celcli/go.mod
cache-dependency-path: tools/celcli/go.sum
- name: Test CEL policy expressions
working-directory: tools/celcli
# Policy manifests are outside the Go module.
run: go test -count=1 -v ./...

test-all-policies:
runs-on: ubuntu-latest
steps:
Expand Down
102 changes: 102 additions & 0 deletions tools/celcli/c0055_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"os"
"testing"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"gopkg.in/yaml.v3"
)

func TestC0055PodAppArmorField(t *testing.T) {
data, err := os.ReadFile("../../controls/C-0055/policy.yaml")
if err != nil {
t.Fatal(err)
}
var policy struct {
Spec struct {
Variables []struct{ Name, Expression string }
Validations []struct{ Expression string }
}
}
if err := yaml.Unmarshal(data, &policy); err != nil {
t.Fatal(err)
}

env, err := cel.NewEnv(
cel.OptionalTypes(),
cel.Variable("object", cel.DynType),
cel.Variable("variables", cel.MapType(cel.StringType, cel.DynType)),
)
if err != nil {
t.Fatal(err)
}
eval := func(t *testing.T, expression string, activation map[string]any) ref.Val {
t.Helper()
ast, issues := env.Compile(expression)
if err := issues.Err(); err != nil {
t.Fatalf("compile %q: %v", expression, err)
}
program, err := env.Program(ast)
if err != nil {
t.Fatalf("build program for %q: %v", expression, err)
}
result, _, err := program.Eval(activation)
if err != nil {
t.Fatalf("evaluate %q: %v", expression, err)
}
return result
}

cases := []struct {
name string
profile string
wantAllowed bool
}{
{name: "RuntimeDefault", profile: "RuntimeDefault", wantAllowed: true},
{name: "missing profile"},
{name: "Unconfined", profile: "Unconfined"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Keep AppArmor as the Pod's only possible hardening setting.
// Direct CEL evaluation keeps legacy annotations out of the object.
spec := map[string]any{
"containers": []map[string]any{{"name": "app", "image": "busybox"}},
}
if tc.profile != "" {
spec["securityContext"] = map[string]any{
"appArmorProfile": map[string]any{"type": tc.profile},
}
}
variables := map[string]ref.Val{}
activation := map[string]any{
"object": map[string]any{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]any{"name": "apparmor-test"},
"spec": spec,
},
"variables": variables,
}
for _, variable := range policy.Spec.Variables {
variables[variable.Name] = eval(t, variable.Expression, activation)
}

allowed := true
for _, validation := range policy.Spec.Validations {
result := eval(t, validation.Expression, activation)
valid, ok := result.(types.Bool)
if !ok {
t.Fatalf("validation %q returned %v, want a boolean", validation.Expression, result)
}
allowed = allowed && bool(valid)
}
if allowed != tc.wantAllowed {
t.Errorf("C-0055 allowed = %t, want %t", allowed, tc.wantAllowed)
}
})
}
}
Loading