Documentation
¶
Overview ¶
Package secrets defines the SecretProvider interface that agentmon uses to fetch real credentials from external secret stores at session start, plus the URI grammar, sentinel errors, and the provider Registry shared by all provider implementations.
The Registry (registry.go) constructs providers in dependency order via topological sort, enabling auth chaining where one provider's bootstrap credentials come from another (e.g. Vault reads its token from the OS keyring).
Provider implementations live in subpackages, one per backend:
- internal/proxy/secrets/keyring — OS keyring (Keychain / Secret Service / Credential Manager) via github.com/zalando/go-keyring.
- internal/proxy/secrets/vault — HashiCorp Vault / OpenBao KV v2 via github.com/hashicorp/vault/api.
Future plans add awssm, gcpsm, azurekv, and op subpackages. Every provider imports this package for the interface and types; this package imports none of them.
Test doubles live in the sibling secretstest package. Production code must not import secretstest.
The design is documented in docs/superpowers/specs/2026-04-09-plan-04-vault-provider-registry-design.md and the parent migration spec docs/superpowers/specs/2026-04-07-external-secrets-design.md.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidFakeFormat is returned by ParseFormat when the // format template is syntactically invalid (empty, missing // {rand:N} placeholder, placeholder not at end, etc.). ErrInvalidFakeFormat = errors.New("secrets: invalid fake format template") // ErrFakeLengthMismatch is returned by GenerateFake when the // total length produced by the format (len(prefix) + randLen) // does not equal the real secret's length. ErrFakeLengthMismatch = errors.New("secrets: fake format length does not match real secret length") // ErrFakeEntropyTooLow is returned by ParseFormat when the // random portion of the format has fewer than 24 characters, // which would make collisions or brute-force feasible. ErrFakeEntropyTooLow = errors.New("secrets: fake format has fewer than 24 random characters") )
var ErrCyclicDependency = errors.New("secrets: cyclic provider dependency")
ErrCyclicDependency is returned by NewRegistry when the provider dependency graph contains a cycle. The error message lists all configs that could not be resolved (cycle members and their downstream dependents).
var ErrFieldNotSupported = errors.New("secrets: field not supported")
ErrFieldNotSupported is returned by a provider when a SecretRef carries a non-empty Field but the provider's backend stores scalar values only (e.g. OS keyring entries are single-valued, unlike Vault KV-v2 entries which are field-addressable).
var ErrInvalidURI = errors.New("secrets: invalid URI")
ErrInvalidURI is returned by ParseRef when a URI is syntactically malformed (empty string, bad scheme delimiter, missing host, query string present, userinfo present). Also returned by a provider's Fetch when a SecretRef is missing required semantic pieces for that backend.
ErrKeyringUnavailable is returned by the keyring provider's New constructor when the OS keyring backend cannot be reached at all (D-Bus not running, Keychain access denied at the OS level, Windows Credential Manager subsystem unavailable). It is a hard error; the operator must either set up the keyring or use a different provider.
var ErrNotFound = errors.New("secrets: not found")
ErrNotFound is returned when a Fetch targets a secret that does not exist in the backend. Callers should branch on errors.Is(err, ErrNotFound) to honor policy like on_missing.
ErrUnauthorized is returned when the backend rejects the provider's own credentials (bad token, expired lease, etc.). Distinct from ErrKeyringUnavailable, which means the backend itself is unreachable.
var ErrUnsupportedScheme = errors.New("secrets: unsupported scheme")
ErrUnsupportedScheme is returned by ParseRef when the URI scheme is syntactically valid but not one of the six v1 schemes (vault, aws-sm, gcp-sm, azure-kv, op, keyring).
Functions ¶
func GenerateFake ¶
GenerateFake produces a fake credential from a format template. The generated credential has the same length as realLen (the real secret's byte length) with the literal prefix preserved and the random portion filled with crypto/rand base62 characters.
Returns ErrFakeLengthMismatch when len(prefix)+randLen != realLen.
func ParseFormat ¶
ParseFormat validates a fake credential format template and returns its components. The format syntax is "<prefix>{rand:<count>}" where prefix is a literal string (may be empty) and {rand:N} emits N random base62 characters. The {rand:N} placeholder must appear exactly once and must be the last element in the format string.
Returns ErrInvalidFakeFormat for syntactic problems and ErrFakeEntropyTooLow when count is below minFakeEntropy (24).
Types ¶
type ConstructorFunc ¶
type ConstructorFunc func(ctx context.Context, cfg ProviderConfig, resolver RefResolver) (SecretProvider, error)
ConstructorFunc builds a SecretProvider from its config. The resolver can fetch secrets from providers that have already been constructed (for auth chaining like vault's token_ref pointing at keyring://...).
type ProviderConfig ¶
type ProviderConfig interface {
TypeName() string
Dependencies() []SecretRef
// contains filtered or unexported methods
}
ProviderConfig is the marker interface that every provider's config struct must satisfy. The registry type-switches on ProviderConfig to dispatch to the right constructor at policy-load time.
TypeName returns the provider type name, which doubles as the URI scheme this provider handles (e.g. "vault", "keyring"). Each config MUST implement TypeName explicitly — there is no default on ProviderConfigMarker.
Dependencies returns the SecretRefs that must be resolved from other providers before this provider can be constructed (auth chaining). Providers with no dependencies inherit the default nil return from ProviderConfigMarker.
Types outside package secrets satisfy ProviderConfig by embedding ProviderConfigMarker. Embedding is required because Go's unexported-method sealing does not work across package boundaries: an unexported method's identity is qualified by the declaring package, so a type in another package cannot directly implement a sealed interface from this one. Embedding ProviderConfigMarker gives the outer type a promoted providerConfig() method whose identity lives in package secrets, which is what satisfies the interface.
The seal is strong-by-convention: anyone can embed ProviderConfigMarker, but doing so is a deliberate opt-in, and the registry only dispatches on config types it already knows about.
type ProviderConfigMarker ¶
type ProviderConfigMarker struct{}
ProviderConfigMarker is a zero-size struct that provider config types embed to satisfy ProviderConfig. Example usage:
type Config struct {
secrets.ProviderConfigMarker
// provider-specific fields...
}
Do not remove providerConfig() even if a linter flags it "unused" — its sole purpose is to seal ProviderConfig through method promotion to embedders of ProviderConfigMarker.
func (ProviderConfigMarker) Dependencies ¶
func (ProviderConfigMarker) Dependencies() []SecretRef
Dependencies returns nil. Providers with no auth chaining inherit this default. Providers that need auth chaining (e.g. Vault with a keyring-backed token) override this method on their own Config type.
type RefResolver ¶
type RefResolver func(ctx context.Context, ref SecretRef) (SecretValue, error)
RefResolver lets a provider constructor fetch a secret from an already-constructed provider during auth chaining.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds constructed providers keyed by their config name. It is constructed by NewRegistry, which resolves auth-chaining dependencies via topological sort and builds providers in the right order.
func NewRegistry ¶
func NewRegistry(ctx context.Context, configs map[string]ProviderConfig, constructors map[string]ConstructorFunc) (*Registry, error)
NewRegistry constructs all providers in dependency order.
configs maps config names to their ProviderConfig. constructors maps provider type names (from TypeName()) to their constructor functions. Every config's TypeName() must have a matching entry in constructors.
Dependencies are resolved via topological sort. Cyclic dependencies return ErrCyclicDependency. If any constructor fails, all already-constructed providers are closed before the error is returned.
func (*Registry) Close ¶
Close calls Close() on every provider in reverse construction order. Safe to call multiple times; subsequent calls are no-ops. The lock is released before calling provider Close methods so that concurrent Fetch calls observe the closed flag immediately rather than blocking on a slow provider teardown.
type SecretProvider ¶
type SecretProvider interface {
// Name returns the provider's stable identifier used in audit
// events and error messages (e.g. "keyring", "vault").
Name() string
// Fetch retrieves a single secret identified by ref.
//
// The returned SecretValue's Value buffer is owned by the
// caller; the provider does not retain a reference. The
// caller is responsible for calling SecretValue.Zero when
// the value is no longer needed.
//
// Errors MUST be wrappable with errors.Is against the
// sentinels in this package: ErrNotFound when the secret
// does not exist, ErrUnauthorized when the backend rejects
// the provider's credentials, ErrInvalidURI when the ref
// is missing required semantic pieces for this backend,
// ErrFieldNotSupported when the ref carries a Field the
// backend cannot honor, or a wrapped transport error for
// anything else.
Fetch(ctx context.Context, ref SecretRef) (SecretValue, error)
// Close releases any resources held by the provider. Safe to
// call multiple times; subsequent calls are no-ops. After
// Close returns, Fetch MUST return a non-nil error.
Close() error
}
SecretProvider is implemented by every secret backend.
The daemon constructs one provider per backend at session start, calls Fetch zero or more times, and calls Close exactly once at session end. Implementations MUST be safe for concurrent use by multiple goroutines.
type SecretRef ¶
type SecretRef struct {
// Scheme is the URI scheme: "keyring", "vault", "aws-sm",
// "gcp-sm", "azure-kv", or "op".
Scheme string
// Host is the URI host component. Interpreted per-provider:
// for keyring it is the OS keyring service name; for vault
// it is the mount point; etc.
Host string
// Path is the URI path with its leading slash trimmed.
// Interpreted per-provider. Path must not contain a leading
// slash; callers that construct SecretRef by hand are
// responsible for stripping it.
Path string
// Field is the optional URI fragment (everything after "#").
// Empty if the URI had no fragment. Providers that cannot
// honor a Field (e.g. keyring) return ErrFieldNotSupported
// when it is non-empty.
Field string
// Metadata holds provider-specific hints. Reserved for v2;
// v1 providers do not read it. Included now so adding it
// later is not a public-API churn.
Metadata map[string]string
}
SecretRef identifies one secret in one provider.
Callers should construct SecretRef via ParseRef rather than building literals, except in tests. Going through ParseRef guarantees consistent validation.
func ParseRef ¶
ParseRef parses a secret reference URI of the form
scheme://host[/path][#field]
and returns a SecretRef. The fragment, if present, becomes SecretRef.Field. The path's leading slash is stripped.
ParseRef does not validate per-provider semantics — it only validates the URI grammar and that the scheme is one of the six known schemes. Each provider validates its own SecretRef inside its Fetch implementation.
Errors are always wrappable with errors.Is against ErrInvalidURI or ErrUnsupportedScheme.
func (SecretRef) String ¶
String renders a SecretRef back to its URI form. It uses net/url's own escaping, so characters that would normally need percent-encoding in the path or fragment (e.g. spaces) are escaped on output.
Round-trip property: for any SecretRef produced by ParseRef, ParseRef(r.String()) returns a ref that is semantically equal to r. The literal bytes of r.String() may differ from the original URI (for example, capitalization of hex escapes may change, or an empty fragment may be dropped), but the parsed fields will match.
String does not validate the ref: constructing an invalid SecretRef by hand and calling String may produce a URI that ParseRef rejects.
type SecretValue ¶
type SecretValue struct {
// Value is the secret material. Caller-owned.
Value []byte
// TTL is the remaining lifetime of the secret as reported by
// the backend. Zero means no lease information is available
// (not "expires immediately").
TTL time.Duration
// LeaseID is the backend-specific lease identifier. Empty if
// the secret is not leased.
LeaseID string
// Version is the backend-specific version string. Empty if
// the secret is not versioned.
Version string
// FetchedAt is the local wall-clock time the provider
// retrieved the secret. Set by the provider.
FetchedAt time.Time
}
SecretValue is the result of a successful Fetch.
The Value field holds the raw secret bytes. The caller owns this buffer and should call SecretValue.Zero when done.
func (*SecretValue) Zero ¶
func (sv *SecretValue) Zero()
Zero overwrites the secret bytes with zeros and clears the lease and version fields. After Zero returns, the SecretValue holds no secret material. Idempotent; safe to call on a zero-value SecretValue.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package awssm implements secrets.SecretProvider using AWS Secrets Manager.
|
Package awssm implements secrets.SecretProvider using AWS Secrets Manager. |
|
Package azurekv implements secrets.SecretProvider using Azure Key Vault.
|
Package azurekv implements secrets.SecretProvider using Azure Key Vault. |
|
Package gcpsm implements secrets.SecretProvider using GCP Secret Manager.
|
Package gcpsm implements secrets.SecretProvider using GCP Secret Manager. |
|
Package keyring implements secrets.SecretProvider using the OS keyring: macOS Keychain (via /usr/bin/security), Linux Secret Service (via D-Bus), or Windows Credential Manager.
|
Package keyring implements secrets.SecretProvider using the OS keyring: macOS Keychain (via /usr/bin/security), Linux Secret Service (via D-Bus), or Windows Credential Manager. |
|
Package onepassword implements secrets.SecretProvider using the 1Password Connect API.
|
Package onepassword implements secrets.SecretProvider using the 1Password Connect API. |
|
Package secretstest provides test doubles for the secrets.SecretProvider interface.
|
Package secretstest provides test doubles for the secrets.SecretProvider interface. |
|
Package vault implements secrets.SecretProvider using HashiCorp Vault's KV v2 secrets engine.
|
Package vault implements secrets.SecretProvider using HashiCorp Vault's KV v2 secrets engine. |