Use this when writing compliance rules as code.
Policy as Code · Compliance Automation
Policy as code writes compliance and security rules as code, with tools like Open Policy Agent (Rego), Sentinel or Kyverno, so policies are versioned, tested and enforced automatically in pipelines.
1. Policy as code enforces rules in CI and at runtime.
2. Tested policies.
3. Clear messages.
4. Exceptions as code.
5. Version control.
change → policy evaluation → allow/deny
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as stale policy, incomplete input, exception creep, false compliance from missing data, destructive auto-remediation and passing policy check without validating actual target, choosing the cases most relevant to versioned policy bundle, deterministic input, rule decision/violations, exceptions and re-evaluation after remediation.
Step 2 Common mistakes Narrow the fault
- Policies without tests.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- OPA/Rego, Sentinel, Kyverno.
- Test policies like code.
- Enforce in pipelines.
opa eval / conftest test— OPA policies- Kyverno/Gatekeeper policies (Kubernetes) — admission
- Sentinel/OPA on Terraform plans — IaC
ReferenceWhy it matters1 section · depth on demand
What it is
Policy as code expresses rules as testable code (OPA/Rego, Batfish checks, Ansible asserts) run in CI.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Policy as code expresses compliance and security rules in machine-readable form, stored in Git, tested and evaluated automatically against configurations, infrastructure plans or live systems. Rules become consistent, versioned and reviewable instead of living in documents.
| Tool | Evaluates |
|---|---|
| Open Policy Agent / Conftest | JSON/YAML: Terraform plans, Kubernetes manifests, configs |
| Kyverno | Kubernetes resources at admission |
| Checkov, tfsec/Trivy | IaC security |
| Batfish | Network configurations and reachability |
| Sentinel | Terraform Cloud/Enterprise plans |
package network
deny[msg] {
some i
input.interfaces[i].mode == "access"
not input.interfaces[i].bpduguard
msg := sprintf("%s: access port without BPDU Guard", [input.interfaces[i].name])
}
Run policies in CI before changes are applied, and against live state on a schedule.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Compliance Automation > Policy as Code. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.openpolicyagent.org/docs/latest/ · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For versioned policy bundle, deterministic input, rule decision/violations, exceptions and re-evaluation after remediation, prefer typed fields and stable IDs over human presentation text.