Skip to content

Expose SPIFFE grant types in the operator CRD - #7175

Merged
jhrozek merged 1 commit into
mainfrom
spiffe-v2-10-discovery
Nov 18, 2026
Merged

jhrozek merged 1 commit into
mainfrom
spiffe-v2-10-discovery

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The auth server already accepts client_credentials on a SPIFFE association, but the operator hid the grant list and always configured token exchange, so no MCPExternalAuthConfig or VirtualMCPServer could use the new grant.

  • Add a grantTypes field to spiffeClientAuth entries: client_credentials and/or the token-exchange URN, one or two items, a set. It defaults to the token-exchange grant, which is what the operator configured before the field existed. The API server applies the default when it reads objects, including ones already stored, so existing associations keep working with no migration and client_credentials stays opt-in.
  • The converter copies grantTypes into the RunConfig and falls back to token exchange when the list is empty. The CRD default normally fills it in, but when the operator chart is upgraded without the CRD chart, the old schema prunes the field and every stored association would otherwise fail to reconcile.
  • The insecureAllowHTTP and HTTP-issuer CEL rules, and the matching Go validation, now treat an association with spiffe_jwt in methods and client_credentials in grantTypes as a confidential client, the same condition the runtime already applies.
  • Doc 18 describes the field, its default and the upgrade note below.

Part of #6204 (Integrate SPIFFE principals with OAuth grants and discovery).

Type of change

  • New feature

Test plan

  • Unit tests: go test on cmd/thv-operator/api/..., cmd/thv-operator/controllers/..., cmd/thv-operator/pkg/controllerutil/... and pkg/authserver/...
  • envtest integration suite cmd/thv-operator/test-integration/mcp-external-auth (Kubernetes 1.31 assets), including a new case that an association created without grantTypes reads back with the token-exchange default
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)

API Compatibility

  • This PR does not break the v1beta1 API, OR the api-break-allowed label is applied and the migration guidance is described above.

grantTypes is a new optional field with a default that matches the previous behavior. The two HTTP-transport rules only match associations that list client_credentials in the new field, so no existing object is affected.

Changes

File Change
cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types.go GrantTypes field with default, cleartext CEL rules and their Go mirror
cmd/thv-operator/pkg/controllerutil/authserver.go Copy grantTypes into the RunConfig instead of injecting token exchange
docs/arch/18-spiffe-association-declarations.md Field, default, upgrade note
CRD YAMLs, deepcopy, docs/operator/crd-api.md Regenerated

Does this introduce a user-facing change?

Yes.

  • Before: SPIFFE associations always got token exchange. After: spiffeClientAuth[].grantTypes selects client_credentials, token exchange, or both; omitting it keeps token exchange.
  • A JWT-SVID association that grants client_credentials cannot be combined with insecureAllowHTTP, or with an http:// issuer without insecureAllowConfidentialOverLoopbackHTTP; such a CR is rejected at admission.
  • Upgrade note for the release notes: install the new CRDs and the new operator before adding client_credentials to any association. An operator from before this change ignores grantTypes and always configures token exchange, and the operator-crds and operator charts are separate releases, so nothing enforces this order.

Special notes for reviewers

  • The converter fallback deliberately duplicates the CRD default rather than leaving the default to the CRD alone, so stored associations keep reconciling when the CRDs lag behind the operator. On an old CRD, client_credentials cannot be enabled until the CRDs are upgraded, because the old schema prunes grantTypes.
  • JWT-SVID associations that only grant token exchange are deliberately not treated as confidential clients for the HTTP rules, to keep existing deployments working. Doc 18 says plainly that token exchange over plain HTTP is still exposed.
  • The nil-registry change and the doc 17 token-exchange paragraph that were here moved to Skip SPIFFE wiring without associations #7177.

@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 18, 2026 10:55
@github-actions github-actions Bot added the size/XL Extra large PR: 1000+ lines changed label Nov 18, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not ready to approve: the new required grantTypes field breaks existing SPIFFE MCPExternalAuthConfig resources on upgrade, and a default avoids that.

Design: Exposing the field at the CRD is the right layer. buildSPIFFEClientAuthRunConfigs is the only converter and both CRDs share it, and the Go and CEL transport rules match the runtime rule. The weak point is the upgrade story. SPIFFEClientConfig has shipped since v0.47.0 with no grantTypes (v0.66.0 explicitly says it is not exposed), and the operator always supplied token exchange.

  1. cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types.go (the SPIFFEClientConfig CEL presence rule) and validateSPIFFEGrantsAndResourcesPresent: every existing SPIFFE association stored without grantTypes becomes invalid after the CRD and operator upgrade. ValidateInboundGrants returns "grantTypes is required", the MCPExternalAuthConfig goes Valid=false, and the servers consuming it stop reconciling until someone edits it. On clusters before Kubernetes 1.30 (no CRD validation ratcheting) even operator writes such as finalizer updates on old objects are rejected. Suggested fix: add +kubebuilder:default={"urn:ietf:params:oauth:grant-type:token-exchange"}. Defaults apply when objects are read back from etcd, so old objects keep working with no migration, client_credentials stays opt-in, and you can drop the CEL presence rule, the Go presence check and the ratcheting caveat. If you keep the required design, tick the API Compatibility box and make the break explicit (label, release notes) rather than only mentioning it in the body.
  2. Operator/CRD version skew: an old operator with the new CRD ignores grantTypes and configures token exchange, so a CR that declares only client_credentials gets a grant it did not ask for. The operator-crds and operator charts are separate Helm releases, so the upgrade order in docs/arch/18 is not enforced. This cannot be fixed on the old operator; put the ordering in the chart or release notes, not only in the architecture doc.
  3. Mixed scope: the nil-registry change in NewSPIFFEAssociationRegistry (with its comments and test) and the rewrite of the token-exchange paragraph in docs/arch/17 are separate from exposing the CRD field. They are a distinct behaviour change; per the one-logical-change rule, please move them to their own PR.

Simpler alternative: with the CRD default from finding 1, what remains is the GrantTypes field with its enum and MinItems, the converter copy, and the two transport CEL rules with their Go mirror.

🤖 Reviewed with Claude Code

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.55%. Comparing base (2cd24f4) to head (4c51934).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7175      +/-   ##
==========================================
+ Coverage   81.49%   81.55%   +0.05%     
==========================================
  Files         894      894              
  Lines       91153    91164      +11     
==========================================
+ Hits        74284    74346      +62     
+ Misses      16864    16813      -51     
  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 mentioned this pull request Nov 18, 2026
2 of 5 tasks
@jhrozek
jhrozek force-pushed the spiffe-v2-10-discovery branch from 94de838 to 9c08651 Compare November 18, 2026 11:16
@jhrozek

jhrozek commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, agreed on all three. grantTypes now defaults to token exchange, so the CEL presence rule, the Go presence check and the grantTypes ratcheting text are gone. The envtest now checks that an association without the field reads back with token exchange. The version skew note is in the PR body's user-facing section so it lands in the release notes, and doc 18 keeps a one-line version of it. The nil-registry change and the doc 17 paragraph moved to #7177.

@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Nov 18, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Re-review of 9c08651: the CRD default is the right fix for the earlier findings, with one gap left in the converter.

  1. Required grantTypes breaks stored associations on upgrade: resolved. It is now optional with a CRD default of the token-exchange grant, applied on read for stored objects, in all four CRD copies (including the inline VirtualMCPServer path), with an envtest case.
  2. Old operator with new CRD: resolved in the sense that matters. An old operator behaves as before for every CR that omits the field.
  3. Mixed scope (pkg/authserver change and doc 17): resolved, both are gone from the PR.

New finding:

  1. cmd/thv-operator/pkg/controllerutil/authserver.go:410: the converter copies GrantTypes with no fallback, so a nil or empty list reaches validateSPIFFEGrantTypes (pkg/authserver/spiffe_trust.go:735) and is rejected. Scenario: the thv-operator chart is upgraded but the separate CRD chart is not (Helm does not upgrade CRDs). The old schema does not default or admit grantTypes, so every stored SPIFFE association now fails at reconcile, where before this PR the converter hard-coded token exchange. The same nil reaches the converter from any caller that builds the object without the API server. The tests at authserver_test.go:3753 and :3827 set GrantTypes explicitly, so nothing covers nil. Suggested fix: in buildSPIFFEClientAuthRunConfigs, use []string{oauthproto.GrantTypeTokenExchange} when len(spiffeClient.GrantTypes) == 0, and add a converter test with nil GrantTypes. If you would rather keep the converter default-free and rely on "install CRDs first", say so and state that operators on the old CRD hard-fail; that is a deliberate trade-off I would want recorded in the PR body.

🤖 Reviewed with Claude Code

The runtime now accepts client_credentials on a SPIFFE association,
but the operator hid the grant list and always injected token
exchange, so no custom resource could use the new grant.

Add a grantTypes field to spiffeClientAuth entries, limited to
client_credentials and the token-exchange URN, one or two items, a set.
It defaults to the token-exchange grant, which is what the operator
configured before the field existed. The API server applies CRD
defaults when it reads an object, including objects already stored,
so existing associations keep their behavior with no migration and
client_credentials stays opt-in. The converter copies grantTypes into
the RunConfig and falls back to the same token-exchange grant when the
list is empty, which happens when the operator chart is upgraded
without the CRD chart and the old schema neither defaults nor keeps
the field. Failing the reconcile there would break every existing
SPIFFE association that the previous operator served.

Now that a custom resource can list client_credentials, the CRD rules
for insecureAllowHTTP and for http issuers, and ValidateInboundGrants,
apply the same cleartext-transport condition as the runtime: an
association counts as a confidential client only when it has
spiffe_jwt in methods and client_credentials in grantTypes. Token-
exchange-only JWT-SVID and X.509 associations are not affected. The
rules guard the field with has(a.grantTypes). The field comments for
both HTTP opt-ins name the SPIFFE case, and the loopback opt-in now
says the combination is rejected at admission, with reconcile adding
the loopback host check.

Doc 18 describes the field and its default, and says to install the
new CRDs and operator before adding client_credentials: an operator
from before this change ignores grantTypes and always configures token
exchange. The CRD YAMLs, deepcopy and crd-api.md are regenerated.

Part of #6204.

Co-authored-by: Mecatl <noreply@mecatl.dev>
@jhrozek
jhrozek force-pushed the spiffe-v2-10-discovery branch from 9c08651 to 4c51934 Compare November 18, 2026 11:55
@jhrozek

jhrozek commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Good catch, fixed. The converter now falls back to token exchange when grantTypes is empty, so an operator upgraded without the CRD chart keeps serving existing associations as before instead of failing reconcile. The converter test now covers nil and empty, doc 18 says the same, and the PR body records the trade-off: the converter deliberately duplicates the CRD default, and client_credentials needs the new CRDs because the old schema prunes the field.

@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Nov 18, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Re-review of 4c51934: still right, approving. All earlier findings are resolved.

  1. Required grantTypes breaking stored associations: resolved. The field is optional with a CRD default of token exchange.
  2. Old operator with new CRD: resolved.
  3. Mixed scope: resolved. The unrelated change moved to #7177.
  4. No converter fallback when the CRD chart lags the operator chart: resolved. buildSPIFFEClientAuthRunConfigs now uses spiffeGrantTypesOrDefault, which returns token exchange for a nil or empty list and otherwise returns a copy. Nil and empty cases are tested.

Design: the CRD default stays the primary mechanism. The converter fallback only covers an old CRD schema with a new operator, or an object built without the API server, and it preserves what the operator served before the field existed. An explicit empty list is still refused at admission (MinItems=1) and in RunConfig validation, so the fallback does not weaken those checks.

🤖 Reviewed with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Extra large PR: 1000+ lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants