# Open Policy Agent turns client access rules into tested code

> When the decision is separated from where it is enforced, authorisation rules can be tested, reviewed and updated without a restart.

Original: https://fdetimes.net/en/tools/open-policy-agent-tested-access-rules/

Picture the rules for "who can do what" at an enterprise client. Some live in if/else statements in the backend, some in the gateway configuration, and the rest in the head of one person in the security team.

Then comes the first meeting after you arrive to deploy. Quite possibly the client will not ask about features at all, but this: how do you prove the new system does not grant the wrong access?

Open Policy Agent (OPA) is a tool worth considering for that question. The official documentation describes OPA as an open-source, general-purpose policy engine that unifies policy enforcement across the entire stack; the project reached CNCF graduated status on 29 January 2021.

For you, the real value lies elsewhere: OPA turns authorisation rules into tested code. What the client signs off on is then no longer a Word document but a file that can be reviewed, tested and deployed like everything else.

## The core idea is to separate who asks from who decides

According to the OPA documentation, the tool decouples decision-making from enforcement. The application, the enforcement point, sends OPA structured input, such as JSON describing the user, the action and the resource. OPA returns a decision. The application complies.

On a client site, this separation solves a very specific problem. The security team can read and change policy without touching your service code, and you can change the service without rewriting the rules. Each side keeps its own part, and the boundary between them is just a JSON document.

Policies are written in Rego, pronounced "ray-go", a language built specifically to express policy over complex hierarchical data. Rego is inspired by Datalog and extends it to structured document models such as JSON. In other words, you write conditions, not procedures.

## A small policy, end to end

Take a client that wants this rule: analysts may read reports but not delete them. A minimal policy looks like this:

```rego
package httpapi.authz

default allow := false

allow if {
    input.method == "GET"
    input.user.role == "analyst"
    input.path[0] == "reports"
}
```

The most important line is `default allow := false`. The Rego documentation explains that a default value is used when all rules with the same name are undefined. If no condition matches, the answer is denial. This is the default-deny pattern, and it should be the first thing you write in every policy for a client.

Now for the part that makes OPA worth using. The `opa test` command runs every rule prefixed with `test_`:

```rego
package httpapi.authz_test

import data.httpapi.authz

test_analyst_reads_reports if {
    authz.allow with input as {"method": "GET", "path": ["reports"], "user": {"role": "analyst"}}
}

test_analyst_cannot_delete if {
    not authz.allow with input as {"method": "DELETE", "path": ["reports"], "user": {"role": "analyst"}}
}
```

The second test is the one the client cares about. Proving the right people get in is easy. Proving the wrong people are blocked is what the security team wants to see in the review.

OPA also produces a coverage report, listing every line that was and was not evaluated in the Rego files passed in; you turn it on with `opa test --coverage`. Every unevaluated line is a branch of the rules nobody has checked. Put the report into CI and put it on screen during the demo.

## Where should OPA sit in the client's infrastructure?

The deployment documentation recommends running OPA as close as possible to the enforcement points. A sidecar, or an agent running on the node, is one of the two main models. The reason is simple: every request has to ask OPA, so each extra network hop adds latency.

But once OPA runs in many places, policy has to be distributed. OPA uses bundles for this. A bundle is a gzipped tarball (`.tar.gz`) containing policy, data or both, pulled from a remote HTTP server. New policy and data are loaded on the fly, without restarting OPA.

If the client runs Kubernetes, there is a ready-made shortcut. Gatekeeper is a validating and mutating webhook that enforces CRD-based policies using OPA. It blocks at the moment a resource is created, before a bad configuration has a chance to run.

## Limits to tell the client up front

OPA only decides; it does not enforce. If a service forgets to ask OPA, or ignores the answer, however elegant the policy, it means nothing. Wiring each enforcement point to OPA is still your job, and that is where the bugs usually are.

Rego is also one more language for the client's team to learn. Its declarative, Datalog-style approach is quite unlike Python or Java, so the operations team that stays after you leave needs a thorough handover. Finally, the bundle model requires an HTTP server to serve policy, which is one more component to run and secure.

**Key point:** Proving the right people get in is easy; proving the wrong people are blocked is what policy is for.

## What to learn first, and whom to show

A sensible order: the default-deny pattern, then tests with `opa test`, then coverage, and only then bundles and Gatekeeper. The first two steps are enough for you to propose OPA in an architecture meeting with a client next week.

When reading FDE job postings, there is a hypothesis worth testing for yourself: roles serving clients in heavily regulated industries, or that mention policy-as-code outright, are likely to be where this skill is valued.

Check it against a few postings you are targeting. On your CV, do not write "knows OPA"; write how many authorisation rules you moved into Rego, with how many tests and what coverage.

Clients trust a system when they see it refuse at the right moment. With OPA, you can show them that in a single test run.

**Try this week:**

- Install OPA, write a default-deny policy for a small API and three tests, at least one of which proves a request is denied.
- Run opa test --coverage and add tests until no line of Rego is left unevaluated.
- Take an authorisation rule scattered across if/else statements in an old project, rewrite it in Rego and record the differences in the README.

## Sources

- [Open Policy Agent documentation](https://www.openpolicyagent.org/docs)

- [Open Policy Agent (OPA) – CNCF project page](https://www.cncf.io/projects/open-policy-agent-opa/)

- [Policy Language – Open Policy Agent](https://www.openpolicyagent.org/docs/policy-language)

- [Policy Testing – Open Policy Agent](https://www.openpolicyagent.org/docs/policy-testing)

- [Deploying OPA – Open Policy Agent](https://www.openpolicyagent.org/docs/deploy)

- [Bundles – Open Policy Agent](https://www.openpolicyagent.org/docs/management-bundles)

- [Gatekeeper – Introduction](https://open-policy-agent.github.io/gatekeeper/website/docs/)
