Skip to main content
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 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 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.

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.

AspectWritten policy documentPolicy as code
FormProse in a document, approved by a council or executiveDeclarative rules in a repository, approved through code review
When it appliesWhen someone remembers it and interprets it the same wayOn every request the engine evaluates
Change processEdit, re-approve, recirculatePull request, review, automated tests, deploy
TestingNone before rolloutTests against sample requests before deploy
EvidenceAttestations and periodic samplingA decision log for each request
Strong atIntent, judgment, scope, accountabilityPrecise, repeatable decisions at scale
Weak atConsistent enforcement across many systemsAmbiguity, 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. 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.
  • 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.
  • Metadata gates in CI/CD. A deployment pipeline fails if a new table has no owner, description, or 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.

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.
  • 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 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 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 and data catalog hold the definitions and classifications such rules depend on, and interactive lineage shows where governed data flows.

For AI agents, Dawiso 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

See it in action

AI Governance

Trust and transparency in your AI use cases.

A cookie a day keeps bad UX away.

We use cookies to personalize content, ads and to analyze our traffic. We also share information about your use of our site with our advertising and analytics partners who may combine it with other information that you've provided to them or that they've collected from your use of their services. By clicking "Accept All", you allow us to use cookies for analytics and ads via Google Tag Manager. You can also customize cookies.

Customize Consent Preferences

We use cookies to personalize content, ads and to analyze our traffic. We also share information about your use of our site with our advertising and analytics partners. Privacy Policy

Necessary cookies allow core website functionality such as user login and account management. The website cannot be used properly without strictly necessary cookies.

Functionality cookies are used to remember visitor information on the website, eg. language, timezone, enhanced content.

Analytics cookies are used to see how visitors use the website, eg. analytics cookies. Those cookies cannot be used to directly identify a certain visitor.

We use Microsoft Clarity to see how you use our website (including heatmaps and session replays) so we can improve it. By using our site, you agree that we and Microsoft can collect and use this data. See our Privacy Policy for details.

Advertisement cookies are used to identify visitors between different websites, eg. content partners, banner networks. Those cookies may be used by companies to build a profile of visitor interests or show relevant ads on other websites.