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:
6
akode-rs/keyring-store/BUILD.bazel
Normal file
6
akode-rs/keyring-store/BUILD.bazel
Normal file
@@ -0,0 +1,6 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "keyring-store",
|
||||
crate_name = "akode_keyring_store",
|
||||
)
|
||||
32
akode-rs/keyring-store/Cargo.toml
Normal file
32
akode-rs/keyring-store/Cargo.toml
Normal file
@@ -0,0 +1,32 @@
|
||||
[package]
|
||||
name = "akode-keyring-store"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
keyring = { workspace = true, features = ["crypto-rust"] }
|
||||
tracing = { workspace = true }
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
secret-service = "4"
|
||||
keyring = { workspace = true, features = ["linux-native-async-persistent"] }
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
security-framework = "3"
|
||||
keyring = { workspace = true, features = ["apple-native"] }
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
keyring = { workspace = true, features = ["windows-native"] }
|
||||
|
||||
[target.'cfg(any(target_os = "freebsd", target_os = "openbsd"))'.dependencies]
|
||||
keyring = { workspace = true, features = ["sync-secret-service"] }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
51
akode-rs/keyring-store/src/error_display_tests.rs
Normal file
51
akode-rs/keyring-store/src/error_display_tests.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
//! Ensures credential payloads cannot escape through diagnostic error formatting.
|
||||
|
||||
use super::*;
|
||||
use keyring::credential::CredentialApi;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn payload_bearing_errors_redact_credentials_and_end_the_source_chain() {
|
||||
let credential = keyring::mock::MockCredential::default();
|
||||
credential.set_password("private-credential-value").unwrap();
|
||||
for (error, message) in [
|
||||
(
|
||||
KeyringError::Ambiguous(vec![Box::new(credential) as Box<keyring::Credential>]),
|
||||
"Entry is matched by 1 credentials",
|
||||
),
|
||||
(
|
||||
KeyringError::BadEncoding(b"private-credential-value".to_vec()),
|
||||
"Data is not UTF-8 encoded",
|
||||
),
|
||||
] {
|
||||
let error = CredentialStoreError::new(error);
|
||||
assert_eq!(
|
||||
(
|
||||
error.to_string(),
|
||||
error.message(),
|
||||
format!("{error:?}"),
|
||||
error.source().is_none()
|
||||
),
|
||||
(
|
||||
message.to_string(),
|
||||
message.to_string(),
|
||||
message.to_string(),
|
||||
true
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn debug_preserves_native_error_details() {
|
||||
let error = CredentialStoreError::new(KeyringError::PlatformFailure(Box::new(
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"permission denied by keyring",
|
||||
),
|
||||
)));
|
||||
assert_eq!(
|
||||
format!("{error:?}"),
|
||||
"Platform secure storage failure: permission denied by keyring",
|
||||
);
|
||||
}
|
||||
51
akode-rs/keyring-store/src/error_kind.rs
Normal file
51
akode-rs/keyring-store/src/error_kind.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
//! Maps reliable native failures to portable IO kinds without examining error messages.
|
||||
//! Ambiguous keyring errors stay Other: NoStorageAccess alone does not prove a locked store.
|
||||
|
||||
use crate::CredentialStoreError;
|
||||
use std::error::Error;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
pub(super) fn classify(error: &CredentialStoreError) -> ErrorKind {
|
||||
let mut cause: Option<&(dyn Error + 'static)> = Some(error);
|
||||
while let Some(error) = cause {
|
||||
if let Some(error) = error.downcast_ref::<std::io::Error>() {
|
||||
return error.kind();
|
||||
}
|
||||
if let Some(error) = error.downcast_ref::<std::sync::Arc<std::io::Error>>() {
|
||||
return error.kind();
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Some(error) = error.downcast_ref::<security_framework::base::Error>() {
|
||||
return match error.code() {
|
||||
-25291 => ErrorKind::NotConnected, // errSecNotAvailable
|
||||
-25292 | -25293 => ErrorKind::PermissionDenied, // errSecReadOnly / errSecAuthFailed
|
||||
-4 => ErrorKind::Unsupported, // errSecUnimplemented
|
||||
_ => ErrorKind::Other,
|
||||
};
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
if let Some(error) = error.downcast_ref::<secret_service::Error>() {
|
||||
match error {
|
||||
secret_service::Error::Locked => return ErrorKind::WouldBlock,
|
||||
secret_service::Error::Unavailable => return ErrorKind::NotConnected,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
if let Some(error) = error.downcast_ref::<keyring::windows::Error>() {
|
||||
return match error.0 {
|
||||
1312 => ErrorKind::NotConnected, // ERROR_NO_SUCH_LOGON_SESSION
|
||||
5 => ErrorKind::PermissionDenied, // ERROR_ACCESS_DENIED
|
||||
50 => ErrorKind::Unsupported, // ERROR_NOT_SUPPORTED
|
||||
1460 => ErrorKind::TimedOut, // ERROR_TIMEOUT
|
||||
_ => ErrorKind::Other,
|
||||
};
|
||||
}
|
||||
cause = error.source();
|
||||
}
|
||||
ErrorKind::Other
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "error_kind_tests.rs"]
|
||||
mod tests;
|
||||
102
akode-rs/keyring-store/src/error_kind_tests.rs
Normal file
102
akode-rs/keyring-store/src/error_kind_tests.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
//! Tests native cause classification without touching the real keyring.
|
||||
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn preserves_typed_causes_without_guessing_from_messages() {
|
||||
let cases = [
|
||||
(
|
||||
keyring::Error::NoStorageAccess(Box::new(std::io::Error::other(
|
||||
"locked timeout access denied",
|
||||
))),
|
||||
ErrorKind::Other,
|
||||
),
|
||||
(
|
||||
keyring::Error::PlatformFailure(Box::new(std::io::Error::new(
|
||||
ErrorKind::TimedOut,
|
||||
"private account",
|
||||
))),
|
||||
ErrorKind::TimedOut,
|
||||
),
|
||||
(
|
||||
keyring::Error::PlatformFailure(Box::new(std::sync::Arc::new(std::io::Error::new(
|
||||
ErrorKind::PermissionDenied,
|
||||
"private D-Bus socket",
|
||||
)))),
|
||||
ErrorKind::PermissionDenied,
|
||||
),
|
||||
(
|
||||
keyring::Error::Invalid("private account".into(), "private reason".into()),
|
||||
ErrorKind::Other,
|
||||
),
|
||||
];
|
||||
for (error, expected) in cases {
|
||||
let error = std::io::Error::from(CredentialStoreError::new(error));
|
||||
assert_eq!(error.kind(), expected);
|
||||
assert!(
|
||||
error
|
||||
.get_ref()
|
||||
.unwrap()
|
||||
.downcast_ref::<CredentialStoreError>()
|
||||
.is_some()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_native_backend_errors() {
|
||||
#[cfg(target_os = "macos")]
|
||||
let native: Box<dyn Error + Send + Sync> = Box::new(
|
||||
security_framework::base::Error::from_code(/*code*/ -25291),
|
||||
);
|
||||
#[cfg(target_os = "linux")]
|
||||
let native: Box<dyn Error + Send + Sync> = Box::new(secret_service::Error::Locked);
|
||||
#[cfg(target_os = "windows")]
|
||||
let native: Box<dyn Error + Send + Sync> = Box::new(keyring::windows::Error(1312));
|
||||
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
|
||||
assert_eq!(
|
||||
classify(&CredentialStoreError::new(keyring::Error::NoStorageAccess(
|
||||
native
|
||||
))),
|
||||
if cfg!(target_os = "linux") {
|
||||
ErrorKind::WouldBlock
|
||||
} else {
|
||||
ErrorKind::NotConnected
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn unavailable_secret_service_is_not_connected() {
|
||||
let error = CredentialStoreError::new(keyring::Error::PlatformFailure(Box::new(
|
||||
secret_service::Error::Unavailable,
|
||||
)));
|
||||
assert_eq!(std::io::Error::from(error).kind(), ErrorKind::NotConnected);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn secret_service_dbus_io_error_preserves_its_kind() {
|
||||
let error = secret_service::Error::Zbus(
|
||||
std::io::Error::new(ErrorKind::PermissionDenied, "private D-Bus socket").into(),
|
||||
);
|
||||
let error = CredentialStoreError::new(keyring::Error::PlatformFailure(Box::new(error)));
|
||||
assert_eq!(
|
||||
std::io::Error::from(error).kind(),
|
||||
ErrorKind::PermissionDenied
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn read_only_keychain_is_permission_denied() {
|
||||
let error = CredentialStoreError::new(keyring::Error::NoStorageAccess(Box::new(
|
||||
security_framework::base::Error::from_code(/*code*/ -25292),
|
||||
)));
|
||||
assert_eq!(
|
||||
std::io::Error::from(error).kind(),
|
||||
ErrorKind::PermissionDenied
|
||||
);
|
||||
}
|
||||
249
akode-rs/keyring-store/src/lib.rs
Normal file
249
akode-rs/keyring-store/src/lib.rs
Normal file
@@ -0,0 +1,249 @@
|
||||
use keyring::Entry;
|
||||
use keyring::Error as KeyringError;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
use tracing::trace;
|
||||
|
||||
pub enum CredentialStoreError {
|
||||
Other(KeyringError),
|
||||
}
|
||||
|
||||
impl CredentialStoreError {
|
||||
pub fn new(error: KeyringError) -> Self {
|
||||
Self::Other(error)
|
||||
}
|
||||
|
||||
pub fn message(&self) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
|
||||
pub fn into_error(self) -> KeyringError {
|
||||
match self {
|
||||
Self::Other(error) => error,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CredentialStoreError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Other(KeyringError::Ambiguous(items)) => {
|
||||
write!(f, "Entry is matched by {} credentials", items.len())
|
||||
}
|
||||
Self::Other(error) => write!(f, "{error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for CredentialStoreError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for CredentialStoreError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
match self {
|
||||
// Do not expose payload-bearing variants to error-chain formatters.
|
||||
Self::Other(KeyringError::Ambiguous(_) | KeyringError::BadEncoding(_)) => None,
|
||||
Self::Other(error) => Some(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "error_display_tests.rs"]
|
||||
mod error_display_tests;
|
||||
|
||||
mod error_kind;
|
||||
|
||||
impl From<CredentialStoreError> for std::io::Error {
|
||||
fn from(error: CredentialStoreError) -> Self {
|
||||
Self::new(error_kind::classify(&error), error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared credential store abstraction for keyring-backed implementations.
|
||||
pub trait KeyringStore: Debug + Send + Sync {
|
||||
fn load(&self, service: &str, account: &str) -> Result<Option<String>, CredentialStoreError>;
|
||||
fn save(&self, service: &str, account: &str, value: &str) -> Result<(), CredentialStoreError>;
|
||||
fn delete(&self, service: &str, account: &str) -> Result<bool, CredentialStoreError>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct DefaultKeyringStore;
|
||||
|
||||
impl KeyringStore for DefaultKeyringStore {
|
||||
fn load(&self, service: &str, account: &str) -> Result<Option<String>, CredentialStoreError> {
|
||||
trace!("keyring.load start");
|
||||
let entry = Entry::new(service, account).map_err(CredentialStoreError::new)?;
|
||||
match entry.get_password() {
|
||||
Ok(password) => {
|
||||
trace!("keyring.load success");
|
||||
Ok(Some(password))
|
||||
}
|
||||
Err(keyring::Error::NoEntry) => {
|
||||
trace!("keyring.load no entry");
|
||||
Ok(None)
|
||||
}
|
||||
Err(error) => {
|
||||
trace!("keyring.load error");
|
||||
Err(CredentialStoreError::new(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn save(&self, service: &str, account: &str, value: &str) -> Result<(), CredentialStoreError> {
|
||||
trace!("keyring.save start");
|
||||
let entry = Entry::new(service, account).map_err(CredentialStoreError::new)?;
|
||||
match entry.set_password(value) {
|
||||
Ok(()) => {
|
||||
trace!("keyring.save success");
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => {
|
||||
trace!("keyring.save error");
|
||||
Err(CredentialStoreError::new(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn delete(&self, service: &str, account: &str) -> Result<bool, CredentialStoreError> {
|
||||
trace!("keyring.delete start");
|
||||
let entry = Entry::new(service, account).map_err(CredentialStoreError::new)?;
|
||||
match entry.delete_credential() {
|
||||
Ok(()) => {
|
||||
trace!("keyring.delete success");
|
||||
Ok(true)
|
||||
}
|
||||
Err(keyring::Error::NoEntry) => {
|
||||
trace!("keyring.delete no entry");
|
||||
Ok(false)
|
||||
}
|
||||
Err(error) => {
|
||||
trace!("keyring.delete error");
|
||||
Err(CredentialStoreError::new(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod tests {
|
||||
use super::CredentialStoreError;
|
||||
use super::KeyringStore;
|
||||
use keyring::Error as KeyringError;
|
||||
use keyring::credential::CredentialApi as _;
|
||||
use keyring::mock::MockCredential;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::PoisonError;
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct MockKeyringStore {
|
||||
credentials: Arc<Mutex<HashMap<String, Arc<MockCredential>>>>,
|
||||
}
|
||||
|
||||
impl MockKeyringStore {
|
||||
pub fn credential(&self, account: &str) -> Arc<MockCredential> {
|
||||
let mut guard = self
|
||||
.credentials
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
guard
|
||||
.entry(account.to_string())
|
||||
.or_insert_with(|| Arc::new(MockCredential::default()))
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub fn saved_value(&self, account: &str) -> Option<String> {
|
||||
let credential = {
|
||||
let guard = self
|
||||
.credentials
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
guard.get(account).cloned()
|
||||
}?;
|
||||
credential.get_password().ok()
|
||||
}
|
||||
|
||||
pub fn set_error(&self, account: &str, error: KeyringError) {
|
||||
let credential = self.credential(account);
|
||||
credential.set_error(error);
|
||||
}
|
||||
|
||||
pub fn contains(&self, account: &str) -> bool {
|
||||
let guard = self
|
||||
.credentials
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
guard.contains_key(account)
|
||||
}
|
||||
}
|
||||
|
||||
impl KeyringStore for MockKeyringStore {
|
||||
fn load(
|
||||
&self,
|
||||
_service: &str,
|
||||
account: &str,
|
||||
) -> Result<Option<String>, CredentialStoreError> {
|
||||
let credential = {
|
||||
let guard = self
|
||||
.credentials
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
guard.get(account).cloned()
|
||||
};
|
||||
|
||||
let Some(credential) = credential else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
match credential.get_password() {
|
||||
Ok(password) => Ok(Some(password)),
|
||||
Err(KeyringError::NoEntry) => Ok(None),
|
||||
Err(error) => Err(CredentialStoreError::new(error)),
|
||||
}
|
||||
}
|
||||
|
||||
fn save(
|
||||
&self,
|
||||
_service: &str,
|
||||
account: &str,
|
||||
value: &str,
|
||||
) -> Result<(), CredentialStoreError> {
|
||||
let credential = self.credential(account);
|
||||
credential
|
||||
.set_password(value)
|
||||
.map_err(CredentialStoreError::new)
|
||||
}
|
||||
|
||||
fn delete(&self, _service: &str, account: &str) -> Result<bool, CredentialStoreError> {
|
||||
let credential = {
|
||||
let guard = self
|
||||
.credentials
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
guard.get(account).cloned()
|
||||
};
|
||||
|
||||
let Some(credential) = credential else {
|
||||
return Ok(false);
|
||||
};
|
||||
|
||||
let removed = match credential.delete_credential() {
|
||||
Ok(()) => Ok(true),
|
||||
Err(KeyringError::NoEntry) => Ok(false),
|
||||
Err(error) => Err(CredentialStoreError::new(error)),
|
||||
}?;
|
||||
|
||||
let mut guard = self
|
||||
.credentials
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
guard.remove(account);
|
||||
Ok(removed)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user