Policy as Code
Policy as code is the practice of writing security, compliance and operational rules as version-controlled code that tools evaluate automatically, in pipelines, at deployment and at runtime.
Also known as: PaC, compliance as code, guardrails as code
How it works
Instead of a document that says "containers must not run as root" or "every cloud resource needs a cost-center tag", the rule is written in a policy language and checked by software. Policies live in Git, are reviewed and tested like application code, and are enforced at several points:
- In CI: checking Terraform or OpenTofu plans, Kubernetes manifests and Dockerfiles before merge, with tools such as Checkov, Trivy or Conftest.
- At the Kubernetes API: admission control rejects or mutates objects that break the rules. Common options are Open Policy Agent (OPA) Gatekeeper using the Rego language, Kyverno using YAML policies, and the built-in ValidatingAdmissionPolicy, which uses CEL expressions and has been stable since Kubernetes 1.30.
- In the cloud: service control policies, Azure Policy and similar guardrails at the account level.
- In applications: OPA or Cedar for authorization decisions.
OPA and Kyverno are both CNCF projects; OPA graduated in 2021.
Why it matters
Written rules that rely on people remembering them are applied inconsistently and are hard to audit. Policy as code applies the same rule every time, gives developers fast feedback, and produces evidence for auditors. A sensible rollout starts in audit or warn mode, measures violations, fixes existing workloads and then switches to enforcement, with a documented exception process. See Gepton's DevSecOps and Platform Engineering services.
Policy as Code in depth
Writing and testing a policy
Policies are code, so they need the same discipline: a clear requirement, tests, review and versioning. A typical workflow:
- State the requirement in plain words, including the reason and the exceptions, for example "Production containers must not run as root, except the approved node-exporter".
- Write the policy in the chosen engine: a Kyverno ClusterPolicy, an OPA Gatekeeper ConstraintTemplate and Constraint, or a Kubernetes ValidatingAdmissionPolicy with CEL.
- Write tests with passing and failing examples. Kyverno's CLI,
opa testfor Rego, and Conftest all support policy unit tests. - Run in CI against manifests and IaC plans on every pull request.
- Deploy to clusters in audit mode, review reported violations, fix them, then switch to enforce.
Example: enforcing image and tagging rules
Example: a platform team wants three rules across its clusters: images only from the company registry, every workload labeled with an owning team, and no privileged containers. In CI, Conftest checks rendered Kustomize output and OpenTofu plans, so developers see failures on their pull request with a message that explains the fix. In clusters, Kyverno policies enforce the same rules at admission, and a Kyverno image verification rule checks Sigstore signatures, which ties into the team's SLSA provenance work. Exceptions use Kyverno's PolicyException resource, each linked to a ticket with an expiry date, and a weekly report lists active exceptions.
Choosing a policy engine
| Engine | Language | Good fit |
|---|---|---|
| OPA and Gatekeeper | Rego | One language across Kubernetes, CI, APIs and authorization |
| Kyverno | YAML with CEL support | Kubernetes-focused teams; also mutates, generates and verifies images |
| ValidatingAdmissionPolicy | CEL | Simple validation without running an extra controller |
| Checkov, Trivy | Built-in rule sets, custom rules | Scanning IaC and manifests in CI |
Many teams combine a built-in scanner for broad coverage in CI with one admission engine for enforcement in clusters.
Policy as code also reaches beyond Kubernetes. Cloud organization policies stop resources from being created in unapproved regions, which helps with data residency. Application authorization engines such as OPA or Cedar keep access rules out of scattered if-statements. For AI agents, the same idea applies to tool permissions: which tools an agent may call, with which arguments, and when a person must approve.
Measuring the program
- Number of policies in enforce mode versus audit mode.
- Violations found in CI versus at admission. More caught earlier is the goal.
- Open exceptions and their age.
- Time for a developer to fix a failed check, which shows whether messages are clear.
- Audit evidence: policy reports exported for reviews and certifications.
Common mistakes
Typical problems are switching on hundreds of rules in enforce mode at once, cryptic failure messages, policies that only exist in clusters so developers learn about failures at deploy time, no exception process so teams work around controls, and untested Rego that blocks valid workloads. Policy as code is a core part of DevSecOps, platform engineering guardrails and AI guardrails for agent permissions. Gepton implements it in DevSecOps and platform engineering.
Questions about Policy as Code
Something else? Send us your question and an engineer replies with next steps.
Should policies block deployments or only warn?
Both, at different stages. New policies should start in audit or warn mode so you can see existing violations without breaking anything. Once workloads are fixed and the policy is tested, switch it to enforce for clear security rules such as no privileged containers. Some guidance-style rules, such as recommended labels, can stay as warnings permanently. Kubernetes ValidatingAdmissionPolicy supports Deny, Warn and Audit actions, and Kyverno and Gatekeeper offer similar modes.
Can policy as code help with compliance audits?
Yes. Written policies in Git show exactly which controls exist and when they changed, with reviews attached. Engines produce reports of passing and failing resources, and CI logs show checks on every change. Together these give auditors evidence that a control is applied continuously, not just described in a document. Map each policy to the control it supports, such as a requirement in your security framework, so evidence is easy to collect.