Skip to main content

a2a_protocol_server/builder/
multi_tenant.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! What [`Profile::MultiTenant`](crate::profile::Profile::MultiTenant) asks of a
5//! handler that serves several tenants (adversarial review, 2026-11-12,
6//! "multi-tenant production policy must be explicit").
7
8use std::sync::Arc;
9
10use super::{CallerIsolation, RequestHandlerBuilder, TenantIsolation};
11use crate::error::{ServerError, ServerResult};
12use crate::tenant_resolver::TenantResolver;
13
14fn refuse(why: &str) -> ServerResult<()> {
15    Err(ServerError::Config(format!(
16        "the multi-tenant profile {why}"
17    )))
18}
19
20impl RequestHandlerBuilder {
21    /// Sets the tenant resolver and **asserts that it is authorized**: it names
22    /// a tenant only from something the server has verified, or a gateway sets
23    /// and strips from client traffic, never from what the client chose.
24    /// [`Profile::MultiTenant`](crate::profile::Profile::MultiTenant) refuses a
25    /// resolver given through [`with_tenant_resolver`](Self::with_tenant_resolver)
26    /// alone, since a tenant the caller can choose isolates nothing (see the
27    /// [`tenant_resolver`](crate::tenant_resolver) module's security section).
28    ///
29    /// The assertion is yours: nothing here can check that a gateway strips a
30    /// header or that a JWT's signature was verified before the claim was read.
31    /// It is a signed statement in the configuration, and the profile refuses a
32    /// configuration that does not make it.
33    #[must_use]
34    pub fn with_authorized_tenant_resolver(mut self, resolver: impl TenantResolver) -> Self {
35        self.tenant_resolver = Some(Arc::new(resolver));
36        self.tenant_resolver_authorized = true;
37        self
38    }
39
40    /// What the profile asks, checked before `build()` consumes the stores.
41    pub(super) fn check_multi_tenant(&self) -> ServerResult<()> {
42        if !self.interceptors.has_authenticator() {
43            return refuse(
44                "needs an authenticating interceptor: otherwise no request names a caller, \
45                 and no tenant can be authorized for one",
46            );
47        }
48        if self.tenant_resolver.is_none() || !self.tenant_resolver_authorized {
49            return refuse(
50                "needs a tenant resolver the deployment asserts is authorized \
51                 (with_authorized_tenant_resolver): a tenant the client chose isolates nothing",
52            );
53        }
54        if self.caller_isolation == Some(CallerIsolation::Shared) && self.task_store.is_none() {
55            return refuse(
56                "was told to share tasks across callers within a tenant, which needs a \
57                 tenant-aware task store you supply: the default keeps one set of records \
58                 for every tenant",
59            );
60        }
61        // What `build()` will use: a store given, or the default, which
62        // partitions by tenant because callers are isolated (sharing without a
63        // store of its own was refused above).
64        let task_partitions = self
65            .task_store
66            .as_ref()
67            .is_none_or(|s| s.isolates_tenants());
68        // An unset push-config store takes the default, which partitions when the
69        // task store does, so only one that was given can fail on its own.
70        let push_partitions = self
71            .push_config_store
72            .as_ref()
73            .is_none_or(|s| s.isolates_tenants());
74        if self.tenant_isolation == TenantIsolation::SharedAccepted
75            || !(task_partitions && push_partitions)
76        {
77            return refuse(
78                "needs a task store and a push-config store that partition by tenant (the \
79                 TenantAware* stores) and does not accept stores that share records across \
80                 tenants (accept_unisolated_tenants)",
81            );
82        }
83        let Some(config) = &self.tenant_config else {
84            return refuse(
85                "needs finite per-tenant budgets (with_tenant_config): a tenant with no \
86                 concurrency limit can take every executor slot from the others",
87            );
88        };
89        let unbounded = std::iter::once(("the default", &config.default)).chain(
90            config
91                .overrides
92                .iter()
93                .map(|(tenant, limits)| (tenant.as_str(), limits)),
94        );
95        for (whose, limits) in unbounded {
96            if limits.max_concurrent_tasks.is_none() {
97                return Err(ServerError::Config(format!(
98                    "the multi-tenant profile needs a concurrency limit for every tenant \
99                     (TenantLimits::with_max_concurrent_tasks), and {whose} has none"
100                )));
101            }
102        }
103        Ok(())
104    }
105}