hyperlane-plugin-tenant 23.3.1

A zero-allocation multi-tenant resolution and isolation plugin for the Hyperlane framework supporting X-Tenant-Id header, Host subdomain, and path-prefix extraction, XxHash3_64 tenant registry, pluggable TenantResolver, and prebuilt 400/403/404 wire responses.
Documentation
use super::*;

/// Tenant metadata entry stored in the registry or returned by [`TenantResolver`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct TenantRecord {
    /// Unique tenant identifier.
    pub(crate) id: String,
    /// Current lifecycle status.
    #[get(type(copy))]
    pub(crate) status: TenantStatus,
    /// Optional subscription tier or plan label (for example `"enterprise"`).
    pub(crate) plan: String,
}

/// Resolved tenant context attached to Hyperlane [`Context`] under [`ATTR_TENANT_CONTEXT`].
#[derive(Clone, Debug, Eq, Getter, PartialEq)]
pub struct TenantContext {
    /// Active tenant identifier.
    pub(crate) tenant_id: String,
    /// Plan/tier label from the matched [`TenantRecord`].
    pub(crate) plan: String,
    /// Extraction mechanism that identified this tenant.
    #[get(type(copy))]
    pub(crate) source: TenantSource,
}

/// Configuration for [`TenantPlugin`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct TenantConfig {
    /// Header name checked for tenant ID (empty disables header extraction).
    pub(crate) header_name: String,
    /// Optional base domain for subdomain extraction (for example `"example.com"`).
    pub(crate) base_domain: Option<String>,
    /// Optional path prefix for path extraction (for example `"/t"`).
    pub(crate) path_prefix: Option<String>,
    /// Optional default tenant ID used when no extraction source matched.
    pub(crate) default_tenant: Option<String>,
    /// When `true`, extracted tenant IDs must exist in the static registry or custom resolver.
    #[get(type(copy))]
    pub(crate) require_known: bool,
    /// Static registered tenants.
    pub(crate) tenants: Vec<TenantRecord>,
    /// Path prefixes exempt from tenant resolution (for example `"/healthz"`).
    pub(crate) exempt_prefixes: Vec<String>,
}

/// Compiled multi-tenant resolution engine backing [`TenantHook`].
pub struct TenantPlugin {
    /// Retained configuration.
    pub(crate) config: TenantConfig,
    /// Lowercased extraction header name.
    pub(crate) header_lower: String,
    /// Lowercased `.base_domain` suffix for zero-alloc subdomain matching.
    pub(crate) dot_base_domain: Option<String>,
    /// Normalized path extraction prefix.
    pub(crate) normalized_path_prefix: Option<String>,
    /// Normalized exempt prefixes.
    pub(crate) exempt_prefixes: Box<[String]>,
    /// `XxHash3_64` static tenant directory.
    pub(crate) registry: HashMapXxHash3_64<String, TenantRecord>,
    /// Optional custom dynamic resolver.
    pub(crate) resolver: Option<Arc<dyn TenantResolver>>,
    /// Prebuilt `400 Bad Request` wire bytes.
    pub(crate) missing_wire: Vec<u8>,
    /// Prebuilt `404 Not Found` wire bytes.
    pub(crate) unknown_wire: Vec<u8>,
    /// Prebuilt `403 Forbidden` wire bytes.
    pub(crate) suspended_wire: Vec<u8>,
}

/// Opt-in request middleware resolving tenant identity and enforcing isolation.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-98)]
pub struct TenantHook;