Skip to content

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

feat(keyring): route secrets through the storage backend when configured

Steven Enamakel90a624eb2d47
diff --git a/crates/openhuman-core/src/security/keyring/ops.rs b/crates/openhuman-core/src/security/keyring/ops.rs
index 9760c4c868..66cd82c184 100644
--- a/crates/openhuman-core/src/security/keyring/ops.rs
+++ b/crates/openhuman-core/src/security/keyring/ops.rs
@@ -38,12 +38,83 @@ pub enum MigrationOutcome {
}
// ── Core operations ───────────────────────────────────────────────────────────
+//
+// `get` / `set` / `delete` serve a user's secrets from the storage backend
+// when the host configured one (`crate::storage::secrets`, the acting agent's
+// scope), else from the process backend. The `process_*` functions always use
+// the process backend: the availability probe and the app-level keys
+// (`get_or_create_random`, `migrate_from_file` — the config encryption key)
+// belong to this process, not to an agent.
+
+/// The storage-backed secret store for this call, when one is configured.
+fn storage_secrets(
+) -> Result<Option<tinystoragedrivers::secrets::DocumentSecrets>, KeyringError> {
+ crate::storage::secrets::current()
+ .map_err(|error| KeyringError::Backend(format!("storage secrets: {error}")))
+}
+
+fn storage_error(error: crate::storage::StorageError) -> KeyringError {
+ KeyringError::Backend(format!("storage secrets: {error}"))
+}
-/// Retrieve a secret from the active backend.
+/// Retrieve a secret: from the storage backend when one is configured,
+/// else from the process backend.
///
/// Returns `Ok(None)` when no entry exists for this user + key combination.
/// Never logs the secret value.
pub fn get(user_id: &str, key: &str) -> Result<Option<String>, KeyringError> {
+ use tinystoragedrivers::secrets::SecretStore as _;
+ let Some(secrets) = storage_secrets()? else {
+ return process_get(user_id, key);
+ };
+ log::debug!("[keyring] get (storage) user_id={user_id} key={key}");
+ let name = namespaced_key(user_id, key);
+ let value = crate::storage::block_on(async move { secrets.get(&name).await })
+ .map_err(storage_error)?;
+ value
+ .map(|bytes| {
+ String::from_utf8(bytes.to_vec()).map_err(|_| KeyringError::InvalidUtf8 {
+ key: key.to_string(),
+ })
+ })
+ .transpose()
+}
+
+/// Store a secret: in the storage backend when one is configured, else in
+/// the process backend. Overwrites any existing entry. Never logs the value.
+pub fn set(user_id: &str, key: &str, value: &str) -> Result<(), KeyringError> {
+ use tinystoragedrivers::secrets::SecretStore as _;
+ let Some(secrets) = storage_secrets()? else {
+ return process_set(user_id, key, value);
+ };
+ log::debug!("[keyring] set (storage) user_id={user_id} key={key}");
+ let name = namespaced_key(user_id, key);
+ let value = zeroize::Zeroizing::new(value.as_bytes().to_vec());
+ crate::storage::block_on(async move { secrets.set(&name, &value).await })
+ .map_err(storage_error)
+}
+
+/// Delete a secret: from the storage backend when one is configured, else
+/// from the process backend. Idempotent.
+pub fn delete(user_id: &str, key: &str) -> Result<(), KeyringError> {
+ use tinystoragedrivers::secrets::SecretStore as _;
+ let Some(secrets) = storage_secrets()? else {
+ return process_delete(user_id, key);
+ };
+ log::debug!("[keyring] delete (storage) user_id={user_id} key={key}");
+ let name = namespaced_key(user_id, key);
+ crate::storage::block_on(async move { secrets.delete(&name).await.map(|_| ()) })
+ .map_err(storage_error)
+}
+
+// ── Process backend ───────────────────────────────────────────────────────────
+
+
+/// Retrieve a secret from the process backend.
+///
+/// Returns `Ok(None)` when no entry exists for this user + key combination.
+/// Never logs the secret value.
+pub(crate) fn process_get(user_id: &str, key: &str) -> Result<Option<String>, KeyringError> {
log::debug!("[keyring] get user_id={user_id} key={key}");
let namespaced = namespaced_key(user_id, key);
let result = backend().get(&namespaced);
@@ -58,10 +129,10 @@ pub fn get(user_id: &str, key: &str) -> Result<Option<String>, KeyringError> {
result
}
-/// Store a secret in the active backend.
+/// Store a secret in the process backend.
///
/// Overwrites any existing entry for this user + key. Never logs the value.
-pub fn set(user_id: &str, key: &str, value: &str) -> Result<(), KeyringError> {
+pub(crate) fn process_set(user_id: &str, key: &str, value: &str) -> Result<(), KeyringError> {
log::debug!("[keyring] set user_id={user_id} key={key}");
let namespaced = namespaced_key(user_id, key);
let result = backend().set(&namespaced, value);
@@ -75,10 +146,10 @@ pub fn set(user_id: &str, key: &str, value: &str) -> Result<(), KeyringError> {
result
}
-/// Delete a secret from the active backend.
+/// Delete a secret from the process backend.
///
/// Returns `Ok(())` even if no entry existed (idempotent).
-pub fn delete(user_id: &str, key: &str) -> Result<(), KeyringError> {
+pub(crate) fn process_delete(user_id: &str, key: &str) -> Result<(), KeyringError> {
log::debug!("[keyring] delete user_id={user_id} key={key}");
let namespaced = namespaced_key(user_id, key);
let result = backend().delete(&namespaced);
@@ -103,6 +174,10 @@ pub fn delete(user_id: &str, key: &str) -> Result<(), KeyringError> {
/// the macOS access-permission dialogs they trigger) when polled by
/// wallet guards or snapshot loops.
pub fn is_available() -> bool {
+ // Secrets on a storage backend do not depend on the OS keychain.
+ if crate::storage::installed().is_some() {
+ return true;
+ }
let mut cached = AVAILABILITY_CACHE.lock();
if let Some(val) = *cached {
return val;
@@ -150,10 +225,10 @@ fn probe_availability() -> bool {
// Without this, `set` fails with "item already exists" (-25299) on
// every launch after the first, causing `is_available` to incorrectly
// return false even when the keychain is fully functional.
- let _ = delete(PROBE_USER, PROBE_KEY);
- set(PROBE_USER, PROBE_KEY, PROBE_VALUE)?;
- let readback = get(PROBE_USER, PROBE_KEY)?;
- delete(PROBE_USER, PROBE_KEY)?;
+ let _ = process_delete(PROBE_USER, PROBE_KEY);
+ process_set(PROBE_USER, PROBE_KEY, PROBE_VALUE)?;
+ let readback = process_get(PROBE_USER, PROBE_KEY)?;
+ process_delete(PROBE_USER, PROBE_KEY)?;
Ok(readback.as_deref() == Some(PROBE_VALUE))
})();
@@ -195,7 +270,7 @@ pub fn get_or_create_random(
));
}
- if let Some(existing) = get(user_id, key)? {
+ if let Some(existing) = process_get(user_id, key)? {
log::debug!(
"[keyring] get_or_create_random returning existing value user_id={user_id} key={key}"
);
@@ -208,10 +283,10 @@ pub fn get_or_create_random(
let hex_value = hex_encode(&bytes);
log::debug!("[keyring] get_or_create_random creating new entry user_id={user_id} key={key}");
- set(user_id, key, &hex_value)?;
+ process_set(user_id, key, &hex_value)?;
// Verify write succeeded.
- let readback = get(user_id, key)?;
+ let readback = process_get(user_id, key)?;
if readback.as_deref() != Some(&hex_value) {
log::warn!(
"[keyring] get_or_create_random write verification failed user_id={user_id} key={key}"
@@ -246,7 +321,7 @@ pub fn migrate_from_file(
);
// Step 1: check if already migrated.
- if get(user_id, key)?.is_some() {
+ if process_get(user_id, key)?.is_some() {
log::debug!("[keyring] migrate_from_file already migrated user_id={user_id} key={key}");
return Ok(MigrationOutcome::AlreadyMigrated);
}
@@ -274,10 +349,10 @@ pub fn migrate_from_file(
// Step 4: write to backend.
log::debug!("[keyring] migrate_from_file writing to backend user_id={user_id} key={key}");
- set(user_id, key, &value)?;
+ process_set(user_id, key, &value)?;
// Step 5: verify read-back matches.
- let readback = get(user_id, key)?;
+ let readback = process_get(user_id, key)?;
if readback.as_deref() != Some(value.as_str()) {
log::warn!(
"[keyring] migrate_from_file verification failed user_id={user_id} key={key}; NOT deleting source file"
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index c4299297bc..32708a751e 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -18,6 +18,8 @@
//! driver the build does not carry fails at [`open`] naming the feature, so a
//! misconfigured deployment stops at boot instead of at its first write.
+pub mod secrets;
+
use std::future::Future;
use std::sync::{Arc, LazyLock, OnceLock, RwLock};