Policy as Code
Cheat sheet · https://pcornel.dk/topic.php?topic=5169
Answer
Policy as Code expresses governance, security or operational constraints in machine-testable definitions that can evaluate proposed or observed state consistently.
Remember
- Policy test pass ≠ runtime enforcement; the enforcement point may not use the tested policy/version.
- Policy as code ≠ desired infrastructure state; it constrains or evaluates state rather than necessarily creating it.
- Deny result ≠ root cause; input normalization, exemptions and policy selection must be traceable.
- Policy in Git ≠ active policy automatically; promotion/deployment and enforcement linkage are separate boundaries.
First checks
opa eval -d policy.rego -i input.json 'data.main.deny'— OPA policy testconftest test manifest.yaml— policy check on filescheckov -d .— IaC policy scan
Verify in this order
change → policy evaluation → allow/deny with reason
Where it usually breaks
A policy evaluation proves what a specific policy version decided for specific input; it does not prove that decision was enforced at runtime.
Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness. Preserve at least three independent checks: prerequisite/interface or identity/transport state; the owning control/service/policy state; then effective data-plane, backend or workload state. A healthy upstream boundary does not prove the downstream consumer or return path.
Policy as Code automation boundary map
| Boundary | Healthy evidence | Suspicious evidence |
|---|---|---|
| Intent / inputs | Owner, scope and authoritative inputs are explicit and versioned where applicable | Hidden defaults, stale inventory or competing sources of truth |
| Decision / execution | Plan or decision is explainable and actions are bounded per target | Blind fan-out, implicit assumptions or success based only on executor status |
| State / verification | Target is re-observed and acceptance criteria prove the intended outcome | No read-back, perpetual drift, partial side effects or unresolved exceptions |
| Recovery / evidence | Retry, rollback or escalation behavior is defined and auditable | Unknown partial completion, unsafe retry or evidence lost during remediation |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5169