---
title: "What Is Policy as Code in Data Governance? | Dawiso"
description: "Policy as code turns data governance rules into versioned, tested code that a policy engine enforces on every request. How it works, examples, and limits."
url: "https://www.dawiso.com/glossary/policy-as-code"
language: "en"
---

November 26, 2026

policy as codepolicy as code data governancegovernance as codeOPA Regopolicy as code examplespolicy as code for AI agentsdata access policy

# What Is Policy as Code in Data Governance?

**Policy as code** is the practice of writing governance rules as machine-readable code, keeping that code in version control, testing it like software, and letting a policy engine evaluate it every time someone, or something, asks to read or change data. The rule "mask personal data for anyone who is not the owner" stops being a sentence in a PDF and becomes a decision the platform makes on each query, the same way every time, with a log entry to prove it.

The idea comes from infrastructure and security engineering. In data governance it answers a familiar gap: a [data governance policy](https://www.dawiso.com/glossary/data-governance-policy) states what should happen, but nothing in the document makes it happen. Policy as code closes that gap for rules precise enough to be computed.

TL;DR

**Policy as code** expresses governance rules in a declarative language, stores them in version control, tests them, and deploys them to a policy engine that answers allow, deny, or mask at request time and logs each decision. The best-known engine is Open Policy Agent (OPA), whose language is Rego. It works for rules that can be stated precisely, such as masking [PII](https://www.dawiso.com/glossary/pii-personally-identifiable-information) by classification, owner-approved access, or blocking an AI agent's write tools. Every such rule reads metadata (classification, ownership, purpose), so **the quality of your metadata sets the quality of your policy**. Rules that need legal or ethical judgment stay with people.

## What Is Policy as Code?

Policy as code separates the decision ("may this request see this column?") from the enforcement ("return the column, or a masked value"). The decision lives in one policy, and every system that needs the answer asks for it. When the approach covers more than access, for example blocking a release if a new table has no owner, teams often call it **governance as code**.

The most widely used engine is **Open Policy Agent (OPA)**, an open source, general-purpose policy engine. Its policies are written in **Rego**, a declarative language, and it is used across microservices, Kubernetes, CI/CD pipelines, and API gateways. The Cloud Native Computing Foundation announced OPA's graduation on February 4, 2021. Kyverno, another CNCF project, writes policies in YAML instead of a dedicated language. The approach matters more than the engine. A simplified Rego rule for the masking example:

```
package governance.masking

default show_raw := false

show_raw if input.column.classification != "pii"
show_raw if input.user.id in input.column.owners
```

The request arrives as `input`, including the column's classification and owners. The rule defaults to masked and shows raw values only when one condition holds. It depends on a classification tag and an owner list it does not create itself.

## How Policy as Code Works

A policy-as-code workflow follows the lifecycle of application code, plus one step that matters most to governance: the decision log.

1. **Author in version control.** A rule is committed to a repository with a reference to the written policy it implements.
2. **Review and test.** The rule owner and an engineer review the change. Unit tests feed the policy sample requests ("analyst asks for a PII column", "owner asks for it") and assert the expected decisions, so a change that unmasks data fails before it ships.
3. **Deploy to a decision point.** The approved policy is published to the policy engine (the policy decision point), so every enforcing service runs the same version.
4. **Enforce at request time.** When a query, API call, or agent tool call arrives, the enforcing system sends the request context to the engine and applies the answer: allow, deny, mask, or require approval.
5. **Log the decision.** The engine records the request, the policy version, and the result. Those logs are audit evidence and the raw material for an [audit trail review](https://www.dawiso.com/glossary/audit-trail-review).

Because the chain is versioned, you can answer what written documents rarely can: which rule was in force on a given date, who approved it, and how it decided a specific request.

## Policy as Code vs Written Policy

Policy as code does not replace the written policy. The written policy carries intent, scope, and accountability; the code implements the parts of it that can be computed. The table shows where each is strong.

| Aspect | Written policy document | Policy as code |
| --- | --- | --- |
| Form | Prose in a document, approved by a council or executive | Declarative rules in a repository, approved through code review |
| When it applies | When someone remembers it and interprets it the same way | On every request the engine evaluates |
| Change process | Edit, re-approve, recirculate | Pull request, review, automated tests, deploy |
| Testing | None before rollout | Tests against sample requests before deploy |
| Evidence | Attestations and periodic sampling | A decision log for each request |
| Strong at | Intent, judgment, scope, accountability | Precise, repeatable decisions at scale |
| Weak at | Consistent enforcement across many systems | Ambiguity, and anything outside its inputs |

For the layers in between, from policy statement to measurable rule to enforcing mechanism, see [policy vs rule vs control](https://www.dawiso.com/glossary/governance-rule-vs-policy-vs-control). In that vocabulary, policy as code is one way to build automated controls.

## Policy as Code Examples for Data

The rules that suit policy as code are narrow, high-volume, and stated in terms the engine can check:

- **Masking PII by classification.** Any column tagged as personal data returns masked values unless the requester is an owner or holds an approved grant. The rule reads the tag instead of listing columns, so a newly classified column is covered the moment it is tagged. Techniques are covered under [data masking](https://www.dawiso.com/glossary/data-masking).
- **Access by data owner approval.** Read access to a confidential dataset is allowed only if an unexpired approval record from the dataset's owner exists for this user. The engine checks the record; the owner makes the decision. This is the policy-based model in [data access management](https://www.dawiso.com/glossary/data-access-management).
- **Metadata gates in CI/CD.** A deployment pipeline fails if a new table has no owner, description, or [classification](https://www.dawiso.com/glossary/data-classification), which is cheaper than finding the gap in production.
- **Blocking an agent's write tool.** An AI agent may call read tools freely, but a tool that changes production records is denied without an explicit grant, and paused for human approval when the data is sensitive.

## Policy as Code for AI Agents

Agents raise the stakes because they turn model output into actions. In the 2026 Gartner CIO and Technology Executive Survey, only 17% of organizations have deployed AI agents so far, while more than 60% expect to do so within the next two years. Each agent that goes live makes tool calls that someone has to permit or refuse.

The OWASP Top 10 for LLM Applications 2025 names this risk **Excessive Agency** (LLM06): damaging actions that follow from unexpected, ambiguous, or manipulated model output, caused by excessive functionality, permissions, or autonomy. Its main preventions are to minimize the tools an agent has, apply least privilege, and require human approval for high-impact actions. The OWASP Top 10 for Agentic Applications 2026 adds tool misuse (ASI02) and identity and privilege abuse (ASI03), where agents reuse, escalate, or leak inherited credentials.

Those preventions are decisions that must run on every call, which is what policy as code does well. Connection-level authorization does not cover them: the Model Context Protocol authorization spec, which builds on OAuth 2.1 and is optional, governs access to an MCP server. Whether this agent may call this tool on this data right now is a finer decision. A per-tool policy (deny, allow, or require approval) answers it, and the decision log shows what the agent was allowed to do. The wider operating model is [AI agent governance](https://www.dawiso.com/glossary/ai-agent-governance).

## Why Metadata Quality Decides Policy Quality

Look again at the masking rule: it is correct only if the classification tag is correct. Almost every data policy reads metadata as its input:

- **Classification** decides what is sensitive. An untagged PII column is invisible to a rule that masks PII.
- **Ownership** decides who can approve. A dataset with a departed owner cannot grant access, or grants it through whoever answers first.
- **Definitions** decide scope. A rule about "customer data" depends on an agreed meaning of customer in the [business glossary](https://www.dawiso.com/glossary/business-glossary).
- **Lineage** decides reach. If a PII column is copied into a derived table without its tag, the rule protects the source and leaks the copy. [Data lineage](https://www.dawiso.com/glossary/data-lineage) shows where tags need to follow the data.

This is why policy as code fails quietly: a correct rule reading a missing tag returns "allow" and logs a clean decision. Two habits help. Design rules to fail closed, treating unclassified data as sensitive until someone classifies it, and track metadata coverage (classified, owned, defined) as a governance metric. The engine cannot be more accurate than the [metadata](https://www.dawiso.com/glossary/metadata-management) it reads.

## Limits of Policy as Code

- **Not every policy is codeable.** "Use customer data fairly" or a legitimate-interest balancing test needs judgment. Code can route such cases to a person; it cannot make the call.
- **Legal interpretation stays human.** Code captures one reading of a regulation. If that reading is wrong, the engine enforces the error consistently and at scale, so legal and privacy owners should review the rules that encode their interpretations.
- **Inputs are bounded.** The engine sees only what the request carries, not the purpose in a user's head.
- **Rules need owners and an exception path.** Policies with no named owners become a second, unreviewed rulebook, and without expiring exceptions people route around the engine.

## How Dawiso Helps

Policy engines decide; they depend on the metadata behind each decision being complete and current. Dawiso's [business glossary](https://www.dawiso.com/product/business-glossary) and [data catalog](https://www.dawiso.com/product/data-catalog) hold the definitions and classifications such rules depend on, and [interactive lineage](https://www.dawiso.com/product/interactive-data-lineage) shows where governed data flows.

For AI agents, [Dawiso Agent Studio](https://www.dawiso.com/product/ai-agent-studio) applies the same pattern to the agents themselves. Each tool an agent can use has a permission of Deny, Allow, or Approval required, and with Approval required the run pauses until an admin approves the call. Read and write tools are labeled, budgets and limits stop a run at its ceiling, and agent specs are versioned with an audit log of who changed what and when. Agents can also draft and check context such as glossary terms and classifications, then hand their work to the responsible owner for approval, which raises metadata coverage while the owner keeps the decision.

## Conclusion

Policy as code turns the computable part of data governance into versioned, tested rules that an engine enforces on every request and logs. It suits masking, owner-approved access, metadata gates, and agent tool permissions, and leaves judgment to people. Its accuracy is bounded by its inputs, so invest in classification, ownership, definitions, and lineage first.

## Sources

- Open Policy Agent - [OPA documentation](https://www.openpolicyagent.org/docs)
- Cloud Native Computing Foundation - [Cloud Native Computing Foundation Announces Open Policy Agent Graduation (February 4, 2021)](https://www.cncf.io/announcements/2021/02/04/cloud-native-computing-foundation-announces-open-policy-agent-graduation/)
- OWASP GenAI Security Project - [LLM06:2025 Excessive Agency](https://genai.owasp.org/llmrisk/llm062025-excessive-agency/)
- OWASP GenAI Security Project - [OWASP Top 10 for Agentic Applications for 2026](https://genai.owasp.org/resource/owasp-top-10-for-agentic-applications-for-2026/)
- Model Context Protocol - [Authorization specification (2026-07-28)](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization)
- Gartner - [What the 2026 Hype Cycle for Agentic AI Reveals](https://www.gartner.com/en/articles/hype-cycle-for-agentic-ai) (April 15, 2026)

[See it in action](https://www.dawiso.com/solutions/ai-governance)

AI Governance

Trust and transparency in your AI use cases.

### Table of contents

What Is Policy as Code?How Policy as Code WorksPolicy as Code vs Written PolicyPolicy as Code Examples for DataPolicy as Code for AI AgentsWhy Metadata Quality Decides Policy QualityLimits of Policy as CodeHow Dawiso HelpsSources

## Related Terms

### [OpenAI Frontier: The Data Governance Problem](https://www.dawiso.com/glossary/openai-frontier-data-governance)

OpenAI Frontier lets enterprises deploy AI agents across their data and systems. Learn the data governance problem this creates - and how a context layer solves it.

Read more →

### [Policy vs Rule vs Control in Data Governance](https://www.dawiso.com/glossary/governance-rule-vs-policy-vs-control)

Policy vs control in data governance: a policy states intent, a rule makes it measurable, and a control enforces or checks it. One example, start to audit.

Read more →

### [What Is a Data Governance Policy? Template and Examples](https://www.dawiso.com/glossary/data-governance-policy)

Without a written data governance policy, every team invents its own rules for data. See what a policy must include, with a template outline and examples.

Read more →

### [What Is Context Governance?](https://www.dawiso.com/glossary/context-governance)

Context governance keeps the meaning AI relies on trustworthy: owned, versioned, validated, and consistent. Learn how it differs from data governance.

Read more →

### [Data Governance](https://www.dawiso.com/glossary/data-governance)

Learn what data governance is, how to build a framework, and why it's essential for AI-ready organizations.

Read more →

### [What Is a Data Governance Framework?](https://www.dawiso.com/glossary/data-governance-framework)

A data governance framework is the roles, policies, processes, and metrics that put data governance into practice. Learn its components and how to build one.

Read more →
