skillfed

cerbos-sqlalchemy

SQLAlchemy adapter for generating queries with Cerbos: an open core, language-agnostic, scalable authorization solution

cerbos-sqlalchemy v0.4.0 128.6K downloads/30d#11,701 on PyPI21
Permissive license Apache-2.0 Active released

What it is and what it does

cerbos-sqlalchemy bridges Cerbos, an open-source authorization engine, and SQLAlchemy by translating Cerbos query plans (from its PlanResources API) into native SQLAlchemy Select queries. Instead of fetching all records and filtering in application code, this adapter pushes authorization logic down to the database layer, where the SQL WHERE clause enforces attribute-based access control rules.

The adapter handles logical operators, comparisons, field-to-field checks, string operations, timestamps, and hierarchy comparisons. It is tested against Cerbos PDP 0.54.0 with 112 reference conformance actions and supports both ORM entities and raw Table instances. Users can override default SQL operators for database-specific optimizations (e.g., PostgreSQL's `= ANY` instead of `IN`), and must ensure mapped columns use case-sensitive collation to prevent silent over-granting of access.

Use it for:

  • Enforce department-level access control on leave requests by converting Cerbos policies into WHERE clauses that filter authorized rows.
  • Implement role-based filtering on multi-tenant data by translating principal attributes and resource policies into database queries.
  • Optimize authorization checks on large datasets by pushing policy evaluation to the database query layer instead of loading in memory.
  • Support complex attribute-based rules (e.g., geography, team, priority) by mapping Cerbos CEL expressions to portable SQL predicates.
  • Override default SQL operators for database-specific idioms, such as using PostgreSQL's `= ANY` for collection membership instead of `IN`.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Converts Cerbos authorization query plans into SQLAlchemy Select queries, enabling attribute-based access control enforcement directly in database queries.

Yes. This is a well-maintained, low-friction adapter (pure Python, two dependencies, no vulnerabilities) that solves a real problem: pushing authorization enforcement into database queries rather than application code. It is actively developed, permissively licensed, and tested against a reference conformance suite. Install it if you use Cerbos and SQLAlchemy together and want to avoid loading unauthorized records into memory.

Install

cerbos-sqlalchemy on PyPI

pip

pip install cerbos-sqlalchemy

uv

uv add cerbos-sqlalchemy

poetry

poetry add cerbos-sqlalchemy

Installing cerbos-sqlalchemy

Before you install

Low friction: pure Python wheel with only two runtime dependencies (cerbos and sqlalchemy). Active maintenance with a recent release (11 days old) and no known vulnerabilities.

License in practice

Apache-2.0 permissive license allows commercial use, modification, and distribution with minimal restrictions—suitable for most projects.

Quickstart

pip install cerbos-sqlalchemy

from cerbos.sdk.client import CerbosClient
from cerbos_sqlalchemy import get_query
from sqlalchemy.orm import declarative_base

Base = declarative_base()
class LeaveRequest(Base):
    __tablename__ = "leave_request"
    # ... columns ...

with CerbosClient(host="http://localhost:3592") as c:
    plan = c.plan_resources("view", principal, resource_desc)

attr_map = {"request.resource.attr.department": LeaveRequest.department}
query = get_query(plan, LeaveRequest, attr_map)

Requires a running Cerbos PDP server (typically at localhost:3592 or configured host); SQLAlchemy >= 1.4 and Cerbos > v0.16.

Verify before relying

  • Exact scope of CEL operator coverage beyond the 112 reference conformance actions listed.
  • Performance characteristics when translating complex query plans with many nested conditions.
  • Compatibility with SQLAlchemy 2.0 ORM features beyond the basic declarative patterns shown.

Package facts

License Apache-2.0 (permissive)
Python support supports the current Python release (>=3.8)
Install friction low — pure-Python wheel
Runtime dependencies 2 — cerbos, sqlalchemy
Maintenance actively maintained — 11 days since the last release
Last repo commit
First released
Downloads 128,627/month — #11,701 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: cerbos_sqlalchemy-0.4.0-py3-none-any.whl

Development Status :: 4 - BetaIntended Audience :: DevelopersTopic :: SecurityTopic :: Software Development :: Libraries

Tags

cerbos sqlalchemy integrationauthorization query plan adapterattribute-based access control databasecerbos query plan to sqlpolicy enforcement in sqlalchemydatabase-level authorization filteringcerbos plan resources adapter
authorizationaccess-controlcerbos

More Libraries packages

urllib3

urllib3 is an HTTP client library that provides…

permissive · top 100 on PyPI

requests

Requests is a Python HTTP library that…

permissive · top 100 on PyPI

pluggy

Pluggy provides a plugin system that lets you…

permissive · top 100 on PyPI

python-dateutil

Provides parsing, arithmetic, and recurrence…

permissive · top 100 on PyPI

six

Six provides utility functions to write Python…

permissive · top 100 on PyPI

pytest

pytest is a testing framework that lets you…

permissive · top 100 on PyPI

cerbos

Python client library for querying and managing…

permissive · top 15,000 on PyPI

casbin-async-sqlalchemy-adapter

Provides an asynchronous SQLAlchemy adapter for…

permissive · top 15,000 on PyPI

fastapi-users-db-sqlalchemy

Provides a SQLAlchemy ORM adapter for FastAPI…

permissive · top 5,000 on PyPI

sqlalchemy-spanner

Provides a SQLAlchemy dialect that enables…

permissive · top 5,000 on PyPI

casbin-sqlalchemy-adapter

Connects PyCasbin access control policies to…

permissive · top 15,000 on PyPI

sqlalchemy-adapter

Bridges PyCasbin access-control policies with…

permissive · top 5,000 on PyPI

magic-filter

Provides a filter mechanism based on dynamic…

permissive · top 5,000 on PyPI

sqlalchemy-solr

Provides a SQLAlchemy dialect that allows you…

permissive · top 15,000 on PyPI

ibm-db-sa

SQLAlchemy dialect adapter that enables Python…

permissive · top 15,000 on PyPI

sqlalchemy-hana

Provides a SQLAlchemy dialect that enables you…

permissive · top 15,000 on PyPI