diff --git a/src/dfx-core/src/error/identity/create_identity_config.rs b/src/dfx-core/src/error/identity/create_identity_config.rs new file mode 100644 index 0000000000..2367238749 --- /dev/null +++ b/src/dfx-core/src/error/identity/create_identity_config.rs @@ -0,0 +1,8 @@ +use crate::error::encryption::EncryptionError; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum CreateIdentityConfigError { + #[error("Failed to generate a fresh encryption configuration: {0}")] + GenerateFreshEncryptionConfigurationFailed(EncryptionError), +} diff --git a/src/dfx-core/src/error/identity/create_new_identity.rs b/src/dfx-core/src/error/identity/create_new_identity.rs index 0f0739fee4..7bf9fdf46b 100644 --- a/src/dfx-core/src/error/identity/create_new_identity.rs +++ b/src/dfx-core/src/error/identity/create_new_identity.rs @@ -1,9 +1,12 @@ use crate::error::fs::FsError; use crate::error::identity::convert_mnemonic_to_key::ConvertMnemonicToKeyError; +use crate::error::identity::create_identity_config::CreateIdentityConfigError; use crate::error::identity::generate_key::GenerateKeyError; use crate::error::identity::load_pem_from_file::LoadPemFromFileError; use crate::error::identity::remove_identity::RemoveIdentityError; +use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError; use crate::error::identity::save_pem::SavePemError; +use crate::error::identity::validate_pem_file::ValidatePemFileError; use crate::error::identity::IdentityError; use thiserror::Error; @@ -22,7 +25,7 @@ pub enum CreateNewIdentityError { ConvertSecretKeyToSec1PemFailed(Box), #[error("Failed to create identity config: {0}")] - CreateIdentityConfigFailed(IdentityError), + CreateIdentityConfigFailed(CreateIdentityConfigError), #[error("Failed to create mnemonic from phrase: {0}")] CreateMnemonicFromPhraseFailed(String), @@ -46,7 +49,7 @@ pub enum CreateNewIdentityError { RenameTemporaryIdentityDirectoryFailed(FsError), #[error("Failed to save identity configuration: {0}")] - SaveIdentityConfigurationFailed(IdentityError), + SaveIdentityConfigurationFailed(SaveIdentityConfigurationError), #[error("Failed to save pem: {0}")] SavePemFailed(SavePemError), @@ -58,5 +61,5 @@ pub enum CreateNewIdentityError { SwitchToAnonymousIdentityFailed(IdentityError), #[error("Failed to validate pem file: {0}")] - ValidatePemFileFailed(IdentityError), + ValidatePemFileFailed(ValidatePemFileError), } diff --git a/src/dfx-core/src/error/identity/export_identity.rs b/src/dfx-core/src/error/identity/export_identity.rs index 9882ee9e7f..92b493c6b6 100644 --- a/src/dfx-core/src/error/identity/export_identity.rs +++ b/src/dfx-core/src/error/identity/export_identity.rs @@ -1,5 +1,6 @@ use crate::error::identity::get_identity_config_or_default::GetIdentityConfigOrDefaultError; use crate::error::identity::load_pem::LoadPemError; +use crate::error::identity::validate_pem_file::ValidatePemFileError; use crate::error::identity::IdentityError; use std::string::FromUtf8Error; use thiserror::Error; @@ -19,5 +20,5 @@ pub enum ExportIdentityError { TranslatePemContentToTextFailed(FromUtf8Error), #[error("Failed to validate pem file: {0}")] - ValidatePemFileFailed(IdentityError), + ValidatePemFileFailed(ValidatePemFileError), } diff --git a/src/dfx-core/src/error/identity/map_wallets_to_renamed_identity.rs b/src/dfx-core/src/error/identity/map_wallets_to_renamed_identity.rs new file mode 100644 index 0000000000..6fef2e6fb3 --- /dev/null +++ b/src/dfx-core/src/error/identity/map_wallets_to_renamed_identity.rs @@ -0,0 +1,15 @@ +use crate::error::config::ConfigError; +use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum MapWalletsToRenamedIdentityError { + #[error("Failed to get config directory for identity manager: {0}")] + GetConfigDirectoryFailed(ConfigError), + + #[error("Failed to get shared network data directory: {0}")] + GetSharedNetworkDataDirectoryFailed(ConfigError), + + #[error("Failed to rename wallet global config key: {0}")] + RenameWalletGlobalConfigKeyFailed(RenameWalletGlobalConfigKeyError), +} diff --git a/src/dfx-core/src/error/identity/mod.rs b/src/dfx-core/src/error/identity/mod.rs index 346ce8e569..24e53f24af 100644 --- a/src/dfx-core/src/error/identity/mod.rs +++ b/src/dfx-core/src/error/identity/mod.rs @@ -1,4 +1,5 @@ pub mod convert_mnemonic_to_key; +pub mod create_identity_config; pub mod create_new_identity; pub mod export_identity; pub mod generate_key; @@ -10,61 +11,34 @@ pub mod load_identity; pub mod load_pem; pub mod load_pem_from_file; pub mod load_pem_identity; +pub mod map_wallets_to_renamed_identity; pub mod new_hardware_identity; pub mod new_identity; pub mod new_identity_manager; pub mod remove_identity; pub mod rename_identity; +pub mod rename_wallet_global_config_key; +pub mod save_identity_configuration; pub mod save_pem; +pub mod validate_pem_file; pub mod write_pem_to_file; -use crate::error::config::ConfigError; -use crate::error::encryption::EncryptionError; -use crate::error::fs::FsError; use crate::error::structured_file::StructuredFileError; -use crate::error::wallet_config::WalletConfigError; use ic_agent::export::PrincipalError; -use ic_agent::identity::PemError; use std::path::PathBuf; use thiserror::Error; #[derive(Error, Debug)] pub enum IdentityError { - #[error("Failed to ensure identity configuration directory exists: {0}")] - EnsureIdentityConfigurationDirExistsFailed(FsError), - - #[error("Failed to generate a fresh encryption configuration: {0}")] - GenerateFreshEncryptionConfigurationFailed(EncryptionError), - - #[error("Failed to get config directory for identity manager: {0}")] - GetConfigDirectoryFailed(ConfigError), - - #[error("Failed to get shared network data directory: {0}")] - GetSharedNetworkDataDirectoryFailed(ConfigError), - #[error("Identity {0} does not exist at '{1}'.")] IdentityDoesNotExist(String, PathBuf), #[error("Failed to read principal from id '{0}': {1}")] ParsePrincipalFromIdFailed(String, PrincipalError), - #[error("Failed to rename '{0}' to '{1}' in the global wallet config: {2}")] - RenameWalletFailed(Box, Box, WalletConfigError), - #[error("An Identity named {0} cannot be created as it is reserved for internal use.")] ReservedIdentityName(String), - #[error("Failed to save identity configuration: {0}")] - SaveIdentityConfigurationFailed(StructuredFileError), - #[error("Failed to save identity manager configuration: {0}")] SaveIdentityManagerConfigurationFailed(StructuredFileError), - - #[error( - "Ed25519 v1 keys (those generated by OpenSSL) are not supported. Try again with a v2 key" - )] - UnsupportedKeyVersion(), - - #[error("Failed to validate PEM content: {0}")] - ValidatePemContentFailed(Box), } diff --git a/src/dfx-core/src/error/identity/rename_identity.rs b/src/dfx-core/src/error/identity/rename_identity.rs index 211dbcd7b2..d5cfc65e89 100644 --- a/src/dfx-core/src/error/identity/rename_identity.rs +++ b/src/dfx-core/src/error/identity/rename_identity.rs @@ -1,6 +1,8 @@ use crate::error::fs::FsError; use crate::error::identity::get_identity_config_or_default::GetIdentityConfigOrDefaultError; use crate::error::identity::load_pem::LoadPemError; +use crate::error::identity::map_wallets_to_renamed_identity::MapWalletsToRenamedIdentityError; +use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError; use crate::error::identity::save_pem::SavePemError; use crate::error::identity::IdentityError; use crate::error::keyring::KeyringError; @@ -24,7 +26,7 @@ pub enum RenameIdentityError { LoadPemFailed(LoadPemError), #[error("Failed to map wallets to renamed identity: {0}")] - MapWalletsToRenamedIdentityFailed(IdentityError /*MapWalletsToRenamedIdentityError*/), + MapWalletsToRenamedIdentityFailed(MapWalletsToRenamedIdentityError), #[error("Failed to remove identity from keyring: {0}")] RemoveIdentityFromKeyringFailed(KeyringError), @@ -33,7 +35,7 @@ pub enum RenameIdentityError { RenameIdentityDirectoryFailed(FsError), #[error("Failed to save identity configuration: {0}")] - SaveIdentityConfigurationFailed(IdentityError), + SaveIdentityConfigurationFailed(SaveIdentityConfigurationError), #[error("Failed to save pem: {0}")] SavePemFailed(SavePemError), diff --git a/src/dfx-core/src/error/identity/rename_wallet_global_config_key.rs b/src/dfx-core/src/error/identity/rename_wallet_global_config_key.rs new file mode 100644 index 0000000000..3e67cef07e --- /dev/null +++ b/src/dfx-core/src/error/identity/rename_wallet_global_config_key.rs @@ -0,0 +1,8 @@ +use crate::error::wallet_config::WalletConfigError; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum RenameWalletGlobalConfigKeyError { + #[error("Failed to rename '{0}' to '{1}' in the global wallet config: {2}")] + RenameWalletFailed(Box, Box, WalletConfigError), +} diff --git a/src/dfx-core/src/error/identity/save_identity_configuration.rs b/src/dfx-core/src/error/identity/save_identity_configuration.rs new file mode 100644 index 0000000000..b4e4921e9e --- /dev/null +++ b/src/dfx-core/src/error/identity/save_identity_configuration.rs @@ -0,0 +1,12 @@ +use crate::error::fs::FsError; +use crate::error::structured_file::StructuredFileError; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum SaveIdentityConfigurationError { + #[error("Failed to ensure identity configuration directory exists: {0}")] + EnsureIdentityConfigurationDirExistsFailed(FsError), + + #[error("Failed to save identity configuration: {0}")] + SaveIdentityConfigurationFailed(StructuredFileError), +} diff --git a/src/dfx-core/src/error/identity/validate_pem_file.rs b/src/dfx-core/src/error/identity/validate_pem_file.rs new file mode 100644 index 0000000000..4be6561ca4 --- /dev/null +++ b/src/dfx-core/src/error/identity/validate_pem_file.rs @@ -0,0 +1,16 @@ +use ic_agent::identity::PemError; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum ValidatePemFileError { + #[error(transparent)] + PemError(#[from] ic_agent::identity::PemError), + + #[error( + "Ed25519 v1 keys (those generated by OpenSSL) are not supported. Try again with a v2 key" + )] + UnsupportedKeyVersion(), + + #[error("Failed to validate PEM content: {0}")] + ValidatePemContentFailed(Box), +} diff --git a/src/dfx-core/src/identity/identity_manager.rs b/src/dfx-core/src/identity/identity_manager.rs index 9a8f7db47e..41f81f1160 100644 --- a/src/dfx-core/src/identity/identity_manager.rs +++ b/src/dfx-core/src/identity/identity_manager.rs @@ -6,6 +6,8 @@ use crate::error::encryption::EncryptionError::{NonceGenerationFailed, SaltGener use crate::error::fs::FsError; use crate::error::identity::convert_mnemonic_to_key::ConvertMnemonicToKeyError; use crate::error::identity::convert_mnemonic_to_key::ConvertMnemonicToKeyError::DeriveExtendedKeyFromPathFailed; +use crate::error::identity::create_identity_config::CreateIdentityConfigError; +use crate::error::identity::create_identity_config::CreateIdentityConfigError::GenerateFreshEncryptionConfigurationFailed; use crate::error::identity::create_new_identity::CreateNewIdentityError; use crate::error::identity::create_new_identity::CreateNewIdentityError::{ CleanupPreviousCreationAttemptsFailed, ConvertSecretKeyToSec1PemFailed, @@ -43,10 +45,9 @@ use crate::error::identity::rename_identity::RenameIdentityError::{ GetIdentityConfigFailed, LoadPemFailed, MapWalletsToRenamedIdentityFailed, RenameIdentityDirectoryFailed, SavePemFailed, SwitchDefaultIdentitySettingsFailed, }; +use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError; +use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError::EnsureIdentityConfigurationDirExistsFailed; use crate::error::identity::IdentityError; -use crate::error::identity::IdentityError::{ - EnsureIdentityConfigurationDirExistsFailed, GenerateFreshEncryptionConfigurationFailed, -}; use crate::error::structured_file::StructuredFileError; use crate::foundation::get_user_home; use crate::fs::composite::ensure_parent_dir_exists; @@ -318,7 +319,7 @@ impl IdentityManager { mode: IdentityStorageMode, name: &str, hardware_config: Option, - ) -> Result { + ) -> Result { if let Some(hsm) = hardware_config { Ok(IdentityConfiguration { hsm: Some(hsm), @@ -767,11 +768,12 @@ pub(super) fn save_identity_configuration( log: &Logger, path: &Path, config: &IdentityConfiguration, -) -> Result<(), IdentityError> { +) -> Result<(), SaveIdentityConfigurationError> { trace!(log, "Writing identity configuration to {}", path.display()); ensure_parent_dir_exists(path).map_err(EnsureIdentityConfigurationDirExistsFailed)?; - save_json_file(path, &config).map_err(IdentityError::SaveIdentityConfigurationFailed) + save_json_file(path, &config) + .map_err(SaveIdentityConfigurationError::SaveIdentityConfigurationFailed) } /// Removes the file if it exists. diff --git a/src/dfx-core/src/identity/mod.rs b/src/dfx-core/src/identity/mod.rs index 1684258301..1a0abe8d2c 100644 --- a/src/dfx-core/src/identity/mod.rs +++ b/src/dfx-core/src/identity/mod.rs @@ -5,13 +5,14 @@ use crate::config::directories::{get_config_dfx_dir_path, get_shared_network_data_directory}; use crate::error::identity::load_pem_identity::LoadPemIdentityError; use crate::error::identity::load_pem_identity::LoadPemIdentityError::ReadIdentityFileFailed; +use crate::error::identity::map_wallets_to_renamed_identity::MapWalletsToRenamedIdentityError; +use crate::error::identity::map_wallets_to_renamed_identity::MapWalletsToRenamedIdentityError::RenameWalletGlobalConfigKeyFailed; use crate::error::identity::new_hardware_identity::NewHardwareIdentityError; use crate::error::identity::new_hardware_identity::NewHardwareIdentityError::InstantiateHardwareIdentityFailed; use crate::error::identity::new_identity::NewIdentityError; +use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError; +use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError::RenameWalletFailed; use crate::error::identity::IdentityError; -use crate::error::identity::IdentityError::{ - GetConfigDirectoryFailed, GetSharedNetworkDataDirectoryFailed, RenameWalletFailed, -}; use crate::error::wallet_config::WalletConfigError; use crate::error::wallet_config::WalletConfigError::{ EnsureWalletConfigDirFailed, LoadWalletConfigFailed, SaveWalletConfigFailed, @@ -196,7 +197,7 @@ impl Identity { original_identity: &str, renamed_identity: &str, wallet_path: PathBuf, - ) -> Result<(), IdentityError> { + ) -> Result<(), RenameWalletGlobalConfigKeyError> { Identity::load_wallet_config(&wallet_path) .and_then(|mut config| { let identities = &mut config.identities; @@ -222,9 +223,9 @@ impl Identity { project_temp_dir: Option, original_identity: &str, renamed_identity: &str, - ) -> Result<(), IdentityError> { + ) -> Result<(), MapWalletsToRenamedIdentityError> { let persistent_wallet_path = get_config_dfx_dir_path() - .map_err(GetConfigDirectoryFailed)? + .map_err(MapWalletsToRenamedIdentityError::GetConfigDirectoryFailed)? .join("identity") .join(original_identity) .join(WALLET_CONFIG_FILENAME); @@ -233,17 +234,19 @@ impl Identity { original_identity, renamed_identity, persistent_wallet_path, - )?; + ) + .map_err(RenameWalletGlobalConfigKeyFailed)?; } let shared_local_network_wallet_path = get_shared_network_data_directory("local") - .map_err(GetSharedNetworkDataDirectoryFailed)? + .map_err(MapWalletsToRenamedIdentityError::GetSharedNetworkDataDirectoryFailed)? .join(WALLET_CONFIG_FILENAME); if shared_local_network_wallet_path.exists() { Identity::rename_wallet_global_config_key( original_identity, renamed_identity, shared_local_network_wallet_path, - )?; + ) + .map_err(RenameWalletGlobalConfigKeyFailed)?; } if let Some(temp_dir) = project_temp_dir { let local_wallet_path = temp_dir.join("local").join(WALLET_CONFIG_FILENAME); @@ -252,7 +255,8 @@ impl Identity { original_identity, renamed_identity, local_wallet_path, - )?; + ) + .map_err(RenameWalletGlobalConfigKeyFailed)?; } } Ok(()) diff --git a/src/dfx-core/src/identity/pem_utils.rs b/src/dfx-core/src/identity/pem_utils.rs index 0466a0c78f..8e157341aa 100644 --- a/src/dfx-core/src/identity/pem_utils.rs +++ b/src/dfx-core/src/identity/pem_utils.rs @@ -1,10 +1,12 @@ -use crate::error::identity::IdentityError; -use crate::error::identity::IdentityError::{UnsupportedKeyVersion, ValidatePemContentFailed}; +use crate::error::identity::validate_pem_file::ValidatePemFileError; +use crate::error::identity::validate_pem_file::ValidatePemFileError::{ + UnsupportedKeyVersion, ValidatePemContentFailed, +}; use ic_agent::identity::BasicIdentity; use ic_agent::identity::PemError; use ic_agent::identity::Secp256k1Identity; -pub fn validate_pem_file(pem_content: &[u8]) -> Result<(), IdentityError> { +pub fn validate_pem_file(pem_content: &[u8]) -> Result<(), ValidatePemFileError> { let secp_res = Secp256k1Identity::from_pem(pem_content).map_err(|e| ValidatePemContentFailed(Box::new(e))); if let Err(e) = secp_res {