Skip to main content

gent_crypto/authority/
hosting.rs

1//! Signed chat transport delegation preserves logical node and key identities.
2use super::*;
3
4impl ClusterState {
5    pub(super) fn transport_host(&self, node: &str) -> Result<&str> {
6        let (node, member) = self.nodes.get_key_value(node).context("node unavailable")?;
7        let Some(parent) = &member.hosted_by else {
8            return Ok(node);
9        };
10        ensure!(parent != node, "node cannot host itself");
11        let (parent, host) = self
12            .nodes
13            .get_key_value(parent)
14            .context("hosting parent unavailable")?;
15        ensure!(host.hosted_by.is_none(), "hosting parent must be native");
16        Ok(parent)
17    }
18
19    pub(super) fn require_no_hosted_children(&self, node: &str) -> Result<()> {
20        ensure!(
21            !self
22                .nodes
23                .values()
24                .any(|member| member.hosted_by.as_deref() == Some(node)),
25            "remove hosted children before removing or replacing their parent"
26        );
27        Ok(())
28    }
29}
30
31pub(super) fn verify_step(previous: &ClusterState, next: &ClusterState) -> Result<()> {
32    for (id, member) in &previous.nodes {
33        if member.hosted_by.is_some()
34            && let Some(replacement) = next.nodes.get(id)
35        {
36            ensure!(
37                replacement.hosted_by.is_some(),
38                "a hosted node cannot become a native node"
39            );
40        }
41    }
42    Ok(())
43}
44
45impl VerifiedState {
46    /// Resolve a live logical node to its native transport host. This does not
47    /// check freshness or authenticate the host's key.
48    pub fn transport_host(&self, node: &str) -> Result<&str> {
49        self.state().transport_host(node)
50    }
51
52    /// Check only the signed transport mapping. The caller must first use
53    /// authorize_node on this same verified state to authenticate the carrier's
54    /// exact node key and check freshness at the current wall-clock time.
55    /// Hosting permits chat transport only, never configuration operations,
56    /// and does not replace per-chat permission checks.
57    pub fn authorize_chat_carrier(
58        &self,
59        logical_node: &str,
60        authenticated_node: &str,
61    ) -> Result<()> {
62        ensure!(
63            self.transport_host(logical_node)? == authenticated_node,
64            "unauthorized chat carrier"
65        );
66        Ok(())
67    }
68
69    /// Assign a native parent using existing configuration signing authority.
70    /// Repeating the same mapping is allowed and changes no configuration grants.
71    pub fn host_node(
72        &self,
73        owner: &(impl peer::ConfigurationSigner + ?Sized),
74        node_id: &str,
75        parent: &str,
76        now: u64,
77    ) -> Result<SignedState> {
78        self.require_fresh(now)?;
79        self.revise(owner, now, |state| {
80            let member = state.nodes.get_mut(node_id).context("node unavailable")?;
81            member.hosted_by = Some(parent.into());
82            // Validate every relationship: hosting a node that already hosts
83            // children would otherwise introduce a chain indirectly.
84            for node in state.nodes.keys() {
85                state.transport_host(node)?;
86            }
87            Ok(())
88        })
89    }
90}