diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 063b676..1a7421d 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -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: diff --git a/tools/celcli/c0055_test.go b/tools/celcli/c0055_test.go new file mode 100644 index 0000000..69231ef --- /dev/null +++ b/tools/celcli/c0055_test.go @@ -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) + } + }) + } +}