Skip to content

feat(chart, e2e): an OPA policy that does not compile is reported when it is applied, and its restart cases are in CI (#1447, #1448) - #1495

Closed
bpalermo wants to merge 1 commit into
mainfrom
fix/1447-1448-opa-bad-policy
Closed

bpalermo wants to merge 1 commit into
mainfrom
fix/1447-1448-opa-bad-policy

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 9, 2026

Copy link
Copy Markdown
Owner

Closes #1447
Closes #1448

An agent wrote this pull request.

The standing decision from #1383 bounds it: OPA runs with --watch, and validating a policy is the job of whoever updates it, before the update. Nothing here validates, refuses or rolls back a policy. This is about seeing a policy that does not compile when it is applied, and about pinning in CI what it does at the restarts that follow.

What changes

  1. A policy check (chart aether 2.4.23, proxy.authzSidecar.opa.policyCheck, on by default with the OPA preset). One more pod, the Deployment <release>-opa-policy-check: the sidecar's image, the same ConfigMap mounted, and a readiness probe that runs /opa check /policy/policy.rego. Its pod template carries a checksum of the policy and its strategy is Recreate, so each policy change is judged by a new pod that mounts the ConfigMap as it is. With a policy that does not compile:

    • kubectl rollout status deploy/aether-opa-policy-check does not complete and the Deployment shows 0/1;
    • the pod's Unhealthy event carries the compile error with file and line (measured 7 s after helm upgrade began);
    • kube_deployment_status_replicas_unavailable{deployment="aether-opa-policy-check"} > 0 (standard kube-state-metrics, no log pipeline needed);
    • helm upgrade --wait fails at its timeout. The ConfigMap is updated all the same: this reports, it does not block.

    It takes no part in any request and no proxy pod is touched.

  2. e2e/authz-bad-policy.sh and the nightly job authz-bad-policy (e2e: the bad-policy restart cases of the OPA sidecar are not in CI #1448): its own kind cluster, e2e/authz.sh sourced for the bring-up (so it adds no image reference of its own). Steps under "Measured" below.

  3. Docs: docs/runbook.md, new section A policy that does not compile (what to run before updating the ConfigMap, what OPA reports by itself, the signal and its query, what a bad file does at each later event, recovering a node whose proxy is down, the open decision); docs/configuration.md rows; the readinessProbe/livenessProbe notes for the sidecar now say what a failing readiness probe does to a roll.

What an operator sees on upgrade. With the OPA preset on: one new Deployment (5m / 32Mi requests, 96Mi memory limit, no CPU limit, GOMAXPROCS=1); the proxy DaemonSet renders byte-identical, so no proxy pod is replaced. With the preset off (the default), a bring-your-own sidecar, or policyCheck.enabled=false: the whole render is byte-identical to main. Nothing to do; after the upgrade kubectl -n aether-system rollout status deploy/aether-opa-policy-check should return 0.

Why a pod of its own: the readiness probe was tried first, and it is unsafe

The smallest change would be opa check as the authz sidecar's own readiness probe: the proxy pod turns NotReady on the node whose file is bad. I built that first and ran the new e2e against it. It reported the bad policy as designed, and then step e failed:

==> e. a proxy roll with the bad policy on the node stalls with the old pod still deciding
  ✓ aether-proxy-n5jz7: authz exits 1 (2 restarts), proxy never started
  ✗ the roll deleted the old pod aether-proxy-wbsbr although its successor never became Ready
01:24:22Z  aether-proxy        SuccessfulCreate  Created pod: aether-proxy-n5jz7          (the roll's surge pod; its sidecar cannot start)
01:25:01Z  aether-proxy-wbsbr  Unhealthy         Readiness probe failed: … /policy/policy.rego:7: rego_parse_error: unexpected eof token
01:25:01Z  aether-proxy        SuccessfulDelete  Deleted pod: aether-proxy-wbsbr           (the serving pod, the same second)

maxUnavailable: 0 protects only Ready pods: the DaemonSet controller deletes a NotReady old pod as soon as its replacement exists, whether or not the replacement becomes Ready. Without the probe the same roll stalls with the old pod serving (step e below, on main and on this branch). So a readiness signal on the proxy pod turns "bad policy, then a roll" from a stalled roll into every node losing its proxy after the successor wait. The check therefore lives in a pod nothing depends on, a guard test keeps the preset from rendering a readiness or liveness probe on the sidecar, and the docs for proxy.authzSidecar.readinessProbe (which said it "gates a proxy roll") now say what it does.

Measured

kind v0.33.0, Kubernetes 1.35.8, one node, OPA 1.21.1-envoy-static (sha256:b4a8bbe8…344b, verified with docker buildx imagetools inspect), failureMode: DENY unless stated. Policies are changed with helm upgrade, all values passed each time.

What OPA itself exposes when a watched file fails to load (docker, the chart's exact arguments, a ConfigMap-style volume swapped the way the kubelet does; also with status.console=true and status.prometheus=true):

Signal After a file that does not compile
process keeps running, keeps the last good policy (/v1/policies unchanged)
/health, /health?plugins, /health?bundles 200 {}, unchanged
/metrics no new series, plugin_status_gauge{…status="OK"} 1 for every plugin
/v1/status plugin states and metrics, nothing about the file watch
log "msg":"Processed file watch event." with a non-null "err", level info

So the honest answer to "is there an OPA signal the chart can surface" is: a log line only.

opa check against a starting opa run, per policy (the two agreed on all seven):

Policy opa check opa run at start running --watch sidecar
valid 0 starts loads it
does not parse 1 exit 1 load error keeps last good
parses, undefined function 1 exit 1 initialization error keeps last good
empty file / comments only 1 (empty module) exit 1 keeps last good
compiles, denies everything 0 starts loaded: 403 by decision (ext_authz.denied +2, error +0)
compiles, no envoy.authz.allow 0 starts loaded: every check is an ext_authz error ("error":"undefined decision"), 403 under DENY

Cost of one opa check: 30 ms wall, 33 MB peak RSS; inside a 128Mi container already running OPA the cgroup peak went from 14.9 MB to 23.6 MB over 40 runs.

On kind, with the chart's real pod spec (each row is a step of e2e/authz-bad-policy.sh unless marked "by hand"):

Case main (chart 2.4.21) this branch
a. baseline 200 / 403 decided by the sidecar same; policy check available
b. deny-all policy loaded; 403 by decision, same pod same; policy check available
c. policy that does not parse, sidecar running last good policy decides (200 / 403), same pod, no restart, same Envoy epoch, ext_authz.error +0. Nothing reports it: no object and no event in the namespace same on the node, proxy pod still Ready, DaemonSet numberUnavailable 0. Reported 7 s after the upgrade began: Unhealthy event Readiness probe failed: … /policy/policy.rego:7: rego_parse_error: unexpected eof token, Deployment unavailable, rollout status does not complete. (The file reached the node 75 s after the upgrade began.)
d. authz container stopped, bad file on the node restarted OPA exits 1, Init:CrashLoopBackOff; proxy 0 restarts, same epoch; allowed request 403, ext_authz.error 0 → 2. Valid policy: 200 again in the same pod same
d'. the same with failureMode: ALLOW (by hand) not run request without the allow header: 200, failure_mode_allowed 0 → 4 (it was 403 before the sidecar stopped)
e. rollout restart ds/aether-proxy, bad file on the node surge pod's sidecar exits 1, its proxy never starts; the roll stalls; old pod still decides 200 / 403, ext_authz.error +0 same, old pod still Ready
f. the old pod deleted its Envoy serves 200 at +15 s, the pod is gone after 166 s; then the allowed request fails to connect (000): the node has no proxy same (168 s)
g. valid policy 200 again 110 s after the upgrade began (kubelet delivery, then the restart back-off); the stuck pod starts its proxy same (115 s); policy check available
recovering by deleting the stuck pod once the ConfigMap is fixed (by hand) not run 200 again 4 s after the delete
helm upgrade --wait --timeout 60s with the bad policy (by hand) not run Error: UPGRADE FAILED: context deadline exceeded after 63 s, release failed, ConfigMap updated; the next upgrade with a valid policy returns it to deployed

The decision left open: should the proxy start when its sidecar cannot?

Not changed here. Row f is the remaining blast radius, and removing it means changing what #1275 guarantees.

  • Today (A): no proxy without its authz. The kubelet starts proxy only after the sidecar's startup probe passes. With a bad file on the node a new pod never starts its proxy. A surge roll stalls safely (row e). Any replacement without a surviving old pod (pod deleted, node drained, rebooted or added) leaves the node with no data plane: every route, including routes with no extAuthz, and regardless of failureMode. It never serves a request unchecked and never answers a spurious 403; it serves nothing.
  • Alternative (B): the proxy starts after a bounded wait, and failureMode decides. Routes with extAuthz get 403 under DENY and pass unchecked under ALLOW; every other route works. That is what already happens when only the sidecar restarts (rows d and d'), so B makes a pod replacement behave like a sidecar restart. Its costs: under ALLOW a node comes up serving extAuthz routes unchecked until someone notices (ext_authz.failure_mode_allowed), and proxy pod: the ext_authz sidecar can start after the proxy is serving — authz errors (403 under failureMode DENY) until it is up #1275's invariant becomes "the proxy waits for its authz up to N seconds" instead of "always". It needs a startup probe that can give up (a flag on proxy-ready, staged one release before the chart uses it), not a values change.
  • A third option, keeping the last good policy across restarts, would remove the problem instead of choosing a failure. OPA can persist a bundle, but not from a directory: with bundles.<n>.resource=file:///policy/..data and persist=true it logs Persisting bundle to disk failed: no raw bundle bytes to persist to disk and never activates (docker, 1.21.1). It would mean shipping the policy as a tarball and dropping --watch, which chart: the OPA authz sidecar does not watch its policy, so a policy change needs a proxy roll #1383 decided against.

Recommendation: B. With the default failureMode: DENY nothing is ever served unchecked under B, and the outage shrinks from "the node" to "routes that asked for authorization on that node". Under ALLOW the operator has already chosen availability over enforcement for exactly this failure, and today that choice is honoured for a sidecar restart and overridden by a node outage for a pod replacement. It is the owner's call because it trades a guarantee for availability; the e2e's step f pins today's behaviour and says in its failure message that it must change with that decision.

Render, before and after

helm template of main and of this branch, same version string on both sides, --kube-version 1.35.0:

Values Result
default byte-identical
bring-your-own sidecar byte-identical
OPA preset, policyCheck.enabled=false byte-identical to main's OPA preset render
OPA preset one document added, nothing else changed (the DaemonSet is byte-identical)

The added document, comments omitted:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: aether-opa-policy-check
  namespace: aether-system
  labels: {…, app.kubernetes.io/component: opa-policy-check}
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector: {matchLabels: {…, app.kubernetes.io/component: opa-policy-check}}
  template:
    metadata:
      annotations:
        checksum/opa-policy: 4d2b53476811ded7f3e9f37b684721007ab75e14aa97bbd0b02cdbdd4f3d8080
    spec:
      automountServiceAccountToken: false
      tolerations: [{key: aether.io/agent-not-ready, operator: Exists, effect: NoSchedule}]
      securityContext: {runAsNonRoot: true, seccompProfile: {type: RuntimeDefault}}
      containers:
        - name: check
          image: "openpolicyagent/opa:1.21.1-envoy-static"
          args: ["run", "--server", "--skip-version-check", "--addr=127.0.0.1:8181"]
          env: [{name: GOMAXPROCS, value: "1"}]
          readinessProbe:
            exec: {command: ["/opa", "check", "/policy/policy.rego"]}
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 1
          volumeMounts: [{name: opa-policy, mountPath: /policy, readOnly: true}]
          resources: {limits: {memory: 96Mi}, requests: {cpu: 5m, memory: 32Mi}}
          securityContext: {readOnlyRootFilesystem: true, allowPrivilegeEscalation: false, capabilities: {drop: ["ALL"]}}
      volumes: [{name: opa-policy, configMap: {name: aether-opa-policy}}]

Decisions in it:

  • GOMAXPROCS=1, no CPU limit. The container's OPA is idle and the probe is a new 30 ms Go process every period; without the pin both would size themselves to the node's cores.
  • The image is proxy.authzSidecar.opa.image, the sidecar's value, so the check always runs the OPA the sidecars run, and it follows the digest pin of fix(chart, e2e): third-party images are pinned by digest from one inventory with a check; every udsecho object is labelled (#1400, #1401, #1402) #1476 when that lands (this branch adds no image reference).
  • Idle opa run --server with no policy argument: the image holds one binary and a pod needs a process. Given the policy it would exit on a bad one, and the signal would be a crash loop without the error in an event.
  • Left unchanged on purpose: the sidecar, its probes, its arguments and failureMode semantics.

Tests

Template tests, written first and run against the chart without the new template:

//charts/aether:aether_opa_policy_check_test                           FAILED  could not find template templates/agent-proxy-opa-policy-check.yaml
//charts/aether:aether_opa_policy_check_overrides_test                 FAILED
//charts/aether:aether_opa_policy_check_checksum_changes_test          FAILED
//charts/aether:aether_opa_policy_check_no_liveness_no_cpu_limit_test  FAILED
//charts/aether:aether_opa_policy_check_disabled_test                  PASSED  (guard)
//charts/aether:aether_opa_policy_check_not_for_byo_sidecar_test       PASSED  (guard)
//charts/aether:aether_opa_policy_check_not_by_default_test            PASSED  (guard)
//charts/aether:aether_opa_sidecar_no_readiness_or_liveness_probe_test PASSED  (guard: the sidecar gets no readiness or liveness probe)

With the template: all green, and bazel test //charts/... //bazel/helm/... //e2e/... passes. The strategy: Recreate pattern was added to aether_opa_policy_check_test together with the strategy, after the first kind run; it was not seen red on its own.

e2e, on kind:

  • against main's chart with ABP_EXPECT=red (the gate of step c inverted: nothing may report the policy): passed, which is the evidence that main shows nothing;
  • against this branch: passed, every step, twice (the second time with the final Recreate strategy; the event came 7 s after the upgrade began both times);
  • against main's chart expecting the fix: fails in step a, the policy check is not available … deployments.apps "aether-opa-policy-check" not found;
  • against the first design (the sidecar readiness probe): failed at step e, as quoted above.

Locally: the lint build scoped to //e2e/... //charts/... //bazel/helm/... with --config=lint --@aspect_rules_lint//lint:fail_on_violation, make format-check, make actionlint, scripts/check-shell-lint.sh.

Not verified

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

…n it is applied, and its restart cases are in CI (#1447, #1448)

A running OPA sidecar that is handed a policy it cannot compile keeps the
last good one, and says so only in a log line at level info: no metric, no
health endpoint, no status (measured on 1.21.1, status plugin on as well).
No sidecar can start with that file, so the node's next sidecar restart
fails every check per failureMode and its next pod replacement leaves the
node without a proxy.

Chart 2.4.23: with the OPA preset on, a one-pod Deployment,
<release>-opa-policy-check, runs the sidecar's image with the same
ConfigMap and `opa check` on the mounted file as its readiness probe
(proxy.authzSidecar.opa.policyCheck, default on). Its pod template carries
a checksum of the policy and it uses Recreate, so each policy change is
judged by a new pod within seconds: the Deployment is unavailable, the
pod's Unhealthy event carries the compile error, and `helm upgrade --wait`
fails. It validates nothing before the update and takes no part in any
request. The proxy DaemonSet renders byte-identical.

It is a pod of its own because the same probe on the sidecar is unsafe:
during a roll the DaemonSet controller deletes a NotReady old pod as soon
as its replacement exists. Measured on kind with that probe: a bad policy,
then a proxy roll, and the serving pod was deleted in the second it turned
NotReady while its successor crash-looped. A guard test keeps the preset
from rendering a readiness or liveness probe on the sidecar, and the
values and docs for proxy.authzSidecar.readinessProbe say so.

e2e/authz-bad-policy.sh (nightly job authz-bad-policy, its own kind
cluster, e2e/authz.sh sourced): a deny-all policy is a decision; a policy
that does not parse leaves the running sidecar deciding and is reported by
the check; a stopped sidecar crash-loops while the proxy runs on and every
check fails closed; a roll stalls with the old pod serving; a deleted pod
leaves the node with no proxy after the successor wait (today's behaviour,
pinned until the #1447 decision); a valid policy recovers it.

Whether the proxy should start when its sidecar cannot is left to the
owner; nothing here changes fail-open or fail-closed behaviour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Copilot AI balanced review requested due to automatic review settings October 9, 2026 02:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The implementation is isolated, thoroughly tested, and no unresolved correctness issues were identified.

0 open findings

What changed in this PR

Adds an isolated OPA policy compiler check and CI coverage for bad-policy restart scenarios.

Changes:

  • Adds the configurable OPA policy-check Deployment.
  • Adds destructive nightly authorization E2E coverage.
  • Documents detection, impact, and recovery procedures.
File Description
.github/​workflows/​e2e.yaml Adds the nightly bad-policy job.
charts/​aether/​BUILD.bazel Adds Helm rendering tests.
charts/​aether/​Chart.yaml Bumps chart version.
charts/​aether/​templates/​_helpers.tpl Adds policy-check naming and labels.
charts/​aether/​templates/​agent-proxy-opa-policy-check.yaml Defines the isolated checker Deployment.
charts/​aether/​values.yaml Adds policy-check defaults and guidance.
docs/​configuration.md Documents new values and probe behavior.
docs/​runbook.md Documents detection and recovery operations.
e2e/​authz-bad-policy.sh Tests bad-policy lifecycle scenarios.
e2e/​authz.sh Supports reuse as a sourced harness.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bpalermo

Copy link
Copy Markdown
Owner Author

Closed on the owner's decision: the chart will not run a workload that checks policies. A policy is assumed to be tested before it is distributed (the standing rule from #1383), so what an untested policy does at a later restart is not something the chart or CI will cover.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

@bpalermo bpalermo closed this Oct 10, 2026
@bpalermo
bpalermo deleted the fix/1447-1448-opa-bad-policy branch October 11, 2026 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants