Files
hapi/docs/adr/0004-database-access.md
2026-11-27 15:40:22 -05:00

6.4 KiB
Raw Permalink Blame History

Database access: VPC-only, two roles, Personal data isolated by row-level security

hAPI uses a Linode Managed PostgreSQL 18 cluster (Shared 2 GB, 1 node) attached to the cluster's VPC with public_access = false, and its allowlist holds only the VPC subnet. Clients connect with sslmode=verify-full and scram-sha-256 passwords. Two roles share the hapi database. hapi_migrator owns every schema and table and runs hapi migrate. hapi_app has DML only; the running app and the admin CLI both connect as it. Personal-data tables have ENABLE ROW LEVEL SECURITY (not FORCE), with policies keyed on a per-transaction SET LOCAL hapi.principal_id, so a forgotten WHERE in a handler returns nothing instead of leaking another Person's data. hapi_app never owns a table and has no BYPASSRLS. That keeps ADR-0001's rule (not even the Owner reads others' Personal data) in the database for everything the app does. We accept that VPC for Managed Databases is still labelled beta, because ADR-0002 rules out a public database endpoint. It was verified working in us-iad.

Amended by ADR-0005: RLS also covers Household data tables. Their policies allow an active Household member or a Bot with a Household grant for that domain. Personal-data policies also allow a Bot that holds an active Binding from the record's owner. In both cases USING checks the read Scope and WITH CHECK checks the write Scope. hapi.principal_id is always the authenticated Principal, Person or Bot. The CI isolation check below covers every owned table, Personal or Household.

Considered Options

  • Public endpoint with a node-IP allowlist. Rejected: it contradicts ADR-0002 ("Never public: the database").
  • App-only filtering (no RLS). Rejected: one missed filter becomes a data leak. Wrong-Principal tests stay mandatory on top of RLS.
  • FORCE ROW LEVEL SECURITY. Rejected: it binds the owner hapi_migrator too, so every data migration and break-glass session would need ALTER TABLE … NO FORCE. The only thing it adds is protection against the migrator, which is the operator, and the operator is outside RLS's guarantee anyway.
  • Signed principal context, or one role per Person. Rejected as protection against leaked hapi_app credentials. A signing key would live in the same pod as the password. Per-Person roles would all have to be grantable to hapi_app, so leaked credentials could switch to any of them.
  • A single role, or one role per domain schema. One role lets the app run DDL and own tables, and owners bypass RLS. Per-domain roles add no isolation, because the monolith process would hold all of them anyway.
  • A BYPASSRLS role for admin tooling. Rejected: the Owner has no read access to Personal data, so nothing in the request path or admin CLI needs one.
  • Managed or self-hosted PgBouncer. Not needed at this load (see the connection budget below).
  • PostgreSQL 19. Newly offered on the platform. We stay on 18, which all our verification ran on and which nothing in v1 outgrows. Revisit at the first major-upgrade window.

Consequences

  • What RLS protects against: app bugs only. Anyone holding hapi_app credentials can set any hapi.principal_id. Leaked credentials are contained by the network, because the VPC-only allowlist (verified as the enforcing control) limits use to the cluster and VPC. Whoever holds akmadmin (which has BYPASSRLS), hapi_migrator or the OpenTofu state can read everything. Owner non-access is enforced by policy and audit, not cryptography. Per-Person encryption is out of scope for v1.
  • Policies treat an unset principal as no principal. After a transaction, the setting reads '', not NULL, so policies use nullif(current_setting('hapi.principal_id', true), '').
  • CI enforces isolation: every owner-mixin table must have relrowsecurity set and be owned by hapi_migrator, never hapi_app. Tests and local dev use the same bootstrap and connect as hapi_app.
  • akmadmin is never in the cluster. An idempotent hapi db bootstrap admin CLI command creates the roles and the database. The Owner runs it in a pod and pastes in the akmadmin password at run time. It does the following:
    1. Waits until the private hostname resolves (about 60 s after the cluster reports ready).
    2. Creates hapi_migrator and hapi_app.
    3. Runs CREATE DATABASE hapi OWNER hapi_migrator.
    4. Runs SET ROLE hapi_migrator for the database-level REVOKE/GRANTs and ALTER DEFAULT PRIVILEGES. Run as akmadmin, these silently do nothing.
  • Cross-Person work runs per Person. Purging a removed Person's data, or a job such as reminders, sets hapi.principal_id to each Person in turn.
  • Data repairs normally ship as reviewed Alembic data migrations run as hapi_migrator, targeting rows by id or condition without printing them.
  • Break-glass goes through hapi db breakglass --reason "…". It opens psql as hapi_migrator from a pod and emits structured audit events to hAPI's centralized logs: start, end, reason, and every statement. Query results are never logged. The platform offers no statement logging or pgaudit, so this is the only audit trail.
  • Connection budget: the plan allows 50 connections. That leaves about 46 usable after 3 reserved superuser slots and one platform backend. Each app pod holds at most 10 (pool_size=5, max_overflow=5), and the admin CLI holds 1–2. The rule is: (maximum concurrent app pods, including rollout surge) × 10, plus about 6 for migrate, CLI, break-glass and bootstrap, must be ≤ 46. That caps app pods at 4. Any future worker process counts against the same budget. Amended by ADR-0012: housekeeping CronJobs use a pool of 1, so the rule is app pods (with surge) × 10 + concurrently runnable CronJobs × 1 + ~6 ≤ 46.
  • Plan changes are restores. An in-place resize stalled during verification. Changing size or node count is done as a restore-to-new-cluster, with the same runbook as disaster recovery: fork, wait for DNS, hapi db bootstrap, repoint the app. Single-node maintenance windows (pinned to a quiet slot) cause short downtime, covered by 14-day point-in-time recovery. Amended by ADR-0014: our own daily dumps (an Object Lock bucket, pulled off-account) cover damage found after 14 days and account loss, and hapi db bootstrap also creates a read-only BYPASSRLS hapi_backup role for hapi db backup.
  • Passwords are rotated manually with a runbook. password_encryption is set explicitly to scram-sha-256.