Open Policy Agent: How Rego's Declarative Logic Rewires Authorization at Scale
Hook
Netflix evaluates over 500 million authorization decisions daily using a query language most developers have never heard of—and it's not SQL.
Context
Before policy engines, authorization logic lived scattered across microservices: hardcoded if-statements checking user roles, feature flags buried in configuration files, compliance rules duplicated in Terraform scripts and runtime code. Every policy change meant code deploys across dozens of services. Auditing who-can-do-what required reading source code across repositories. The coupling was organizational poison—security teams couldn't update policies without engineering sprints, and compliance frameworks like SOC2 demanded centralized policy management that simply didn't exist.
Open Policy Agent emerged from the cloud-native movement's realization that infrastructure-as-code needed policy-as-code. If Kubernetes manifests are declarative YAML, why are admission control policies imperative Go? If Terraform describes desired state, why are compliance checks procedural Python? OPA introduced Rego, a declarative logic programming language where you describe what should be true rather than how to check it. The separation is architectural: policies become versioned artifacts deployed independently from application code, evaluated by a standalone engine that speaks REST. This inverts the traditional model—instead of scattering authorization across services, you centralize policy logic while distributing its enforcement.
Technical Insight
OPA's architecture revolves around three components: the Rego compiler, the evaluation runtime, and the data/policy store. When a service needs an authorization decision, it sends JSON input to OPA's REST API. The runtime evaluates Rego rules against this input, producing a decision—typically allow/deny, but Rego can compute arbitrary transformations. The magic happens in how Rego executes.
Here's a real example showing why Rego's declarative style matters. Suppose you're building a document management system where users can access documents based on department membership and clearance level:
package document.authz
import future.keywords.if
import future.keywords.in
default allow := false
allow if {
user_has_clearance
user_in_document_department
}
user_has_clearance if {
input.user.clearance >= data.documents[input.document_id].required_clearance
}
user_in_document_department if {
some dept in input.user.departments
dept == data.documents[input.document_id].department
}
# Audit trail - computed regardless of decision
audit_context := {
"user_id": input.user.id,
"document_id": input.document_id,
"clearance_check": user_has_clearance,
"department_check": user_in_document_department,
"timestamp": time.now_ns()
}
Notice what's absent: loops, conditionals, mutation. You define relationships between facts. The OPA runtime uses a topdown evaluation strategy with unification—it starts with the query allow and works backward, finding variable bindings that satisfy all rules. The some dept in syntax triggers iteration internally, but you never write the loop. This isn't just syntactic sugar—it enables OPA's killer feature: partial evaluation.
Partial evaluation pre-compiles policies against known schemas. If OPA knows your user object structure at compile time but the specific user ID comes at runtime, it can eliminate entire rule branches that can never match. For the example above, if your schema guarantees input.user.departments is always an array of strings, OPA removes type-checking code. If your documents data is indexed by ID, OPA rewrites rules to use direct lookups instead of scans. The result: runtime queries execute 10-100x faster because redundant computation vanishes.
The compiler architecture makes this possible. Rego source code parses into an AST, which undergoes comprehension rewriting (translating high-level some/every syntax into core logic), safety checking (ensuring all variables are bound before use, preventing infinite loops), and type inference. The compiler can output multiple targets: the standard runtime interpreter, optimized query plans for specific input schemas, or WebAssembly modules. WASM compilation is particularly clever—it bundles policy and data into a single .wasm file executable in browsers or edge workers:
# Compile policy to WASM
opa build -t wasm -e 'document/authz/allow' policy.rego
# Result: bundle.tar.gz containing policy.wasm
# Runs in Cloudflare Workers, Fastly Compute@Edge, browsers
# No OPA server required, <1ms cold start
The tradeoff: WASM builds strip most built-in functions for security. HTTP requests, file I/O, and time functions become host-provided imports. You gain portability but lose convenience.
OPA's data layer supports multiple integration patterns. The default in-memory store loads JSON/YAML into the data namespace. For larger datasets, you can load bundles from S3/HTTP with signature verification, or use external data sources via the http.send built-in. Critically, OPA doesn't replicate data—it pulls references and caches. This works when policy evaluation needs metadata (user roles, resource attributes) but not when you're filtering thousands of records. For "which documents can this user access?" queries over large datasets, you need to push filtering to your database, not pull everything into OPA.
The evaluation strategy handles recursive policies gracefully through memoization. If a rule references itself (common in hierarchical permission models like "managers inherit their reports' permissions"), OPA caches intermediate results to avoid exponential recomputation. The topdown planner also reorders rule evaluation based on selectivity—if one rule branch fails fast, OPA checks it first to short-circuit expensive computations.
Gotcha
Rego's learning curve is OPA's biggest liability. Developers expecting imperative code struggle with logic programming's mental model. Debugging requires understanding unification—why did a variable binding fail? Printing intermediate values isn't straightforward; you learn to use trace(sprintf(...)) to instrument policies. The declarative style also makes certain patterns awkward. Sequential operations with side effects ("check permission, then log, then rate-limit") don't compose naturally because Rego has no execution order guarantees. You end up structuring policies to compute all decisions simultaneously, then interpreting results in application code.
Performance surprises lurk in complex policies. The default store loads everything into memory, so multi-gigabyte datasets cause OOM. Nested comprehensions over large arrays trigger quadratic evaluation that timeouts don't catch until production. OPA provides profiling tools, but they require expertise to interpret. The partial evaluation optimizer helps, but only when you provide schemas—many teams skip this step and wonder why latency spikes. Distributed deployments are another gap. OPA is a stateless process; you run multiple replicas behind a load balancer, but each pulls bundles independently. There's no built-in consistency guarantee, so policy updates propagate eventually. For critical paths requiring strong consistency, you're building your own synchronization or accepting brief windows where replicas enforce different policies.
Verdict
Use if: You're managing authorization across multiple services and policy changes require coordinating deploys across teams. OPA shines when policies are complex enough that hardcoding creates maintenance nightmares—nested conditionals with 5+ branches, cross-cutting rules that span microservices, or compliance requirements needing audit trails. Also choose OPA for Kubernetes admission control, API gateway authorization, or infrastructure compliance (Terraform/CloudFormation validation) where its ecosystem integrations are mature. The declarative model pays off when non-engineers (security, compliance teams) need to update policies without touching application code. Skip if: Your authorization is simple RBAC that fits in a database table, or latency matters more than flexibility—OPA adds ~1-10ms per decision, unacceptable for tight loops. Also skip if your team lacks patience for learning Rego; the paradigm shift is real and forcing it on reluctant developers creates resentment. For straightforward use cases, AWS Cedar offers better performance and simpler syntax. For relationship-based permissions at scale (think Google Drive sharing), Zanzibar-inspired systems like AuthZed outperform OPA's general-purpose engine. Choose OPA when policy flexibility and infrastructure-wide consistency justify the complexity tax.