Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
timglabisch committed Jan 9, 2024
1 parent 3718d32 commit f31eae9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ serde_derive = "*"
serde = "*"
clap = "4.*"
regex = "1.*"
anyhow = "1.0.*"
anyhow = { version = "1.0.*", features = ["backtrace"] }
byteorder = "1.5.*"
dirs = "5.*"
globset = "0.4.*"
Expand Down
18 changes: 9 additions & 9 deletions src/rotate_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::key::Pem;
use crate::ui::question::Question;

pub fn rotate_keys(keymap_config: &KeyMapConfig) -> Result<(), Error> {

let keymap = KeyMap::from_path(keymap_config)?;

let pems = keymap.get_private_pems().iter().filter(|x|!x.get_name().contains("_backup_")).collect::<Vec<_>>();
Expand All @@ -22,16 +22,16 @@ pub fn rotate_keys(keymap_config: &KeyMapConfig) -> Result<(), Error> {
let username_rotated = &format!("{}_to_rotate", username_current);

println!("1. generate new key");
create_keys(&format!("{}_to_rotate", username_current))?;
create_keys(&format!("{}_to_rotate", username_current)).context("create_keys")?;

let keymap = KeyMap::from_path(keymap_config)?;

println!("2. allow access to all keys");
allow_access_to_all_keys(&keymap, &username_rotated)?;
allow_access_to_all_keys(&keymap, &username_rotated).context("allow_access_to_all_keys")?;
println!("2. delete the old key");
delete_user(username_current)?;
delete_user(username_current).context("delete_user")?;
println!("3. rename user");
rename_user(&username_rotated, &username_current)?;
rename_user(&username_rotated, &username_current).context("rename_user")?;
println!("the key has been rotated, the old key is still there and has a backup suffix. ");

Ok(())
Expand Down Expand Up @@ -68,7 +68,7 @@ fn rename_user(username_from: &str, username_to: &str) -> Result<(), Error> {

let secret_directory_path = "./.vault/secrets/";

let secret_directory_path_readdir = fs::read_dir(&secret_directory_path).context(anyhow!(
let secret_directory_path_readdir = fs::read_dir(&secret_directory_path).context(format!(
"could not read subscription path. directory is missing? {}",
&secret_directory_path
))?;
Expand Down Expand Up @@ -115,7 +115,7 @@ fn delete_user(username: &str) -> Result<(), Error> {

let secret_directory_path = "./.vault/secrets/";

let secret_directory_path_readdir = fs::read_dir(&secret_directory_path).context(anyhow!(
let secret_directory_path_readdir = fs::read_dir(&secret_directory_path).context(format!(
"could not read subscription path. directory is missing? {}",
&secret_directory_path
))?;
Expand All @@ -136,7 +136,7 @@ fn delete_user(username: &str) -> Result<(), Error> {
continue;
}

fs::remove_file(&crypt_file_path).context(anyhow!(
fs::remove_file(&crypt_file_path).context(format!(
"could not remove file {}",
&secret_directory_path
))?;
Expand Down Expand Up @@ -213,7 +213,7 @@ fn allow_access_to_all_keys(keymap: &KeyMap, username_rotated: &str) -> Result<(
Ok(_k) => {},
Err(_e) => {
let crypt_file_path = format!("./.vault/secrets/{}/{}.crypt", secret_name, username_rotated);
if fs::metadata(&crypt_file_path)?.is_file() {
if fs::metadata(&crypt_file_path).is_ok() {
return Err(anyhow!("could not read secret {}", crypt_file_path));
}
}
Expand Down

0 comments on commit f31eae9

Please sign in to comment.