keychain

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: AGPL-3.0 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 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")
)

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 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) 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) 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) 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) 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.

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 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, 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
}

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