]> git.proxmox.com Git - proxmox-backup.git/commitdiff
more xdg cleanup and encryption parameter improvements
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 8 Jul 2020 08:56:16 +0000 (10:56 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 8 Jul 2020 08:57:28 +0000 (10:57 +0200)
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>
src/bin/proxmox-backup-client.rs
src/bin/proxmox_backup_client/key.rs
src/bin/proxmox_backup_client/mod.rs

index 7bd82e783cf92dc7909ee668b19acbe0bb7c33c3..a6cbcc1eed0fcfccf8c4deb0275ced4dccfde408 100644 (file)
@@ -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)?;
-                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<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!
index 05d0a28ddc37bc65ebcc14173ea0205bce974124..b0218975a89c03f7da20b54ee2c732ae466fe550 100644 (file)
@@ -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")?;
-
-    // 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> {
@@ -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())?;
 
index 054aff5bd254ad871e2ce5c6225c98dd69c45ff2..0c4bffb957be78ebcb646219037f5aa186fc5fd5 100644 (file)
@@ -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))
+}