secrets

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0 modules, 0 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.

Documentation

Overview

Package secrets implements envelope encryption for secrets stored in the replicated state (ADR 0011, FA-SEC-SEC): a master key (KEK) wraps data encryption keys (DEKs), and DEKs encrypt individual values with AES-256-GCM. Only the standard library is used.

Index

Constants

View Source
const (
	SourceEnv       = "MASTER_KEY"
	SourceFile      = "MASTER_KEY_FILE"
	SourceGenerated = "generated"
)

KEK sources.

View Source
const GeneratedKEKFile = "master.key"

GeneratedKEKFile is the file a KEK is generated into when none is configured (ADR 0011 option B).

View Source
const KeySize = 32

KeySize is the size of KEKs and DEKs in bytes (AES-256).

Variables

View Source
var (
	// ErrKEKMismatch means the replicated DEKs are wrapped with a different
	// master key than the one this node was started with (ADR 0011).
	ErrKEKMismatch = errors.New("master key does not match the cluster")
	// ErrNoActiveKey means no data key exists yet (the leader creates one at startup).
	ErrNoActiveKey = errors.New("no active data key")
	// ErrDecrypt covers malformed or tampered ciphertexts and wrong AAD.
	ErrDecrypt = errors.New("cannot decrypt secret")
)

Errors of the keyring.

Functions

func InstallGenerated

func InstallGenerated(dataDir string, key []byte, fingerprint string) error

InstallGenerated stores a master key received at join in dataDir, as if it had been generated there. It never overwrites an existing key; an existing key must be the same one (fingerprint), otherwise the node belongs to another cluster.

func IsEncrypted

func IsEncrypted(s string) bool

IsEncrypted reports whether s has the format produced by Encrypt.

func KeyFingerprint

func KeyFingerprint(key []byte) string

KeyFingerprint returns the fingerprint a key would have as KEK.

func KeyID

func KeyID(ciphertext string) (string, bool)

KeyID returns the key a value is encrypted with, so a rotation job can find values that still use an old key.

func NewDEK

func NewDEK() ([]byte, error)

NewDEK returns a fresh random data encryption key.

Types

type KEK

type KEK struct {

	// Fingerprint identifies the key without revealing it.
	Fingerprint string
	// Source tells where the key came from (SourceEnv, SourceFile, SourceGenerated).
	Source string
	// contains filtered or unexported fields
}

KEK is the master key. It never leaves the process except during a join over mTLS (phase 2).

func LoadKEK

func LoadKEK(dataDir, envKey, keyFile string) (*KEK, error)

LoadKEK returns the master key from envKey (base64), from keyFile (base64 or 32 raw bytes) or, if neither is set, from dataDir/master.key, which is generated with mode 0600 on first start.

func (*KEK) Transferable

func (k *KEK) Transferable() ([]byte, bool)

Transferable returns the key for a joining voter, only if it was generated in the data directory. Operators who configure MASTER_KEY or MASTER_KEY_FILE provide the same key to every voter themselves.

func (*KEK) Unwrap

func (k *KEK) Unwrap(keyID string, wrapped []byte) ([]byte, error)

Unwrap decrypts a DEK wrapped by Wrap.

func (*KEK) Wrap

func (k *KEK) Wrap(keyID string, dek []byte) ([]byte, error)

Wrap encrypts a DEK with the KEK, bound to its key ID.

type KeyLoader

type KeyLoader func(ctx context.Context) ([]store.SecretKey, error)

KeyLoader returns all data keys from the replicated state.

type Keyring

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

Keyring encrypts and decrypts values with the cluster's data keys. New keys arrive through Raft; an unknown key ID triggers a reload.

func NewKeyring

func NewKeyring(kek *KEK, load KeyLoader) *Keyring

NewKeyring creates a keyring; call Reload before use.

func (*Keyring) ActiveKeyID

func (r *Keyring) ActiveKeyID() string

ActiveKeyID returns the key new values are encrypted with ("" if none).

func (*Keyring) Decrypt

func (r *Keyring) Decrypt(ctx context.Context, ciphertext, aad string) ([]byte, error)

Decrypt reverses Encrypt. Values encrypted with older (rotated) keys remain readable as long as the key exists.

func (*Keyring) Encrypt

func (r *Keyring) Encrypt(plaintext []byte, aad string) (string, error)

Encrypt encrypts plaintext with the active key. aad binds the ciphertext to its place, e.g. "notification_channels/<id>/url", so it cannot be copied to another record (ADR 0011).

func (*Keyring) Refresh added in v0.0.2

func (r *Keyring) Refresh(ctx context.Context, interval time.Duration, log *slog.Logger) error

Refresh reloads the data keys every interval until ctx ends. Encrypt never reloads on its own, so without this a voter that loaded its keys before it caught up after a join has no active key, and every node keeps encrypting with the previous key after a rotation. It returns ErrKEKMismatch (the node must stop); other load errors are logged and retried on the next tick.

func (*Keyring) Reload

func (r *Keyring) Reload(ctx context.Context) error

Reload reads all data keys and unwraps them. It fails with ErrKEKMismatch if any key is wrapped with another master key.

type Secret

type Secret string

Secret is a string that never appears in logs (ADR 0011).

func (Secret) LogValue

func (Secret) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (Secret) String

func (Secret) String() string

String hides the value in fmt output as well.

Jump to

Keyboard shortcuts

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