Open Policy Agent turns a client's access rules into tested code
Moving the decision out of the code that enforces it lets authorization rules be tested, reviewed and updated without a restart.
In brief
- OPA separates making an authorization decision from enforcing it: the application asks, OPA answers.
- Policies are written in Rego and tested with opa test, with a coverage report, like ordinary code.
- Bundles update policies without restarting OPA; Gatekeeper brings OPA to Kubernetes.
At a typical enterprise client, the rules on who may do what live in three places. Some sit in if/else branches 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. The client may well skip the feature questions entirely and ask something harder: how can 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 graduated status at the CNCF on January 29, 2021.
For a forward deployed engineer, the real value lies elsewhere: OPA turns authorization rules into tested code. What the client signs off on is no longer a Word document but a file that can be reviewed, tested and deployed like everything else.
The core idea is to separate the one who asks from the one who decides
According to the OPA documentation, the tool decouples decision-making from enforcement. The application, acting as 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 owns its part, and the boundary between them is a single JSON document.
Policies are written in Rego, pronounced “ray-go”, a language built specifically for expressing policy over complex hierarchical data. Rego is inspired by Datalog and extends it to structured document models such as JSON. In practice, you write conditions rather than procedures.
A small policy, from start to finish
Take a client that wants this rule: analysts may read reports but may not delete them. A minimal policy looks like this:
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 every rule with the same name is undefined. If no condition matches, the answer is a refusal. This is the default-deny pattern, and it should be the first thing you write in any policy for a client.
Now for the part that makes OPA worth using. The opa test command runs every rule prefixed with test_:
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 that the right person gets in is easy. Proving that the wrong person is 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 to it; you turn it on with opa test --coverage. Each 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 each node, is one of the two main models. The reason is simple: every request has to ask OPA, so every extra network hop adds latency.
Once OPA runs in many places, though, policies have 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 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 raise with the client up front
OPA only decides; it does not enforce. If a service forgets to ask OPA, or ignores the answer, even an elegant policy counts for nothing. Wiring each enforcement point to OPA is still your job, and that is usually where the bugs are.
Rego is also one more language for the client’s team to learn. Its declarative, Datalog-style approach is quite different from Python or Java, so the team that runs the system after you leave needs a careful handover. Finally, the bundle model requires an HTTP server to serve policies, which means one more component to operate and secure.
What to learn first, and whom to show it to
A sensible order: the default-deny pattern, then tests with opa test, then coverage, and only after that bundles and Gatekeeper. The first two steps are enough to propose OPA in an architecture meeting with a client next week.
When reading FDE job postings, one hypothesis is 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 a CV, do not write “knows OPA”; write how many authorization rules you moved into Rego, with how many tests and what level of 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.
Was this article useful?
Thanks for the feedback!