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
71 changes: 56 additions & 15 deletions cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stacklok/toolhive/pkg/authserver"
"github.com/stacklok/toolhive/pkg/authserver/oauthparams"
"github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange"
"github.com/stacklok/toolhive/pkg/oauthproto"
)

// External auth configuration types
Expand Down Expand Up @@ -742,9 +743,6 @@ type SPIFFETrustDomainConfig struct {
// Trust material comes from the declared bundle source (see
// SPIFFETrustDomainConfig's doc comment).
//
// 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 @@ -787,8 +785,9 @@ type SPIFFEClientConfig struct {
// allowlist membership is validated at reconcile time, not admission.
// Shape (a well-formed absolute HTTP(S) URI) is independent of that
// derived allowlist and is validated here. Required, with at least one
// entry, for every association: token exchange takes exactly one resource
// per request and grants it as the token audience.
// entry, for every association: client_credentials and token exchange each
// take exactly one resource per request and grant it as the token
// audience.
// +optional
// +kubebuilder:validation:MaxItems=50
// +kubebuilder:validation:items:MinLength=1
Expand Down Expand Up @@ -819,6 +818,22 @@ type SPIFFEClientConfig struct {
// +kubebuilder:validation:items:MaxLength=256
// +listType=set
Scopes []string `json:"scopes"`

// GrantTypes contains the OAuth grants permitted for this association.
// It defaults to the token-exchange grant when omitted, which is what the
// operator configured before this field existed. List client_credentials
// to opt in to the SPIFFE-bound client credentials grant. The list alone
// decides which grants the association may use: client authentication does
// not confer any grant, and there is no separate per-grant enable switch.
// Use urn:ietf:params:oauth:grant-type:token-exchange for RFC 8693 token
// exchange.
// +optional
// +kubebuilder:default={"urn:ietf:params:oauth:grant-type:token-exchange"}
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=2
// +kubebuilder:validation:items:Enum={"client_credentials","urn:ietf:params:oauth:grant-type:token-exchange"}
// +listType=set
GrantTypes []string `json:"grantTypes,omitempty"`
}

// InboundGrantsConfig groups canonical inbound OAuth grant-family configuration.
Expand Down Expand Up @@ -926,8 +941,9 @@ type JWTBearerIssuerPolicyConfig struct {
// This type is shared by MCPExternalAuthConfig.Spec.EmbeddedAuthServer and
// VirtualMCPServer.Spec.AuthServerConfig, so the XValidation rules below are
// enforced at admission for both CRDs. CEL only requires the explicit opt-in for
// delegate clients using an HTTP issuer; the shared Go validator performs the
// precise loopback-host security check.
// delegate clients and JWT-SVID SPIFFE client_credentials associations using an
// HTTP issuer; the shared Go validator performs the precise loopback-host
// security check.
//
// +kubebuilder:validation:XValidation:rule="(has(self.upstreamProviders) && size(self.upstreamProviders) > 0) || (has(self.delegateClients) && size(self.delegateClients) > 0) || (has(self.trustedIssuers) && self.trustedIssuers.exists(issuer, has(issuer.jwtBearerGrant))) || (has(self.inboundGrants) && (has(self.inboundGrants.tokenExchange) || has(self.inboundGrants.jwtBearer) || (has(self.inboundGrants.spiffeClientAuth) && size(self.inboundGrants.spiffeClientAuth) > 0)))",message="at least one upstream provider or inbound grant family is required"
//
Expand All @@ -943,9 +959,9 @@ type JWTBearerIssuerPolicyConfig struct {
// +kubebuilder:validation:XValidation:rule="!has(self.inboundGrants) || !has(self.inboundGrants.spiffeClientAuth) || self.inboundGrants.spiffeClientAuth.all(client, !client.clientId.startsWith('synthetic:'))",message="spiffeClientAuth clientId must not use the reserved synthetic: prefix"
// +kubebuilder:validation:XValidation:rule="!has(self.inboundGrants) || !has(self.inboundGrants.spiffeClientAuth) || self.inboundGrants.spiffeClientAuth.all(client, !client.clientId.matches('^[A-Za-z][A-Za-z0-9+.-]*://.+'))",message="spiffeClientAuth clientId must not be an absolute URL"
//
// +kubebuilder:validation:XValidation:rule="!(has(self.insecureAllowHTTP) && self.insecureAllowHTTP && ((has(self.allowConfidentialClientRegistration) && self.allowConfidentialClientRegistration) || (has(self.delegateClients) && size(self.delegateClients) > 0) || (has(self.inboundGrants) && has(self.inboundGrants.tokenExchange) && has(self.inboundGrants.tokenExchange.delegateClients) && size(self.inboundGrants.tokenExchange.delegateClients) > 0)))",message="insecureAllowHTTP cannot be combined with confidential client registration or delegateClients; client secrets would be issued or used in cleartext over an unauthenticated endpoint"
// +kubebuilder:validation:XValidation:rule="!(has(self.insecureAllowHTTP) && self.insecureAllowHTTP && ((has(self.allowConfidentialClientRegistration) && self.allowConfidentialClientRegistration) || (has(self.delegateClients) && size(self.delegateClients) > 0) || (has(self.inboundGrants) && has(self.inboundGrants.tokenExchange) && has(self.inboundGrants.tokenExchange.delegateClients) && size(self.inboundGrants.tokenExchange.delegateClients) > 0) || (has(self.inboundGrants) && has(self.inboundGrants.spiffeClientAuth) && self.inboundGrants.spiffeClientAuth.exists(a, 'spiffe_jwt' in a.methods && has(a.grantTypes) && 'client_credentials' in a.grantTypes))))",message="insecureAllowHTTP cannot be combined with confidential client registration, delegateClients or JWT-SVID SPIFFE associations that grant client_credentials; client secrets or JWT-SVIDs would be issued or used in cleartext over an unauthenticated endpoint"
// +kubebuilder:validation:XValidation:rule="(!has(self.forceConfidentialRedirectUris) || size(self.forceConfidentialRedirectUris) == 0) || (has(self.allowConfidentialClientRegistration) && self.allowConfidentialClientRegistration)",message="forceConfidentialRedirectUris requires allowConfidentialClientRegistration to be true"
// +kubebuilder:validation:XValidation:rule="!self.issuer.startsWith('http://') || (has(self.insecureAllowConfidentialOverLoopbackHTTP) && self.insecureAllowConfidentialOverLoopbackHTTP) || ((!has(self.allowConfidentialClientRegistration) || !self.allowConfidentialClientRegistration) && (!has(self.delegateClients) || size(self.delegateClients) == 0) && (!has(self.inboundGrants) || !has(self.inboundGrants.tokenExchange) || !has(self.inboundGrants.tokenExchange.delegateClients) || size(self.inboundGrants.tokenExchange.delegateClients) == 0))",message="confidential client registration or delegateClients with an HTTP issuer require insecureAllowConfidentialOverLoopbackHTTP to be explicitly enabled; the issuer must still be loopback"
// +kubebuilder:validation:XValidation:rule="!self.issuer.startsWith('http://') || (has(self.insecureAllowConfidentialOverLoopbackHTTP) && self.insecureAllowConfidentialOverLoopbackHTTP) || ((!has(self.allowConfidentialClientRegistration) || !self.allowConfidentialClientRegistration) && (!has(self.delegateClients) || size(self.delegateClients) == 0) && (!has(self.inboundGrants) || !has(self.inboundGrants.tokenExchange) || !has(self.inboundGrants.tokenExchange.delegateClients) || size(self.inboundGrants.tokenExchange.delegateClients) == 0) && !(has(self.inboundGrants) && has(self.inboundGrants.spiffeClientAuth) && self.inboundGrants.spiffeClientAuth.exists(a, 'spiffe_jwt' in a.methods && has(a.grantTypes) && 'client_credentials' in a.grantTypes)))",message="confidential client registration, delegateClients or JWT-SVID SPIFFE associations that grant client_credentials with an HTTP issuer require insecureAllowConfidentialOverLoopbackHTTP to be explicitly enabled; the issuer must still be loopback"
// +kubebuilder:validation:XValidation:rule="!has(self.inboundGrants) || !has(self.inboundGrants.tokenExchange) || ((!has(self.delegateClients) || size(self.delegateClients) == 0) && (!has(self.trustedIssuers) || !self.trustedIssuers.exists(issuer, has(issuer.expectedAudience) || has(issuer.actorClaim) || has(issuer.allowedActors) || has(issuer.actorMatcher) || has(issuer.allowedDelegateClients) || (has(issuer.allowMayAct) && issuer.allowMayAct))))",message="canonical tokenExchange conflicts with legacy delegateClients or RFC 8693 trusted issuer policy"
// +kubebuilder:validation:XValidation:rule="!has(self.inboundGrants) || !has(self.inboundGrants.jwtBearer) || !has(self.trustedIssuers) || !self.trustedIssuers.exists(issuer, has(issuer.jwtBearerGrant))",message="canonical jwtBearer conflicts with legacy jwtBearerGrant"
// +kubebuilder:validation:XValidation:rule="!has(self.trustedIssuers) || self.trustedIssuers.all(issuer, !has(issuer.name) || self.trustedIssuers.filter(other, has(other.name) && other.name == issuer.name).size() == 1)",message="trustedIssuers must not contain duplicate names"
Expand Down Expand Up @@ -1102,8 +1118,10 @@ type EmbeddedAuthServerConfig struct {
//
// One combination is rejected at admission on all three CRDs regardless of the
// above: setting this field alongside confidential client registration or
// delegate clients, which would issue or use client secrets in cleartext over
// an unauthenticated endpoint (see the XValidation rule on
// delegate clients, or a SPIFFE association that has spiffe_jwt in methods and
// client_credentials in grantTypes on the same association, which would issue
// or use client secrets or JWT-SVIDs in cleartext over an unauthenticated
// endpoint (see the XValidation rule on
// EmbeddedAuthServerConfig).
// +kubebuilder:default=false
// +optional
Expand Down Expand Up @@ -1166,9 +1184,12 @@ type EmbeddedAuthServerConfig struct {
AllowPrivateKeyJWTRegistration bool `json:"allowPrivateKeyJWTRegistration,omitempty"`

// InsecureAllowConfidentialOverLoopbackHTTP opts in to confidential
// Dynamic Client Registration (DCR) and delegate clients when issuer is a
// plain-HTTP loopback URL (e.g. "http://localhost:8080"). Without this
// flag, that combination is rejected at reconcile time: a loopback http://
// Dynamic Client Registration (DCR), delegate clients and SPIFFE
// associations that have spiffe_jwt in methods and client_credentials in
// grantTypes on the same association, when issuer is a plain-HTTP loopback
// URL (e.g. "http://localhost:8080"). Without this
// flag, that combination is rejected at admission, and reconcile additionally
// requires the issuer host to be loopback. A loopback http://
// issuer is normally fine for local development since the traffic never
// leaves the machine, but confidential clients send secrets over cleartext.
// Forcing TLS onto every loopback deployment instead would just push
Expand Down Expand Up @@ -1403,6 +1424,14 @@ func applyEffectiveJWTBearer(
return nil
}

// hasConfidentialClients reports whether DCR of confidential clients is
// enabled, the effective grants include a delegate client, or a SPIFFE
// association allows the JWT-SVID method and grants client_credentials.
func (c *EmbeddedAuthServerConfig) hasConfidentialClients(grants effectiveInboundGrants) bool {
return c.AllowConfidentialClientRegistration || len(grants.delegateClients) > 0 ||
c.hasSPIFFEJWTClientCredentials()
}

// ValidateInboundGrants validates the effective grant projection shared by
// MCPExternalAuthConfig and inline VirtualMCPServer auth configuration.
func (c *EmbeddedAuthServerConfig) ValidateInboundGrants() error {
Expand All @@ -1411,7 +1440,7 @@ func (c *EmbeddedAuthServerConfig) ValidateInboundGrants() error {
return err
}
if err := authserver.ValidateConfidentialClientTransport(
c.AllowConfidentialClientRegistration || len(grants.delegateClients) > 0,
c.hasConfidentialClients(grants),
c.InsecureAllowHTTP,
c.Issuer,
c.InsecureAllowConfidentialOverLoopbackHTTP,
Expand Down Expand Up @@ -1485,6 +1514,18 @@ func (c *EmbeddedAuthServerConfig) hasSPIFFEMethod(method SPIFFEAuthenticationMe
})
}

// hasSPIFFEJWTClientCredentials reports whether any single SPIFFE association
// allows the JWT-SVID method and lists the client_credentials grant.
func (c *EmbeddedAuthServerConfig) hasSPIFFEJWTClientCredentials() bool {
if c.InboundGrants == nil {
return false
}
return slices.ContainsFunc(c.InboundGrants.SPIFFEClientAuth, func(a SPIFFEClientConfig) bool {
return slices.Contains(a.Methods, SPIFFEAuthenticationMethodJWT) &&
slices.Contains(a.GrantTypes, oauthproto.GrantTypeClientCredentials)
})
}

func hasLegacyTokenExchangePolicy(issuer TrustedIssuerConfig) bool {
return issuer.ExpectedAudience != "" || issuer.ActorClaim != "" || len(issuer.AllowedActors) > 0 ||
issuer.ActorMatcher != "" || len(issuer.AllowedDelegateClients) > 0 || issuer.AllowMayAct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,10 @@ func TestMCPExternalAuthConfig_validateEmbeddedAuthServer(t *testing.T) {
// admission-time pre-check fabricated a nil
// allowlist/default-scopes set instead of
// deferring to reconcile time.
Resources: []string{"https://backend.example.com"},
Audiences: []string{"https://mcp.example.com"},
Scopes: []string{"custom:scope"},
Resources: []string{"https://backend.example.com"},
Audiences: []string{"https://mcp.example.com"},
Scopes: []string{"custom:scope"},
GrantTypes: []string{"urn:ietf:params:oauth:grant-type:token-exchange"},
}},
},
},
Expand Down Expand Up @@ -1352,11 +1353,13 @@ func mustEmbeddedAuthServerConfigWithPrincipalPatterns(first, second string) *MC
TrustDomainRef: "example", PrincipalPattern: first, ClientID: "spiffe-client-1",
Methods: []SPIFFEAuthenticationMethod{SPIFFEAuthenticationMethodX509},
Audiences: []string{"https://mcp.example.com"}, Resources: []string{"https://mcp.example.com"}, Scopes: []string{"openid"},
GrantTypes: []string{"urn:ietf:params:oauth:grant-type:token-exchange"},
},
{
TrustDomainRef: "example", PrincipalPattern: second, ClientID: "spiffe-client-2",
Methods: []SPIFFEAuthenticationMethod{SPIFFEAuthenticationMethodX509},
Audiences: []string{"https://mcp.example.com"}, Resources: []string{"https://mcp.example.com"}, Scopes: []string{"openid"},
GrantTypes: []string{"urn:ietf:params:oauth:grant-type:token-exchange"},
},
},
},
Expand Down Expand Up @@ -1398,7 +1401,8 @@ func mustEmbeddedAuthServerConfigWithResource(resource string) *MCPExternalAuthC
ClientID: "spiffe-client",
Methods: []SPIFFEAuthenticationMethod{SPIFFEAuthenticationMethodX509},
Audiences: []string{"https://mcp.example.com"}, Scopes: []string{"openid"},
Resources: []string{resource},
Resources: []string{resource},
GrantTypes: []string{"urn:ietf:params:oauth:grant-type:token-exchange"},
}},
},
},
Expand Down Expand Up @@ -1465,6 +1469,7 @@ func TestMCPExternalAuthConfig_ZeroUpstreamAlternatives(t *testing.T) {
ClientID: "spiffe-client",
Methods: []SPIFFEAuthenticationMethod{SPIFFEAuthenticationMethodX509},
Audiences: []string{"https://mcp.example.com"}, Resources: []string{"https://mcp.example.com"}, Scopes: []string{"openid"},
GrantTypes: []string{"urn:ietf:params:oauth:grant-type:token-exchange"},
}},
},
},
Expand Down Expand Up @@ -2114,30 +2119,71 @@ func TestEmbeddedAuthServerConfig_ValidateInboundGrants_ConfidentialClientTransp
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"},
Resources: []string{"https://mcp.example.com"},
Scopes: []string{"openid"}, GrantTypes: []string{"client_credentials"},
}},
},
}
mutate(&config)
return config
}
splitSPIFFEMethodAndGrant := func(c *EmbeddedAuthServerConfig) {
c.SPIFFETrustDomains[0].Methods = []SPIFFEAuthenticationMethod{SPIFFEAuthenticationMethodJWT, SPIFFEAuthenticationMethodX509}
first := &c.InboundGrants.SPIFFEClientAuth[0]
first.GrantTypes = []string{"urn:ietf:params:oauth:grant-type:token-exchange"}
second := *first
second.PrincipalPattern = "spiffe://example.org/ns/default/other"
second.ClientID = "spiffe-client-x509"
second.Methods = []SPIFFEAuthenticationMethod{SPIFFEAuthenticationMethodX509}
second.GrantTypes = []string{"client_credentials"}
c.InboundGrants.SPIFFEClientAuth = append(c.InboundGrants.SPIFFEClientAuth, second)
}
tests := []struct {
name string
config EmbeddedAuthServerConfig
expectErr bool
}{
{
name: "SPIFFE JWT association is admitted with insecure HTTP",
name: "SPIFFE JWT client_credentials association rejects insecure HTTP",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(c *EmbeddedAuthServerConfig) {
c.InsecureAllowHTTP = true
}),
expectErr: true,
},
{
name: "SPIFFE JWT client_credentials association rejects HTTP loopback without explicit opt in",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(*EmbeddedAuthServerConfig) {}),
expectErr: true,
},
{
name: "SPIFFE JWT client_credentials association allows HTTP loopback with explicit opt in",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(c *EmbeddedAuthServerConfig) {
c.InsecureAllowConfidentialOverLoopbackHTTP = true
}),
},
{
name: "SPIFFE JWT token-exchange-only association is admitted with insecure HTTP",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(c *EmbeddedAuthServerConfig) {
c.InsecureAllowHTTP = true
c.InboundGrants.SPIFFEClientAuth[0].GrantTypes = []string{"urn:ietf:params:oauth:grant-type:token-exchange"}
}),
},
{
name: "SPIFFE JWT association is admitted on HTTP loopback without explicit opt in",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(*EmbeddedAuthServerConfig) {}),
name: "SPIFFE JWT token-exchange-only association is admitted on HTTP loopback without explicit opt in",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(c *EmbeddedAuthServerConfig) {
c.InboundGrants.SPIFFEClientAuth[0].GrantTypes = []string{"urn:ietf:params:oauth:grant-type:token-exchange"}
}),
},
{
name: "SPIFFE JWT token exchange plus X.509 client_credentials on separate associations is admitted with insecure HTTP",
config: spiffeConfig(SPIFFEAuthenticationMethodJWT, func(c *EmbeddedAuthServerConfig) {
c.InsecureAllowHTTP = true
c.Issuer = "https://auth.example.com"
splitSPIFFEMethodAndGrant(c)
}),
},
{
name: "SPIFFE X.509-only association is unaffected by insecure HTTP",
name: "SPIFFE X.509 client_credentials association is unaffected by insecure HTTP",
config: spiffeConfig(SPIFFEAuthenticationMethodX509, func(c *EmbeddedAuthServerConfig) {
c.InsecureAllowHTTP = true
c.Issuer = "https://auth.example.com"
Expand Down Expand Up @@ -2401,6 +2447,7 @@ func TestEmbeddedAuthServerConfig_ValidateTLSListener(t *testing.T) {
return &InboundGrantsConfig{SPIFFEClientAuth: []SPIFFEClientConfig{{
TrustDomainRef: "example", PrincipalPattern: "spiffe://example.org/ns/default/agent",
ClientID: "spiffe-client", Methods: []SPIFFEAuthenticationMethod{method},
GrantTypes: []string{"client_credentials"},
}}}
}
trustDomains := []SPIFFETrustDomainConfig{{
Expand Down
5 changes: 5 additions & 0 deletions cmd/thv-operator/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading