Help us shape the future of Go! Take the 2026 Go Developer Survey

secrets

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: GPL-3.0 Imports: 39 Imported by: 0

Documentation

Overview

Package secrets resolves secret references (`op://...`, `ref+<backend>://...`) against external secret managers.

Index

Constants

This section is empty.

Variables

View Source
var CredentialKeys = []string{
	"token", "connect_token", "secret_id", "client_secret", "api_key", "access_key",
	"password", "jwt", "credentials_json", "config", "session",
}

CredentialKeys are provider config keys that hold credentials. Their values are redacted like resolved values.

Functions

func ContainsRef

func ContainsRef(s string) bool

ContainsRef is true when s is a reference or holds an embedded `ref+...+` one.

func HasRef

func HasRef(v any) bool

HasRef is true when v holds any reference, deeply.

func IsRef

func IsRef(s string) bool

IsRef is true when the whole string is a secret reference.

func Kinds

func Kinds() []string

Kinds returns the registered kinds, sorted.

func Register

func Register(kind string, f Factory, aliases ...string)

Register adds a backend kind. Aliases map other scheme names to it.

Types

type Config

type Config map[string]ProviderConfig

Config is the secret_providers block.

func ParseConfig

func ParseConfig(raw map[string]map[string]any) (Config, error)

ParseConfig reads the raw secret_providers block. Each entry needs a `type`.

func (Config) Merge

func (c Config) Merge(other Config) Config

Merge returns a copy of c with the entries of other on top.

type Factory

type Factory func(cfg ProviderConfig) (Provider, error)

Factory builds a provider instance from its config.

type Options

type Options struct {
	Config   Config
	CacheTTL time.Duration  // 0 keeps values for the process life
	Timeout  time.Duration  // per fetch, default 30s
	OnValue  func(v string) // called for each resolved value (redaction hook)
	BaseDir  string         // folder for relative `file` paths

	// IsSecret tells if the value at a key path is redacted. The path is
	// empty when the key is not known. Nil redacts all values.
	IsSecret func(path []string) bool

	// KeyAliases renames keys of a `from:` object, e.g. username -> user.
	KeyAliases map[string]string
}

type Provider

type Provider interface {
	Get(ctx context.Context, ref Ref) ([]byte, error)
}

Provider fetches the raw value of one secret. The resolver applies the pointer, trim, cache, and redaction. A provider does none of these.

type ProviderConfig

type ProviderConfig struct {
	Name  string
	Kind  string
	Props map[string]any // ${VAR} already expanded
	// contains filtered or unexported fields
}

ProviderConfig is one entry of secret_providers, or a zero-config default.

func (ProviderConfig) BaseDir

func (c ProviderConfig) BaseDir() string

BaseDir is the folder that relative paths resolve against.

func (ProviderConfig) Bool

func (c ProviderConfig) Bool(key string, envKeys ...string) bool

Bool is Get parsed as a boolean.

func (ProviderConfig) Decode

func (c ProviderConfig) Decode(out any) error

Decode copies Props into a typed struct of the provider (JSON tags).

func (ProviderConfig) Get

func (c ProviderConfig) Get(key string, envKeys ...string) string

Get returns Props[key], else the first set env var of envKeys.

func (ProviderConfig) Redact

func (c ProviderConfig) Redact(s string) string

Redact hides known secret values in s. Use it on CLI stderr and HTTP bodies.

type Ref

type Ref struct {
	Raw      string     // original text, used in errors
	Kind     string     // registry kind after alias mapping, e.g. "op", "awssecrets"
	Path     string     // "prod/postgres"
	Params   url.Values // backend params, common params removed
	Pointer  string     // "/password", "" for the whole value
	Provider string     // instance name from ?provider=, "" for default
	Trim     bool
}

Ref is one parsed secret reference.

func ParseRef

func ParseRef(s string) (Ref, error)

ParseRef parses a whole-value reference.

func Refs

func Refs(v any) (refs []Ref)

Refs returns the references in v, deeply. A reference that does not parse is left out.

func (Ref) Key

func (r Ref) Key() string

Key is the cache key: kind + provider + path + params, without the pointer. Two fields that read different keys of one secret share one fetch.

func (Ref) Select

func (r Ref) Select(raw []byte) (any, error)

Select applies the pointer to a JSON or YAML value. Without a pointer it returns the value as a string.

func (Ref) String

func (r Ref) String() string

String is the reference without the pointer, in the `ref+` form.

type ResolveError

type ResolveError struct {
	Ref      string
	Provider string
	Kind     string
	Err      error
}

ResolveError is the error of one failed reference.

func (*ResolveError) Error

func (e *ResolveError) Error() string

func (*ResolveError) Unwrap

func (e *ResolveError) Unwrap() error

type Resolver

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

Resolver turns secret references into values. It is safe for concurrent use.

func NewResolver

func NewResolver(opts Options) *Resolver

func (*Resolver) Close

func (r *Resolver) Close() error

func (*Resolver) Redact

func (r *Resolver) Redact(s string) string

Redact replaces every resolved value in s with ***.

func (*Resolver) Resolve

func (r *Resolver) Resolve(ctx context.Context, s string) (any, error)

Resolve returns the value for one reference string. A string that is not a reference comes back unchanged. A pointer to an object is an error: objects are valid only in `from:`. Its value is always redacted.

func (*Resolver) ResolveEntry

func (r *Resolver) ResolveEntry(ctx context.Context, props map[string]any) (map[string]any, error)

ResolveEntry is ResolveValue plus the `from:` merge for one connection entry. Keys written in the entry win over keys from the secret.

func (*Resolver) ResolveValue

func (r *Resolver) ResolveValue(ctx context.Context, v any) (any, error)

ResolveValue deep-walks maps and lists and replaces every reference. It returns a copy. It fetches unique Ref.Key values concurrently.

Jump to

Keyboard shortcuts

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