Skip to content

Expose explicit primaryUpstreamProvider for Cedar authz on VirtualMCPServer #5197

Closed

Description

@tgrunnagle

Background

Sub-issue of #5146, addressing root cause 1: the operator auto-binds PrimaryUpstreamProvider to the first upstream provider's name without exposing a CRD field that lets users override or opt out of that choice.

cmd/thv-operator/pkg/vmcpconfig/converter.go:201-205 unconditionally sets:

if vmcp.Spec.AuthServerConfig != nil && len(vmcp.Spec.AuthServerConfig.UpstreamProviders) > 0 {
    incoming.Authz.PrimaryUpstreamProvider = authserver.ResolveUpstreamName(
        vmcp.Spec.AuthServerConfig.UpstreamProviders[0].Name,
    )
}

A user with multiple upstreams configured cannot point Cedar at a non-first provider, and there is no way to ask the controller to leave the field empty (Cedar's documented "fall back to ToolHive-issued token claims" path). The validator at virtualmcpserver_controller.go:570-645 already surfaces an AuthzUpstreamSelectionWarning advisory when multiple upstreams are configured, but offers no remediation knob beyond reordering the list.

The token-exchange and AWS-STS strategies on MCPExternalAuthConfig already follow the desired pattern: they expose an explicit SubjectProviderName on the CRD (mcpexternalauthconfig_types.go:148, :858) and the controller falls back to "first upstream" only when it's empty. This issue brings Cedar/authz to parity.

Goal

Add an optional primaryUpstreamProvider field to vMCP's incoming-auth inline authz config. When set, the controller honors it; when empty, the existing first-upstream auto-binding behavior is preserved verbatim so existing manifests continue to work.

Implementation plan

1. CRD type

Add field to the existing shared InlineAuthzConfig struct at mcpserver_types.go:765-776:

// PrimaryUpstreamProvider names the upstream IDP whose access token's claims
// Cedar should evaluate. Only meaningful for VirtualMCPServer with an embedded
// auth server. When empty and an embedded auth server has upstreams configured,
// the controller defaults to the first upstream provider.
// +optional
PrimaryUpstreamProvider string `json:"primaryUpstreamProvider,omitempty"`

The shared-type approach matches the precedent set by SubjectProviderName on the token-exchange and AWS-STS strategies — also defined on shared types but documented as vMCP-only and ignored by MCPServer/MCPRemoteProxy.

(Alternative: introduce a vMCP-specific VirtualMCPInlineAuthzConfig and a vMCP-specific AuthzConfigRef. More accurate typing, larger refactor with knock-on changes in the converter, controllerutil.GenerateOrUpdateInlineAuthzConfigMap, and tests. Recommend the shared-type approach unless reviewers prefer the stricter typing.)

2. Converter wiring

Replace the unconditional first-upstream assignment in converter.go:201-205 with explicit-then-fallback:

if vmcp.Spec.IncomingAuth.AuthzConfig.Inline != nil &&
    vmcp.Spec.IncomingAuth.AuthzConfig.Inline.PrimaryUpstreamProvider != "" {
    incoming.Authz.PrimaryUpstreamProvider = authserver.ResolveUpstreamName(
        vmcp.Spec.IncomingAuth.AuthzConfig.Inline.PrimaryUpstreamProvider,
    )
} else if vmcp.Spec.AuthServerConfig != nil && len(vmcp.Spec.AuthServerConfig.UpstreamProviders) > 0 {
    incoming.Authz.PrimaryUpstreamProvider = authserver.ResolveUpstreamName(
        vmcp.Spec.AuthServerConfig.UpstreamProviders[0].Name,
    )
}

Run ResolveUpstreamName on the user-supplied value too, so the field is normalized consistently with the auto-selected path.

3. Validation — validateAuthzUpstreamAvailable

Extend the validator at virtualmcpserver_controller.go:570-645:

  • Reject (SpecValidationError, AuthServerConfigValidated=False) when an explicit PrimaryUpstreamProvider is set but does not match any entry in Spec.AuthServerConfig.UpstreamProviders after ResolveUpstreamName normalization on both sides. Cedar would otherwise deny every request at runtime — fail loudly at admission instead.
  • Suppress the AuthzUpstreamSelectionWarning advisory when the user has set the field explicitly: with multiple upstreams and an explicit choice, no auto-selection is happening. Gate the existing len(upstreamProviders) > 1 branch on Inline.PrimaryUpstreamProvider == "".
  • Leave the existing "embedded AS but zero upstreams" reject path unchanged.

4. Tests

  • Converter — extend TestConvertIncomingAuth_PrimaryUpstreamProvider (converter_test.go:1902) with cases: explicit name with single upstream; explicit name overriding the first of multiple; explicit name routed through ResolveUpstreamName (empty user value falls into the auto-select branch — covered already); explicit name with no embedded AS (left as-is via the explicit branch).
  • Validator — add cases alongside the existing validateAuthzUpstreamAvailable tests: explicit name matching one of the upstreams (no warning, no error); explicit name not matching any upstream (SpecValidationError); explicit name + multi-upstream (no advisory warning emitted).

5. Generated artifacts

  • task operator-generate (deepcopy)
  • task operator-manifests (CRD YAML — both deploy/charts/operator-crds/files/ and templates/ copies)
  • task -d cmd/thv-operator crdref-gen (API docs)

6. Backward compatibility

Existing manifests without the new field keep current behavior — the converter's fallback branch is unchanged for that path. The advisory warning continues to fire on multi-upstream + no explicit choice, and now points users at a CRD knob to silence it.

Out of scope

Estimated size

~3 files of production code (mcpserver_types.go, converter.go, virtualmcpserver_controller.go) + tests + regenerated CRD/deepcopy/docs. Should fit a single PR under the 400-line / 10-file cap.

Activity

  1. added
    enhancement
    go
    kubernetes
    vmcp
    on May 5, 2026
  2. self-assigned this
    on May 5, 2026
  3. tgrunnagle commented on May 5, 2026

    @tgrunnagle
    CollaboratorAuthor

    @jhrozek What do you think of removing the "fallback to first upstream provider when none is specified in the CRD" logic for authz enforcement, and use the toolhive issued (or incoming) token by default? I think it's better behavior, but it is a breaking change.

  4. jhrozek commented on May 6, 2026

    @jhrozek
    Contributor

    @jhrozek What do you think of removing the "fallback to first upstream provider when none is specified in the CRD" logic for authz enforcement, and use the toolhive issued (or incoming) token by default? I think it's better behavior, but it is a breaking change.

    Not sure if I understand, would the toolhive issued token be useful at all for cedar policies? Would it have e.g. groups or roles?

  5. tgrunnagle commented on May 6, 2026

    @tgrunnagle
    CollaboratorAuthor

    It could have a subject, but yes you're right wouldn't have more details. I see the toolhive issued tokens wouldn't be useful for production- will keep the fallback logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

authauthorizationenhancementgokubernetesoperatorvmcp

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions