Compliance as Code: Automating Governance at the Speed of Delivery
Compliance has traditionally been a manual, periodic, and adversarial process -- auditors versus engineers, spreadsheets versus velocity. Compliance as Code changes the paradigm: policies are written as code, enforced in pipelines, and validated continuously. The audit becomes an automated report, not a fire drill.
Tool Comparison
| Capability | OPA / Rego | HashiCorp Sentinel | Kyverno | Checkov | Cloud Custodian |
|---|---|---|---|---|---|
| Type | General-purpose policy engine | Terraform policy-as-code | Kubernetes-native policies | IaC static analysis | Cloud resource policy |
| Language | Rego (declarative) | Sentinel (proprietary) | YAML (declarative) | Python + YAML | YAML (DSL) |
| Enforcement point | API gateway, K8s admission, CI | Terraform Cloud/Enterprise | K8s admission controller | CI/CD pipeline | Cloud API (reactive) |
| K8s native | Via Gatekeeper | No | Yes (CRDs) | Via CI scanning | No |
| IaC scanning | Conftest (Terraform, K8s) | Terraform plans | No | Terraform, CloudFormation, K8s, Helm | No |
| Cloud posture | Via integrations | Via Terraform | No | Yes (AWS, GCP, Azure) | Yes (multi-cloud) |
| Learning curve | High (Rego is unique) | Medium | Low (YAML) | Low | Medium |
| Community | CNCF Graduated | HashiCorp ecosystem | CNCF Incubating | Large OSS community | Strong OSS community |
| Best for | Universal policy engine | Terraform governance | K8s policy enforcement | Shift-left IaC scanning | Cloud compliance automation |
Framework Mapping Matrix
| Control Domain | SOC 2 (TSC) | ISO 27001 (Annex A) | PCI DSS 4.0 | NIST 800-53 | Automatable? |
|---|---|---|---|---|---|
| Access control | CC6.1-CC6.3 | A.9 | Req 7, 8 | AC family | Yes (IAM policies, RBAC checks) |
| Encryption | CC6.1, CC6.7 | A.10 | Req 3, 4 | SC-12, SC-13 | Yes (config scanning) |
| Logging & monitoring | CC7.1-CC7.3 | A.12.4 | Req 10 | AU family | Yes (log pipeline checks) |
| Change management | CC8.1 | A.12.1 | Req 6.5 | CM family | Yes (GitOps, PR policies) |
| Vulnerability mgmt | CC7.1 | A.12.6 | Req 6, 11 | RA-5, SI-2 | Yes (scanner integration) |
| Incident response | CC7.3-CC7.5 | A.16 | Req 12.10 | IR family | Partial (runbook automation) |
| Asset inventory | CC6.1 | A.8 | Req 2, 12 | CM-8 | Yes (resource discovery) |
| Network security | CC6.1, CC6.6 | A.13 | Req 1 | SC family | Yes (security group analysis) |
| Data protection | CC6.1 | A.8.2 | Req 3 | MP family | Partial (classification tools) |
| Business continuity | A1.1-A1.3 | A.17 | Req 12.10 | CP family | Partial (backup verification) |
Compliance Maturity Model
| Level | Name | Characteristics | Audit Experience |
|---|---|---|---|
| 1 | Manual | Spreadsheets, screenshots, periodic reviews | Weeks of preparation, evidence gathering |
| 2 | Documented | Policies written, some automated checks, evidence in docs | Days of preparation, some gaps |
| 3 | Automated Checks | Policy-as-code in CI, IaC scanning, automated evidence | Hours to generate report, few gaps |
| 4 | Continuous Compliance | Real-time policy enforcement, continuous monitoring, drift detection | On-demand report generation |
| 5 | Autonomous | Self-healing compliance, automated remediation, predictive risk | Audit is a dashboard query |
Policy-as-Code Pipeline
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Policy Repo │ │ App / IaC │ │ Cloud │
│ (OPA/Rego, │ │ Repository │ │ Runtime │
│ Kyverno, │ │ │ │ │
│ Checkov) │ │ │ │ │
└──────┬───────┘ └──────┬───────┘ └──────┬───────┘
│ │ │
▼ ▼ ▼
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Policy Tests │ │ Pre-commit │ │ Continuous │
│ (unit tests │ │ / CI Scan │ │ Monitoring │
│ for rules) │ │ (Checkov, │ │ (Cloud │
│ │ │ Conftest) │ │ Custodian) │
└──────┬───────┘ └──────┬───────┘ └──────┬───────┘
│ │ │
▼ ▼ ▼
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Policy │ │ Admission │ │ Drift │
│ Distribution │ │ Control │ │ Detection │
│ (GitOps) │ │ (Gatekeeper │ │ & Auto- │
│ │ │ / Kyverno) │ │ Remediation │
└──────┬───────┘ └──────┬───────┘ └──────┬───────┘
│ │ │
└───────────────────┼───────────────────┘
▼
┌──────────────────┐
│ Compliance │
│ Dashboard │
│ (evidence store,│
│ audit reports) │
└──────────────────┘
Audit Automation Workflow
Continuous Audit Pipeline
│
├── Evidence Collection (automated)
│ ├── IaC scan results --> evidence store
│ ├── Admission controller logs --> evidence store
│ ├── Cloud config snapshots --> evidence store
│ ├── Access review exports --> evidence store
│ └── Vulnerability scan reports --> evidence store
│
├── Control Validation (automated)
│ ├── Map evidence to control framework
│ ├── Evaluate pass/fail per control
│ ├── Flag controls requiring manual review
│ └── Calculate compliance score
│
├── Reporting (automated)
│ ├── Generate per-framework report (SOC 2, ISO, PCI)
│ ├── Highlight gaps and remediation steps
│ ├── Track compliance score over time
│ └── Export for auditor consumption
│
└── Remediation (semi-automated)
├── Auto-remediate drift (Cloud Custodian actions)
├── Create tickets for manual items
├── Escalate critical non-compliance
└── Track remediation SLAs
Strategic Recommendations
Treat policies like code. Store them in git, write unit tests, require code review, deploy through CI/CD. Policy drift is as dangerous as infrastructure drift.
Start with Checkov in CI. It requires zero infrastructure, scans Terraform/CloudFormation/Kubernetes manifests, and maps findings to compliance frameworks out of the box. You can have shift-left compliance in an afternoon.
Use Kyverno for Kubernetes. Its YAML-native approach means platform engineers can write policies without learning a new language. Use it for image signature verification, label enforcement, resource limits, and network policy requirements.
Separate detection from enforcement. Start in audit mode (detect and report), then graduate to enforcement after validating that policies do not block legitimate work. Nothing kills adoption faster than false positives blocking deployments.
Build a compliance evidence store. Automate the collection of scan results, configuration snapshots, and access reviews into a central store. When the auditor asks for evidence, it should be a query -- not a scramble.
Resources
- OPA / Gatekeeper Documentation
- Kyverno Documentation
- Checkov by Bridgecrew
- Cloud Custodian Documentation
- NIST Cybersecurity Framework
:::