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),
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),
}
}
};
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!
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};
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")?;
-
- // usually $HOME/.config/proxmox-backup/master-public.pem
- let path = base.place_config_file("master-public.pem")?;
+pub fn find_master_pubkey() -> Result<Option<PathBuf>, Error> {
+ super::find_xdg_file(MASTER_PUBKEY_FILE_NAME, "main public key file")
+}
- 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> {
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())?;
+use anyhow::{Context, Error};
+
mod benchmark;
pub use benchmark::*;
mod mount;
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))
+}