ADR-0006: Tenant isolation with forced row-level security and SECURITY DEFINER lookups¶
Status: accepted
Date: 2026-11-09
Context¶
Each tenant must only ever see its own data. Application checks alone
fail the day one query forgets its WHERE tenant_id = .... The same database
also serves a few lookups that happen before the tenant is known: ingest
learns the tenant from the device ID, enrollment from the token, the API from
the token ID.
Decision¶
Every tenant table has
tenant_idand row-level security, enabled and forced. The policy for the runtime role comparestenant_idwithfleetwire.current_tenant(), which reads the transaction-local settingfleetwire.tenant_id.current_tenant()wrapscurrent_setting(..., true)inNULLIF(..., ''). Once a placeholder setting has been used on a connection, PostgreSQL returns an empty string instead of NULL; withoutNULLIFevery query on a reused pooled connection would fail with a cast error instead of matching nothing.The policies and grants name one runtime role. In Compose and the tests it is
fleetwire_app(NOBYPASSRLS,NOINHERIT, owns nothing), created by the deployment.fleetwire-api migratetakes the name as a parameter (FLEETWIRE_DATABASE_RUNTIME_ROLE), because Charmed PostgreSQL cannot create roles for an application: there the runtime role is the charm’scharmed_dmlgroup and the services log in as relation users that belong to it (ADR-0012). Policies on a group apply to its members.At start the services call
CheckRuntimeRoleand refuse to run as a role that is a superuser, can bypass RLS, or owns the tables.store.InTenantTxis the only way to reach tenant data. It runsset_config('fleetwire.tenant_id', $1, true); thetruemakes it local to the transaction, so it cannot leak to the next borrower of the connection.Pre-tenant lookups are SECURITY DEFINER functions owned by the migration role, with
search_path = pg_catalog, fleetwire, pg_temp,EXECUTErevoked fromPUBLICand granted to the runtime role only, each returning the minimum columns.device_for_ingestalso takes the device row lock.Because RLS is forced, the owner is subject to it too. The owner gets an explicit permissive policy (
TO CURRENT_USER USING (true)) instead of theBYPASSRLSattribute, because only a superuser can grantBYPASSRLSand managed or charmed PostgreSQL does not give out a superuser. The owner is never a runtime role.Child tables reference
devices (tenant_id, id)with composite foreign keys, so a chunk, gap or range cannot belong to a device of another tenant.The audit log has separate read and write policies: a tenant reads only its rows; a row with
tenant_id IS NULL(fleet-wide action) can only be written outside a tenant transaction, and is read throughoperator_audit().Operator tokens are not tenant data: the runtime role has no privilege on that table at all, and new operator tokens are minted only with the owner’s credentials from the operator’s shell.
Alternatives considered¶
Schema or database per tenant. Strong isolation, but migrations and connection pools multiply with the tenant count, and cross-tenant operator views need dynamic SQL.
Application-side filtering only. One missed
WHEREleaks data; RLS makes the safe path the only path.BYPASSRLSowner for the definer functions. Simpler policies, but needs a superuser and does not work on managed PostgreSQL.
Consequences¶
Forgetting to set the tenant returns no rows instead of everything.
Every query on tenant tables pays for the policy predicate; the indexes lead with
tenant_idor are selective ondevice_id.Integration tests prove the isolation at the SQL level: reads, updates, deletes and inserts across tenants, the missing-tenant case on a reused connection, audit immutability for both roles, and single use of enrollment tokens under concurrency.
The definer functions are a small, reviewed attack surface; adding one is a security-relevant change.
The isolation is against mistakes in tenant scoping, not against a compromised service: any session of the runtime role can set
fleetwire.tenant_idto any tenant. What a compromised service cannot do is alter the schema, drop the policies, or rewrite the audit log, because it is not the owner.