tollgate-core 0.30.1

Zero-I/O, clock-free domain layer for quota admission and accounting: cost tables, account snapshots, fenced local leases, and the reservation state machine.
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Identifier newtypes.
//!
//! Tollgate's identifiers are opaque 128-bit values so a store backend may use
//! UUIDs without this crate depending on a uuid library; 64-bit backends simply
//! use the low half. [`PolicyRevision`] is the one 256-bit member: it is not
//! Tollgate's identifier at all but the *consumer's*, carried through
//! admission and billing and never interpreted here.
//!
//! Every one of them shares a single canonical spelling rule — a fixed number
//! of lowercase hexadecimal digits, no `0x` — enforced by one function so the
//! two widths cannot drift apart.
//!
//! `FencingToken` and `Generation` are ordered u64 sequences, but they serve
//! different contracts: a fencing token identifies one lease capability, while
//! a generation rejects older account snapshots.

use core::{fmt, str::FromStr};

/// An opaque identifier was not written in Tollgate's canonical textual form.
///
/// The wire form is a fixed number of lowercase hexadecimal digits, without a
/// `0x` prefix. Keeping the parser strict gives display output, paths, and JSON
/// one representation rather than a collection of equivalent spellings.
///
/// The expected width travels with the error because Tollgate has identifiers
/// of two widths — 32 digits for the 128-bit types, 64 for
/// [`PolicyRevision`] — and one rule serving both must be able to say which it
/// was applying. A single error type stating the width is one rule; a second
/// error type beside a second parser would be two rules that have to agree.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseIdError {
    expected_digits: usize,
}

impl ParseIdError {
    const fn new(expected_digits: usize) -> Self {
        Self { expected_digits }
    }

    /// How many lowercase hexadecimal digits the canonical form has.
    #[must_use]
    pub const fn expected_digits(self) -> usize {
        self.expected_digits
    }
}

impl fmt::Display for ParseIdError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "identifier must be exactly {} lowercase hexadecimal digits",
            self.expected_digits
        )
    }
}

impl std::error::Error for ParseIdError {}

/// The one canonical-spelling rule, shared by every opaque identifier.
///
/// Width and character set only; what the digits decode *to* differs by type
/// and belongs to the caller. Uppercase is rejected, a `0x` prefix is rejected
/// (because `x` is not a hexadecimal digit), and the length must match
/// exactly — no padding, no truncation, no surrounding whitespace.
const fn validate_hex_digits(value: &str, expected_digits: usize) -> Result<(), ParseIdError> {
    if value.len() != expected_digits {
        return Err(ParseIdError::new(expected_digits));
    }
    let bytes = value.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        let byte = bytes[index];
        if !(byte.is_ascii_digit() || (byte >= b'a' && byte <= b'f')) {
            return Err(ParseIdError::new(expected_digits));
        }
        index += 1;
    }
    Ok(())
}

/// Digits per 128-bit identifier, and per 256-bit [`PolicyRevision`].
const ID_DIGITS: usize = 32;
const REVISION_DIGITS: usize = 64;

fn parse_id(value: &str) -> Result<u128, ParseIdError> {
    validate_hex_digits(value, ID_DIGITS)?;
    u128::from_str_radix(value, 16).map_err(|_| ParseIdError::new(ID_DIGITS))
}

macro_rules! id128 {
    ($(#[$doc:meta])* $name:ident) => {
        $(#[$doc])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $name(pub u128);

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{:032x}", self.0)
            }
        }

        impl FromStr for $name {
            type Err = ParseIdError;

            fn from_str(value: &str) -> Result<Self, Self::Err> {
                parse_id(value).map(Self)
            }
        }

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

        #[cfg(feature = "serde")]
        impl<'de> serde::Deserialize<'de> for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                struct IdVisitor;

                impl serde::de::Visitor<'_> for IdVisitor {
                    type Value = $name;

                    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                        formatter.write_str(
                            "an identifier containing exactly 32 lowercase hexadecimal digits",
                        )
                    }

                    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
                    where
                        E: serde::de::Error,
                    {
                        value.parse().map_err(E::custom)
                    }
                }

                deserializer.deserialize_str(IdVisitor)
            }
        }
    };
}

id128!(
    /// An account: the owner of quota, limits, and permissions.
    AccountId
);
id128!(
    /// A credential within an account; optional in contexts that admit an
    /// already-verified principal without key attribution.
    KeyId
);
id128!(
    /// One allocated lease of units to one service instance.
    LeaseId
);
id128!(
    /// Idempotency key for usage accounting: one request, one charge.
    RequestId
);
id128!(
    /// The request path's lookup key: an opaque fingerprint of an
    /// already-verified credential. How it is derived (API-key HMAC,
    /// capability subject, session id) is the embedding service's concern —
    /// by the time it reaches admission, verification has happened.
    ///
    /// # A caller must not be able to choose these bits
    ///
    /// Derive the fingerprint under a secret the caller does not hold, as the
    /// reference embedding does with a truncated HMAC-SHA256 of the API key.
    /// **Never key admission by a raw client-supplied token.**
    ///
    /// The reason is not subtle. This value selects an account's snapshot, its
    /// lease and its rate limiter. A caller who can choose it can aim at
    /// another tenant's entry — spending their quota, drawing on their limiter,
    /// and being admitted under their permissions. No hashing choice defends
    /// against that; only the derivation does.
    ///
    /// Because the bits are unsteerable, admission hashes them with a fast
    /// non-cryptographic hasher rather than SipHash (see
    /// `tollgate_admission`'s `PrincipalHasher`). That is a *consequence* of
    /// the rule above, not an additional requirement: an embedder who breaks
    /// it has already lost the tenant isolation SipHash was never protecting,
    /// and would merely lose it more slowly.
    Principal
);

/// One lease's capability token, drawn from a strictly increasing per-account
/// allocation sequence.
///
/// The ordering supplies an audit trail; it is not an account-wide validity
/// epoch. A newer token does not invalidate an older active lease. Stores
/// require this token to match the record named by the accompanying lease ID
/// (and account ID for usage ingest; INVARIANTS.md GL-4).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct FencingToken(pub u64);

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

/// Monotonic version of an account's compiled policy snapshot. A snapshot with
/// a generation older than the newest one an instance has seen is stale and
/// must not be (re-)installed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Generation(pub u64);

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

/// The consuming application's identity for the product policy compiled into a
/// snapshot — 256 opaque bits Tollgate carries and never interprets.
///
/// A service publishes compiled enforcement data (limits, a cost table,
/// permissions) derived from its own versioned product records. This is how it
/// says *which* records those were, so a response can report the policy that
/// priced a request and the billing event can be traced to the same one,
/// without any product vocabulary — plan, model, schedule, tier — entering
/// Tollgate. A content hash of the resolved inputs is the natural value; a
/// consumer with a shorter identifier zero-pads.
///
/// # Not a generation
///
/// [`Generation`] orders publication: it decides which snapshot is newer and
/// which is stale, and admission enforces it (INVARIANTS.md GL-15, GL-26). This
/// identifies the *inputs* compiled into a publication and carries no order at
/// all. Two generations can share a revision (the same policy republished after
/// a status change), and one generation carries exactly one revision. Neither
/// substitutes for the other, which is why this deliberately does **not**
/// derive `PartialOrd`/`Ord` as the identifier types do: comparing two of them
/// for order would be asking a question the value cannot answer, and the
/// answer would look plausible.
///
/// # Unstated is a value, not an error
///
/// [`Default`] is all zeroes and means "no revision stated". A control plane
/// that predates the field publishes snapshots without it, and they decode to
/// this rather than failing — the same fail-open-on-*identity* choice
/// `enforcement_mode` and `budget` make, and safe for the same reason: nothing
/// in Tollgate reads it, so an absent revision cannot change an enforcement
/// outcome.
///
/// # Wire form
///
/// Exactly 64 lowercase hexadecimal digits, under the same strict rule the
/// 128-bit identifiers use (INVARIANTS.md GL-21). Strictness matters more here
/// than elsewhere: the consumer compares these for equality to select its own
/// metadata, and two spellings of one revision would silently look like two
/// policies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct PolicyRevision(pub [u8; 32]);

impl PolicyRevision {
    /// The "no revision stated" value: all zeroes.
    pub const UNSTATED: Self = Self([0; 32]);

    /// Whether this is the unstated revision.
    #[must_use]
    pub const fn is_unstated(self) -> bool {
        let mut index = 0;
        while index < self.0.len() {
            if self.0[index] != 0 {
                return false;
            }
            index += 1;
        }
        true
    }

    /// The raw bytes, for a consumer storing or comparing them.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

impl fmt::Display for PolicyRevision {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.0 {
            write!(f, "{byte:02x}")?;
        }
        Ok(())
    }
}

impl FromStr for PolicyRevision {
    type Err = ParseIdError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        validate_hex_digits(value, REVISION_DIGITS)?;
        let mut bytes = [0u8; 32];
        for (index, byte) in bytes.iter_mut().enumerate() {
            let pair = &value[index * 2..index * 2 + 2];
            // The charset and width are already proven, so each pair is two
            // hexadecimal digits and this cannot fail. Delegating the nibble
            // arithmetic keeps it that way: a hand-rolled `(high << 4) | low`
            // reads correctly, but its `|` is indistinguishable from `^` and
            // `+` here because the halves never share a bit — an equivalent
            // mutant no test could ever kill, standing where a real one
            // should.
            *byte = u8::from_str_radix(pair, 16).map_err(|_| ParseIdError::new(REVISION_DIGITS))?;
        }
        Ok(Self(bytes))
    }
}

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

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for PolicyRevision {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct RevisionVisitor;

        impl serde::de::Visitor<'_> for RevisionVisitor {
            type Value = PolicyRevision;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str(
                    "a policy revision containing exactly 64 lowercase hexadecimal digits",
                )
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                value.parse().map_err(E::custom)
            }
        }

        deserializer.deserialize_str(RevisionVisitor)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! canonical_id_tests {
        ($($name:ident),+ $(,)?) => {
            $(
                assert_eq!($name(0).to_string(), "00000000000000000000000000000000");
                assert_eq!($name(u128::MAX).to_string(), "ffffffffffffffffffffffffffffffff");
                assert_eq!(
                    "8000000000000000000000000000002a".parse::<$name>(),
                    Ok($name((1u128 << 127) | 0x2a)),
                );
            )+
        };
    }

    #[test]
    fn every_id_uses_the_same_fixed_width_lowercase_hexadecimal_text() {
        canonical_id_tests!(AccountId, KeyId, LeaseId, RequestId, Principal);
    }

    #[test]
    fn noncanonical_spellings_are_rejected() {
        for value in [
            "1",
            "00000000000000000000000000000001 ",
            "0x00000000000000000000000000000001",
            "0000000000000000000000000000000A",
            "gggggggggggggggggggggggggggggggg",
        ] {
            assert_eq!(
                value.parse::<AccountId>(),
                Err(ParseIdError::new(ID_DIGITS)),
                "{value:?}"
            );
        }
    }

    /// The same rule at the other width, with the same rejection corpus scaled
    /// up — uppercase, a prefix, trailing space, a non-hex digit.
    #[test]
    fn noncanonical_revision_spellings_are_rejected() {
        let ok = "8".repeat(64);
        assert!(
            ok.parse::<PolicyRevision>().is_ok(),
            "the corpus baseline parses"
        );
        for value in [
            "1".to_string(),
            format!("{}{}", "0".repeat(63), "1 "),
            format!("0x{}", "0".repeat(64)),
            format!("{}A", "0".repeat(63)),
            "g".repeat(64),
        ] {
            assert_eq!(
                value.parse::<PolicyRevision>(),
                Err(ParseIdError::new(REVISION_DIGITS)),
                "{value:?}"
            );
        }
    }

    /// The two widths are enforced separately, and each rejects the other's
    /// canonical form. Sharing one rule must not mean sharing one width: a
    /// 32-digit value is a perfectly good identifier and not a revision at all.
    #[test]
    fn the_two_identifier_widths_reject_each_others_canonical_form() {
        let id_width = "0".repeat(32);
        let revision_width = "0".repeat(64);

        assert!(id_width.parse::<AccountId>().is_ok());
        assert_eq!(
            id_width.parse::<PolicyRevision>(),
            Err(ParseIdError::new(REVISION_DIGITS))
        );

        assert!(revision_width.parse::<PolicyRevision>().is_ok());
        assert_eq!(
            revision_width.parse::<AccountId>(),
            Err(ParseIdError::new(ID_DIGITS))
        );
    }

    /// The error says which width it was applying, so a caller reading it is
    /// not told a 64-digit value should have been 32.
    #[test]
    fn the_parse_error_names_the_width_it_expected() {
        let id_error = "".parse::<AccountId>().unwrap_err();
        assert_eq!(id_error.expected_digits(), 32);
        assert_eq!(
            id_error.to_string(),
            "identifier must be exactly 32 lowercase hexadecimal digits"
        );

        let revision_error = "".parse::<PolicyRevision>().unwrap_err();
        assert_eq!(revision_error.expected_digits(), 64);
        assert_eq!(
            revision_error.to_string(),
            "identifier must be exactly 64 lowercase hexadecimal digits"
        );
    }

    /// The unstated revision is a value, not an absence: it has a canonical
    /// spelling, it round-trips, and it reports itself as unstated.
    #[test]
    fn the_unstated_revision_is_all_zeroes_and_round_trips() {
        let unstated = PolicyRevision::default();
        assert_eq!(unstated, PolicyRevision::UNSTATED);
        assert!(unstated.is_unstated());
        assert_eq!(unstated.to_string(), "0".repeat(64));
        assert_eq!(unstated.to_string().parse::<PolicyRevision>(), Ok(unstated));

        let stated = PolicyRevision([0xab; 32]);
        assert!(!stated.is_unstated());
        assert_eq!(stated.to_string(), "ab".repeat(32));
        assert_eq!(stated.to_string().parse::<PolicyRevision>(), Ok(stated));
    }

    /// Every byte position survives the text round trip in the right order —
    /// a transposition would still be 64 valid digits, so the pattern is
    /// deliberately asymmetric.
    #[test]
    fn a_revision_round_trips_every_byte_position_in_order() {
        let mut bytes = [0u8; 32];
        for (index, byte) in bytes.iter_mut().enumerate() {
            *byte = index as u8;
        }
        let revision = PolicyRevision(bytes);
        assert_eq!(
            revision.to_string(),
            "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
        );
        assert_eq!(revision.to_string().parse::<PolicyRevision>(), Ok(revision));
    }

    #[cfg(feature = "serde")]
    #[test]
    fn human_readable_serde_is_textual_and_strict() {
        macro_rules! assert_textual {
            ($name:ident) => {{
                let value = $name((1u128 << 127) | 0x2a);
                let encoded = serde_json::to_string(&value).unwrap();
                assert_eq!(encoded, r#""8000000000000000000000000000002a""#);
                assert_eq!(serde_json::from_str::<$name>(&encoded).unwrap(), value);
                assert!(serde_json::from_str::<$name>("42").is_err());
            }};
        }

        assert_textual!(AccountId);
        assert_textual!(KeyId);
        assert_textual!(LeaseId);
        assert_textual!(RequestId);
        assert_textual!(Principal);
    }

    /// The revision follows the same textual-and-strict Serde contract: a JSON
    /// string in canonical form, and a number is refused rather than coerced.
    #[cfg(feature = "serde")]
    #[test]
    fn revision_serde_is_textual_and_strict() {
        let value = PolicyRevision([0x8f; 32]);
        let encoded = serde_json::to_string(&value).unwrap();
        assert_eq!(encoded, format!("\"{}\"", "8f".repeat(32)));
        assert_eq!(
            serde_json::from_str::<PolicyRevision>(&encoded).unwrap(),
            value
        );
        // A number is refused, and the refusal says what was expected. The
        // visitor's `expecting` text is the only thing telling a caller what
        // shape the field wanted, so it is asserted rather than assumed —
        // otherwise it could return nothing at all and no test would notice.
        let wrong_type =
            serde_json::from_str::<PolicyRevision>("42").expect_err("a number is not a revision");
        assert!(
            wrong_type
                .to_string()
                .contains("exactly 64 lowercase hexadecimal digits"),
            "the type error must name the expected form, got {wrong_type}"
        );
        // An identifier-width string is not a revision either, and that
        // refusal carries the parse rule's own width.
        let wrong_width =
            serde_json::from_str::<PolicyRevision>(&format!("\"{}\"", "0".repeat(32)))
                .expect_err("32 digits is not a revision");
        assert!(
            wrong_width
                .to_string()
                .contains("exactly 64 lowercase hexadecimal digits"),
            "the width error must name the expected width, got {wrong_width}"
        );
    }
}