Skip to content

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

test(keyring): add fixture compatibility tests for encrypted file backend

Steven Enamakel06f86887ea2a
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..c835df309b 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -544,7 +544,7 @@ impl EncryptedFileBackend {
}
/// [`KeyringBackend::get`] under an explicit master key.
- fn get_with_key(
+ pub(super) fn get_with_key(
&self,
key: &[u8; KEY_LEN],
namespaced_key: &str,
@@ -559,7 +559,7 @@ impl EncryptedFileBackend {
}
/// [`KeyringBackend::set`] under an explicit master key.
- fn set_with_key(
+ pub(super) fn set_with_key(
&self,
key: &[u8; KEY_LEN],
namespaced_key: &str,
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
index 9e8f5eb69a..a0a30730da 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
@@ -483,23 +483,3 @@ fn init_once_caches_a_loaded_key_and_a_keychain_outage_as_ok() {
Ok(())
);
}
-
-#[test]
-fn zz_generate_fixtures_tmp() {
- let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/security/keyring/fixtures");
- std::fs::create_dir_all(&dir).unwrap();
- let key = [0x42u8; KEY_LEN];
- let tmp = tempfile::TempDir::new().unwrap();
- let b = EncryptedFileBackend::new(tmp.path());
- b.set_with_key(&key, "user-1:session_token", "tok-abc-123").unwrap();
- b.set_with_key(&key, "user-1:api_key", "sk-fixture-\u{00e9}\u{4e2d}").unwrap();
- b.set_with_key(&key, "user-2:session_token", "tok-other").unwrap();
- std::fs::copy(tmp.path().join("secrets.enc"), dir.join("secrets.enc")).unwrap();
- let store_dir = tempfile::TempDir::new().unwrap();
- std::fs::write(store_dir.path().join(".secret_key"), crypto::hex_encode(&key)).unwrap();
- let enc = |p: &str| {
- tinystoragedrivers::secrets::crypto::encrypt_enc2(&key, p.as_bytes()).unwrap()
- };
- let values = format!("{}\n{}\n", enc("sk-test-fixture-value"), enc("pa\u{00df}word \u{1f511}"));
- std::fs::write(dir.join("enc2_values.txt"), values).unwrap();
-}
diff --git a/crates/openhuman-core/src/security/keyring/fixture_compat_tests.rs b/crates/openhuman-core/src/security/keyring/fixture_compat_tests.rs
new file mode 100644
index 0000000000..e8aaa073ef
--- /dev/null
+++ b/crates/openhuman-core/src/security/keyring/fixture_compat_tests.rs
@@ -0,0 +1,65 @@
+//! Compatibility fixtures: bytes written by the pre-`SecretStore`-port code.
+//!
+//! `fixtures/secrets.enc` and `fixtures/enc2_values.txt` were generated by
+//! `EncryptedFileBackend` and `crypto::encrypt_enc2` BEFORE the keyring
+//! backends became adapters over `tinystoragedrivers`' `SecretStore`, and were
+//! committed untouched. They pin the on-disk formats: a user upgrading must
+//! still read every secret, and a downgrade must still read what the new code
+//! writes. Never regenerate them to make a test pass.
+
+use super::encrypted_file_backend::EncryptedFileBackend;
+use super::KeyringBackend;
+use tinystoragedrivers::secrets::crypto;
+
+const FIXTURE_KEY: [u8; 32] = [0x42; 32];
+const SECRETS_ENC: &[u8] = include_bytes!("fixtures/secrets.enc");
+const ENC2_VALUES: &str = include_str!("fixtures/enc2_values.txt");
+
+fn fixture_dir() -> tempfile::TempDir {
+ let dir = tempfile::TempDir::new().unwrap();
+ std::fs::write(dir.path().join("secrets.enc"), SECRETS_ENC).unwrap();
+ dir
+}
+
+#[test]
+fn enc2_fixture_values_decrypt_under_the_fixture_key() {
+ let plain: Vec<String> = ENC2_VALUES
+ .lines()
+ .map(|line| {
+ assert!(line.starts_with("enc2:"));
+ String::from_utf8(crypto::decrypt_enc2(&FIXTURE_KEY, line).unwrap().to_vec()).unwrap()
+ })
+ .collect();
+ assert_eq!(plain, ["sk-test-fixture-value", "pa\u{00df}word \u{1f511}"]);
+}
+
+#[test]
+fn secrets_enc_fixture_reads_through_the_backend() {
+ let dir = fixture_dir();
+ let backend = EncryptedFileBackend::new(dir.path());
+ let get = |k: &str| backend.get_with_key(&FIXTURE_KEY, k).unwrap();
+ assert_eq!(get("user-1:session_token").as_deref(), Some("tok-abc-123"));
+ assert_eq!(
+ get("user-1:api_key").as_deref(),
+ Some("sk-fixture-\u{00e9}\u{4e2d}")
+ );
+ assert_eq!(get("user-2:session_token").as_deref(), Some("tok-other"));
+ assert_eq!(get("user-1:missing"), None);
+}
+
+#[test]
+fn secrets_enc_fixture_survives_a_write_and_stays_in_format() {
+ let dir = fixture_dir();
+ let backend = EncryptedFileBackend::new(dir.path());
+ backend
+ .set_with_key(&FIXTURE_KEY, "user-3:new", "added")
+ .unwrap();
+ // The file is still one raw `nonce || ciphertext || tag` blob over a JSON
+ // object of strings, holding the old entries and the new one.
+ let blob = std::fs::read(dir.path().join("secrets.enc")).unwrap();
+ let json = crypto::decrypt(&FIXTURE_KEY, &blob).unwrap();
+ let map: std::collections::BTreeMap<String, String> = serde_json::from_slice(&json).unwrap();
+ assert_eq!(map.len(), 4);
+ assert_eq!(map["user-3:new"], "added");
+ assert_eq!(map["user-1:session_token"], "tok-abc-123");
+}
diff --git a/crates/openhuman-core/src/security/keyring/fixtures/enc2_values.txt b/crates/openhuman-core/src/security/keyring/fixtures/enc2_values.txt
new file mode 100644
index 0000000000..b1beed4966
--- /dev/null
+++ b/crates/openhuman-core/src/security/keyring/fixtures/enc2_values.txt
@@ -0,0 +1,2 @@
+enc2:79d133de946033592a2d679e2ffbeb3e9674b0d46624c0b603b6df135a1d223638c831e52ac426402e4391cf0a7c80a6ce
+enc2:249df4bcde146f2dd9480c1e92772f3d47d2495396796faf8f4c2af0b7b80a90da892c53f36487d4b0
diff --git a/crates/openhuman-core/src/security/keyring/mod.rs b/crates/openhuman-core/src/security/keyring/mod.rs
index 12f8be5317..020a061028 100644
--- a/crates/openhuman-core/src/security/keyring/mod.rs
+++ b/crates/openhuman-core/src/security/keyring/mod.rs
@@ -63,6 +63,10 @@ pub use store::init_workspace;
#[allow(unused_imports)]
pub(crate) use ops::force_backend_for_test;
+#[cfg(test)]
+#[path = "fixture_compat_tests.rs"]
+mod fixture_compat_tests;
+
#[cfg(test)]
#[path = "keyring_tests.rs"]
mod tests;