From 5a7bde5393ea6f00ef217f3eda05505a6a82d117 Mon Sep 17 00:00:00 2001 From: Eric Swanson Date: Fri, 18 Aug 2023 08:45:24 -0700 Subject: [PATCH 1/4] refactor: SaveIdentityConfigurationError --- .../src/error/identity/create_new_identity.rs | 3 ++- src/dfx-core/src/error/identity/mod.rs | 7 +------ src/dfx-core/src/error/identity/rename_identity.rs | 3 ++- .../error/identity/save_identity_configuration.rs | 12 ++++++++++++ src/dfx-core/src/identity/identity_manager.rs | 11 ++++++----- 5 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 src/dfx-core/src/error/identity/save_identity_configuration.rs 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..060df75abf 100644 --- a/src/dfx-core/src/error/identity/create_new_identity.rs +++ b/src/dfx-core/src/error/identity/create_new_identity.rs @@ -3,6 +3,7 @@ use crate::error::identity::convert_mnemonic_to_key::ConvertMnemonicToKeyError; 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::IdentityError; use thiserror::Error; @@ -46,7 +47,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), diff --git a/src/dfx-core/src/error/identity/mod.rs b/src/dfx-core/src/error/identity/mod.rs index 346ce8e569..a0671ad14c 100644 --- a/src/dfx-core/src/error/identity/mod.rs +++ b/src/dfx-core/src/error/identity/mod.rs @@ -15,6 +15,7 @@ pub mod new_identity; pub mod new_identity_manager; pub mod remove_identity; pub mod rename_identity; +pub mod save_identity_configuration; pub mod save_pem; pub mod write_pem_to_file; @@ -30,9 +31,6 @@ 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), @@ -54,9 +52,6 @@ pub enum IdentityError { #[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), diff --git a/src/dfx-core/src/error/identity/rename_identity.rs b/src/dfx-core/src/error/identity/rename_identity.rs index 211dbcd7b2..3ad35c8bbb 100644 --- a/src/dfx-core/src/error/identity/rename_identity.rs +++ b/src/dfx-core/src/error/identity/rename_identity.rs @@ -1,6 +1,7 @@ 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::save_identity_configuration::SaveIdentityConfigurationError; use crate::error::identity::save_pem::SavePemError; use crate::error::identity::IdentityError; use crate::error::keyring::KeyringError; @@ -33,7 +34,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/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/identity/identity_manager.rs b/src/dfx-core/src/identity/identity_manager.rs index 9a8f7db47e..82be38e147 100644 --- a/src/dfx-core/src/identity/identity_manager.rs +++ b/src/dfx-core/src/identity/identity_manager.rs @@ -43,10 +43,10 @@ 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::identity::IdentityError::GenerateFreshEncryptionConfigurationFailed; use crate::error::structured_file::StructuredFileError; use crate::foundation::get_user_home; use crate::fs::composite::ensure_parent_dir_exists; @@ -767,11 +767,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. From 7cd95234d1b9646b595d8171f753188b2ac79d7d Mon Sep 17 00:00:00 2001 From: Eric Swanson Date: Fri, 18 Aug 2023 08:48:31 -0700 Subject: [PATCH 2/4] refactor: CreateIdentityConfigError --- src/dfx-core/src/error/identity/create_identity_config.rs | 8 ++++++++ src/dfx-core/src/error/identity/create_new_identity.rs | 3 ++- src/dfx-core/src/error/identity/mod.rs | 6 +----- src/dfx-core/src/identity/identity_manager.rs | 5 +++-- 4 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 src/dfx-core/src/error/identity/create_identity_config.rs 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 060df75abf..2157b39fae 100644 --- a/src/dfx-core/src/error/identity/create_new_identity.rs +++ b/src/dfx-core/src/error/identity/create_new_identity.rs @@ -1,5 +1,6 @@ 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; @@ -23,7 +24,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), diff --git a/src/dfx-core/src/error/identity/mod.rs b/src/dfx-core/src/error/identity/mod.rs index a0671ad14c..333cec4ce3 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; @@ -20,8 +21,6 @@ pub mod save_pem; 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; @@ -31,9 +30,6 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum IdentityError { - #[error("Failed to generate a fresh encryption configuration: {0}")] - GenerateFreshEncryptionConfigurationFailed(EncryptionError), - #[error("Failed to get config directory for identity manager: {0}")] GetConfigDirectoryFailed(ConfigError), diff --git a/src/dfx-core/src/identity/identity_manager.rs b/src/dfx-core/src/identity/identity_manager.rs index 82be38e147..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, @@ -46,7 +48,6 @@ use crate::error::identity::rename_identity::RenameIdentityError::{ 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::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), From a7ea9187fdcfaf39f3ae262b352c7c6de87650a5 Mon Sep 17 00:00:00 2001 From: Eric Swanson Date: Fri, 18 Aug 2023 08:53:18 -0700 Subject: [PATCH 3/4] refactor: MapWalletsToRenamedIdentityError, MapWalletsToRenamedIdentityError --- .../map_wallets_to_renamed_identity.rs | 17 +++++++++++++++ src/dfx-core/src/error/identity/mod.rs | 10 ++------- .../src/error/identity/rename_identity.rs | 3 ++- .../rename_wallet_global_config_key.rs | 9 ++++++++ src/dfx-core/src/identity/mod.rs | 21 ++++++++++--------- 5 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 src/dfx-core/src/error/identity/map_wallets_to_renamed_identity.rs create mode 100644 src/dfx-core/src/error/identity/rename_wallet_global_config_key.rs 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..1e82f1ab8f --- /dev/null +++ b/src/dfx-core/src/error/identity/map_wallets_to_renamed_identity.rs @@ -0,0 +1,17 @@ +use thiserror::Error; +use crate::error::config::ConfigError; +use crate::error::identity::IdentityError; +use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError; + +#[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), + +} \ No newline at end of file diff --git a/src/dfx-core/src/error/identity/mod.rs b/src/dfx-core/src/error/identity/mod.rs index 333cec4ce3..4970a8c025 100644 --- a/src/dfx-core/src/error/identity/mod.rs +++ b/src/dfx-core/src/error/identity/mod.rs @@ -19,6 +19,8 @@ pub mod rename_identity; pub mod save_identity_configuration; pub mod save_pem; pub mod write_pem_to_file; +pub mod map_wallets_to_renamed_identity; +pub mod rename_wallet_global_config_key; use crate::error::config::ConfigError; use crate::error::structured_file::StructuredFileError; @@ -30,20 +32,12 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum IdentityError { - #[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), diff --git a/src/dfx-core/src/error/identity/rename_identity.rs b/src/dfx-core/src/error/identity/rename_identity.rs index 3ad35c8bbb..6a58029fea 100644 --- a/src/dfx-core/src/error/identity/rename_identity.rs +++ b/src/dfx-core/src/error/identity/rename_identity.rs @@ -6,6 +6,7 @@ use crate::error::identity::save_pem::SavePemError; use crate::error::identity::IdentityError; use crate::error::keyring::KeyringError; use thiserror::Error; +use crate::error::identity::map_wallets_to_renamed_identity::MapWalletsToRenamedIdentityError; #[derive(Error, Debug)] pub enum RenameIdentityError { @@ -25,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), 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..15b52aaeda --- /dev/null +++ b/src/dfx-core/src/error/identity/rename_wallet_global_config_key.rs @@ -0,0 +1,9 @@ +use thiserror::Error; +use crate::error::wallet_config::WalletConfigError; + +#[derive(Error, Debug)] +pub enum RenameWalletGlobalConfigKeyError { + #[error("Failed to rename '{0}' to '{1}' in the global wallet config: {2}")] + RenameWalletFailed(Box, Box, WalletConfigError), + +} \ No newline at end of file diff --git a/src/dfx-core/src/identity/mod.rs b/src/dfx-core/src/identity/mod.rs index 1684258301..aa82ebd94e 100644 --- a/src/dfx-core/src/identity/mod.rs +++ b/src/dfx-core/src/identity/mod.rs @@ -9,9 +9,6 @@ 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::IdentityError; -use crate::error::identity::IdentityError::{ - GetConfigDirectoryFailed, GetSharedNetworkDataDirectoryFailed, RenameWalletFailed, -}; use crate::error::wallet_config::WalletConfigError; use crate::error::wallet_config::WalletConfigError::{ EnsureWalletConfigDirFailed, LoadWalletConfigFailed, SaveWalletConfigFailed, @@ -33,6 +30,10 @@ use serde::{Deserialize, Serialize}; use slog::{info, Logger}; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; +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::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError; +use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError::RenameWalletFailed; mod identity_file_locations; pub mod identity_manager; @@ -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,17 @@ 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 +253,7 @@ impl Identity { original_identity, renamed_identity, local_wallet_path, - )?; + ).map_err(RenameWalletGlobalConfigKeyFailed)?; } } Ok(()) From 7b0b434735eb565dcb7ef5955a3d267e5b1412b2 Mon Sep 17 00:00:00 2001 From: Eric Swanson Date: Fri, 18 Aug 2023 10:59:26 -0700 Subject: [PATCH 4/4] refactor: ValidatePemFileError --- .../src/error/identity/create_new_identity.rs | 3 ++- .../src/error/identity/export_identity.rs | 3 ++- .../identity/map_wallets_to_renamed_identity.rs | 6 ++---- src/dfx-core/src/error/identity/mod.rs | 17 +++-------------- .../src/error/identity/rename_identity.rs | 2 +- .../identity/rename_wallet_global_config_key.rs | 5 ++--- .../src/error/identity/validate_pem_file.rs | 16 ++++++++++++++++ src/dfx-core/src/identity/mod.rs | 17 ++++++++++------- src/dfx-core/src/identity/pem_utils.rs | 8 +++++--- 9 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 src/dfx-core/src/error/identity/validate_pem_file.rs 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 2157b39fae..7bf9fdf46b 100644 --- a/src/dfx-core/src/error/identity/create_new_identity.rs +++ b/src/dfx-core/src/error/identity/create_new_identity.rs @@ -6,6 +6,7 @@ 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; @@ -60,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 index 1e82f1ab8f..6fef2e6fb3 100644 --- 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 @@ -1,7 +1,6 @@ -use thiserror::Error; use crate::error::config::ConfigError; -use crate::error::identity::IdentityError; use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError; +use thiserror::Error; #[derive(Error, Debug)] pub enum MapWalletsToRenamedIdentityError { @@ -13,5 +12,4 @@ pub enum MapWalletsToRenamedIdentityError { #[error("Failed to rename wallet global config key: {0}")] RenameWalletGlobalConfigKeyFailed(RenameWalletGlobalConfigKeyError), - -} \ No newline at end of file +} diff --git a/src/dfx-core/src/error/identity/mod.rs b/src/dfx-core/src/error/identity/mod.rs index 4970a8c025..24e53f24af 100644 --- a/src/dfx-core/src/error/identity/mod.rs +++ b/src/dfx-core/src/error/identity/mod.rs @@ -11,22 +11,20 @@ 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; -pub mod map_wallets_to_renamed_identity; -pub mod rename_wallet_global_config_key; -use crate::error::config::ConfigError; 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; @@ -38,18 +36,9 @@ pub enum IdentityError { #[error("Failed to read principal from id '{0}': {1}")] ParsePrincipalFromIdFailed(String, PrincipalError), - #[error("An Identity named {0} cannot be created as it is reserved for internal use.")] ReservedIdentityName(String), #[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 6a58029fea..d5cfc65e89 100644 --- a/src/dfx-core/src/error/identity/rename_identity.rs +++ b/src/dfx-core/src/error/identity/rename_identity.rs @@ -1,12 +1,12 @@ 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; use thiserror::Error; -use crate::error::identity::map_wallets_to_renamed_identity::MapWalletsToRenamedIdentityError; #[derive(Error, Debug)] pub enum RenameIdentityError { 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 index 15b52aaeda..3e67cef07e 100644 --- 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 @@ -1,9 +1,8 @@ -use thiserror::Error; 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), - -} \ No newline at end of file +} 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/mod.rs b/src/dfx-core/src/identity/mod.rs index aa82ebd94e..1a0abe8d2c 100644 --- a/src/dfx-core/src/identity/mod.rs +++ b/src/dfx-core/src/identity/mod.rs @@ -5,9 +5,13 @@ 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::wallet_config::WalletConfigError; use crate::error::wallet_config::WalletConfigError::{ @@ -30,10 +34,6 @@ use serde::{Deserialize, Serialize}; use slog::{info, Logger}; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; -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::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError; -use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError::RenameWalletFailed; mod identity_file_locations; pub mod identity_manager; @@ -234,7 +234,8 @@ impl Identity { original_identity, renamed_identity, persistent_wallet_path, - ).map_err(RenameWalletGlobalConfigKeyFailed)?; + ) + .map_err(RenameWalletGlobalConfigKeyFailed)?; } let shared_local_network_wallet_path = get_shared_network_data_directory("local") .map_err(MapWalletsToRenamedIdentityError::GetSharedNetworkDataDirectoryFailed)? @@ -244,7 +245,8 @@ impl Identity { original_identity, renamed_identity, shared_local_network_wallet_path, - ).map_err(RenameWalletGlobalConfigKeyFailed)?; + ) + .map_err(RenameWalletGlobalConfigKeyFailed)?; } if let Some(temp_dir) = project_temp_dir { let local_wallet_path = temp_dir.join("local").join(WALLET_CONFIG_FILENAME); @@ -253,7 +255,8 @@ impl Identity { original_identity, renamed_identity, local_wallet_path, - ).map_err(RenameWalletGlobalConfigKeyFailed)?; + ) + .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 {