Repository navigation
feat(identitybroker): wire the embedded gateway's authorization server as AS-2 - #2744
Conversation
…r as AS-2 With the identity broker on and a protected MCP route, register the broker client mecatl at the embedded authorization server (private_key_jwt, keys from a temporary jwks_file, token exchange restricted to its own tokens), make the vMCP resource the issuer plus /mcp so ID-JAGs can be addressed to the issuer, and record AS-2's issuer, token endpoint, resource and an in-process client at startup for the identity broker. Production trusts no ID-JAG issuer until toolhive#6887; tests trust the offline harness through an unexported option. Startup refuses an AS-2 issuer equal to AS-1's, and sets up no AS-2 without a protected route. Closes #2738 Co-authored-by: Mecatl <[email protected]>
…he embedded gateway in the design Address review of the AS-2 wiring: - Move the in-process HTTP transport that as1server and the MCP broker's AS-2 both used into internal/inprocesshttp, with its tests; each caller keeps only its admission rule. - Validate the broker client before constructing the embedded server, and set up no AS-2 for an empty key set. - Pass the broker's real client signer through startup, and test AS-2 with hosted AS-1, external AS-1, and no AS-1, and key-file cleanup when construction fails. - Design: the embedded vMCP's resource is distinct from AS-2's issuer, and there is no embedded gateway without a protected MCP server. Co-authored-by: Mecatl <[email protected]>
ChrisJBurns
left a comment
There was a problem hiding this comment.
-
medium · maintainability — hasKeys/writeKeySet copied between packages instead of shared, though the PR extracts a shared package for the transport (
internal/adapter/mcpbroker/as2.go:90)hasKeysandwriteKeySetare byte-for-byte copies of as1server/delegate.go. Comments in both files say "fix both together". The same PR createsinternal/inprocesshttpto remove a duplicated helper, so leaving this pair copied is inconsistent. A key-set helper that writes a private temp file for ToolHive'sjwks_fileis security-relevant code, and two copies can drift apart (for example in file mode or cleanup).Recommendation: Move both helpers into one stdlib-only internal package (or into inprocesshttp's sibling, e.g. internal/jwksfile). Allow it in as1server's depguard list the way inprocesshttp was allowed, and delete the "fix both together" comments.
Write AS-2's key set before creating ToolHive's auth store, and close a store this function created when registration fails before ToolHive takes ownership. Share the key-set file helpers in internal/jwksfile instead of copying them, and pull the AS-2 setup out of newToolHiveProcess into helpers. Co-authored-by: Mecatl <[email protected]>
…by server Export one Recorder from internal/inprocesshttp and use it in as1server's discovery filter instead of a second copy of the buffered response. Refuse an AS-2 issuer that names the same server as AS-1's even when spelled differently: scheme and host compare case-insensitively, a default port equals none, and a trailing slash is ignored. Co-authored-by: Mecatl <[email protected]>
Move internal/inprocesshttp back under internal/identitybroker/adapter so the identity broker still moves into its own service as one tree, and drop as1server's depguard exception for it. The MCP broker no longer builds a client for AS-2: AS2.Handler serves only a POST to AS-2's token endpoint, and internal/app wraps it in the in-process client the identity broker uses while it shares the process, admitting only the token endpoint. Co-authored-by: Mecatl <[email protected]>
…ed package Replace internal/jwksfile with a marked copy of hasKeys and writeKeySet in as1server and in the MCP broker, and drop its depguard rule and as1server's exception for it, so the identity broker tree imports nothing from Mecatl outside it. The copies are temporary: #2538 replaces the generated broker client key with an operator-supplied key-set file and deletes both. Co-authored-by: Mecatl <[email protected]>
toolhive#7088 closed toolhive#6954 with a fixed 60-second skew and kept the 5-minute bound on a client assertion's exp, with no cap on exp - iat; the maintainer declined to make either configurable until there is a consumer. The broker already signs 60-second assertions, and only the broker client key's holder can sign one, so step (5) check 5 now states the broker's lifetime and the server's fixed bound instead of a configured 60-second server maximum. Move AS-2's "exp too far" case to 10 minutes so it stays past ToolHive's bound once the 60-second skew arrives. Co-authored-by: Mecatl <[email protected]>
The ID-JAGs the AS-2 tests mint now carry act as the design shapes it, the instance's ag_ identifier with AS-1 as its issuer, rather than a SPIFFE path, so later work does not copy the wrong form. Note in the mecak8s operator guide that turning the identity broker on or off with protected MCP servers changes the gateway's token audience, so users sign in to those servers again. Co-authored-by: Mecatl <[email protected]>
AS-2's in-process handler must refuse a POST to the embedded server's consent and callback routes, not only a GET to its other routes, so a handler that served any POST fails the test. Note that the in-process client buffers whole responses and cannot carry a streamed one, and say in the mecak8s guide that protected MCP servers must be enrolled again, as the MCP OAuth page calls it. Co-authored-by: Mecatl <[email protected]>
|
Re the
The other two review threads are fixed and resolved. |
jhrozek
left a comment
There was a problem hiding this comment.
Thanks, this reads well, and the shortcuts are easy to follow because each one names the ticket that removes it. Two questions and one test request inline; nothing blocking.
Closes #2738.
Problem
The identity broker will swap each per-call ID-JAG for a gateway token at the embedded gateway's authorization server (AS-2). That server served only the MCP broker's own logins: it had no broker client, its vMCP resource was its issuer (so the design's ID-JAG audience could not be configured), and the identity broker had no way to reach it.
What
With the identity broker on, a broker client key, and at least one protected MCP route:
/mcp, its endpoint. The login client's audience andresource, the server's allowed audience, and the vMCP's incoming audience all use it. ID-JAGs are addressed to AS-2's issuer, as the design says; ToolHive refuses that audience while it is also the resource.mecatlat AS-2:private_key_jwtwith the broker client's public keys, written to a private temporaryjwks_fileand removed after construction; grants token exchange and JWT-bearer; scopeopenid; audience the vMCP resource; token exchange restricted to its own tokens, so it can't exchange users' login tokens.buildIdentityBrokerreturns the broker client key set;buildMCPBrokertakes it in a newmcpBrokerIdentityparameter (which MCP broker authorizes gateway calls and sends them to the configured gateway #2536 extends) and registersmecatl; startup then records AS-2's issuer, token endpoint, resource and an in-process client (10 s timeout) for Per-call grant and gateway token #2535 to use. Startup refuses an AS-2 issuer that names the same server as AS-1's: scheme and host compare case-insensitively, a default port equals none, and a trailing slash is ignored.mcpbroker.AS2.Handler()answers aPOSTto AS-2's token endpoint and nothing else (other paths 404, other methods 405).internal/appwraps it in the identity broker's in-process client (as2InProcessClient), which admits only the token endpoint URL. The broker never sees the MCP broker.Identity broker off, or an injected identity broker core with no key: nothing changes.
The ID-JAG harness gains
AccessTokenSpec.Claimsso it can sign ID-JAGs withresourceandact.Design and shortcuts
Design companion: #2773 records the end-state rules this review surfaced (AS-1 in-process calls, AS-2's anonymous routes, distinct AS-1/AS-2 issuers). Review the two side by side.
mcp-gateway-modes.md, Embedded mode, added here): the vMCP's resource is distinct from AS-2's issuer, and there is no embedded gateway without a protected MCP server.implementation.md, "Modulith constraints"): it imports nothing from Mecatl outsideinternal/identitybroker, so it can move into its own service as one tree.internal/identitybroker/adapter/inprocesshttp. AS-1 uses it, andinternal/appuses it to reach AS-2. Onlyappmay import broker adapters.hasKeys,writeKeySet) are a deliberate, marked duplicate inas1serverandmcpbroker(keysetfile.go), not a shared package. They are temporary; Keys and gateway entries for the identity broker #2538 deletes both.mecatlcan exchange a gateway token issued to itself (Configure the embedded gateway's authorization server as AS-2 #2533, after Add a delegated-access marker that gates credential lookup for approved delegated tokens toolhive#6404);resource, not the ID-JAG'sresourceclaim; the client's audiences allow only the vMCP resource, so there is no exposure today (Configure the embedded gateway's authorization server as AS-2 #2533, after ID-JAG redemption: compare the requested resource with the assertion's resource claim, and decide the default when it is omitted toolhive#6892 and ID-JAG grant ignores the assertion's resource claim and requires a form resource toolhive#6983);mecatlrecords with Redis (Phase 1: Signed-in users' agents call gateway tools with their own access #2451's one-replica row, Keys and gateway entries for the identity broker #2538);aud(Phase 1: Signed-in users' agents call gateway tools with their own access #2451's row for it, after Authorization server: require the issuer as the only aud of a private_key_jwt client assertion toolhive#7039);expis up to 5 minutes ahead, ToolHive's fixed bound (plus a fixed 60-second skew after Allow clock skew on private_key_jwt assertions toolhive#7088, which closed Authorization server: allow clock skew on private_key_jwt client assertions toolhive#6954 without a configurable maximum). The broker signs 60-second assertions, so this PR changes step (5) check 5 to accept the server's fixed bound, and moves the "exp too far" test to 10 minutes so it stays past the bound after the ToolHive bump;exp,iat, andnbfexactly, and Allow clock skew on private_key_jwt assertions toolhive#7088's 60-second skew is in no release yet (the ToolHive bump that carries it);Tests
internal/adapter/mcpbroker/as2_test.go):mecatlregistration and the vMCP resource; the temporary key file is gone after construction; a real gateway login yields a token for issuer +/mcpthat the vMCP accepts, and an issuer-audience token is refused;mecatlcan't exchange a login token; assertion refusals (other audience, expired,exp> 5 min ahead, reusedjti, unregistered key); a harness-signed ID-JAG redeems once for a gateway token withact,client_idmecatland no refresh token, which passes an MCPinitialize; production configuration refuses the same ID-JAG;AS2.Handler()serves only aPOSTto the token endpoint, while the embedded server does serve the other routes.internal/app): AS-2 recorded throughBuildwith the broker key accepted and another key refused; the recorded client refuses other URLs; nothing recorded with the broker off, an injected core, no protected route, or no MCP broker; AS-2/AS-1 issuer collision refused (hosted AS-1, external AS-1, and the same server spelled differently);sameIssuertable test;TestBuildKeeps{Bundled,Configured}MCPBrokerWithIdentityBrokerunchanged.internal/identitybroker/adapter/inprocesshttp): the client AS-1 and AS-2 are reached through, and the responseRecorderas1server's discovery filter also uses, with its tests (served response, deadline, abandoned and refused request bodies closed, panic re-raised, no redirects); each caller tests its own admission rule.keysetfile_test.goinas1serverandmcpbroker): the key check,0700/0600modes, removal, and no key material in errors, duplicated with the helpers.API compatibility
Assessment: no change to the versioned gRPC/HTTP API. With the identity broker on (an experimental mecak8s flag), the embedded MCP gateway's protected-resource
resourceand its login tokens' audience change from the gateway issuer to issuer +/mcp. The gateway is served on the server's mux but is not part of the versioned contract, and only Mecatl's own broker login client andmecatlobtain its tokens. No major or feature change. Upgrade: turning the identity broker flag on or off makes users log in to protected MCP servers again.Follow-ups