Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix: encrypt keyring files by default

Steven Enamakel3ca12a2fa88f
diff --git a/crates/openhuman-core/src/security/keyring/README.md b/crates/openhuman-core/src/security/keyring/README.md
index 4625bde76c..9a59e6b7fc 100644
--- a/crates/openhuman-core/src/security/keyring/README.md
+++ b/crates/openhuman-core/src/security/keyring/README.md
@@ -57,9 +57,9 @@ None. There is no `bus.rs`, and the module publishes and subscribes to no `Domai
Secret storage backend, selected once and frozen in a `OnceLock`:
-- `os` (production default outside staging/prod special-casing): native OS credential store, macOS Keychain, Windows Credential Manager, or Linux Secret Service, under service name `"openhuman"`.
-- `encrypted_file` (staging/production, and via `OPENHUMAN_KEYRING_BACKEND=encrypted_file`): single ChaCha20-Poly1305 file `{workspace}/secrets.enc`, encrypted with a master key loaded once, from `OPENHUMAN_KEYRING_MASTER_KEY` / `OPENHUMAN_KEYRING_MASTER_KEY_FILE` when set, otherwise from the OS keychain (`openhuman` / `app:master_key`). Files are written `0600` on Unix via temp-file plus atomic rename.
-- `file` (dev default, `cfg(test)`, or `OPENHUMAN_KEYRING_BACKEND=file`): plaintext JSON `{workspace}/dev-keychain.json`. Not encrypted; test and debug use only.
+- `os` (via `OPENHUMAN_KEYRING_BACKEND=os`): native OS credential store, macOS Keychain, Windows Credential Manager, or Linux Secret Service, under service name `"openhuman"`.
+- `encrypted_file` (default unless development mode or a backend override is explicit): single ChaCha20-Poly1305 file `{workspace}/secrets.enc`, encrypted with a master key loaded once, from `OPENHUMAN_KEYRING_MASTER_KEY` / `OPENHUMAN_KEYRING_MASTER_KEY_FILE` when set, otherwise from the OS keychain (`openhuman` / `app:master_key`). Files are written `0600` on Unix via temp-file plus atomic rename.
+- `file` (explicit `OPENHUMAN_APP_ENV=dev`/`development`, `cfg(test)`, or `OPENHUMAN_KEYRING_BACKEND=file`): plaintext JSON `{workspace}/dev-keychain.json`. Not encrypted; test and debug use only.
- `mock` (test-only): in-memory `HashMap`.
`SecretStore` additionally manages a master encryption key: keychain-backed (slot `secretstore.master_key`) in normal builds with one-time migration from the legacy `{data_dir}/openhuman/.secret_key` file; the file path is retained only for unit tests. Decoded keys are cached process-wide, keyed by normalized path.
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
index 5c662e4b84..a816771867 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -53,13 +53,13 @@ static KEYCHAIN_UNAVAILABLE: OnceLock<()> = OnceLock::new();
// ── Public API for core startup ──────────────────────────────────────────────
/// Initialize the keyring subsystem: set the workspace directory and load
-/// the master encryption key (staging/production only) — from
+/// the master encryption key (whenever encrypted storage is selected) — from
/// [`MASTER_KEY_ENV`] or [`MASTER_KEY_FILE_ENV`] when an operator set one,
/// otherwise from the OS keychain.
///
/// Call this once at core startup before any keyring operations. In dev
-/// environments the master key is not loaded (the plain file backend is
-/// used instead). The result is cached process-wide; subsequent calls are
+/// environments that explicitly select the plain file backend, the master key
+/// is not loaded. The result is cached process-wide; subsequent calls are
/// no-ops. Which source supplied the key is logged at `info`; the key never
/// is.
///
diff --git a/crates/openhuman-core/src/security/keyring/mod.rs b/crates/openhuman-core/src/security/keyring/mod.rs
index 12f8be5317..25970b276c 100644
--- a/crates/openhuman-core/src/security/keyring/mod.rs
+++ b/crates/openhuman-core/src/security/keyring/mod.rs
@@ -1,11 +1,10 @@
-//! OS-keychain backed secret storage with a test/debug file backend override.
+//! Encrypted secret storage with explicit OS-keychain and debug overrides.
//!
//! Wraps the [`keyring`] crate (or an explicit file backend override) to provide a
//! namespaced, user-scoped interface to secret storage:
-//! - **macOS**: Keychain (prod)
-//! - **Windows**: Credential Manager (prod)
-//! - **Linux**: Secret Service / libsecret (prod)
-//! - **Tests / explicit override**: JSON file at `{workspace}/dev-keychain.json`
+//! - **Default**: encrypted file with a master key from the environment or OS keychain
+//! - **Explicit `os` override**: native OS credential store
+//! - **Tests / explicit `file` override**: JSON file at `{workspace}/dev-keychain.json`
//!
//! All keys are scoped under a `user_id` parameter so multiple users can
//! coexist without collision. The backend entry key format is:
@@ -15,9 +14,10 @@
//!
//! The backend is chosen **once** at first use, in this priority order:
//!
-//! 1. `OPENHUMAN_KEYRING_BACKEND` env var: `"os"` | `"file"` | `"mock"`.
+//! 1. `OPENHUMAN_KEYRING_BACKEND` env var: `"os"` | `"file"` | `"encrypted_file"`.
//! 2. `cfg!(test)` → `file`.
-//! 3. Otherwise → `os`.
+//! 3. Explicit `OPENHUMAN_APP_ENV=dev` or `development` → `file`.
+//! 4. Otherwise → `encrypted_file`.
//!
//! The selected backend is logged once with `[keyring] backend=<name> ...`.
//!
diff --git a/crates/openhuman-core/src/security/keyring/store.rs b/crates/openhuman-core/src/security/keyring/store.rs
index 1412a76a5b..9f06dcad0a 100644
--- a/crates/openhuman-core/src/security/keyring/store.rs
+++ b/crates/openhuman-core/src/security/keyring/store.rs
@@ -120,9 +120,9 @@ pub(super) fn build_backend_at(path: &Path) -> Box<dyn KeyringBackend> {
return Box::new(backend::FileBackend::new(&path));
}
- // Priority 3: staging/production → encrypted file backend (master key in OS keychain).
- // Dev builds → plain file backend (no keychain interaction, avoids codesign prompts).
- if is_staging_or_production() {
+ // Priority 3: encrypt by default, including headless deployments where
+ // OPENHUMAN_APP_ENV is unset. Plain files require an explicit dev mode.
+ if default_to_encrypted_file(std::env::var("OPENHUMAN_APP_ENV").as_deref().ok()) {
log::info!("[keyring] backend=encrypted_file path={}", path.display());
Box::new(super::encrypted_file_backend::EncryptedFileBackend::new(
&path,
@@ -137,10 +137,6 @@ pub(super) fn build_backend_at(path: &Path) -> Box<dyn KeyringBackend> {
}
}
-fn is_staging_or_production() -> bool {
- is_staging_or_production_value(std::env::var("OPENHUMAN_APP_ENV").as_deref().ok())
-}
-
pub(super) fn effective_backend_kind() -> BackendKind {
effective_backend_kind_for(
std::env::var("OPENHUMAN_APP_ENV").as_deref().ok(),
@@ -160,7 +156,7 @@ fn effective_backend_kind_for(
if cfg_test {
return BackendKind::File;
}
- if is_staging_or_production_value(app_env) {
+ if default_to_encrypted_file(app_env) {
BackendKind::EncryptedFile
} else {
BackendKind::File
@@ -176,8 +172,8 @@ fn backend_kind_from_env_value(value: &str) -> Option<BackendKind> {
}
}
-fn is_staging_or_production_value(app_env: Option<&str>) -> bool {
- matches!(app_env.map(str::trim), Some("staging") | Some("production"))
+fn default_to_encrypted_file(app_env: Option<&str>) -> bool {
+ !matches!(app_env.map(str::trim), Some("development") | Some("dev"))
}
/// Derive the directory for keyring files (`secrets.enc`, `dev-keychain.json`).