Skip to main content

qualia_core_db/net/qdnf/contracts/
generations.rs

1//! Source / policy / identity generations bound into compiled decisions.
2//!
3//! A live generation past the bound is StaleGeneration, never Allow.
4
5use crate::net::qdnf::authority::PolicyOutcome;
6use crate::net::qdnf::errors::QdnfError;
7use crate::net::qdnf::types::Generation;
8
9/// Generations captured when the decision was compiled.
10#[repr(C)]
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub struct BoundGenerations {
13    pub source: Generation,
14    pub policy: Generation,
15    pub identity: Generation,
16}
17
18/// Live generations at commit / release.
19#[repr(C)]
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct LiveGenerations {
22    pub source: Generation,
23    pub policy: Generation,
24    pub identity: Generation,
25}
26
27impl BoundGenerations {
28    /// Creates a default `BoundGenerations`.
29    pub const fn new(source: Generation, policy: Generation, identity: Generation) -> Self {
30        Self {
31            source,
32            policy,
33            identity,
34        }
35    }
36
37    #[inline]
38    /// `BoundGenerations::as_live`.
39    pub const fn as_live(self) -> LiveGenerations {
40        LiveGenerations {
41            source: self.source,
42            policy: self.policy,
43            identity: self.identity,
44        }
45    }
46}
47
48/// Recheck a queued compiled permit at commit/release.
49///
50/// Any live generation greater than the bound is [`QdnfError::StaleGeneration`].
51/// Zero bound generations are not a compiled permit.
52pub fn recheck_permit(bound: BoundGenerations, live: LiveGenerations) -> Result<(), QdnfError> {
53    if bound.source == Generation::ZERO
54        || bound.policy == Generation::ZERO
55        || bound.identity == Generation::ZERO
56    {
57        return Err(QdnfError::Unauthorized);
58    }
59    if live.source == Generation::ZERO
60        || live.policy == Generation::ZERO
61        || live.identity == Generation::ZERO
62    {
63        return Err(QdnfError::Unauthorized);
64    }
65    if live.source.0 > bound.source.0
66        || live.policy.0 > bound.policy.0
67        || live.identity.0 > bound.identity.0
68    {
69        return Err(QdnfError::StaleGeneration);
70    }
71    Ok(())
72}
73
74/// Bind an already-compiled outcome at the commit boundary. Stale is not Allow.
75pub fn commit_compiled(
76    outcome: PolicyOutcome,
77    bound: BoundGenerations,
78    live: LiveGenerations,
79) -> Result<PolicyOutcome, QdnfError> {
80    recheck_permit(bound, live)?;
81    Ok(outcome)
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    fn bound() -> BoundGenerations {
89        BoundGenerations::new(Generation(3), Generation(5), Generation(2))
90    }
91
92    #[test]
93    fn matching_live_generations_pass() {
94        let b = bound();
95        assert!(recheck_permit(b, b.as_live()).is_ok());
96        assert_eq!(
97            commit_compiled(PolicyOutcome::Allow, b, b.as_live()).unwrap(),
98            PolicyOutcome::Allow
99        );
100    }
101
102    #[test]
103    fn source_generation_advance_is_stale_not_allow() {
104        let b = bound();
105        let mut live = b.as_live();
106        live.source = Generation(4);
107        assert_eq!(recheck_permit(b, live), Err(QdnfError::StaleGeneration));
108        assert_eq!(
109            commit_compiled(PolicyOutcome::Allow, b, live),
110            Err(QdnfError::StaleGeneration)
111        );
112    }
113
114    #[test]
115    fn policy_generation_advance_is_stale_not_allow() {
116        let b = bound();
117        let mut live = b.as_live();
118        live.policy = Generation(9);
119        assert_eq!(recheck_permit(b, live), Err(QdnfError::StaleGeneration));
120    }
121
122    #[test]
123    fn identity_generation_advance_is_stale_not_allow() {
124        let b = bound();
125        let mut live = b.as_live();
126        live.identity = Generation(3);
127        assert_eq!(recheck_permit(b, live), Err(QdnfError::StaleGeneration));
128    }
129
130    #[test]
131    fn zero_bound_is_unauthorized() {
132        let live = LiveGenerations {
133            source: Generation(1),
134            policy: Generation(1),
135            identity: Generation(1),
136        };
137        let bound = BoundGenerations::new(Generation::ZERO, Generation(1), Generation(1));
138        assert_eq!(recheck_permit(bound, live), Err(QdnfError::Unauthorized));
139    }
140}