secretsource

package module
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: BSD-3-Clause Imports: 29 Imported by: ? modules, 11 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.

README

secretsource

Resolve a secret (a FIDO2 PIN, a container password, a recovery code) from a reference instead of a plaintext file left on disk.

s, err := secretsource.Open(ctx, "keepassxc:https://fido.invalid/yubikey-5c")
if err != nil { ... }
defer s.Close()          // overwrites the bytes
use(s.Bytes())
Reference Reads
file:PATH a regular file readable by its owner alone, not a symbolic link (Unix: opened with O_NOFOLLOW, owned by the calling user, mode 0600 or stricter; Windows: opened with FILE_FLAG_OPEN_REPARSE_POINT and refused if it is a reparse point, and an access list that grants no read or write right to anyone but the owner, SYSTEM and Administrators); one trailing newline is stripped
keychain:SERVICE/ACCOUNT a macOS Keychain generic-password item (pure Go, go-macos/keychain)
secretservice:SERVICE/ACCOUNT a freedesktop Secret Service item with attributes service=SERVICE, account=ACCOUNT
secretservice:KEY=VALUE[,KEY=VALUE...] the one Secret Service item carrying all those attributes, whatever else it has (a KeePassXC entry exposed by its Secret Service integration: Title=..., UserName=...; or secret-tool store). No escaping: keys and values hold no ,. Several matches are refused
keepassxc:URL the KeePassXC entry whose URL field is that URL (any scheme://host...; https://NAME.invalid/... is a good, never-resolving choice), through KeePassXC's browser-integration protocol. Two entries with the same URL are refused as ambiguous
systemd-creds:NAME (Linux) a systemd credential, returned byte for byte (no trailing-newline stripping, unlike file:); see systemd credentials. First the file NAME in $CREDENTIALS_DIRECTORY (what LoadCredential=, LoadCredentialEncrypted= and SetCredentialEncrypted= place there): opened with O_NOFOLLOW, a regular file, owned by root or the caller, no access for "other" (group bits are accepted: systemd gives a User= service a root-owned 0440 file with an ACL), the directory likewise. If the variable is unset or the directory lacks NAME, the credential NAME or NAME.cred is found in /etc, /run, /usr/lib credstore.encrypted and decrypted by systemd-creds decrypt --name=NAME PATH - (run directly, no shell, empty environment, secret on stdout only, output capped at 4096 bytes; a non-root caller tries --user, systemd 256 or later, then system scope). NAME is a plain file name: no /, not . or .., no control character, no .cred suffix, 255 bytes at most. A symlink, a loose mode or a name that does not match the one embedded in the credential is refused, not skipped. Refused with ErrUnavailable on other systems

keepassxc:by-uuid/... and keepassxc:by-path/... (KeePassXC's own lookups) are refused: KeePassXC 2.8.0 crashes (SIGSEGV, BrowserService::sortPriority, std::max_element of an empty list) when a lookup finds an entry with no URL, and a PIN entry often has none. Found by URL, an entry has one by construction.

A reference never holds the secret, so it may sit in a flag, a script or a shell history. There is no scheme that runs a command: a flag must never be able to start a process. A reference with no scheme is refused, not guessed to be a path (file: is how a path is written).

Migrating from v0.8 (symlinked secret files). From v0.9.0 file: refuses a symbolic link in the last component of the path (dangling or not), and on Unix a file the calling user does not own. A link could point at a 0600 file of another account, or be planted in a writable directory. If you used file:/etc/app/pin with pin -> /run/secrets/pin, write file:/run/secrets/pin (readlink -f prints the real path). Links in the directories before the last component are still followed.

Bounds: a secret longer than 4096 bytes is refused; an empty one from a manager is refused; a call ends with its context (2 minutes when the context has no deadline) even when the manager is waiting for you to unlock it, and a late answer is wiped, never used. Errors are typed: ErrNotFound, ErrAmbiguous, ErrUnavailable (no manager, locked, unlock dismissed), ErrNotAssociated, ErrDenied, ErrBadRef, ErrScheme, ErrEmpty, ErrTooLarge. (The Go runtime keeps a copy of a secret that crossed a JSON string until the garbage collector reclaims it; the buffers this package owns are wiped.)

systemd credentials (systemd-creds:NAME)

Grammar, exactly: the scheme systemd-creds in lower case, a colon, then NAME, the credential name as given to LoadCredential= / LoadCredentialEncrypted= / SetCredentialEncrypted= / systemd-creds encrypt --name=. For example password_from = "systemd-creds:smtp".

  • NAME is a plain file name of 1 to 255 bytes. Letters, digits, ., - and _ are what systemd credential names use; other characters pass through, except /, NUL and control characters, which are refused.
  • No path (systemd-creds:/etc/credstore.encrypted/smtp is refused), not . or .., and no .cred suffix (systemd-creds:smtp.cred is refused): the suffix belongs to the file, the reference names the credential. smtp finds the file smtp or smtp.cred.

Resolution:

  1. $CREDENTIALS_DIRECTORY/NAME (what the service manager placed there). Opened with O_NOFOLLOW, a regular file, owned by root or the caller, no access for "other"; the directory likewise. Group bits are accepted: systemd gives a User= service a root-owned 0440 file with an ACL for that user (measured on systemd 257). A symlink or a loose mode is refused, never skipped. $CREDENTIALS_DIRECTORY itself must be an absolute path with no access for "other" (a mode with any o bit, such as 0755, is refused with ErrUnavailable); systemd creates it as 0700, so a test fixture should make the directory 0700 too.
  2. If the variable is unset or the directory lacks NAME: NAME or NAME.cred in /etc/credstore.encrypted, /run/credstore.encrypted, /usr/lib/credstore.encrypted, decrypted by systemd-creds decrypt --name=NAME PATH -, run directly (no shell, empty environment, no stdin), the secret read from stdout only, never from argv or the environment, at most 4096 bytes, ended with the context. A non-root caller tries --user (systemd 256 or later) first, then system scope. A credential whose embedded name differs from NAME is refused by systemd-creds.
  3. Any other system: ErrUnavailable.

No trimming. Open("systemd-creds:NAME") returns the exact bytes: a trailing newline, NUL bytes and non-UTF-8 bytes are kept, so keytabs, PEM keys and HMAC keys survive. This differs from file:, which strips one trailing newline. There is one entry point, Open; for systemd-creds: it never trims. Create a password credential without a newline: printf %s 's3cret' | systemd-creds encrypt --name=smtp - /etc/credstore.encrypted/smtp.cred. A credential of zero bytes is ErrEmpty; one longer than 4096 bytes is ErrTooLarge.

Choosing what a credential is sealed to

systemd-creds encrypt --with-key= takes host, tpm2, host+tpm2, null and auto (the TPM2 key when a TPM2 device is found, else the host key on persistent media). --tpm2-pcrs= defaults to no PCRs. Binding to PCRs makes the credential unreadable whenever those measurements change, and the service then fails closed:

  • --tpm2-pcrs=7 binds to the Secure Boot state: a firmware or shim update, or a VM migration on a vTPM, can make every credential sealed that way unreadable. Do not use a bare --tpm2-pcrs=7 without a way back (below).
  • --with-key=host+tpm2 with no --tpm2-pcrs: the key derives from the TPM2 chip and the host key file (/var/lib/systemd/credential.secret); it survives firmware updates, and is lost with the TPM2 chip or that file.
  • A signed PCR policy: --tpm2-public-key=PATH (a PEM RSA public key; --tpm2-public-key-pcrs=, default PCR 11), with signatures made by systemd-measure (--tpm2-signature=). A new boot component gets a new signature; credentials need no re-sealing.
  • systemd-pcrlock predicts PCR values into a TPM2 policy: before a firmware update run systemd-pcrlock unlock-firmware-code, afterwards make-policy (see its manual; systemd-cryptenroll and systemd-repart take it with --tpm2-pcrlock=; check that your systemd-creds does before relying on it).
  • Re-sealing: keep the secret in a place that does not depend on this machine (an offline copy) and, when decryption fails, run systemd-creds encrypt again from that copy. A failed decryption returns ErrUnavailable, says the credential may need re-sealing, and carries systemd-creds' own diagnostic, never the secret.

KeePassXC (macOS, Linux)

KeePassXC has no Secret Service on macOS; it does have a browser-integration socket, which this package speaks as a client: NaCl crypto_box over a local Unix socket ($TMPDIR-independent per-user temporary directory on macOS, $XDG_RUNTIME_DIR/app/org.keepassxc.KeePassXC/ on Linux), after an association that you approve once in KeePassXC. Windows (a named pipe) is not implemented yet. The protocol was read from KeePassXC's src/browser/ (BrowserHost, BrowserShared, BrowserAction, BrowserMessageBuilder, BrowserService).

  1. In KeePassXC: Settings > Browser Integration > Enable browser integration. Leave the database open (or let the request unlock it).

  2. Associate this machine, once per database:

    go install github.com/go-authn/secretsource/cmd/secretsource@latest
    secretsource keepassxc associate
    

    KeePassXC asks you to name the connection (say laptop) and accept. The association (that name and an identification public key; no password) is kept in the macOS Keychain, in the Secret Service on Linux when one answers, and otherwise in a 0600 file under your configuration directory. A lookup never creates an association; without one it fails with ErrNotAssociated.

  3. Create the entry (Title Yubikey PIN, Password = the PIN) and fill its URL field with https://fido.invalid/yubikey-5c (any URL not used by another entry: the reference is that URL). Then check the reference without printing anything:

    secretsource check 'keepassxc:https://fido.invalid/yubikey-5c'
    

    The first read of an entry shows KeePassXC's access dialog: allow it, and tick Remember to stop being asked.

  4. Use it: fido fingerprint list --pin-from 'keepassxc:https://fido.invalid/yubikey-5c'.

KeePassXC answers whichever database is open; with a locked database it opens its unlock dialog and waits for you (up to the 2-minute bound).

Taking a secret from a command-line flag: secretsource/secretflag

A secret passed as a flag value is visible in ps. The secretflag package gives a command the pair --<name> (deprecated, warns on stderr) and --<name>-from <ref>, mutually exclusive, with the reference resolved by secretsource.Open:

var password string
b := secretflag.Add(cmd, "password", &password, "the admin password") // cobra
// or: secretflag.AddStd(fs, os.Stderr, "password", &password, "the admin password", "p")
// ... after parsing:
if err := secretflag.Resolve(ctx, b); err != nil { return err }

A secret that never had a plain flag registers only --<name>-from: secretflag.AddFrom(cmd, "seal-key", &key, "the sealing key"), or secretflag.AddFromList(cmd, "seal-old-key", &olds, "previous keys") for a repeatable flag (key rotation).

Storing a secret: secretsource store

No entry has to be made by hand:

secretsource store 'keepassxc:https://fido.invalid/yubikey-5c'        # typed twice, echo off
secretsource store 'keepassxc:https://fido.invalid/yubikey-5c' --generate 24   # draws 24 letters and digits, prints nothing secret
printf '%s' "$PIN" | secretsource store keychain:fido/yubikey-5c       # from stdin when it is not a terminal

The secret is never taken from an argument or the environment. An existing entry is refused unless --replace (for KeePassXC that updates that entry, found by its URL, in place). keychain:, secretservice:SERVICE/ACCOUNT and secretservice:KEY=VALUE[,...] write an item (the last carries exactly those attributes; to replace one that has more attributes, name them all, since the Secret Service replaces only an item with the same attributes); file: is refused (no more plaintext files).

For keepassxc: it uses the browser protocol's set-login (KeePassXC 2.8.0: BrowserAction::handleSetLogin, BrowserService::addEntry/updateEntry), so the entry always carries the URL that finds it again. The login defaults to the URL's host (--login); 2.8.0 titles the new entry after that host and ignores a title (BrowserService.cpp:891), so --title is refused; the group is KeePassXC-Browser Passwords (created under the root) unless --group Keys/FIDO (names below the root). The duplicate check is a lookup by URL, never by uuid or path. KeePassXC shows its own dialogs (an access request for the existing entry, an update confirmation) and saves the database according to its settings.

Deleting an entry: secretsource.Delete

err := secretsource.Delete(ctx, "keychain:fido/yubikey-5c")
if errors.Is(err, fs.ErrNotExist) { /* already gone: your call whether that is success */ }
Reference What Delete does
keychain:SERVICE/ACCOUNT removes the generic-password item
secretservice:SERVICE/ACCOUNT removes the item with service=SERVICE, account=ACCOUNT; if several match, ErrAmbiguous and nothing is deleted
keepassxc:URL the one entry whose URL field is URL, by the browser protocol's delete-entry (KeePassXC 2.8.0: BrowserAction::handleDeleteEntry, BrowserService::deleteEntry); KeePassXC asks you to confirm, then moves it to the database's Recycle Bin, where the password stays until the bin is emptied; several entries: ErrAmbiguous, nothing deleted
file:, systemd-creds:, secretservice:KEY=VALUE ErrNoDelete: this package never manages a file or credential it did not store, and an attribute lookup names no single item

An absent entry is an error that is both ErrNotFound and fs.ErrNotExist. The managers have no "does it exist" call, so the entry is looked up first, as Store does before it replaces one: a keychain or Secret Service value passes through memory and is wiped at once, a KeePassXC reply is decoded without its password field. The secret is never returned, printed or logged.

Storing a secret by hand

macOS Keychain: security add-generic-password -s fido -a yubikey-5c -w (-w last, with no value, asks on the terminal; the secret is not in argv), then keychain:fido/yubikey-5c.

Linux: secret-tool store --label "yubikey PIN" service fido account yubikey-5c, then secretservice:fido/yubikey-5c.

Used by fido --pin-from (go-authn/fido) and theca --pin-from/--password-from/--recovery-from (go-theca/theca).

BSD 3-Clause.

Documentation

Overview

Package secretsource resolves a secret (a FIDO2 PIN, a container password, a recovery code) from a REFERENCE instead of a plaintext file the caller has to leave on disk.

A reference is "scheme:value":

file:PATH                    a regular file readable by its owner alone
keychain:SERVICE/ACCOUNT     a macOS Keychain generic-password item
secretservice:SERVICE/ACCOUNT  a freedesktop Secret Service item whose
                             attributes are service=SERVICE, account=ACCOUNT
                             (GNOME Keyring, KWallet, KeePassXC with "Secret
                             Service Integration" enabled)
systemd-creds:NAME           a systemd credential (Linux): the file NAME in
                             $CREDENTIALS_DIRECTORY, else NAME decrypted from
                             a credstore.encrypted directory by systemd-creds

There is deliberately no scheme that runs a command of the caller's choosing: a reference reaches the program through a command-line flag, and a flag must never be able to start a process. (systemd-creds runs one fixed program, systemd-creds, with a validated name; the reference never picks the program or its other arguments.) A reference never holds the secret itself, so it may sit in argv, a shell history or a script; the secret travels only from the manager to the returned Secret, which the caller wipes with Secret.Close.

s, err := secretsource.Open(ctx, "keychain:fido/yubikey-5c")
if err != nil { ... }
defer s.Close()
use(s.Bytes())

Every call is bounded: a secret longer than MaxSize is refused, and the call ends with the context (or after Timeout when the context has no deadline) even when the manager is waiting for the person to unlock it.

Index

Constants

View Source
const MaxGenerate = 256

MaxGenerate bounds Generate.

View Source
const MaxSize = 4096

MaxSize is the longest secret accepted, in bytes.

View Source
const Timeout = 2 * time.Minute

Timeout bounds a call whose context has no deadline. It is generous because a manager may be asking the person to unlock it.

Variables

View Source
var (
	// ErrBadRef is a reference that does not parse.
	ErrBadRef = errors.New("secretsource: bad reference")
	// ErrScheme is a reference whose scheme is not supported.
	ErrScheme = errors.New("secretsource: unsupported scheme")
	// ErrNotFound means the manager answered and holds no such entry.
	ErrNotFound = errors.New("secretsource: no such entry")
	// ErrUnavailable means no manager could be reached: none running, the
	// wrong platform, or it is locked and refused to ask.
	ErrUnavailable = errors.New("secretsource: secret manager unavailable")
	// ErrEmpty means the entry exists and holds nothing.
	ErrEmpty = errors.New("secretsource: the entry is empty")
	// ErrTooLarge means the secret is longer than [MaxSize].
	ErrTooLarge = errors.New("secretsource: the secret is too long")
)

Sentinel errors, comparable with errors.Is.

View Source
var ErrAmbiguous = errors.New("secretsource: the reference matches several entries")

ErrAmbiguous means the reference matches more than one KeePassXC entry; a secret is never picked among several.

View Source
var ErrDenied = errors.New("secretsource: KeePassXC refused the request")

ErrDenied means the person refused the request in KeePassXC.

View Source
var ErrExists = errors.New("secretsource: the entry already exists (use --replace to overwrite it)")

ErrExists means the entry is already there and Replace was not asked.

View Source
var ErrNoDelete = errors.New("secretsource: cannot delete this reference")

ErrNoDelete means the reference cannot be deleted by this package: a file or a systemd credential is never managed here, and a Secret Service lookup by arbitrary attributes names no single item to remove.

View Source
var ErrNoStore = errors.New("secretsource: cannot store into this reference")

ErrNoStore means the reference cannot be written by this package.

View Source
var ErrNotAssociated = errors.New("secretsource: not associated with the KeePassXC database")

ErrNotAssociated means this machine has no (or no longer a valid) association with the open KeePassXC database: run `secretsource keepassxc associate` once and approve the request in KeePassXC.

Functions

func Delete added in v0.11.0

func Delete(ctx context.Context, ref string) error

Delete removes the entry ref names and never prints it:

keychain:SERVICE/ACCOUNT        the Keychain generic-password item
secretservice:SERVICE/ACCOUNT   the Secret Service item
keepassxc:URL                   the KeePassXC entry whose URL field is URL

An absent entry is an error that is both ErrNotFound and fs.ErrNotExist, so a caller that wants "already gone" to succeed says so with errors.Is. Several matching entries are ErrAmbiguous and nothing is deleted. file:, systemd-creds: and a Secret Service lookup by attributes are ErrNoDelete.

The managers have no "does it exist" call, so the entry is looked up first the way Store does before it replaces one: the value may transit memory (a keychain or Secret Service item) and is wiped at once; a KeePassXC reply is decoded without its password field. Nothing is returned or logged.

KeePassXC deletes with its browser protocol's delete-entry (BrowserAction.cpp handleDeleteEntry, BrowserService.cpp deleteEntry in 2.8.0): KeePassXC asks the person to confirm, then MOVES the entry to the database's Recycle Bin. The password stays in the database until the Recycle Bin is emptied.

func Generate added in v0.4.0

func Generate(n int) ([]byte, error)

Generate draws n random characters (no look-alikes: no 0 O 1 l I) from crypto/rand, without modulo bias. The caller wipes the result.

func Store added in v0.4.0

func Store(ctx context.Context, ref string, secret []byte, o StoreOptions) error

Store writes secret under ref and never reads or prints it elsewhere:

keychain:SERVICE/ACCOUNT        a Keychain generic-password item
secretservice:SERVICE/ACCOUNT   a Secret Service item
keepassxc:URL                   a KeePassXC entry whose URL field is URL

An existing entry is ErrExists unless o.Replace. file: is refused: this package exists to move secrets off plaintext files. A KeePassXC entry is created by the browser protocol's set-login, so it always carries the URL that finds it again.

Types

type Association added in v0.2.0

type Association struct {
	// Database is KeePassXC's hash of the open database.
	Database string
	// ID is the name the person gave the connection in KeePassXC.
	ID string
}

Association names what Associate stored.

func Associate added in v0.2.0

func Associate(ctx context.Context) (Association, error)

Associate registers this machine with the database open in KeePassXC. The person approves it in KeePassXC and names the connection; nothing else in this package ever creates trust. The association (the connection's name and an identification public key, no secret) is kept by the OS keychain on macOS, the Secret Service on Linux when one answers, otherwise a 0600 file in the user's configuration directory.

type Ref

type Ref struct {
	// Scheme is "file", "keychain" or "secretservice".
	Scheme string
	// Path is the file path (file scheme).
	Path string
	// Service and Account name the entry (keychain, secretservice).
	Service, Account string
	// Entry names a KeePassXC entry (keepassxc): by-uuid/HEX, by-path/PATH
	// or a URL the entry holds.
	Entry string
	// Attrs is a secretservice lookup by arbitrary attributes, canonical
	// ("key=value,key=value", keys sorted); see [Ref.AttrMap].
	Attrs string
	// Name is a systemd credential name (systemd-creds).
	Name string
}

Ref is a parsed reference.

func Parse

func Parse(ref string) (Ref, error)

Parse reads a reference. It never touches a manager. A reference without a scheme is refused rather than guessed to be a path: "file:PATH" is how a path is written.

func (Ref) AttrMap added in v0.2.0

func (r Ref) AttrMap() map[string]string

AttrMap returns the attributes of a secretservice:key=value,... reference.

func (Ref) String

func (r Ref) String() string

String renders the reference back as written.

type Secret

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

Secret is a resolved secret. Its bytes live until Secret.Close overwrites them.

func Open

func Open(ctx context.Context, ref string) (Secret, error)

Open resolves ref. The returned Secret must be closed.

func (Secret) Bytes

func (s Secret) Bytes() []byte

Bytes returns the secret. The slice is valid until Close.

func (Secret) Close

func (s Secret) Close() error

Close overwrites the secret with zeros. It is safe to call twice and never fails; it returns an error only to satisfy io.Closer.

type StoreOptions added in v0.4.0

type StoreOptions struct {
	// Replace overwrites an existing entry; without it an existing one is ErrExists.
	Replace bool
	// Login is the user name of a KeePassXC entry (default: the URL's host).
	Login string
	// Group is the KeePassXC group a new entry goes into, as a path of names
	// below the root group ("Keys/FIDO"). Default: the group KeePassXC picks,
	// "KeePassXC-Browser Passwords" in 2.8.0 (BrowserService.cpp:57, created
	// under the root when absent).
	Group string
}

StoreOptions are the knobs of Store.

Directories

Path Synopsis
cmd
secretsource command
Command secretsource sets up and checks the references that fido and theca read their secrets from (--pin-from, --password-from, --recovery-from).
Command secretsource sets up and checks the references that fido and theca read their secrets from (--pin-from, --password-from, --recovery-from).
Package secretflag lets a command take a secret by reference.
Package secretflag lets a command take a secret by reference.

Jump to

Keyboard shortcuts

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