mig: add auth client ids to upstream selections and EMA credentials
AIM-594
Stacked on #7810 (โ #7768 โ #7761 โ #7653).
Summary
upstream_client_selections:- Gains
auth_client_idandauth_client_attachment_scope. A composite FK tomcp_auth_clients (id, attachment_scope)pins the client to the selection's own project or org, withON DELETE CASCADE. no_authnow means neitherremote_session_client_idnorauth_client_idis set. A selection naming a static or pass-through client is never "no auth", and deleting a client leaves a missing selection (a gap), never a fake no-auth.
- Gains
remote_session_ema_credentials:- Gains
auth_client_idandauth_client_attachment_scope, with a scope-pinned composite FK.ON DELETE SET NULLclears only those two columns, soorganization_idis kept. - The scope CHECK allows tenant-less rows: project deletion nulls the tenant columns and leaves orphans. Readers must filter
organization_id IS NOT NULL.
- Gains
- New partial unique keys keyed by auth client, without the sign-in:
- The external key is keyed by org, auth client, resource and subject.
- The Speakeasy key also includes
user_session_id, and only covers rows that still have a session. - The old sign-in-keyed keys are untouched.
- Nothing reads the new columns yet.
Motivation
Auth-client resolution (RFC milestone 2) records which typed auth client each selection and EMA token uses. EMA tokens stop being keyed by the sign-in, so one token can serve every server on the same EMA client.
Migration notes
-
Locking: new FKs and CHECKs on the existing tables are added
NOT VALIDand validated in separate statements under-- atlas:txmode none. Indexes are builtCONCURRENTLY. Validation can't fail, because the new columns are NULL on every existing row. -
Hand edits that Atlas drops on regeneration:
NOT VALID- the separate
VALIDATE CONSTRAINTstatements ON DELETE SET NULL ("auth_client_id", "auth_client_attachment_scope")
Re-apply them after any
db:diff, then rundb:hash. Without the column list, deleting an auth client would also nullorganization_id. -
Accepted Atlas warnings:
- PG110 (column padding).
- MF101 on the new partial unique indexes. They can't fail, because no existing row has
auth_client_idset.
-
Recovering from a failed
CONCURRENTLYbuild: find the INVALID index (pg_index.indisvalid = false),DROP INDEX CONCURRENTLYit, then resume the migration with Atlas. -
Rollout precondition:
RecordUpstreamClientSelectionsmust fillauth_client_idand its scope for new selections before any org's auth-client resolver flag is turned on. Otherwise new credentials resolve as gaps.
๐ค Generated with Claude Code
Summary by cubic
Adds auth_client_id and auth_client_attachment_scope to upstream client selections and EMA credentials, so the auth client resolver can record which typed auth client a choice names and key EMA tokens per auth client instead of per sign-in. One EMA token now serves every server on the same auth client; nothing reads the new columns yet.
Migration
- New FKs and CHECKs are added
NOT VALIDand validated separately; indexes are builtCONCURRENTLY. Validation can't fail because the new columns are NULL on every existing row. - Auth-client scope is pinned to the selection's own project or org (no
globalscopes), and the EMA scope CHECK allows tenant-less rows so project deletion can null the tenant columns; readers must filterorganization_id IS NOT NULL. - New partial unique indexes key EMA tokens by auth client instead of sign-in: external by org, auth client, resource, and subject; Speakeasy also includes
user_session_idand only covers rows still holding a session. The sign-in-keyed keys are untouched. - Atlas drops the
NOT VALIDflags, the separateVALIDATEstatements, and theON DELETE SET NULLcolumn list on regeneration; re-apply them after anydb:diff. Without the column list, deleting an auth client would also nullorganization_id. no_authnow means neitherremote_session_client_idnorauth_client_idis set. Deleting a client leaves a missing selection (a gap), never an implied no-auth; soft-deleted (retired) auth clients keep their selections, which read as gaps too.- Rollout precondition:
RecordUpstreamClientSelectionsmust fillauth_client_idfor new selections before any org's auth-client resolver flag is enabled.