Skip to main content

qualia_core_db/net/peer/fabric/
session.rs

1//! SessionReady exists only after verified path evidence and a live grant.
2
3use super::carrier::prohibited;
4use super::evidence::PathEvidence;
5use super::intent::PeerId;
6use super::kernel::{FabricError, Kernel};
7
8/// Successful application session. Fields are readable; construction is crate-private.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct SessionReady {
11    pub peer: PeerId,
12    pub path: PathEvidence,
13    pub generation: u32,
14}
15
16impl SessionReady {
17    /// Attempts `new`; error/`None` on failure.
18    pub(crate) fn try_new(
19        kernel: &Kernel,
20        peer: PeerId,
21        path: PathEvidence,
22    ) -> Result<Self, FabricError> {
23        if !kernel.grant_live {
24            return Err(FabricError::GrantRevoked);
25        }
26        if !path.validated() || path.stale_generation(kernel.generation) {
27            return Err(FabricError::Illegal);
28        }
29        if kernel.state != super::kernel::FabricState::SessionLive {
30            return Err(FabricError::Illegal);
31        }
32        let intent = kernel.intent().ok_or(FabricError::Illegal)?;
33        if prohibited(intent.protection.disclosure, path.class) {
34            return Err(FabricError::PolicyDenied);
35        }
36        if kernel.selected != Some(path.class) {
37            return Err(FabricError::Illegal);
38        }
39        Ok(Self {
40            peer,
41            path,
42            generation: kernel.generation,
43        })
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::net::peer::fabric::carrier::PathClass;
51    use crate::net::peer::fabric::evidence::{PathEvidence, TransportWitness};
52    use crate::net::peer::fabric::intent::{ConnectionIntent, ProtectionPolicy, Purpose};
53    use crate::net::peer::fabric::kernel::{Kernel, KernelEvent};
54    use crate::net::peer::runtime::ResourceBudget;
55
56    #[test]
57    fn no_public_session_without_kernel_admit() {
58        let mut k = Kernel::new();
59        k.admit(
60            ConnectionIntent::new(
61                [9u8; 32],
62                Purpose::ordinary(),
63                ProtectionPolicy::ORDINARY,
64                ResourceBudget {
65                    bytes: 64,
66                    work: 1,
67                    io: 1,
68                },
69                0,
70                1000,
71            ),
72            0,
73        )
74        .unwrap();
75        let path = PathEvidence::from_witness(TransportWitness::from_local(
76            PathClass::Relayed,
77            1,
78            64,
79            5,
80            1,
81        ));
82        assert_eq!(
83            SessionReady::try_new(&k, [9u8; 32], path),
84            Err(FabricError::Illegal)
85        );
86        k.step(
87            KernelEvent::Descriptor {
88                stale: false,
89                expired: false,
90                direct_locator: false,
91            },
92            1,
93        );
94        k.step(KernelEvent::LeaseLive, 1);
95        k.step(
96            KernelEvent::PathValidated {
97                class: PathClass::Relayed,
98            },
99            1,
100        );
101        k.step(KernelEvent::SessionAuthenticated, 1);
102        assert!(SessionReady::try_new(&k, [9u8; 32], path).is_ok());
103    }
104
105    #[test]
106    fn relay_only_session_rejects_direct_evidence() {
107        let mut k = Kernel::new();
108        k.admit(
109            ConnectionIntent::new(
110                [9u8; 32],
111                Purpose::ordinary(),
112                ProtectionPolicy::RELAY_ONLY,
113                ResourceBudget {
114                    bytes: 64,
115                    work: 1,
116                    io: 1,
117                },
118                0,
119                1000,
120            ),
121            0,
122        )
123        .unwrap();
124        k.step(
125            KernelEvent::Descriptor {
126                stale: false,
127                expired: false,
128                direct_locator: false,
129            },
130            1,
131        );
132        k.step(KernelEvent::LeaseLive, 1);
133        k.step(
134            KernelEvent::PathValidated {
135                class: PathClass::Relayed,
136            },
137            1,
138        );
139        k.step(KernelEvent::SessionAuthenticated, 1);
140        let relayed = PathEvidence::from_witness(TransportWitness::from_local(
141            PathClass::Relayed,
142            1,
143            64,
144            5,
145            1,
146        ));
147        assert!(SessionReady::try_new(&k, [9u8; 32], relayed).is_ok());
148        let direct = PathEvidence::from_witness(TransportWitness::from_local(
149            PathClass::DirectV6,
150            1,
151            64,
152            1,
153            1,
154        ));
155        assert_eq!(
156            SessionReady::try_new(&k, [9u8; 32], direct),
157            Err(FabricError::PolicyDenied)
158        );
159    }
160}