Skip to content

Commit 93be935

Browse files
authored
feat(cli): Implement error handling for cli encryptor parsing (#2160)
In the main loop of iggy-cli, putting invalid value for encryptor will generate a panic from client side, however, a panic should refer to a programmer bug rather than invalid user input. This PR fixes it.
1 parent 2cf4b69 commit 93be935

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

core/cli/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use thiserror::Error;
2222
#[derive(Error, Debug)]
2323
pub(crate) enum CmdToolError {
2424
MissingCredentials,
25+
InvalidEncryptionKey,
2526
#[cfg(feature = "login-session")]
2627
MissingServerAddress,
2728
}
@@ -32,6 +33,9 @@ impl Display for CmdToolError {
3233
Self::MissingCredentials => {
3334
write!(f, "Missing iggy server credentials")
3435
}
36+
Self::InvalidEncryptionKey => {
37+
write!(f, "Invalid encryption key provided")
38+
}
3539
#[cfg(feature = "login-session")]
3640
Self::MissingServerAddress => {
3741
write!(f, "Missing iggy server address")

core/cli/src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::args::{
2727
personal_access_token::PersonalAccessTokenAction, stream::StreamAction, topic::TopicAction,
2828
};
2929
use crate::credentials::IggyCredentials;
30-
use crate::error::IggyCmdError;
30+
use crate::error::{CmdToolError, IggyCmdError};
3131
use crate::logging::Logging;
3232
use args::context::ContextAction;
3333
use args::message::MessageAction;
@@ -370,9 +370,15 @@ async fn main() -> Result<(), IggyCmdError> {
370370

371371
let encryptor = match iggy_args.encryption_key.is_empty() {
372372
true => None,
373-
false => Some(Arc::new(EncryptorKind::Aes256Gcm(
374-
Aes256GcmEncryptor::from_base64_key(&iggy_args.encryption_key).unwrap(),
375-
))),
373+
false => {
374+
let encryption_key = Aes256GcmEncryptor::from_base64_key(&iggy_args.encryption_key)
375+
.map_err(|_| {
376+
<IggyCmdError as Into<anyhow::Error>>::into(IggyCmdError::CmdToolError(
377+
CmdToolError::InvalidEncryptionKey,
378+
))
379+
})?;
380+
Some(Arc::new(EncryptorKind::Aes256Gcm(encryption_key)))
381+
}
376382
};
377383
let client_provider_config = Arc::new(ClientProviderConfig::from_args_set_autologin(
378384
iggy_args.clone(),

0 commit comments

Comments
 (0)