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
61 changes: 54 additions & 7 deletions cmd/thv-operator/api/v1beta1/mcpexternalauthconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,20 @@ type TrustedIssuerConfig struct {
JWTBearerGrant *JWTBearerGrantConfig `json:"jwtBearerGrant,omitempty"`
}

// JWTBearerAssertionType identifies one assertion form the RFC 7523
// jwt-bearer grant may accept for a trusted issuer.
type JWTBearerAssertionType string

const (
// JWTBearerAssertionTypeJWTBearer accepts plain RFC 7523 assertions
// (typ absent, empty, or "JWT") — e.g. service-account/machine credentials.
JWTBearerAssertionTypeJWTBearer JWTBearerAssertionType = "jwt_bearer"
// JWTBearerAssertionTypeIDJAG accepts Identity Assertion Authorization
// Grant assertions (typ: oauth-id-jag+jwt) — cross-app-access grants
// minted by this issuer acting as IdP for a different resource owner.
JWTBearerAssertionTypeIDJAG JWTBearerAssertionType = "id_jag"
)

// JWTBearerGrantConfig limits RFC 7523 JWT-bearer assertions for one trusted
// issuer. Each assertion subject must have an exact binding and request exactly
// one of that binding's allowed resources.
Expand Down Expand Up @@ -566,6 +580,29 @@ type JWTBearerGrantConfig struct {
// +listType=atomic
// +optional
AcceptedAudiences []string `json:"acceptedAudiences,omitempty"`

// AcceptedAssertionTypes selects which assertion form(s) this issuer's
// jwt-bearer grant accepts. When omitted, both "jwt_bearer" and "id_jag"
// are accepted, which is the behavior before this field existed. Set
// ["id_jag"] to restrict an IdP issuer (e.g. Okta acting as a
// Cross-App-Access IdP) to ID-JAG grants, so its other signed JWTs cannot
// be redeemed on the credential-free plain jwt-bearer path. Set
// ["jwt_bearer"] to refuse ID-JAG.
//
// When "id_jag" is listed explicitly and acceptedAudiences is set, it must
// include this AS's own issuer identifier (the ID-JAG's "aud" per
// draft-ietf-oauth-identity-assertion-authz-grant §4.4.1), not just the
// token endpoint URL. When acceptedAudiences is empty the issuer is
// accepted by default, unless it is also listed as an allowed audience
// (resource), which fails startup. Subject bindings are shared between both forms, so
// each binding also authorizes a plain jwt-bearer assertion from that
// subject whenever "jwt_bearer" is accepted.
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=2
// +kubebuilder:validation:items:Enum=jwt_bearer;id_jag
// +listType=set
// +optional
AcceptedAssertionTypes []JWTBearerAssertionType `json:"acceptedAssertionTypes,omitempty"`
}

// JWTBearerSubjectBinding configures the exact subject and allowed resources
Expand Down Expand Up @@ -1420,7 +1457,7 @@ func applyEffectiveJWTBearer(
return err
}
seen[policy.IssuerRef] = i
result.trustedIssuers[issuerIndex].JWTBearerGrant = buildJWTBearerGrantPolicy(&policy.JWTBearerGrantConfig)
result.trustedIssuers[issuerIndex].JWTBearerGrant = BuildJWTBearerGrantPolicy(&policy.JWTBearerGrantConfig)
}
return nil
}
Expand Down Expand Up @@ -3102,15 +3139,16 @@ func buildTrustedIssuerConfigs(issuers []TrustedIssuerConfig) []tokenexchange.Tr
ActorMatcher: issuer.ActorMatcher,
AllowedDelegateClients: slices.Clone(issuer.AllowedDelegateClients),
AllowMayAct: issuer.AllowMayAct,
JWTBearerGrant: buildJWTBearerGrantPolicy(issuer.JWTBearerGrant),
JWTBearerGrant: BuildJWTBearerGrantPolicy(issuer.JWTBearerGrant),
}
}
return configs
}

// buildJWTBearerGrantPolicy converts the CRD's JWT-bearer grant config to the
// runtime policy type without sharing caller-owned slices.
func buildJWTBearerGrantPolicy(grant *JWTBearerGrantConfig) *tokenexchange.JWTBearerGrantPolicy {
// BuildJWTBearerGrantPolicy converts the CRD's JWT-bearer grant config to the
// runtime policy type without sharing caller-owned slices. It is the single
// converter used by reconcile-time validation and RunConfig generation.
func BuildJWTBearerGrantPolicy(grant *JWTBearerGrantConfig) *tokenexchange.JWTBearerGrantPolicy {
if grant == nil {
return nil
}
Expand All @@ -3121,11 +3159,20 @@ func buildJWTBearerGrantPolicy(grant *JWTBearerGrantConfig) *tokenexchange.JWTBe
AllowedResources: slices.Clone(binding.AllowedResources),
}
}
return &tokenexchange.JWTBearerGrantPolicy{
MaxAssertionAge: grant.MaxAssertionAge.Duration.String(),
policy := &tokenexchange.JWTBearerGrantPolicy{
SubjectBindings: bindings,
AcceptedAudiences: slices.Clone(grant.AcceptedAudiences),
}
if grant.MaxAssertionAge != nil {
policy.MaxAssertionAge = grant.MaxAssertionAge.Duration.String()
}
if grant.AcceptedAssertionTypes != nil {
policy.AcceptedAssertionTypes = make([]tokenexchange.JWTBearerAssertionType, len(grant.AcceptedAssertionTypes))
for i, assertionType := range grant.AcceptedAssertionTypes {
policy.AcceptedAssertionTypes[i] = tokenexchange.JWTBearerAssertionType(assertionType)
}
}
return policy
}

// validateUpstreamProvider validates a single upstream provider configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange"
)

// makeStringOfLen returns a string of exactly n ASCII bytes.
Expand Down Expand Up @@ -1201,6 +1203,93 @@ func TestEmbeddedAuthServerConfig_ValidateInboundGrants_TrustedIssuerEndpoints(t
}
}

func TestEmbeddedAuthServerConfig_ValidateInboundGrants_JWTBearerAssertionTypes(t *testing.T) {
t.Parallel()

const selfIssuer = "https://auth.example.com"
grant := func(types []JWTBearerAssertionType, audiences []string) JWTBearerGrantConfig {
return JWTBearerGrantConfig{
MaxAssertionAge: &metav1.Duration{Duration: time.Minute},
SubjectBindings: []JWTBearerSubjectBinding{{
Subject: "workload", AllowedResources: []string{"https://mcp.example.com"},
}},
AcceptedAssertionTypes: types,
AcceptedAudiences: audiences,
}
}
newConfig := func(canonical bool, g JWTBearerGrantConfig) EmbeddedAuthServerConfig {
issuer := TrustedIssuerConfig{Name: "external", IssuerURL: "https://issuer.example.com"}
config := EmbeddedAuthServerConfig{Issuer: selfIssuer}
if canonical {
config.TrustedIssuers = []TrustedIssuerConfig{issuer}
config.InboundGrants = &InboundGrantsConfig{JWTBearer: &JWTBearerInboundGrantConfig{
IssuerPolicies: []JWTBearerIssuerPolicyConfig{{IssuerRef: "external", JWTBearerGrantConfig: g}},
}}
return config
}
issuer.JWTBearerGrant = &g
config.TrustedIssuers = []TrustedIssuerConfig{issuer}
return config
}

idJAG := []JWTBearerAssertionType{JWTBearerAssertionTypeIDJAG}
tests := []struct {
name string
types []JWTBearerAssertionType
audiences []string
wantErr string
}{
{name: "id_jag without issuer audience rejected", types: idJAG, audiences: []string{"https://other.example.com"},
wantErr: "must include the authorization server issuer"},
{name: "id_jag with no audiences accepted, the issuer is the default", types: idJAG},
{name: "id_jag with issuer audience accepted", types: idJAG, audiences: []string{selfIssuer}},
{name: "unsupported assertion type rejected", types: []JWTBearerAssertionType{"bogus"},
wantErr: "unsupported value"},
{name: "unset assertion types keep prior behavior", audiences: []string{"https://other.example.com"}},
}

for _, tt := range tests {
for _, canonical := range []bool{false, true} {
shape := map[bool]string{false: "legacy", true: "canonical"}[canonical]
t.Run(shape+" "+tt.name, func(t *testing.T) {
t.Parallel()
config := newConfig(canonical, grant(tt.types, tt.audiences))
err := config.ValidateInboundGrants()
if tt.wantErr == "" {
require.NoError(t, err)
return
}
require.ErrorContains(t, err, tt.wantErr)
})
}
}
}

func TestBuildJWTBearerGrantPolicy_AssertionTypes(t *testing.T) {
t.Parallel()

tests := []struct {
name string
types []JWTBearerAssertionType
want []tokenexchange.JWTBearerAssertionType
}{
{name: "nil stays nil"},
{name: "empty stays empty non-nil", types: []JWTBearerAssertionType{}, want: []tokenexchange.JWTBearerAssertionType{}},
{name: "values converted", types: []JWTBearerAssertionType{JWTBearerAssertionTypeJWTBearer, JWTBearerAssertionTypeIDJAG},
want: []tokenexchange.JWTBearerAssertionType{
tokenexchange.JWTBearerAssertionTypeJWTBearer, tokenexchange.JWTBearerAssertionTypeIDJAG}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
policy := BuildJWTBearerGrantPolicy(&JWTBearerGrantConfig{AcceptedAssertionTypes: tt.types})
require.NotNil(t, policy)
assert.Equal(t, tt.want, policy.AcceptedAssertionTypes)
assert.Empty(t, policy.MaxAssertionAge, "nil maxAssertionAge must not panic")
})
}
}

func TestEmbeddedAuthServerConfig_ValidateInboundGrants_DuplicateCredentialIssuerRedaction(t *testing.T) {
t.Parallel()

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.

44 changes: 19 additions & 25 deletions cmd/thv-operator/pkg/controllerutil/authserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func buildTrustedIssuerRunConfigs(issuers []mcpv1beta1.TrustedIssuerConfig) []to
ActorMatcher: ti.ActorMatcher,
AllowedDelegateClients: append([]string(nil), ti.AllowedDelegateClients...),
AllowMayAct: ti.AllowMayAct,
JWTBearerGrant: buildJWTBearerGrantPolicy(ti.JWTBearerGrant),
JWTBearerGrant: mcpv1beta1.BuildJWTBearerGrantPolicy(ti.JWTBearerGrant),
}
if ti.CABundleRef != nil {
configs[i].CAFilePath = trustedIssuerCABundleFilePath(i)
Expand All @@ -318,26 +318,6 @@ func buildTrustedIssuerRunConfigs(issuers []mcpv1beta1.TrustedIssuerConfig) []to
return configs
}

func buildJWTBearerGrantPolicy(config *mcpv1beta1.JWTBearerGrantConfig) *tokenexchange.JWTBearerGrantPolicy {
if config == nil {
return nil
}
policy := &tokenexchange.JWTBearerGrantPolicy{
SubjectBindings: make([]tokenexchange.JWTBearerSubjectBinding, len(config.SubjectBindings)),
AcceptedAudiences: append([]string(nil), config.AcceptedAudiences...),
}
if config.MaxAssertionAge != nil {
policy.MaxAssertionAge = config.MaxAssertionAge.Duration.String()
}
for i, binding := range config.SubjectBindings {
policy.SubjectBindings[i] = tokenexchange.JWTBearerSubjectBinding{
Subject: binding.Subject,
AllowedResources: append([]string(nil), binding.AllowedResources...),
}
}
return policy
}

// buildSPIFFETrustDomainRunConfigs converts CRD SPIFFETrustDomainConfig
// entries to authserver.SPIFFETrustDomainRunConfig, the runtime type
// authserver.RunConfig.SPIFFETrustDomains consumes directly. None of these
Expand Down Expand Up @@ -1148,17 +1128,31 @@ func buildInboundGrantsRunConfig(
return nil, fmt.Errorf("jwtBearer.issuerPolicies[%d].maxAssertionAge is required", i)
}
policies[i] = authserver.JWTBearerIssuerPolicyRunConfig{
IssuerRef: policy.IssuerRef,
MaxAssertionAge: policy.MaxAssertionAge.Duration.String(),
SubjectBindings: buildJWTBearerSubjectBindings(policy.SubjectBindings),
AcceptedAudiences: append([]string(nil), policy.AcceptedAudiences...),
IssuerRef: policy.IssuerRef,
MaxAssertionAge: policy.MaxAssertionAge.Duration.String(),
SubjectBindings: buildJWTBearerSubjectBindings(policy.SubjectBindings),
AcceptedAudiences: append([]string(nil), policy.AcceptedAudiences...),
AcceptedAssertionTypes: buildJWTBearerAssertionTypes(policy.AcceptedAssertionTypes),
}
}
grants.JWTBearer = &authserver.JWTBearerInboundGrantRunConfig{IssuerPolicies: policies}
}
return grants, nil
}

func buildJWTBearerAssertionTypes(
types []mcpv1beta1.JWTBearerAssertionType,
) []tokenexchange.JWTBearerAssertionType {
if types == nil {
return nil
}
converted := make([]tokenexchange.JWTBearerAssertionType, len(types))
for i, assertionType := range types {
converted[i] = tokenexchange.JWTBearerAssertionType(assertionType)
}
return converted
}

func buildJWTBearerSubjectBindings(
bindings []mcpv1beta1.JWTBearerSubjectBinding,
) []tokenexchange.JWTBearerSubjectBinding {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

mcpv1beta1 "github.com/stacklok/toolhive/cmd/thv-operator/api/v1beta1"
"github.com/stacklok/toolhive/pkg/authserver/server/tokenexchange"
)

func TestBuildAuthServerRunConfigConvertsCanonicalInboundGrants(t *testing.T) {
Expand Down Expand Up @@ -42,6 +43,8 @@ func TestBuildAuthServerRunConfigConvertsCanonicalInboundGrants(t *testing.T) {
SubjectBindings: []mcpv1beta1.JWTBearerSubjectBinding{{
Subject: "workload", AllowedResources: []string{"https://resource.example.com"},
}},
AcceptedAssertionTypes: []mcpv1beta1.JWTBearerAssertionType{mcpv1beta1.JWTBearerAssertionTypeIDJAG},
AcceptedAudiences: []string{"https://auth.example.com"},
},
}},
},
Expand All @@ -65,6 +68,9 @@ func TestBuildAuthServerRunConfigConvertsCanonicalInboundGrants(t *testing.T) {
require.Len(t, config.InboundGrants.JWTBearer.IssuerPolicies, 1)
assert.Equal(t, "5m0s", config.InboundGrants.JWTBearer.IssuerPolicies[0].MaxAssertionAge)
assert.Equal(t, "workload", config.InboundGrants.JWTBearer.IssuerPolicies[0].SubjectBindings[0].Subject)
assert.Equal(t, []tokenexchange.JWTBearerAssertionType{tokenexchange.JWTBearerAssertionTypeIDJAG},
config.InboundGrants.JWTBearer.IssuerPolicies[0].AcceptedAssertionTypes,
"canonical inboundGrants.jwtBearer.issuerPolicies[*].acceptedAssertionTypes must thread through to the RunConfig")
}

func TestGenerateAuthServerEnvVarsUsesCanonicalDelegateClients(t *testing.T) {
Expand Down
26 changes: 25 additions & 1 deletion cmd/thv-operator/pkg/controllerutil/authserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3604,6 +3604,26 @@ func TestBuildAuthServerRunConfig_RejectsNilJWTBearerMaxAssertionAge(t *testing.
}
}

func TestBuildTrustedIssuerRunConfigs_JWTBearerGrantUnsetAssertionTypes(t *testing.T) {
t.Parallel()

configs := buildTrustedIssuerRunConfigs([]mcpv1beta1.TrustedIssuerConfig{{
IssuerURL: "https://issuer.example.com",
JWTBearerGrant: &mcpv1beta1.JWTBearerGrantConfig{
MaxAssertionAge: &metav1.Duration{Duration: 5 * time.Minute},
SubjectBindings: []mcpv1beta1.JWTBearerSubjectBinding{{
Subject: "workload-123",
AllowedResources: []string{"https://mcp.example.com"},
}},
},
}})

require.Len(t, configs, 1)
require.NotNil(t, configs[0].JWTBearerGrant)
assert.Empty(t, configs[0].JWTBearerGrant.AcceptedAssertionTypes,
"an unset acceptedAssertionTypes must stay unset so the runtime default (both types) applies")
}

func TestBuildTrustedIssuerRunConfigs_JWTBearerGrant(t *testing.T) {
t.Parallel()

Expand All @@ -3616,7 +3636,8 @@ func TestBuildTrustedIssuerRunConfigs_JWTBearerGrant(t *testing.T) {
Subject: "workload-123",
AllowedResources: []string{"https://mcp.example.com"},
}},
AcceptedAudiences: acceptedAudiences,
AcceptedAudiences: acceptedAudiences,
AcceptedAssertionTypes: []mcpv1beta1.JWTBearerAssertionType{mcpv1beta1.JWTBearerAssertionTypeIDJAG},
},
}})

Expand All @@ -3628,6 +3649,9 @@ func TestBuildTrustedIssuerRunConfigs_JWTBearerGrant(t *testing.T) {
AllowedResources: []string{"https://mcp.example.com"},
}}, configs[0].JWTBearerGrant.SubjectBindings)
assert.Equal(t, []string{"https://auth.example.com/legacy-token"}, configs[0].JWTBearerGrant.AcceptedAudiences)
assert.Equal(t, []tokenexchange.JWTBearerAssertionType{tokenexchange.JWTBearerAssertionTypeIDJAG},
configs[0].JWTBearerGrant.AcceptedAssertionTypes,
"legacy trustedIssuers[*].jwtBearerGrant.acceptedAssertionTypes must thread through to the runtime policy")

// The runtime policy must not retain the CRD object's backing slice: the
// reconciler may reuse or mutate the source object after conversion.
Expand Down
Loading
Loading