Repository navigation
feat(authn): own sessions by the sign-in's sub_id identity - #2497
Conversation
Add an opt-in OIDC option, --oidc-owner-from-sub-id with --oidc-accepted-client-ids (Helm oidc.ownerFromSubID and oidc.acceptedClientIDs). With it on, a bearer must be an RFC 9068 at+jwt access token from an accepted client_id carrying an RFC 9493 iss_sub sub_id, and the session owner is that sub_id identity, so owners keyed by the company IdP keep matching without migration. Off by default for mecated and existing mecak8s deployments. mecak8s refuses to start the identity broker with OIDC on but the option off. A presented-and-rejected bearer now gets WWW-Authenticate: Bearer error="invalid_token"; a missing bearer keeps the bare Bearer challenge. Another user's session stays a 404, and the design drafts now say so. Closes #2487. Co-authored-by: Mecatl <[email protected]>
…tor comparable With --oidc-owner-from-sub-id on, refuse startup when --oidc-client-id (advertised in protected-resource metadata) is not in --oidc-accepted-client-ids; otherwise clients enroll and every request gets 401. The Helm chart refuses the same combination. Hold the accepted client IDs behind a pointer so oidc.Validator stays comparable, as it was in authn/oidc v0.3.0. Document that existing owners keep matching only when the upstream IdP returns the same issuer and sub for each user (not a pairwise subject per application), and that storage_management.principals and the execution allowed subjects must name the sub_id identity. Co-authored-by: Mecatl <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
Approving. These are follow-ups found during an automated review; none blocks merging, and they can be fixed in a later PR.
Two points have no line in this diff:
- The proto description of
Principalis now wrong.contracts/proto/mecatl/v1/harness.proto:1148-1150still saysissueris "the IdP that minted the token (the canonicalissclaim)" andsubjectis "thesubclaim". With--oidc-owner-from-sub-idon, both come fromsub_id. The PR fixed the same comment inengine/session/principal.gobut not here, and the proto is what API clients read (it is generated intouser-docs/reference/grpc-schema.mdand the TypeScript SDK). Copy theprincipal.gowording into the proto and runtask generate. - A few tests don't cover what they look like they cover.
- The
alg: noneand HS256 tests (and the harnessalg: nonecase) would still pass with this PR's code removed, because toolhive-core already rejects those algorithms. - No test uses a different case, such as
AT+JWT. - No test has a non-string
sub_id.suborsub_id.format. The code already handles both safely. - No test changes the caller's
AcceptedClientIDsslice afterNewValidatorto prove theslices.Clonematters.
- The
| if identity == nil { | ||
| return nil, subIDRejection("sub_id claim is missing or not a well-formed iss_sub identifier") | ||
| } | ||
| out.Issuer, out.Subject = identity.Issuer, identity.Subject |
There was a problem hiding this comment.
The execution allowlist no longer pins the issuer. ownerAllowed (cmd/mecak8s/flags.go:727-731) matches --execution-allowed-subjects against p.Subject only. Before this PR the issuer was always --oidc-issuer, so a subject named one person. With the option on, the issuer is whatever AS-1 writes into sub_id.iss, and this line accepts any non-empty, NUL-free value.
Example: AS-1 signs in users from both the company IdP and a partner IdP. A partner user whose sub is alice-204 matches the allowlist entry meant for the company's alice-204, and can list and bind execution templates. This goes against the Principal doc comment ("never Subject alone"). storage_management.principals compares issuer and subject together, so it is not affected.
It needs AS-1 to issue such a token, so it isn't exploitable today. Possible fixes: match the (issuer, subject) pair, refuse --execution-enabled while the option is on, or add the expected-issuer setting the PR description defers. user-docs/features/security-and-execution/caller-identity.md:153-154 should then stop presenting the subject-only match as enough.
Separately, this line drops AS-1's own iss, sub and client_id. The audit design (audit.md, credentials-and-lifecycle.md:211) expects mecak8s to record the AS-1 subject, so Validate will need to return it once request auditing lands. The description already defers this; a note in implementation.md would keep it from being lost.
There was a problem hiding this comment.
Thanks, both points are valid.
- Execution allowlist: agreed, with the option on the issuer is no longer pinned, so
--execution-allowed-subjectsmatching the subject alone is not enough. Not fixed in this PR yet: we are choosing between refusing--execution-enabledwhile--oidc-owner-identity=sub_idis on (nothing uses that combination today) and making the allowlist match issuer and subject. I'll follow up here once decided. - AS-1's own
iss,subandclient_id:implementation.mdnow records that mecak8s keeps only thesub_ididentity for now and that the audit work must also record AS-1's subject andclient_id(afc5565).
There was a problem hiding this comment.
Decided: the allowlist will match issuer and subject together, like storage_management.principals, tracked in #2521 (blocked by #2487, under the sign-in slice so it lands before the real AS-1 is hosted). Subject-only entries keep working while --oidc-owner-identity is off and are refused with it on.
|
|
||
| // validateOwnerFromSubID requires a non-empty, well-formed client list exactly | ||
| // when OwnerFromSubID is on, so neither half is a silent no-op. | ||
| func validateOwnerFromSubID(cfg Config) error { |
There was a problem hiding this comment.
validateOwnerFromSubID doesn't check AllowAnyAudience. A program using this published module could set AllowAnyAudience: true and OwnerFromSubID: true. It would then accept an AS-1 at+jwt minted for a different resource server and give that token the company user's identity. client_id doesn't help here, because it names the client that asked for the token, not the server it was meant for. RFC 9068 §4 says the token MUST be rejected if aud doesn't name the current resource server.
mecak8s and mecated never set AllowAnyAudience, so they aren't affected. Rejecting the combination here, with a constructor test, would close it for other users of the library.
There was a problem hiding this comment.
Fixed in 34c5b65: authn/oidc now refuses the owner option without a configured audience, and with AllowAnyAudience, with constructor tests for both.
| {{- if or (eq (trim $scope) "") (not (regexMatch "^[!-~]+$" $scope)) (contains "," $scope) (contains "\"" $scope) (contains "\\" $scope) -}}{{ fail (printf "oidc.scopes entry %q is invalid" $scope) }}{{- end -}} | ||
| {{- end -}} | ||
| {{- end -}} | ||
| {{- if .Values.oidc.ownerFromSubID -}} |
There was a problem hiding this comment.
The chart doesn't check the new startup rule in cmd/mecak8s/flags.go:670-674. Take a release with extraArgs: [--enable-identity-broker], oidc.enabled: true and ownerFromSubID: false. helm upgrade succeeds, then the new pod exits with --enable-identity-broker with --oidc-issuer requires --oidc-owner-from-sub-id and goes into CrashLoopBackOff, and the rollout stalls on the old pods.
validatePermissionMode already scans extraArgs for --posture and similar flags. The same scan for --enable-identity-broker (and --enable-identity-broker=...) would fail at render time instead. It needs a chart_test case, and a line in the ownerFromSubID comment in values.yaml.
There was a problem hiding this comment.
Fixed in b4f39de: the chart now fails at render time when extraArgs enables the identity broker (--enable-identity-broker, -enable-identity-broker, =true, =1) with OIDC on and no oidc.ownerIdentity=sub_id; =false, the option set, and OIDC off still render. Chart tests cover each case, and the values.yaml comment mentions the rule.
| fs.Func("oidc-accepted-client-ids", | ||
| "comma-separated OAuth client IDs whose access tokens --oidc-owner-from-sub-id accepts", | ||
| func(raw string) error { | ||
| c.AcceptedClientIDs = nil |
There was a problem hiding this comment.
Passing --oidc-accepted-client-ids more than once keeps only the last value, because this line resets the list on each call. --oidc-accepted-client-ids=tui --oidc-accepted-client-ids=studio accepts only studio, and TUI users get 401s. That's the safe direction to fail, but it is surprising. Either append here or say "last value wins" in the help text.
There was a problem hiding this comment.
I'd keep last-wins here: it's the standard Go flag behaviour and matches the other list flags (--oidc-scopes, --execution-allowed-subjects), so appending only for this flag would be the surprising one. Related gap fixed in 34c5b65, though: an empty value (--oidc-accepted-client-ids= or ,,) used to become an empty list silently and is now refused at startup, like --oidc-scopes=.
| in `--oidc-accepted-client-ids`, and its `sub_id` claim has format `iss_sub` | ||
| with a non-empty `iss` and `sub`. The owner is that `iss` and `sub`. When | ||
| `--oidc-client-id` is set, the server refuses to start unless that client ID is | ||
| also listed in `--oidc-accepted-client-ids`. |
There was a problem hiding this comment.
This paragraph names only one startup refusal. The server also refuses to start when:
--oidc-owner-from-sub-idis set without--oidc-accepted-client-ids(authn/oidc/oidc.go:185),--oidc-accepted-client-idsis set without the owner flag (oidc.go:180),- either flag is set without
--oidc-issuer(internal/cliconfig/oidc.go:306-307).
Suggested sentence: "The server refuses to start if either flag is set without the other, or without --oidc-issuer."
There was a problem hiding this comment.
Fixed in 34c5b65: the paragraph is now a list of every startup refusal (either flag without --oidc-issuer, each flag without the other, an advertised --oidc-client-id not in the accepted list, an empty client list, and the mecak8s broker rule). afc5565 added an unknown --oidc-owner-identity value to the list.
| **Failures.** Checks 1–7 return HTTP 401 with `WWW-Authenticate: Bearer error="invalid_token"` (RFC 6750 §3.1). The client refreshes its access token and retries once; a second failure is shown to the user. Check 8 returns 404 with the Mecatl API's `session_not_found` error body, the same response as for a session that does not exist, and is not retryable. A 403 would confirm that another user's session exists, so an ownership failure is deliberately indistinguishable from absence; for the same reason, deleting another user's session succeeds and deletes nothing. | ||
|
|
||
| **Why.** An `id_token` is not accepted here: its audience is the client, not the API. Session ownership is keyed by the company identity in `sub_id`, not by AS-1's own `(iss, sub)`, so existing owners keep matching without migration and the owner stays Alice's stable human identity. | ||
| **Why.** An `id_token` is not accepted here: its audience is the client, not the API. Session ownership is keyed by the company identity in `sub_id`, not by AS-1's own `(iss, sub)`, so existing owners keep matching without migration and the owner stays Alice's stable human identity. That holds only when the IdP gives the same subject across application registrations: an IdP with pairwise subjects per application (for example Microsoft Entra ID) gives AS-1 a different `sub`, so owners must be remapped. |
There was a problem hiding this comment.
Suggest "the same issuer and subject" rather than "the same subject". Owners match on both values byte for byte, so a differently written issuer also orphans every owner (for example an Entra v1 versus v2 issuer, or a trailing slash). caller-identity.md already says issuer and subject.
Related: implementation.md:13 says two AS-1 subjects never carry the same company identity. Only AS-1 can guarantee that, so it reads better as a requirement on AS-1 than as something mecak8s tests.
|
One more thought before this merges, about the setting's name. stacklok/toolhive#6908 added a second claim next to On Entra, keying owners on
Moving owners from mecak8s's own registration still needs a one-time remap, but only once. Switching would need three things:
That can be a follow-up. The part that can't wait: |
…empty client list Address review follow-ups on the sub_id owner option: - authn/oidc refuses OwnerFromSubID without a configured audience, or with AllowAnyAudience, as RFC 9068 section 4 requires. - --oidc-accepted-client-ids refuses a value with no client IDs, as --oidc-scopes already does. - The Principal proto comments name the sub_id source, matching engine/session; regenerated Go, TypeScript, and the gRPC reference. - Caller identity docs list every startup refusal; the design drafts say the same issuer and subject. - Tests cover a mixed-case typ, non-string sub_id members, and that the validator keeps its own copy of the accepted client IDs. Co-authored-by: Mecatl <[email protected]>
…ntity=sub_id Replace the unreleased boolean --oidc-owner-from-sub-id (Helm oidc.ownerFromSubID) with --oidc-owner-identity (Helm oidc.ownerIdentity), whose only accepted value is sub_id; empty keeps today's behaviour. A later owner identity, such as ToolHive's upstream_subject, can then be added as a new value without a breaking rename. Unknown values fail at startup and in the chart. Also note in the design drafts that the audit work must record AS-1's own subject and client_id, state that AS-1 must never issue one company identity to two subjects, and say that the alg tests pin toolhive-core's algorithm allowlist. Co-authored-by: Mecatl <[email protected]>
…Identity mecak8s refuses --enable-identity-broker with OIDC on unless --oidc-owner-identity=sub_id is set. The chart now fails at render time for that combination instead of rendering a pod that crash-loops. Also document that, with a pairwise-subject IdP, replacing the authorization server's application registration loses users' access to their sessions. Co-authored-by: Mecatl <[email protected]>
|
Thanks for the review. Fixes for the summary points:
On the naming comment: done in afc5565. The option is now a value flag, One point where I read the design differently: |
jhrozek
left a comment
There was a problem hiding this comment.
Re-approving at b4f39de.
Checked the six earlier inline comments against the new commits:
- Audience with the owner option: fixed in 34c5b65 (
AllowAnyAudienceand an empty audience are now refused). - Helm broker-in-
extraArgscrash loop: fixed in b4f39de (fails at render time). - Startup-refusal docs and "issuer and subject" wording: fixed.
- Repeated
--oidc-accepted-client-ids: last-wins kept, consistent with the other list flags. Fine by me. - Execution allowlist matching subject only: still open, tracked in #2521. Please make sure it lands before the real AS-1 is hosted.
The stale Principal comment in harness.proto is also updated.
Define how a delegated client such as Mecatl's broker uses a user's existing gateway connections: what AS-1 issues, how AS-2 decides which local user and whether the client may act for them, and what the gateway reads. The ToolHive side is tracked in stacklok/toolhive#6886 (issuing), AS-1 (credentials-and-lifecycle.md, steps 4 and 7) - AS-1 confirms the acting agent through ToolHive's pluggable actor-token validator (stacklok/toolhive#6899). Mecatl's implementation runs in the broker against its instance registry and returns the agent's ag_ identifier; AS-1 puts it in act.sub and caps the ID-JAG's exp at the SVID's. Why: AS-1 is ToolHive's server and cannot read Mecatl's registry, and the SVID carries only the claims JWT-SVID requires. - "Does not prove" now says the agent check is the broker checking its own registry. A built-in JWT-SVID validator with a session claim stays later work, for an AS-1 that runs apart from the broker. - The agent-instance issuer and the broker client each keep their own key, as "one key per role" already requires. Where SPIFFE client authentication is enabled, no client's SPIFFE ID may fall in the agent trust domain, or an SVID could pass as a client assertion. AS-2 (credentials-and-lifecycle.md step 8, mcp-gateway-modes.md) - Redemption maps the source user to a local user and checks approval: implicit in embedded mode; in external mode the local user is the one whose shared-login identity is the ID-JAG's upstream_subject, trusted only because the approval compared it with AS-2's own login (ID-JAG section 9.5). - An approval is one row per (gateway, local user, client, actor_iss), required actor_iss, no stored copy of AS-1's subject. Why: storing the source subject per approval allowed two rows to map one source user to different local users. - The actor rule: only the top-level act counts; an act without iss uses the token's iss; no act means no actor. - The embedded AS-2 accepts each ID-JAG from AS-1 once, and external gateways are recommended to do the same. It costs nothing because the broker never re-submits, and a stolen ID-JAG redeemed first by a holder of the broker key shows up as a failure of the broker's own redemption. ToolHive's general default (re-submission until exp for new issuers) is unchanged. Single-use leftovers in README, gateway modes, audit and the failure matrix are corrected. - V from an approved redemption carries the delegated-access marker (stacklok/toolhive#6404), is terminal as subject and actor token, and is the only thing that reaches the user-keyed read; a missing tsid or an act claim never stands in for it. - The vMCP binds a session to iss, sub, client_id, act.iss, act.sub and the marker, consistently in step 9 and "Sessions at the vMCP". Approval link (implementation.md "External consent link") - AS-2 refuses an unapproved ID-JAG with consent_required and an error_uri to a pending approval, under a profile AS-2 advertises in its metadata. Mecatl shows AS-2's own link; it adds no callback, state or PKCE, and resumes by redeeming a new ID-JAG. - On approval AS-2 compares the company IdP identity the user signed in with against the ID-JAG's, activates on a match and discards the pending approval on a mismatch. Why: this stops link swapping without a browser, so it also covers CLI and cross-device use, while a Mecatl callback needed a web session Mecatl does not have and did not stop a forwarded link anyway. - AS-2 offers user approval only for issuers that share its company IdP; otherwise plain invalid_grant and no link, leaving administrator pre-approval. The broker shows an error_uri only from a gateway that advertises the profile, on AS-2's https origin. Ledger and tests (implementation.md) - The ToolHive work list links the posted issues instead of "no issue covers it": #6887, #6404, #6407, #6885, #6888, #6892 and the #6886 sub-issues (#6893-#6900); #6894 and #6900 are marked done. - Tests cover the approval comparison, the marker, re-submission policy, the actor rule and the validator. - Removed: issue triage pinned to a commit, a costs paragraph that repeated the table, and duplicated explanations. Identifying claim (credentials-and-lifecycle.md "Identifying claim") - Two claims identify a user and answer different questions. Session ownership stays on sub_id, as merged in #2497: only AS-1's application registration is involved, so a per-application sub is enough. The gateway uses upstream_subject, configured per company IdP as ToolHive's subject claim at AS-1 and AS-2 (oid for Entra, sub where it is the same across applications), because AS-1 and AS-2 are different registrations. - On Entra, replacing AS-1's registration orphans sessions until owners are remapped; the section says so, and moving ownership to upstream_subject is listed as later work. threat-model.md and audit.md follow the same terms. Terminology: new text calls the stored record an "approval" (it can come from the user or an administrator). Existing lines that say "consent link" are left unchanged to keep this diff reviewable; a mechanical rename can follow separately. Co-Authored-By: mecatl <[email protected]>
Closes #2487.
With a new opt-in OIDC option, mecak8s accepts only AS-1 access tokens from the TUI or Studio and owns sessions by the company identity in the token's
sub_id, so existing owners keep matching without migration when the IdP gives each user the same subject as before.Design changes (for the design's author)
This PR changes the agent identity design drafts in two places. Please confirm both.
session_not_found, not 403not_session_owner. The design (credentials-and-lifecycle.mdstep (1) check 8 and the failure matrix;implementation.mdsession binding checks) and Session ownership from the AS-1 sign-in #2487's "Done when" said 403. This PR keeps the server's existing contract instead: another user's session looks exactly like a missing one, so a caller cannot tell which sessions exist, and switching to 403 would change an existing wire behaviour. The drafts are amended to say 404.subfor each user to AS-1 as it did to mecak8s. IdPs that issue a pairwise subject per registered application (for example Microsoft Entra ID) give AS-1, a new registration, a differentsub, so those deployments must remap owners first. The drafts now say so.What changes
New settings, off by default:
--oidc-owner-identityand--oidc-accepted-client-ids(Helmoidc.ownerIdentity,oidc.acceptedClientIDs).--oidc-owner-identityis a value flag whose only accepted value today issub_id(empty is off), so a later owner identity, such as ToolHive'supstream_subject, can be added without a breaking rename. They are registered through the shared OIDC flags, so mecated has them too.With the option on (
authn/oidc), after the existing signature, issuer, expiry, and audience checks, a token must:typat+jwt(orapplication/at+jwt), through toolhive-core'sAcceptedTokenTypes;algis already limited to RS/PS/ES by toolhive-core;client_idfrom the accepted list;sub_idwith formatiss_sub, built throughsession.PrincipalFromClaims, soissandsubmust be non-empty and NUL-free.Any failure is a 401
invalid_token. The owner principal'sIssuerandSubjectcome fromsub_id, soPrincipalScopeHashand every stored owner key stay the same for users whose IdP returns the same issuer and subject as before.Misconfiguration fails at startup (mecated and mecak8s, and the Helm chart): the option without client IDs, client IDs without the option, either without
--oidc-issuer, an advertised--oidc-client-idmissing from the accepted list (clients would enroll and then get 401 on every request), an empty--oidc-accepted-client-ids, an unknown--oidc-owner-identityvalue, or the option on the Kubernetes token validator. Theauthn/oidclibrary also refuses the option without a configured audience or withAllowAnyAudience(RFC 9068 §4). mecak8s also refuses--enable-identity-brokerwith--oidc-issuerunless the option is on.oidc.Validatorstays comparable (the accepted client IDs sit behind a pointer), so the publishedauthn/oidcAPI keeps its v0.3.0 shape; a compile-time test guards it.401 challenge: a presented bearer that is rejected now gets
WWW-Authenticate: Bearer error="invalid_token"(RFC 6750 §3.1); a request with no bearer keeps the bareBearer. This applies to every server and to static-token auth.Another user's session stays 404
session_not_found(a foreign delete stays 204 and deletes nothing), so a session's existence is not disclosed. This is a design change; see above.No change needed in
identityBrokerOwner: the broker owner is now the company identity, asidentitybroker.Ownerdocuments.Docs: caller identity (canonical owner rules, the same-subject precondition, and that
storage_management.principalsand the execution allowed subjects must name thesub_ididentity once the option is on; neither mismatch can be detected at startup), the mecak8s identity page, the HTTP/SSE reference (authentication errors), the CLI reference (generated), and a small correction indeployment-and-hardening.md.Not in this PR
sub_idmay name (an expected-issuer setting); worth adding only if AS-1 ever federates to more than one IdP.client_idare dropped once the owner is built; the audit work adds a separate verified-caller value.sub_idonly once Mecatl moves to a ToolHive release that includes Emit sub_id (RFC 9493) with the user's upstream identity in access tokens toolhive#6900 (merged, not yet released). Tests use the offline harness.Testing
authn/oidc: thetypmatrix (at+jwtandapplication/at+jwtpass;JWTand missing fail, and pass with the option off);algnoneand HS256 refused;client_idmissing, non-string, unlisted, and the second listed ID accepted; every malformedsub_id; the owner fromsub_idwith the option on and fromiss/subwith it off; configuration errors.internal/cliconfigandcmd/mecak8s: flag parsing, misconfiguration through the real constructor stopping startup, and the broker refusal matrix.internal/adapter/server: the challenge for a missing, a rejected, and a static-token bearer; gRPC staysUnauthenticated.cmd/mecak8s/identity_broker_harness_test.go):typJWT, notyp,algnone, anotherclient_id, nosub_id, or a malformedsub_idget 401 with theinvalid_tokenchallenge;failmessage.go test -raceoninternal/...,cmd/mecak8s,authn/oidc,engine/session, and the chart;task api:check;task docs:server-cli:check; the offline demo. Locally, some tests in packages this PR does not touch fail identically onmain(the local shell sandbox: git worktrees,bash, sockets).API compatibility
Assessment: compatible and additive; no major bump. No proto field, engine API (
task api:checkis clean), or stored-data change; thePrincipaldoc comments inengine/sessionandharness.proto(and its generated Go, TypeScript, and gRPC reference) now name thesub_idsource.mecatl.v1.Principal.issuer/subject(session owners and event actors) are thesub_ididentity, the company IdP's on an AS-1 deployment, rather than the token's own issuer. The owner key is unchanged.error="invalid_token"parameter for a rejected bearer. Status codes, bodies, and gRPCUnauthenticatedare unchanged, no Mecatl client or SDK parses the challenge, and RFC 6750 clients must accept the parameter.--enable-identity-brokerwith--oidc-issuernow requires--oidc-owner-identity=sub_id. The broker flag is experimental and not in the chart; a release that sets it throughextraArgswith OIDC on will stop starting until the option is set.Upgrade implications: to turn the option on, point
--oidc-issuerat AS-1, list the TUI and Studio client IDs (including any advertised--oidc-client-id), and make sure AS-1's upstream IdP returns the same issuer andsubfor each user as mecak8s saw before; with a pairwise-subject IdP, remap owners first. Otherwise existing data stays with the previous owner identities. Principal lists (storage_management.principals,--execution-allowed-subjects) must name thesub_ididentity. Sessions created in an experimental broker setup with AS-1 tokens before this option are owned by AS-1's identity and are not visible once it is on.