Skip to main content
XenonStack

DevOps

XenonStack Blog Compliance as Code: OPA, OSCAL and CI/CD Guide

| Published | Updated

Key takeaway

  • Compliance as code explained for CIOs and CISOs: how OPA, Gatekeeper, Conftest and NIST OSCAL fit, an adoption sequence and where it falls short.

Compliance as code turns written compliance requirements into versioned rules and automated tests that run in the delivery pipeline and against live systems. The result is a check on every change, evidence generated as a by-product, and a report that does not depend on a manual audit sprint.

This guide is for CIOs, CISOs, platform leads and architects who must decide where to start. It covers how the approach works, what Open Policy Agent (OPA) and OSCAL each contribute, where it fits and where it does not, and a sequence for adopting it. For the wider pipeline view, see our guide to DevSecOps pipeline, tools and governance.

What is compliance as code?

Compliance as code uses programmatic methods to implement, validate, remediate, monitor and report on the standards an organisation must meet. A requirement such as "storage must be encrypted" is written once as a rule, kept in version control and evaluated automatically against infrastructure definitions, pipelines and running resources.

It does not replace security work. Security is not the same as compliance: a rule set proves that the encoded controls were checked, not that the system is safe against every threat.

How does compliance as code work?

Teams translate each requirement into three separate things: the specification (what must be true), the implementation (the rule that checks it) and the enforcement (where the rule runs and what happens on failure). Keeping them separate lets an auditor read the specification while engineers maintain the rule.

Left: control catalogue, baseline, rule and test. Right: pipeline check with Conftest, admission control with Gatekeeper, audit of existing resources and stored evidence.
StageWhat happensTypical mechanism
DefineRequirements and internal policies are mapped to controlsControl catalogue and baseline (for example in OSCAL)
WriteControls become rules and tests in a policy languageRego policies for OPA
EnforceRules run at commit, build, deploy and admissionConftest in CI, Gatekeeper in Kubernetes
MonitorRunning systems are checked against the same rulesAudit of existing resources, scheduled scans
EvidenceResults are stored with who, what, when and whyAssessment results, logs, decision records

What do Open Policy Agent and OSCAL each do?

Open Policy Agent (OPA) describes itself as an open source, general-purpose policy engine that unifies policy enforcement across the stack, and it is a graduated Cloud Native Computing Foundation project under the Apache Licence 2.0. Software sends structured input such as JSON to OPA and receives a decision, so policy is separated from the code that enforces it. Policies are written in Rego, a declarative language built for hierarchical data.

OSCAL, the Open Security Controls Assessment Language, is led by NIST with industry. It provides open, machine-readable formats in XML, JSON and YAML for control-based risk assessment. Its models sit in three layers: a control layer (catalogue, profile and control mapping), an implementation layer (system security plan and component definition) and an assessment layer (assessment plan, assessment results and plan of action and milestones).

Standard or toolRoleUse it whenSource
OPA and RegoDecides allow, deny or other outcomes from structured inputYou need enforcement at Kubernetes admission, in CI/CD or on Terraform plansOPA documentation
GatekeeperValidating and mutating Kubernetes webhook that enforces OPA policies as custom resources; its audit feature lists resources already violating a policyYou run Kubernetes and want policy before and after deploymentGatekeeper documentation
ConftestTests structured configuration (Kubernetes, Terraform, Tekton and others) with RegoYou want policy checks in a pipeline before anything deploysConftest
OSCALMachine-readable controls, plans and assessment resultsYou must exchange control baselines and assessment evidence in a standard formatNIST OSCAL layers

Why do enterprises need compliance as code?

Does it fit CI/CD pipelines?

Yes. When checks run on every commit, developers see compliance status while the change is still small, and non-compliant changes are stopped before release. Our guide to infrastructure as code in a CI/CD pipeline and the explainer on infrastructure as code best practices cover the pipeline side.

Does it improve the audit trail?

An audit trail should answer what changed, who changed it, who requested and reviewed it, why, whether it was tested, and when. Rules and policies held in version control give most of that history for free. Automated evidence collection also shortens audit preparation, because the same rules run every time.

Does it close the knowledge gap?

Compliance documents are long and hard to read. Encoding them as rules means engineers do not need to interpret every clause, and a report listing, for example, twenty non-compliant applications can be ranked by business criticality.

Does it help teams move faster?

Rules can be tested, versioned and grouped into bundles that are shared across teams. Checks can be re-run after each small change, and security teams release a new bundle when a requirement changes instead of briefing every team.

How should an enterprise adopt compliance as code?

StepActionOutcome to look for
1Pick one regulation or internal standard and a handful of high-value controlsA short, agreed control list
2Write each control as a rule and test it with passing and failing examplesRules that behave as the control owner expects
3Run rules in CI in advisory mode, using a tool such as ConftestVisible findings without blocking delivery
4Run the same policies in a test environment, then in production, in that orderNo surprise blocks in production
5Switch selected rules to blocking and add admission control where you run KubernetesNon-compliant changes stopped before release
6Store results as evidence and review them on a scheduleAudit-ready history and a rule review cycle

For governance of AI agents that act on enterprise systems, the same idea applies to agent actions. ElixirData Context OS keeps governed context, versioned policy and decision traces in one decision layer above AI agents, and AgentGRC keeps autonomous agent actions inside policy, with the audit trail risk teams need. The ElixirData Compliance Agent page describes monitoring of activity against regulations, internal rules and contract terms.

What are the best practices?

  1. Start early in the lifecycle (shift left) so developers see failures while changes are small.
  2. Test every rule with cases that should pass and cases that should fail, including edge cases.
  3. Deploy and test policies in a test environment before production.
  4. Review rules whenever a standard, regulation or internal policy changes.
  5. Keep security assessments of applications and infrastructure; rules do not replace them.
  6. Have people review automated reports instead of trusting them blindly.
  7. Do not encode every requirement. Some controls need human judgement.

When is compliance as code not the right answer?

Controls that depend on judgement, culture or physical processes, such as training quality or vendor relationships, cannot be tested by code alone. Rules written without the control owner can also drift from what the requirement means. Treat passing checks as evidence for those controls you have encoded, and keep manual review for the rest. Our guide on the GDPR gives a compliance checklist of the kind that mixes both.

Which video explains continuous compliance automation?

Compliance automation with AI agents, from ElixirClaw: SOC 2 audit evidence in 4 days, not 6 weeks.

To map one compliance decision your agents and pipelines should make, and the context and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

What is compliance as code?

Compliance as code means writing compliance requirements as machine-readable rules and tests that run automatically in the delivery pipeline and against running systems, so that checks, evidence and reports come from the same versioned source.

Is compliance as code the same as policy as code?

They overlap. Policy as code is the technique of writing policy in a language such as Rego and enforcing it through an engine such as OPA. Compliance as code applies that technique to a named regulation, standard or internal control, and adds evidence and reporting.

What is OSCAL and how does it relate to OPA?

OSCAL is a NIST-led set of open, machine-readable formats (XML, JSON and YAML) for control catalogues, baselines, system security plans and assessment results. OPA is a policy engine that makes decisions. OSCAL describes what must be met and what was found; OPA enforces rules at decision points. They complement each other and neither replaces the other.

Where does OPA enforce policy in a pipeline?

Per its own documentation, OPA can be used for Kubernetes admission control, CI/CD pipelines and Terraform plan validation, among other uses. Gatekeeper applies OPA policies to Kubernetes resources, and Conftest tests configuration files such as Kubernetes manifests, Terraform code and Tekton pipelines.

Does compliance as code mean we are compliant?

No. Automated checks cover only the controls you have encoded. Security is not the same as compliance, and assessors still need to review the rules, the evidence and the controls that cannot be tested by code.

Who should own the rules?

Compliance, security and engineering should own them jointly. Compliance interprets the requirement, security approves the control, and engineering implements, tests and versions the rule like any other code.

What next?

Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.

Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.

Table of Contents

dr-jagreet-gill

Dr. Jagreet Kaur

Chief AI Officer, XenonStack

LinkedIn profile

Dr. Jagreet Kaur specializing in Generative AI for synthetic data, Conversational AI, and Intelligent Document Processing. With a focus on responsible AI frameworks, compliance, and data governance, she drives innovation and transparency in AI implementation

Get the latest articles in your inbox

Subscribe Now

Interested in solving your challenges with the XenonStack team?
Get Started

Tell us about you and your priorities so we can prepare the right conversation. It takes about 2 minutes.

Please Select your Industry
Banking
Fintech
Payment Providers
Wealth Management
Discrete Manufacturing
Semiconductor
Machinery Manufacturing / Automation
Appliances / Electrical / Electronics
Elevator Manufacturing
Defense & Space Manufacturing
Computers & Electronics / Industrial Machinery
Motor Vehicle Manufacturing
Food and Beverages
Distillery & Wines
Beverages
Shipping
Logistics
Mobility (EV / Public Transport)
Energy & Utilities
Hospitality
Digital Gaming Platforms
SportsTech with AI
Public Safety - Explosives
Public Safety - Firefighting
Public Safety - Surveillance
Public Safety - Others
Media Platforms
City Operations
Airlines & Aviation
Defense Warfare & Drones
Robotics Engineering
Drones Manufacturing
AI Labs for Colleges
AI MSP / Quantum / AGI Institutes
Retail Apparel and Fashion

Continue