feat: persist credential-bound upstream OAuth client selections

Merged
DA
1mo ago
+2042-15

AIM-585

Summary

Records per-credential upstream OAuth client choices. Serving behavior doesn't change.

  • Credential reference. A user_session credential is COALESCE(user_sessions.credential_id, id). Refresh rotation now copies it onto the new row, so every row of one credential shares it. New rows leave it NULL and are their own credential. This covers user, agent and workload sessions from the MCP token endpoint, inbound ID-JAG sessions, and dashboard playground sessions.
  • Choices at issuance. When a new credential is minted, RecordUpstreamClientSelections runs in the same transaction as the session row (the dashboard mint path is now transactional too). It writes one choice per server the credential can reach: servers on its issuer, members of a gateway signing in through it, and every server in the organization for an organization issuer.
    • A client is chosen when it is the server's single associated client that the credential's issuer also binds, which is exactly what the issuer path serves today. When the issuer binds several of the server's clients, the client picked on the consent page is chosen: the authorization grant carries the pick (UserSessionGrant.UpstreamClientPicks) and issuance passes it to the statement, which records it only if it is still eligible. Without a pick, as for the backfill and dashboard minting, the choice is left to a reconnection.
    • No-auth is chosen for a server with no associated client: a server uses exactly the clients associated with it. If the server is later given a client, the no-auth choice becomes unusable, so the credential reconnects and records the client.
    • Only proxied (remote and tunneled) servers get choices. Hosted and unproxied servers keep serving from the issuer's bindings and are never coverage gaps.
    • Anything else is left without a row, as a reportable gap. Choices are never replaced, never pin a token or grant generation, and are never inferred on a request.
  • Read for cutover. GetUpstreamClientSelection returns the choice plus usable. A detached or deleted client, or a no-auth choice on a server that has since gained a client, reads as unusable, so the caller fails closed instead of switching clients.
  • Backfill. Adds the selectionbackfill package (server/internal/mcpservers/upstreamclients/selectionbackfill). It runs per organization after the association backfill and records choices for every live credential through the same statement issuance uses. It reports coverage by credential class, with gaps classified as ambiguous or unreachable_client. The repair for a gap is reconnecting. Staff run it from the admin dashboard in #7659.

Out of scope, unchanged: principal credentials, assistant tokens, chat sessions and API keys (including agent keys and their attachments) have no durable credential record here and keep resolving through issuer bindings. Hosted toolsets are unchanged. No new RBAC scopes or client types.

Motivation

Cutting serving over to per-server clients (AIM-586) needs, for every credential in scope, the choice that credential was issued with. Principal-wide or consent-derived selection can't express two credentials of the same person using different clients, or survive refresh without pinning tokens. Recording choices at issuance covers new credentials from the moment this ships. The backfill covers existing ones, and #7653 merges only once every organization shows as migrated.


Summary by cubic

Records, at credential issuance, the upstream OAuth client each user session credential uses per MCP server, preparing for the AIM-585 cutover to per-server clients. Serving still resolves through issuer bindings; these rows are written but not yet read.

  • Credential reference is COALESCE(user_sessions.credential_id, id); refresh rotation copies it, so choices survive without pinning tokens or grants.
  • A client is chosen only when it is the server's single associated client that the credential's issuer also binds; when the issuer binds several, the consent page's pick is carried through the authorization grant and recorded. Paths without a consent page record only an unambiguous assignment. No-auth is recorded for any server with no associated client, and never for a gateway member a grant names for its gateway's clients, whichever issuer minted it. Only proxied (remote or tunneled) servers are considered; hosted and unproxied servers keep serving from issuer bindings and are never gaps. Ambiguous or missing pairs stay as reportable gaps, never inferred.
  • Selections copy the chosen client's scope. GetUpstreamClientSelection reports usable; a detached or deleted client, a no-auth choice on a server that gained a client, or a client no longer bound to the credential's issuer fails closed instead of switching.
  • Minting records choices in the same transaction as the session row; the authorization_code grant now does this for every subject, not just the dashboard mint path.
  • The backfill scopes credentials to their issuer's organization, runs dry-run by default, is idempotent, rereads coverage from stored choices after recording, and reports coverage by credential class with gaps classified as ambiguous or unreachable_client.

Migration

  • Run go run ./cmd/tools/migrations credential-upstream-selections after the MCP server association backfill reports gaps=0; it covers every live user session credential.
  • UPSTREAM_SELECTIONS_BACKFILL.md documents the repair procedure for gaps.