Skip to content

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

feat(security): store HTTP credentials in encrypted file backend

Steven Enamakel753ee84b9cdd
diff --git a/crates/openhuman-core/src/security/credentials/http_creds.rs b/crates/openhuman-core/src/security/credentials/http_creds.rs
index 9427050595..53833c0b7b 100644
--- a/crates/openhuman-core/src/security/credentials/http_creds.rs
+++ b/crates/openhuman-core/src/security/credentials/http_creds.rs
@@ -192,6 +192,11 @@ struct PersistedHttpCredential {
updated_at: String,
}
+/// Whether `bytes` parse as the persisted HTTP-credentials document.
+pub(crate) fn is_persisted_document(bytes: &[u8]) -> bool {
+ serde_json::from_slice::<PersistedHttpCredentials>(bytes).is_ok()
+}
+
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistedHttpCredentials {
schema_version: u32,
diff --git a/crates/openhuman-core/src/security/credentials/profiles.rs b/crates/openhuman-core/src/security/credentials/profiles.rs
index 52e4cb8bff..b57a1d7328 100644
--- a/crates/openhuman-core/src/security/credentials/profiles.rs
+++ b/crates/openhuman-core/src/security/credentials/profiles.rs
@@ -440,6 +440,12 @@ impl Drop for AuthProfileLockGuard {
}
}
+/// Whether `bytes` parse as the persisted profiles document (what a read from
+/// the storage backend requires).
+pub(crate) fn is_persisted_document(bytes: &[u8]) -> bool {
+ serde_json::from_slice::<PersistedAuthProfiles>(bytes).is_ok()
+}
+
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistedAuthProfiles {
#[serde(default = "default_schema_version")]
diff --git a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs
index 5b266610e6..52acb7737a 100644
--- a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend.rs
@@ -162,7 +162,7 @@ pub(crate) fn storage_master_key() -> Result<[u8; KEY_LEN], String> {
/// Whether the storage master key can be resolved without minting a new one:
/// an operator key (env), a host key source, an already loaded key, or an
-/// existing workspace key file. A layout migration that deletes the plaintext
+/// existing workspace key file (a host source only when it holds a key). A layout migration that deletes the plaintext
/// credential files only imports them when this holds, because a key minted
/// by an offline tool would not match the one the desktop later loads.
pub(crate) fn storage_master_key_resolvable() -> bool {
@@ -172,7 +172,9 @@ pub(crate) fn storage_master_key_resolvable() -> bool {
resolvable(
matches!(MASTER_KEY.get(), Some(Ok(Some(_)))),
env_set(MASTER_KEY_ENV) || env_set(MASTER_KEY_FILE_ENV),
- key_source::installed_master_key_source().is_some(),
+ // A host source counts only when it already holds the key: an empty
+ // one would have a key minted into it by the load.
+ key_source::installed_master_key_source().is_some_and(|source| matches!(source.load(), Ok(Some(_)))),
dir.join("keyring/master.key").is_file(),
)
}
diff --git a/crates/openhuman-core/src/storage/layout/credentials.rs b/crates/openhuman-core/src/storage/layout/credentials.rs
index 58b3b404ec..48d4343f40 100644
--- a/crates/openhuman-core/src/storage/layout/credentials.rs
+++ b/crates/openhuman-core/src/storage/layout/credentials.rs
@@ -22,8 +22,8 @@ use tinystoragedrivers::secrets::SecretStore as _;
use super::state::LayoutError;
use super::Target;
-use crate::security::credentials::http_creds::{STORAGE_SECRET_NAME, STORE_FILENAME};
-use crate::security::credentials::profiles::{PROFILES_FILENAME, PROFILES_SECRET_NAME};
+use crate::security::credentials::http_creds::{self, STORAGE_SECRET_NAME, STORE_FILENAME};
+use crate::security::credentials::profiles::{self, PROFILES_FILENAME, PROFILES_SECRET_NAME};
use crate::storage;
/// The path prefix a profile-level plan entry carries (`@profile/<file>`),
@@ -36,6 +36,43 @@ pub const FILES: [(&str, &str); 2] = [
(STORE_FILENAME, STORAGE_SECRET_NAME),
];
+/// A credential file the import would take: a regular file beside
+/// `config.toml` holding the document its store persists. `None` otherwise
+/// (missing, a symlink, or not that document: left in place).
+fn eligible(profile_dir: &Path, file: &str) -> Option<zeroize::Zeroizing<Vec<u8>>> {
+ let path = profile_dir.join(file);
+ let meta = std::fs::symlink_metadata(&path).ok()?;
+ if !meta.is_file() {
+ tracing::warn!(target: "openhuman::storage", file, "[layout] credential entry is not a regular file; left in place");
+ return None;
+ }
+ let bytes = zeroize::Zeroizing::new(std::fs::read(&path).ok()?);
+ let valid = if file == PROFILES_FILENAME {
+ profiles::is_persisted_document(&bytes)
+ } else {
+ http_creds::is_persisted_document(&bytes)
+ };
+ if !valid {
+ tracing::warn!(target: "openhuman::storage", file, "[layout] credential file is not a valid credential document; left in place");
+ return None;
+ }
+ Some(bytes)
+}
+
+/// The credential files a migration of `profile_dir` would import: the
+/// eligible ones, and none when no master key is resolvable. Read-only; the
+/// dry run uses it so its plan matches the run.
+pub fn candidates(profile_dir: &Path) -> Vec<String> {
+ if !crate::security::secret_storage::encrypted_file_backend::storage_master_key_resolvable() {
+ return Vec::new();
+ }
+ FILES
+ .iter()
+ .filter(|(file, _)| eligible(profile_dir, file).is_some())
+ .map(|(file, _)| (*file).to_owned())
+ .collect()
+}
+
/// Imports the credential files of `target` into the v1 database's `local`
/// scope and returns the file names that are now safe to archive and delete.
///
@@ -50,20 +87,9 @@ pub const FILES: [(&str, &str); 2] = [
pub async fn import(target: &Target) -> Result<Vec<String>, LayoutError> {
let mut files = Vec::new();
for (file, secret) in FILES {
- let path = target.profile_dir.join(file);
- let Ok(meta) = std::fs::symlink_metadata(&path) else {
- continue;
- };
- if !meta.is_file() {
- tracing::warn!(target: "openhuman::storage", file, "[layout] credential entry is not a regular file; left in place");
- continue;
- }
- let bytes = read(&path)?;
- if serde_json::from_slice::<serde_json::Value>(&bytes).is_err() {
- tracing::warn!(target: "openhuman::storage", file, "[layout] credential file is not valid JSON; left in place");
- continue;
+ if let Some(bytes) = eligible(&target.profile_dir, file) {
+ files.push((file, secret, bytes));
}
- files.push((file, secret, bytes));
}
if files.is_empty() {
return Ok(Vec::new());
@@ -109,10 +135,6 @@ pub async fn import(target: &Target) -> Result<Vec<String>, LayoutError> {
Ok(imported)
}
-fn read(path: &Path) -> Result<zeroize::Zeroizing<Vec<u8>>, LayoutError> {
- Ok(zeroize::Zeroizing::new(std::fs::read(path)?))
-}
-
#[cfg(test)]
#[path = "credentials_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/storage/layout/run.rs b/crates/openhuman-core/src/storage/layout/run.rs
index 696c9d5a49..9525d1b475 100644
--- a/crates/openhuman-core/src/storage/layout/run.rs
+++ b/crates/openhuman-core/src/storage/layout/run.rs
@@ -198,10 +198,7 @@ fn dry_run(target: &Target, resuming: bool) -> Result<DryRun, LayoutError> {
None => {
let mut plan = plan::scan(&target.workspace())?;
// What the import step would copy into encrypted secrets.
- let candidates: Vec<String> = super::credentials::FILES
- .iter()
- .map(|(f, _)| (*f).to_owned())
- .collect();
+ let candidates = super::credentials::candidates(&target.profile_dir);
plan.entries
.extend(plan::scan_credentials(&target.profile_dir, &candidates));
plan.entries.sort_by(|a, b| a.path.cmp(&b.path));