feat: resolve upstream auth through typed auth clients behind a flag

Merged
DA
1mo ago
+2755-24

AIM-594

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

Summary

A typed auth-client resolver for direct MCP endpoints, gated per organization:

  • Modes:
    • Off: the old selection decides, unchanged.
    • Shadow (auth-client-resolver-shadow): the old selection still decides. The new resolver runs alongside without acquiring anything, and reports disagreements.
    • Enforced (auth-client-resolver): the new resolver decides.
  • Validation: the selection's auth_client_id decides. A missing or unmapped choice, or a client that is gone, soft-deleted, disabled, unlinked, in another tenant, or has a deleted or detached registration, is a gap that asks the user to reconnect, never no-auth.
  • No-auth: a no_auth choice with no client set. requires_upstream_auth = true forbids it, false allows it, and NULL allows it only when the server has no live clients, counting legacy attachments and disabled links.
  • Types:
    • remote_session and service_account: narrowed to their registration, using the existing token lookup and self-client minting.
    • static_credential and pass_through: headers only, no bearer.
    • ema: still served the old way (follow-up PR).
  • Legacy fallbacks: these cases are served the old way under enforced and reported in shadow:
    • unmapped_choice: OAuth choices AIM-595 hasn't mapped yet, and no-auth choices on servers whose only clients are header clients.
    • client_headers_stale: header copies that no longer match the server's current headers.
    • legacy_may_chain: a personal connection whose registration also backs an EMA client.
    • Tunneled servers with header-only clients.
  • Setup gap: a server credential header the chosen client doesn't carry is reported as a setup gap.
  • Headers:
    • The server's credential headers are dropped.
    • Non-credential server headers come first, then the client's headers, then the bearer last. A client header can't override the bearer.
    • A client header that would read the inbound Authorization (the Speakeasy session token on these endpoints) is dropped if optional, and is a setup gap if required.
  • Permissions: a stub authClientPermitter gets a request struct with the credential and represented user. A denial is a 403 with the request-access link, not a reconnect. Shadow and refresh never call it. AIM-589 replaces the stub.
  • Refresh: for enforced orgs, a gap sends the refresh to reconnect instead of looping.
  • Observability:
    • mcp.auth_client.parity: carries outcome, legacy outcome, agreement and a closed reason label, with an error outcome when the resolver fails.
    • gram.auth_client.* span attributes, in both shadow and enforced.
    • A debug-level auth client resolved line carrying ids only.
  • Legacy query change: GetUpstreamClientSelection / ListUpstreamClientSelections treat a selection that names a static_credential or pass_through client and no OAuth client as the old no-auth, matched on type only. That keeps flags-off behavior identical once AIM-595 maps those selections.

Motivation

RFC milestone 2: one resolver that serves a server's chosen auth client of any type, so upstream auth no longer depends on the server's sign-in. Shipping it behind a flag, with a shadow mode, lets us prove parity on our own orgs before anything changes for customers.

Rollout notes

  • Flags: auth-client-resolver-shadow is on for the Speakeasy orgs only. auth-client-resolver exists but is off.
  • Preconditions for enforcing any org:
    • AIM-595 maps existing selections, and the selection writer fills auth_client_id for new ones.
    • AIM-589/591 replace the permission stub.
    • The token-lookup decoupling PR (stacked on this one) lands.
  • Known limitation: an explicit no-auth choice and an unmapped one look the same on header-only servers, so both are served the old way for now.
  • Out of scope: gateways, EMA and public paths come in later PRs.
  • AIM-630: the legacy path forwards the inbound Authorization through configured pass-through headers on sign-in-protected endpoints. That's tracked separately; this PR blocks it only on the new path.

๐Ÿค– Generated with Claude Code


Summary by cubic

Adds a typed auth-client resolver that decides upstream authentication on direct MCP endpoints, gated per organization behind two flags. Off leaves the old client selection serving unchanged; shadow runs both and records disagreement; enforced makes the resolver authoritative. A chosen client that is gone, disabled, unlinked, or in another tenant is a gap that asks the user to reconnect, never an implied no-auth.

Behavior

  • Resolves OAuth clients (remote_session and service_account) through their registration, and static_credential and pass_through clients through their headers alone; EMA keeps the old path pending a follow-up.
  • Drops the server's credential headers when a client is chosen; non-credential server headers come first, then client headers, then the bearer last. Auth client headers deny the same pass-through sources as the proxy: a header reading the inbound Authorization or another forwarded source is dropped if optional and a setup gap if required.
  • Falls back to the old path under enforced, reporting the difference in shadow, for unmapped choices, stale header copies (no matter the client type, so a rotated or remapped credential is never sent from its stale copy), legacy identity chaining, and tunneled servers with header-only clients.
  • Uses a stub permission check that denies with the request-access link, not a reconnect.
  • Sends an enforced org's refresh to reconnect when resolution fails, instead of looping.
  • Reads a selection that names a static_credential or pass_through client as the old no-auth, so flags-off behavior stays identical once AIM-595 maps those selections.

Rollout

  • auth-client-resolver-shadow is on for the Speakeasy orgs only; auth-client-resolver exists but is off.
  • Enforcing an org requires AIM-595's mapping, AIM-589/591 replacing the permission stub, and the token-lookup decoupling PR to land.