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 <w.bumiller@proxmox.com>
This commit is contained in:
parent
b65390ebc9
commit
05389a0109
|
@ -677,7 +677,10 @@ fn keyfile_parameters(param: &Value) -> Result<(Option<PathBuf>, 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)?;
|
||||
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))
|
||||
} else {
|
||||
(Some(Arc::new(crypt_config)), None)
|
||||
}
|
||||
_ => (Some(Arc::new(crypt_config)), None),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1734,15 +1737,6 @@ fn complete_chunk_size(_arg: &str, _param: &HashMap<String, String>) -> Vec<Stri
|
|||
result
|
||||
}
|
||||
|
||||
fn master_pubkey_path() -> Result<PathBuf, Error> {
|
||||
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!
|
||||
|
|
|
@ -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<PathBuf, Error> {
|
||||
let base = BaseDirectories::with_prefix("proxmox-backup")?;
|
||||
pub fn find_master_pubkey() -> Result<Option<PathBuf>, 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<PathBuf, Error> {
|
||||
super::place_xdg_file(MASTER_PUBKEY_FILE_NAME, "main public key file")
|
||||
}
|
||||
|
||||
pub fn find_default_encryption_key() -> Result<Option<PathBuf>, 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<PathBuf, Error> {
|
||||
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<Vec<u8>, 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())?;
|
||||
|
||||
|
|
|
@ -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, Error> {
|
||||
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<std::path::Path>,
|
||||
description: &'static str,
|
||||
) -> Result<Option<std::path::PathBuf>, 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<std::path::Path>,
|
||||
description: &'static str,
|
||||
) -> Result<std::path::PathBuf, Error> {
|
||||
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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue