Files
hapi/docs/adr/0004-database-access.md
T

3.5 KiB

status
status
proposed

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 + FORCE ROW LEVEL SECURITY, 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. No role bypasses RLS except hapi_migrator, by owning the tables. That keeps ADR-0001's rule (not even the Owner reads others' Personal data) in the database as well as the app. We accept that VPC for Managed Databases is still labelled beta, because ADR-0002 rules out a public database endpoint.

Considered Options

  • Public endpoint with a node-IP allowlist. Rejected: it contradicts ADR-0002 ("Never public: the database"). It remains the documented fallback if VPC attach fails, as an explicit ADR-0002 amendment.
  • App-only filtering (no RLS). Rejected: one missed filter becomes a data leak. Wrong-Principal tests stay mandatory on top of RLS.
  • 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. Each pod runs a small SQLAlchemy async pool (5 connections + 5 overflow) against the plan's 200-connection limit.

Consequences

  • akmadmin is never in the cluster. Roles and the hapi database are created by an idempotent hapi db bootstrap admin CLI command. The Owner runs it in a pod and pastes in the akmadmin password at run time. It also re-creates roles after a restore, which always forks a new cluster. The akmadmin password otherwise lives only in encrypted OpenTofu state.
  • 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 is psql as hapi_migrator from a pod, following a runbook and recorded on the incident.
  • RLS protects Persons from app bugs and leaked app credentials, not from the operator. Whoever holds akmadmin, 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.
  • Tests and local dev run under RLS. They use the same bootstrap and connect as hapi_app. CI fails if any table with the owner mixin lacks forced RLS.
  • Single-node trade-off: maintenance windows and resizes cause short downtime, covered by 14-day point-in-time recovery. Moving to 3 nodes is a plan change.
  • Passwords are rotated manually with a runbook. password_encryption is set explicitly, because Akamai's sources disagree on the default.