Repository navigation
feat(mcpbroker): AS-2 resolves AS-1's ID-JAG users through the company IdP - #3015
Conversation
v0.70.1 turns on subject resolution at ID-JAG redemption (stacklok/toolhive#7247), which #2533 needs. Co-authored-by: Mecatl <[email protected]>
…y IdP With mcpbroker.AS1Trust set, the embedded gateway's authorization server (AS-2) trusts AS-1's ID-JAGs in ToolHive's subject mode resolve: it finds the gateway user from the ID-JAG's upstream_subject through the company IdP, which becomes the first step of AS-2's sign-in, accepts only the agent trust domain as the actor's issuer, and issues marked gateway tokens that ToolHive refuses at token exchange. This replaces the test-only per-subject trust. Composition still sets the trust only in tests; the next change wires it from mecak8s's configuration. What only tests may set (the vMCP's maximum call duration, and a sign-in client in AS-2's store) moves to mcpbroker.TestSeam, kept out of production code by an internal package and depguard. mcpbrokertest signs users in at AS-2 so tests across packages can redeem through resolution. Part of #2533. Co-authored-by: Mecatl <[email protected]>
…s scans `go work sync` dropped the go.mod hashes the standalone modules need, so `GOWORK=off` builds failed; `task tidy:module` restores them for environment/microvm, authn/oidc and the providers. go-redis 9.24 deprecates ScanIterator.Next and Val, so the Redis stores range over Vals instead. Co-authored-by: Mecatl <[email protected]>
Co-authored-by: Mecatl <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
Approving: the trust model holds, and production doesn't turn it on yet, so none of the comments below affect a running gateway. I couldn't find a configuration that lets AS-2 redeem for more users than it should. It accepts ID-JAGs only, for its own issuer, with the one configured actor issuer, and the client and resource are fixed. Gateway tokens carry the marker, last at most five minutes, get no refresh token and can't be exchanged. The test seam stays out of production, and the tests assert what the description says.
The inline comments are yours to take here or in a follow-up, whichever you prefer.
ToolHive identity binding (note, no action here). The stored company IdP identity is keyed by the upstream name (mecatl-company-idp) and the claim value only. If an operator changes the company IdP's issuer or claim while keeping the store, a matching value resolves to the old user. This is fixed in ToolHive by "Bind provider-identity rows to their issuer and claim" (stacklok/toolhive#7347), which merged after v0.70.1 was tagged. We'll bump ToolHive to the release that contains it before the trust is wired in production.
For the wiring PR. Nothing here needs to change in this PR:
- With the trust on, every sign-in at AS-2, including the existing workspace enrollment, gains a company IdP step. It's silent only while the user's IdP session is live, so the operator guide should mention it.
- Turning the trust on over an existing AS-2 store probably re-keys users to their company identity, which would orphan the provider connections they already made. That needs checking, or at least an operator note.
- The design still says
Vgets AS-2's default lifetime of one hour "until #2533" (credentials-and-lifecycle.mdlines 695, 720 and 1094, andmcp-gateway-modes.mdline 76). It's now capped at five minutes by ToolHive's limit for marked tokens. Worth updating when #2533 closes.
A question outside this PR: ToolHive's consent page at AS-2. "ToolHive upgrade to v0.54 is blocked on consent handling" (#2438) planned to pass WithConsentExemptClientIDs for the broker's own confidential client, together with a check of the callback's state and PKCE handling. The upgrade then landed with the test helper that clicks Approve (#2472), and #2438 was closed without adopting the exemption. Under "Connect provider accounts at the embedded gateway" (#2537), sign-in and provider connections are one /authorize chain, so users will see ToolHive's consent page again on each return once remembered consent expires. Should that be part of #2533 or #2537, or does it need its own issue?
| // Issuer and SubjectClaim must be AS-1's (credentials-and-lifecycle.md, | ||
| // "Identifying claim"): AS-2 resolves an ID-JAG's upstream_subject only when | ||
| // its issuer and claim are these. | ||
| type AS2CompanyIdP struct { |
There was a problem hiding this comment.
The comment says Issuer and SubjectClaim must be AS-1's, but nothing checks that. Issuer, SubjectClaim and CAFile are free fields, and SubjectClaim defaults to sub. #2533 says they "come from AS-1's settings, so the two can't differ". If they do differ:
- If AS-1 uses
oidand AS-2 is left on the defaultsub, ToolHive answers every redemption withinvalid_grant/invalid_assertion("does not name this server's sign-in identity provider"). The broker reports that as an internal error, so every call fails and looks like a bad signature. - If both use
subon Entra, wheresubdiffers per app registration, every user getssubject_unknownforever, even after signing in at AS-2.
I assume the wiring PR builds the trust in internal/app from the hosted AS-1's config, using UpstreamIssuer, SubjectClaim, UpstreamCAFile and UpstreamAllowPrivateAddresses from IdentityBrokerAS1Config, so the operator supplies only AS-2's own client ID and secret. Can you confirm? The same goes for ActorIssuer: it should come from the trust domain through identitybroker.ActorIssuer, because a typo there shows up as actor_denied, which reads as a denial and not as a configuration fault. Until then, could the comment say that composition must fill these values from AS-1's settings?
There was a problem hiding this comment.
Confirmed. #3018 builds the trust in internal/app from the hosted AS-1's config: the IdP issuer, subject claim, CA and private-address setting come from AS-1's upstream settings, and ActorIssuer from the trust domain via identitybroker.ActorIssuer. The operator supplies only AS-2's own client ID and secret. TestHostedAS1ComposesAS2sTrust checks this with a non-default oid claim. In dd166a7 the doc here now says composition must fill these from AS-1, and describes what a mismatch looks like.
| // as2CompanyIdPRunConfig is ToolHive's configuration of the company IdP as | ||
| // AS-2's first upstream, at AS-2's issuer. It forces no prompt, so the step | ||
| // is silent while the user's session at the IdP is live. | ||
| func as2CompanyIdPRunConfig(idp AS2CompanyIdP, issuer string) authserver.UpstreamRunConfig { |
There was a problem hiding this comment.
This leaves Scopes unset, so on the runner path ToolHive v0.70.1 asks the company IdP for openid offline_access (runner/embeddedauthserver.go, the default for OIDC upstreams). The callback then stores the company access, refresh and ID tokens for every user who signs in at AS-2. AS-2 never uses them: resolution reads only the stored identity. Holding a company refresh token also goes against the design's "nothing downstream receives or exchanges a company token" (implementation.md line 26). And offline_access without prompt=consent makes some IdPs show a consent screen or drop the scope, which works against the silent sign-in.
Could you set Scopes: []string{"openid"} here? Could you also add a test on the authorize request the company IdP actually receives (the scope, and no prompt)? TestAS2PassesTheCompanyIdPConfigurationToToolHive only checks the run config. And the harness IdP issues a refresh token only when the request carries prompt=consent as well as offline_access, so the current tests can't notice a stored refresh token.
There was a problem hiding this comment.
Agreed on the design point: AS-2 never uses the company tokens. Before changing it, though, I found a side effect in ToolHive.
For a token with a tsid, i.e. an interactive AS-2 sign-in such as workspace enrollment, the token validator loads every upstream's stored tokens. If any of them is expired and can't be refreshed, it rejects the whole request with 401 (pkg/auth/token.go around 1881; upstreamtoken/service.go credentialFor → ErrNoRefreshToken). With scope=openid only, the company IdP's access token expires after about an hour with no refresh token. From then on, every interactive vMCP request on that session fails until the user signs in again. Delegated gateway tokens are unaffected.
So openid alone looks safe only if ToolHive treats the first (sign-in) upstream as identity-only, i.e. doesn't load or refresh its tokens per request, or better, doesn't store them at all. Is that something you'd take on the ToolHive side? If so, I'll switch to openid once it ships. If you'd rather accept hourly re-sign-in for enrollment routes, I can switch now.
Meanwhile, TestAS2AsksTheCompanyIdPForASilentSignIn (dd166a7) pins the authorization request the company IdP actually receives: no prompt or max_age, PKCE S256, AS-2's client and callback, and today's openid offline_access scope. A change to the scope will show up there.
There was a problem hiding this comment.
A correction to my approval note: please don't plan around a ToolHive "identity-only sign-in upstream" for now. ToolHive's outbound strategies (token exchange, OBO, AWS STS) take their subject token from the user's stored upstream tokens (identity.UpstreamTokens[SubjectProviderName]). An identity-only company leg would rule out a later OBO or token exchange with the company IdP as the subject provider, for example Entra OBO to Graph from a gateway call. Whether we want that option is still open on the design side. So offline_access stays on the company leg with no end date, and the operator guide's "the IdP must issue a refresh token" stands as the requirement, not as a temporary note. Nothing to change in this PR or in #3018.
| trust = config.AS1Trust | ||
| } | ||
| if trust != nil { | ||
| if err := trust.check(); err != nil { |
There was a problem hiding this comment.
trust.check() and the provider-name collision check run after discoverAnonymous (line 165). By then startup has already contacted every anonymous upstream over the network, before it refuses an incomplete trust. The AS1Trust doc and TestAS2RefusesAnIncompleteAS1Trust say the refusal comes before anything is built. Nothing persistent happens first, but the operator sees discovery calls, and possibly timeouts, before the real error. as2 depends only on config.BrokerClient and construction.upstreams, so the whole block can move to just after compileToolHiveConstruction.
There was a problem hiding this comment.
Done in dd166a7: the trust check and the provider-name collision check now run before discoverAnonymous, straight after compileToolHiveConstruction.
| // CAFile is a PEM bundle trusted for the IdP's certificate beside the | ||
| // system roots; empty trusts the system roots only. | ||
| CAFile string | ||
| // AllowPrivateAddresses lets AS-2 reach the IdP on a private or loopback |
There was a problem hiding this comment.
Small doc fix: for the company IdP, this setting doesn't govern loopback. ToolHive builds the IdP client with networking.NewHostScopedClientBuilder, which always allows a loopback host, including over plain HTTP. Your own test works around this with the IPv4-compatible address. Maybe: "lets AS-2 reach the IdP on a private address; ToolHive always allows a loopback issuer."
A related point on AS1Trust.JWKSURL: startup refuses only a literal private IP (ValidateJWKSURL). A DNS name that resolves to a private address, such as an in-cluster as1.ns.svc, passes startup and is refused only at the first key fetch. #2533 says "a private key-set address without the setting fails at startup", so it's worth saying which case fails when, in the doc or in the operator guide.
There was a problem hiding this comment.
Done in dd166a7. AS2CompanyIdP.AllowPrivateAddresses now says ToolHive always allows a loopback issuer. AS1Trust.AllowPrivateAddresses now says a literal private key-set address is refused at startup, and a host name that resolves to one is refused at the first key fetch. #3018's operator guide and flag help say the same.
| bindings[i] = tokenexchange.JWTBearerSubjectBinding{Subject: subject, AllowedResources: []string{resource}} | ||
| } | ||
| // as2MaxAssertionAge is the longest lifetime, exp minus iat, of an ID-JAG | ||
| // AS-2 redeems. ToolHive does not compare iat with the current time. |
There was a problem hiding this comment.
This comment and the one in as2_test.go around line 1369 say ToolHive doesn't compare iat with the current time. It does reject an iat in the future: validateJWTBearerAssertionClaims calls go-jose's ValidateWithLeeway with a 60-second skew. What it doesn't do is limit how old iat is, and this cap (exp minus iat), together with a current exp, covers that. Maybe: "ToolHive refuses an iat beyond its 60-second skew but does not bound how old it is; this cap, with exp current, does." A refusal-table row for an ID-JAG issued in the future would pin the behaviour.
There was a problem hiding this comment.
Right, thanks. Both comments are reworded in dd166a7: ToolHive refuses an iat beyond its 60-second skew but doesn't bound how old it is, and the lifetime cap with a current exp does that. I also added a refusal row for an ID-JAG issued two minutes in the future.
…docs Address review of #3015: - Check AS-2's trust in AS-1 and the company IdP name collision before anonymous discovery contacts any upstream. - Say that composition must fill the company IdP's issuer, claim and CA, and the actor issuer, from AS-1's settings, and what a mismatch looks like. - ToolHive always allows a loopback company IdP, and refuses a private key-set host at startup only when it is a literal address. - ToolHive refuses a future iat within its skew; the lifetime cap bounds how old an ID-JAG can be. A refusal row pins the future iat. - Pin the authorization request the company IdP receives: no prompt or max_age, PKCE, AS-2's client and callback, and today's scope. Co-authored-by: Mecatl <[email protected]>
|
Thanks for the review. Replies to the inline comments are on each thread; the fixes are in dd166a7. On the body:
|
jhrozek
left a comment
There was a problem hiding this comment.
Thanks, I checked dd166a7: the trust check now runs before discovery, the AS2CompanyIdP, AS1Trust, loopback, key-set and iat comments are corrected, and the new tests pin the future-iat refusal and the request the company IdP receives.
On offline_access, you're right. I followed it through ToolHive v0.70.1: GetAllUpstreamCredentials loads every stored upstream row for an interactive session, the company IdP's included, and the validator answers 401 if any refresh fails. So openid alone would turn into hourly 401s on enrollment sessions. Please keep offline_access for now. The real fix is on the ToolHive side: the sign-in upstream should be identity-only, with its tokens not stored, or at least not loaded and refreshed on each request. I'll follow up there, and switching to openid can come with that release. Until then, it's worth a line in #3018's operator guide: the company IdP must actually issue a refresh token for offline_access. Google needs access_type=offline instead, and Okta needs the refresh grant enabled on the app. Without one, interactive sessions hit the same hourly 401 even with today's scopes.
origin/main's tree equals #3015's final commit, already on this branch, so every conflict resolves to this branch's side. Co-authored-by: Mecatl <[email protected]>
Brings in #3014 (toolhive v0.70.1) and #3015 (AS-2 resolves AS-1's ID-JAG users through the company IdP). The gateway tests keep main's AS-2 sign-in and read the ID-JAG records from the audit file, since the in-memory recorder is gone on this branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Part of #2533. Stacked on #3014 (ToolHive v0.70.1); review that first.
The MCP broker's embedded gateway authorization server (AS-2) can now trust AS-1 the way the design's embedded mode describes. Production doesn't use it yet: composition still sets the trust only in tests. The next PR wires it from mecak8s's configuration, removes the startup refusal of gateway servers, and adds the Helm values and operator guide.
Changes
mcpbroker.AS1Trustis a production configuration type that replaces the test-only per-subject trust. With AS-2 set up, AS-2 now:resolve. It finds the gateway user from the ID-JAG'supstream_subjectthrough the company IdP, with no subject list and no approval record. A user who never signed in at AS-2 is refused withsubject_unknown, and no user is created.actor_denied. ToolHive also accepts AS-1's own issuer asact.iss, and anactwith noiss(which design check 9 also treats as the token'siss); AS-1 never stamps either.mcpbroker.TestSeam, kept out of production by an internal package and depguard: the vMCP's maximum call duration, and a sign-in client in AS-2's store.app.Config.MCPBrokerTestSeampasses it through composition.mcpbrokerteststarts AS-2 with a real AS-1 and the harness IdP. It signs users in at AS-2, through the company IdP and a fake provider, so tests in every package can redeem through resolution.Tests
TestHostedAS1IssuesTheGatewaysIDJAGsnow shows redemption refused withsubject_unknownbefore Alice signs in at AS-2, then succeeding for her resolved user.subject_unknown, no user created, ID-JAG not consumed);sub_id;upstream_subject;oid;resource_deniedcan't be reached with AS-2's configuration: the broker client may only request the vMCP resource, and no other client can redeem. A test comment explains why.resolve, the actor issuers, the company-IdP prepend, the subject-claim passthrough,check(), the name-collision check, or the "only with AS-2" gate;API compatibility
No wire change. Production AS-2 still trusts no issuer. The new configuration type and test seam are internal Go APIs.
Testing
go testpasses for./internal/adapter/mcpbroker/...,./internal/identitybroker/...,./internal/appand./cmd/mecak8s. The AS-2 tests also pass with-race.golangci-lintreports no issues across the repository.