Designing authorization systems
I need to create a scalable authorization system, likely choosing a hybrid model. I'll consider architecture aspects such as hierarchy, multitenancy, and caching. While I'll provide example policies and configurations using tools like OPA or Rego, I want to be detailed but concise. Key decisions will revolve around whether to use RBAC, ABAC, or ReBAC. I'm thinking of proposing ReBAC with ABAC constraints, and I’ll structure it with a policy engine and relationship store.Designing with sidecars and policies
I’m considering using data plane sidecars with a central Policy Decision Point (PDP) that includes local caching. For propagation, I think an event bus would work well. Auditing will rely on an append-only log, maintaining versioned policies and tracking change requests. My testing will involve property-based tests, static analysis, and simulations to ensure there are no privilege escalations. I also want to provide various configurations, support for different permission models, and establish a solid resource hierarchy.Creating a multi-tenant data model
I’m designing a data model with entities like tenant, organization, project, folder, document, group, user, and service account. I need to establish relations such as member, admin, owner, viewer, and editor. Permissions will be based on these relations, with propagation through parent links. For multi-tenancy, each object will include a tenant attribute.
The enforcement architecture will feature a Policy Administration Point (PAP), Policy Enforcement Points (PEP), and a Policy Decision Point (PDP), along with caching for efficient decision-making and query isolation.Enhancing system features
I’m focusing on propagation, which involves generating events when there are relationship or policy changes. Subscribers will need to invalidate their caches and precompute permission edges for frequently used paths. For auditing, an append-only log will track relation writes and policy versions, recording who made changes and why, and I’ll provide an "explain" endpoint to return the decision graph for audits. My testing strategy includes unit tests, privilege escalation tests, and static checks for issues like cyclical relations to ensure everything runs smoothly and securely.Modeling permissions and hierarchies
I’m thinking about how to provide clear guidance for modeling permissions and resource hierarchies. I should use relations like parent or computed relationships. For instance, a document can inherit permissions from its folder, project, or organization. I can define computed user sets, such as making the document's viewer set equal to the viewers of its parent plus those it's shared with.
Additionally, I need to consider service accounts for machine-to-machine interactions while always aiming for the principle of least privilege.
1) Choosing RBAC vs ABAC vs ReBAC (recommended: ReBAC core + ABAC constraints, optional RBAC “roles” as relations)
- RBAC (roles → permissions): simple and fast, but struggles with:
- per-resource sharing (“Alice can view this doc”),
- deep hierarchies (org → project → folder → doc),
- multi-tenant + delegation rules.
- ABAC (attributes + rules): expressive, but can become hard to reason about and expensive (needs many attributes at decision time; debugging is harder).
- ReBAC (relationships/graph): best for sharing + hierarchies + delegation. It scales well when modeled like Google Zanzibar (tuples + computed permissions).
Design choice
- Use ReBAC as the primary authorization model (relationships + inherited permissions).
- Add ABAC as a “constraint layer” for context (tenant boundary, MFA, IP ranges, time windows, device posture, data classification).
- Represent “RBAC roles” as ReBAC relationships (e.g.,
project:123#admin@user:alice) so you get RBAC ergonomics with ReBAC scalability.
This hybrid is common in large systems because:
- ReBAC answers “who is related to what?”
- ABAC answers “is it allowed right now under these conditions?”
2) Core model: objects, relations, permissions, and hierarchies
2.1 Entity model (example)
- Tenancy:
tenant
- Identity:
user, service_account, group
- Resources:
org, project, folder, document
- Optional:
role objects if you want reusable bundles, but often relations are enough.
2.2 Relationship tuples (Zanzibar-style)
Store facts like:
- “Alice is an org admin”
- “Doc belongs to folder”
- “Group X can view project Y”
A tuple format:
<object_type>:<object_id>#<relation>@<subject_type>:<subject_id>
Examples:
tenant:t1#member@user:alice
org:t1/acme#admin@user:alice
org:t1/acme#member@group:t1/eng
group:t1/eng#member@user:bob
project:t1/acme/payments#parent@org:t1/acme
project:t1/acme/payments#editor@group:t1/eng
folder:t1/acme/payments/f1#parent@project:t1/acme/payments
document:t1/acme/payments/f1/d1#parent@folder:t1/acme/payments/f1
document:t1/acme/payments/f1/d1#viewer@user:carol // direct share
2.3 Permission computation via resource hierarchy
Define computed relations so permissions inherit from parents.
A typical pattern:
document.viewer includes:
- direct
document#viewer
- inherited
folder#viewer
- inherited
project#viewer
- inherited
org#viewer
- Similarly for
editor, admin, etc.
3) Example authorization model config (OpenFGA / SpiceDB-style)
Below is an OpenFGA-like model showing hierarchy and inheritance. (SpiceDB/Authzed is very similar conceptually.)
model
schema 1.1
type user
type group
relations
define member: [user]
type tenant
relations
define member: [user, group#member]
type org
relations
define tenant: [tenant]
define member: [user, group#member]
define admin: [user, group#member]
define viewer: member
define editor: admin
type project
relations
define parent: [org]
define owner: [user, group#member]
define admin: owner or parent.admin
define editor: admin or parent.editor
define viewer: editor or parent.viewer
type folder
relations
define parent: [project]
define admin: parent.admin
define editor: parent.editor
define viewer: parent.viewer
type document
relations
define parent: [folder]
define viewer: [user, group#member] or parent.viewer
define editor: [user, group#member] or parent.editor
define admin: parent.admin
Notes
project.admin is either explicit owner or inherited from org.admin.
document.admin is inherited only (so “admin” can’t be granted ad-hoc on an individual doc unless you add that relation).
viewer/editor can be granted directly on a document (sharing), but also inherits.
4) Multi-tenant constraints (hard isolation + safe defaults)
4.1 Object naming / partitioning
Enforce tenant scoping everywhere:
- IDs include tenant:
document:t1/...
- All relationship writes and checks must include tenant-prefixed object IDs.
Rule: any API that accepts a resource ID must validate the tenant prefix matches the caller’s tenant context.
4.2 Storage isolation options
Pick based on compliance and scale:
- Strongest isolation: separate DB per tenant (costly operationally).
- Common: shared DB with tenant partition key (index on
(tenant_id, object_type, object_id, relation)).
- Hybrid: high-compliance tenants on dedicated shards; others shared.
4.3 Cross-tenant prevention
- Disallow tuples where subject tenant ≠ object tenant (except explicitly allowed “global” identities).
- ABAC constraint:
request.tenant_id == resource.tenant_id must always hold.
5) Decision flow and system architecture (PDP/PEP)
Components
- PEP (Policy Enforcement Point): in each API service (middleware) or at the API gateway.
- PDP (Policy Decision Point): centralized auth service that evaluates checks using:
- relationship store (ReBAC tuples)
- policy model (computed relations)
- optional ABAC conditions
- PAP (Policy Administration Point): UI/API for granting/revoking access.
- Relationship store: tuple database (SpiceDB/OpenFGA or your own).
- Audit log: append-only record of policy and relationship changes.
Typical check
can(user, "document:…", "read", context):
- PDP resolves
document.viewer (graph expansion; often cached).
- ABAC checks:
- tenant match
- session assurance (MFA)
- data classification constraints
- Return
ALLOW/DENY (+ optional “explain” trace for debugging/audit).
6) Caching and propagation (scale without stale-privilege bugs)
6.1 What to cache
- Relationship reads / expansions at PDP (hot graph edges).
- Final decisions at PEP for very hot endpoints.
6.2 Correct invalidation (avoid stale allows)
Use a consistency token / revision strategy (Zanzibar calls this a zookie):
- Every relationship write increments a revision.
- Every check executes “at revision ≥ X”.
- Cache keys include
(subject, action, object, revision, relevant_context_hash).
Safe pattern
- PEP caches decision for short TTL (e.g., 1–30s) and includes revision.
- PDP caches expansions by object+relation at a given revision.
6.3 Propagating changes
- Relationship store emits events:
TupleWritten/TupleDeleted with affected object(s) and new revision.
- PEP/PDP instances subscribe (Kafka/SNS/SQS/etc.) and invalidate:
- decision cache entries touching that object subtree (or just rely on revision mismatch)
- expansion caches for affected object types/relations
Practical tip: revision-based correctness + short TTL is usually enough; explicit invalidation improves latency and reduces load.
7) Auditing and change control (who changed what, when, and why)
7.1 Audit log requirements
Log every:
- relationship write/delete
- policy model change (schema)
- ABAC policy change
Include:
- actor (user/service), tenant, request ID, source IP
- before/after tuple (or diff)
- justification / ticket reference (optional but valuable)
Store in an append-only system (WORM where required). Consider a hash chain for tamper evidence.
7.2 “Explain” for audits and debugging
Provide an explain(check) endpoint returning:
- which tuples matched (e.g.,
doc.parent -> folder.parent -> project.editor -> group.member)
- which ABAC constraints passed/failed
This is critical for incident response and privilege escalation investigations.
8) ABAC constraint layer (example policies)
Use ABAC for contextual requirements that don’t belong in the relationship graph.
Example: Cedar policy (conceptual)
permit(
principal,
action == Action::"read",
resource
)
when {
principal.tenant_id == resource.tenant_id &&
resource.classification != "restricted" ||
(resource.classification == "restricted" && principal.mfa == true)
};
Example: OPA/Rego snippet (gateway-side)
package authz
default allow = false
allow {
input.tenant_id == input.resource.tenant_id
input.rebac_allow == true # result from ReBAC PDP check
not blocked_by_risk
}
blocked_by_risk {
input.resource.classification == "restricted"
input.session.mfa != true
}
In practice:
- PDP returns
rebac_allow
- Gateway/PEP enforces ABAC constraints locally (or PDP does both)
9) Preventing privilege escalation (model + tests + guardrails)
9.1 Model guardrails
Common escalation vector: users granting themselves permissions.
Mitigations:
- Separate permissions:
can_grant_* distinct from *_editor/viewer.
- Only admins/owners can modify bindings.
- No “write auth tuples” permission should be derivable from the same permission it grants.
Example rule (conceptual):
- Only
project.admin can add/remove project.editor/viewer tuples.
- Only
org.admin can assign project.owner.
9.2 Test strategy (automated)
- Policy unit tests (golden cases)
- “viewer can read, cannot write”
- “org admin can administer all descendants”
- “cross-tenant always denied”
- Property-based tests
- generate random graphs + operations
- invariant: “a user cannot become org admin unless an existing org admin (or system) granted it”
- Reachability / escalation analysis
- search for paths that imply
admin from non-admin starting points
- detect cycles that accidentally broaden permissions
- Mutation tests
- flip relations in model and ensure tests fail (validates test strength)
- Shadow evaluation (diff tests)
- run new PDP beside old logic; compare decisions in production traffic
Example escalation invariant (pseudo)
- For every tenant/org:
- If
user U is not in org.admin at revision R,
- then after any sequence of operations performed by U alone,
- U must still not be in
org.admin.
10) Operational APIs you’ll want (for scale and product features)
Beyond Check:
- BatchCheck: evaluate many (resource,action) pairs at once.
- ListObjects: “list all documents Bob can read” (must be optimized/cached).
- ListSubjects: “who can access this document?”
- Expand/Explain: show membership expansion and reasoning.
- WriteTuples with validation hooks: enforce tenant constraints, forbid illegal edges.
11) Minimal reference schema for a tuple store (if building yourself)
A normalized table (Postgres example):
create table auth_tuples (
tenant_id text not null,
object_type text not null,
object_id text not null,
relation text not null,
subject_type text not null,
subject_id text not null,
subject_relation text null, -- for group#member style
created_at timestamptz not null default now(),
created_by text not null,
primary key (tenant_id, object_type, object_id, relation, subject_type, subject_id, coalesce(subject_relation,''))
);
create index on auth_tuples (tenant_id, subject_type, subject_id);
create index on auth_tuples (tenant_id, object_type, object_id);
You’ll still need:
- a revision log (monotonic version) for consistency tokens
- an efficient graph evaluator (or adopt SpiceDB/OpenFGA rather than reinventing)
Summary recommendation
- Implement ReBAC (Zanzibar-style) for scalable sharing + hierarchy inheritance.
- Add ABAC constraints for tenant boundary + runtime/session conditions.
- Enforce multi-tenant isolation by ID namespacing + storage partitioning + policy constraints.
- Scale via revision-based caching + event-driven invalidation.
- Make auditing first-class with append-only logs + explainable decisions.
- Continuously test for privilege escalation using invariants, property-based tests, and reachability analysis.
If you share your resource types/actions and expected delegation rules (e.g., “project owner can grant editor, but editor can’t grant anyone”), I can tailor an exact model file (OpenFGA/SpiceDB) and a concrete test matrix for escalation paths.