Skip to main content

hyperlane_plugin_tenant/
struct.rs

1use super::*;
2
3/// Tenant metadata entry stored in the registry or returned by [`TenantResolver`].
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct TenantRecord {
6    /// Unique tenant identifier.
7    pub(crate) id: String,
8    /// Current lifecycle status.
9    #[get(type(copy))]
10    pub(crate) status: TenantStatus,
11    /// Optional subscription tier or plan label (for example `"enterprise"`).
12    pub(crate) plan: String,
13}
14
15/// Resolved tenant context attached to Hyperlane [`Context`] under [`ATTR_TENANT_CONTEXT`].
16#[derive(Clone, Debug, Eq, Getter, PartialEq)]
17pub struct TenantContext {
18    /// Active tenant identifier.
19    pub(crate) tenant_id: String,
20    /// Plan/tier label from the matched [`TenantRecord`].
21    pub(crate) plan: String,
22    /// Extraction mechanism that identified this tenant.
23    #[get(type(copy))]
24    pub(crate) source: TenantSource,
25}
26
27/// Configuration for [`TenantPlugin`].
28#[derive(Clone, Data, Debug, Eq, PartialEq)]
29pub struct TenantConfig {
30    /// Header name checked for tenant ID (empty disables header extraction).
31    pub(crate) header_name: String,
32    /// Optional base domain for subdomain extraction (for example `"example.com"`).
33    pub(crate) base_domain: Option<String>,
34    /// Optional path prefix for path extraction (for example `"/t"`).
35    pub(crate) path_prefix: Option<String>,
36    /// Optional default tenant ID used when no extraction source matched.
37    pub(crate) default_tenant: Option<String>,
38    /// When `true`, extracted tenant IDs must exist in the static registry or custom resolver.
39    #[get(type(copy))]
40    pub(crate) require_known: bool,
41    /// Static registered tenants.
42    pub(crate) tenants: Vec<TenantRecord>,
43    /// Path prefixes exempt from tenant resolution (for example `"/healthz"`).
44    pub(crate) exempt_prefixes: Vec<String>,
45}
46
47/// Compiled multi-tenant resolution engine backing [`TenantHook`].
48pub struct TenantPlugin {
49    /// Retained configuration.
50    pub(crate) config: TenantConfig,
51    /// Lowercased extraction header name.
52    pub(crate) header_lower: String,
53    /// Lowercased `.base_domain` suffix for zero-alloc subdomain matching.
54    pub(crate) dot_base_domain: Option<String>,
55    /// Normalized path extraction prefix.
56    pub(crate) normalized_path_prefix: Option<String>,
57    /// Normalized exempt prefixes.
58    pub(crate) exempt_prefixes: Box<[String]>,
59    /// `XxHash3_64` static tenant directory.
60    pub(crate) registry: HashMapXxHash3_64<String, TenantRecord>,
61    /// Optional custom dynamic resolver.
62    pub(crate) resolver: Option<Arc<dyn TenantResolver>>,
63    /// Prebuilt `400 Bad Request` wire bytes.
64    pub(crate) missing_wire: Vec<u8>,
65    /// Prebuilt `404 Not Found` wire bytes.
66    pub(crate) unknown_wire: Vec<u8>,
67    /// Prebuilt `403 Forbidden` wire bytes.
68    pub(crate) suspended_wire: Vec<u8>,
69}
70
71/// Opt-in request middleware resolving tenant identity and enforcing isolation.
72#[derive(Clone, Copy, Debug, Default)]
73#[hyperlane::request_middleware(-98)]
74pub struct TenantHook;