From f253185bae2cdfe7cfd1c863bbd28bb228f557d0 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sun, 15 Nov 2026 21:39:46 +0100 Subject: [PATCH] Skip SPIFFE wiring without associations A deployment with no SPIFFE associations still received a non-nil association registry, because the trust config is always built. The registry then installed a client resolver and an X.509 matcher, so a request carrying the SPIFFE JWT assertion type on a plain server took the SPIFFE arm and failed there instead of reaching the default client authentication path. NewSPIFFEAssociationRegistry now returns nil when the trust config has no associations, which leaves the resolver and the matcher unset. Associations is safe on a nil trust config, so a nil config takes the same path. The server comments that said these are nil only without a trust config are updated. Doc 17 lists the SPIFFE discovery entries. It also no longer says that omitting token_exchange from inbound_grants disables RFC 8693 entirely: token exchange stays enabled and advertised when a legacy delegate client or trusted-issuer policy is present, or when any SPIFFE association lists the token-exchange grant. Breaking: on a server with no SPIFFE associations, a request with the SPIFFE JWT assertion type now gets the default strategy's rejection instead of a SPIFFE-specific one. Part of #6204. Co-authored-by: Mecatl --- docs/arch/17-token-exchange-delegation.md | 23 +++++++++++++------ .../runner/embeddedauthserver_test.go | 2 +- pkg/authserver/server_impl.go | 20 ++++++++-------- pkg/authserver/spiffe_association_registry.go | 11 +++++---- .../spiffe_association_registry_test.go | 16 +++++++++++++ 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/docs/arch/17-token-exchange-delegation.md b/docs/arch/17-token-exchange-delegation.md index f0822f6f53..f33ee1d072 100644 --- a/docs/arch/17-token-exchange-delegation.md +++ b/docs/arch/17-token-exchange-delegation.md @@ -281,19 +281,28 @@ reuse IDs between the two mechanisms. Both `/.well-known/oauth-authorization-server` and `/.well-known/openid-configuration` advertise the token-exchange grant in -`grant_types_supported` by default. Setting `inbound_grants` with -`token_exchange` omitted disables and stops advertising RFC 8693 entirely -(`Config.DisableTokenExchange`, `AuthorizationServerConfig.TokenExchangeEnabled`) -— the same flag governs both registration with fosite and discovery -advertisement, so they cannot drift out of sync. `token_endpoint_auth_methods_supported` always +`grant_types_supported` when token exchange is enabled. With no `inbound_grants` +block it is enabled by default. Once `inbound_grants` is set, omitting its +`token_exchange` block does not by itself turn RFC 8693 off: it stays enabled, +and advertised, when a legacy delegate client or legacy trusted-issuer RFC 8693 +policy is present, or when any SPIFFE association lists the token-exchange +grant. It is disabled, and no longer advertised, only when none of these is +present (see the enablement rules in the token-only section above). +`Config.DisableTokenExchange` and `AuthorizationServerConfig.TokenExchangeEnabled` +carry the result. The same flag governs both registration with fosite and +discovery advertisement, so they cannot drift out of sync. `token_endpoint_auth_methods_supported` always includes `none`. It also includes `client_secret_basic` and `client_secret_post` when confidential DCR is enabled or a configured delegate client authenticates with a secret, so a deployment whose delegate clients all use `private_key_jwt` does not advertise them. It includes `private_key_jwt`, together with `token_endpoint_auth_signing_alg_values_supported` (the implemented algorithms), when `allowPrivateKeyJWTRegistration` is enabled or a configured delegate client -uses `private_key_jwt`. The RFC 7523 §2.1 JWT-bearer grant is advertised only -when a trusted issuer opts into it. A private-key JWT DCR client is limited to +uses `private_key_jwt`. It lists `spiffe_x509` and `spiffe_jwt` when some SPIFFE +association enables that method, and `grant_types_supported` lists +`client_credentials` when some association grants it (see +[SPIFFE association declarations](18-spiffe-association-declarations.md)). The +RFC 7523 §2.1 JWT-bearer grant is advertised only when a trusted issuer opts +into it. A private-key JWT DCR client is limited to token exchange; it is not an authorization-code client. On Kubernetes, a delegate-client Secret is injected as a pod environment diff --git a/pkg/authserver/runner/embeddedauthserver_test.go b/pkg/authserver/runner/embeddedauthserver_test.go index f266e16c92..4657198ced 100644 --- a/pkg/authserver/runner/embeddedauthserver_test.go +++ b/pkg/authserver/runner/embeddedauthserver_test.go @@ -2861,7 +2861,7 @@ func TestEmbeddedAuthServer_SPIFFESerializedRestartPolicy(t *testing.T) { assert.Empty(t, trust.Associations()) registry, err := authserver.NewSPIFFEAssociationRegistry(trust) require.NoError(t, err) - require.NotNil(t, registry) + assert.Nil(t, registry) // Every restart uses fresh memory. The initial and changed shapes // name listener certificate files that do not exist, so only the // SPIFFE-removed configuration can build a full server here. diff --git a/pkg/authserver/server_impl.go b/pkg/authserver/server_impl.go index 5dabc97535..a117fe91f4 100644 --- a/pkg/authserver/server_impl.go +++ b/pkg/authserver/server_impl.go @@ -571,11 +571,10 @@ func warnBlanketSelfIssuedExchange(delegateClients []DelegateClient, jwtBearerEn // decorateStorageForSPIFFE resolves the immutable association registry from the // validated SPIFFE trust model and installs the static overlay outside CIMD. -// A nil cfg.SPIFFETrust means no SPIFFE associations are configured, which -// yields a nil registry and leaves the storage chain unchanged. The returned +// The registry is nil when no SPIFFE trust is configured or the trust config has +// no associations, and the storage chain is then left unchanged. The returned // registry is also wired onto the fosite client-authentication strategy by the -// caller (see newSPIFFEClientResolver); it is nil when no SPIFFE trust is -// configured. +// caller (see newSPIFFEClientResolver). func decorateStorageForSPIFFE( ctx context.Context, cfg Config, stor storage.Storage, ) (storage.Storage, *SPIFFEAssociationRegistry, error) { @@ -603,9 +602,10 @@ func decorateStorageForSPIFFE( // X.509-SVID and a JWT-SVID for the same association cannot reach different // authorization outcomes. // -// It returns a nil resolver when no SPIFFE trust is configured. Returning a -// non-nil func closing over a nil registry would make the strategy's -// resolver != nil check pass when nothing is actually configured. +// It returns a nil resolver when no SPIFFE trust or no SPIFFE association is +// configured. Returning a non-nil func closing over a nil registry would make +// the strategy's resolver != nil check pass when nothing is actually +// configured. // // Method uses the shared leaf-package type because package server cannot // import authserver (authserver imports server). Package authserver aliases @@ -627,9 +627,9 @@ func newSPIFFEClientResolver(registry *SPIFFEAssociationRegistry) oauthserver.SP } // newSPIFFEX509ClientMatcher returns the matcher the client-authentication -// strategy uses to select the X.509 arm, or nil when no SPIFFE trust is -// configured, like newSPIFFEClientResolver. A method value on a nil registry -// would be non-nil. +// strategy uses to select the X.509 arm, or nil when no SPIFFE trust or no +// SPIFFE association is configured, like newSPIFFEClientResolver. A method +// value on a nil registry would be non-nil. func newSPIFFEX509ClientMatcher(registry *SPIFFEAssociationRegistry) oauthserver.SPIFFEX509ClientMatcher { if registry == nil { return nil diff --git a/pkg/authserver/spiffe_association_registry.go b/pkg/authserver/spiffe_association_registry.go index 584b0a2703..74e39cf6a7 100644 --- a/pkg/authserver/spiffe_association_registry.go +++ b/pkg/authserver/spiffe_association_registry.go @@ -27,8 +27,11 @@ type SPIFFEAssociationRegistry struct { } // NewSPIFFEAssociationRegistry creates an immutable lookup registry from a -// trust configuration. A nil trust configuration represents an absent SPIFFE -// configuration and returns nil without enabling SPIFFE clients. +// trust configuration. A nil trust configuration, or one with no associations, +// represents an absent SPIFFE configuration and returns nil without enabling +// SPIFFE clients: callers rely on a nil registry to leave the resolver and the +// X.509 matcher unset, so a server without associations keeps the default +// client authentication path. // // It rejects any two associations whose patterns could both match the same // concrete SPIFFE ID (including an exact duplicate, which is the identical @@ -36,11 +39,11 @@ type SPIFFEAssociationRegistry struct { // patterns by an unordered map scan, so an unrejected overlap would make // resolution depend on random iteration order. func NewSPIFFEAssociationRegistry(trust *SPIFFETrustConfig) (*SPIFFEAssociationRegistry, error) { - if trust == nil { + associations := trust.Associations() + if len(associations) == 0 { return nil, nil } - associations := trust.Associations() registry := &SPIFFEAssociationRegistry{ byPattern: make(map[string]SPIFFEClientAuthConfig, len(associations)), byClientID: make(map[string]SPIFFEClientAuthConfig, len(associations)), diff --git a/pkg/authserver/spiffe_association_registry_test.go b/pkg/authserver/spiffe_association_registry_test.go index 7ec6bec043..d7913b5c91 100644 --- a/pkg/authserver/spiffe_association_registry_test.go +++ b/pkg/authserver/spiffe_association_registry_test.go @@ -500,3 +500,19 @@ func testSPIFFEAssociation(clientID, scope string) SPIFFEClientAuthRunConfig { GrantTypes: []string{oauthproto.GrantTypeTokenExchange}, } } + +func TestNewSPIFFEAssociationRegistryReturnsNilWithoutAssociations(t *testing.T) { + t.Parallel() + + empty, err := NewSPIFFETrustConfig(nil, nil, []string{"openid"}, nil) + require.NoError(t, err) + require.Empty(t, empty.Associations()) + + for _, trust := range []*SPIFFETrustConfig{nil, empty} { + registry, err := NewSPIFFEAssociationRegistry(trust) + require.NoError(t, err) + assert.Nil(t, registry) + assert.Nil(t, newSPIFFEClientResolver(registry)) + assert.Nil(t, newSPIFFEX509ClientMatcher(registry)) + } +}