mig: add auth client ids to upstream selections and EMA credentials

Merged
DA
1mo ago
+179-79

AIM-594

Stacked on #7810 (โ† #7768 โ† #7761 โ† #7653).

Summary

  • upstream_client_selections:
    • Gains auth_client_id and auth_client_attachment_scope. A composite FK to mcp_auth_clients (id, attachment_scope) pins the client to the selection's own project or org, with ON DELETE CASCADE.
    • no_auth now means neither remote_session_client_id nor auth_client_id is 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.
  • remote_session_ema_credentials:
    • Gains auth_client_id and auth_client_attachment_scope, with a scope-pinned composite FK. ON DELETE SET NULL clears only those two columns, so organization_id is 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.
  • 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 VALID and validated in separate statements under -- atlas:txmode none. Indexes are built CONCURRENTLY. 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 CONSTRAINT statements
    • ON DELETE SET NULL ("auth_client_id", "auth_client_attachment_scope")

    Re-apply them after any db:diff, then run db:hash. Without the column list, deleting an auth client would also null organization_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_id set.
  • Recovering from a failed CONCURRENTLY build: find the INVALID index (pg_index.indisvalid = false), DROP INDEX CONCURRENTLY it, then resume the migration with Atlas.

  • Rollout precondition: RecordUpstreamClientSelections must fill auth_client_id and 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 VALID and validated separately; indexes are built CONCURRENTLY. 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 global scopes), and the EMA scope CHECK allows tenant-less rows so project deletion can null the tenant columns; readers must filter organization_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_id and only covers rows still holding a session. The sign-in-keyed keys are untouched.
  • Atlas drops the NOT VALID flags, the separate VALIDATE statements, and the ON DELETE SET NULL column list on regeneration; re-apply them after any db:diff. Without the column list, deleting an auth client would also null organization_id.
  • no_auth now means neither remote_session_client_id nor auth_client_id is 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: RecordUpstreamClientSelections must fill auth_client_id for new selections before any org's auth-client resolver flag is enabled.