Skip to main content

lean_ctx_ext/core/trust/
tenant_isolation.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Small, composable tenant-boundary checks.
3
4use std::fmt;
5
6use super::identity::{IdentityContext, TenantId};
7
8/// Marker/access trait implemented by values that carry a tenant boundary.
9pub trait TenantBoundary {
10    /// Returns the tenant that owns the value.
11    fn tenant_id(&self) -> &TenantId;
12}
13
14impl TenantBoundary for TenantId {
15    fn tenant_id(&self) -> &TenantId {
16        self
17    }
18}
19
20impl TenantBoundary for IdentityContext {
21    fn tenant_id(&self) -> &TenantId {
22        &self.tenant
23    }
24}
25
26/// Error returned when two tenant-scoped values would be joined across a
27/// boundary.
28#[derive(Clone, Debug, Eq, PartialEq)]
29pub struct CrossTenantLeak {
30    /// Tenant selected by the left/owning value.
31    pub expected: TenantId,
32    /// Tenant carried by the right/referenced value.
33    pub actual: TenantId,
34}
35
36impl CrossTenantLeak {
37    /// Creates a cross-tenant error from the two observed tenants.
38    #[must_use]
39    pub fn new(expected: TenantId, actual: TenantId) -> Self {
40        Self { expected, actual }
41    }
42}
43
44impl fmt::Display for CrossTenantLeak {
45    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(
47            formatter,
48            "tenant boundary violation: expected {}, found {}",
49            self.expected, self.actual
50        )
51    }
52}
53
54impl std::error::Error for CrossTenantLeak {}
55
56/// Stateless isolation helper for pair and collection checks.
57#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
58pub struct IsolationCheck;
59
60impl IsolationCheck {
61    /// Checks that two tenant-scoped values belong to the same tenant.
62    pub fn same_tenant<A: TenantBoundary + ?Sized, B: TenantBoundary + ?Sized>(
63        left: &A,
64        right: &B,
65    ) -> Result<(), CrossTenantLeak> {
66        assert_same_tenant(left, right)
67    }
68
69    /// Checks that every value in a collection belongs to its first tenant.
70    pub fn all_same_tenant<T: TenantBoundary>(values: &[T]) -> Result<(), CrossTenantLeak> {
71        let Some(first) = values.first() else {
72            return Ok(());
73        };
74        for value in values.iter().skip(1) {
75            assert_same_tenant(first, value)?;
76        }
77        Ok(())
78    }
79
80    /// Alias suitable for call sites that describe the operation as a check.
81    pub fn verify<A: TenantBoundary + ?Sized, B: TenantBoundary + ?Sized>(
82        left: &A,
83        right: &B,
84    ) -> Result<(), CrossTenantLeak> {
85        Self::same_tenant(left, right)
86    }
87}
88
89/// Verifies that two values share exactly the same tenant identifier.
90pub fn assert_same_tenant<A: TenantBoundary + ?Sized, B: TenantBoundary + ?Sized>(
91    left: &A,
92    right: &B,
93) -> Result<(), CrossTenantLeak> {
94    if left.tenant_id() == right.tenant_id() {
95        Ok(())
96    } else {
97        Err(CrossTenantLeak::new(
98            left.tenant_id().clone(),
99            right.tenant_id().clone(),
100        ))
101    }
102}