Tenancy and the audit log

A multi-tenant service keeps every tenant's rows in shared tables and must never let one tenant read or change another's. Many such services must also be able to show who changed what, and prove the record was not edited afterwards. This chapter puts together pieces from earlier chapters into one pattern, row-level tenancy with a tamper-evident audit trail:

  1. Authentication turns the request into a principal that carries its tenant: id_effect_auth's Authenticate layer (Credentials and JWT), Principal::tenant() from id_effect_authz (Authorization).
  2. Per-request capabilities: two CapOverrideLayers turn the principal into this request's CurrentTenant (which tenant's rows) and AuditContext (actor and tenant of audit records).
  3. The repository filter: a Repository of a #[entity(tenant = "org_id")] entity built with Repository::for_current_tenant adds org_id = <tenant> to every statement and writes it on insert (Query builder, entities and repositories).
  4. The audit record is appended with audit::record in the request's transaction, so it commits or rolls back with the change.
  5. Verification: audit::verify_chain checks a tenant's hash chain and reports the first edited, removed or relinked record.

The data side

Mark the tenant column on the entity. It need not be a field: the repository writes it on insert (whatever the struct says), filters every SELECT, UPDATE and DELETE by it, and never updates it.

#![allow(unused)]
fn main() {
use id_effect_sql::{Entity, FromRow};

#[derive(Clone, Debug, PartialEq, FromRow, Entity)]
#[entity(table = "invoices", tenant = "org_id")]
struct Invoice {
    #[entity(id)]
    id: i64,
    name: String,
}

assert_eq!(Invoice::TENANT, Some("org_id"));
assert_eq!(Invoice::COLUMNS, ["id", "name"]); // org_id is not a field
}
CREATE TABLE invoices (
  id      BIGINT PRIMARY KEY,
  org_id  TEXT   NOT NULL,
  name    TEXT   NOT NULL
);
CREATE INDEX invoices_org_id ON invoices (org_id);

Index the tenant column (every query filters on it), and include it in unique constraints that are per tenant (UNIQUE (org_id, number)).

The audit log needs the audit feature and its schema, applied once: with the migrate feature, audit::migrator() runs the bundled migration with its own tracking table; without it, copy audit::MIGRATIONS into your migrations. It creates audit_log and audit_log_head. Provide AuditLog::new() at startup (AuditLog::new().layer(), or as a value in your state).

The request path

The router below authenticates API keys, derives both capabilities from the principal, and renames an invoice in one transaction together with its audit record. The axum handler builds the request's environment from its RequestCaps, provides transaction_layer (commit on success, roll back on failure) and runs the effect. The handler effect never sees a tenant argument: the filter comes from the environment.

#![allow(unused)]
fn main() {
use axum::body::Body;
use axum::extract::{Path, State};
use axum::http::{Request, StatusCode, request::Parts};
use axum::response::{IntoResponse, Response};
use axum::routing::put;
use axum::Router;
use http_body_util::BodyExt;
use id_effect::{Effect, HCons, HNil, Layer, fail, hlist, need};
use id_effect_auth::{ApiKey, ApiKeys, AuthPrincipal, Authenticate, MemoryApiKeyStore};
use id_effect_authz::guard::principal_of;
use id_effect_axum::{CapOverrideLayer, RequestCaps, exit_response, run_with_env};
use id_effect_sql::audit::{self, AuditContext, AuditEntry, AuditLog};
use id_effect_sql::{
    CurrentTenant, Dialect, Entity, FromRow, Repository, SqlClient, SqlClientService, SqlError, SqlRow,
    SqlValue, SqlTransactionService, TestSqlClient, transaction_layer,
};
use serde_json::json;
use tower::ServiceExt;

#[derive(Clone, Debug, PartialEq, FromRow, Entity)]
#[entity(table = "invoices", tenant = "org_id")]
struct Invoice {
    #[entity(id)]
    id: i64,
    name: String,
}

/// One request's environment: its capabilities and its transaction.
type RequestEnv = HCons<SqlClientService, HCons<CurrentTenant, HCons<AuditContext,
    HCons<AuditLog, HCons<SqlTransactionService, HNil>>>>>;

#[derive(Debug)]
enum AppError {
    Sql(SqlError),
    NoTenant,
}
impl From<SqlError> for AppError {
    fn from(e: SqlError) -> Self { AppError::Sql(e) }
}
impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let status = match self {
            AppError::NoTenant => StatusCode::FORBIDDEN,
            AppError::Sql(SqlError::RowNotFound) => StatusCode::NOT_FOUND,
            AppError::Sql(SqlError::StaleVersion { .. }) => StatusCode::CONFLICT,
            AppError::Sql(_) => StatusCode::INTERNAL_SERVER_ERROR,
        };
        status.into_response()
    }
}

/// PUT /invoices/{id}: rename an invoice of the caller's tenant and audit it.
fn rename_invoice(id: i64, name: String) -> Effect<String, AppError, RequestEnv> {
    Repository::<Invoice>::for_current_tenant(Dialect::Postgres)
        .flat_map(move |repo| {
            need::<SqlTransactionService>().flat_map(move |tx| {
                let invoices = repo.on(tx.0.clone());
                let update = invoices.clone();
                invoices
                    .find(id) // WHERE org_id = <tenant> AND id = <id>
                    .flat_map(move |found| match found {
                        None => fail(SqlError::RowNotFound), // another tenant's invoice, too
                        Some(before) => update
                            .update(Invoice { name, ..before.clone() })
                            .map(move |after| (before, after)),
                    })
                    .narrow()
            })
        })
        .flat_map(move |(before, after)| {
            let entry = AuditEntry::new("invoice.rename", format!("invoice:{id}"))
                .with_before(json!({ "name": before.name }))
                .with_after(json!({ "name": after.name }));
            audit::record(entry).map(move |_record| after.name) // same transaction
        })
        .map_err(AppError::from)
}

#[derive(Clone)]
struct App {
    db: SqlClientService,
    audit: AuditLog,
}

async fn put_invoice(
    State(app): State<App>,
    caps: RequestCaps,
    Path(id): Path<i64>,
    name: String,
) -> Response {
    // Fail closed: a principal without a tenant has no CurrentTenant, so nothing runs.
    let (Some(tenant), Some(actor)) = (caps.get::<CurrentTenant>(), caps.get::<AuditContext>()) else {
        return AppError::NoTenant.into_response();
    };
    let request = Layer::succeed(hlist![
        app.db.clone(),
        CurrentTenant::clone(&tenant),
        AuditContext::clone(&actor),
        app.audit.clone(),
    ]);
    let handler = rename_invoice(id, name)
        .provide_layer(transaction_layer::<AppError>().provide_merge(request));
    exit_response(run_with_env(HNil, move |_| handler).await)
}

fn router(db: SqlClientService, keys: ApiKeys) -> Router {
    Router::new()
        .route("/invoices/{id}", put(put_invoice))
        // 2. The principal's tenant and identity become this request's capabilities.
        .layer(CapOverrideLayer::new(|parts: &Parts| {
            let principal = principal_of(parts);
            Some(CurrentTenant::new(principal.tenant()?.to_owned()))
        }))
        .layer(CapOverrideLayer::new(|parts: &Parts| {
            let principal = principal_of(parts);
            Some(AuditContext::for_tenant(principal.id(), principal.tenant()?))
        }))
        // 1. Authentication is the outermost layer, so it runs first.
        .layer(Authenticate::api_key(keys))
        .with_state(App { db, audit: AuditLog::new() })
}

// Two API keys: ada acts for tenant `acme`; a service account has no tenant.
let store = MemoryApiKeyStore::new();
let (ada_key, ada) = ApiKey::generate("ledger", AuthPrincipal::new("ada").with_tenant("acme")).unwrap();
let (svc_key, svc) = ApiKey::generate("ledger", AuthPrincipal::new("svc-reports")).unwrap();
store.insert(ada);
store.insert(svc);

let test = TestSqlClient::new();
test.script_query(
    r#"SELECT "id", "name" FROM "invoices" WHERE "org_id" = $1 AND "id" = $2 LIMIT $3"#,
    vec![SqlRow::from_pairs([("id", SqlValue::I64(1)), ("name", "draft".into())])],
);
test.script_execute(r#"UPDATE "invoices" SET "name" = $1 WHERE "org_id" = $2 AND "id" = $3"#, 1);
let at = id_effect_sql::jiff::Timestamp::from_second(1_800_000_000).unwrap();
test.script_query(
    "INSERT INTO audit_log_head AS h (chain, seq, hash) VALUES ($1, 0, $2) ON CONFLICT (chain) DO UPDATE SET seq = h.seq RETURNING h.seq, h.hash, now() AS at",
    vec![SqlRow::from_pairs([("seq", SqlValue::I64(0)), ("hash", SqlValue::Bytes(vec![0; 32])), ("at", SqlValue::Timestamp(at))])],
);
test.script_query(
    "INSERT INTO audit_log (chain, seq, actor, tenant, action, entity, before, after, at, prev_hash, hash) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING id",
    vec![SqlRow::from_pairs([("id", SqlValue::I64(1))])],
);
let _ = test.connect().block_on();
let app = router(SqlClientService::new(test.clone()), ApiKeys::new(store, "ledger"));
let rename_request = |key: &str| {
    Request::put("/invoices/1").header("x-api-key", key).body(Body::from("final")).unwrap()
};

let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
    let res = app.clone().oneshot(rename_request(ada_key.expose())).await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    let body = res.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(&body[..], b"final");

    // A principal without a tenant is refused before any statement runs.
    let res = app.clone().oneshot(rename_request(svc_key.expose())).await.unwrap();
    assert_eq!(res.status(), StatusCode::FORBIDDEN);
});

// One transaction, committed: the tenant filter is in both statements...
assert_eq!(test.commits(), 1);
let queries = test.recorded_queries();
assert_eq!(queries[0].1, [SqlValue::Text("acme".into()), SqlValue::I64(1), SqlValue::I64(1)]);
let (update, params) = &test.recorded_executes()[0];
assert_eq!(update, r#"UPDATE "invoices" SET "name" = $1 WHERE "org_id" = $2 AND "id" = $3"#);
assert_eq!(params[1], SqlValue::Text("acme".into()));
// ... and the audit record names the actor and the tenant.
let (_, record) = queries.iter().find(|(sql, _)| sql.starts_with("INSERT INTO audit_log ")).unwrap();
assert_eq!(record[2..4], [SqlValue::Text("ada".into()), SqlValue::Text("acme".into())]);
}

Notes on the wiring:

  • principal_of(parts) reads the principal the authentication layer installed (the guard reads it the same way). The CapOverrideLayers sit inside Authenticate: axum runs the layer added last first.
  • CurrentTenant holds any ToSqlValue (CurrentTenant::new(org_id) for an integer or UUID column), and AuditContext::for_tenant(actor, tenant) takes strings; derive both from the same principal.
  • Handlers on a DynEnv router (execute_with_request_caps) see both capabilities in their environment as well.
  • The example puts the whole request in one transaction (transaction_layer). audit::record needs that SqlTransactionService in the environment; with an explicit transaction, use AuditLog::append(&tx, &ctx, entry) or audit::append.

Fail closed

Every layer refuses rather than guessing:

  • A principal without a tenant gets no CurrentTenant; the handler answers 403 before any statement runs.
  • A tenant-scoped repository without a tenant fails every call with SqlError::Unsupported and sends nothing (Repository::new(dialect).find_all() on Invoice above).
  • Another tenant's row is invisible: find returns None, update is SqlError::RowNotFound and delete returns false, so a guessed id answers 404, not 403, and does not reveal that the row exists.
  • unscoped() lifts the filter for one repository value (support tools, data migrations). Search for it in review.

Tenant match in policies

The repository filters rows; authorization checks the resource you already hold. Since 0.11 it does so without being asked (ADR 0013 R1): a policy defined for a Tenanted resource (Policies::builder().define::<Invoice>(..)), or run on one with check_with / authorize_with, first compares the principal's tenant with the resource's. Another tenant, or a principal of none, is denied with resource belongs to another tenant before the policy (and any lookup it does) runs, whatever the policy says. same_tenant() is no longer needed in those definitions; it remains for policies you evaluate directly.

Crossing tenants is a named opt-out: policy.dangerous_cross_tenant(reason) lets the policy decide for principals of any tenant, and records id_effect_authz::PolicyExt::dangerous_cross_tenant in the posture registry, so the prod profile refuses to start until the operator accepts it. It covers the policy it wraps, so it is the outermost call.

The check reads the resource's TenantScope: a Tenanted type has its tenant, (), str, String and Acl have none. A resource type of no tenant says so with an empty impl, and its policies decide as before.

#![allow(unused)]
fn main() {
use id_effect::Exit;
use id_effect::observability::posture::dangerous_opt_outs;
use id_effect_authz::{
    BasicPrincipal, CurrentPrincipal, Decision, Policies, PolicyExt, Rbac, TenantScope, Tenanted,
};

struct Invoice {
    org: String,
}
impl Tenanted for Invoice {
    fn tenant_id(&self) -> &str {
        &self.org
    }
}

/// Platform-wide, of no tenant.
struct ExchangeRates;
impl TenantScope for ExchangeRates {}

let rbac = Rbac::builder()
    .grant("accountant", ["invoices:read", "rates:read"])
    .grant("support", ["invoices:read"])
    .build();
let policies = Policies::builder()
    // No `same_tenant()`: the tenant match is part of every policy over `Invoice`.
    .define::<Invoice>("invoice.read", rbac.require("invoices:read"))
    .define::<Invoice>(
        "invoice.support",
        rbac.require_role("support").dangerous_cross_tenant("the support desk serves every tenant"),
    )
    .define::<ExchangeRates>("rates.read", rbac.require("rates:read"))
    .build();

let decide = |name: &str, who: BasicPrincipal, invoice: &Invoice| {
    match policies.evaluate(name, &CurrentPrincipal::new(who), invoice).block_on() {
        Exit::Success(decision) => decision,
        other => panic!("{other:?}"),
    }
};
let invoice = Invoice { org: "acme".into() };
let ada = BasicPrincipal::new("ada").with_role("accountant").with_tenant("acme");
let eve = BasicPrincipal::new("eve").with_role("accountant").with_tenant("globex");
let sue = BasicPrincipal::new("sue").with_role("support").with_tenant("platform");

assert_eq!(decide("invoice.read", ada, &invoice), Decision::Allow);
let Decision::Deny(denial) = decide("invoice.read", eve.clone(), &invoice) else {
    panic!("another tenant's accountant cannot read the invoice")
};
assert_eq!(denial.to_string(), "invoice.read: resource belongs to another tenant");
assert_eq!(decide("invoice.support", sue, &invoice), Decision::Allow);
assert!(dangerous_opt_outs().iter().any(|(id, _)| id.ends_with("dangerous_cross_tenant")));

// A resource of no tenant is unaffected.
let rates = policies.evaluate("rates.read", &CurrentPrincipal::new(eve), &ExchangeRates);
assert_eq!(rates.block_on(), Exit::Success(Decision::Allow));
}

The audit trail

A record is {actor, tenant, action, entity, before, after, at}; at is the database's now(). Each tenant has its own hash chain: record n stores sha256(prev_hash ‖ record), and the chain's head row holds the last (seq, hash). Concurrent writers in one tenant queue on the head row and never fork the chain, while different tenants don't wait on each other. Use the default READ COMMITTED isolation for audited writes, or wrap REPEATABLE READ / SERIALIZABLE work in retry_serialization_failures: under those levels a racing append fails with a serialization failure instead of waiting.

audit::verify_chain(Some(tenant)) streams the chain in order and reports the first edited, removed or relinked record. Run it on a schedule or behind an admin route, and log the returned ChainReport::head somewhere outside the database, so that a complete rewrite of the chain can be detected too:

#![allow(unused)]
fn main() {
use std::sync::Arc;

use id_effect::{Effect, HCons, HNil, fail, succeed};
use id_effect_sql::audit::{self, AuditLog};
use id_effect_sql::migrate::MigrateError;
use id_effect_sql::{SqlClient, SqlClientService, SqlError};

/// At startup: the audit schema, tracked apart from the application's migrations.
fn audit_schema(client: Arc<dyn SqlClient>) -> Effect<(), MigrateError> {
    audit::migrator().run_on(client).map(|_applied| ())
}

/// Nightly: verify one tenant's chain and anchor its head outside the database.
fn verify_tenant(tenant: String) -> Effect<String, SqlError, HCons<AuditLog, HCons<SqlClientService, HNil>>> {
    audit::verify_chain(Some(tenant.as_str())).flat_map(move |report| match report.first_broken {
        Some(link) => fail(SqlError::Unsupported(format!("audit chain of {tenant} broken at {link}"))),
        None => succeed(format!("{tenant}: {} records, head {:?}", report.records, report.head)),
    })
}
let _ = (audit_schema, verify_tenant);
}

What the chain detects: an edited record (its hash no longer matches), a removed or inserted record (the sequence or the next prev_hash no longer follows), and records removed from the end (the chain no longer reaches its head). Someone who can rewrite the whole chain and its head can only be caught by comparing with a head you anchored elsewhere.

Per-tenant rate limits

A tenant's traffic can be limited as a whole: RateLimitKey::tenant keys RateLimitLayer's buckets by the request's tenant (tenant:<id>), so every user of one organisation draws from the same bucket and one busy tenant cannot starve the others. The tenant is read from a value the authentication layer recorded — the request's RequestCaps override of a type, else a request extension of that type — through a closure, so id_effect_axum needs no dependency on the auth or SQL crates. With id_effect_auth's Authenticate (which records the CurrentPrincipal in both places) that is RateLimitKey::tenant(|p: &CurrentPrincipal| p.tenant().map(str::to_owned)). The rate limit layer goes inside the authentication layer (added to the router before it), so the principal is there when the key is computed; a request without a tenant falls back to the key given with .or(..).

#![allow(unused)]
fn main() {
use std::sync::Arc;

use axum::body::Body;
use axum::extract::Request;
use axum::http::StatusCode;
use axum::middleware::{self, Next};
use axum::response::Response;
use axum::routing::get;
use id_effect_authz::{BasicPrincipal, CurrentPrincipal, Principal};
use id_effect_axum::middleware::{MemoryRateLimitStore, RateLimitKey, RateLimitLayer};
use tower::ServiceExt;

// A stand-in for the authentication layer: `x-user` signs in as a member of `x-org`.
async fn authenticate(mut req: Request, next: Next) -> Response {
  let signed_in = {
    let header = |name: &str| req.headers().get(name).and_then(|v| v.to_str().ok()).map(str::to_owned);
    (header("x-user"), header("x-org"))
  };
  if let (Some(user), Some(org)) = signed_in {
    let principal = BasicPrincipal::new(user).with_tenant(org);
    req.extensions_mut().insert(CurrentPrincipal::new(principal));
  }
  next.run(req).await
}

// Two requests per tenant (no refill, for the example); anonymous callers share one bucket.
let store = Arc::new(MemoryRateLimitStore::new(0.0, 2.0));
let per_tenant = RateLimitKey::tenant(|p: &CurrentPrincipal| p.tenant().map(str::to_owned))
  .or(RateLimitKey::global());
let app = axum::Router::new()
  .route("/reports", get(|| async { "report" }))
  .layer(RateLimitLayer::new(Arc::clone(&store)).key(per_tenant))
  .layer(middleware::from_fn(authenticate));

let call = |user: &str, org: &str| {
  let req = Request::builder().uri("/reports").header("x-user", user).header("x-org", org);
  app.clone().oneshot(req.body(Body::empty()).unwrap())
};
tokio::runtime::Runtime::new().unwrap().block_on(async {
// Two acme users spend acme's bucket; globex is not affected.
assert_eq!(call("ada", "acme").await.unwrap().status(), StatusCode::OK);
assert_eq!(call("bob", "acme").await.unwrap().status(), StatusCode::OK);
assert_eq!(call("ada", "acme").await.unwrap().status(), StatusCode::TOO_MANY_REQUESTS);
assert_eq!(call("eve", "globex").await.unwrap().status(), StatusCode::OK);
});
assert_eq!(store.len(), 2); // tenant:acme, tenant:globex
}

Every tenant gets the same rate. RedisRateLimitStore (feature rate-limit of id_effect_redis) holds the per-tenant buckets across instances; tenants on different plans need a RateLimitStore that sizes a bucket by its key, or one layer per plan.

Resolving the tenant

The request path above reads the tenant straight from the principal. id_effect_authz::TenantResolver (0.13, ADR 0015 D10) makes that rule a value the whole service shares, and adds the two other places a tenant can come from, each behind a rule that keeps it safe:

SourceHowWhen it yields a tenant
The principal (default)TenantResolver::new()the authenticated principal has a tenant claim (Principal::tenant())
The Host.with_host_allowlist([("acme.example.com", "acme")])the host (case-insensitive, without port and trailing dot) is in the map; any other host yields nothing, and a host is never used as a tenant id itself
A header.dangerous_tenant_from_header("x-tenant")the header holds a valid id (1–128 bytes of [A-Za-z0-9._-]); a recorded opt-out

When more than one source yields a tenant they must agree: ada of acme on globex.example.com has no tenant, not either of them. No tenant means no tenant-scoped work. Any client can send any header, so reading the tenant from one is only safe behind a gateway that authenticates the caller, sets the header and strips the client's; dangerous_tenant_from_header records id_effect_authz::TenantResolver::dangerous_tenant_from_header in the posture registry and the prod profile refuses to start until the operator accepts it (ADR 0013 R10).

With the authz feature axum, resolver.layer() resolves every request and records the TenantId as a request extension and in the request's RequestCaps. It reads the principal, so it goes inside the authentication layer. Handlers take TenantId (401 for an anonymous caller, 403 for a principal without a tenant) or Option<TenantId>; .require_tenant() answers those requests in the layer instead. A TenantId is Tenanted, so check_with(policy, tenant) first requires the principal to belong to it.

#![allow(unused)]
fn main() {
use axum::Router;
use axum::body::Body;
use axum::extract::Request;
use axum::http::StatusCode;
use axum::middleware::{self, Next};
use axum::response::Response;
use axum::routing::get;
use id_effect::observability::posture::dangerous_opt_outs;
use id_effect_authz::{BasicPrincipal, CurrentPrincipal, TenantId, TenantResolver};
use tower::ServiceExt;

/// A stand-in authentication layer: `x-user: <id>@<tenant>`.
async fn authenticate(mut req: Request, next: Next) -> Response {
    let user = req.headers().get("x-user").and_then(|v| v.to_str().ok()).map(str::to_owned);
    if let Some((id, tenant)) = user.as_deref().and_then(|u| u.split_once('@')) {
        req.extensions_mut()
            .insert(CurrentPrincipal::new(BasicPrincipal::new(id).with_tenant(tenant)));
    }
    next.run(req).await
}

let resolver = TenantResolver::new().with_host_allowlist([("acme.example.com", "acme")]);
let app = Router::new()
    .route("/whoami", get(|tenant: TenantId| async move { tenant.to_string() }))
    .layer(resolver.layer()) // inside authentication
    .layer(middleware::from_fn(authenticate));

let call = |host: &str, user: Option<&str>| {
    let mut req = Request::get("/whoami").header("host", host);
    if let Some(user) = user {
        req = req.header("x-user", user);
    }
    app.clone().oneshot(req.body(Body::empty()).unwrap())
};
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
rt.block_on(async {
    // The principal's claim; an anonymous visitor on an allowlisted host.
    assert_eq!(call("api.example.com", Some("ada@acme")).await.unwrap().status(), StatusCode::OK);
    assert_eq!(call("acme.example.com", None).await.unwrap().status(), StatusCode::OK);
    // A host outside the allowlist, or a principal of another tenant on acme's host: no tenant.
    assert_eq!(call("evil.example.com", None).await.unwrap().status(), StatusCode::UNAUTHORIZED);
    assert_eq!(
        call("acme.example.com", Some("eve@globex")).await.unwrap().status(),
        StatusCode::FORBIDDEN
    );
});

// Reading the tenant from a header is a recorded opt-out.
let gateway = TenantResolver::new().dangerous_tenant_from_header("x-tenant");
assert_eq!(gateway.tenant_header(), Some("x-tenant"));
assert!(dangerous_opt_outs().iter().any(|(id, _)| id.ends_with("dangerous_tenant_from_header")));
}

The resolved tenant feeds both halves of this chapter: TenantScope checks in authorization, and CurrentTenant in the repository. id_effect_sql and id_effect_authz do not depend on each other, so the bridge is one CapOverrideLayer in your router, or the kit feature tenancy, which installs it (and the database routing below) for you:

#![allow(unused)]
fn main() {
use axum::http::request::Parts;
use id_effect_authz::TenantId;
use id_effect_axum::{CapOverrideLayer, RequestCaps};
use id_effect_sql::CurrentTenant;

// Inside the tenant layer: the request's `TenantId` becomes its `CurrentTenant`.
let bridge = CapOverrideLayer::new(|parts: &Parts| {
    let tenant = parts.extensions.get::<TenantId>()?;
    Some(CurrentTenant::new(tenant.as_str().to_owned()))
});
let _ = (bridge, RequestCaps::new());
}

One database per tenant

Row-level tenancy keeps every tenant in shared tables. When a tenant needs its own database (a contract, a data-residency rule, a noisy neighbour), id_effect_sql::TenantRouter maps each tenant to its own client. It is itself a SqlClient, so repositories, transactions and the audit log run on it unchanged; each statement goes to the database of the tenant the fiber acts for (TenantRouter::with_tenant(tenant, effect)), or of a fixed tenant (router.for_tenant(tenant), for jobs).

  • From configuration or a resolver. TenantRouter::from_map([(tenant, tenant_database(client))]), TenantRouter::from_pg_configs([(tenant, PgPoolConfig)]) (feature pg), or TenantRouter::from_resolver(|tenant| …) returning a new client for the tenant, or None.
  • Fails closed. No tenant, or a tenant the map or resolver does not know, has no connection: every statement fails with SqlError::NotConnected and nothing reaches any database. There is no default database.
  • Bounded. At most 64 tenant clients are open (with_max_pools(n)); opening another closes the least recently used one, and that tenant reconnects on its next statement (ADR 0013 R4).
  • Replicas compose. A tenant's client can be a RoutingSqlClient: its reads go to that tenant's replicas, its writes and transactions to that tenant's primary.
#![allow(unused)]
fn main() {
use std::sync::Arc;
use id_effect::{Cause, Exit};
use id_effect_sql::{
    RoutingSqlClient, SqlClient, SqlError, TenantRouter, TestSqlClient, tenant_database,
};

// acme shares the main database; globex has its own primary and read replica.
let main = TestSqlClient::new();
let (globex_primary, globex_replica) = (TestSqlClient::new(), TestSqlClient::new());
let globex = RoutingSqlClient::new(
    Arc::new(globex_primary.clone()),
    vec![Arc::new(globex_replica.clone()) as Arc<dyn SqlClient>],
);
let db = TenantRouter::from_map([
    ("acme", tenant_database(main.clone())),
    ("globex", tenant_database(globex)),
])
.with_max_pools(32);

let work = |tenant: &str| {
    TenantRouter::with_tenant(
        tenant,
        db.query("SELECT * FROM documents", &[])
            .then(db.execute("UPDATE documents SET seen = TRUE", &[])),
    )
};
assert!(matches!(work("acme").block_on(), Exit::Success(0)));
assert!(matches!(work("globex").block_on(), Exit::Success(0)));
assert_eq!((main.recorded_queries().len(), main.recorded_executes().len()), (1, 1));
assert_eq!(globex_replica.recorded_queries().len(), 1); // globex's reads: its replica
assert_eq!(globex_primary.recorded_executes().len(), 1); // globex's writes: its primary

// An unknown tenant, or no tenant at all, has no connection.
let unknown = work("initech").block_on();
assert!(matches!(unknown, Exit::Failure(Cause::Fail(SqlError::NotConnected))));
let no_tenant = db.query("SELECT 1", &[]).block_on();
assert!(matches!(no_tenant, Exit::Failure(Cause::Fail(SqlError::NotConnected))));
assert_eq!(db.open_tenants(), ["acme", "globex"]);
}

A tenant routed to its own database still keeps its tenant column and CurrentTenant filter: the router chooses the database, the repository filter chooses the rows, and a misrouted statement still finds nothing of another tenant's. Use tenant ids from the resolver above (authenticated, validated) as router keys, and never build a database name or URL from an id that was not validated.

What this pattern does not cover

  • Database-enforced isolation. The filter lives in the repository, so raw SQL (query, execute) and statements you build with the query builder are not filtered: add the tenant condition yourself, and keep such code close to the repository. For a second line of defence, PostgreSQL row-level security (CREATE POLICY … USING (org_id = current_setting('app.tenant')), with SET LOCAL app.tenant at the start of each transaction) enforces the same rule in the database; id_effect_sql does not set it up for you.
  • Schema per tenant. One database per tenant is TenantRouter (above); one schema per tenant in a shared database, selected with search_path, is not provided. Use a database per tenant, or row-level tenancy.
  • Per-tenant configuration and quotas other than a request rate (storage, seats, a different rate per plan out of the box) are not provided; per-tenant request rate limits are (above).
  • Audit on other databases. The audit log's SQL is PostgreSQL's. The repository and its tenant filter work on PostgreSQL, MySQL and SQLite.

Tests and example

  • crates/id_effect_sql/tests/repository.rs: tenant_filter_scopes_every_query runs on SQLite and PostgreSQL (two tenants, every operation, CurrentTenant from the environment, fail-closed without a tenant).
  • crates/id_effect_sql/tests/audit.rs (PostgreSQL): tampered_row_breaks_chain, audit_rolls_back_with_tx, removed records, concurrent writers that never fork a chain, and the REPEATABLE READ race.
  • crates/id_effect_authz/tests/guards.rs: principals installed by CapOverrideLayer reach the guards.
  • crates/id_effect_authz/tests/tenant_resolver.rs: tenant_resolved_from_principal_by_default, host_outside_allowlist_has_no_tenant, dangerous_tenant_from_header_is_recorded, and the layer and extractors.
  • crates/id_effect_sql/tests/tenant_routing.rs: unknown_tenant_has_no_connection, r4_tenant_pool_count_bounded, tenant_router_composes_with_replicas, and on PostgreSQL (one container, two databases) tenant_router_routes_two_tenants_to_two_databases.
  • crates/id_effect_axum/src/middleware/rate_limit.rs: each_tenant_has_its_own_bucket (per-tenant buckets behind a CapOverrideLayer) and tenant_keys_read_request_caps_then_extensions.
  • crates/id_effect_sql/examples/050_tenancy_audit.rs runs the request path for two tenants: by default on SQLite in memory (tenant filter, transaction, fail-closed), with --live and DATABASE_URL on PostgreSQL including the audit chain, a rolled-back request, verify_chain and a detected edit: cargo run -p id_effect_sql --example 050_tenancy_audit --features repository,sqlite,audit,migrate,pg -- --live.