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
25 changes: 25 additions & 0 deletions cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stacklok/toolhive/pkg/authserver"
"github.com/stacklok/toolhive/pkg/authserver/oauthparams"
"github.com/stacklok/toolhive/pkg/authserver/server/registration"
"github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange"
"github.com/stacklok/toolhive/pkg/oauthproto"
)
Expand Down Expand Up @@ -1982,6 +1983,20 @@ type OAuth2UpstreamConfig struct {
//
//nolint:lll // CEL validation rules exceed line length limit
type DCRUpstreamConfig struct {
// ClientName is the optional RFC 7591 "client_name" registration metadata
// sent when ToolHive registers with the upstream. When empty, ToolHive
// registers as "ToolHive MCP Client". It is display-only metadata: the
// upstream shows it on its authorization screen, and it is never used as
// a key. Changing it does not rename an already-registered client; the
// new value applies only the next time ToolHive registers a fresh client,
// for example after rotating via RotationKey, or when the upstream
// rejects the cached registration. Names with leading or trailing
// whitespace, or containing control or format characters, are rejected.
// +optional
// +kubebuilder:validation:MaxLength=256
Comment thread
aponcedeleonch marked this conversation as resolved.
Resolved
// +kubebuilder:validation:Pattern=`^[^\p{Cc}\p{Cf}\p{Z}]([^\p{Cc}\p{Cf}]*[^\p{Cc}\p{Cf}\p{Z}])?$`
ClientName string `json:"clientName,omitempty"`

// DiscoveryURL is the RFC 8414 / OIDC Discovery document URL. The resolver
// issues a single GET against this URL (no well-known-path fallback) and
// reads registration_endpoint, authorization_endpoint, token_endpoint,
Expand Down Expand Up @@ -3192,6 +3207,10 @@ const (
// MaxSoftwareStatementLength matches the MaxLength marker on
// DCRUpstreamConfig.SoftwareStatement.
MaxSoftwareStatementLength = 16384

// MaxDCRClientNameLength matches the MaxLength marker on
// DCRUpstreamConfig.ClientName.
MaxDCRClientNameLength = 256
)

// validateUpstreamCABundleRef validates the source shape shared by OIDC and
Expand Down Expand Up @@ -3239,6 +3258,9 @@ func ValidateOIDCDCRConfig(cfg *OIDCUpstreamConfig) error {
if l := len(cfg.DCRConfig.SoftwareStatement); l > MaxSoftwareStatementLength {
return fmt.Errorf("oidcConfig.dcrConfig.softwareStatement: length %d exceeds maximum %d", l, MaxSoftwareStatementLength)
}
if err := registration.ValidateClientName(cfg.DCRConfig.ClientName); err != nil {
return fmt.Errorf("oidcConfig.dcrConfig.clientName: %w", err)
}
return nil
}

Expand Down Expand Up @@ -3293,6 +3315,9 @@ func ValidateOAuth2DCRConfig(cfg *OAuth2UpstreamConfig) error {
"oauth2Config.dcrConfig.softwareStatement: length %d exceeds maximum %d",
l, MaxSoftwareStatementLength)
}
if err := registration.ValidateClientName(cfg.DCRConfig.ClientName); err != nil {
return fmt.Errorf("oauth2Config.dcrConfig.clientName: %w", err)
}
return nil
}

Expand Down
142 changes: 142 additions & 0 deletions cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ func makeStringOfLen(n int) string {
return strings.Repeat("a", n)
}

// makeMultibyteStringOfRunes returns a string of exactly n runes, each 3 bytes
// ("あ"), so its byte length is 3n. Useful for boundary-length tests that must
// distinguish rune counts from byte counts.
func makeMultibyteStringOfRunes(n int) string {
return strings.Repeat("あ", n)
}

// makeURLOfLen returns an https URL whose total length is exactly n.
// The host is padded with 'a' characters; useful for boundary-length tests
// against +kubebuilder:validation:Pattern URL regexes that require a host.
Expand Down Expand Up @@ -1939,6 +1946,141 @@ func TestMCPExternalAuthConfig_validateUpstreamProvider(t *testing.T) {
expectErr: true,
errMsg: "dcrConfig.softwareStatement: length",
},
{
name: "OAuth2 provider with DCRConfig clientName at MaxLength is accepted",
provider: UpstreamProviderConfig{
Name: "dcr-client-name-at-cap",
Type: UpstreamProviderTypeOAuth2,
OAuth2Config: &OAuth2UpstreamConfig{
AuthorizationEndpoint: "https://idp.example.com/authorize",
TokenEndpoint: "https://idp.example.com/token",
UserInfo: &UserInfoConfig{EndpointURL: "https://idp.example.com/userinfo"},
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: makeStringOfLen(MaxDCRClientNameLength),
},
},
},
expectErr: false,
},
{
name: "OAuth2 provider with DCRConfig clientName over MaxLength is rejected",
provider: UpstreamProviderConfig{
Name: "dcr-client-name-over-cap",
Type: UpstreamProviderTypeOAuth2,
OAuth2Config: &OAuth2UpstreamConfig{
AuthorizationEndpoint: "https://idp.example.com/authorize",
TokenEndpoint: "https://idp.example.com/token",
UserInfo: &UserInfoConfig{EndpointURL: "https://idp.example.com/userinfo"},
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: makeStringOfLen(MaxDCRClientNameLength + 1),
},
},
},
expectErr: true,
errMsg: "dcrConfig.clientName: must be at most 256 characters",
},
{
name: "OIDC provider with DCRConfig clientName at MaxLength is accepted",
provider: UpstreamProviderConfig{
Name: "oidc-dcr-client-name-at-cap",
Type: UpstreamProviderTypeOIDC,
OIDCConfig: &OIDCUpstreamConfig{
IssuerURL: "https://idp.example.com",
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: makeStringOfLen(MaxDCRClientNameLength),
},
},
},
expectErr: false,
},
{
name: "OIDC provider with DCRConfig clientName over MaxLength is rejected",
provider: UpstreamProviderConfig{
Name: "oidc-dcr-client-name-over-cap",
Type: UpstreamProviderTypeOIDC,
OIDCConfig: &OIDCUpstreamConfig{
IssuerURL: "https://idp.example.com",
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: makeStringOfLen(MaxDCRClientNameLength + 1),
},
},
},
expectErr: true,
errMsg: "oidcConfig.dcrConfig.clientName: must be at most 256 characters",
},
{
name: "OAuth2 provider with DCRConfig clientName of 256 multibyte runes is accepted",
provider: UpstreamProviderConfig{
Name: "dcr-client-name-multibyte-at-cap",
Type: UpstreamProviderTypeOAuth2,
OAuth2Config: &OAuth2UpstreamConfig{
AuthorizationEndpoint: "https://idp.example.com/authorize",
TokenEndpoint: "https://idp.example.com/token",
UserInfo: &UserInfoConfig{EndpointURL: "https://idp.example.com/userinfo"},
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: makeMultibyteStringOfRunes(MaxDCRClientNameLength),
},
},
},
expectErr: false,
},
{
name: "OAuth2 provider with DCRConfig clientName of 257 multibyte runes is rejected",
provider: UpstreamProviderConfig{
Name: "dcr-client-name-multibyte-over-cap",
Type: UpstreamProviderTypeOAuth2,
OAuth2Config: &OAuth2UpstreamConfig{
AuthorizationEndpoint: "https://idp.example.com/authorize",
TokenEndpoint: "https://idp.example.com/token",
UserInfo: &UserInfoConfig{EndpointURL: "https://idp.example.com/userinfo"},
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: makeMultibyteStringOfRunes(MaxDCRClientNameLength + 1),
},
},
},
expectErr: true,
errMsg: "dcrConfig.clientName: must be at most 256 characters",
},
{
name: "OAuth2 provider with DCRConfig whitespace-only clientName is rejected",
provider: UpstreamProviderConfig{
Name: "dcr-client-name-whitespace-only",
Type: UpstreamProviderTypeOAuth2,
OAuth2Config: &OAuth2UpstreamConfig{
AuthorizationEndpoint: "https://idp.example.com/authorize",
TokenEndpoint: "https://idp.example.com/token",
UserInfo: &UserInfoConfig{EndpointURL: "https://idp.example.com/userinfo"},
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: " ",
},
},
},
expectErr: true,
errMsg: "dcrConfig.clientName: must not have leading or trailing whitespace",
},
{
name: "OIDC provider with DCRConfig whitespace-only clientName is rejected",
provider: UpstreamProviderConfig{
Name: "oidc-dcr-client-name-whitespace-only",
Type: UpstreamProviderTypeOIDC,
OIDCConfig: &OIDCUpstreamConfig{
IssuerURL: "https://idp.example.com",
DCRConfig: &DCRUpstreamConfig{
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
ClientName: " ",
},
},
},
expectErr: true,
errMsg: "oidcConfig.dcrConfig.clientName: must not have leading or trailing whitespace",
},
{
name: "OAuth2 provider with identityFromToken empty subjectPath rejected",
provider: UpstreamProviderConfig{
Expand Down
1 change: 1 addition & 0 deletions cmd/thv-operator/pkg/controllerutil/authserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,7 @@ func buildDCRUpstreamRunConfig(
initialAccessTokenEnvVar string,
) *authserver.DCRUpstreamConfig {
rc := &authserver.DCRUpstreamConfig{
ClientName: dcr.ClientName,
DiscoveryURL: dcr.DiscoveryURL,
RegistrationEndpoint: dcr.RegistrationEndpoint,
RotationKey: dcr.RotationKey,
Expand Down
2 changes: 2 additions & 0 deletions cmd/thv-operator/pkg/controllerutil/authserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@ func TestBuildAuthServerRunConfig(t *testing.T) {
TokenEndpoint: "https://idp.example.com/token",
UserInfo: &mcpv1beta1.UserInfoConfig{EndpointURL: "https://idp.example.com/userinfo"},
DCRConfig: &mcpv1beta1.DCRUpstreamConfig{
ClientName: "Acme MCP Client",
DiscoveryURL: "https://idp.example.com/.well-known/openid-configuration",
SoftwareID: "toolhive",
SoftwareStatement: "jwt-statement",
Expand All @@ -1782,6 +1783,7 @@ func TestBuildAuthServerRunConfig(t *testing.T) {
assert.Empty(t, upstream.OAuth2Config.ClientID,
"ClientID should be empty when DCRConfig is used")
require.NotNil(t, upstream.OAuth2Config.DCRConfig)
assert.Equal(t, "Acme MCP Client", upstream.OAuth2Config.DCRConfig.ClientName)
assert.Equal(t,
"https://idp.example.com/.well-known/openid-configuration",
upstream.OAuth2Config.DCRConfig.DiscoveryURL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,20 @@ spec:
runtime rather than being pre-provisioned, and ClientID must be left empty.
Mutually exclusive with ClientID.
properties:
clientName:
description: |-
ClientName is the optional RFC 7591 "client_name" registration metadata
sent when ToolHive registers with the upstream. When empty, ToolHive
registers as "ToolHive MCP Client". It is display-only metadata: the
upstream shows it on its authorization screen, and it is never used as
a key. Changing it does not rename an already-registered client; the
new value applies only the next time ToolHive registers a fresh client,
for example after rotating via RotationKey, or when the upstream
rejects the cached registration. Names with leading or trailing
whitespace, or containing control or format characters, are rejected.
maxLength: 256
pattern: ^[^\p{Cc}\p{Cf}\p{Z}]([^\p{Cc}\p{Cf}]*[^\p{Cc}\p{Cf}\p{Z}])?$
type: string
discoveryUrl:
description: |-
DiscoveryURL is the RFC 8414 / OIDC Discovery document URL. The resolver
Expand Down Expand Up @@ -2278,6 +2292,20 @@ spec:
and ClientSecretRef must be omitted. If neither discoveryUrl nor
registrationEndpoint is set, discovery is derived from issuerUrl.
properties:
clientName:
description: |-
ClientName is the optional RFC 7591 "client_name" registration metadata
sent when ToolHive registers with the upstream. When empty, ToolHive
registers as "ToolHive MCP Client". It is display-only metadata: the
upstream shows it on its authorization screen, and it is never used as
a key. Changing it does not rename an already-registered client; the
new value applies only the next time ToolHive registers a fresh client,
for example after rotating via RotationKey, or when the upstream
rejects the cached registration. Names with leading or trailing
whitespace, or containing control or format characters, are rejected.
maxLength: 256
pattern: ^[^\p{Cc}\p{Cf}\p{Z}]([^\p{Cc}\p{Cf}]*[^\p{Cc}\p{Cf}\p{Z}])?$
type: string
discoveryUrl:
description: |-
DiscoveryURL is the RFC 8414 / OIDC Discovery document URL. The resolver
Expand Down Expand Up @@ -4997,6 +5025,20 @@ spec:
runtime rather than being pre-provisioned, and ClientID must be left empty.
Mutually exclusive with ClientID.
properties:
clientName:
description: |-
ClientName is the optional RFC 7591 "client_name" registration metadata
sent when ToolHive registers with the upstream. When empty, ToolHive
registers as "ToolHive MCP Client". It is display-only metadata: the
upstream shows it on its authorization screen, and it is never used as
a key. Changing it does not rename an already-registered client; the
new value applies only the next time ToolHive registers a fresh client,
for example after rotating via RotationKey, or when the upstream
rejects the cached registration. Names with leading or trailing
whitespace, or containing control or format characters, are rejected.
maxLength: 256
pattern: ^[^\p{Cc}\p{Cf}\p{Z}]([^\p{Cc}\p{Cf}]*[^\p{Cc}\p{Cf}\p{Z}])?$
type: string
discoveryUrl:
description: |-
DiscoveryURL is the RFC 8414 / OIDC Discovery document URL. The resolver
Expand Down Expand Up @@ -5454,6 +5496,20 @@ spec:
and ClientSecretRef must be omitted. If neither discoveryUrl nor
registrationEndpoint is set, discovery is derived from issuerUrl.
properties:
clientName:
description: |-
ClientName is the optional RFC 7591 "client_name" registration metadata
sent when ToolHive registers with the upstream. When empty, ToolHive
registers as "ToolHive MCP Client". It is display-only metadata: the
upstream shows it on its authorization screen, and it is never used as
a key. Changing it does not rename an already-registered client; the
new value applies only the next time ToolHive registers a fresh client,
for example after rotating via RotationKey, or when the upstream
rejects the cached registration. Names with leading or trailing
whitespace, or containing control or format characters, are rejected.
maxLength: 256
pattern: ^[^\p{Cc}\p{Cf}\p{Z}]([^\p{Cc}\p{Cf}]*[^\p{Cc}\p{Cf}\p{Z}])?$
type: string
discoveryUrl:
description: |-
DiscoveryURL is the RFC 8414 / OIDC Discovery document URL. The resolver
Expand Down
Loading
Loading