ADR-0002 — Use Open Policy Agent (OPA) for Policy Enforcement¶
Status¶
Accepted
Context¶
The organization requires automated, pre-deployment compliance verification of all infrastructure changes. Specifically:
- Security policies (e.g., "no unencrypted storage") must be enforced as code, not checked by human reviewers post-merge.
- FinOps policies (e.g., "all resources must carry cost-allocation tags") must block non-compliant pull requests automatically.
- Policy violations must generate actionable, human-readable compliance reports that feed into the technical design specification.
- Policies must be unit-testable independently of live infrastructure.
Alternatives evaluated:
| Tool | Pros | Cons |
|---|---|---|
| OPA + Rego | CNCF graduated; domain-agnostic; Rego metadata embeds docs; opa test for unit tests |
Rego learning curve |
| Checkov | Python-based; large built-in rule library | Limited custom-policy expressiveness |
| Sentinel (HashiCorp) | Native Terraform Cloud integration | Proprietary; tied to HashiCorp ecosystem |
| Conftest | OPA wrapper with simpler CLI | Less direct integration with Backstage/MkDocs |
Decision¶
Adopt OPA (v0.60+) with policies written in Rego as the policy
enforcement engine. Policies live in policies/terraform/ and are evaluated
against the terraform plan JSON output during CI/CD (before terraform apply
is permitted).
Key design choices:
- Every
.regofile includes a__rego__metadoc__block withid,title,description,severity, andremediationfields. This metadata is extracted by the documentation pipeline to generate a compliance chapter in the technical design specification automatically. - Corresponding
_test.regofiles provide unit tests executed withopa test, ensuring policy logic is correct before it reaches production. - The
denyrule set is the authoritative contract: a non-emptydenyset causes the CI/CD pipeline to fail and surfaces the denial messages as pull-request annotations.
Consequences¶
- Positive: Compliance checks are deterministic, version-controlled, and
self-documenting; policy failures block merges automatically;
opa testprovides a fast feedback loop. - Negative: Rego requires a learning investment; complex policies can become verbose.
- Future work: Extend OPA policies to cover Kubernetes manifests and API gateway configurations as the platform matures.