keychain

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 19 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 keychain stores business secrets in a local encrypted file, with only its random master key in the operating system keychain.

Index

Constants

View Source
const DefaultBackupKey = "backup/default-key"

DefaultBackupKey is the key derived from the default backup password, which exports and backup targets may seal with instead of their own.

View Source
const MCPToken = "mcp/token"

MCPToken is the bearer token of the MCP endpoint.

View Source
const MasterKey = "master/v1"

Variables

View Source
var (
	ErrNotFound      = errors.New("keychain: secret not found")
	ErrUnavailable   = errors.New("keychain: system keychain unavailable")
	ErrLocked        = errors.New("keychain: secret store locked")
	ErrBusy          = errors.New("keychain: secret store in use")
	ErrMasterMissing = errors.New("keychain: master key missing")
	ErrMasterInvalid = errors.New("keychain: invalid master key")
	ErrCorrupt       = errors.New("keychain: encrypted store damaged or key mismatched")
	ErrVersion       = errors.New("keychain: unsupported encrypted store version")
	// ErrInteractionRequired is a silent unlock that needs system authorization.
	ErrInteractionRequired = errors.New("keychain: system authorization required")
	// ErrPasswordRequired is an unlock of a store a local master password
	// protects, which only UnlockPassword opens.
	ErrPasswordRequired = errors.New("keychain: local master password required")
	// ErrWrongPassword is a local master password that does not open the
	// master key.
	ErrWrongPassword = errors.New("keychain: wrong local master password")
	// ErrNoPassword is a local master password operation on a store the
	// system keychain protects.
	ErrNoPassword = errors.New("keychain: no local master password")
	// ErrPasswordSetup is a local master password set up for a store that
	// already has secrets or a master key, which it would orphan.
	ErrPasswordSetup = errors.New("keychain: local master password needs an empty store")
)

Kinds are every kind of secret a credential has.

Functions

func AIAgentEnv

func AIAgentEnv(agentID, name string) string

AIAgentEnv is an environment variable injected into an external agent from the keychain: ai/<agentID>/env/<NAME>. A3 reads it; nothing here puts the value into a process.

func AIKey

func AIKey(profileID string) string

AIKey is the API key of a built-in provider profile: ai/<profileID>/apikey.

func BackupKey added in v0.0.7

func BackupKey(id string) string

BackupKey is the key a backup target's backups are sealed with, derived from its backup password: backup/<id>/key.

func BackupSecret added in v0.0.7

func BackupSecret(id string) string

BackupSecret is the password, secret key or token a backup target is reached with: backup/<id>/secret.

func Cred

func Cred(id string, kind Kind) string

func ForwardPassword

func ForwardPassword(id string) string

ForwardPassword is the entry of the password a dynamic forward's clients authenticate with: forward/<id>/password.

func Host

func Host(id string, kind Kind) string

Cred is the entry of a credential's secret: cred/<id>/<kind>. Host is a host-owned secret entry.

func LocalKeyPassphrase added in v0.0.12

func LocalKeyPassphrase(keyID string) string

LocalKeyPassphrase is the remembered passphrase of a key file of this computer, by its session.KeyID: localkey/<keyID>/passphrase. It opens only that file here, so no backup holds it.

func ProxyPassword

func ProxyPassword(id string) string

ProxyPassword is the entry of a proxy's password: proxy/<id>/password.

func SnippetContent

func SnippetContent(id string) string

SnippetContent is the encrypted body of a saved command: snippet/<id>/content.

Types

type Backend

type Backend interface {
	Set(key string, value []byte) error
	Get(key string) ([]byte, error)
	Delete(key string) error
}

Backend is the operating system secret store, or an explicitly injected test store.

type Change

type Change struct {
	Key    string
	Value  []byte
	Delete bool
}

Change is one mutation in an atomic encrypted-file commit.

type Keychain

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

Keychain is the secret store facade. New is only for explicit test injection; production must use OpenLocked or Open, which never fall back to memory.

func New

func New(b Backend) *Keychain

func Open

func Open(o OpenOptions) (*Keychain, error)

Open acquires the lifetime lock and immediately unlocks the store. A failure never replaces existing ciphertext or regenerates a missing master.

func OpenLocked

func OpenLocked(o OpenOptions) (_ *Keychain, err error)

OpenLocked acquires the lifetime lock and reads ciphertext without accessing the system keychain or creating a master key or encrypted file.

func (*Keychain) Apply

func (k *Keychain) Apply(changes []Change) error

Apply commits production changes once. Explicit test backends keep their own Get/Set/Delete hooks, including injected failures, with best-effort rollback.

func (*Keychain) Backend

func (k *Keychain) Backend() Backend

func (*Keychain) CanUsePassword added in v1.0.1

func (k *Keychain) CanUsePassword() bool

CanUsePassword reports whether SetupPassword may protect the store with a local master password: nothing is stored yet, so no secrets are left behind under a master key of the system keychain.

func (*Keychain) ChangePassword added in v1.0.1

func (k *Keychain) ChangePassword(current, next []byte) error

ChangePassword seals the master key under next in place of current.

func (*Keychain) Close

func (k *Keychain) Close() error

Close clears the cached master key and releases the lifetime lock.

func (*Keychain) Delete

func (k *Keychain) Delete(key string) error

func (*Keychain) DeleteCred

func (k *Keychain) DeleteCred(id string) error

func (*Keychain) Exists

func (k *Keychain) Exists(key string) (bool, error)

func (*Keychain) Get

func (k *Keychain) Get(key string) ([]byte, error)

func (*Keychain) Has

func (k *Keychain) Has(key string) bool

Has is for presentation only; use Exists when read failures matter.

func (*Keychain) PasswordProtected added in v1.0.1

func (k *Keychain) PasswordProtected() bool

PasswordProtected reports whether a local master password, not the system keychain, protects the master key.

func (*Keychain) Seal

func (k *Keychain) Seal(purpose string, plain, aad []byte) ([]byte, error)

Seal encrypts plain for purpose, bound to aad, which Unseal must be given again. It fails with ErrLocked while the store is locked, and with ErrUnavailable on a backend without a master key.

func (*Keychain) Set

func (k *Keychain) Set(key string, value []byte) error

func (*Keychain) SetupPassword added in v1.0.1

func (k *Keychain) SetupPassword(password []byte) error

SetupPassword creates the master key of an empty, locked store under a local master password, and unlocks the store with it.

func (*Keychain) TryUnlock

func (k *Keychain) TryUnlock() error

TryUnlock unlocks an existing store only when the system keychain hands over the master key without asking, as after the user always allowed SSHYard. Otherwise it fails and the store stays locked for Unlock.

func (*Keychain) Unlock

func (k *Keychain) Unlock() error

Unlock explicitly accesses the system keychain and verifies the encrypted store, retaining the master key only after successful verification.

func (*Keychain) UnlockPassword added in v1.0.1

func (k *Keychain) UnlockPassword(password []byte) error

UnlockPassword opens the master key with the local master password and unlocks the store with it.

func (*Keychain) Unlocked

func (k *Keychain) Unlocked() bool

Unlocked reports whether this open store is ready for secret operations. Explicitly injected test backends do not require unlocking.

func (*Keychain) Unseal

func (k *Keychain) Unseal(purpose string, sealed, aad []byte) ([]byte, error)

Unseal decrypts what Seal gave for purpose and aad; ErrCorrupt when it was not, or was changed since.

func (*Keychain) UseSystem added in v1.0.1

func (k *Keychain) UseSystem(current []byte) error

UseSystem moves the master key from the local master password, current, to the system keychain, then forgets master.enc; failing, the password still protects it.

type Kind

type Kind string

Kind is the kind of a credential's secret.

const (
	Password   Kind = "password"
	PrivateKey Kind = "key"
	Passphrase Kind = "passphrase"
)

type LinuxServiceError added in v0.0.19

type LinuxServiceError struct {
	Kind LinuxServiceErrorKind
	Err  error
}

LinuxServiceError preserves the original error for errors.Is and errors.As. Kind is diagnostic only and never authorizes creating a new master key.

func (*LinuxServiceError) Error added in v0.0.19

func (e *LinuxServiceError) Error() string

func (*LinuxServiceError) Unwrap added in v0.0.19

func (e *LinuxServiceError) Unwrap() error

type LinuxServiceErrorKind added in v0.0.19

type LinuxServiceErrorKind uint8

LinuxServiceErrorKind identifies only causes retained by MyGo's Linux backend. Unrecognized or stripped GError messages remain LinuxServiceUnknown.

const (
	LinuxServiceUnknown LinuxServiceErrorKind = iota
	LinuxServiceDependency
	LinuxServiceSessionBus
	LinuxServiceAccessDenied
	LinuxServiceStartFailed
	LinuxServiceLocked
	// LinuxServiceMissing is a session bus without a Secret Service.
	LinuxServiceMissing
	// LinuxServiceTimeout is a Secret Service that did not answer.
	LinuxServiceTimeout
)

type Memory

type Memory struct {

	// Fail, when set, is returned by every call.
	Fail error
	// contains filtered or unexported fields
}

Memory is an explicitly injected test Backend; production never falls back to it.

func NewMemory

func NewMemory() *Memory

NewMemory returns an empty Memory.

func (*Memory) Delete

func (m *Memory) Delete(key string) error

func (*Memory) Get

func (m *Memory) Get(key string) ([]byte, error)

func (*Memory) Keys

func (m *Memory) Keys() []string

Keys are the keys Memory holds, for tests.

func (*Memory) Len

func (m *Memory) Len() int

Len is the number of entries, for tests.

func (*Memory) Set

func (m *Memory) Set(key string, v []byte) error

type OpError

type OpError struct {
	Op  string
	Err error
}

OpError identifies the failed stage without requiring UI to render raw errors. Op is lock, master-read, master-write, master-verify, master-password, master-password-write, file-read, file-decrypt, file-write, or closed.

func (*OpError) Error

func (e *OpError) Error() string

func (*OpError) Unwrap

func (e *OpError) Unwrap() error

type OpenOptions

type OpenOptions struct {
	Path     string
	LockPath string
	Scope    string
	System   Backend
	// KDF is what a new local master password derives with, vault's
	// default when zero.
	KDF vault.Params
}

OpenOptions separates the per-app lifetime lock from an overridden data path.

type Silent

type Silent interface {
	GetSilently(key string) ([]byte, error)
}

Silent is a system Backend that can read without asking the user.

type System

type System struct{}

System is the Backend of the operating system's keychain, mygo.Secrets, under the app's identifier (com.sshyard.desktop).

func (System) Delete

func (System) Delete(key string) error

func (System) Get

func (System) Get(key string) ([]byte, error)

func (System) GetSilently

func (System) GetSilently(key string) ([]byte, error)

GetSilently reads key without any system authorization prompt: where one would be needed, the read fails.

func (System) Set

func (System) Set(key string, v []byte) error

type SystemError

type SystemError struct {
	Code int
	Err  error
}

SystemError preserves a structured OSStatus and its original error chain.

func (*SystemError) Error

func (e *SystemError) Error() string

func (*SystemError) Is

func (e *SystemError) Is(target error) bool

func (*SystemError) Unwrap

func (e *SystemError) Unwrap() error

Jump to

Keyboard shortcuts

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