Policy as Code: How It Works, Tools and Rollout

Policy as code turns security, compliance, and operational rules into version-controlled logic that runs automatically and leaves an audit trail. The piece explains the core parts, the main tools, and how to roll out rules safely so teams can protect pipelines, clusters, and business workflows without slowing delivery.

Hubert Olkiewicz[email protected]
LinkedIn
7 min read

Policy as code means writing your security, compliance and operational rules as machine-readable, version-controlled code that is evaluated automatically, instead of keeping them in documents people are expected to remember. OWASP’s DevSecOps Guideline describes it in these terms. Every change, request or deployment is checked against the rules before it takes effect, and the outcome is logged. For a company building or buying enterprise systems, the practical question is where the rules live: in a pipeline, in a cluster, or inside the business application itself. We build the last kind, so we cover all three here.

Bitecode
Put Business Rules Into Your Software
Bitecode builds tailored business software with modular components, supporting workflows where payment limits, approvals and audit rules need to be applied.
Visit

TL;DR

  • Policy as code has three parts: rules in a policy language, an engine that evaluates them against structured input, and an enforcement point that acts on the decision.
  • Open Policy Agent (OPA) with its Rego language is the common general-purpose engine. Kyverno, Sentinel, Checkov and cloud-native services cover narrower scopes.
  • Start every new rule in advisory (audit-only) mode, then promote it to blocking once false positives are gone.
  • Pipeline policies protect infrastructure. Business rules such as payment limits, approvals and audit evidence belong in the application, and that is where custom, modular software matters.
  • Treat policy changes like application changes: pull requests, tests, review and release.

What policy as code actually is

A traditional policy lives in a PDF or a wiki page. It says, for example, that storage must not be publicly readable, or that payments above a threshold need a second approver. Compliance then depends on people reading it, remembering it and applying it under deadline pressure.

Policy as code moves that rule into an executable file. IBM’s overview describes the flow: developers write policy in a language such as Rego, submit it to version control, and it goes through pull requests and code review. A policy engine parses the code, receives normalized input describing the proposed change, checks the rules against it, combines the outcomes into one decision and returns that decision with messages and severity information.

The result is a rule that is consistent everywhere it runs, reviewable like any other change, and testable before rollout. OWASP also notes that every decision can be logged with the input, the policy version and the outcome, which gives auditors structured evidence rather than screenshots.

What policy as code actually is

The three moving parts

It helps to separate the pieces, because tool choices become clearer once you do:

  1. The rule. A statement in a policy language, such as “deny any workload in the default namespace.”
  2. The engine. The component that evaluates the rule against structured input, usually JSON or YAML.
  3. The enforcement point. The place that acts on the decision: a CI job that fails a build, a Kubernetes admission controller that rejects a deployment, an API gateway that returns a denial.

The engine and the rule are portable. The enforcement point is where you decide how much friction you are willing to add and where.

Where policies get enforced

OWASP lists five enforcement points, and most organizations end up using several of them:

Enforcement point What it does Typical rule
CI/CD gates Blocks merges or builds that violate policy No build ships with a known critical finding
Admission control Kubernetes rejects non-compliant workloads at deploy time No privileged containers
Infrastructure as code Guardrails on Terraform or Kubernetes definitions before provisioning No public storage buckets
Cloud guardrails Provider-level controls such as service control policies Restrict regions or services
API authorization An external engine decides who may call what Only finance roles may approve refunds

The first four protect infrastructure and delivery. The fifth is where policy as code begins to touch business logic, and it is the point where many engineering-led programs stall, as the section on business rules below explains.

Tools and frameworks, by the job they do

There is no single best policy as code framework. The right question is which layer you are protecting. A tool comparison from Spacelift groups the field by stage, and the grouping is a useful way to shortlist:

  • Pre-commit and pull request scanning of infrastructure code: Checkov.
  • Terraform plan and apply enforcement: HashiCorp Sentinel, which is embedded in Terraform Cloud and Terraform Enterprise.
  • Kubernetes admission control: Gatekeeper and Kyverno.
  • Cloud-provider guardrails: AWS service control policies, Azure Policy and Google Cloud Organization Policy.
  • Application authorization: AWS Cedar.
  • Cross-layer general-purpose engine: Open Policy Agent.

Open Policy Agent and Rego

OPA is an open source project that graduated within the Cloud Native Computing Foundation. Snyk’s write-up notes it is widely used for application policies in microservice architectures and, through Gatekeeper, as a Kubernetes admission controller. Its language, Rego, is high-level and declarative. Because the engine is domain-agnostic, Harness points out that the same engine can block a public S3 bucket in Terraform, reject a latest tag in a Dockerfile or enforce mutual TLS in an Envoy proxy.

The trade-off is that Rego is a new language. Teams need someone who can write and test it well.

Kyverno and Cedar

Kyverno lets teams write cluster admission policies in native Kubernetes YAML, which removes the language learning curve for teams already fluent in manifests. Harness also notes that Kubernetes 1.30 brought CEL, the Common Expression Language, into the API for stateless validations.

AWS Cedar takes a different path from OPA. It enforces a strict schema up front, catches attribute errors when a policy is written and supports formal verification. It suits application authorization where you want mathematical assurance about who can do what.

Choosing without overthinking it

If your problem is Kubernetes only and your team lives in YAML, a Kubernetes-native tool is the shortest path. If your problem is Terraform, use what integrates with your plan and apply workflow. If you need one policy language across pipelines, clusters, gateways and services, a general-purpose engine such as OPA earns its learning curve. Plan for more than one tool over time: enforcement points differ, and so do their best-fit engines.

An OPA example you can read in a minute

The OPA Kubernetes primer shows the pattern. A deny rule rejects a request and returns a message when the conditions in its body are true. The policy then aggregates all deny rules into an admission response that is either allowed or not.

A minimal rule in the same spirit looks like this:

package kubernetes.admission

deny contains msg if {
  input.request.object.metadata.namespace == "default"
  msg := "workloads may not be deployed to the default namespace"
}

Read it as plain logic: if the requested namespace is default, the request is denied and the message explains why. Testing follows the same code-first discipline. The primer’s unit-test example defines an unsafe input, evaluates the policy with that input substituted in, checks that the expected message appears in the deny set and runs the whole thing with the opa test command, which reports a pass or fail count.

Two habits come out of this small example. First, every rule ships with at least one test of an input it should reject and one it should allow. Second, messages are written for the developer who will see them, because a denial without an explanation becomes a support ticket.

Rolling out without breaking delivery

The failure mode of policy as code is rarely technical. It is a blocking rule that breaks a legitimate deployment on a Friday, followed by a quiet decision to switch enforcement off. Several sources converge on the same remedy.

Start in advisory mode. OWASP recommends introducing rules as non-blocking, for example with warn rules or Conftest’s no-fail flag, running them for two sprints without significant false positives, then promoting them to deny. Kestra puts the reason bluntly: rules that block on day one tend to be disabled on day two. Its suggested sequence is audit-only first, measure how many existing resources would fail, fix them or grant exceptions, then block.

Gather requirements before writing Rego. Fannie Mae’s platform team described its process on the HashiCorp blog: requirements, development, testing, review and release, with stakeholders from platform, security, compliance, application development and FinOps. Requirements gathering was called the most important phase, and the goal was policies that reduce risk without hindering developers.

Keep policy logic narrow. The CNCF’s guidance on securing OPA deployments says Rego should evaluate conditions and make access decisions, while data fetching and processing happen outside the engine. It also warns that functions such as http.send can let a policy exfiltrate sensitive data if left unrestricted. Run policy changes through the same CI/CD pipeline as application code so they are tested and reviewed before production.

A practical first quarter looks like this:

  1. Pick three rules that auditors or incident reviews have already asked for.
  2. Write them with tests and run them in advisory mode on one team’s pipeline.
  3. Review the findings weekly and fix the rule or the resource.
  4. Promote to blocking only after a clean run, with a documented exception path.
  5. Expand to the next enforcement point.

Why the benefits are real, and where they stop

OWASP summarizes the case: policy as code makes policy consistent, testable, auditable and hard to skip silently. Red Hat adds that it centralizes rules and control and provides traceability for compliance. One central policy library can serve thousands of pipelines and clusters at once, and a change to a rule arrives with a diff, a reviewer and a history.

There is also a scope question. Pipeline and cluster policies answer questions such as “is this infrastructure configured safely?” They do not answer “may this controller approve this transfer?” or “does this ledger entry carry the evidence an auditor will ask for?” Those decisions sit inside business systems, and a policy file bolted on from outside can only see what the application chooses to send it. Enterprises with several disconnected systems feel this sharply: Tyk describes sprawl from siloed departments and acquisitions as a source of integration challenges for policy programs.

Why the benefits are real, and where they stop

Business rules are policy too

For finance teams, fintechs and SaaS providers, the policies that carry the most risk are business policies: approval chains, spending limits, who can release a payment, which currencies a workflow may touch, what must be recorded for an audit. Treating these with the same discipline as infrastructure rules means three things:

If you are mapping controls across a whole organization, the enterprise security compliance checklist and the IT automation governance checklist give you the control inventory that policy rules should encode. Teams working with digital assets can start from the crypto compliance framework checklist.

Our perspective

Most policy as code programs begin in the pipeline, and that is a sensible place to start. The trouble comes later. The rules that matter most to executives, such as who may move money, which approvals a workflow requires and what evidence exists for each decision, live in the business application, and a foundation that was not designed for that gets retrofitted at high cost.

This is why we build enterprise systems from modular, ready-made components, with up to 60% of the baseline system pre-built. The financial, automation, AI assistant, CRM and token modules are designed to be audit-ready, multi-currency and self-hostable, so approvals, limits and decision records are part of the structure from the first sprint rather than added after the first audit.

Our view is that policy logic should accelerate work without accelerating chaos. Keep rules explicit, version them, test them and record every decision. A modular foundation that you control makes that discipline cheaper than starting from a blank repository or relocating complexity into a vendor’s black box.

Bitecode

Building policy-aware business systems with us

If your policy program has reached the point where approvals, financial controls and audit evidence need to live in the application, we can start from a modular base instead of a greenfield build. Our Financial Module covers accounting, audit and subscriptions, and our Automation Module and AI Assistant handle workflow automation on top of it. For a scoped first release, we deliver small MVPs as well as larger software projects; pricing is set per engagement.

Bitecode

To discuss a system for your own workflows, start from our custom business software page or the Bitecode home page.

Checklist before you commit to a policy as code framework

  • Scope: Which enforcement points are in play now: pipeline, cluster, cloud account, API, business application?
  • Skills: Who will own the rules, and can they write and test them?
  • Rule inventory: Which three to five rules are already demanded by auditors or past incidents?
  • Modes: Does every rule have an advisory stage and an exception path?
  • Evidence: Does each decision record the input, the policy version and the outcome?
  • Separation: Is policy logic kept apart from application logic and data fetching?
  • Delivery security: Does the pipeline itself meet the standard you expect? See our guides on secure software deployment and software development security.

FAQ

What is the difference between policy as code and infrastructure as code?

Infrastructure as code describes what should be built. Policy as code describes the rules that what is built must satisfy. The two work together: a policy engine evaluates the infrastructure definition before it is provisioned and allows, warns or denies it.

Which policy as code tool should we start with?

Match the tool to the layer. Kubernetes-native teams often begin with a Kubernetes-focused tool, Terraform shops with a policy framework integrated into their workflow, and teams that need one language across layers with Open Policy Agent. Begin with a handful of rules in advisory mode on one team before standardizing.

Do we need to learn Rego?

Only if you choose a Rego-based engine such as OPA. Rego is declarative, so rules read as conditions that trigger a denial, and the engine’s unit-test framework lets you check each rule against sample inputs. Kubernetes-native tools let teams write policies in YAML instead.

Can policy as code cover business rules like payment approvals?

Yes, with a caveat on where the rule runs. The decision has to be made where the business action happens, so approvals and limits are best built into the application with an audit trail, or enforced through an authorization engine the application calls. Either way, the rule is versioned, tested and logged.

Sources

Articles

Dive deeper into the practical steps behind adopting innovation.

Software delivery6 min

From idea to tailor-made software for your business

A step-by-step look at the process of building custom software.

AI5 min

Hosting your own AI model inside the company

Running private AI models on your own infrastructure brings tighter data & cost control.

Hi!
Let's talk about your project.

this helps us tailor the scope of the offer

Przemyslaw Szerszeniewski's photo

Przemyslaw Szerszeniewski

Bitecode co-founder

LinkedIn