Policy-as-Code with Open Policy Agent (OPA) & Conftest
Master Policy-as-Code in DevSecOps pipelines: Rego declarative policy syntax, automated Terraform plan validation with Conftest, and Kubernetes admission control.
Policy-as-Code with Open Policy Agent (OPA) & Conftest
Section titled βPolicy-as-Code with Open Policy Agent (OPA) & ConftestβIn traditional IT operations, compliance and security guardrails were enforced through multi-page PDF documents and manual change advisory board (CAB) reviews. In high-velocity cloud engineering, manual checks cannot keep up with continuous delivery.
Policy-as-Code (PaC) treats governance, security standards, and compliance rules as executable source code. It validates infrastructure configurations in the CI/CD pipeline before resources are provisioned in production.
1. What is Open Policy Agent (OPA)?
Section titled β1. What is Open Policy Agent (OPA)?βOpen Policy Agent (OPA) is an open-source, general-purpose policy engine hosted by the Cloud Native Computing Foundation (CNCF). OPA decouples policy decisions from application code, taking structured JSON/YAML input data, evaluating it against declarative Rego rules, and returning a deterministic decision (allow / deny).
[ Structured Input Data ] (Terraform Plan JSON, Kubernetes Manifest) β βΌ βββββββββββββββββββββββββββββββββ β Open Policy Agent Engine β <ββ [ Rego Policy Rules ] βββββββββββββββββββββββββββββββββ β βΌ [ Decision: Allow / Deny ]2. Rego Policy Language Essentials
Section titled β2. Rego Policy Language EssentialsβRego is a declarative query language designed to query structured data collections:
Example: Prohibiting Public AWS S3 Buckets
Section titled βExample: Prohibiting Public AWS S3 Bucketsβpackage terraform.aws.s3
default allow = false
# Rule: Deny any S3 bucket that has an ACL set to "public-read" or "public-read-write"deny[msg] { resource := input.resource_changes[_] resource.type == "aws_s3_bucket" acl := resource.change.after.acl bad_acls := ["public-read", "public-read-write"] bad_acls[_] == acl
msg := sprintf("S3 Bucket '%v' must not have public ACL: '%v'", [resource.address, acl])}
# Allow if zero deny rules triggerallow { count(deny) == 0}3. Shifting Policy Left with Conftest in CI/CD
Section titled β3. Shifting Policy Left with Conftest in CI/CDβConftest is a utility built on top of OPA that enables developers and pipelines to run Rego policies against structured configuration files including Terraform, Kubernetes YAML, Dockerfiles, and Helm charts.
Production Pipeline Step: Evaluating Terraform Plans
Section titled βProduction Pipeline Step: Evaluating Terraform Plansβ- name: Terraform Plan to JSON run: | terraform plan -out=tfplan.binary terraform show -json tfplan.binary > tfplan.json
- name: Test Terraform with Conftest Rego Policies run: | conftest test tfplan.json \ --policy ./policies \ --fail-on-warnIf an engineer submits a PR with an unencrypted EBS volume or an open Security Group (0.0.0.0/0), Conftest fails the CI build and posts the exact policy violation directly in the GitHub PR.
4. Comparing Policy Enforcement Layers
Section titled β4. Comparing Policy Enforcement Layersβ| Layer | Tool | Enforcement Point | Action on Violation |
|---|---|---|---|
| Pre-Commit (Local) | checkov / tfsec | Developer workstation | Blocks git commit |
| Pipeline CI Gate | Conftest / OPA | Pull Request build | Breaks CI build & blocks PR merge |
| Kubernetes Admission | OPA Gatekeeper / Kyverno | Kubernetes API Server | Rejects Pod / manifest creation |
| Cloud Runtime Guardrail | AWS Config / SCPs | Cloud Account Organization | Reverts resource or denies AWS API call |
5. Real-Time Production Knowledge Checks
Section titled β5. Real-Time Production Knowledge ChecksβQ1: Why is validating a compiled terraform show -json plan in CI superior to scanning raw .tf files?
Scanning raw .tf files often misses dynamically computed values, child module defaults, local variables, and ternary logic (e.g. kms_key_id = var.is_prod ? aws_kms_key.prod.arn : null). A compiled Terraform plan JSON represents the exact graph of resource changes (resource_changes) that will be sent to the cloud API, allowing Conftest to evaluate the true resolved state.
Q2: What is the primary operational difference between Conftest in CI and OPA Gatekeeper in Kubernetes?
Conftest operates out-of-band in the CI/CD pipeline, providing fast feedback to developers before code is merged into main. OPA Gatekeeper operates as an in-cluster Admission Controller webhook; it acts as the last line of defense, blocking non-compliant manifests even if an engineer bypasses CI and attempts a manual kubectl apply.