aKode setup: scelta di dove salvare la chiave API (portachiavi o file protetto)
- nuovo passo «Dove salvo la chiave?» dopo il campo Chiave API (solo se inserita), preselezionato secondo provider_api_key_store; senza portachiavi solo il file - la scelta scrive provider_api_key_store (impostazione globale: file oppure auto), salva la chiave solo nel backend scelto, rimuove la copia nell'altro e sposta le chiavi degli altri provider - akode-login: save_provider_api_key_to(_with_keyring), sonda del portachiavi - test: file 0600 e nessuna chiave in config.toml, portachiavi senza file, cambio con rimozione della vecchia copia; snapshot 60/80/120 senza la chiave Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -23,3 +23,7 @@ pub use provider_api_key::ProviderApiKeyFileStore;
|
||||
pub use provider_api_key::configured_provider_api_key_store;
|
||||
pub use provider_api_key::configured_provider_api_key_store_with_keyring;
|
||||
pub use provider_api_key::install_provider_api_key_store;
|
||||
pub use provider_api_key::provider_api_key_keyring_available;
|
||||
pub use provider_api_key::provider_api_key_keyring_available_with;
|
||||
pub use provider_api_key::save_provider_api_key_to;
|
||||
pub use provider_api_key::save_provider_api_key_to_with_keyring;
|
||||
|
||||
@@ -251,6 +251,82 @@ pub fn install_provider_api_key_store(codex_home: &Path, mode: AuthCredentialsSt
|
||||
));
|
||||
}
|
||||
|
||||
/// Keyring account used only to check that the system keyring answers (never written).
|
||||
const KEYRING_PROBE_PROVIDER_ID: &str = "akode-keyring-probe";
|
||||
|
||||
/// Whether the system keyring can be used for provider keys (a read-only probe).
|
||||
pub fn provider_api_key_keyring_available() -> bool {
|
||||
provider_api_key_keyring_available_with(Arc::new(DefaultKeyringStore))
|
||||
}
|
||||
|
||||
/// [`provider_api_key_keyring_available`] with an explicit keyring backend.
|
||||
pub fn provider_api_key_keyring_available_with(keyring: Arc<dyn KeyringStore>) -> bool {
|
||||
KeyringProviderApiKeyStore::new(keyring)
|
||||
.load(KEYRING_PROBE_PROVIDER_ID)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
/// Stores `api_key` for `provider_id` only in the backend chosen by `mode` (`file`, or the
|
||||
/// system keyring for any other mode) and removes the copy left in the other backend. The
|
||||
/// keys of `other_providers` found in the other backend move to the chosen one, so a global
|
||||
/// mode switch keeps them readable.
|
||||
pub fn save_provider_api_key_to(
|
||||
codex_home: &Path,
|
||||
mode: AuthCredentialsStoreMode,
|
||||
provider_id: &str,
|
||||
api_key: &str,
|
||||
other_providers: &[String],
|
||||
) -> Result<ProviderApiKeyLocation, String> {
|
||||
save_provider_api_key_to_with_keyring(
|
||||
codex_home,
|
||||
mode,
|
||||
provider_id,
|
||||
api_key,
|
||||
other_providers,
|
||||
Arc::new(DefaultKeyringStore),
|
||||
)
|
||||
}
|
||||
|
||||
/// [`save_provider_api_key_to`] with an explicit keyring backend.
|
||||
pub fn save_provider_api_key_to_with_keyring(
|
||||
codex_home: &Path,
|
||||
mode: AuthCredentialsStoreMode,
|
||||
provider_id: &str,
|
||||
api_key: &str,
|
||||
other_providers: &[String],
|
||||
keyring: Arc<dyn KeyringStore>,
|
||||
) -> Result<ProviderApiKeyLocation, String> {
|
||||
if api_key.trim().is_empty() {
|
||||
return Err("API key must not be empty".to_string());
|
||||
}
|
||||
let keyring = KeyringProviderApiKeyStore::new(keyring);
|
||||
let file = ProviderApiKeyFileStore::new(codex_home);
|
||||
let (target, other): (&dyn ProviderApiKeyStore, &dyn ProviderApiKeyStore) = match mode {
|
||||
AuthCredentialsStoreMode::File => (&file, &keyring),
|
||||
_ => (&keyring, &file),
|
||||
};
|
||||
for id in other_providers
|
||||
.iter()
|
||||
.filter(|id| id.as_str() != provider_id)
|
||||
{
|
||||
// An unreadable keyring simply has nothing to move.
|
||||
let Ok(Some(key)) = other.load(id) else {
|
||||
continue;
|
||||
};
|
||||
if !matches!(target.load(id), Ok(Some(_))) {
|
||||
target.save(id, &key)?;
|
||||
}
|
||||
if let Err(error) = other.delete(id) {
|
||||
warn!("could not remove the old copy of a provider API key: {error}");
|
||||
}
|
||||
}
|
||||
let location = target.save(provider_id, api_key)?;
|
||||
if let Err(error) = other.delete(provider_id) {
|
||||
warn!("could not remove the old copy of a provider API key: {error}");
|
||||
}
|
||||
Ok(location)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "provider_api_key_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -141,3 +141,112 @@ fn ephemeral_mode_keeps_keys_in_memory() {
|
||||
assert!(!home.path().join(PROVIDER_API_KEYS_FILE).exists());
|
||||
assert!(!format!("{store:?}").contains(SECRET));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chosen_file_backend_is_private_and_skips_the_keyring() {
|
||||
let home = TempDir::new().unwrap();
|
||||
let keyring = MockKeyringStore::default();
|
||||
let location = save_provider_api_key_to_with_keyring(
|
||||
home.path(),
|
||||
AuthCredentialsStoreMode::File,
|
||||
"imeai",
|
||||
SECRET,
|
||||
&[],
|
||||
Arc::new(keyring.clone()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matches!(location, ProviderApiKeyLocation::File { .. }));
|
||||
assert!(!location.to_string().contains(SECRET));
|
||||
assert!(keyring.saved_value(ACCOUNT).is_none());
|
||||
let path = home.path().join(PROVIDER_API_KEYS_FILE);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mode = std::fs::metadata(&path).unwrap().permissions().mode();
|
||||
assert_eq!(mode & 0o777, 0o600);
|
||||
}
|
||||
assert_eq!(
|
||||
ProviderApiKeyFileStore::new(home.path())
|
||||
.load("imeai")
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some(SECRET)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chosen_keyring_backend_creates_no_file() {
|
||||
let home = TempDir::new().unwrap();
|
||||
let keyring = MockKeyringStore::default();
|
||||
let location = save_provider_api_key_to_with_keyring(
|
||||
home.path(),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
"imeai",
|
||||
SECRET,
|
||||
&[],
|
||||
Arc::new(keyring.clone()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matches!(location, ProviderApiKeyLocation::Keyring { .. }));
|
||||
assert_eq!(keyring.saved_value(ACCOUNT).as_deref(), Some(SECRET));
|
||||
assert!(!home.path().join(PROVIDER_API_KEYS_FILE).exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switching_backend_removes_the_old_copy_and_moves_other_keys() {
|
||||
let home = TempDir::new().unwrap();
|
||||
let keyring = MockKeyringStore::default();
|
||||
let other = "model-provider:altro";
|
||||
for (id, key) in [("imeai", "old-key"), ("altro", "other-key")] {
|
||||
save_provider_api_key_to_with_keyring(
|
||||
home.path(),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
id,
|
||||
key,
|
||||
&[],
|
||||
Arc::new(keyring.clone()),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
// Keyring -> file: nothing stays in the keyring.
|
||||
save_provider_api_key_to_with_keyring(
|
||||
home.path(),
|
||||
AuthCredentialsStoreMode::File,
|
||||
"imeai",
|
||||
SECRET,
|
||||
&["altro".to_string(), "ollama".to_string()],
|
||||
Arc::new(keyring.clone()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(keyring.saved_value(ACCOUNT).is_none());
|
||||
assert!(keyring.saved_value(other).is_none());
|
||||
let file = ProviderApiKeyFileStore::new(home.path());
|
||||
assert_eq!(file.load("imeai").unwrap().as_deref(), Some(SECRET));
|
||||
assert_eq!(file.load("altro").unwrap().as_deref(), Some("other-key"));
|
||||
// File -> keyring: the file goes away with its last key.
|
||||
save_provider_api_key_to_with_keyring(
|
||||
home.path(),
|
||||
AuthCredentialsStoreMode::Keyring,
|
||||
"imeai",
|
||||
SECRET,
|
||||
&["altro".to_string()],
|
||||
Arc::new(keyring.clone()),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(keyring.saved_value(ACCOUNT).as_deref(), Some(SECRET));
|
||||
assert_eq!(keyring.saved_value(other).as_deref(), Some("other-key"));
|
||||
assert!(!home.path().join(PROVIDER_API_KEYS_FILE).exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keyring_probe_reports_an_unusable_keyring() {
|
||||
assert!(provider_api_key_keyring_available_with(Arc::new(
|
||||
MockKeyringStore::default()
|
||||
)));
|
||||
let keyring = MockKeyringStore::default();
|
||||
keyring.set_error(
|
||||
"model-provider:akode-keyring-probe",
|
||||
keyring::Error::NoStorageAccess(std::io::Error::other("no secret service").into()),
|
||||
);
|
||||
assert!(!provider_api_key_keyring_available_with(Arc::new(keyring)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user