Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: method-specific concrete errors for dfx core #3382

Merged
merged 4 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/dfx-core/src/error/identity/create_identity_config.rs
Original file line number Diff line number Diff line change
@@ -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),
}
9 changes: 6 additions & 3 deletions src/dfx-core/src/error/identity/create_new_identity.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,7 +25,7 @@ pub enum CreateNewIdentityError {
ConvertSecretKeyToSec1PemFailed(Box<sec1::Error>),

#[error("Failed to create identity config: {0}")]
CreateIdentityConfigFailed(IdentityError),
CreateIdentityConfigFailed(CreateIdentityConfigError),

#[error("Failed to create mnemonic from phrase: {0}")]
CreateMnemonicFromPhraseFailed(String),
Expand All @@ -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),
Expand All @@ -58,5 +61,5 @@ pub enum CreateNewIdentityError {
SwitchToAnonymousIdentityFailed(IdentityError),

#[error("Failed to validate pem file: {0}")]
ValidatePemFileFailed(IdentityError),
ValidatePemFileFailed(ValidatePemFileError),
}
3 changes: 2 additions & 1 deletion src/dfx-core/src/error/identity/export_identity.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,5 +20,5 @@ pub enum ExportIdentityError {
TranslatePemContentToTextFailed(FromUtf8Error),

#[error("Failed to validate pem file: {0}")]
ValidatePemFileFailed(IdentityError),
ValidatePemFileFailed(ValidatePemFileError),
}
Original file line number Diff line number Diff line change
@@ -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),
}
36 changes: 5 additions & 31 deletions src/dfx-core/src/error/identity/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String>, Box<String>, 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<PemError>),
}
6 changes: 4 additions & 2 deletions src/dfx-core/src/error/identity/rename_identity.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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),
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>, Box<String>, WalletConfigError),
}
12 changes: 12 additions & 0 deletions src/dfx-core/src/error/identity/save_identity_configuration.rs
Original file line number Diff line number Diff line change
@@ -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),
}
16 changes: 16 additions & 0 deletions src/dfx-core/src/error/identity/validate_pem_file.rs
Original file line number Diff line number Diff line change
@@ -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<PemError>),
}
14 changes: 8 additions & 6 deletions src/dfx-core/src/identity/identity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -318,7 +319,7 @@ impl IdentityManager {
mode: IdentityStorageMode,
name: &str,
hardware_config: Option<HardwareIdentityConfiguration>,
) -> Result<IdentityConfiguration, IdentityError> {
) -> Result<IdentityConfiguration, CreateIdentityConfigError> {
if let Some(hsm) = hardware_config {
Ok(IdentityConfiguration {
hsm: Some(hsm),
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 14 additions & 10 deletions src/dfx-core/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -222,9 +223,9 @@ impl Identity {
project_temp_dir: Option<PathBuf>,
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);
Expand All @@ -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);
Expand All @@ -252,7 +255,8 @@ impl Identity {
original_identity,
renamed_identity,
local_wallet_path,
)?;
)
.map_err(RenameWalletGlobalConfigKeyFailed)?;
}
}
Ok(())
Expand Down
8 changes: 5 additions & 3 deletions src/dfx-core/src/identity/pem_utils.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Loading