Open Policy Agent (OPA) Introduction
You'll learn how OPA's architecture works, the basics of the Rego policy language, and how OPA evaluates policies against input data to make authorization decisions.
What is OPA?
Open Policy Agent (OPA) is a general-purpose policy engine that decouples policy decision-making from your application code. Think of it as a specialized "rule checker" service: your applications send queries with context ("Can user X do action Y on resource Z?"), and OPA returns a decision based on policies written in its Rego language.
OPA Architecture
OPA operates as a standalone service or library. The architecture has three key components:
- Policy Store: Contains Rego policies defining your rules
- Data Store: Holds contextual data (user roles, resource attributes, etc.)
- Decision Engine: Evaluates queries against policies and data
Applications send JSON input to OPA via API or embedded library. OPA processes this through its engine and returns a JSON decision.
Rego Language Basics
Rego is a declarative query language designed for policy expression. Unlike imperative code ("do this, then that"), Rego describes "what should be true."
A simple Rego policy might state: allow if user.role == "admin". Rego uses pattern matching and logical operators to express complex conditions concisely. Policies define rules that either produce boolean decisions or compute specific values.
Policy Evaluation Flow
When OPA receives a query with input data, it:
- Loads relevant policies from the policy store
- Combines input data with stored reference data
- Evaluates all applicable rules
- Returns the decision (often
true/falseor computed values)
The engine treats evaluation as a query problem, testing whether the input satisfies the policy conditions.
Key Takeaway: OPA separates authorization logic from application code using the declarative Rego language, allowing centralized, testable, and auditable policy management across your infrastructure.