credentials

package
v0.1.111 Latest Latest
Warning

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

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

Documentation

Overview

Package credentials keeps an app's secrets encrypted at rest: config/credentials.yml.enc holds KEY: value pairs sealed with AES-256-GCM under the master key in config/master.key (never committed) or LIDZA_MASTER_KEY (production). Every pack reads them the way it reads .env: pkg/env merges the decrypted values between the .env files and the process environment, so MAIL_API_KEY in the credentials is MAIL_API_KEY to the mail pack. Values saved at runtime (the admin pages) are held in the database by the db pack, encrypted with the same key, and override the file.

A value can be for one mode only: under a section named after the mode (dev:, production:) in the file, "dev.STRIPE_SECRET_KEY" to Set and Read. A mode reads the plain values and its own section, which wins; one sealed file holds a sandbox key for dev and the live key for production, and production needs only the master key.

Index

Constants

View Source
const (
	// MasterKeyFile holds the key, 32 bytes as 64 hex characters.
	MasterKeyFile = "config/master.key"
	// File is the sealed credentials.
	File = "config/credentials.yml.enc"
	// EnvMasterKey carries the key where no file should exist: production.
	EnvMasterKey = "LIDZA_MASTER_KEY"
	// EnvKeyOff=1 makes config/master.key unreadable to the process: lidza
	// test and verify set it, so tests never open the app's real
	// credentials. A test that needs a key sets EnvMasterKey itself.
	EnvKeyOff = "LIDZA_MASTER_KEY_OFF"
)

Files and the environment.

View Source
const FileHeader = "" /* 270-byte string literal not displayed */

FileHeader is the comment Format writes at the top of the sealed file.

Variables

View Source
var ErrKeyNotHere = errors.New("credentials: " + File + " is sealed with a master key this checkout lacks: put that key in " + MasterKeyFile + " or " + EnvMasterKey + " (from whoever set up the app); a new key could not open the file")

ErrKeyNotHere is Generate's refusal to make a key for a checkout whose credentials file was sealed with one it lacks: a clone, CI, another machine. A new key could not open the file.

View Source
var ErrNoKey = errors.New("credentials: no master key: " + MasterKeyFile + " is missing and " + EnvMasterKey + " is not set (lidza credentials init creates one)")

ErrNoKey is returned when neither the file nor the variable has a key.

Functions

func Decrypt

func Decrypt(key []byte, sealed string) ([]byte, error)

Decrypt opens what Encrypt sealed.

func Encrypt

func Encrypt(key, plain []byte) (string, error)

Encrypt seals plain with AES-256-GCM under key: base64 of nonce and ciphertext.

func Format

func Format(values map[string]string) string

Format writes values as the flat YAML Parse reads, names sorted.

func FormatValues added in v0.1.110

func FormatValues(values map[string]string) string

FormatValues is Format without the file's header: values shown from somewhere else, such as the ones saved from the admin pages.

func Generate

func Generate(dir string) (bool, error)

Generate writes a new master key to config/master.key (unless there is a key, in the file or LIDZA_MASTER_KEY), adds it to .gitignore, and seals an empty credentials file when there is none. It returns whether a key was created, and ErrKeyNotHere when the credentials file exists without its key: a new one would not open it.

func HasKey

func HasKey(dir string) bool

HasKey reports whether a master key is available.

func Key

func Key(dir string) ([]byte, error)

Key returns the master key: LIDZA_MASTER_KEY, else config/master.key.

func Mode

func Mode() string

Mode is the run mode the values resolve for: LIDZA_MODE, else "production" (pkg/env.Mode, which imports this package).

func Names

func Names(dir string) []string

Names lists the names in the file and the overrides, sorted, values withheld.

func Overrides

func Overrides() map[string]string

Overrides returns a copy of the runtime values.

func Parse

func Parse(text string) (map[string]string, error)

Parse reads the flat YAML the file holds: one `KEY: value` per line, values optionally double-quoted (Go syntax), # comments.

func Read

func Read(dir string) (map[string]string, error)

Read decrypts the credentials file, names as written: NAME, or mode.NAME for a value in a mode's section (Resolve picks a mode's). No file is no credentials.

func Resolve

func Resolve(raw map[string]string, mode string) map[string]string

Resolve returns what mode reads from raw (Read's names): the plain names, with mode's "mode.NAME" entries over them; other modes' entries are left out.

func Set

func Set(dir string, values map[string]string) error

Set stores one or more values in the file.

func SetOverride

func SetOverride(name, value string)

SetOverride sets one runtime value ("" removes it).

func SetOverrides

func SetOverrides(values map[string]string)

SetOverrides replaces the runtime values.

func Unset

func Unset(dir string, names ...string) error

Unset removes names from the file.

func Values

func Values(dir string) map[string]string

Values returns the file's values for the run mode (Resolve) with the runtime overrides applied; without a master key or a file, the overrides alone. This is what pkg/env merges.

func Write

func Write(dir string, values map[string]string) error

Write seals values into the credentials file, atomically: a crash or a concurrent reader sees the old file or the new one, never part of one. The file is sealed and committed, so it stays readable (0644) for the user a container runs as; the master key is the secret.

Types

type Store

type Store interface {
	Save(ctx context.Context, name, value string) error
	Delete(ctx context.Context, name string) error
	// Names lists what the store holds, sorted.
	Names(ctx context.Context) ([]string, error)
}

Store keeps values saved while the app runs, encrypted with the master key; the db pack provides one in Postgres. Save and Delete also update the runtime overrides.

Jump to

Keyboard shortcuts

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