obo

package
v0.69.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package obo implements the proxy-runtime middleware for the on-behalf-of (OBO) external auth type.

Per request the middleware takes the OBO subject (assertion) token from the authenticated Identity that the authentication middleware placed in the request context, exchanges it through a cached Microsoft Entra on-behalf-of Exchanger for a backend-scoped token, sets Authorization: Bearer <backend token> on the upstream request, and calls the downstream handler. The subject comes from one of two Identity sources, selected by MiddlewareParameters.SubjectTokenProviderName: the inbound token the client presented (Identity.Token, the default) or a named upstream provider's token (Identity.UpstreamTokens[name], when an embedded auth server fronts the proxy). On a conditional-access or claims challenge it fails fast with a generic 403 and an audit record, and the downstream handler is not called.

The package, together with pkg/auth/obo/entra, pkg/auth/obo/cache, and pkg/auth/obo/exchange, is kept free of controller-runtime, client-go, and the operator API types, because the proxyrunner links it. A test in this package enforces that.

Index

Constants

View Source
const AuditEventType = "obo_token_exchange"

AuditEventType is the AuditEvent.Type for an OBO exchange decision. The proxy middleware and the vMCP strategy both emit it.

View Source
const ClientSecretEnvVar = "TOOLHIVE_OBO_CLIENT_SECRET"

ClientSecretEnvVar is the environment-variable name the operator injects the OBO confidential-client secret into for a proxyrunner pod (MCPServer and MCPRemoteProxy). A proxy carries at most one OBO config, so a fixed name is enough. The vMCP pod can carry several OBO backends and therefore uses a per-config name instead (see controllerutil.GenerateUniqueOBOSecretEnvVarName).

View Source
const MiddlewareType = "obo"

MiddlewareType is the type identifier used in MiddlewareConfig.Type for OBO middleware. Matches the ExternalAuthType constant value "obo".

Variables

View Source
var ErrEnterpriseRequired = errors.New(
	"on-behalf-of (OBO) external auth type requires an enterprise build")

ErrEnterpriseRequired was returned by the OBO dispatch points before OBO shipped in ToolHive itself. No code path in this module returns it any more.

Deprecated: OBO is built in. The sentinel is kept only so out-of-tree code that still references it keeps compiling; it will be removed once out-of-tree users are gone (see stacklok/toolhive#7200).

Functions

func CreateMiddleware

func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error

CreateMiddleware is the package-level OBO middleware factory registered in the runner's middleware factory map. It dispatches to DefaultFactory unless a replacement was registered through RegisterFactory.

func DefaultFactory

func DefaultFactory(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error

DefaultFactory is the built-in OBO middleware factory. It parses the MiddlewareParameters contract, reads the client secret from the named environment variable, builds the cached Entra Exchanger, and installs the middleware under the config's type name. It fails loudly at startup (returns an error, installs nothing) on malformed parameters or a missing secret rather than deferring the failure to the first request.

func NewCachedExchanger added in v0.69.0

func NewCachedExchanger(tokenURL, clientID, clientSecret string, cacheSkewSeconds int) (exchange.Exchanger, error)

NewCachedExchanger builds the Entra on-behalf-of Exchanger for tokenURL and the confidential client, fronted by the per-user token cache. The proxy middleware and the vMCP strategy both build theirs here, so the same configuration behaves the same on both paths.

cacheSkewSeconds overrides the cache's default expiry skew only when strictly positive; zero (unset, or a sub-second value truncated down) keeps the default. Building does no I/O.

func NewInteractionRequiredAuditEvent added in v0.69.0

func NewInteractionRequiredAuditEvent(
	source audit.EventSource, component string, identity *auth.Identity, target string, err error,
) *audit.AuditEvent

NewInteractionRequiredAuditEvent builds the audit event for an OBO exchange that Entra refused with a conditional-access or claims challenge. It records the denied subject (an identifier, not a credential) and only the non-sensitive fields the entra error carries: OAuthError, Description, HTTPStatus, and whether a claims challenge was present. It never records the user token, the client secret, an issued token, or the raw claims challenge.

Shared by the proxy middleware and the vMCP strategy so both surfaces emit the same record shape. identity may be nil.

func RegisterFactory deprecated

func RegisterFactory(f types.MiddlewareFactory)

RegisterFactory replaces the factory CreateMiddleware dispatches to. Last-write-wins. Panics if f is nil.

Deprecated: OBO is built in and DefaultFactory is the implementation. RegisterFactory is kept only so out-of-tree code that still calls it keeps compiling; it will be removed once out-of-tree users are gone (see stacklok/toolhive#7200).

Types

type MiddlewareParameters added in v0.69.0

type MiddlewareParameters struct {
	// TokenURL is the fully-formed Entra tenant token endpoint, e.g.
	// https://login.microsoftonline.com/<tenant>/oauth2/v2.0/token.
	//
	// SECURITY: TokenURL is an operator trust boundary. The OBO exchange POSTs
	// the client secret and the end-user assertion in the request body to this
	// URL, so the operator is responsible for pointing it only at a trusted
	// Entra token endpoint. See entra.NewFromTokenURL.
	TokenURL string `json:"tokenUrl"`

	// ClientID is the confidential client's application (client) ID.
	ClientID string `json:"clientId"`

	// Audience is the backend resource identifier (Application ID URI or client
	// ID). Used as the exchange scope <audience>/.default when Scopes is empty
	// (see ExchangeTarget).
	Audience string `json:"audience"`

	// Scopes are the delegated scopes to request, e.g.
	// ["api://<backend>/.default"]. When present they take precedence over
	// Audience (see ExchangeTarget).
	Scopes []string `json:"scopes"`

	// ClientSecretEnvVar is the name of the environment variable holding the
	// client secret. The operator injects the value into the proxyrunner pod;
	// only the name travels in the contract.
	ClientSecretEnvVar string `json:"clientSecretEnvVar"`

	// SubjectTokenProviderName selects the source of the OBO subject (assertion)
	// token from the request's authenticated Identity:
	//
	//   - Empty (default): use the inbound token the client presented
	//     (Identity.Token). This is the deployment with no embedded auth server,
	//     where the client holds an Entra token directly.
	//   - Set: use the named upstream provider's token
	//     (Identity.UpstreamTokens[SubjectTokenProviderName]). Set this when an
	//     embedded auth server fronts the proxy, so the inbound token is the
	//     proxy's own session token rather than an Entra token. Mirrors the
	//     upstreamswap middleware. The runner fills it in with the embedded auth
	//     server's upstream when it is left empty.
	//
	// Either way the subject comes from the Identity, so the authentication
	// middleware must run first.
	SubjectTokenProviderName string `json:"subjectTokenProviderName,omitempty"`

	// CacheSkewSeconds overrides the token cache's default expiry skew. Zero
	// leaves the cache default; Validate rejects a negative value and one above
	// math.MaxInt32, the bound the operator applies.
	CacheSkewSeconds int `json:"cacheSkewSeconds"`
}

MiddlewareParameters is the operator-to-runtime contract carried in the OBO middleware's MiddlewareConfig.Parameters. The operator marshals it and the proxy middleware factory unmarshals it, so the JSON key set is defined once.

It deliberately uses only standard-library types so this package stays free of the Kubernetes dependencies the proxyrunner must not link.

func (*MiddlewareParameters) ExchangeTarget added in v0.69.0

func (p *MiddlewareParameters) ExchangeTarget() string

ExchangeTarget returns the single value the Exchanger receives as its audience argument; see exchange.Target for the collapse rule.

func (*MiddlewareParameters) Validate added in v0.69.0

func (p *MiddlewareParameters) Validate() error

Validate checks the contract's required fields and bounds, failing loudly so a misconfiguration surfaces when the parameters are first consumed rather than per request. It does not validate TokenURL's URL shape; that is entra's concern (see entra.NewFromTokenURL).

type ValidationError added in v0.28.2

type ValidationError struct {
	Message string
}

ValidationError is the typed error OBO validation returns when its input is genuinely malformed and the user must fix the spec for the failure to clear. It is the contract for the "permanent, user-fix" bucket the operator's OBO error triage applies:

  • errors.As(err, &*ValidationError): permanent until the user edits the spec; the operator writes condition.Reason=InvalidConfig and does not requeue.
  • anything else: treated as transient by the reconciler, which returns the error so controller-runtime requeues with backoff.

Use ValidationError for any condition the user can fix by editing the spec (missing field, malformed URL, schema violation), and a plain error for transient I/O failures (Secret not yet available) so the reconciler retries instead of locking the resource into a permanent InvalidConfig state.

The Message field is written verbatim into condition.Message, so it must be safe to expose (no Secret names, no internal addressing, no credential fragments).

func (*ValidationError) Error added in v0.28.2

func (e *ValidationError) Error() string

Error returns the user-facing message verbatim.

Directories

Path Synopsis
Package cache adds a session-partitioned, in-memory token cache in front of any exchange.TokenExchanger.
Package cache adds a session-partitioned, in-memory token cache in front of any exchange.TokenExchanger.
Package entra implements the Microsoft Entra (Azure AD) variant of the on-behalf-of (OBO) token exchange.
Package entra implements the Microsoft Entra (Azure AD) variant of the on-behalf-of (OBO) token exchange.
Package exchange defines the provider-agnostic contract for an on-behalf-of (OBO) token exchange: the Exchanger and TokenExchanger interfaces, the Token result, and the rule that collapses an audience/scopes pair into the single exchange target a provider receives.
Package exchange defines the provider-agnostic contract for an on-behalf-of (OBO) token exchange: the Exchanger and TokenExchanger interfaces, the Token result, and the rule that collapses an audience/scopes pair into the single exchange target a provider receives.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL