Skip to content

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

feat(security): persist credentials to the storage backend

Steven Enamakelbef7dca6280c
diff --git a/crates/openhuman-core/src/security/credentials/http_creds.rs b/crates/openhuman-core/src/security/credentials/http_creds.rs
index b5ca39ebca..95aa9ebe42 100644
--- a/crates/openhuman-core/src/security/credentials/http_creds.rs
+++ b/crates/openhuman-core/src/security/credentials/http_creds.rs
@@ -334,6 +334,13 @@ impl HttpCredentialsStore {
}
fn read_persisted(&self) -> Result<PersistedHttpCredentials> {
+ if let Some(secrets) = crate::storage::secrets::current()? {
+ return match crate::storage::secrets::get_blocking(&secrets, STORAGE_SECRET_NAME)? {
+ Some(bytes) => serde_json::from_slice(&bytes)
+ .context("http-credentials record on the storage backend is not valid JSON"),
+ None => Ok(PersistedHttpCredentials::default()),
+ };
+ }
if !self.path.exists() {
return Ok(PersistedHttpCredentials::default());
}
@@ -355,6 +362,12 @@ impl HttpCredentialsStore {
}
fn write_persisted(&self, persisted: &PersistedHttpCredentials) -> Result<()> {
+ if let Some(secrets) = crate::storage::secrets::current()? {
+ let json = serde_json::to_vec(persisted)
+ .context("failed to serialize http-credentials store")?;
+ crate::storage::secrets::set_blocking(&secrets, STORAGE_SECRET_NAME, &json)?;
+ return Ok(());
+ }
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent).with_context(|| {
format!(
@@ -388,6 +401,11 @@ impl HttpCredentialsStore {
}
}
+/// The secret the store lives in on a storage backend
+/// ([`crate::storage::secrets`]): the same JSON as the file, encrypted as one
+/// secret in the acting agent's scope.
+const STORAGE_SECRET_NAME: &str = "file:http-credentials.json";
+
fn parse_scheme(raw: &str) -> Option<HttpCredentialScheme> {
match raw.trim().to_ascii_lowercase().as_str() {
"bearer" => Some(HttpCredentialScheme::Bearer),
diff --git a/crates/openhuman-core/src/security/credentials/profiles/persistence.rs b/crates/openhuman-core/src/security/credentials/profiles/persistence.rs
index 0e2ce1afe9..66c376b4b6 100644
--- a/crates/openhuman-core/src/security/credentials/profiles/persistence.rs
+++ b/crates/openhuman-core/src/security/credentials/profiles/persistence.rs
@@ -18,6 +18,12 @@ use super::{
PERSIST_RETRY_BASE_MS, PROFILES_FILENAME,
};
+
+/// The secret the profiles live in on a storage backend
+/// ([`crate::storage::secrets`]): the same JSON as `auth-profiles.json`,
+/// encrypted as one secret in the acting agent's scope.
+const STORAGE_SECRET_NAME: &str = "file:auth-profiles.json";
+
impl AuthProfilesStore {
pub(super) fn save_locked(&self, data: &AuthProfilesData) -> Result<()> {
let mut persisted = PersistedAuthProfiles {
@@ -114,6 +120,28 @@ impl AuthProfilesStore {
}
pub(super) fn read_persisted_locked(&self) -> Result<PersistedAuthProfiles> {
+ if let Some(secrets) = crate::storage::secrets::current()? {
+ let Some(bytes) = crate::storage::secrets::get_blocking(&secrets, STORAGE_SECRET_NAME)?
+ else {
+ return Ok(PersistedAuthProfiles::default());
+ };
+ // No quarantine on a storage backend: an unparseable record is an
+ // error, so a later write can never replace profiles it could not
+ // read.
+ let mut persisted: PersistedAuthProfiles = serde_json::from_slice(&bytes)
+ .context("auth profile record on the storage backend is not valid JSON")?;
+ if persisted.schema_version == 0 {
+ persisted.schema_version = CURRENT_SCHEMA_VERSION;
+ }
+ if persisted.schema_version > CURRENT_SCHEMA_VERSION {
+ anyhow::bail!(
+ "Unsupported auth profile schema version {} (max supported: {})",
+ persisted.schema_version,
+ CURRENT_SCHEMA_VERSION
+ );
+ }
+ return Ok(persisted);
+ }
if !self.path.exists() {
return Ok(PersistedAuthProfiles::default());
}
@@ -163,6 +191,12 @@ impl AuthProfilesStore {
}
pub(super) fn write_persisted_locked(&self, persisted: &PersistedAuthProfiles) -> Result<()> {
+ if let Some(secrets) = crate::storage::secrets::current()? {
+ let json =
+ serde_json::to_vec(persisted).context("Failed to serialize auth profiles")?;
+ crate::storage::secrets::set_blocking(&secrets, STORAGE_SECRET_NAME, &json)?;
+ return Ok(());
+ }
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent).with_context(|| {
format!(
diff --git a/crates/openhuman-core/src/storage/secrets.rs b/crates/openhuman-core/src/storage/secrets.rs
index c234b0661d..9d6398403e 100644
--- a/crates/openhuman-core/src/storage/secrets.rs
+++ b/crates/openhuman-core/src/storage/secrets.rs
@@ -54,6 +54,36 @@ pub fn over(scoped: &ScopedStorage, keys: Arc<dyn KeyProvider>) -> DocumentSecre
DocumentSecrets::new(scoped, keys)
}
+/// Reads secret `name` from synchronous code.
+///
+/// # Errors
+///
+/// A storage or decryption error.
+pub fn get_blocking(
+ secrets: &DocumentSecrets,
+ name: &str,
+) -> Result<Option<Zeroizing<Vec<u8>>>, StorageError> {
+ use tinystoragedrivers::secrets::SecretStore as _;
+ let (secrets, name) = (secrets.clone(), name.to_string());
+ super::block_on(async move { secrets.get(&name).await })
+}
+
+/// Writes secret `name` from synchronous code.
+///
+/// # Errors
+///
+/// A storage or encryption error.
+pub fn set_blocking(
+ secrets: &DocumentSecrets,
+ name: &str,
+ value: &[u8],
+) -> Result<(), StorageError> {
+ use tinystoragedrivers::secrets::SecretStore as _;
+ let (secrets, name) = (secrets.clone(), name.to_string());
+ let value = Zeroizing::new(value.to_vec());
+ super::block_on(async move { secrets.set(&name, &value).await })
+}
+
#[cfg(test)]
#[path = "secrets_tests.rs"]
mod tests;