secretsource

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package secretsource defines the single secret reference model (secret://conn/path#key), a Provider interface and a Resolver that routes by connection name. It imports only the standard library so existing providers can adapt to it without import cycles.

Index

Constants

View Source
const Mask = "[redacted]"

Mask replaces every registered value in output.

Variables

View Source
var (
	ErrInvalidRef        = errors.New("secretsource: invalid reference")
	ErrNotFound          = errors.New("secretsource: secret not found")
	ErrDenied            = errors.New("secretsource: access denied")
	ErrUnavailable       = errors.New("secretsource: provider unavailable")
	ErrFailClosed        = errors.New("secretsource: failing closed, no value released")
	ErrUnknownConnection = errors.New("secretsource: unknown connection")
	ErrUnsupported       = errors.New("secretsource: operation not supported")
)

Sentinels are matched with errors.Is. Resolver wraps provider errors so callers always see one of these kinds.

Functions

func IsKind

func IsKind(err error) bool

IsKind reports whether err is one of the package sentinels.

func IsRef

func IsRef(s string) bool

IsRef reports whether s looks like a secret:// reference.

func SetLimitsSource

func SetLimitsSource(f func() Limits)

SetLimitsSource makes the runtime settings the limit source; nil goes back to env.

func ValidConnName

func ValidConnName(name string, maxLen int) error

ValidConnName enforces [a-z0-9][a-z0-9_-]* up to max bytes.

Types

type Capabilities

type Capabilities struct {
	Renew    bool // provider implements Renewer
	Versions bool // honors Ref.Version
	Dynamic  bool // returns leased values
}

Capabilities lets callers skip unsupported calls without probing.

type FakeProvider

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

FakeProvider is an in-memory Provider for tests, including other packages.

func NewFake

func NewFake(name string) *FakeProvider

NewFake returns an empty fake named name.

func (*FakeProvider) Calls

func (f *FakeProvider) Calls() int

Calls reports how many times Resolve ran.

func (*FakeProvider) Capabilities

func (f *FakeProvider) Capabilities() Capabilities

Capabilities implements Provider.

func (*FakeProvider) Deny

func (f *FakeProvider) Deny(path string)

Deny makes path return ErrDenied.

func (*FakeProvider) FailWith

func (f *FakeProvider) FailWith(err error)

FailWith makes every Resolve return err (nil clears it).

func (*FakeProvider) Name

func (f *FakeProvider) Name() string

Name implements Provider.

func (*FakeProvider) Resolve

func (f *FakeProvider) Resolve(_ context.Context, ref Ref) (Value, error)

Resolve implements Provider.

func (*FakeProvider) Set

func (f *FakeProvider) Set(path, key, value string) *FakeProvider

Set stores the latest value (version 0) for path and key.

func (*FakeProvider) SetVersion

func (f *FakeProvider) SetVersion(path, key string, version int, value string) *FakeProvider

SetVersion stores a specific version; version 0 is the latest.

type Lease

type Lease struct {
	ID      string
	Expires time.Time
}

Lease identifies a renewable dynamic credential.

type Limits

type Limits struct {
	MaxRef      int
	MaxConn     int
	MaxPath     int
	MaxSegments int
	MaxKey      int
}

Limits caps reference parts. Zero fields fall back to env, then defaults.

func CurrentLimits

func CurrentLimits() Limits

CurrentLimits is the live limit set: the runtime source when wired, else env then defaults.

func LimitsFromEnv

func LimitsFromEnv() Limits

LimitsFromEnv reads APP_SECRETREF_MAX_{REF,CONN,PATH,SEGMENTS,KEY}, the seed for the runtime settings.

type Provider

type Provider interface {
	Name() string
	Capabilities() Capabilities
	Resolve(ctx context.Context, ref Ref) (Value, error)
}

Provider adapts one secret backend. Implementations should return the package sentinels (or wrap them) and never include secret values in errors.

type Redactor

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

Redactor masks known secret values in strings and errors. Safe for concurrent use.

func NewRedactor

func NewRedactor() *Redactor

NewRedactor returns an empty Redactor.

func (*Redactor) Add

func (r *Redactor) Add(value []byte)

Add registers a value. Empty values are ignored.

func (*Redactor) Mask

func (r *Redactor) Mask(s string) string

Mask returns s with every registered value replaced.

func (*Redactor) MaskError

func (r *Redactor) MaskError(err error) error

MaskError keeps the error chain for errors.Is but masks the message.

type Ref

type Ref struct {
	Conn    string
	Path    string
	Key     string
	Version int // 0 means latest
}

Ref points at one secret value in one named connection. It holds no value.

func ParseRef

func ParseRef(s string) (Ref, error)

ParseRef parses secret://conn/path#key[?version=N] using the current limits.

func ParseRefWith

func ParseRefWith(s string, l Limits) (Ref, error)

ParseRefWith parses against explicit limits. Errors never echo the input beyond a short reason, since a mistyped value may be a secret itself.

func (Ref) String

func (r Ref) String() string

String is the canonical URI form.

func (Ref) Validate

func (r Ref) Validate() error

Validate checks the parts with the current limits.

func (Ref) ValidateWith

func (r Ref) ValidateWith(l Limits) error

ValidateWith checks every part. Connection names are lowercase only, so two spellings can never name two connections. Paths and keys keep case.

type RefSpec

type RefSpec struct {
	Conn    string `json:"conn" yaml:"conn"`
	Path    string `json:"path" yaml:"path"`
	Key     string `json:"key" yaml:"key"`
	Version int    `json:"version,omitempty" yaml:"version,omitempty"`
}

RefSpec carries the parts of the object form.

func (RefSpec) Ref

func (s RefSpec) Ref() (Ref, error)

Ref validates the parts with the current limits.

func (RefSpec) RefWith

func (s RefSpec) RefWith(l Limits) (Ref, error)

RefWith validates the parts against explicit limits.

type Renewer

type Renewer interface {
	Renew(ctx context.Context, l Lease) (Lease, error)
}

Renewer is the optional lease renewal side of a Provider.

type Resolver

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

Resolver dispatches a Ref to the provider registered under its connection. It fails closed: no provider, an unclassified error or an empty value all yield an error and never a partial or default value.

func NewResolver

func NewResolver() *Resolver

NewResolver returns an empty Resolver with its own Redactor.

func (*Resolver) Connections

func (r *Resolver) Connections() []string

Connections lists registered connection names (unordered).

func (*Resolver) Provider

func (r *Resolver) Provider(conn string) (Provider, bool)

Provider returns the provider for a connection.

func (*Resolver) Redactor

func (r *Resolver) Redactor() *Redactor

Redactor exposes the masker so callers can scrub their own logs.

func (*Resolver) Register

func (r *Resolver) Register(conn string, p Provider) error

Register binds a connection name to a provider. Rebinding is rejected; call Unregister first so a swap is always deliberate.

func (*Resolver) Renew

func (r *Resolver) Renew(ctx context.Context, conn string, l Lease) (Lease, error)

Renew calls the provider's Renewer, or ErrUnsupported.

func (*Resolver) Resolve

func (r *Resolver) Resolve(ctx context.Context, ref Ref) (Value, error)

Resolve validates ref, calls the provider and registers the value with the redactor. Errors carry the ref (never the value) and one sentinel kind.

func (*Resolver) Unregister

func (r *Resolver) Unregister(conn string)

Unregister removes a connection.

type Spec

type Spec struct {
	Secret RefSpec `json:"secret" yaml:"secret"`
}

Spec is the YAML/JSON object form: {secret: {conn, path, key, version}}.

func (Spec) Ref

func (s Spec) Ref() (Ref, error)

Ref validates the object form with the same rules as the URI form.

type Value

type Value struct {
	Version   string
	FetchedAt time.Time
	TTL       time.Duration // 0 means do not cache
	// contains filtered or unexported fields
}

Value is a resolved secret. Its formatting verbs print a mask so a stray log line or %v cannot leak it; use Reveal at the single point of use.

func NewValue

func NewValue(data []byte, version string) Value

NewValue copies data into a Value.

func (Value) GoString

func (v Value) GoString() string

GoString masks the value for %#v.

func (Value) Len

func (v Value) Len() int

Len is the byte length, safe to log.

func (Value) Reveal

func (v Value) Reveal() []byte

Reveal returns a copy of the secret bytes.

func (Value) String

func (v Value) String() string

String masks the value.

Jump to

Keyboard shortcuts

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