purser

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 3 modules, 4 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.

README

purser

Shared authentication and authorization for go-steer services: caller identity from SPIFFE X.509-SVIDs, standard-CA client certificates, and OIDC tokens.

A purser is the officer who checks who is aboard. That is the whole job here — turn a TLS connection or an HTTP request into a Caller, and answer whether that caller may perform an action.

Status: phase 1a complete. The identity contract, the test harness, the bearer-token table, both mTLS profiles — standard-CA and SPIFFE — the dialling half in client, the HTTP middleware, OIDC, and the authorization layer are in place, with a worked example under examples/ and a per-package coverage floor holding them there. Tagged v0.2.0 — pin that rather than a commit.

Pre-1.0: the exported API may break at any minor version. It has no consumers yet, and phase 2 — migrating core-agent and mast onto it — is the thing most likely to change it. Breaks are never silent: each one is reported by dev/tools/verify-apidiff, acknowledged in dev/api-breaks.txt, and listed under Changed or Removed in CHANGELOG.md.

Why it exists

Every go-steer service that exposes an HTTP/SSE surface needs the same two layers, and today they each solve them differently or not at all:

  • Connection admission — should this TLS peer be allowed to connect?
  • Caller identity — who is on the other end, so the request can be attributed, authorized, and audited?

core-agent ships the second layer as a static bearer-token → identity table, which does not scale past a handful of operators. mast carries an undrifted fork of that code. k8s-lookout has no answer at all. Extracting the layer once, with real identity sources behind it, is cheaper than four divergent copies — and a standalone module is far easier to test at the component level than a package buried in a daemon.

Consumers

Repo Role
core-agent server (attach endpoint), client
mast server — replaces its pkg/auth fork
core-tui client
switchboard client
k8s-lookout server
mast-web server (browser edge), client — authenticates the human, then asserts them to core-agent

Packages

Package Contents
purser Caller, AuthSource, the sentinel errors, and the context plumbing. Stdlib-only and depends on no other purser package, so a client that merely reads an identity off a context links nothing else.
authn The Authenticator contract and its implementations. Each credential type lands in its own subpackage.
authn/bearer The static token table, kept for compatibility with deployments running one today. It is the thing purser exists to replace, not the thing to reach for in a new deployment.
authn/mtls Caller identity from a client certificate, in two profiles: NewPKI for certificates from a standard CA, NewSPIFFE for X.509-SVIDs. Each constructor returns a *tls.Config and the Authenticator that understands the connections it admits, together — the two are one decision, and the profiles verify with opposite crypto/tls idioms. Also the connection-admission matchers for both, anchored so that a rule naming one namespace cannot be widened into one naming several.
authn/oidc Caller identity from an OpenID Connect ID token — the human-operator path, so an engineer's morning sign-in authenticates them without anyone issuing a certificate for their laptop. New does no network I/O; discovery and the key fetch happen on the first token. The key cache refetches both on an unseen key ID and on age, under a floor that keeps an unauthenticated caller from turning this service into an amplifier pointed at its own IdP. Caller.Admin is never read from a claim.
httpmw The serving half: NewCaller puts the Caller and the auth-source verdict on the request context, NewBrowserWriteGuard closes the browser CSRF vectors against a JSON API, NewTokenGate is the shared transport token, and CheckBind refuses to bind a network address unless something in that chain reports that it enforces credentials. Stdlib-only; it learns everything about a credential through the authn interfaces.
authz The deciding half: an ACL and the Admin / Owner / Contributor / Viewer matrix for one resource, plus Rules — named matchers over identity, email domain, SPIFFE path segments and labels that grant the Admin bit and the right to proxy, replacing the exact-match identity lists that scale no better than the token table. WithRules applies a rule set to any Authenticator. Stdlib-only, and it names no resource type: the vocabulary is the consuming service's.
client The dialling half of authn/mtls: NewPKI and NewSPIFFE return a *tls.Config that pairs with a listener from the matching server constructor, and Transport wraps one in an http.Transport that keeps HTTP/2.
authtest An in-memory CA, an in-process OIDC issuer, and RunAuthenticatorSuite, the conformance suite every Authenticator — here and in consuming repos — is expected to pass.

Example

examples/ is a hello-world REST/JSON server and client deployed four ways — locally, on Kubernetes with cert-manager, on Kubernetes with SPIRE, and on GKE with managed workload identity — over both mTLS profiles. Every cell deploys two clients, one that is served and one holding an equally valid certificate that is refused during the handshake.

examples/local/demo    # no root, no network, no infrastructure

That script is also the smoke step of dev/tools/ci. purser itself ships no binary and no image; the examples are a demonstration and a test, not a product.

Design

docs/DESIGN.md is the design of record — the phase plan, the decisions and why they went that way, and the properties the tests exist to pin. Read it before adding to the API surface. In short:

  • Two mTLS profiles — standard PKI and SPIFFE — verified with opposite crypto/tls idioms, behind one matched-pair constructor that returns a *tls.Config and the authenticator that understands it.
  • Connection-layer authorization and identity extraction are separate concerns. Identity is always extracted and passed on, whatever the connection authorizer decided.
  • SPIFFE is the goal, but nothing may require it: certificates from a standard CA are a first-class path.

Development

dev/tools/ci    # every check CI runs, in fast-fail order

See dev/README.md for the tooling layout and AGENTS.md for the conventions this repo is developed under.

License

Apache 2.0 — see LICENSE.

Documentation

Overview

Package purser is the root of go-steer's shared authentication and authorization module: caller identity derived from SPIFFE X.509-SVIDs, standard-CA client certificates, and OIDC tokens.

The root package holds only what every consumer needs and nothing can be defined without — the Caller identity, the AuthSource verdict, the sentinel errors, and the context plumbing that carries them. It is stdlib-only and depends on no other purser package, so it can be imported by a client that merely reads an identity off a context.

Everything that verifies a credential lives in a subpackage, so a consumer links only the machinery it uses: authn for the Authenticator contract and its implementations, authz for the authorization matrix, httpmw for the middleware, client for outbound credentials, and authtest for the conformance suite and an in-memory CA.

See README.md for the layout and docs/DESIGN.md for the design.

Index

Constants

View Source
const AnonymousIdentity = "anon"

AnonymousIdentity is the Identity of the conventional unauthenticated Caller. Compare against it rather than against a Caller value: Caller contains a map and so is not comparable with ==.

Variables

View Source
var ErrAssertedCallerForbidden = errors.New("purser: caller is not permitted to assert identities")

ErrAssertedCallerForbidden is returned when a Caller that is not permitted to proxy attempts to assert another identity. Surfaces map it to 401 and should log the attempt: it indicates either misconfiguration or a credential that should not be assertable.

View Source
var ErrAssertedCallerUnknown = errors.New("purser: asserted identity is not provisioned")

ErrAssertedCallerUnknown is returned when a proxy-permitted Caller asserts an identity that is not provisioned. Surfaces map it to 401.

View Source
var ErrUnauthenticated = errors.New("purser: unauthenticated")

ErrUnauthenticated is returned by an Authenticator when no valid credential is present on the request. HTTP surfaces map it to 401.

It deliberately does not distinguish "no credential presented" from "credential presented but invalid": the difference is useful to an attacker probing which identities exist and useless to a legitimate client, which must retry with a good credential either way.

Functions

func ProxyByFromContext

func ProxyByFromContext(ctx context.Context) (by string, ok bool)

ProxyByFromContext returns the proxying identity stored on ctx by WithProxyBy. ok is false, and the string empty, when the request did not take the proxy path.

func WithAuthSource

func WithAuthSource(ctx context.Context, s AuthSource) context.Context

WithAuthSource returns a copy of ctx carrying the verdict on how the request was authenticated. Only code that performed or directly observed the verification may call this.

func WithCaller

func WithCaller(ctx context.Context, c Caller) context.Context

WithCaller returns a copy of ctx carrying c. Called by the middleware that resolved the request's Caller; downstream code reads it back with CallerFromContext.

func WithProxyBy

func WithProxyBy(ctx context.Context, by string) context.Context

WithProxyBy returns a copy of ctx carrying the proxying identity alongside the effective Caller, so audit records capture both: the human the request acts for and the credential that asserted them (effective="alice@example.com", proxy_by="sa:slack-bot").

Pair it with WithCaller, which carries the effective identity.

Types

type AuthSource

type AuthSource string

AuthSource names the class of credential a server actually verified for a request. It answers an operator's "how did this request get in?" — which is a different question from "who is this?" (Caller) and from "may they do this?" (authorization).

The critical property: an AuthSource is stamped by the code that performed the verification, and is never re-derived from request headers. A value inferred from a header is a value any client can forge, and a forgeable "how did they authenticate" field is worse than no field at all, because it is trusted.

The string values are wire-visible (they surface on core-agent's /whoami) and are part of the compatibility contract.

const (
	// AuthSourceAnonymous — the server verified no credential and the
	// surface allowed the request through. This also covers requests
	// that carried credential-looking headers the server had no
	// validator for: presenting a credential nobody checked is
	// indistinguishable from presenting none.
	AuthSourceAnonymous AuthSource = "anonymous"

	// AuthSourceBearer — the server validated a bearer token, either
	// against the static table or at a transport-level bearer gate.
	// The mere presence of an Authorization header does not produce
	// this value.
	AuthSourceBearer AuthSource = "bearer"

	// AuthSourceMTLS — the server's own TLS stack verified the peer's
	// certificate chain against the configured CA pool (the PKI
	// profile: ClientAuth = RequireAndVerifyClientCert), and the
	// identity was read from the configured subject field. A merely
	// presented-but-unverified certificate never produces this value.
	AuthSourceMTLS AuthSource = "mtls"

	// AuthSourceSPIFFE — the peer presented an X.509-SVID that
	// verified against the SPIFFE trust bundle, and the identity is
	// the SVID's URI SAN.
	//
	// Distinct from AuthSourceMTLS on purpose. Both are mutual TLS,
	// but they verify by opposite means and can be configured
	// independently, so collapsing them would leave an operator unable
	// to tell which listener profile admitted a caller.
	AuthSourceSPIFFE AuthSource = "spiffe"

	// AuthSourceOIDC — the server validated a signed OIDC/JWT token
	// against the issuer's published keys, including audience and
	// expiry.
	AuthSourceOIDC AuthSource = "oidc"

	// AuthSourceGoogleAccessToken — the server validated an opaque
	// Google OAuth access token by asking Google (tokeninfo): unexpired,
	// issued to one of the server's own OAuth clients, carrying the
	// scopes it requires, for a verified account.
	//
	// Distinct from AuthSourceOIDC because no signature was checked: a
	// Google access token is not a JWT, and its validity is Google's
	// answer at lookup time, not a local verification against published
	// keys. Distinct from AuthSourceBearer because the token is the
	// caller's own Google credential, not an entry in a table the server
	// provisioned, and the server may go on to act as the caller with it.
	// Emitted by github.com/go-steer/steward.
	AuthSourceGoogleAccessToken AuthSource = "google-access-token"

	// AuthSourceAsserted — a proxy-permitted credential asserted
	// another identity and the server validated the assertion. The
	// asserted path is the audit-relevant source and outranks the
	// underlying credential's own class; the proxying identity is
	// recorded separately (see WithProxyBy).
	AuthSourceAsserted AuthSource = "asserted"

	// AuthSourceIAP — reserved for a verified identity-gateway
	// assertion (Google IAP, Cloudflare Access, a service-mesh
	// sidecar). Not emitted yet: it may only be stamped once the
	// server validates a gateway's signed assertion, never inferred
	// from the gateway's plaintext headers, which any client that can
	// reach the port may set.
	AuthSourceIAP AuthSource = "iap"

	// AuthSourceProxyHeader — the server read the identity from a
	// header set by a fronting proxy and verified no credential of its
	// own. This is the weakest source purser defines, and the only one
	// whose trustworthiness rests entirely on deployment topology
	// rather than on cryptography.
	//
	// It is worth naming rather than leaving consumers to mis-stamp.
	// The oauth2-proxy / X-Forwarded-Email shape is the most common
	// hosted deployment of anything behind an authenticating load
	// balancer, and a server in that mode really has resolved a
	// specific person and really will attribute their actions to them
	// — so AuthSourceAnonymous is untrue. But it validated no
	// signature, so AuthSourceIAP (which requires exactly that) is
	// forbidden, and AuthSourceAsserted (which presumes a
	// proxy-permitted credential underneath) has nothing to be
	// permitted. An audit record reading "proxy-header" is a truthful
	// statement about how much the identity is worth.
	//
	// Stamping it is a claim about the deployment, not about the
	// request: that the fronting proxy strips client-supplied copies
	// of the header, and that nothing else can reach the port. Neither
	// is checkable from inside this process. A consumer that can
	// verify a gateway's signature should stamp AuthSourceIAP instead,
	// and downstream policy is entitled to treat this value as less
	// trustworthy than any verified one.
	//
	// Note that this counts as authenticated everywhere purser tests
	// for it, which is only AuthSourceAnonymous — so an authenticator
	// reporting it may be wrapped by authz.WithRules and may reach
	// httpmw's proxy-assertion path. Both are intended (mapping a
	// header identity onto rules is the point), but they mean the
	// operator's topology promise is load-bearing for authorization,
	// not just for attribution.
	AuthSourceProxyHeader AuthSource = "proxy-header"
)

func AuthSourceFromContext

func AuthSourceFromContext(ctx context.Context) (s AuthSource, ok bool)

AuthSourceFromContext returns the verdict stored on ctx by WithAuthSource. ok is false when no authentication middleware ran; treat absence as AuthSourceAnonymous, since nothing was verified.

func (AuthSource) Known

func (s AuthSource) Known() bool

Known reports whether s is one of the sources purser defines.

Authenticators live outside this package, including in consuming repos, so an unknown source is a real possibility rather than an impossible state; it means some authenticator invented a value the rest of the stack cannot interpret. The conformance suite rejects it.

func (AuthSource) String

func (s AuthSource) String() string

String implements fmt.Stringer.

type Caller

type Caller struct {
	Identity string
	Labels   map[string]string
	Admin    bool
}

Caller is the identity attached to every request entering an authenticated surface. It is the output of an authn.Authenticator and the input to authorization and audit.

Identity is a stable opaque ID. Its shape depends on the credential that produced it: an email ("alice@example.com") for OIDC or an email-SAN client certificate, a SPIFFE ID ("spiffe://example.org/ns/prod/sa/api") for the SPIFFE mTLS profile, a service-account marker ("sa:platform-agent") for the bearer table, or "anon" for unauthenticated requests where anonymous access is allowed. Identity is the only field compared for equality; consumers must not parse it to recover structure that Labels already carries.

Labels carry free-form metadata from the credential — the OIDC issuer and residual claims, a certificate's issuer DN and serial, a SPIFFE trust domain and path. They are available to authorization rules and audit consumers.

Labels must be treated as read-only by everything downstream of the authenticator. An authenticator that resolves a Caller from a static table would otherwise hand every request a pointer into its own state, where one consumer's mutation silently rewrites another caller's metadata. Authenticators that retain their Labels maps return Clone; consumers that need to add a label call Clone themselves. The authtest conformance suite checks this.

Admin grants the see-everything role. It is set by the consuming service's policy (authz rules), never by a credential's own claim — a token that could assert its own admin bit would be a privilege escalation.

func Anonymous

func Anonymous() Caller

Anonymous returns the conventional Caller for unauthenticated requests where anonymous access is allowed. Consumers may configure a different default identity, but "anon" is the documented one.

It is a function rather than a package-level variable so that no importer can reassign the identity every unauthenticated request in the process resolves to. Each call returns a fresh value carrying no Labels, so there is no shared map to alias either.

func CallerFromContext

func CallerFromContext(ctx context.Context) (c Caller, ok bool)

CallerFromContext returns the Caller stored on ctx by WithCaller. ok is false when no Caller is present — a code path reached before the authentication middleware, or a handler exercised directly in a test. Absence must be treated as unauthenticated, never as a permissive default.

func (Caller) Clone

func (c Caller) Clone() Caller

Clone returns a deep copy of c: the returned Caller shares no map with the receiver, so the caller may mutate its Labels freely. Cloning a Caller with nil Labels yields nil Labels.

func (Caller) IsZero

func (c Caller) IsZero() bool

IsZero reports whether c carries no identity. A zero Caller is never a valid authentication result — an authenticator that returns one alongside a nil error has a bug, which the authtest conformance suite rejects.

func (Caller) Label

func (c Caller) Label(key string) string

Label returns the value of the named label, or "" when absent. A convenience over indexing a possibly-nil map.

Directories

Path Synopsis
Package authn defines the contract every purser authenticator implements: turn an inbound HTTP request into a purser.Caller, or report that no valid credential was present.
Package authn defines the contract every purser authenticator implements: turn an inbound HTTP request into a purser.Caller, or report that no valid credential was present.
bearer
Package bearer authenticates callers against a static table of tokens.
Package bearer authenticates callers against a static table of tokens.
mtls
Package mtls authenticates callers by the client certificate they presented on the TLS connection.
Package mtls authenticates callers by the client certificate they presented on the TLS connection.
oidc
Package oidc authenticates callers by OpenID Connect ID token.
Package oidc authenticates callers by OpenID Connect ID token.
Package authtest provides the test harness every purser authenticator is held to: an in-memory certificate authority and a conformance suite.
Package authtest provides the test harness every purser authenticator is held to: an in-memory certificate authority and a conformance suite.
Package authz decides what an authenticated caller may do.
Package authz decides what an authenticated caller may do.
Package client dials purser-authenticated services.
Package client dials purser-authenticated services.
examples
cmd/hello-client command
Command hello-client calls hello-server over mutual TLS and prints the identity the server saw.
Command hello-client calls hello-server over mutual TLS and prints the identity the server saw.
cmd/hello-server command
Command hello-server serves one mutually authenticated JSON endpoint and reports who called it.
Command hello-server serves one mutually authenticated JSON endpoint and reports who called it.
cmd/mint-credentials command
Command mint-credentials writes a throwaway CA and the certificates the local examples need.
Command mint-credentials writes a throwaway CA and the certificates the local examples need.
internal/credentials
Package credentials mints the throwaway certificates the local examples run on.
Package credentials mints the throwaway certificates the local examples run on.
internal/hello
Package hello is the REST/JSON service the purser examples authenticate.
Package hello is the REST/JSON service the purser examples authenticate.
internal/profile
Package profile turns example command-line flags into the matched pair purser hands out: a *tls.Config and, on the server side, the authenticator that understands the connections it admits.
Package profile turns example command-line flags into the matched pair purser hands out: a *tls.Config and, on the server side, the authenticator that understands the connections it admits.
Package httpmw is the HTTP layer that turns purser's authenticators into a serving surface: it resolves the Caller onto the request context, stamps how the request was authenticated, guards the browser CSRF vectors, gates a shared transport token, and answers whether an address is safe to bind.
Package httpmw is the HTTP layer that turns purser's authenticators into a serving surface: it resolves the Caller onto the request context, stamps how the request was authenticated, guards the browser CSRF vectors, gates a shared transport token, and answers whether an address is safe to bind.
internal
ca
Package ca is an in-memory certificate authority for tests and examples.
Package ca is an in-memory certificate authority for tests and examples.
tlsfloor
Package tlsfloor holds purser's minimum TLS version policy.
Package tlsfloor holds purser's minimum TLS version policy.

Jump to

Keyboard shortcuts

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