secrets

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 17 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 keeps account credentials out of the database (docs/design/storage.md#credentials): the Secret Service over D-Bus on Linux, a file sealed with DPAPI on Windows, and a plain File for development.

Index

Constants

View Source
const (
	// CredentialName is the systemd credential that holds the key of the
	// sealed store (ADR-0044): the unit's
	// LoadCredentialEncrypted=frostmail-secrets-key puts it in
	// $CREDENTIALS_DIRECTORY.
	CredentialName = "frostmail-secrets-key"
	// SealedFileName is the sealed store's file in maild's data directory.
	SealedFileName = "secrets.sealed"
)
View Source
const ProbeKeyPrefix = "probe/"

ProbeKeyPrefix starts the key of every probe: Probe uses the key ProbeKeyPrefix plus 16 random bytes in hex, so that probes that run at the same time never touch each other's item.

Variables

View Source
var ErrDismissed = errors.New("secrets: the Secret Service's prompt was dismissed")

ErrDismissed means the user dismissed one of the service's prompts: to unlock a collection, or to make the default one.

View Source
var ErrNoKeyring = errors.New("secrets: no keyring can keep the secret")

ErrNoKeyring means no keyring can keep a secret: the Secret Service has no default collection and cannot make one, or no Secret Service answers and the store may not fall back to a file. Every error that wraps it says the cause and ends with the hint of the store's Options.

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

ErrNotFound means no secret is stored under the key.

View Source
var ErrReadOnly = errors.New("secrets: read-only store")

ErrReadOnly is what a store from OpenReader answers to Set and Delete.

View Source
var ErrUnavailable = errors.New("secrets: no Secret Service")

ErrUnavailable means no Secret Service answered on the bus.

View Source
var ErrUnseal = errors.New("secrets: unseal")

ErrUnseal matches every error of a store that cannot be unsealed: with FROSTMAIL_SECRETS=sealed, a key that is missing, of the wrong size or that does not open the file, and any file whose bytes do not unseal. The error's own text says which.

Functions

func AccountPassword

func AccountPassword(accountID int64) string

AccountPassword is the key of an account's password.

func Credential

func Credential(accountID int64, oauth bool) string

Credential is the key of the secret an account signs in with.

func LoadKey

func LoadKey(credDir string) ([]byte, error)

LoadKey reads the sealing key from the credential CredentialName in credDir ($CREDENTIALS_DIRECTORY). It must be exactly 32 raw bytes. An error names the credential and never holds the key.

func OAuthClientSecret

func OAuthClientSecret(provider string) string

OAuthClientSecret is the key of a provider's OAuth client secret.

func Probe added in v0.3.0

func Probe(ctx context.Context, s Store) error

Probe checks that s can keep a secret: it writes a random value that is not a secret under a key of its own (ProbeKeyPrefix and a random suffix), reads it back, compares it and deletes that key, and no other. A failure wraps its cause, so that ErrNoKeyring, ErrDismissed and ErrUnavailable stay testable. A probe item that was written is deleted again, whatever the failure.

func RefreshToken

func RefreshToken(accountID int64) string

RefreshToken is the key of an oauth2 account's refresh token.

Types

type File

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

File is a Store kept in one JSON object ({"key": "value", ...}) in a file with mode 0600. It is the M1 development store: plain text on disk, guarded only by file permissions. On Windows, NewDPAPIFile seals the same object for the user (ADR-0038).

func NewFile

func NewFile(path string) *File

NewFile returns a File store at path. The file is created on first Set; its directory must exist.

func NewSealedFile

func NewSealedFile(path string, key []byte) (*File, error)

NewSealedFile returns a File store at path whose bytes are sealed with AES-256-GCM under key, which must be 32 bytes (ADR-0044). A file is the magic "FMSEAL01", a fresh random 12-byte nonce, then the ciphertext with its tag; the magic is also the additional data. The rest is File's: the atomic write, mode 0600, a missing file is an empty store.

func (*File) All

func (f *File) All() (map[string]string, error)

All returns every stored secret (for moving them to another store).

func (*File) Delete

func (f *File) Delete(ctx context.Context, key string) error

Delete removes key, rewriting the file atomically like Set. A missing key or a missing file is not an error.

func (*File) Get

func (f *File) Get(ctx context.Context, key string) (string, error)

Get returns the secret stored under key, or ErrNotFound. A missing file holds no secrets. A file whose mode allows group or other access is refused with an error naming the file and its mode.

func (*File) Remove

func (f *File) Remove() error

Remove deletes the file; a missing file is not an error.

func (*File) Set

func (f *File) Set(ctx context.Context, key, value string) error

Set stores value under key. It rewrites the whole file atomically: write a temporary file in the same directory with mode 0600, then rename it over the old file.

type Namespace added in v0.3.0

type Namespace struct {
	Application string
	Store       string
}

Namespace names the Secret Service items a store owns: its items have the attributes application=Application and, when Store is not empty, store=Store, beside key=<key>. Every search, write and delete of the store uses all of them, so that two namespaces never see each other's items.

type Options added in v0.3.0

type Options struct {
	// Namespace names the items; the zero value is the host's
	// (application=frostmail, no store attribute, ADR-0011).
	Namespace Namespace
	// NoFallback makes Open keep no plain file when FROSTMAIL_SECRETS is
	// unset and no Secret Service answers: its store fails every Get, Set
	// and Delete with an error that wraps ErrNoKeyring.
	NoFallback bool
	// Hint ends the message of ErrNoKeyring errors: how the user keeps
	// secrets in a file instead.
	Hint string
}

Options say where Open and OpenReader find the Secret Service's items and what they do without one.

func FlatpakOptions added in v0.3.0

func FlatpakOptions(storeID string) Options

FlatpakOptions are the options of the Flatpak's maild (ADR-0046): its own namespace, with the id of its database as the store, and no plain file unless the user asks for one. An empty storeID is refused by Open.

func HostOptions added in v0.3.0

func HostOptions() Options

HostOptions are the options of a maild outside a Flatpak.

func OptionsFor added in v0.3.0

func OptionsFor(inFlatpak bool, storeID string) Options

OptionsFor picks the options of a maild: the Flatpak's, with storeID, when it runs in the Flatpak (config.InFlatpak), else the host's. maild and mailctl export use it, so that they look in the same namespace.

type SecretService

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

SecretService is a Store in the desktop's Secret Service.

func OpenSecretService

func OpenSecretService(ctx context.Context, conn *dbus.Conn, ns Namespace, hint string) (*SecretService, error)

OpenSecretService opens a plain session with the Secret Service on conn, or returns an error wrapping ErrUnavailable. The store's items are the ones of namespace ns (the zero value is the host's); hint ends the message of the ErrNoKeyring errors it returns.

func (*SecretService) Delete

func (s *SecretService) Delete(ctx context.Context, key string) error

Delete removes key; a missing key is not an error.

func (*SecretService) Get

func (s *SecretService) Get(ctx context.Context, key string) (string, error)

Get returns the secret stored under key, or ErrNotFound.

func (*SecretService) Set

func (s *SecretService) Set(ctx context.Context, key, value string) error

Set stores value under key, replacing any previous item.

type Store

type Store interface {
	// Get returns the secret, or ErrNotFound.
	Get(ctx context.Context, key string) (string, error)
	// Set stores the secret, replacing any previous value.
	Set(ctx context.Context, key, value string) error
	// Delete removes the secret; a missing key is not an error.
	Delete(ctx context.Context, key string) error
}

Store keeps string secrets by key.

func Open

func Open(ctx context.Context, file string, log *slog.Logger, opts Options) (Store, error)

Open picks maild's secret store by FROSTMAIL_SECRETS:

  • "file": the plain file at file, with a warning (CI, development, a server with no systemd credentials, a Flatpak whose user asked for it);
  • "sealed": the file beside it sealed with the key of the systemd credential frostmail-secrets-key, never a fallback (ADR-0044); a plain file moves in once and is removed;
  • empty: the Secret Service on the session bus, in opts.Namespace. With it, secrets still in the file are moved over, with one Info line. The file is removed only after every key is copied and reads back; when one fails, Open fails, the file stays as it is, the keys copied before stay in the store, and the next start repeats the move. With no Secret Service, the plain file with a warning, unless opts.NoFallback: then a store that fails every call with ErrNoKeyring, an Error line at start, and the file, if there is one, left as it is (ADR-0046);
  • anything else is an error.

Options with the Flatpak's namespace and no store id are refused.

func OpenReader

func OpenReader(ctx context.Context, file string, opts Options) (Store, error)

OpenReader opens the store Open would pick, to read it and nothing else (mailctl export, with maild stopped): it never moves a secret out of the file and never writes. With the Secret Service or the sealed file, a key it does not hold is also looked up in the plain file, a leftover Open has not migrated yet. With no Secret Service it reads the plain file alone, whether opts.NoFallback is set or not: the file is what an export needs of a store that Open has not moved yet. The Flatpak's namespace with no store id asks the Secret Service nothing, for no item can have that namespace yet. With "sealed" it needs the credential's key.

Jump to

Keyboard shortcuts

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