Skip to main content

Module secret_store

Module secret_store 

Source
Expand description

srv’s secret storage: account API keys, OAuth accounts, MuxBus credentials, browser-pane logins.

Backs SecretRef::Keychain { service, account }, which names an entry in this store, wherever it keeps secrets; the DB holds just the pointer plus non-secret metadata + a masked tail. Reads return a Zeroizing<String> so the plaintext is wiped from memory on drop.

Two backends, chosen once at startup:

  • The OS keychain (macOS Keychain / Windows Credential Manager / Linux Secret Service via the keyring-core crate). The default, and what the desktop app always uses.
  • A file store (use_file_store): one 0600 file per entry in a 0700 directory, for machines with no keychain — a container, a server, CI. Headless srv uses it unless told otherwise (docs/specs/SPEC_SRV_HEADLESS_MODE_2026_09_26.md §3.6). Secrets are protected by file permissions (and the volume’s own encryption), not encrypted by srv: a key kept beside them would add nothing.

There is no silent downgrade from one to the other: failures surface as errors. See docs/specs/archive/SPEC_TRUST_CENTER_2026_06_15.md §7 and §12.2.

The rest of this comment is about the keychain backend.

macOS: srv never shows the keychain’s consent dialog ([keychain_without_dialogs]). A keychain item trusts the code identity that wrote it, plus any the person allowed. One written by a build with another identity (every srv before v0.59.16 had a versioned one; a task dev build changes identity on each rebuild) used to bring up “AgentMux Server wants to use your confidential information…” unasked, at startup, after an upgrade. Now such a read fails at once with NEEDS_CONSENT, and each caller decides: the MuxBus sign-in counts as signed out (get_optional_if_trusted; signing in again writes a fresh entry), and everything else reports it. A write over such an item replaces it (put), so the new one trusts this build. docs/analysis/ANALYSIS_MACOS_KEYCHAIN_PROMPT_AFTER_UPGRADE_2026_10_21.md.

Reads are bounded by [TIMEOUT]; writes are not — see below. Every operation here can require interactive OS consent (“App wants to access your confidential information…”) the first time a given code signature touches a given entry, and that consent call has no cancellation mechanism: an unanswered prompt (headless process, dialog on another Space, no attached display session) blocks the underlying platform call indefinitely. Confirmed live — see docs/retro/retro-macos-muxbus-keychain-prompt-storm-2026-08-19.md §5.

get/get_optional run the real platform call on a detached thread and give up waiting after TIMEOUT, so a stuck prompt bounds how long the CALLER waits (and any lock it’s holding) instead of hanging it forever. This is safe specifically because a read has no side effect: if the timeout fires, the detached thread’s eventual (possibly much later) result is just discarded — nothing acts on a stale read. At most one read per account is in flight ([SharedReads]): a retry while one is still waiting on a dialog joins it rather than queueing another dialog behind it.

put/delete deliberately do NOT use this timeout (reagent + Codex, PR #2679 round 2): the same “detached thread keeps running after the caller gives up” behavior is unsafe for a MUTATION. A caller that treats a timed-out write as failure and proceeds — e.g. skipping a dependent DB write, or a rollback in muxbus.rs writing a different value to the same entry — can have the orphaned original write land afterward and silently clobber whatever the caller did next, with no ordering guarantee between them. There is no cancellation-equivalent compensation available (the platform call truly cannot be cancelled), so until one exists, an unbounded wait — the pre-existing, safe-if-slow behavior — is the correct tradeoff for anything that mutates state. A stuck consent prompt on a write still blocks its caller indefinitely; only reads are protected today.

Modules§

file
The file store. Each entry is one file named by the SHA-256 of its account key, so no key text (ids, hostnames) reaches a file name and no key can escape the directory. No timeouts: file operations don’t wait on a user.

Enums§

KeychainProbe
What a try of the OS keychain found (probe_keychain).

Constants§

NEEDS_CONSENT
What a keychain read or write of an item this build isn’t trusted for fails with on macOS, instead of asking the person (is_needs_consent).
SERVICE
Keychain service string — constant across all AgentMux secrets.

Functions§

account_key
Build the keychain account string for an identity-account id.
backend_name
Which backend this process uses, for logs: "keychain" or "file".
delete
Delete the secret for account_id. A missing entry is treated as success (idempotent delete).
get
Read the secret for account_id. Returned wrapped in Zeroizing so it is wiped on drop. Resolved at agent spawn time when injecting env vars.
get_optional
Read the secret for account_id, distinguishing “no entry stored yet” (Ok(None)) from a real storage failure (Err) — a locked keychain, no Secret Service daemon running, permission denied, etc. get collapses both into the same Err variant, which is correct for its own callers (a spawn-time credential resolve should fail either way), but is the wrong shape for a caller that needs to tell “genuinely never logged in” apart from “storage is transiently broken” — treating a transient failure as “no credential” can silently present as a full logout. Use this variant when that distinction matters.
get_optional_if_trusted
get_optional, except that an entry this build may not read without asking (NEEDS_CONSENT) counts as absent, and is logged. For the MuxBus sign-in: one saved by an older build is then signed out, and signing in again writes a fresh entry this build owns, so no dialog ever shows. Never for a key whose absence makes the caller create a new one (the Vault).
is_needs_consent
Whether err (from get, get_optional or put) is NEEDS_CONSENT: the entry exists, but this build may not use it without the person’s consent. Not a transient failure, and not “no entry”.
isolate_for_home_override
An isolated instance (started with AGENTMUX_HOME_OVERRIDE, as task dev always is) keeps its secrets in files under its own home and never in the person’s OS keychain. The keychain is the person’s: a test build that saved the first Vault entry would make the real Vault key there, trusted only by that build, and the person’s own AgentMux could not read it (nor would an account key it saved be theirs). Called once at startup, before anything reads a secret; true when it chose the file store.
probe_keychain
Try the OS keychain as the server would use it: make the store, then store one test entry, read it back and delete it, giving up after wait. A call that waits on a prompt is left running on its own thread (the caller is a short program that then ends), never waited for.
put
Store (or overwrite) the secret for account_id in the OS keychain.
use_file_store
Keep every secret in files under dir instead of the OS keychain, for the rest of this process. Called once at startup, before anything reads or writes a secret. Creates dir (mode 0700) if needed.