Skip to content
About Me
----

Policy-as-Code with Open Policy Agent (OPA) & Conftest

Architect Key Takeaways

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.


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 ]

Rego is a declarative query language designed to query structured data collections:

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 trigger
allow {
count(deny) == 0
}

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.

- 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-warn

If 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.


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

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.