Documentation
¶
Overview ¶
core/spiffe/consumer.go
Package spiffe provides framework-shared SPIFFE credential helpers. Its consumers are the mTLS layer in core/tlsconfig with the proxy-sidecar listeners (X509Source), the token-exchange plugin (JWTSource), and the plugin framework, which injects *Provider into any plugin implementing ProviderConsumer; future LLM-judges or audit plugins that need workload identity can layer on top.
Both interfaces are declared here rather than inside the plugin that consumes them, so the framework Provider can hand them out without importing plugin-internal code.
Index ¶
Constants ¶
const DefaultMirrorDir = "/opt"
DefaultMirrorDir is the directory the mirror writes svid.pem, svid_key.pem, svid_bundle.pem, and (when MirrorJWT is invoked) jwt_svid.token into. /opt matches the historical spiffe-helper layout that any remaining file-reading consumers expect.
const DefaultSocketPath = "unix:///spiffe-workload-api/spire-agent.sock"
DefaultSocketPath is the conventional SPIRE Workload API UDS path inside an authbridge sidecar. The agent socket is bind-mounted under /spiffe-workload-api/ by the operator-injected spec; this default matches that contract.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JWTSource ¶
JWTSource is the per-fetch JWT-SVID interface satisfied by workloadJWT. It is declared in this framework package rather than inside token-exchange, its only consumer today, so Provider.JWTSource can hand it out without the framework importing plugin-internal code.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider holds the SDK Workload API clients, exposes them through the framework's X509Source / JWTSource interfaces, and runs the X.509 file-mirror goroutine when MirrorFiles is set. NewProvider blocks until the first X.509-SVID arrives, so a successful return guarantees the X509Source is immediately usable for TLS handshakes.
The JWT SDK client is opened lazily on the first JWTSource(audience) or MirrorJWT(ctx, audience) call — a Provider that no plugin ever asks for a JWT pays no SPIRE round-trip for it. Subsequent calls reuse the cached SDK client; new audiences just rebind the adapter.
One Provider per process is the intended deployment shape.
func NewProvider ¶
func NewProvider(ctx context.Context, cfg ProviderConfig) (*Provider, error)
NewProvider creates a Provider from cfg. It blocks until the first X.509-SVID arrives (the cold-start gate is delegated to workloadapi.NewX509Source's context). Pass a ctx with a deadline if you want to bound the agent-unreachable wait.
The JWT SDK client is NOT opened here — it's opened on the first JWTSource(audience) / MirrorJWT(ctx, audience) call.
On error every SDK client constructed up to that point is closed before returning, so the caller doesn't have to chase partial initialization.
func (*Provider) Close ¶
Close shuts down the mirror goroutines and both SDK sources. Order: cancel mirror first (so the rotation loops exit before their SDK sources go away), then close the SDK sources. Errors from the SDK Close calls are joined; idempotent calls return nil because every step is nil-guarded.
func (*Provider) JWTSource ¶
JWTSource returns a framework JWTSource adapter bound to the given audience. The first call also opens the underlying SDK workloadapi.JWTSource (cached on the Provider); subsequent calls reuse that single SDK client and just bind a fresh adapter to the requested audience. Returns an error only if the SDK client cannot be opened (Workload API unreachable).
func (*Provider) MirrorJWT ¶
MirrorJWT mirrors a JWT-SVID for the supplied audience to <MirrorDir>/jwt_svid.token. No-op when MirrorFiles is false.
Performs a synchronous initial write so the file is observable on return, then spawns a goroutine that refreshes at 90% of the token's lifetime (or every 30s on error). The goroutine respects the Provider's mirror context and exits on Close().
Single-audience contract: all audiences write to the SAME file path (<MirrorDir>/jwt_svid.token). A second call with a DIFFERENT audience returns an error rather than silently corrupting the first caller's file. A second call with the same audience is a no-op (idempotent).
func (*Provider) X509Source ¶
func (p *Provider) X509Source() X509Source
X509Source returns the framework X509Source adapter. Never nil after a successful NewProvider — TLS listeners can capture this once at startup and call Certificate / TrustBundle on every handshake.
type ProviderConfig ¶
type ProviderConfig struct {
// SocketPath is the SPIRE Workload API endpoint URL. Empty
// resolves to DefaultSocketPath.
SocketPath string
// MirrorFiles, when true, runs a background goroutine that
// mirrors the in-memory X.509 SVIDs to disk under MirrorDir.
// Defaults to true at the config layer above.
MirrorFiles bool
// MirrorDir is the directory the mirror writes to. Empty
// resolves to DefaultMirrorDir.
MirrorDir string
}
ProviderConfig parameterizes Provider construction.
Empty SocketPath / MirrorDir resolve to DefaultSocketPath / DefaultMirrorDir at NewProvider time.
MirrorFiles enables the file-mirror goroutine that copies the X.509 SVID + bundle to MirrorDir for legacy file-reading consumers. The wider config layer (the spiffe block in authbridge-runtime) defaults this to true, so callers that omit the field get on-by-default mirroring at the surface they actually touch.
JWT-SVID mirroring is plugin-driven: a plugin that needs an audience- specific token to be written to disk calls Provider.MirrorJWT. The framework no longer carries an audience here — only the tokenexchange plugin's spiffe identity path consumes the JWT-SVID.
type ProviderConsumer ¶
type ProviderConsumer interface {
SetSPIFFEProvider(p *Provider)
}
ProviderConsumer is implemented by plugins that need access to the process-wide SPIFFE Provider. plugins.BuildWithSPIFFE invokes SetSPIFFEProvider on every plugin that satisfies this interface before Configure runs, so plugin configuration code can use the Provider's sources directly.
Plugins that don't need SPIFFE simply don't implement this interface and are unaffected.
type X509Source ¶
type X509Source interface {
// Certificate returns the local SVID (cert + private key) for use
// in tls.Config.GetCertificate / GetClientCertificate.
Certificate() (*tls.Certificate, error)
// TrustBundle returns the SPIRE trust bundle for verifying peer
// certificates. Must be reloaded by the caller on every handshake
// to pick up bundle rotation.
TrustBundle() (*x509.CertPool, error)
}
X509Source produces the local X.509-SVID + trust bundle on demand. Implementations are responsible for hot-rotation handling — callers invoke Certificate / TrustBundle on every TLS handshake.