# Policy as Code and Compliance

> Encode security and compliance rules as code with OPA, Kyverno or cloud policy services, and enforce them in CI and at admission. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/devops/policy-as-code
Last updated: 2026-11-28

Encode security and compliance rules as code with OPA, Kyverno or cloud policy services, and enforce them in CI and at admission.

Advanced · about 5 hours · difficulty 4/5

Learn first: [Identity, Access and Least Privilege](https://www.internhack.in/roadmaps/tracks/devops/iam-least-privilege), [Terraform and OpenTofu](https://www.internhack.in/roadmaps/tracks/devops/terraform)

As organisations grow, rules like "no public buckets", "all images from our registry" or "every resource has an owner tag" cannot rely on reviewers remembering them. **Policy as code** writes those rules down and enforces them automatically.

### Where policies run

- **In CI**: check Terraform plans, Kubernetes manifests and Dockerfiles before merge (Conftest, Checkov custom rules).
- **At admission**: Kubernetes validating and mutating webhooks reject or fix resources as they are created.
- **In the cloud control plane**: AWS Service Control Policies and Config rules, Azure Policy, GCP Organization Policy.
- **At runtime**: detection tools such as **Falco** alert on suspicious behaviour inside containers.

### The main engines

- **Open Policy Agent (OPA)** with the **Rego** language is general purpose: Kubernetes (via **Gatekeeper**), Terraform, APIs and more.
- **Kyverno** is Kubernetes-native, writes policies as YAML, and can validate, mutate, generate resources and verify image signatures.
- Kubernetes **Pod Security Admission** enforces the baseline and restricted pod standards with no extra install.

### Rolling policies out

Start in **audit** mode to see what would break, fix existing violations, then switch to **enforce**. Give clear error messages that tell developers how to comply, keep policies in Git with tests, and document exceptions.

### Compliance

Frameworks such as SOC 2, ISO 27001, PCI DSS and CIS Benchmarks map to concrete controls. Policy as code plus audit logs turns an evidence-gathering scramble into a report you can generate.

**Good enough to move on:** you can write a Kyverno or Gatekeeper policy that requires resource limits and blocks privileged pods, run it in audit then enforce mode, and test Terraform plans against a Conftest rule in CI.

## Key concepts

- **Policy enforcement points**
- **OPA, Rego and Gatekeeper**
- **Kyverno policies**
- **Pod Security Admission**
- **Conftest for IaC in CI**
- **Audit vs enforce rollout**
- **Runtime detection with Falco**
- **CIS Benchmarks and compliance frameworks**

## Mini project

Install Kyverno on a local cluster with three policies (no latest tag, required limits, no privileged pods) in audit mode, fix the violations it reports, switch to enforce, and add a Conftest check for public buckets to your Terraform pipeline.

## Check yourself

Why roll a new admission policy out in audit mode first, and what information do you need before switching it to enforce?

## Free resources

- [Open Policy Agent documentation](https://www.openpolicyagent.org/docs/latest/) (OPA)
- [Kyverno documentation](https://kyverno.io/docs/) (Kyverno)
- [Pod Security Admission](https://kubernetes.io/docs/concepts/security/pod-security-admission/) (kubernetes.io)

Previous: [Identity, Access and Least Privilege](https://www.internhack.in/roadmaps/tracks/devops/iam-least-privilege) · Next: [Database Backups and Restores](https://www.internhack.in/roadmaps/tracks/devops/database-backups)

## More in DevSecOps and Supply Chain Security

- [Shift-Left Security: SAST, SCA and DAST](https://www.internhack.in/roadmaps/tracks/devops/security-scanning)
- [Software Supply Chain Security](https://www.internhack.in/roadmaps/tracks/devops/supply-chain-security)
- [Identity, Access and Least Privilege](https://www.internhack.in/roadmaps/tracks/devops/iam-least-privilege)
- [Policy as Code and Compliance](https://www.internhack.in/roadmaps/tracks/devops/policy-as-code)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DevOps Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/devops)
