Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,8 @@ type SPIFFETrustDomainConfig struct {
// Trust material comes from the declared bundle source (see
// SPIFFETrustDomainConfig's doc comment).
//
// GrantTypes is deliberately not exposed here: the runtime only accepts
// exactly the RFC 8693 token-exchange grant for a SPIFFE client
// (validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
// always supplies it instead of letting it be configured.
// The CRD does not expose grantTypes. The converter always supplies the
// RFC 8693 token-exchange grant instead of letting it be configured.
//
// +kubebuilder:validation:XValidation:rule="self.principalPattern.split('/').all(segment, segment != '.' && segment != '..')",message="principalPattern path must not contain . or .. segments"
// +kubebuilder:validation:XValidation:rule="has(self.resources) && size(self.resources) > 0",message="resources is required"
Expand Down Expand Up @@ -1423,7 +1421,7 @@ func (c *EmbeddedAuthServerConfig) ValidateInboundGrants() error {
if err := tokenexchange.ValidateTrustedIssuers(grants.trustedIssuers, c.Issuer, nil); err != nil {
return fmt.Errorf("trustedIssuers: %w", err)
}
if c.hasSPIFFEX509ClientAuth() {
if c.hasSPIFFEMethod(SPIFFEAuthenticationMethodX509) {
if _, err := authserver.ValidateSPIFFEX509Issuer(c.Issuer); err != nil {
return err
}
Expand Down Expand Up @@ -1471,22 +1469,20 @@ func (c *EmbeddedAuthServerConfig) ValidateTLSListener() error {
}
return nil
}
if c.hasSPIFFEX509ClientAuth() {
if c.hasSPIFFEMethod(SPIFFEAuthenticationMethodX509) {
return fmt.Errorf("tlsListener is required when SPIFFE X.509 client authentication is configured")
}
return nil
}

func (c *EmbeddedAuthServerConfig) hasSPIFFEX509ClientAuth() bool {
// hasSPIFFEMethod reports whether any SPIFFE client association allows method.
func (c *EmbeddedAuthServerConfig) hasSPIFFEMethod(method SPIFFEAuthenticationMethod) bool {
if c.InboundGrants == nil {
return false
}
for _, association := range c.InboundGrants.SPIFFEClientAuth {
if slices.Contains(association.Methods, SPIFFEAuthenticationMethodX509) {
return true
}
}
return false
return slices.ContainsFunc(c.InboundGrants.SPIFFEClientAuth, func(a SPIFFEClientConfig) bool {
return slices.Contains(a.Methods, method)
})
}

func hasLegacyTokenExchangePolicy(issuer TrustedIssuerConfig) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ func TestMCPExternalAuthConfig_validateEmbeddedAuthServer(t *testing.T) {
},
},
expectErr: true,
errMsg: "confidential clients (allow_confidential_client_registration or delegate clients) cannot be combined with insecure_allow_http",
errMsg: "confidential clients (allow_confidential_client_registration, delegate clients or JWT-SVID SPIFFE associations that grant client_credentials) cannot be combined with insecure_allow_http",
},
{
name: "confidential clients with plain-HTTP loopback issuer without opt-in - invalid",
Expand Down Expand Up @@ -2099,11 +2099,50 @@ func TestEmbeddedAuthServerConfig_ValidateInboundGrants_ConfidentialClientTransp
Scopes: []string{"openid"},
Audiences: []string{"https://api.example.com"},
}}
spiffeConfig := func(method SPIFFEAuthenticationMethod, mutate func(*EmbeddedAuthServerConfig)) EmbeddedAuthServerConfig {
config := EmbeddedAuthServerConfig{
Issuer: "http://localhost:8080",
SPIFFETrustDomains: []SPIFFETrustDomainConfig{{
Name: "example", TrustDomain: "example.org",
Methods: []SPIFFEAuthenticationMethod{method},
BundleSource: SPIFFEBundleSourceConfig{
Type: SPIFFEBundleSourceTypeFile,
File: &SPIFFEFileBundleSourceConfig{ConfigMapName: "spire-bundle", ConfigMapKey: "bundle.json"},
},
}},
InboundGrants: &InboundGrantsConfig{
SPIFFEClientAuth: []SPIFFEClientConfig{{
TrustDomainRef: "example", PrincipalPattern: "spiffe://example.org/ns/default/agent",
ClientID: "spiffe-client", Methods: []SPIFFEAuthenticationMethod{method},
Resources: []string{"https://mcp.example.com"}, Scopes: []string{"openid"},
}},
},
}
mutate(&config)
return config
}
tests := []struct {
name string
config EmbeddedAuthServerConfig
expectErr bool
}{
{
name: "SPIFFE JWT association is admitted with insecure HTTP",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(c *EmbeddedAuthServerConfig) {
c.InsecureAllowHTTP = true
}),
},
{
name: "SPIFFE JWT association is admitted on HTTP loopback without explicit opt in",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(*EmbeddedAuthServerConfig) {}),
},
{
name: "SPIFFE X.509-only association is unaffected by insecure HTTP",
config: spiffeConfig(SPIFFEAuthenticationMethodX509, func(c *EmbeddedAuthServerConfig) {
c.InsecureAllowHTTP = true
c.Issuer = "https://auth.example.com"
}),
},
{
name: "delegate clients remain independent of confidential DCR",
config: EmbeddedAuthServerConfig{
Expand Down
10 changes: 5 additions & 5 deletions cmd/thv-operator/pkg/controllerutil/authserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
authrunner "github.com/stacklok/toolhive/pkg/authserver/runner"
"github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange"
"github.com/stacklok/toolhive/pkg/authserver/storage"
"github.com/stacklok/toolhive/pkg/oauthproto"
"github.com/stacklok/toolhive/pkg/runner"
)

Expand Down Expand Up @@ -387,10 +388,9 @@ func buildSPIFFEBundleSourceRunConfig(
}

// buildSPIFFEClientAuthRunConfigs converts CRD SPIFFEClientConfig entries to
// authserver.SPIFFEClientAuthRunConfig. GrantTypes is not a CRD field: the
// runtime only accepts exactly the RFC 8693 token-exchange grant for a
// SPIFFE client (validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so
// it is always supplied here rather than configured.
// authserver.SPIFFEClientAuthRunConfig. The CRD does not expose grantTypes,
// so the RFC 8693 token-exchange grant is always supplied here rather than
// configured.
func buildSPIFFEClientAuthRunConfigs(
clients []mcpv1beta1.SPIFFEClientConfig,
) []authserver.SPIFFEClientAuthRunConfig {
Expand All @@ -410,7 +410,7 @@ func buildSPIFFEClientAuthRunConfigs(
Resources: append([]string(nil), spiffeClient.Resources...),
Audiences: deprecatedAudiences,
Scopes: append([]string(nil), spiffeClient.Scopes...),
GrantTypes: []string{authserver.SPIFFEGrantTypeTokenExchange},
GrantTypes: []string{oauthproto.GrantTypeTokenExchange},
}
}
return configs
Expand Down
3 changes: 2 additions & 1 deletion cmd/thv-operator/pkg/controllerutil/authserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
authrunner "github.com/stacklok/toolhive/pkg/authserver/runner"
"github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange"
"github.com/stacklok/toolhive/pkg/authserver/storage"
"github.com/stacklok/toolhive/pkg/oauthproto"
"github.com/stacklok/toolhive/pkg/runner"
)

Expand Down Expand Up @@ -3718,7 +3719,7 @@ func TestBuildSPIFFEClientAuthRunConfigs(t *testing.T) {
assert.Equal(t, []string{"openid"}, configs[0].Scopes)
// GrantTypes is not a CRD field; the converter always supplies exactly
// the RFC 8693 token-exchange grant.
assert.Equal(t, []string{authserver.SPIFFEGrantTypeTokenExchange}, configs[0].GrantTypes)
assert.Equal(t, []string{oauthproto.GrantTypeTokenExchange}, configs[0].GrantTypes)

// The runtime type must not retain the CRD object's backing slices.
audiences[0] = "https://mutated.example.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ var _ = Describe("MCPExternalAuthConfig SPIFFE CEL validation", func() {
config.InboundGrants.SPIFFEClientAuth[0].Resources = nil
},
},
{
name: "JWT-SVID association with insecureAllowHTTP is admitted", trustDomains: true, clients: true,
principal: "spiffe://example.org/ns/default/agent", shouldAdmit: true,
mutate: func(config *mcpv1beta1.EmbeddedAuthServerConfig) {
config.InsecureAllowHTTP = true
config.SPIFFETrustDomains[0].Methods = []mcpv1beta1.SPIFFEAuthenticationMethod{mcpv1beta1.SPIFFEAuthenticationMethodJWT}
config.InboundGrants.SPIFFEClientAuth[0].Methods = []mcpv1beta1.SPIFFEAuthenticationMethod{mcpv1beta1.SPIFFEAuthenticationMethodJWT}
},
},
{
name: "JWT-SVID association with an http loopback issuer is admitted without the loopback opt-in", trustDomains: true, clients: true,
principal: "spiffe://example.org/ns/default/agent", shouldAdmit: true,
mutate: func(config *mcpv1beta1.EmbeddedAuthServerConfig) {
config.Issuer = "http://localhost:8080"
config.SPIFFETrustDomains[0].Methods = []mcpv1beta1.SPIFFEAuthenticationMethod{mcpv1beta1.SPIFFEAuthenticationMethodJWT}
config.InboundGrants.SPIFFEClientAuth[0].Methods = []mcpv1beta1.SPIFFEAuthenticationMethod{mcpv1beta1.SPIFFEAuthenticationMethodJWT}
},
},
{
name: "X.509-only association with insecureAllowHTTP is unaffected", trustDomains: true, clients: true,
principal: "spiffe://example.org/ns/default/agent", shouldAdmit: true,
mutate: func(config *mcpv1beta1.EmbeddedAuthServerConfig) {
config.InsecureAllowHTTP = true
},
},
{
name: "empty-string deprecated audience is still rejected", trustDomains: true, clients: true,
principal: "spiffe://example.org/ns/default/agent", errMatch: "audiences",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down Expand Up @@ -3701,10 +3699,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down Expand Up @@ -5980,10 +5978,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down Expand Up @@ -3704,10 +3702,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down Expand Up @@ -5983,10 +5981,8 @@ spec:
Trust material comes from the declared bundle source (see
SPIFFETrustDomainConfig's doc comment).

GrantTypes is deliberately not exposed here: the runtime only accepts
exactly the RFC 8693 token-exchange grant for a SPIFFE client
(validateSPIFFEGrants in pkg/authserver/spiffe_trust.go), so the converter
always supplies it instead of letting it be configured.
The CRD does not expose grantTypes. The converter always supplies the
RFC 8693 token-exchange grant instead of letting it be configured.
properties:
audiences:
description: |-
Expand Down
Loading
Loading