resourceproxy

package
v1.88.3 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 24 Imported by: 0 modules, 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 resourceproxy is the machinery of cmd/resource-proxy: a sidecar that puts a sluis resource server's front door on a stock MCP server (or any HTTP service) that knows nothing about sluis.

Two sides, independent:

  • Inbound faces the gateway. It verifies the caller's access token (identity/resource), serves the resource's RFC 9728 metadata, writes one audit line per request, and reverse-proxies to the stock server with the caller's Authorization header removed.
  • Outbound is optional and faces the stock server, on loopback only. The stock server points its backend URL at it; it adds a bearer token this workload's OWN identity earned by an RFC 8693 exchange. The caller's token is never forwarded anywhere (the MCP spec forbids passing it through), so the stock server holds no credential at all.

The outbound side has two modes. `workload`, the default, calls the backend as this workload. `delegate` calls it as the caller: the inbound side puts an opaque handle for the request on the header DelegationHeader, the stock server passes that header through to the outbound side, and the outbound side exchanges the caller's token, with this workload's own token as the actor (RFC 8693 delegation), for a token the backend accepts. The handle is good only while the inbound request is in flight, and only in this process; the caller's token itself never leaves the proxy.

Index

Constants

View Source
const (
	// ModeWorkload calls the backend as this workload.
	ModeWorkload = "workload"
	// ModeDelegate calls the backend as the caller of the request the stock
	// server is serving, with this workload named as the actor.
	ModeDelegate = "delegate"
)

The outbound modes.

View Source
const (
	DefaultMaxRequestBytes = 4 << 20
	DefaultBodyTimeout     = 30 * time.Second
	DefaultUpstreamTimeout = 2 * time.Minute
	DefaultRefreshBefore   = time.Minute
	// DefaultDelegationHeader is what the stock server must pass through
	// (for the VictoriaMetrics MCP servers, MCP_PASSTHROUGH_HEADERS).
	DefaultDelegationHeader    = "X-Sluis-Delegation"
	DefaultDelegationCacheSize = 1024
)

Default limits.

Variables

This section is empty.

Functions

func IsLoopbackListen

func IsLoopbackListen(addr string) bool

IsLoopbackListen reports whether a listen address binds loopback only. A bare port (":8429") binds every interface and is not loopback.

func Run

func Run(ctx context.Context, cfg Config, log *slog.Logger) error

Run starts the listeners and blocks until ctx is done, then drains them. A listener that fails to start, or dies, ends the process: a sidecar with half its sides gone should be restarted, not trusted.

Types

type Config

type Config struct {
	// Listen is the inbound listener.
	Listen string
	// Upstream is the stock server. Its path, if any, is where the
	// resource's own path maps to: upstream http://127.0.0.1:8081/mcp for
	// resource https://h/metrics sends /metrics to /mcp and /metrics/x to
	// /mcp/x.
	Upstream string
	// IssuerURL, ResourceURL and Scope configure identity/resource.
	IssuerURL   string
	ResourceURL string
	Scope       string

	// MaxRequestBytes caps a request body; more is refused with 413.
	MaxRequestBytes int64
	// BodyReadTimeout bounds reading one request body.
	BodyReadTimeout time.Duration
	// UpstreamTimeout bounds the wait for the stock server's response
	// headers. A streamed response is not bounded by it.
	UpstreamTimeout time.Duration

	// Outbound side. Empty OutboundListen turns it off.
	OutboundListen        string
	OutboundTarget        string
	OutboundTokenEndpoint string
	OutboundClientID      string
	OutboundAudience      string
	OutboundSATokenFile   string
	// OutboundCAFile is a PEM bundle appended to the system roots for the
	// outbound forwarder's connection to OutboundTarget, and only that
	// one: the issuer and the token exchange keep the system roots alone.
	OutboundCAFile string
	// OutboundAllowNonLoopback lets the outbound listener bind anything
	// but loopback. Anyone who can reach it borrows this workload's
	// identity, so it is refused unless asked for by name.
	OutboundAllowNonLoopback bool
	// RefreshBefore is how long before expiry a token is exchanged again.
	RefreshBefore time.Duration

	// OutboundMode is [ModeWorkload] (empty) or [ModeDelegate].
	OutboundMode string
	// DelegationHeader carries a request's handle from the inbound side,
	// through the stock server, to the outbound side in [ModeDelegate].
	DelegationHeader string
	// DelegationCacheSize bounds how many callers' delegated tokens are
	// kept at once in [ModeDelegate].
	DelegationCacheSize int
}

Config is everything the proxy is told. cmd/resource-proxy fills it from flags, each defaulting to an environment variable.

func (Config) Delegating added in v1.88.0

func (c Config) Delegating() bool

Delegating reports whether the outbound side acts for the caller.

func (Config) OutboundEnabled

func (c Config) OutboundEnabled() bool

OutboundEnabled reports whether the outbound side runs.

func (*Config) Validate

func (c *Config) Validate() error

Validate refuses a configuration that cannot work, or that works unsafely, before anything listens.

type Delegator added in v1.88.0

type Delegator struct {
	// contains filtered or unexported fields
}

Delegator trades a caller's token for one the backend accepts, acting for them with this workload's own ServiceAccount token as the actor (RFC 8693 delegation), and keeps what it was given per caller token until shortly before it expires.

A cached token is keyed by the SHA-256 of the caller's token, never by the token: the cache holds the delegated tokens, which are the backend's, and no caller's credential. It holds at most size entries; a full cache drops the expired ones first and then the one that expires soonest.

func NewDelegator added in v1.88.0

func NewDelegator(cfg Config, client *http.Client) (*Delegator, error)

NewDelegator builds the delegator from a validated Config. client is the transport to the issuer; nil uses tokens' default.

func (*Delegator) Ready added in v1.88.0

func (d *Delegator) Ready() error

Ready reports whether the actor token can be read: what readiness means for a delegating outbound side, which holds no token of its own until a caller arrives.

func (*Delegator) Token added in v1.88.0

func (d *Delegator) Token(ctx context.Context, callerToken string) (string, bool, error)

Token returns a token for the backend that acts for the holder of callerToken, and whether it came from the cache.

type Handles added in v1.88.0

type Handles struct {
	// contains filtered or unexported fields
}

Handles are the inbound requests in flight in ModeDelegate, each under an opaque handle the stock server carries to the outbound side. A handle is 256 random bits, names nothing, and is gone when its request ends, so the stock server holds nothing it could use after the request, or anywhere but on this process's loopback.

func NewHandles added in v1.88.0

func NewHandles() *Handles

NewHandles is an empty set of handles.

func (*Handles) Open added in v1.88.0

func (h *Handles) Open(token, subject string) (string, func())

Open registers one request's caller and returns its handle, and the func that ends it.

type Inbound

type Inbound struct {
	// contains filtered or unexported fields
}

Inbound is the public side.

func NewInbound

func NewInbound(cfg Config, log *slog.Logger, extraReady func() error) (*Inbound, error)

NewInbound builds the inbound side from a validated Config.

func (*Inbound) Delegate added in v1.88.0

func (in *Inbound) Delegate(handles *Handles)

Delegate has every proxied request carry a handle to its caller on Config.DelegationHeader, for the outbound side to act for them.

func (*Inbound) Handler

func (in *Inbound) Handler() http.Handler

Handler is the whole public listener.

func (*Inbound) Resource

func (in *Inbound) Resource() *resource.Resource

Resource is the verifier and metadata the inbound side is built on.

type Outbound

type Outbound struct {
	// contains filtered or unexported fields
}

Outbound is the loopback forwarder: whatever reaches it is sent to the target with this workload's own bearer token.

func NewOutbound

func NewOutbound(cfg Config, log *slog.Logger, source *TokenSource) (*Outbound, error)

NewOutbound builds the forwarder from a validated Config.

func (*Outbound) ActFor added in v1.88.0

func (o *Outbound) ActFor(handles *Handles, header string, delegator *Delegator)

ActFor turns the forwarder to ModeDelegate: each request is sent as the caller of the inbound request its handle names, and a request naming none is refused.

func (*Outbound) Handler

func (o *Outbound) Handler() http.Handler

Handler is the loopback listener.

type TokenSource

type TokenSource struct {
	// contains filtered or unexported fields
}

TokenSource is this workload's own identity, as a bearer token: its projected ServiceAccount token traded at the issuer (RFC 8693) for one audienced at the backend. The kubelet rotates the ServiceAccount token under the pod, so it is read afresh for every exchange, never cached.

func NewTokenSource

func NewTokenSource(cfg Config, log *slog.Logger, client *http.Client) (*TokenSource, error)

NewTokenSource builds the source from a validated Config. client is the transport to the issuer; nil uses tokens' default.

func (*TokenSource) Obtained

func (s *TokenSource) Obtained() bool

Obtained reports whether a token has ever been obtained: what readiness means for the outbound side.

func (*TokenSource) Token

func (s *TokenSource) Token(ctx context.Context) (string, error)

Token returns a bearer token with at least the refresh margin left, exchanging when it has none. One exchange at a time: a burst of calls that all find the token stale waits for one answer rather than making one each.

A failed exchange while the previous token is still valid serves the previous token and tries again next call: a blip at the issuer is not an outage of the backend.

func (*TokenSource) Warm

func (s *TokenSource) Warm(ctx context.Context)

Warm tries until the first token is obtained, so readiness does not wait for the first request, and stops on ctx.

Jump to

Keyboard shortcuts

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