secrets

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package secrets supplies credentials (push keys, DEP tokens, challenge keys) to the library without letting them leak into logs, errors, or JSON: a Secret that redacts itself wherever it is formatted and Providers that read from a static map, the environment, or a directory of files.

Why

An MDM server holds the keys to every device it manages, and the commonest way they escape is a log line or an error message. Phase 3 of the plan of record introduces this package (decision record 0011) so every credential the library touches is a Secret, which prints as Redacted in fmt, slog, and JSON, and is fetched by name from a Provider the deployment chooses: Static for tests, Env for twelve-factor deployments, Dir for the file layout Docker and Kubernetes mount, and Chain to combine them. Dir is os.Root-scoped so a name cannot escape the directory.

The package supplies secrets; it does not seal them. Encryption of stored per-device secrets under a key from a Provider is storage/crypt.

References

Index

Constants

View Source
const Redacted = "[redacted]"

Redacted is what a Secret prints as.

Variables

View Source
var ErrName = errors.New("secrets: invalid name")

ErrName is returned for names a provider cannot map safely.

View Source
var ErrNotFound = errors.New("secrets: not found")

ErrNotFound is returned when a provider has no value for a name.

Functions

This section is empty.

Types

type Chain

type Chain []Provider

Chain queries providers in order and returns the first hit.

func (Chain) Get

func (c Chain) Get(ctx context.Context, name string) (Secret, error)

Get implements Provider. Errors other than ErrNotFound stop the chain.

type Dir

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

Dir reads each secret from a file named after it inside one directory, the layout Docker and Kubernetes secrets mount. Trailing newlines are trimmed. Names must not contain path separators.

func NewDir

func NewDir(path string) (*Dir, error)

NewDir opens the directory.

func (*Dir) Close

func (d *Dir) Close() error

Close releases the directory handle.

func (*Dir) Get

func (d *Dir) Get(_ context.Context, name string) (Secret, error)

Get implements Provider.

type Env

type Env struct {
	Prefix string
	// Lookup defaults to os.LookupEnv.
	Lookup func(string) (string, bool)
}

Env reads secrets from environment variables. The name is upper-cased with dots and dashes replaced by underscores, then prefixed.

func (Env) Get

func (e Env) Get(_ context.Context, name string) (Secret, error)

Get implements Provider.

func (Env) Key

func (e Env) Key(name string) string

Key returns the variable name for a secret name.

type Provider

type Provider interface {
	Get(ctx context.Context, name string) (Secret, error)
}

Provider resolves secrets by name.

type Secret

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

Secret holds a credential. Its String, GoString, Format, MarshalJSON, and MarshalText outputs are always Redacted; use Bytes to get the value.

func New

func New(b []byte) Secret

New wraps a value (copied).

func (Secret) Bytes

func (s Secret) Bytes() []byte

Bytes returns a copy of the value.

func (Secret) Equal

func (s Secret) Equal(o Secret) bool

Equal compares in constant time.

func (Secret) Format

func (Secret) Format(f fmt.State, _ rune)

Format implements fmt.Formatter so every verb prints the constant.

func (Secret) GoString

func (Secret) GoString() string

GoString implements fmt.GoStringer with a constant.

func (Secret) IsZero

func (s Secret) IsZero() bool

IsZero reports whether the secret is empty.

func (Secret) MarshalJSON

func (Secret) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler with the constant.

func (Secret) MarshalText

func (Secret) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler with the constant.

func (Secret) String

func (Secret) String() string

String implements fmt.Stringer with a constant.

type Static

type Static map[string][]byte

Static serves secrets from memory, for tests and embedded config.

func (Static) Get

func (s Static) Get(_ context.Context, name string) (Secret, error)

Get implements Provider.

Jump to

Keyboard shortcuts

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