aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
459
akode-rs/login/src/auth/storage_tests.rs
Normal file
459
akode-rs/login/src/auth/storage_tests.rs
Normal file
@@ -0,0 +1,459 @@
|
||||
use super::*;
|
||||
use anyhow::Context;
|
||||
use akode_secrets::LocalSecretsNamespace;
|
||||
use akode_secrets::SecretScope;
|
||||
use akode_secrets::SecretsBackendKind;
|
||||
use akode_secrets::SecretsManager;
|
||||
use akode_secrets::compute_keyring_account;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use tempfile::tempdir;
|
||||
|
||||
use akode_keyring_store::tests::MockKeyringStore;
|
||||
use keyring::Error as KeyringError;
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn file_storage_creates_dangling_symlink_target_and_rejects_cycles() -> anyhow::Result<()> {
|
||||
use std::os::unix::fs::symlink;
|
||||
let home = tempdir()?;
|
||||
let auth = serde_json::from_value(json!({"OPENAI_API_KEY":"fake-key"}))?;
|
||||
let storage = FileAuthStorage::new(home.path().to_path_buf());
|
||||
symlink("intermediate", home.path().join("auth.json"))?;
|
||||
symlink("new-target.json", home.path().join("intermediate"))?;
|
||||
storage.save(&auth)?;
|
||||
assert_eq!(storage.load()?, Some(auth.clone()));
|
||||
assert!(
|
||||
std::fs::symlink_metadata(home.path().join("auth.json"))?
|
||||
.file_type()
|
||||
.is_symlink()
|
||||
);
|
||||
std::fs::remove_file(home.path().join("new-target.json"))?;
|
||||
symlink("auth.json", home.path().join("new-target.json"))?;
|
||||
assert_eq!(
|
||||
storage.save(&auth).unwrap_err().kind(),
|
||||
std::io::ErrorKind::InvalidInput
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn file_storage_does_not_replace_read_only_or_hard_linked_credentials() -> anyhow::Result<()> {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let home = tempdir()?;
|
||||
let path = home.path().join("auth.json");
|
||||
let storage = FileAuthStorage::new(home.path().to_path_buf());
|
||||
let first = serde_json::from_value(json!({"OPENAI_API_KEY":"first"}))?;
|
||||
let second = serde_json::from_value(json!({"OPENAI_API_KEY":"second"}))?;
|
||||
storage.save(&first)?;
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o400))?;
|
||||
assert_eq!(
|
||||
storage.save(&second).unwrap_err().kind(),
|
||||
std::io::ErrorKind::PermissionDenied
|
||||
);
|
||||
assert_eq!(storage.load()?, Some(first.clone()));
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?;
|
||||
let peer = home.path().join("peer.json");
|
||||
std::fs::hard_link(&path, &peer)?;
|
||||
assert_eq!(
|
||||
storage.save(&second).unwrap_err().kind(),
|
||||
std::io::ErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(std::fs::read(path)?, std::fs::read(peer)?);
|
||||
assert_eq!(storage.load()?, Some(first));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn file_storage_load_returns_auth_dot_json() -> anyhow::Result<()> {
|
||||
let codex_home = tempdir()?;
|
||||
let storage = FileAuthStorage::new(codex_home.path().to_path_buf());
|
||||
let auth_dot_json = AuthDotJson {
|
||||
auth_mode: Some(AuthMode::ApiKey),
|
||||
openai_api_key: Some("test-key".to_string()),
|
||||
};
|
||||
|
||||
storage
|
||||
.save(&auth_dot_json)
|
||||
.context("failed to save auth file")?;
|
||||
|
||||
let loaded = storage.load().context("failed to load auth file")?;
|
||||
assert_eq!(Some(auth_dot_json), loaded);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn file_storage_save_persists_auth_dot_json() -> anyhow::Result<()> {
|
||||
let codex_home = tempdir()?;
|
||||
let storage = FileAuthStorage::new(codex_home.path().to_path_buf());
|
||||
let auth_dot_json = AuthDotJson {
|
||||
auth_mode: Some(AuthMode::ApiKey),
|
||||
openai_api_key: Some("test-key".to_string()),
|
||||
};
|
||||
|
||||
let file = get_auth_file(codex_home.path());
|
||||
storage
|
||||
.save(&auth_dot_json)
|
||||
.context("failed to save auth file")?;
|
||||
|
||||
let same_auth_dot_json = storage
|
||||
.try_read_auth_json(&file)
|
||||
.context("failed to read auth file after save")?;
|
||||
assert_eq!(auth_dot_json, same_auth_dot_json);
|
||||
|
||||
// An overlapping reader must keep seeing the complete old document.
|
||||
let mut overlapping_reader = File::open(&file)?;
|
||||
let mut replacement = auth_dot_json.clone();
|
||||
replacement.openai_api_key = Some("replacement-with-a-different-length".to_string());
|
||||
storage
|
||||
.save(&replacement)
|
||||
.context("atomically replace auth while the original reader remains open")?;
|
||||
let mut original_contents = String::new();
|
||||
overlapping_reader.read_to_string(&mut original_contents)?;
|
||||
assert_eq!(
|
||||
serde_json::from_str::<AuthDotJson>(&original_contents)?,
|
||||
auth_dot_json
|
||||
);
|
||||
assert_eq!(storage.load()?, Some(replacement));
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
assert_eq!(
|
||||
std::fs::metadata(&file)?.permissions().mode() & 0o777,
|
||||
0o600
|
||||
);
|
||||
|
||||
let linked_home = tempdir()?;
|
||||
let link = get_auth_file(linked_home.path());
|
||||
std::os::unix::fs::symlink(&file, &link)?;
|
||||
FileAuthStorage::new(linked_home.path().to_path_buf()).save(&auth_dot_json)?;
|
||||
assert!(std::fs::symlink_metadata(link)?.file_type().is_symlink());
|
||||
assert_eq!(storage.load()?, Some(auth_dot_json));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_storage_failed_replacement_cleans_up_temporary_file() -> anyhow::Result<()> {
|
||||
let home = tempdir()?;
|
||||
let auth_path = get_auth_file(home.path());
|
||||
// A directory cannot be replaced by an auth file on any supported platform.
|
||||
std::fs::create_dir(&auth_path)?;
|
||||
let storage = FileAuthStorage::new(home.path().to_path_buf());
|
||||
let auth = serde_json::from_value(json!({"OPENAI_API_KEY": "test-key"}))?;
|
||||
assert!(storage.save(&auth).is_err());
|
||||
let entries = std::fs::read_dir(home.path())?
|
||||
.map(|entry| entry.map(|entry| entry.file_name()))
|
||||
.collect::<std::io::Result<Vec<_>>>()?;
|
||||
assert_eq!(entries, vec![std::ffi::OsString::from("auth.json")]);
|
||||
assert!(auth_path.is_dir());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_storage_delete_removes_auth_file() -> anyhow::Result<()> {
|
||||
let dir = tempdir()?;
|
||||
let auth_dot_json = AuthDotJson {
|
||||
auth_mode: Some(AuthMode::ApiKey),
|
||||
openai_api_key: Some("sk-test-key".to_string()),
|
||||
};
|
||||
let storage = create_auth_storage(
|
||||
dir.path().to_path_buf(),
|
||||
AuthCredentialsStoreMode::File,
|
||||
AuthKeyringBackendKind::default(),
|
||||
);
|
||||
storage.save(&auth_dot_json)?;
|
||||
assert!(dir.path().join("auth.json").exists());
|
||||
let storage = FileAuthStorage::new(dir.path().to_path_buf());
|
||||
let removed = storage.delete()?;
|
||||
assert!(removed);
|
||||
assert!(!dir.path().join("auth.json").exists());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ephemeral_storage_save_load_delete_is_in_memory_only() -> anyhow::Result<()> {
|
||||
let dir = tempdir()?;
|
||||
let storage = create_auth_storage(
|
||||
dir.path().to_path_buf(),
|
||||
AuthCredentialsStoreMode::Ephemeral,
|
||||
AuthKeyringBackendKind::default(),
|
||||
);
|
||||
let auth_dot_json = AuthDotJson {
|
||||
auth_mode: Some(AuthMode::ApiKey),
|
||||
openai_api_key: Some("sk-ephemeral".to_string()),
|
||||
};
|
||||
|
||||
storage.save(&auth_dot_json)?;
|
||||
let loaded = storage.load()?;
|
||||
assert_eq!(Some(auth_dot_json), loaded);
|
||||
|
||||
let removed = storage.delete()?;
|
||||
assert!(removed);
|
||||
let loaded = storage.load()?;
|
||||
assert_eq!(None, loaded);
|
||||
assert!(!get_auth_file(dir.path()).exists());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn seed_secrets_backend_and_fallback_auth_file_for_delete(
|
||||
mock_keyring: &MockKeyringStore,
|
||||
codex_home: &Path,
|
||||
auth: &AuthDotJson,
|
||||
) -> anyhow::Result<PathBuf> {
|
||||
let manager = SecretsManager::new_with_keyring_store_and_namespace(
|
||||
codex_home.to_path_buf(),
|
||||
SecretsBackendKind::Local,
|
||||
Arc::new(mock_keyring.clone()),
|
||||
LocalSecretsNamespace::CodexAuth,
|
||||
);
|
||||
manager.set(
|
||||
&SecretScope::Global,
|
||||
&AKODE_AUTH_SECRET_NAME,
|
||||
&serde_json::to_string(auth)?,
|
||||
)?;
|
||||
let auth_file = get_auth_file(codex_home);
|
||||
std::fs::write(&auth_file, "stale")?;
|
||||
Ok(auth_file)
|
||||
}
|
||||
|
||||
fn seed_secrets_backend_with_auth(
|
||||
mock_keyring: &MockKeyringStore,
|
||||
codex_home: &Path,
|
||||
auth: &AuthDotJson,
|
||||
) -> anyhow::Result<()> {
|
||||
let manager = SecretsManager::new_with_keyring_store_and_namespace(
|
||||
codex_home.to_path_buf(),
|
||||
SecretsBackendKind::Local,
|
||||
Arc::new(mock_keyring.clone()),
|
||||
LocalSecretsNamespace::CodexAuth,
|
||||
);
|
||||
manager.set(
|
||||
&SecretScope::Global,
|
||||
&AKODE_AUTH_SECRET_NAME,
|
||||
&serde_json::to_string(auth)?,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn assert_keyring_saved_auth_and_removed_fallback(
|
||||
mock_keyring: &MockKeyringStore,
|
||||
codex_home: &Path,
|
||||
expected: &AuthDotJson,
|
||||
) -> anyhow::Result<()> {
|
||||
let manager = SecretsManager::new_with_keyring_store_and_namespace(
|
||||
codex_home.to_path_buf(),
|
||||
SecretsBackendKind::Local,
|
||||
Arc::new(mock_keyring.clone()),
|
||||
LocalSecretsNamespace::CodexAuth,
|
||||
);
|
||||
let saved_value = manager
|
||||
.get(&SecretScope::Global, &AKODE_AUTH_SECRET_NAME)?
|
||||
.context("encrypted auth entry should exist")?;
|
||||
let expected_serialized = serde_json::to_string(expected)?;
|
||||
assert_eq!(saved_value, expected_serialized);
|
||||
let old_key = compute_store_key(codex_home)?;
|
||||
assert!(
|
||||
mock_keyring.saved_value(&old_key).is_none(),
|
||||
"legacy keyring auth entry should not be used"
|
||||
);
|
||||
let secrets_key = compute_keyring_account(codex_home, LocalSecretsNamespace::CodexAuth);
|
||||
assert!(
|
||||
mock_keyring.saved_value(&secrets_key).is_some(),
|
||||
"secrets backend should persist an encryption passphrase in the keyring"
|
||||
);
|
||||
assert!(encrypted_auth_file(codex_home).exists());
|
||||
let auth_file = get_auth_file(codex_home);
|
||||
assert!(
|
||||
!auth_file.exists(),
|
||||
"fallback auth.json should be removed after keyring save"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn encrypted_auth_file(codex_home: &Path) -> PathBuf {
|
||||
codex_home.join("secrets").join("codex_auth.age")
|
||||
}
|
||||
|
||||
fn auth_with_prefix(prefix: &str) -> AuthDotJson {
|
||||
AuthDotJson {
|
||||
auth_mode: Some(AuthMode::ApiKey),
|
||||
openai_api_key: Some(format!("{prefix}-api-key")),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secrets_keyring_auth_storage_load_returns_deserialized_auth() -> anyhow::Result<()> {
|
||||
let codex_home = tempdir()?;
|
||||
let mock_keyring = MockKeyringStore::default();
|
||||
let storage = SecretsKeyringAuthStorage::new(
|
||||
codex_home.path().to_path_buf(),
|
||||
Arc::new(mock_keyring.clone()),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
);
|
||||
let expected = AuthDotJson {
|
||||
auth_mode: Some(AuthMode::ApiKey),
|
||||
openai_api_key: Some("sk-test".to_string()),
|
||||
};
|
||||
seed_secrets_backend_with_auth(&mock_keyring, codex_home.path(), &expected)?;
|
||||
|
||||
let loaded = storage.load()?;
|
||||
assert_eq!(Some(expected), loaded);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keyring_auth_storage_compute_store_key_for_home_directory() -> anyhow::Result<()> {
|
||||
let codex_home = PathBuf::from("~/.akode");
|
||||
|
||||
let key = compute_store_key(codex_home.as_path())?;
|
||||
|
||||
assert_eq!(key, "cli|940db7b1d0e4eb40");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn direct_keyring_auth_storage_delete_removes_keyring_and_file() -> anyhow::Result<()> {
|
||||
let codex_home = tempdir()?;
|
||||
let mock_keyring = MockKeyringStore::default();
|
||||
let storage = DirectKeyringAuthStorage::new(
|
||||
codex_home.path().to_path_buf(),
|
||||
Arc::new(mock_keyring.clone()),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
);
|
||||
let auth = auth_with_prefix("direct-delete");
|
||||
storage.save(&auth)?;
|
||||
let auth_file = get_auth_file(codex_home.path());
|
||||
std::fs::write(&auth_file, "stale")?;
|
||||
|
||||
let removed = storage.delete()?;
|
||||
|
||||
assert!(removed, "delete should report removal");
|
||||
assert_eq!(storage.load()?, None, "keyring auth should be removed");
|
||||
assert!(
|
||||
mock_keyring
|
||||
.saved_value(&compute_store_key(codex_home.path())?)
|
||||
.is_none(),
|
||||
"legacy keyring auth entry should be removed"
|
||||
);
|
||||
assert!(
|
||||
!auth_file.exists(),
|
||||
"fallback auth.json should be removed after keyring delete"
|
||||
);
|
||||
assert!(!encrypted_auth_file(codex_home.path()).exists());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn factory_uses_secrets_backend_only_when_requested() -> anyhow::Result<()> {
|
||||
let direct_home = tempdir()?;
|
||||
let direct_keyring = MockKeyringStore::default();
|
||||
let direct_storage = create_auth_storage_with_store(
|
||||
direct_home.path().to_path_buf(),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
Arc::new(direct_keyring.clone()),
|
||||
AuthKeyringBackendKind::Direct,
|
||||
);
|
||||
let direct_auth = auth_with_prefix("factory-direct");
|
||||
direct_storage.save(&direct_auth)?;
|
||||
assert!(
|
||||
direct_keyring
|
||||
.saved_value(&compute_store_key(direct_home.path())?)
|
||||
.is_some()
|
||||
);
|
||||
assert!(!encrypted_auth_file(direct_home.path()).exists());
|
||||
|
||||
let secrets_home = tempdir()?;
|
||||
let secrets_keyring = MockKeyringStore::default();
|
||||
let secrets_storage = create_auth_storage_with_store(
|
||||
secrets_home.path().to_path_buf(),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
Arc::new(secrets_keyring.clone()),
|
||||
AuthKeyringBackendKind::Secrets,
|
||||
);
|
||||
let secrets_auth = auth_with_prefix("factory-secrets");
|
||||
secrets_storage.save(&secrets_auth)?;
|
||||
assert!(
|
||||
secrets_keyring
|
||||
.saved_value(&compute_keyring_account(
|
||||
secrets_home.path(),
|
||||
LocalSecretsNamespace::CodexAuth,
|
||||
))
|
||||
.is_some()
|
||||
);
|
||||
assert!(encrypted_auth_file(secrets_home.path()).exists());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secrets_keyring_auth_storage_delete_removes_keyring_and_file() -> anyhow::Result<()> {
|
||||
let codex_home = tempdir()?;
|
||||
let mock_keyring = MockKeyringStore::default();
|
||||
let storage = SecretsKeyringAuthStorage::new(
|
||||
codex_home.path().to_path_buf(),
|
||||
Arc::new(mock_keyring.clone()),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
);
|
||||
let auth = auth_with_prefix("to-delete");
|
||||
let auth_file = seed_secrets_backend_and_fallback_auth_file_for_delete(
|
||||
&mock_keyring,
|
||||
codex_home.path(),
|
||||
&auth,
|
||||
)?;
|
||||
|
||||
let removed = storage.delete()?;
|
||||
|
||||
assert!(removed, "delete should report removal");
|
||||
assert_eq!(storage.load()?, None, "encrypted auth should be removed");
|
||||
assert!(
|
||||
!auth_file.exists(),
|
||||
"fallback auth.json should be removed after keyring delete"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secrets_keyring_auth_storage_delete_removes_legacy_direct_keyring_entry() -> anyhow::Result<()> {
|
||||
let codex_home = tempdir()?;
|
||||
let mock_keyring = MockKeyringStore::default();
|
||||
let direct_storage = DirectKeyringAuthStorage::new(
|
||||
codex_home.path().to_path_buf(),
|
||||
Arc::new(mock_keyring.clone()),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
);
|
||||
direct_storage.save(&auth_with_prefix("legacy-direct"))?;
|
||||
let storage = SecretsKeyringAuthStorage::new(
|
||||
codex_home.path().to_path_buf(),
|
||||
Arc::new(mock_keyring.clone()),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
);
|
||||
let auth = auth_with_prefix("to-delete");
|
||||
let auth_file = seed_secrets_backend_and_fallback_auth_file_for_delete(
|
||||
&mock_keyring,
|
||||
codex_home.path(),
|
||||
&auth,
|
||||
)?;
|
||||
|
||||
let removed = storage.delete()?;
|
||||
|
||||
assert!(removed, "delete should report removal");
|
||||
assert_eq!(storage.load()?, None, "encrypted auth should be removed");
|
||||
assert_eq!(
|
||||
direct_storage.load()?,
|
||||
None,
|
||||
"legacy direct keyring auth should be removed"
|
||||
);
|
||||
assert!(
|
||||
!auth_file.exists(),
|
||||
"fallback auth.json should be removed after keyring delete"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[path = "storage_policy_tests.rs"]
|
||||
mod policy;
|
||||
|
||||
#[path = "storage_error_tests.rs"]
|
||||
mod errors;
|
||||
Reference in New Issue
Block a user