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
- Variables
- func InstallGenerated(dataDir string, key []byte, fingerprint string) error
- func IsEncrypted(s string) bool
- func KeyFingerprint(key []byte) string
- func KeyID(ciphertext string) (string, bool)
- func NewDEK() ([]byte, error)
- type KEK
- type KeyLoader
- type Keyring
- func (r *Keyring) ActiveKeyID() string
- func (r *Keyring) Decrypt(ctx context.Context, ciphertext, aad string) ([]byte, error)
- func (r *Keyring) Encrypt(plaintext []byte, aad string) (string, error)
- func (r *Keyring) Refresh(ctx context.Context, interval time.Duration, log *slog.Logger) error
- func (r *Keyring) Reload(ctx context.Context) error
- type Secret
Constants ¶
const ( SourceEnv = "MASTER_KEY" SourceFile = "MASTER_KEY_FILE" SourceGenerated = "generated" )
KEK sources.
const GeneratedKEKFile = "master.key"
GeneratedKEKFile is the file a KEK is generated into when none is configured (ADR 0011 option B).
const KeySize = 32
KeySize is the size of KEKs and DEKs in bytes (AES-256).
Variables ¶
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 ¶
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 ¶
IsEncrypted reports whether s has the format produced by Encrypt.
func KeyFingerprint ¶
KeyFingerprint returns the fingerprint a key would have as KEK.
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 ¶
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 ¶
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.
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 ¶
NewKeyring creates a keyring; call Reload before use.
func (*Keyring) ActiveKeyID ¶
ActiveKeyID returns the key new values are encrypted with ("" if none).
func (*Keyring) Decrypt ¶
Decrypt reverses Encrypt. Values encrypted with older (rotated) keys remain readable as long as the key exists.
func (*Keyring) Encrypt ¶
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
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.