zanzibar-index
Zanzibar-style relationship-based access control (ReBAC) for Python, stored in SQLite or
PostgreSQL through SQLModel. You write your permission model in a subset of the
OpenFGA modelling language (schema 1.1, including and and
but not), record relationships such as "alice is a member of eng", and ask questions such
as "can alice view this document?" and "who can view it?". The package is not affiliated
with Google or OpenFGA.
Two backends give the same answers with opposite cost models. The set engine stores only
the raw tuples and computes each answer when you ask; the graph index stores every implied
relationship ahead of time, so a check is a few indexed lookups and the work moves to writes.
Status: alpha, research code, no support promised. It is heavily tested but has never
run in production, it has no performance target, and the API may change in any 0.x release.
Issues and pull requests are welcome; answers are best-effort.
Install
pip install zanzibar-index # SQLite
pip install "zanzibar-index[postgres]" # adds psycopg2-binary for PostgreSQL
Python 3.13 or later. The import name is zanzibar.
Quickstart
from sqlmodel import SQLModel, Session, create_engine
from zanzibar import Store
SCHEMA = """
model
schema 1.1
type user
type group
relations
define member: [user, group#member]
type doc
relations
define banned: [user]
define viewer: [user, group#member] but not banned
"""
engine = create_engine("sqlite:///perms.db")
SQLModel.metadata.create_all(engine) # creates the zanzibar_* tables
with Session(engine) as session:
store = Store(session, "my-app", schema=SCHEMA)
# add_tuple(user, relation, object), in OpenFGA's "type:name[#relation]" form
store.add_tuple("user:alice", "member", "group:eng")
store.add_tuple("user:bob", "member", "group:eng")
store.add_tuple("group:eng#member", "viewer", "doc:d1") # every member of eng
print(store.check("user:alice", "viewer", "doc:d1")) # True
print(store.check("user:carol", "viewer", "doc:d1")) # False
store.add_tuple("user:bob", "banned", "doc:d1")
print(store.check("user:bob", "viewer", "doc:d1")) # False
print(store.check("user:alice", "viewer", "doc:d1")) # True
Each write commits and returns a freshness token. A store's schema is write-once:
reopening with Store(session, "my-app") reads the stored one.
Documentation
The user documentation is at
https://averykhoo.github.io/graph-reachability-zanzibar-index/:
- Tutorials:
start with your first permission check.
- How-to guides:
find who can access what,
run on PostgreSQL,
port an OpenFGA model,
use async mode and freshness tokens,
handle errors,
upgrade from 0.0.x.
- Reference:
the Python API, the schema language,
exceptions,
configuration,
database tables,
and the release notes.
- Explanation:
the two backends,
differences from OpenFGA,
limits and costs
and how correctness is checked.
Before a first deploy, read the guides to async mode and freshness tokens,
runtime checks in production,
pruning the change outbox, and the
exceptions reference.
License
Apache-2.0. See LICENSE in the
source repository.