Skip to content

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

refactor(secret_storage): move master key file helpers into key_source

Steven Enamakele3a8be9318f4
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 d33a01afc8..90cfb66984 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
@@ -20,7 +20,9 @@ use zeroize::Zeroizing;
#[path = "encrypted_file_backend/key_source.rs"]
pub mod key_source;
-use key_source::{env_value, master_key_from_env};
+use key_source::{env_value, master_key_from_env, read_master_key_file};
+#[cfg(all(test, unix))]
+use key_source::key_file_mode_is_other_writable;
/// Environment variable carrying the master key inline as `2 * KEY_LEN` hex
/// characters (`openssl rand -hex 32`). Lets a headless `openhuman-core
@@ -254,34 +256,6 @@ fn try_load_master_key() -> Result<([u8; KEY_LEN], String), MasterKeyError> {
.map_err(|e| MasterKeyError::Configured(e.to_string()))
}
-/// Reads the file named by [`MASTER_KEY_FILE_ENV`].
-///
-/// On Unix a key file must not be writable by other users. Read-only group or
-/// other permissions are supported for container secret mounts, where the
-/// runtime may add group-read access for a non-root core.
-fn read_master_key_file(path: &Path) -> Result<String, String> {
- #[cfg(unix)]
- {
- use std::os::unix::fs::PermissionsExt;
- let metadata = std::fs::metadata(path).map_err(|e| {
- format!("cannot inspect master key file permissions ({MASTER_KEY_FILE_ENV}): {e}")
- })?;
- let mode = metadata.permissions().mode() & 0o777;
- if key_file_mode_is_other_writable(mode) {
- return Err(format!(
- "master key file ({MASTER_KEY_FILE_ENV}) is writable by other users; \
- restrict it to a read-only secret mount"
- ));
- }
- }
- std::fs::read_to_string(path).map_err(|e| format!("cannot read master key file: {e}"))
-}
-
-#[cfg(unix)]
-fn key_file_mode_is_other_writable(mode: u32) -> bool {
- mode & 0o002 != 0
-}
-
/// Decodes a master key supplied as exactly `2 * KEY_LEN` hex characters.
/// The value never appears in the error.
fn parse_master_key_hex(hex: &str) -> Result<[u8; KEY_LEN], String> {
diff --git a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs
index 4664c84ce4..1dd9335f4b 100644
--- a/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs
+++ b/crates/openhuman-core/src/security/secret_storage/encrypted_file_backend/key_source.rs
@@ -140,10 +140,40 @@ pub(super) fn workspace_master_key(
}
}
+/// Reads the file named by [`MASTER_KEY_FILE_ENV`].
+///
+/// On Unix a key file must not be writable by other users. Read-only group or
+/// other permissions are supported for container secret mounts, where the
+/// runtime may add group-read access for a non-root core.
+pub(super) fn read_master_key_file(path: &Path) -> Result<String, String> {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ let metadata = std::fs::metadata(path).map_err(|e| {
+ format!("cannot inspect master key file permissions ({MASTER_KEY_FILE_ENV}): {e}")
+ })?;
+ let mode = metadata.permissions().mode() & 0o777;
+ if key_file_mode_is_other_writable(mode) {
+ return Err(format!(
+ "master key file ({MASTER_KEY_FILE_ENV}) is writable by other users; \
+ restrict it to a read-only secret mount"
+ ));
+ }
+ }
+ std::fs::read_to_string(path).map_err(|e| format!("cannot read master key file: {e}"))
+}
+
+#[cfg(unix)]
+pub(super) fn key_file_mode_is_other_writable(mode: u32) -> bool {
+ mode & 0o002 != 0
+}
+
/// Resolves a key for a profile-scoped diagnostic read without creating or
/// changing key state. Explicit operator key variables retain their normal
/// precedence; otherwise only an existing key file beneath `workspace` is
/// considered. Host key sources are excluded because they can prompt or mint.
+/// Only the encrypted-file backend reads profiles this way.
+#[cfg(feature = "secret-file")]
pub(super) fn profile_master_key_read_only(
workspace: &Path,
) -> Result<Option<Zeroizing<[u8; 32]>>, SecretStorageError> {
@@ -154,7 +184,7 @@ pub(super) fn profile_master_key_read_only(
if let Some((key, _)) = master_key_from_env(
inline.as_deref(),
file.as_deref(),
- super::read_master_key_file,
+ read_master_key_file,
)
.map_err(SecretStorageError::Backend)?
{