Skip to content

[M2.3c2a] Separate database runtime and migration privileges - #81

Merged
samuelmbabhazi merged 2 commits into
mainfrom
feat/m2.3c2a-runtime-database-role
Nov 5, 2026
Merged

samuelmbabhazi merged 2 commits into
mainfrom
feat/m2.3c2a-runtime-database-role

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

M2.3c2a, stacked on #80 (feat/m2.3c1-tenant-transactions).

What changed and why

Separate the administrative migration URL from the API runtime URL. Add explicit restricted-role provisioning, a loopback-only development setup command and deployment instructions. Runtime grants exclude migration tracking, public-schema creation and tenant DDL; audit events allow SELECT/INSERT only. Existing credentials are never rewritten.

Create the queue schema during migration and disable pg-boss schema creation at runtime. Provisioning transfers only existing queue objects inside shipkeep_jobs, preserving queued work while leaving tenant tables under administrative ownership. A disposable restricted-login app fixture enables future tenant acceptance tests.

Validation

  • All 128 PostgreSQL integration tests pass, including four new full-app restricted-login tests: account registration/verification with audit, tenant transactions, forbidden audit/DDL operations, queue preservation on repeated provisioning and unsafe-role rejection.
  • The development setup command was exercised against a disposable PostgreSQL instance: initial creation, rerun, real login and unsafe-configuration rejection passed.
  • API lint, typechecks, 103 unit tests and build pass; tooling ESLint passes. Workspace web/domain checks, Go tests and buf lint passed earlier in this session; their sources are unchanged.

Security considerations

The runtime group is NOLOGIN and has no elevated role attributes or memberships. The SQL provisioning script operates on a dedicated Shipkeep database and must run transactionally as its administrator. It never globally reassigns administrative objects. Development setup accepts only matching loopback databases and a separate shipkeep_app login, refuses production mode, and prints no credentials or driver diagnostics. No application shell invocation or new runtime domain command is introduced. M2.4 retains the remaining audit guarantees/export/UI scope.

Existing development .env files must explicitly adopt the two URLs; this PR does not rewrite them. Production operators must apply migrations, then runtime grants, and configure a private login before exposing tenant routes.

Checklist

  • M2.3c2a acceptance covered with a real restricted login
  • PostgreSQL regression suite and API/tooling checks pass
  • Development setup tested on a disposable database
  • SPEC, PLAN, README and deployment instructions updated
  • No automatic password overwrite, secret output or application shell call

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.3c1-tenant-transactions branch from 4cab3ff to d90d39d Compare November 5, 2026 12:45
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m2.3c1-tenant-transactions to main November 5, 2026 12:45
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.3c2a-runtime-database-role branch from 840011a to 2fcc9a0 Compare November 5, 2026 12:46
@samuelmbabhazi
samuelmbabhazi merged commit c212404 into main Nov 5, 2026
10 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m2.3c2a-runtime-database-role branch November 5, 2026 12:46
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…#82)

## Task

M2.3c2b1, stacked on #81 (`feat/m2.3c2a-runtime-database-role`).

## What changed and why

Allow an authenticated account to discover its own organizations before
selecting a tenant. SELECT-only RLS policies expose only its memberships
and organization metadata, and only while the account is active and
verified. Project/environment contents, role bindings and tenant writes
remain inaccessible in discovery mode.

Add `runAccountDiscovery` with a transaction-local account setting.
Tenant and account scopes are mutually exclusive, preserve same-scope
nesting and clear both settings on exit. A caller cannot switch account
or scope within an active transaction context. The identity must come
from authentication, never a caller-selected account parameter.

## Validation

- Eight restricted-login PostgreSQL tests cover independent account
discovery, disabled/pending/unknown accounts, private resource contents,
write denial, nested-scope rejection, cleanup, current account status
and immediate membership removal.
- Existing restricted-role fixtures receive only the user columns needed
by the account-status policy, preserving their lack of credential
access.
- All 136 PostgreSQL integration tests pass, including concurrent
last-owner protection and pooled transaction isolation.
- API lint, types, 103 unit tests and build pass. Workspace web/domain,
Go and buf checks passed earlier in this session; their sources are
unchanged.

## Security considerations

No security-definer function or RLS bypass is added. Discovery does not
expose other members, grant tenant write permissions or substitute for
live application authorization. No tenant HTTP route is exposed by this
slice; scoped repositories and authorization adapters follow.
Account-status checks use existing identity columns. No dependency,
application shell call, secret or new domain mutation is introduced.

## Checklist

- [x] M2.3c2b1 acceptance and cross-account boundaries covered
- [x] PostgreSQL regression suite and API checks pass
- [x] SPEC and PLAN updated
- [x] No new application mutation requiring an audit event
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
… RLS (#99)

## Task

Review follow-up for #79 (M2.3b3) and #81 (M2.3c2a). Refs #22

## What changed

- `deploy/postgres/runtime-role.sql`: the runtime role now gets only
SELECT and INSERT on `data_encryption_keys` and `installation_keys`,
like `audit_events`. No code updates or deletes these rows: both stores
only use `findUnique` and `create`, and SPEC 9 says rotation inserts new
rows.
- `runtime-role.integration.spec.ts`: UPDATE and DELETE on both key
tables must now be rejected for the runtime login.
- PLAN: new task **M2.3g**, row-level security for `nodes`,
`join_tokens`, `node_certificates` and `audit_events`, with cross-tenant
lookups moved to a separate reviewed role. It must land before any node,
join-token or audit read endpoint.
- SPEC 16: open question describing the gap and what the cross-tenant
role must cover.

## Why

- With UPDATE or DELETE, an injection or logic bug in the API could
delete or overwrite wrapped data-encryption keys, making every secret of
an environment unrecoverable, or replace the CA and confirmation keys.
- The fleet and audit tables carry `organization_id` but only repository
filters protect them. SPEC 4 requires both layers, and the gap was not
tracked anywhere (#79 even marked the database isolation task done).

## How it was tested

- `nx run-many -t lint typecheck -p api` in a clean worktree: clean.
- The runtime-role integration test needs PostgreSQL. It could not run
locally (no Docker in the sandbox), and GitHub Actions is blocked by the
account spending limit. CI must pass before merge.

## Security considerations

Least privilege for key material. No change to tenant tables. The RLS
gap is documented and scheduled, not fixed here, because it needs a
design decision on the cross-tenant role (enrollment by join-token hash
happens before any tenant context exists).

## Checklist

- [x] Lint and typecheck pass locally
- [x] Integration test extended
- [x] No new shell invocation and no new secret exposure path
- [x] Audit events: not applicable
- [x] PLAN and SPEC updated
- [ ] CI green (blocked by billing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant