Skip to content

Send KSA service account token only to the in-cluster API server - #7248

Merged
rdimitrov merged 2 commits into
mainfrom
rdimitrov/ksa-token-in-cluster-only
Nov 23, 2026
Merged

rdimitrov merged 2 commits into
mainfrom
rdimitrov/ksa-token-in-cluster-only

Conversation

@rdimitrov

@rdimitrov rdimitrov commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

For an MCPOIDCConfig of type kubernetesServiceAccount, useClusterAuth defaults to true. The workload's own service account token was then attached to every OIDC discovery, JWKS and introspection request, whatever host the issuer, jwksUrl or introspectionUrl named. The token was added so a workload could fetch JWKS from the Kubernetes API server, which needs authentication (#978). But the token carries the workload's Role: StatefulSet management for the MCPServer proxy runner, and Secret reads for vMCP in discovered mode. So it should not go to any other host, such as a custom or external issuer.

  • With useClusterAuth unset, the resolver attaches the token only when the URL the validator actually fetches is an https URL whose host is exactly kubernetes.default.svc or kubernetes.default.svc.cluster.local. That URL is jwksUrl when set, since the validator then skips discovery and never contacts the issuer; otherwise it is the issuer used for discovery. A jwks_uri advertised by the in-cluster API server's discovery document is trusted.
  • introspectionUrl is not part of the check. A kubernetesServiceAccount config has no client credentials, so the validator's introspection client never carries the token.
  • The CA bundle and the private-IP allowance are unchanged, so in-cluster discovery and JWKS keep working, including kind's node-IP jwks_uri.
  • An explicit useClusterAuth: true sends the token unconditionally. This is the opt-in for an issuer that requires it but sits behind other names, such as a custom cluster domain. false sends nothing, as before.
  • The useClusterAuth field docs and docs/arch/09-operator-architecture.md describe the rules.

Type of change

  • Bug fix

Test plan

  • Unit tests: new table cases in resolver_configref_test.go for in-cluster issuer and JWKS (token sent); in-cluster jwksUrl with an external issuer (token sent); external issuer, external jwksUrl, a lookalike kubernetes.default.svc.example.com host and a plain-http issuer (token withheld); external introspectionUrl (ignored, token still sent); explicit true with an external issuer (token sent). The operator pkg/... and controllers packages pass.
  • Integration tests: envtest suites test-integration/mcp-oidc-config (35/35) and test-integration/mcp-server (103/103).
  • Linting (go run github.com/golangci/golangci-lint/v2/cmd/[email protected] run on the touched packages: 0 issues).

API Compatibility

  • This PR does not break the v1beta1 API. Only the useClusterAuth field docs change; no schema change.

Does this introduce a user-facing change?

Yes.

  • Before: with useClusterAuth unset, the workload's service account token was sent to whatever discovery and JWKS hosts the config named.
  • After: with useClusterAuth unset, the token is sent only when the fetched URL (jwksUrl, or the issuer if jwksUrl is unset) is on the in-cluster API server (https://kubernetes.default.svc[.cluster.local]).

Upgrade impact:

  • Default in-cluster issuers, and an external issuer with an in-cluster jwksUrl: no change.
  • External issuers whose discovery and JWKS are public, such as EKS or GKE OIDC endpoints: no change. They don't need the token and no longer receive it.
  • An issuer that requires the token but is not reachable under those two names (a custom cluster domain, or an API server reached by IP or another DNS name): discovery or JWKS now fails with 401/403 until useClusterAuth: true is set explicitly.

Special notes for reviewers

  • Exact host match on purpose. A prefix match on kubernetes.default.svc. would also accept kubernetes.default.svc.example.com, which cluster DNS forwards upstream. The resolver cannot see the cluster domain, so custom domains use the explicit opt-in.
  • inline configs are unchanged. Their jwksAuthTokenPath is a path the author sets explicitly.
  • Redirects and discovered jwks_uri. The gate checks the configured URL only. A cross-host redirect, or a jwks_uri advertised by discovery, would still carry the token. Both require the in-cluster API server to point elsewhere, so that's not a practical risk. Setting networking.SameHostRedirectPolicy() on the token-bearing client would close the redirect case and is left for a separate PR.
  • Interaction with Pass the JWKS auth token path to vMCP #7221: that PR passes JWKSAuthTokenPath through to vMCP and notes this trade-off as a follow-up. The two PRs touch different files and don't conflict.

🤖 Generated with Claude Code

For kubernetesServiceAccount MCPOIDCConfigs, useClusterAuth defaults to
true and attached the workload's own service account token to every
OIDC discovery, JWKS and introspection request, whatever host the
issuer, jwksUrl or introspectionUrl named. The token was added so
workloads could fetch the API server's JWKS, but it carries the
workload's Role (statefulset management for the proxy runner, secret
reads for vMCP), so it should not reach any other host.

With useClusterAuth unset, attach the token only when the issuer and any
explicit JWKS or introspection URL are https URLs on the exact in-cluster
API server names. The CA bundle and private-IP allowance are unchanged.
An explicit useClusterAuth: true keeps sending the token everywhere, for
issuers behind a custom cluster domain, and false still sends nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label Nov 23, 2026
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.66%. Comparing base (46d464d) to head (6b63526).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7248      +/-   ##
==========================================
- Coverage   81.69%   81.66%   -0.03%     
==========================================
  Files         903      904       +1     
  Lines       91789    91838      +49     
==========================================
+ Hits        74985    74998      +13     
- Misses      16799    16835      +36     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change overall, the exact host match and the comment explaining why are good. Two things inline: the gate checks a couple of URLs that never actually get the token, which breaks an external-issuer + in-cluster-jwksUrl setup, and the field doc wording is a bit stronger than what the code guarantees.

Comment thread cmd/thv-operator/pkg/oidc/resolver.go Resolved
When jwksUrl is set the validator skips discovery and never contacts the
issuer, so requiring an in-cluster issuer as well withheld the token
from an in-cluster JWKS endpoint configured with an external issuer.
Introspection never carries the token for these configs, because they
have no client credentials. Check jwksUrl when set, otherwise the issuer,
and describe the guarantee as what the code checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Nov 23, 2026

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both points are addressed in 6b63526. LGTM.

@rdimitrov
rdimitrov merged commit 710ce6d into main Nov 23, 2026
53 checks passed
@rdimitrov
rdimitrov deleted the rdimitrov/ksa-token-in-cluster-only branch November 23, 2026 15:38
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants