Skip to content

feat(db): split database roles into owner, migrate, worker and api - #976

Draft
bencap wants to merge 10 commits into
integration/rls-baselinefrom
feature/bencap/827/database-roles
Draft

bencap wants to merge 10 commits into
integration/rls-baselinefrom
feature/bencap/827/database-roles

Conversation

@bencap

@bencap bencap commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

Row-level security doesn't apply to a table's owner, and today the api's login owns every table, so no policy could ever bind it. This splits the database roles into mavedb_owner (owns everything, can't log in), mavedb_migrate (the migration login, runs as the owner), mavedb_worker and mavedb_api. Neither of the last two owns anything or belongs to any role. Nothing changes for users: no policies exist yet, so the Python permission checks still decide what anyone sees.

  • Registry: src/mavedb/db/rls.py lists every table, view, materialized view and function in the schema, the access each role gets, and whether a policy will decide the api's rows (Write RLS policies for every guarded table as migrations #809). Anything unlisted gets no privileges, and CI fails while anything in the schema has no entry.
  • Bootstrap: src/mavedb/db/bootstrap.py makes the grants match the registry exactly and fails closed. It refuses roles carrying privileges or memberships it didn't give them, and never sets a password. python -m mavedb.scripts.bootstrap_roles setup runs once per database as the master user.
  • Migrations: alembic/env.py reapplies the grants at the end of every upgrade, in the same transaction, so a table can't commit without its grants. The upgrade stops on a database that was never set up. Migrations may no longer GRANT or REVOKE (bin/check-migrations).
  • Materialized views: the worker refreshes them through mavedb_refresh_mat_views(), a SECURITY DEFINER function only it may run, since refreshing requires owning the view.
  • Worker: its lock timeout moves from an on-connect SET to its role.
  • Baseline: extensions are left out of the squashed baseline, so mavedb_migrate can replay it into an empty database.
  • CI: the replay job sets the database up, then migrates as mavedb_migrate, as a new deployment does.

The api keeps its grants on mapped_variants and the legacy keyword tables. It never reads them, but deleting a score set or experiment loads them through ORM relationships, which failed as mavedb_api once revoked.

Closes #827

Outside the diff

  • Release order matters. setup has to run on each database before this deploys, the first grants run moves ownership away from the api's login, and the worker needs its own credentials. The runbook covers staging, the rollback rehearsal, then production.
  • A deployment PR gives the mapper its own gene_normalizer login and moves migrations and dumps to mavedb_migrate in releasing.md and database-dumps.md.
  • Local databases: alembic upgrade head stops until bootstrap_roles setup --commit has run once as postgres. The local stack's move onto the roles is a separate issue.
  • Issue edits to follow: Write RLS policies for every guarded table as migrations #809's tables-outside-RLS list (the legacy keyword tables keep api grants) and deployment#35's restore role (restores set session_replication_role as the master user, not mavedb_migrate).

No API schema change.

Tests

  • tests/db/test_bootstrap.py: each part on a fresh database and on rerun, unsafe existing roles and memberships refused, grants matching the registry exactly, a simulated RDS master user (non-superuser) leaving no borrowed membership, real logins as each role (no CREATE, no SET ROLE, owner can't log in, worker has nothing on access_keys), the refresh function, and a full alembic upgrade head as mavedb_migrate after setup.
  • tests/routers/test_api_role.py: score set delete, target replacement and experiment delete with the app logged in as mavedb_api.
  • The test template is now built under the roles. The rest of the suite still connects as a superuser; moving it onto the roles is Run tests and local development under the production database roles #834.
  • Ran bin/check and the PR set (bin/t tests/ -n 6 -m "not network and not slow"): 5,675 passed.

Checklist

  • Targets the open patch branch if this is a small, non-breaking change, or the cycle's release-<year>.<n>.0 branch otherwise; never main
  • API schema change? A matching mavedb-ui PR regenerates src/schema/openapi.d.ts (none needed)
  • Docs and instruction files touched by this change are updated

@bencap bencap added this to the R2026.4 milestone Nov 12, 2026
@bencap bencap added the app: backend Task implementation touches the backend label Nov 12, 2026
@coveralls

coveralls commented Nov 12, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 46492354554

Warning

No base build found for commit 942937f on integration/rls-baseline.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 91.383%

Details

  • Patch coverage: 8 uncovered changes across 1 file (200 of 208 lines covered, 96.15%).

Uncovered Changes

File Changed Covered %
src/mavedb/db/bootstrap.py 166 158 95.18%
Total (4 files) 208 200 96.15%

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 20238
Covered Lines: 18494
Line Coverage: 91.38%
Coverage Strength: 0.91 hits per line

💛 - Coveralls

Base automatically changed from release-2026.3.3 to main November 16, 2026 20:12
@bencap
bencap changed the base branch from main to release-2026.4.0 November 16, 2026 20:26
@bencap
bencap changed the base branch from release-2026.4.0 to release-2026.3.4 November 17, 2026 20:59
Production's schema dump carried CREATE EXTENSION pg_stat_statements and
its COMMENT, a monitoring extension installed on production, not part of
MaveDB's schema. Creating it needs a superuser and commenting on it needs
its owner, so mavedb_migrate couldn't replay the baseline into an empty
database. bin/build-baseline now drops extension statements, and the
current baseline loses the same lines. Production and staging are stamped
at the baseline, so nothing changes for them.

Expect: agents squashing migrations leave extensions to the master user
instead of carrying them into a baseline.
Row-level security doesn't apply to a table's owner, and today the api's
login owns every table, so no policy could ever bind it. The roles are
now mavedb_owner (owns everything, can't log in), mavedb_migrate (runs as
the owner), mavedb_worker and mavedb_api, neither of which owns anything
or belongs to any role.

mavedb.db.rls lists every table, view, materialized view and function in
the schema with the access each role gets, and whether a policy decides
the api's rows. mavedb.db.bootstrap makes the database's grants match it
exactly, and fails closed: anything unlisted gets nothing, and it refuses
roles carrying privileges or memberships it didn't give them.

Setup (roles, schema grants) runs once per database as the master user,
through scripts/bootstrap_roles. alembic/env.py reapplies the grants at
the end of every upgrade, in the migration's transaction, so a table can
never commit without the grants its entry gives it, and the upgrade stops
on a database that was never set up. Migrations may not GRANT or REVOKE.

The worker owns nothing, so it refreshes materialized views through
mavedb_refresh_mat_views(), a SECURITY DEFINER function only it may run.

The api keeps grants on mapped_variants and the legacy keyword tables,
which it never reads: deleting a score set or experiment loads them
through ORM relationships, which failed as mavedb_api once revoked.

Expect: agents add an rls.py entry for every new table, view or function
and never grant in a migration; migrations run as mavedb_migrate.
The worker set lock_timeout on every connection it opened, because it
shared a login with the api. It has its own login now, so the limit lives
on the role (ALTER ROLE mavedb_worker SET lock_timeout, from the role
bootstrap), and the on-connect SET and DB_WORKER_LOCK_TIMEOUT_SECONDS go.
The replay job sets the database up as the master user, then migrates as
mavedb_migrate, as a new deployment does. A migration that needs more
than the owner's rights, or a database left without its roles, now fails
here instead of at deploy.
The restore steps set session_replication_role, then SET ROLE
mavedb_owner. The public dump sets session_replication_role itself, and
after the switch Postgres checks that against the owner, which may not
set it, so the load failed before writing anything. The master user now
borrows the owner's privileges by membership and loads as itself.
The pytest jobs started a server from the runner's own Postgres binaries,
now 17, while production, the replay job and bin/test-db run 15. Role and
privilege rules changed between those versions: from 16 a CREATEROLE user
needs ADMIN OPTION to grant itself a role, and 17 checks MAINTAIN, not
ownership, before a materialized view refresh. The bootstrap tests failed
on behavior production doesn't have. bin/test-db pins the server to 15,
and bin/t already uses it when it's running.
@bencap
bencap force-pushed the feature/bencap/827/database-roles branch from 572ba08 to 1c6a507 Compare November 17, 2026 21:20
Base automatically changed from release-2026.3.4 to main November 30, 2026 18:02
@bencap
bencap changed the base branch from main to release-2026.3.5 November 30, 2026 18:04
…ent drop

It revised 020b7e003efd, which annotation_status (9a73f37ec1bc) and the
annotation_event drop (4cb129146996) have since built on, leaving two
heads once this branch took in integration/rls-baseline.
…notation_event

The cutover dropped annotation_event and v_current_annotation_events.
annotation_status and v_annotation_status_current take the same entry:
only the worker and operator scripts read them, so the api gets no
grant. The api-role delete test now plants an annotation_status row, so
it still proves a score set delete reaches the worker-only table through
the foreign key and not through the api's own session.
api_role_client put its own session and user on the shared app and
never took them off. The app fixture re-installs its own stand-ins for
each test, which hid the leak, but any test that signs in for real
would have been answered by the stale user, bound to a closed session.
@bencap
bencap changed the base branch from release-2026.3.5 to integration/rls-baseline November 30, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: backend Task implementation touches the backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants