zanzibar-index¶
Relationship-based permissions for Python applications, in the style of Google’s Zanzibar, stored in SQLite or PostgreSQL.
You describe your permission model in a schema, written in a subset of the OpenFGA modelling language: users, groups, folders, documents, and how a relationship on one implies a relationship on another. Your application then records relationships (“alice is a member of eng”, “eng can view the projects folder”) and asks questions about them (“can alice view this document?”, “who can view it?”).
The package is called zanzibar-index on PyPI and is imported as zanzibar. It is not
affiliated with Google or OpenFGA.
It is a library, not a service: its tables live in your own SQLite or PostgreSQL database,
next to your application’s, and you call it in-process, so there is no server to run and no
network API (what it is, and is not).
Inside, 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, while the graph index stores every
implied relationship ahead of time, so a check is a few indexed lookups whatever the data
and the work moves to writes (the two backends). An
application does not pick one: it uses a Store, which keeps both in step, and the choice
it actually makes is whether writes update the index at once (sync, the default) or later
(async mode). The exception is a load where writes dominate:
there the set engine on its own is the better trade
(when not to use this library).
Warning
This is alpha research software: a personal research project, with no support promised. It is heavily tested, but it has never run in production, it has no performance target, and its API may change in any 0.x release, including a patch release. Issues and pull requests are welcome; answers are best-effort.
Install¶
pip install zanzibar-index
It needs Python 3.13 or later. For PostgreSQL, install "zanzibar-index[postgres]"
instead, which adds the database driver.
Coming from a 0.0.x release? Version 0.1 renames the main class and changes some answers; see Upgrade from 0.0.x to 0.1.
A first look¶
from sqlmodel import SQLModel, Session, create_engine
from zanzibar import Store
SCHEMA = """
model
schema 1.1
type user
type group
relations
define member: [user]
type doc
relations
define viewer: [user, group#member]
"""
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
session = Session(engine)
store = Store(session, "my-app", schema=SCHEMA)
store.add_tuple("user:alice", "member", "group:eng")
store.add_tuple("group:eng#member", "viewer", "doc:roadmap")
print(store.check("user:alice", "viewer", "doc:roadmap")) # -> True
print(store.check("user:bob", "viewer", "doc:roadmap")) # -> False
Alice can view the roadmap because she is a member of eng, and every member of eng can
view it. Bob is in no group, so he cannot.
Where to go next¶
This documentation has four parts. Each answers a different kind of question.
- Tutorials
Start here if you are new. Short lessons that you follow from start to finish, typing the code as you go.
- How-to guides
Recipes for a specific job, such as running on PostgreSQL, porting an OpenFGA model, or finding out who can access an object.
- Reference
Exact descriptions of the Python API, the schema language, the exceptions and the configuration options.
- Explanation
Background and design: what Zanzibar-style permissions are, how the library answers a question, and how far its answers can be trusted.
Before a first deploy, read Use async mode and freshness tokens, Turn on runtime checks in production, Prune the change outbox and Handle errors.
License¶
Apache-2.0. The license text is the LICENSE file in the
source repository.