assay_auth/zanzibar/types.rs
1//! Plain-old-data types for the Zanzibar / ReBAC layer.
2//!
3//! Mirrors the Google Zanzibar paper terminology (Keto / SpiceDB users
4//! will recognise the names):
5//!
6//! - **object** — a resource being protected, identified as
7//! `<type>:<id>` (e.g. `document:foo`, `circle:immediate`).
8//! - **subject** — who's being checked. Either a *direct* user
9//! (`user:alice`, `subject_rel = None`) or a *userset* — every member
10//! of some other relation (`family:foo#member`, where
11//! `subject_rel = Some("member")`).
12//! - **tuple** — the atomic permission fact:
13//! `object#relation @ subject`. The persistence layer stores
14//! millions of these; the recursive-CTE walks them transitively.
15//! - **namespace schema** — the authoritative description of which
16//! relations + permissions a given `object_type` supports, parsed
17//! from a SpiceDB-compatible DSL by [`super::schema`].
18//!
19//! All identifiers are owned `String`s — we don't intern. Tuples are
20//! short-lived in memory; the SQL layer is where dense storage lives.
21
22use std::collections::BTreeMap;
23
24use serde::{Deserialize, Serialize};
25
26/// `<type>:<id>` reference to a protected resource (the *object* side
27/// of a relation tuple). Field name is `object_type`/`object_id` to
28/// match the column names in `auth.zanzibar_tuples` 1:1 — keeps SQL
29/// hand-rolled queries readable.
30#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
31pub struct ObjectRef {
32 pub object_type: String,
33 pub object_id: String,
34}
35
36impl ObjectRef {
37 /// Convenience constructor — `ObjectRef::new("document", "foo")`.
38 pub fn new(ty: impl Into<String>, id: impl Into<String>) -> Self {
39 Self {
40 object_type: ty.into(),
41 object_id: id.into(),
42 }
43 }
44
45 /// Parse `"<type>:<id>"`. Returns `None` if no `:` separator is
46 /// present or either side is empty — callers wrap this in a typed
47 /// error appropriate to their context (HTTP 400, parser line/col,
48 /// etc.).
49 pub fn parse(s: &str) -> Option<Self> {
50 let (ty, id) = s.split_once(':')?;
51 if ty.is_empty() || id.is_empty() {
52 return None;
53 }
54 Some(Self::new(ty, id))
55 }
56
57 /// `<type>:<id>` rendering. Round-trips with [`Self::parse`].
58 pub fn render(&self) -> String {
59 format!("{}:{}", self.object_type, self.object_id)
60 }
61}
62
63/// `<type>:<id>[#<relation>]` reference. A subject is either:
64///
65/// - a **direct** user (`subject_rel = ""`) — terminal, e.g.
66/// `user:alice`, that's the leaf the recursive CTE walks toward.
67/// - a **userset** (`subject_rel = "member"`) — every member of
68/// `<type>:<id>`'s `relation`, e.g. `family:smith#member`. The walk
69/// follows these one hop at a time.
70///
71/// We use the empty string rather than `Option<String>` so the column
72/// can stay in the primary key (PG implicitly NOT-NULLs PK members) and
73/// so SQLite/PG queries can use plain equality (`subject_rel = ?`)
74/// instead of `IS NOT DISTINCT FROM`. JSON callers may either omit the
75/// field or send `""` for direct tuples; both deserialize the same way.
76#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
77pub struct SubjectRef {
78 pub subject_type: String,
79 pub subject_id: String,
80 #[serde(default)]
81 pub subject_rel: String,
82}
83
84impl SubjectRef {
85 pub fn direct(ty: impl Into<String>, id: impl Into<String>) -> Self {
86 Self {
87 subject_type: ty.into(),
88 subject_id: id.into(),
89 subject_rel: String::new(),
90 }
91 }
92
93 pub fn userset(
94 ty: impl Into<String>,
95 id: impl Into<String>,
96 relation: impl Into<String>,
97 ) -> Self {
98 Self {
99 subject_type: ty.into(),
100 subject_id: id.into(),
101 subject_rel: relation.into(),
102 }
103 }
104
105 /// `true` for `user:alice` (direct subject); `false` for
106 /// `family:smith#member` (userset).
107 pub fn is_direct(&self) -> bool {
108 self.subject_rel.is_empty()
109 }
110
111 /// Parse `"<type>:<id>"` (direct) or `"<type>:<id>#<relation>"`
112 /// (userset). Returns `None` if the structural shape is invalid.
113 pub fn parse(s: &str) -> Option<Self> {
114 let (head, rel) = match s.split_once('#') {
115 Some((h, r)) if !r.is_empty() => (h, r.to_string()),
116 Some(_) => return None,
117 None => (s, String::new()),
118 };
119 let (ty, id) = head.split_once(':')?;
120 if ty.is_empty() || id.is_empty() {
121 return None;
122 }
123 Some(Self {
124 subject_type: ty.to_string(),
125 subject_id: id.to_string(),
126 subject_rel: rel,
127 })
128 }
129
130 /// Round-trip rendering with [`Self::parse`].
131 pub fn render(&self) -> String {
132 if self.subject_rel.is_empty() {
133 format!("{}:{}", self.subject_type, self.subject_id)
134 } else {
135 format!("{}:{}#{}", self.subject_type, self.subject_id, self.subject_rel)
136 }
137 }
138}
139
140/// One row of `auth.zanzibar_tuples`. Field names mirror the columns
141/// 1:1 so hand-rolled SQL stays readable. `subject_rel` is the empty
142/// string for a direct subject (e.g. `user:alice`) and the relation
143/// name for a userset subject (e.g. `family:smith#member`); see
144/// [`SubjectRef`] for the rationale.
145#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
146pub struct Tuple {
147 pub object_type: String,
148 pub object_id: String,
149 pub relation: String,
150 pub subject_type: String,
151 pub subject_id: String,
152 #[serde(default)]
153 pub subject_rel: String,
154}
155
156impl Tuple {
157 /// Direct grant — `user:alice` is `viewer` of `document:foo`.
158 pub fn direct(
159 object: impl Into<ObjectRef>,
160 relation: impl Into<String>,
161 subject: impl Into<SubjectRef>,
162 ) -> Self {
163 let o: ObjectRef = object.into();
164 let s: SubjectRef = subject.into();
165 Self {
166 object_type: o.object_type,
167 object_id: o.object_id,
168 relation: relation.into(),
169 subject_type: s.subject_type,
170 subject_id: s.subject_id,
171 subject_rel: s.subject_rel,
172 }
173 }
174
175 pub fn object(&self) -> ObjectRef {
176 ObjectRef::new(self.object_type.clone(), self.object_id.clone())
177 }
178
179 pub fn subject(&self) -> SubjectRef {
180 SubjectRef {
181 subject_type: self.subject_type.clone(),
182 subject_id: self.subject_id.clone(),
183 subject_rel: self.subject_rel.clone(),
184 }
185 }
186}
187
188/// Read-consistency mode for `check`-style queries. Closely matches
189/// the Zanzibar paper terminology and the SpiceDB API surface.
190///
191/// - [`Consistency::Minimum`] — read at any committed snapshot. Fastest,
192/// no staleness bound. Default for non-critical UI checks.
193/// - [`Consistency::AtLeastAsFresh`] — read at a snapshot at least as
194/// recent as the provided zookie. Used right after a write to read
195/// one's own writes.
196/// - [`Consistency::Exact`] — read at exactly this snapshot. Used for
197/// cache-friendly batched checks where every check should see the
198/// same world.
199///
200/// In v0.2.0 zookies are opaque transaction-id strings; the Postgres
201/// backend serialises `pg_current_wal_lsn()` and the SQLite backend
202/// uses a monotonic counter. The current check implementation is
203/// `Consistency::Minimum` only (the other modes pass through to the
204/// same code path); full snapshot enforcement is future work.
205#[derive(Clone, Debug, PartialEq, Eq)]
206#[derive(Default)]
207pub enum Consistency {
208 #[default]
209 Minimum,
210 AtLeastAsFresh(String),
211 Exact(String),
212}
213
214
215/// Result of a `check` call. `Allowed` carries the (best-effort) tuple
216/// path that resolved the permission so callers can show "why?" in a
217/// debug UI; the path may be empty if the storage layer chose to skip
218/// it for performance.
219///
220/// `DepthExceeded` and `CycleDetected` are *not* errors in the
221/// `Result` sense — they're a deliberate denial signal. A buggy schema
222/// shouldn't crash the request; it should deny the access and let the
223/// operator inspect the response.
224#[derive(Clone, Debug, PartialEq, Eq)]
225pub enum CheckResult {
226 Allowed { resolved_via: Vec<Tuple> },
227 Denied,
228 DepthExceeded,
229 CycleDetected,
230}
231
232impl CheckResult {
233 /// `true` iff [`CheckResult::Allowed`] — convenient for `if check.is_allowed()`.
234 pub fn is_allowed(&self) -> bool {
235 matches!(self, CheckResult::Allowed { .. })
236 }
237}
238
239/// Tree returned by [`super::ZanzibarStore::expand`]. Models the
240/// Zanzibar paper's "userset rewrite tree":
241///
242/// - [`UsersetTree::Leaf`] — terminal, a concrete user (or any
243/// no-relation subject).
244/// - [`UsersetTree::Node`] — an interior node showing how the
245/// permission was decomposed (union/intersect/exclude) plus the
246/// resolved children.
247///
248/// Mostly diagnostic — used by admin tooling and tests. The hot
249/// `check` path doesn't materialise a full tree.
250#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
251#[serde(tag = "kind", rename_all = "snake_case")]
252pub enum UsersetTree {
253 Leaf {
254 subject: SubjectRef,
255 },
256 Node {
257 op: TreeOp,
258 children: Vec<UsersetTree>,
259 },
260}
261
262/// How a non-leaf [`UsersetTree`] node combines its children.
263#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
264#[serde(rename_all = "snake_case")]
265pub enum TreeOp {
266 Union,
267 Intersect,
268 Exclude,
269 /// `viewer` resolved by following the named relation tuples
270 /// directly — the most common shape, e.g. `permission view = viewer`.
271 Direct,
272 /// Userset rewrite via `relation->permission` arrow.
273 TuplesetArrow,
274}
275
276/// Persisted namespace definition — written by `define_namespace`,
277/// read back by every `check` to resolve a permission name to its
278/// underlying relation set.
279///
280/// Kept simple on purpose: the parsed [`super::schema`] AST round-
281/// trips through `serde_json` into `auth.zanzibar_namespaces.schema_json`,
282/// so adding a new permission shape later only needs a parser change,
283/// not a storage migration.
284#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
285pub struct NamespaceSchema {
286 pub name: String,
287 /// Ordered map keyed by relation/permission name. `BTreeMap` keeps
288 /// JSON serialisation stable across runs (matters for diff-friendly
289 /// `auth.zanzibar_namespaces.schema_json` history).
290 pub definitions: BTreeMap<String, RelationDef>,
291}
292
293impl NamespaceSchema {
294 pub fn new(name: impl Into<String>) -> Self {
295 Self {
296 name: name.into(),
297 definitions: BTreeMap::new(),
298 }
299 }
300
301 pub fn with_relation(mut self, name: impl Into<String>, def: RelationDef) -> Self {
302 self.definitions.insert(name.into(), def);
303 self
304 }
305}
306
307/// A single line in a SpiceDB schema — `relation owner: user`,
308/// `permission view = owner + viewer`, etc. Holds either the parsed
309/// type list (for `relation` lines) or the algebraic expression (for
310/// `permission` lines).
311#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
312pub struct RelationDef {
313 pub name: String,
314 pub kind: RelationKind,
315}
316
317impl RelationDef {
318 pub fn relation(name: impl Into<String>, types: Vec<TypeRef>) -> Self {
319 Self {
320 name: name.into(),
321 kind: RelationKind::Direct(types),
322 }
323 }
324
325 pub fn permission(name: impl Into<String>, expr: PermissionExpr) -> Self {
326 Self {
327 name: name.into(),
328 kind: RelationKind::Permission(Box::new(expr)),
329 }
330 }
331}
332
333/// Categorises a definition line.
334///
335/// - [`RelationKind::Direct`] — `relation NAME: TYPE_LIST`. Only direct
336/// tuples count (no rewrite expansion).
337/// - [`RelationKind::Permission`] — `permission NAME = EXPR`. The
338/// expression is composed of unions / intersects / exclusions /
339/// tupleset arrows over relation names defined elsewhere in the
340/// namespace.
341#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
342#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
343pub enum RelationKind {
344 Direct(Vec<TypeRef>),
345 Permission(Box<PermissionExpr>),
346}
347
348/// A type reference on the right-hand side of a `relation` line.
349/// `user` is `TypeRef::direct("user")`; `family#member` is
350/// `TypeRef::userset("family", "member")`; `user:*` is the wildcard form.
351#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
352pub struct TypeRef {
353 pub object_type: String,
354 /// Userset reference — `family#member`. `None` = a direct subject.
355 #[serde(default)]
356 pub relation: Option<String>,
357 /// Wildcard subject id — `user:*`. When `true` the parser saw
358 /// `user:*` (any user is allowed) instead of just `user`. Treated
359 /// as a permission shape rather than a sentinel value at the SQL
360 /// layer. Defaults to `false` when the field is omitted by Lua /
361 /// JSON callers (the common case — wildcards are an escape hatch).
362 #[serde(default)]
363 pub wildcard: bool,
364}
365
366impl TypeRef {
367 pub fn direct(ty: impl Into<String>) -> Self {
368 Self {
369 object_type: ty.into(),
370 relation: None,
371 wildcard: false,
372 }
373 }
374
375 pub fn userset(ty: impl Into<String>, relation: impl Into<String>) -> Self {
376 Self {
377 object_type: ty.into(),
378 relation: Some(relation.into()),
379 wildcard: false,
380 }
381 }
382
383 pub fn wildcard(ty: impl Into<String>) -> Self {
384 Self {
385 object_type: ty.into(),
386 relation: None,
387 wildcard: true,
388 }
389 }
390}
391
392/// Algebraic permission expression — the right-hand side of a
393/// `permission NAME = EXPR` line.
394///
395/// Composes via:
396///
397/// - [`PermissionExpr::Direct`] — name of a relation/permission to
398/// resolve directly. The base case.
399/// - [`PermissionExpr::Union`] / `Intersect` / `Exclude` — set ops
400/// over two child expressions. Parsed as left-associative.
401/// - [`PermissionExpr::TuplesetArrow`] — `relation->permission` — for
402/// each tuple `(object, relation, intermediate_subject)`, recurse
403/// into `intermediate_subject` checking `permission`.
404#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
405#[serde(tag = "op", rename_all = "snake_case")]
406pub enum PermissionExpr {
407 Direct {
408 relation: String,
409 },
410 Union {
411 left: Box<PermissionExpr>,
412 right: Box<PermissionExpr>,
413 },
414 Intersect {
415 left: Box<PermissionExpr>,
416 right: Box<PermissionExpr>,
417 },
418 Exclude {
419 left: Box<PermissionExpr>,
420 right: Box<PermissionExpr>,
421 },
422 TuplesetArrow {
423 tupleset: String,
424 permission: String,
425 },
426}
427
428impl PermissionExpr {
429 pub fn direct(relation: impl Into<String>) -> Self {
430 Self::Direct {
431 relation: relation.into(),
432 }
433 }
434
435 pub fn union(l: PermissionExpr, r: PermissionExpr) -> Self {
436 Self::Union {
437 left: Box::new(l),
438 right: Box::new(r),
439 }
440 }
441
442 pub fn intersect(l: PermissionExpr, r: PermissionExpr) -> Self {
443 Self::Intersect {
444 left: Box::new(l),
445 right: Box::new(r),
446 }
447 }
448
449 pub fn exclude(l: PermissionExpr, r: PermissionExpr) -> Self {
450 Self::Exclude {
451 left: Box::new(l),
452 right: Box::new(r),
453 }
454 }
455
456 pub fn arrow(tupleset: impl Into<String>, permission: impl Into<String>) -> Self {
457 Self::TuplesetArrow {
458 tupleset: tupleset.into(),
459 permission: permission.into(),
460 }
461 }
462}
463
464/// Maximum recursion depth for `check` / `expand` walks. Matches plan
465/// 11's choice and the SpiceDB default. A real-world Zanzibar
466/// deployment rarely exceeds depth ~10; 50 leaves headroom for
467/// pathological-but-legitimate schemas (deeply nested groups).
468pub const MAX_DEPTH: u32 = 50;
469
470#[cfg(test)]
471mod tests {
472 use super::*;
473
474 #[test]
475 fn object_round_trips() {
476 let o = ObjectRef::new("document", "foo");
477 assert_eq!(o.render(), "document:foo");
478 assert_eq!(ObjectRef::parse("document:foo"), Some(o));
479 assert_eq!(ObjectRef::parse(""), None);
480 assert_eq!(ObjectRef::parse("nope"), None);
481 assert_eq!(ObjectRef::parse("a:"), None);
482 }
483
484 #[test]
485 fn subject_round_trips() {
486 let direct = SubjectRef::direct("user", "alice");
487 assert_eq!(direct.render(), "user:alice");
488 assert_eq!(SubjectRef::parse("user:alice"), Some(direct));
489
490 let userset = SubjectRef::userset("family", "ahmed", "member");
491 assert_eq!(userset.render(), "family:ahmed#member");
492 assert_eq!(SubjectRef::parse("family:ahmed#member"), Some(userset));
493
494 // Reject empty parts.
495 assert_eq!(SubjectRef::parse(""), None);
496 assert_eq!(SubjectRef::parse("user:#member"), None);
497 assert_eq!(SubjectRef::parse("family:ahmed#"), None);
498 }
499
500 #[test]
501 fn check_result_is_allowed() {
502 assert!(CheckResult::Allowed { resolved_via: vec![] }.is_allowed());
503 assert!(!CheckResult::Denied.is_allowed());
504 assert!(!CheckResult::DepthExceeded.is_allowed());
505 assert!(!CheckResult::CycleDetected.is_allowed());
506 }
507}