broker

package
v0.11.5 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 29 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 broker is the egress credential broker's engine: a reconciler-less reverse proxy that agent pods address directly (base-URL env overrides in the pod point the claude CLI at it) and that injects or signs the model credential outbound. It is what makes agent pods fully credential-less — the pod authenticates to the broker with an audience-bound projected ServiceAccount token (an identity document, not a capability), and the broker alone holds the Anthropic API key or the cloud workload identity that signs Bedrock/Vertex/Foundry traffic.

One route per upstream, keyed by path prefix (anthropic, bedrock, vertex, foundry). The route set is the extension seam for future brokered upstreams (forge-minted GitHub tokens, package registries). Every request is authenticated via TokenReview before any upstream contact, audited as a single slog line (never bodies or headers), and streamed through with immediate flushing so SSE responses survive multi-minute runs.

Index

Constants

View Source
const (
	// DefaultAudience is the projected-token audience callers must present.
	DefaultAudience = "patchy-egress-broker"
	// DefaultVerdictTTL bounds TokenReview QPS: one review per caller token
	// per TTL, not one per request.
	DefaultVerdictTTL = time.Minute
	// DefaultPingInterval is the SSE idle keep-alive period.
	DefaultPingInterval = 30 * time.Second
	// DefaultMaxRequestBytes caps a buffered (SigV4-signed) request body.
	DefaultMaxRequestBytes = 10 << 20
)

Default knob values, applied by Config.withDefaults.

View Source
const TokenHeader = provider.BrokerTokenHeader

TokenHeader carries the caller's projected ServiceAccount token. It is stripped before any byte is forwarded upstream: the token authenticates the caller to the broker and must never travel further.

Variables

This section is empty.

Functions

func ParseTarget

func ParseTarget(raw string) (*url.URL, error)

ParseTarget parses an upstream base URL, requiring an absolute http(s) URL.

Types

type Config

type Config struct {
	// Audience is the projected-token audience callers must present.
	Audience string
	// AgentNamespace/AgentServiceAccount pin the only identity the broker
	// answers to: system:serviceaccount:<AgentNamespace>:<AgentServiceAccount>.
	AgentNamespace      string
	AgentServiceAccount string
	// VerdictTTL is how long one token's TokenReview verdict is cached.
	VerdictTTL time.Duration
	// PingInterval is the SSE idle keep-alive period; 0 takes the default,
	// negative disables ping injection (pure passthrough).
	PingInterval time.Duration
	// MaxRequestBytes caps a buffered request body (BufferBody routes).
	MaxRequestBytes int64
	// Upstreams is the route table, keyed by path prefix
	// ("anthropic"/"bedrock"/"vertex"/"foundry").
	Upstreams map[string]Upstream
}

Config configures the broker engine.

type CredentialFunc

type CredentialFunc func(ctx context.Context, req *http.Request) error

CredentialFunc attaches the upstream credential to one outbound request: inject an API key header, attach an OAuth bearer, or SigV4-sign. It runs on the rewritten outbound request, after the caller's broker token and any inbound authorization headers have been stripped.

type Server

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

Server is the broker engine: authenticated, audited reverse proxies for the configured upstream routes.

func New

func New(cs kubernetes.Interface, cfg Config, log *slog.Logger) (*Server, error)

New builds a Server. cs performs the TokenReviews; the broker needs no other Kubernetes access of any kind.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler is the proxy surface: one subtree per configured route, everything under it requiring a valid caller token — plus the (unauthenticated) probe endpoints, so controllers can check readiness over the same Service URL agent pods proxy through.

func (*Server) HealthHandler

func (s *Server) HealthHandler() http.Handler

HealthHandler serves the probe endpoints on the health listener: healthz is liveness, readyz additionally requires every configured route's credential source to be usable — which is what replaces controller-side Secret probing as "is the model credential present". Neither touches the API server: TokenReview being down must fail requests, not probes.

type Upstream

type Upstream struct {
	// Target is the upstream base URL the route prefix maps onto.
	Target *url.URL
	// Credential attaches the upstream credential; nil forwards unmodified.
	Credential CredentialFunc
	// BufferBody buffers the request body in memory (bounded by
	// Config.MaxRequestBytes) before forwarding — required when Credential
	// hashes the payload, as SigV4 does.
	BufferBody bool
	// Ready reports whether the route's credential source is usable; readyz
	// fails while any configured route's is not. Nil means always ready.
	Ready func(ctx context.Context) error
}

Upstream is one brokered route: where it forwards and how it credentials.

func Anthropic

func Anthropic(target *url.URL, keyFile string, bearer bool) Upstream

Anthropic is the first-party route: inject the credential from a mounted Secret file and forward otherwise unchanged — the anthropic-version and anthropic-beta headers the CLI sends pass through, so prompt caching and beta features survive the hop. An API key rides x-api-key; bearer=true sends Authorization: Bearer instead, which is how a `claude setup-token` OAuth token authenticates. Either way the file is read per request, so a rotated Secret propagates without a restart.

func Bedrock

func Bedrock(ctx context.Context, target *url.URL, region string) (Upstream, error)

Bedrock is the Amazon Bedrock route: SigV4-sign each request with the broker's ambient AWS identity (IRSA / EKS Pod Identity / env credentials — whatever config.LoadDefaultConfig resolves) and forward. The route buffers bodies: SigV4 signs a hash of the payload, so the whole body must be in hand before the request leaves.

func FoundryEntra

func FoundryEntra(target *url.URL) (Upstream, error)

FoundryEntra is the Foundry route in Entra mode: attach a bearer token from the broker's ambient Azure identity (Workload Identity in-cluster), cached and refreshed broker-side — which is what fixes a static bearer expiring mid-run on long evaluation jobs.

func FoundryKey

func FoundryKey(target *url.URL, keyFile string) Upstream

FoundryKey is the Microsoft Foundry route in API-key mode: inject the key from a mounted Secret file, same rotation posture as the Anthropic route.

func Vertex

func Vertex(ctx context.Context, target *url.URL) (Upstream, error)

Vertex is the GCP Vertex AI route: attach an OAuth bearer from Application Default Credentials (GKE Workload Identity in-cluster) and forward. The token source caches and refreshes; each request reads the current token.

Jump to

Keyboard shortcuts

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