simple-zanzibar 0.3.0

A simplified Rust implementation of Google's Zanzibar authorization system with DSL support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Revision, consistency token, and published snapshot types.

use std::{
    collections::HashMap,
    fmt,
    num::{NonZeroU64, NonZeroUsize},
    str::FromStr,
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
    time::{SystemTime, UNIX_EPOCH},
};

use thiserror::Error;

use crate::{
    model::NamespaceConfig,
    relationship::RelationshipStoreView,
    schema::{
        AllowedSubjectTypes, CompiledSchema, NamespaceDefinition, RelationDefinition,
        UsersetExpression,
    },
};

const TOKEN_VERSION: &str = "sz1";
const MAX_CONSISTENCY_TOKEN_BYTES: usize = 122;
const DEFAULT_RETAINED_SNAPSHOTS: usize = 32;
static NEXT_DATASTORE_COUNTER: AtomicU64 = AtomicU64::new(1);

/// Errors produced by revision and consistency-token handling.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ConsistencyError {
    /// A token string does not match the supported grammar.
    #[error("invalid consistency token: {reason}")]
    InvalidToken {
        /// Static parse failure reason.
        reason: &'static str,
    },

    /// A token came from a different datastore instance.
    #[error("consistency token datastore id does not match this engine")]
    WrongDatastore,

    /// A token references a revision older than retained history.
    #[error("revision {revision} is no longer retained")]
    RevisionExpired {
        /// Expired revision.
        revision: Revision,
    },

    /// A token references a revision that has not been published by this engine.
    #[error("revision {revision} is not available")]
    RevisionUnavailable {
        /// Unavailable revision.
        revision: Revision,
    },

    /// A token revision exists but the schema hash does not match the retained snapshot.
    #[error("consistency token schema hash does not match retained revision {revision}")]
    SchemaHashMismatch {
        /// Revision with mismatched schema hash.
        revision: Revision,
    },

    /// The engine cannot publish another revision without overflowing.
    #[error("revision counter overflowed")]
    RevisionOverflow,
}

/// Monotonic non-zero revision identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Revision(NonZeroU64);

impl Revision {
    /// Returns the first publishable revision.
    #[must_use]
    pub const fn first() -> Self {
        Self(NonZeroU64::MIN)
    }

    /// Creates a revision from a non-zero value.
    #[must_use]
    pub const fn new(value: NonZeroU64) -> Self {
        Self(value)
    }

    /// Returns the revision as a primitive integer.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0.get()
    }

    /// Returns the next revision.
    ///
    /// # Errors
    ///
    /// Returns [`ConsistencyError::RevisionOverflow`] when incrementing would overflow `u64`.
    pub fn next(self) -> Result<Self, ConsistencyError> {
        let value = self
            .get()
            .checked_add(1)
            .ok_or(ConsistencyError::RevisionOverflow)?;
        NonZeroU64::new(value)
            .map(Self)
            .ok_or(ConsistencyError::RevisionOverflow)
    }
}

impl fmt::Display for Revision {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}", self.get())
    }
}

impl FromStr for Revision {
    type Err = ConsistencyError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let parsed = value
            .parse::<u64>()
            .map_err(|_| ConsistencyError::InvalidToken {
                reason: "revision must be a non-zero unsigned integer",
            })?;
        NonZeroU64::new(parsed)
            .map(Self)
            .ok_or(ConsistencyError::InvalidToken {
                reason: "revision must be non-zero",
            })
    }
}

/// Per-engine datastore identifier used to reject foreign tokens.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DatastoreId([u8; 16]);

impl DatastoreId {
    /// Creates a best-effort unique datastore id for an in-memory engine instance.
    #[must_use]
    pub fn new_unique() -> Self {
        let counter = NEXT_DATASTORE_COUNTER.fetch_add(1, Ordering::Relaxed);
        let elapsed = match SystemTime::now().duration_since(UNIX_EPOCH) {
            Ok(duration) => duration.as_nanos(),
            Err(error) => error.duration().as_nanos(),
        };
        let mut hasher = blake3::Hasher::new();
        hasher.update(&counter.to_le_bytes());
        hasher.update(&elapsed.to_le_bytes());
        hasher.update(&u64::from(std::process::id()).to_le_bytes());
        let hash = hasher.finalize();
        let mut bytes = [0_u8; 16];
        for (target, source) in bytes.iter_mut().zip(hash.as_bytes().iter()) {
            *target = *source;
        }
        Self(bytes)
    }

    /// Creates an id from raw bytes.
    #[must_use]
    pub const fn from_bytes(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }

    /// Returns raw id bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 16] {
        &self.0
    }
}

/// Canonical hash of a compiled schema.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SchemaHash([u8; 32]);

impl SchemaHash {
    /// Computes a canonical schema hash.
    #[must_use]
    pub fn for_schema(schema: &CompiledSchema) -> Self {
        let mut hasher = blake3::Hasher::new();
        let mut namespaces = schema.definitions().iter().collect::<Vec<_>>();
        namespaces.sort_by(|left, right| left.name().as_str().cmp(right.name().as_str()));

        for namespace in namespaces {
            update_namespace(&mut hasher, namespace);
        }

        Self(*hasher.finalize().as_bytes())
    }

    /// Creates a schema hash from raw bytes.
    #[must_use]
    pub const fn from_bytes(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Returns raw hash bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

/// Stable exact-snapshot token returned by writes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsistencyToken {
    revision: Revision,
    schema_hash: SchemaHash,
    datastore_id: DatastoreId,
}

impl ConsistencyToken {
    /// Creates a token from validated parts.
    #[must_use]
    pub const fn new(
        revision: Revision,
        schema_hash: SchemaHash,
        datastore_id: DatastoreId,
    ) -> Self {
        Self {
            revision,
            schema_hash,
            datastore_id,
        }
    }

    /// Returns the token revision.
    #[must_use]
    pub const fn revision(&self) -> Revision {
        self.revision
    }

    /// Returns the token schema hash.
    #[must_use]
    pub const fn schema_hash(&self) -> SchemaHash {
        self.schema_hash
    }

    /// Returns the token datastore id.
    #[must_use]
    pub const fn datastore_id(&self) -> DatastoreId {
        self.datastore_id
    }
}

impl fmt::Display for ConsistencyToken {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{TOKEN_VERSION}:{}:", self.revision)?;
        write_hex(formatter, self.schema_hash.as_bytes())?;
        formatter.write_str(":")?;
        write_hex(formatter, self.datastore_id.as_bytes())
    }
}

impl FromStr for ConsistencyToken {
    type Err = ConsistencyError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.len() > MAX_CONSISTENCY_TOKEN_BYTES {
            return Err(ConsistencyError::InvalidToken {
                reason: "token exceeds maximum byte length",
            });
        }

        let mut parts = value.split(':');
        let version = parts.next().ok_or(ConsistencyError::InvalidToken {
            reason: "missing token version",
        })?;
        if version != TOKEN_VERSION {
            return Err(ConsistencyError::InvalidToken {
                reason: "unsupported token version",
            });
        }
        let revision = parts
            .next()
            .ok_or(ConsistencyError::InvalidToken {
                reason: "missing revision",
            })?
            .parse()?;
        let schema_hash = SchemaHash::from_bytes(decode_hex(parts.next().ok_or(
            ConsistencyError::InvalidToken {
                reason: "missing schema hash",
            },
        )?)?);
        let datastore_id = DatastoreId::from_bytes(decode_hex(parts.next().ok_or(
            ConsistencyError::InvalidToken {
                reason: "missing datastore id",
            },
        )?)?);
        if parts.next().is_some() {
            return Err(ConsistencyError::InvalidToken {
                reason: "too many token fields",
            });
        }
        Ok(Self::new(revision, schema_hash, datastore_id))
    }
}

/// Read consistency mode.
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase", tag = "kind", content = "token")
)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Consistency {
    /// Read the latest published snapshot.
    Latest,
    /// Read exactly at a previously returned token.
    Exact(ConsistencyToken),
}

#[cfg(feature = "serde")]
impl serde::Serialize for ConsistencyToken {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ConsistencyToken {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = <String as serde::Deserialize>::deserialize(deserializer)?;
        Self::from_str(&value).map_err(serde::de::Error::custom)
    }
}

/// Published immutable snapshot.
#[derive(Debug, Clone)]
pub struct PublishedSnapshot {
    revision: Revision,
    schema_hash: SchemaHash,
    configs: Arc<HashMap<String, NamespaceConfig>>,
    schema: Arc<CompiledSchema>,
    relationships: Arc<RelationshipStoreView>,
}

impl PublishedSnapshot {
    /// Creates a published snapshot from immutable state.
    #[must_use]
    pub fn new(
        revision: Revision,
        schema_hash: SchemaHash,
        configs: Arc<HashMap<String, NamespaceConfig>>,
        schema: Arc<CompiledSchema>,
        relationships: Arc<RelationshipStoreView>,
    ) -> Self {
        Self {
            revision,
            schema_hash,
            configs,
            schema,
            relationships,
        }
    }

    /// Returns the snapshot revision.
    #[must_use]
    pub const fn revision(&self) -> Revision {
        self.revision
    }

    /// Returns the snapshot schema hash.
    #[must_use]
    pub const fn schema_hash(&self) -> SchemaHash {
        self.schema_hash
    }

    /// Returns legacy namespace configs retained for compatibility evaluation.
    #[must_use]
    pub fn configs(&self) -> &HashMap<String, NamespaceConfig> {
        &self.configs
    }

    /// Returns the compiled schema.
    #[must_use]
    pub fn schema(&self) -> &CompiledSchema {
        &self.schema
    }

    /// Returns the published relationship store view.
    #[must_use]
    pub fn relationships(&self) -> &RelationshipStoreView {
        &self.relationships
    }
}

/// Returns the default snapshot retention.
#[must_use]
pub fn default_retained_snapshots() -> NonZeroUsize {
    match NonZeroUsize::new(DEFAULT_RETAINED_SNAPSHOTS) {
        Some(value) => value,
        None => NonZeroUsize::MIN,
    }
}

fn update_namespace(hasher: &mut blake3::Hasher, namespace: &NamespaceDefinition) {
    update_str(hasher, "namespace");
    update_str(hasher, namespace.name().as_str());
    let mut relations = namespace.relations().iter().collect::<Vec<_>>();
    relations.sort_by(|left, right| left.name().as_str().cmp(right.name().as_str()));
    for relation in relations {
        update_relation(hasher, relation);
    }
}

fn update_relation(hasher: &mut blake3::Hasher, relation: &RelationDefinition) {
    update_str(hasher, "relation");
    update_str(hasher, relation.name().as_str());
    match relation.allowed_subject_types() {
        AllowedSubjectTypes::Unspecified => update_str(hasher, "subjects_unspecified"),
        AllowedSubjectTypes::Explicit(subjects) => {
            update_str(hasher, "subjects_explicit");
            let mut subject_types = subjects.iter().collect::<Vec<_>>();
            subject_types.sort_by(|left, right| left.as_str().cmp(right.as_str()));
            for subject_type in subject_types {
                update_str(hasher, subject_type.as_str());
            }
        }
    }
    match relation.userset_rewrite() {
        Some(expression) => update_expression(hasher, expression),
        None => update_str(hasher, "rewrite_none"),
    }
}

fn update_expression(hasher: &mut blake3::Hasher, expression: &UsersetExpression) {
    match expression {
        UsersetExpression::This => update_str(hasher, "this"),
        UsersetExpression::ComputedUserset { relation } => {
            update_str(hasher, "computed_userset");
            update_str(hasher, relation.as_str());
        }
        UsersetExpression::TupleToUserset {
            tupleset_relation,
            computed_userset_relation,
        } => {
            update_str(hasher, "tuple_to_userset");
            update_str(hasher, tupleset_relation.as_str());
            update_str(hasher, computed_userset_relation.as_str());
        }
        UsersetExpression::Union(expressions) => {
            update_str(hasher, "union");
            for child in expressions {
                update_expression(hasher, child);
            }
        }
        UsersetExpression::Intersection(expressions) => {
            update_str(hasher, "intersection");
            for child in expressions {
                update_expression(hasher, child);
            }
        }
        UsersetExpression::Exclusion { base, exclude } => {
            update_str(hasher, "exclusion");
            update_expression(hasher, base);
            update_expression(hasher, exclude);
        }
    }
}

fn update_str(hasher: &mut blake3::Hasher, value: &str) {
    hasher.update(&value.len().to_le_bytes());
    hasher.update(value.as_bytes());
}

fn write_hex(formatter: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
    for byte in bytes {
        write!(formatter, "{byte:02x}")?;
    }
    Ok(())
}

fn decode_hex<const N: usize>(value: &str) -> Result<[u8; N], ConsistencyError> {
    if value.len() != N.saturating_mul(2) {
        return Err(ConsistencyError::InvalidToken {
            reason: "hex field length is invalid",
        });
    }

    let mut decoded = [0_u8; N];
    for (target, pair) in decoded.iter_mut().zip(value.as_bytes().chunks_exact(2)) {
        let high = pair
            .first()
            .copied()
            .ok_or(ConsistencyError::InvalidToken {
                reason: "hex field length is invalid",
            })?;
        let low = pair.get(1).copied().ok_or(ConsistencyError::InvalidToken {
            reason: "hex field length is invalid",
        })?;
        *target = decode_nibble(high)?
            .checked_mul(16)
            .and_then(|left| left.checked_add(decode_nibble(low).ok()?))
            .ok_or(ConsistencyError::InvalidToken {
                reason: "hex field contains invalid digit",
            })?;
    }
    Ok(decoded)
}

fn decode_nibble(value: u8) -> Result<u8, ConsistencyError> {
    match value {
        b'0'..=b'9' => Ok(value - b'0'),
        b'a'..=b'f' => Ok(value - b'a' + 10),
        b'A'..=b'F' => Ok(value - b'A' + 10),
        _ => Err(ConsistencyError::InvalidToken {
            reason: "hex field contains invalid digit",
        }),
    }
}