Skip to main content

Building blocks for zanzibar style ReBAC

Project description

zanzipy ✨

Zanzibar‑style authorization for Python, with a tiny DSL to declare your schema and a simple client to write and check permissions. Friendly, lightweight, and practical.

Install 📦

pip install zanzipy

Quick start 🚀

from zanzipy.dsl.builder import SchemaBuilder
from zanzipy.client import ZanzibarClient
from zanzipy.storage.repos.concrete.memory.relations import InMemoryRelationRepository

# Define schema with the fluent DSL
registry = (
    SchemaBuilder()
        .namespace("user").done()
        .namespace("document")
            .relation("owner", subjects=["user"])  # direct
            .permission("can_view", union=["owner"])  # computed
            .done()
        .build()
)

# Use the in‑memory repo for a zero‑dependency start
client = ZanzibarClient(schema=registry, relations_repository=InMemoryRelationRepository())

client.write("document:readme", "owner", "user:alice")
assert client.check("document:readme", "can_view", "user:alice")

That’s it. Add more relations/permissions with the DSL, and swap the repository when you’re ready to plug in your storage.

See the examples/ folder 📁 for more patterns (tuple‑to‑userset, groups, nested folders). A good starting point is examples/boobledrive.py.

Quick start with mixins 🧩

Zanzipy also provides convenient mixins for Pythonic integration with your domain models.

from dataclasses import dataclass

from zanzipy.client import ZanzibarClient
from zanzipy.dsl.builder import SchemaBuilder
from zanzipy.engine_integration import ZanzibarEngine, configure_authorization
from zanzipy.integration.mixins import AuthorizableResource, AuthorizableSubject
from zanzipy.storage.repos.concrete.memory.relations import InMemoryRelationRepository

# Define a minimal schema (user + document)
registry = (
    SchemaBuilder()
        .namespace("user").done()
        .namespace("document")
            .relation("owner", subjects=["user"])  # direct relation
            .permission("can_view", union=["owner"])  # computed permission
            .done()
        .build()
)

# Wire up the engine used by the mixins
client = ZanzibarClient(schema=registry, relations_repository=InMemoryRelationRepository())
configure_authorization(ZanzibarEngine(client))

# Domain models using mixins
@dataclass
class User(AuthorizableSubject):
    id: str

    def get_subject_dict(self) -> dict:
        return {"namespace": "user", "id": self.id}


@dataclass
class Document(AuthorizableResource):
    id: str

    def get_resource_dict(self) -> dict:
        return {"namespace": "document", "id": self.id}


# Use high‑level helpers
alice = User(id="alice")
readme = Document(id="readme")

readme.grant(alice, "owner")  # writes a tuple: document:readme#owner@user:alice
assert readme.check(alice, "can_view")  # True via the computed permission

For a fuller mixins setup with groups, SQLAlchemy models, and caching, see examples/boobledrive_sqlalchemy_and_mixins.py.

Key features 🧰

  • ✨ DSL‑first schema authoring (SchemaBuilder, NamespaceBuilder).
  • 🔗 Zanzibar semantics: relations, permissions, union/intersection/exclusion, tuple‑to‑userset.
  • ✅ Correctness‑first evaluation: cycle detection, max‑depth limits, and subject expansion.
  • 🧩 Simple client API: write, delete, check, list_objects, expand.
  • 🗄️ Storage‑agnostic: implement RelationRepository; start with in‑memory.
  • ⚡ Optional caching: tuple cache and compiled rule cache for hot paths.

When should you use zanzipy? 🤔

  • You want ReBAC embedded in your Python app without running another service.
  • You prefer a human‑readable, declarative schema (via a tiny DSL).
  • Your app has shared resources (e.g., docs, folders, teams) and needs roles, groups, or nested access patterns.
  • You need cross‑resource edges (tuple‑to‑userset) and clear, testable authorization logic.

License

Apache‑2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zanzipy-0.1.3.tar.gz (42.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zanzipy-0.1.3-py3-none-any.whl (64.2 kB view details)

Uploaded Python 3

File details

Details for the file zanzipy-0.1.3.tar.gz.

File metadata

  • Download URL: zanzipy-0.1.3.tar.gz
  • Upload date:
  • Size: 42.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for zanzipy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 cb89242838006c7dbc0ff1e7d9cb6dcab2d7ff7bdc33cca8de44c086dc7f92e2
MD5 6dbf0919a28e5a5daf5a317af9182829
BLAKE2b-256 4a2248cce8b3232b5cb8d9fa9b3699940c080e2b416827dc274b16538747bc0e

See more details on using hashes here.

File details

Details for the file zanzipy-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: zanzipy-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 64.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for zanzipy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c7fdced4f1242380e487da79251f50c8c277af89314cdd215ea25e2afcf84049
MD5 64ea9b3589aa797c912043ab9c02c63b
BLAKE2b-256 9d7a157dc1fa65e83492eb679f82e1cbe0a9f1f528e05047bba7705002f3b514

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page