Appearance
ADR-2548: Enforce PostgreSQL Forced RLS and Shared Clinical Scope Guards Across Standalone Services
- Status: Accepted
- Date: 2026-11-20
- Authors: @fderuiter
- Deciders: @fderuiter
- Requirements: PRD-CRF-013, PRD-SYS-192
1. Context & Problem Statement
While Cadence.Kernel.Persistence enforced PostgreSQL FORCE ROW LEVEL SECURITY (tenant_isolation via kernel.current_tenant_id()), the 15 standalone clinical DbContext schemas (ctms, econsent, eisf, etmf, edc, fileshare, imaging, interop, knowledge, notifications, org, payments, quality, safety, tickets) lacked database-enforced tenant_id columns and RLS policies, and GatewayAuthenticationHandler did not populate Principal.TenantId or fail closed when a signed X-Tenant-Id was missing or inactive. Additionally, standalone services duplicated ad-hoc study/site checks instead of sharing a single fail-closed authorization primitive.
2. Decision Drivers & Constraints
- Defense-in-Depth Multi-Tenant Isolation (K-ID-1, K-ID-2, ADR-2250, ADR-2514): Every tenant-scoped standalone table must carry
tenant_id uuid NOT NULLand enforceENABLE+FORCE ROW LEVEL SECURITY(USINGandWITH CHECKtenant_id = kernel.current_tenant_id()) under the non-superusercadence_app(NOSUPERUSER NOBYPASSRLS) role. - Strict Kernel Persistence Boundary (K-XS-2, ADR-2600):
packages/databaseand standaloneapps/*must resolve signed tenant codes viaSELECT kernel.tenant_id_by_code(@code)without referencingCadence.Kernel.Persistenceor migrating standalone aggregates intoCadence.Kernel.Host. - Fail-Closed Study & Site Scope (
PRD-SYS-192): Site-bound roles (crc,investigator,principal_investigator,cra,monitor) and study-scoped callers must be evaluated through a sharedClinicalScopeGuardinpackages/securityand verified by route metadata and handler denial probes.
3. Options Considered
Option 1: Application-Only LINQ Tenant Filters Without PostgreSQL RLS
- Overview: Filter standalone
DbContextqueries in C# usingCallerScope.TenantId(httpContext)without addingtenant_idcolumns or PostgreSQL RLS policies. - Pros:
- Avoids EF Core schema migrations across the 15 standalone services.
- Cons:
- Violates
K-ID-1,K-ID-2, andADR-2250; any raw SQL query or wildcard*study scope can read or mutate cross-tenant records.
- Violates
Option 2: Database-Enforced FORCE ROW LEVEL SECURITY, Session-Bound Connection Interceptors, and Shared ClinicalScopeGuard (Selected)
- Overview: Add
PostgresTenantCodeResolver,StandaloneTenantConnectionInterceptor(set_config('cadence.tenant_id', ...)on open, reset on close),StandaloneTenantSaveChangesInterceptor(fail-closed tenant UUID stamping), and non-relational EF query filters (StandaloneTenantModelExtensions) inpackages/database; addFORCE ROW LEVEL SECURITYmigrations across all 15 standaloneDbContextschemas; populatePrincipal.TenantIdand verify active tenants inGatewayAuthenticationHandler; and enforceRequireTenantScope,RequireCallerStudyScope,RequireCallerStudyAndSiteScope, andClinicalScopeGuardacross standalone endpoints. - Pros:
- Enforces tenant isolation in PostgreSQL even under wildcard
*study scope (PaymentsCaller). - Preserves
K-XS-2package boundaries andServiceTopology.csroute contracts. - Centralizes study/site admission and eliminates hardcoded
tenant_defaultandTenantId.Newfallbacks.
- Enforces tenant isolation in PostgreSQL even under wildcard
- Cons:
- Requires coordinated EF Core migrations across all 15 standalone schemas and explicit
ITenantScopebinding in background workers.
- Requires coordinated EF Core migrations across all 15 standalone schemas and explicit
4. Decision Outcome
- Chosen Option: Option 2
- Justification: Combining PostgreSQL
FORCE ROW LEVEL SECURITYundercadence_appwith session-bound connection/save interceptors andClinicalScopeGuardsatisfiesK-ID-1,K-ID-2,ADR-2250,ADR-2514,PRD-CRF-013, andPRD-SYS-192without violatingK-XS-2orADR-2600.
5. Consequences & Trade-offs
- Positive Impact: Unbound, inactive, or missing-tenant requests fail closed with HTTP 403; cross-tenant reads and writes are blocked by PostgreSQL RLS; country greenlight matrices and eSource provenance trails filter unauthorized site/study/tenant data; and
RouteAuthorizationTestsblocks unguarded standalone routes. - Negative Impact / Technical Debt: Background workers iterating across tenants (
EtmfExportWorker,DocumentConversionWorker,MediaTranscoderWorker) must enumerate active tenant IDs viaITenantCodeResolver.ListActiveTenantIdsAsyncand bind each tenant scope explicitly. - Mitigation Strategy:
StandaloneTenantSaveChangesInterceptorthrowsInvalidOperationExceptionif any code path attempts to persist a tenant-scoped entity without an active verified tenant.
6. Implementation & Verification
- Affected Repositories / Services:
packages/database,packages/security,packages/testing, all 15 standaloneDbContextservices (ctms,econsent,eisf,etmf,execution,fileshare,imaging,interop,knowledge,notifications,org,payments,quality,safety,tickets), anddesigner. - Verification Plan: Verified via
RouteAuthorizationTests,Cadence.ArchitectureTests,StandaloneTenantIsolationPostgresTests,StandaloneServiceScopeAuthorizationTests, andServiceSchemaPostgresTests.