opa-eval
High-performance OPA policy evaluator for Python — powered by Rust via PyO3 + maturin.
Perf benchmarks
Prerequisites
- Python 3.9+
- Rust toolchain (
rustup) - maturin (
pip install maturin)
Quick start
# Create venv and install dependencies
make venv
# Build the native module and install into the venv
make build
# Run tests
make test
Or manually:
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
maturin develop --release
Usage
import opa_eval
# Create an evaluator instance (one per policy / query)
authz = opa_eval.OpaEval("tests/policy.rego", query="data.authz.allow")
# Evaluate per request
result = authz.evaluate('{"role": "admin"}') # JSON string → "true"
parsed = authz.evaluate_parsed('{"role": "admin"}') # Python object → True
With external data
import json, opa_eval
rbac = opa_eval.OpaEval(
"rbac.rego",
data_json=json.dumps({"roles": {"alice": "admin", "bob": "viewer"}}),
query="data.rbac.allow",
)
rbac.evaluate_parsed('{"user": "alice"}') # True
rbac.evaluate_parsed('{"user": "bob"}') # False
Multiple independent instances
authz = opa_eval.OpaEval("authz.rego", query="data.authz.allow")
rbac = opa_eval.OpaEval("rbac.rego", query="data.rbac.allow")
# Each instance is fully independent and thread-safe
FastAPI example
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request, HTTPException
import json, opa_eval
_authz: opa_eval.OpaEval | None = None
@asynccontextmanager
async def lifespan(app: FastAPI):
global _authz
_authz = opa_eval.OpaEval("tests/policy.rego", query="data.authz.allow")
yield
app = FastAPI(lifespan=lifespan)
@app.middleware("http")
async def authz(request: Request, call_next):
input_doc = json.dumps({
"method": request.method,
"path": request.url.path,
"role": request.headers.get("x-role", "anonymous"),
})
if not _authz.evaluate_parsed(input_doc):
raise HTTPException(403, "denied by policy")
return await call_next(request)
Thread safety
Each OpaEval instance is thread-safe — evaluate and evaluate_parsed can be called
concurrently from multiple threads. Independent instances share no state and run fully
in parallel.
Development
make build # build native extension into venv
make test # run pytest
make bench # run benchmarks
make bench-compare # compare vs OPA REST API and OPA CLI
make clean # cargo clean + remove target/
Project structure
src/lib.rs # PyO3 module — OpaEval class
opa_eval.pyi # Python type stubs
pyproject.toml # maturin build config
Cargo.toml # Rust dependencies (pyo3, regorus)
tests/ # pytest tests and benchmarks
Metadata
Release files for opa-eval 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| opa_eval-0.1.1-cp39-abi3-win_amd64.whl | CPython 3.9 | abi3 | Windows x86-64 | Details |
| opa_eval-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.9 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| opa_eval-0.1.1-cp39-abi3-macosx_11_0_arm64.whl | CPython 3.9 | abi3 | macOS 11.0+ ARM64 | Details |
Total release size: 7.9 MB
Release files / opa_eval-0.1.1-cp39-abi3-win_amd64.whl
| Download URL | opa_eval-0.1.1-cp39-abi3-win_amd64.whl |
|---|---|
| Size | 2.6 MB |
| Tags | CPython 3.9 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
066ac54bee6fb0d55633e317f42aff88fc1d537a3fcc84df0f400c86dfbb075c
|
|
BLAKE2b-256 checksum How to use checksums |
859be7aaff38e5904f3af977a512de1f6458fe070fbff4107b28100beb3cc558
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / opa_eval-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | opa_eval-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.8 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
60e16ab72d305cad4265b5f5df1137057232c223137a46b44c232354898779bd
|
|
BLAKE2b-256 checksum How to use checksums |
0a78a88ce224dcb7b47fd18bc4ed84ed19a3cc4a69340b4c900180ba842f9aa0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / opa_eval-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
| Download URL | opa_eval-0.1.1-cp39-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 2.5 MB |
| Tags | CPython 3.9 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
80cb0385ae0d3ee17db71c1e59222a339d6af503b3d5588a8f1e9bc7e68134b7
|
|
BLAKE2b-256 checksum How to use checksums |
56d1b3af63a9844dbdd95ff7b591716dea9fc6689ec56e633d1f2248fa30fd2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|