How does hAPI connect to its managed database? #53

Closed
opened 2026-11-23 16:05:24 -05:00 by bobby · 3 comments
Owner

Part of #44

Question

How is the Linode managed Postgres provisioned and reached from the cluster: private networking vs allowlisted public endpoint, TLS, role/credential model (app role, migration role, per-schema roles), and connection pooling? Also decide the DB plan (node count and size). Per ADR-0003 the cluster is in us-iad and sits in a VPC, so check whether Managed DB VPC attach (beta) is available there and whether linode_database_postgresql_v2 exposes it.

Part of #44 ## Question How is the Linode managed Postgres provisioned and reached from the cluster: private networking vs allowlisted public endpoint, TLS, role/credential model (app role, migration role, per-schema roles), and connection pooling? Also decide the **DB plan** (node count and size). Per ADR-0003 the cluster is in `us-iad` and sits in a VPC, so check whether Managed DB VPC attach (beta) is available there and whether `linode_database_postgresql_v2` exposes it.
bobby added the wayfinder:grilling label 2026-11-23 16:05:24 -05:00
bobby added a new dependency 2026-11-23 16:05:34 -05:00
bobby added a new dependency 2026-11-23 16:05:34 -05:00
bobby added a new dependency 2026-11-23 16:05:34 -05:00
bobby self-assigned this 2026-11-24 07:29:59 -05:00
Author
Owner

Decisions (provisional, pending #64)

Decided in a grilling session on 2026-11-24. This ticket stays open, blocked by Verify Managed DB VPC attach, role setup, and RLS on a live us-iad cluster (#64). Close it when those checks pass. If a check fails, reopen the affected point (region, or RLS) here.

Plan and provisioning

  • Linode Managed PostgreSQL 18, Shared 2 GB, 1 node ($32/mo). Maintenance window pinned to a quiet slot (e.g. Tue 09:00 UTC). Moving to 3 nodes later is a plan change.
  • Managed by OpenTofu (linode_database_postgresql_v2) in the cluster's VPC. engine_config_pg_password_encryption = "scram-sha-256" set explicitly, because Akamai's sources disagree on the default.

Network

  • VPC attach, public_access = false, with allow_list holding only the cluster's VPC subnet CIDR. We accept the beta risk. If us-iad lacks support, try us-iad-2, then us-east (Newark). The fallback is a public endpoint plus a node-IP allowlist, as an explicit ADR-0002 amendment.
  • TLS sslmode=verify-full, with the CA from Tofu's ca_cert. The app fails fast without it. Local dev uses plain Compose.

Roles and credentials

  • A dedicated hapi database (not defaultdb) with two roles:
    • hapi_migrator owns the database, schemas and tables, and runs hapi migrate.
    • hapi_app has DML only via ALTER DEFAULT PRIVILEGES. The app and the admin CLI both connect as hapi_app.
  • akmadmin is never in the cluster. An idempotent hapi db bootstrap admin CLI command, run by the Owner in a pod with the akmadmin password pasted in at run time, creates the database and roles. It is rerun after any restore (a restore forks a new cluster).
  • Password auth (scram-sha-256) with manual rotation by runbook. The root password in encrypted OpenTofu state is accepted. Where the passwords live is decided in How are secrets stored and delivered?

Personal-data isolation

  • RLS on Personal-data tables (ENABLE + FORCE), with policies keyed on SET LOCAL hapi.principal_id per transaction and covering Bindings. Household data uses app-level checks only.
  • No BYPASSRLS role. Only hapi_migrator (as table owner) is exempt. Cross-Person work, such as purging a removed Person or running reminders, runs once per Person with that Person's id set.
  • Data repair:
    • Normally, reviewed Alembic data migrations run as hapi_migrator.
    • Break-glass is psql as hapi_migrator from a pod, following a runbook and recorded on the incident.
    • No "act as Person" admin mode.
  • RLS protects Persons from app bugs and leaked app credentials, not from the operator. Owner non-access is enforced by policy, not cryptography.
  • Tests and local dev run the same bootstrap and connect as hapi_app. CI fails if any owner-mixin table lacks forced RLS.

Pooling

  • App-side SQLAlchemy async pool only (asyncpg, pool_size=5, max_overflow=5, pool_pre_ping, short pool_recycle), budgeted against the plan's ~200 max_connections. No PgBouncer in v1: there's no Terraform resource for Akamai's pools and VPC support for them is undocumented.

ADR: ADR-0004 was drafted on a draft/adr-0004-database-access branch, now deleted. The accepted version, amended per the resolution below, is ADR-0004 on main.

Platform facts gathered for this ticket: VPC attach is still "open beta" with no GA entry. us-iad lists both VPCs and Managed Databases. Terraform private_network updates in place. The "private IP not supported" note refers to legacy private IPv4. Connections are capped at 100 per GB of RAM.

## Decisions (provisional, pending #64) Decided in a grilling session on 2026-11-24. This ticket stays open, blocked by **Verify Managed DB VPC attach, role setup, and RLS on a live us-iad cluster** (#64). Close it when those checks pass. If a check fails, reopen the affected point (region, or RLS) here. **Plan and provisioning** - Linode Managed **PostgreSQL 18**, **Shared 2 GB, 1 node** ($32/mo). Maintenance window pinned to a quiet slot (e.g. Tue 09:00 UTC). Moving to 3 nodes later is a plan change. - Managed by OpenTofu (`linode_database_postgresql_v2`) in the cluster's VPC. `engine_config_pg_password_encryption = "scram-sha-256"` set explicitly, because Akamai's sources disagree on the default. **Network** - **VPC attach, `public_access = false`**, with `allow_list` holding only the cluster's VPC subnet CIDR. We accept the beta risk. If `us-iad` lacks support, try `us-iad-2`, then `us-east` (Newark). The fallback is a public endpoint plus a node-IP allowlist, as an explicit ADR-0002 amendment. - **TLS `sslmode=verify-full`**, with the CA from Tofu's `ca_cert`. The app fails fast without it. Local dev uses plain Compose. **Roles and credentials** - A dedicated `hapi` database (not `defaultdb`) with two roles: - `hapi_migrator` owns the database, schemas and tables, and runs `hapi migrate`. - `hapi_app` has DML only via `ALTER DEFAULT PRIVILEGES`. The app and the admin CLI both connect as `hapi_app`. - `akmadmin` is never in the cluster. An idempotent **`hapi db bootstrap`** admin CLI command, run by the Owner in a pod with the `akmadmin` password pasted in at run time, creates the database and roles. It is rerun after any restore (a restore forks a new cluster). - Password auth (scram-sha-256) with **manual rotation by runbook**. The root password in encrypted OpenTofu state is accepted. Where the passwords live is decided in *How are secrets stored and delivered?* **Personal-data isolation** - **RLS on Personal-data tables** (`ENABLE` + `FORCE`), with policies keyed on `SET LOCAL hapi.principal_id` per transaction and covering Bindings. Household data uses app-level checks only. - **No `BYPASSRLS` role.** Only `hapi_migrator` (as table owner) is exempt. Cross-Person work, such as purging a removed Person or running reminders, runs once per Person with that Person's id set. - **Data repair:** - Normally, reviewed Alembic data migrations run as `hapi_migrator`. - Break-glass is `psql` as `hapi_migrator` from a pod, following a runbook and recorded on the incident. - No "act as Person" admin mode. - RLS protects Persons from app bugs and leaked app credentials, **not from the operator**. Owner non-access is enforced by policy, not cryptography. - Tests and local dev run the same bootstrap and connect as `hapi_app`. CI fails if any owner-mixin table lacks forced RLS. **Pooling** - App-side SQLAlchemy async pool only (asyncpg, `pool_size=5`, `max_overflow=5`, `pool_pre_ping`, short `pool_recycle`), budgeted against the plan's ~200 `max_connections`. No PgBouncer in v1: there's no Terraform resource for Akamai's pools and VPC support for them is undocumented. **ADR**: ADR-0004 was drafted on a `draft/adr-0004-database-access` branch, now deleted. The accepted version, amended per the resolution below, is [ADR-0004 on `main`](https://git.bobbypalko.com/bobby/hapi/src/branch/main/docs/adr/0004-database-access.md). Platform facts gathered for this ticket: VPC attach is still "open beta" with no GA entry. `us-iad` lists both VPCs and Managed Databases. Terraform `private_network` updates in place. The "private IP not supported" note refers to legacy private IPv4. Connections are capped at 100 per GB of RAM.
bobby removed their assignment 2026-11-24 07:51:35 -05:00
Author
Owner

Live verification results: points to re-decide

Verify Managed DB VPC attach, role setup, and RLS on a live us-iad cluster (#64) is resolved (results). VPC attach in us-iad, TLS, scram, the role split, and RLS for hapi_app all hold. Before this ticket closes, the following provisional decisions need revisiting in a grilling session:

  1. FORCE vs "hapi_migrator is exempt". FORCE ROW LEVEL SECURITY also binds the table owner, so the decisions as written contradict each other. Options:
    • (a) ENABLE only. The migrator is exempt as owner, and the CI check tests relrowsecurity.
    • (b) Keep FORCE. Data migrations and break-glass explicitly ALTER TABLE ... NO FORCE inside their transaction, or set a principal per row.
  2. The operator caveat is wider than written. RLS does not protect against leaked hapi_app credentials, because any session can set hapi.principal_id. It protects only against app bugs. Fix the ADR-0004 wording. Decide whether that's acceptable or whether something stronger is needed (e.g. signed principal context, or per-request SET ROLE to a per-Person role).
  3. Break-glass audit. The platform has no statement logging or pgaudit. Is a runbook plus an incident note enough, or should break-glass go through a wrapper that logs to hAPI's own logs?
  4. Connection budget. The Shared 2 GB plan allows 50 connections (47 usable), not ~200. 2 replicas × (5 + 5) + migrate + CLI still fits, but say so explicitly or shrink the pool.
  5. Scaling claim. An in-place resize stalled (without downtime). Drop "moving to 3 nodes or a bigger plan is just a plan change" and replace it with "plan changes may need a support ticket or a restore to a new cluster". Size up front accordingly?
  6. PostgreSQL 19 is now offered. Stay on 18 (decided) or start on 19?
  7. Bootstrap details for hapi db bootstrap: database-level grants must run as hapi_migrator. akmadmin can SET ROLE to the roles it creates and has BYPASSRLS, which fits the operator caveat. After a restore, wait for DNS (~60 s after "ready").
## Live verification results: points to re-decide **Verify Managed DB VPC attach, role setup, and RLS on a live us-iad cluster** (#64) is resolved ([results](https://git.bobbypalko.com/bobby/hapi/issues/64#issuecomment-231)). VPC attach in `us-iad`, TLS, scram, the role split, and RLS for `hapi_app` all hold. Before this ticket closes, the following provisional decisions need revisiting in a grilling session: 1. **`FORCE` vs "`hapi_migrator` is exempt".** `FORCE ROW LEVEL SECURITY` also binds the table owner, so the decisions as written contradict each other. Options: - (a) `ENABLE` only. The migrator is exempt as owner, and the CI check tests `relrowsecurity`. - (b) Keep `FORCE`. Data migrations and break-glass explicitly `ALTER TABLE ... NO FORCE` inside their transaction, or set a principal per row. 2. **The operator caveat is wider than written.** RLS does **not** protect against leaked `hapi_app` credentials, because any session can set `hapi.principal_id`. It protects only against app bugs. Fix the ADR-0004 wording. Decide whether that's acceptable or whether something stronger is needed (e.g. signed principal context, or per-request `SET ROLE` to a per-Person role). 3. **Break-glass audit.** The platform has no statement logging or pgaudit. Is a runbook plus an incident note enough, or should break-glass go through a wrapper that logs to hAPI's own logs? 4. **Connection budget.** The Shared 2 GB plan allows **50** connections (47 usable), not ~200. 2 replicas × (5 + 5) + migrate + CLI still fits, but say so explicitly or shrink the pool. 5. **Scaling claim.** An in-place resize stalled (without downtime). Drop "moving to 3 nodes or a bigger plan is just a plan change" and replace it with "plan changes may need a support ticket or a restore to a new cluster". Size up front accordingly? 6. **PostgreSQL 19** is now offered. Stay on 18 (decided) or start on 19? 7. **Bootstrap details** for `hapi db bootstrap`: database-level grants must run as `hapi_migrator`. `akmadmin` can `SET ROLE` to the roles it creates and has `BYPASSRLS`, which fits the operator caveat. After a restore, wait for DNS (~60 s after "ready").
bobby self-assigned this 2026-11-24 08:40:52 -05:00
Author
Owner

Resolution

The provisional decisions above stand. The live check (Verify Managed DB VPC attach, role setup, and RLS on a live us-iad cluster, #64) confirmed VPC attach in us-iad, verify-full, scram, the two-role bootstrap and RLS. The 7 re-decide points were settled in a grilling session on 2026-11-24:

  1. ENABLE RLS only, no FORCE. hapi_migrator is exempt as owner. CI checks that every owner-mixin table has relrowsecurity set and is owned by hapi_migrator, never hapi_app. Policies use nullif(current_setting('hapi.principal_id', true), '').
  2. RLS protects against app bugs only. Leaked hapi_app credentials can set any principal. The containment is the VPC-only allowlist, which limits use to the cluster and VPC. We accept this: a signed principal context or per-Person roles add nothing real.
  3. Break-glass goes through hapi db breakglass --reason. It runs psql as hapi_migrator and logs start, end, reason and every statement (never results) to centralized logs. An ntfy alert to the Household is a candidate for the notifications work.
  4. Connection budget. Keep pool_size=5 + max_overflow=5. The rule: (app pods including surge) × 10 + ~6 ≤ 46 usable, so at most 4 app pods. Delivery enforces the replica and surge limits, and future workers share the budget. Traffic is expected to be very low.
  5. Plan changes are restores-to-new-cluster, using the same runbook as disaster recovery. Stay on 2 GB / 1 node and don't size up front.
  6. Stay on PostgreSQL 18. Revisit at the first major-upgrade window.
  7. hapi db bootstrap waits for private DNS, creates the roles and CREATE DATABASE hapi OWNER hapi_migrator, then runs SET ROLE hapi_migrator for the database-level grants. akmadmin's BYPASSRLS falls under the operator caveat.

ADR: ADR-0004, accepted on main. It supersedes the draft/adr-0004-database-access branch.

## Resolution The provisional decisions above stand. The live check (**Verify Managed DB VPC attach, role setup, and RLS on a live us-iad cluster**, #64) confirmed VPC attach in `us-iad`, `verify-full`, scram, the two-role bootstrap and RLS. The 7 re-decide points were settled in a grilling session on 2026-11-24: 1. **`ENABLE` RLS only, no `FORCE`.** `hapi_migrator` is exempt as owner. CI checks that every owner-mixin table has `relrowsecurity` set and is owned by `hapi_migrator`, never `hapi_app`. Policies use `nullif(current_setting('hapi.principal_id', true), '')`. 2. **RLS protects against app bugs only.** Leaked `hapi_app` credentials can set any principal. The containment is the VPC-only allowlist, which limits use to the cluster and VPC. We accept this: a signed principal context or per-Person roles add nothing real. 3. **Break-glass goes through `hapi db breakglass --reason`.** It runs `psql` as `hapi_migrator` and logs start, end, reason and every statement (never results) to centralized logs. An ntfy alert to the Household is a candidate for the notifications work. 4. **Connection budget.** Keep `pool_size=5` + `max_overflow=5`. The rule: (app pods including surge) × 10 + ~6 ≤ 46 usable, so at most **4 app pods**. Delivery enforces the replica and surge limits, and future workers share the budget. Traffic is expected to be very low. 5. **Plan changes are restores-to-new-cluster**, using the same runbook as disaster recovery. Stay on 2 GB / 1 node and don't size up front. 6. **Stay on PostgreSQL 18.** Revisit at the first major-upgrade window. 7. **`hapi db bootstrap`** waits for private DNS, creates the roles and `CREATE DATABASE hapi OWNER hapi_migrator`, then runs `SET ROLE hapi_migrator` for the database-level grants. `akmadmin`'s `BYPASSRLS` falls under the operator caveat. **ADR:** [ADR-0004](https://git.bobbypalko.com/bobby/hapi/src/branch/main/docs/adr/0004-database-access.md), accepted on `main`. It supersedes the `draft/adr-0004-database-access` branch.
bobby closed this issue 2026-11-24 08:54:49 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Reference: bobby/hapi#53