From 05389a0109720341d4a63f952cdb4f307699c1de Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 8 Jul 2020 10:56:16 +0200 Subject: [PATCH] more xdg cleanup and encryption parameter improvements Have a single common function to get the BaseDirectories instance and a wrapper for `find()` and `place()` which wrap the error with some context. Signed-off-by: Wolfgang Bumiller --- src/bin/proxmox-backup-client.rs | 30 +++++++++++----------------- src/bin/proxmox_backup_client/key.rs | 28 ++++++++++---------------- src/bin/proxmox_backup_client/mod.rs | 29 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 7bd82e78..a6cbcc1e 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -677,7 +677,10 @@ fn keyfile_parameters(param: &Value) -> Result<(Option, CryptMode), Err Ok(match (keyfile, crypt_mode) { // no parameters: - (None, None) => (key::find_default_encryption_key()?, CryptMode::Encrypt), + (None, None) => match key::find_default_encryption_key()? { + Some(key) => (Some(key), CryptMode::Encrypt), + None => (None, CryptMode::None), + }, // just --crypt-mode=none (None, Some(CryptMode::None)) => (None, CryptMode::None), @@ -906,14 +909,14 @@ async fn create_backup( let crypt_config = CryptConfig::new(key)?; - let path = master_pubkey_path()?; - if path.exists() { - let pem_data = file_get_contents(&path)?; - let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?; - let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?; - (Some(Arc::new(crypt_config)), Some(enc_key)) - } else { - (Some(Arc::new(crypt_config)), None) + match key::find_master_pubkey()? { + Some(ref path) if path.exists() => { + let pem_data = file_get_contents(path)?; + let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?; + let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?; + (Some(Arc::new(crypt_config)), Some(enc_key)) + } + _ => (Some(Arc::new(crypt_config)), None), } } }; @@ -1734,15 +1737,6 @@ fn complete_chunk_size(_arg: &str, _param: &HashMap) -> Vec Result { - let base = BaseDirectories::with_prefix("proxmox-backup")?; - - // usually $HOME/.config/proxmox-backup/master-public.pem - let path = base.place_config_file("master-public.pem")?; - - Ok(path) -} - use proxmox_backup::client::RemoteChunkReader; /// This is a workaround until we have cleaned up the chunk/reader/... infrastructure for better /// async use! diff --git a/src/bin/proxmox_backup_client/key.rs b/src/bin/proxmox_backup_client/key.rs index 05d0a28d..b0218975 100644 --- a/src/bin/proxmox_backup_client/key.rs +++ b/src/bin/proxmox_backup_client/key.rs @@ -1,9 +1,8 @@ use std::path::PathBuf; -use anyhow::{bail, format_err, Context, Error}; +use anyhow::{bail, format_err, Error}; use chrono::{Local, TimeZone}; use serde::{Deserialize, Serialize}; -use xdg::BaseDirectories; use proxmox::api::api; use proxmox::api::cli::{CliCommand, CliCommandMap}; @@ -16,29 +15,22 @@ use proxmox_backup::backup::{ use proxmox_backup::tools; pub const DEFAULT_ENCRYPTION_KEY_FILE_NAME: &str = "encryption-key.json"; +pub const MASTER_PUBKEY_FILE_NAME: &str = "master-public.pem"; -pub fn master_pubkey_path() -> Result { - let base = BaseDirectories::with_prefix("proxmox-backup")?; +pub fn find_master_pubkey() -> Result, Error> { + super::find_xdg_file(MASTER_PUBKEY_FILE_NAME, "main public key file") +} - // usually $HOME/.config/proxmox-backup/master-public.pem - let path = base.place_config_file("master-public.pem")?; - - Ok(path) +pub fn place_master_pubkey() -> Result { + super::place_xdg_file(MASTER_PUBKEY_FILE_NAME, "main public key file") } pub fn find_default_encryption_key() -> Result, Error> { - BaseDirectories::with_prefix("proxmox-backup") - .map(|base| base.find_config_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME)) - .with_context(|| "error searching for default encryption key file") + super::find_xdg_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME, "default encryption key file") } pub fn place_default_encryption_key() -> Result { - BaseDirectories::with_prefix("proxmox-backup") - .map_err(Error::from) - .and_then(|base| { - base.place_config_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME).map_err(Error::from) - }) - .with_context(|| "failed to place default encryption key file in xdg home") + super::place_xdg_file(DEFAULT_ENCRYPTION_KEY_FILE_NAME, "default encryption key file") } pub fn get_encryption_key_password() -> Result, Error> { @@ -216,7 +208,7 @@ fn import_master_pubkey(path: String) -> Result<(), Error> { bail!("Unable to decode PEM data - {}", err); } - let target_path = master_pubkey_path()?; + let target_path = place_master_pubkey()?; replace_file(&target_path, &pem_data, CreateOptions::new())?; diff --git a/src/bin/proxmox_backup_client/mod.rs b/src/bin/proxmox_backup_client/mod.rs index 054aff5b..0c4bffb9 100644 --- a/src/bin/proxmox_backup_client/mod.rs +++ b/src/bin/proxmox_backup_client/mod.rs @@ -1,3 +1,5 @@ +use anyhow::{Context, Error}; + mod benchmark; pub use benchmark::*; mod mount; @@ -8,3 +10,30 @@ mod catalog; pub use catalog::*; pub mod key; + +pub fn base_directories() -> Result { + xdg::BaseDirectories::with_prefix("proxmox-backup").map_err(Error::from) +} + +/// Convenience helper for better error messages: +pub fn find_xdg_file( + file_name: impl AsRef, + description: &'static str, +) -> Result, Error> { + let file_name = file_name.as_ref(); + base_directories() + .map(|base| base.find_config_file(file_name)) + .with_context(|| format!("error searching for {}", description)) +} + +pub fn place_xdg_file( + file_name: impl AsRef, + description: &'static str, +) -> Result { + let file_name = file_name.as_ref(); + base_directories() + .and_then(|base| { + base.place_config_file(file_name).map_err(Error::from) + }) + .with_context(|| format!("failed to place {} in xdg home", description)) +}