[pbs-devel] [PATCH proxmox-backup 12/15] derive/impl and use Default for some structs

Fabian Grünbichler f.gruenbichler at proxmox.com
Mon Jan 25 14:42:57 CET 2021


and revamp HttpClientOptions with two constructors for the common use
cases

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
breaks proxmox-backup-qemu, corresponding patch comes later in this
series

 examples/download-speed.rs       |  2 +-
 examples/upload-speed.rs         |  2 +-
 src/api2/config/remote.rs        |  4 +---
 src/backup/prune.rs              |  1 +
 src/bin/proxmox-backup-client.rs | 18 ++++-----------
 src/client.rs                    | 11 +++++----
 src/client/http_client.rs        | 38 +++++++++++++++++++++++++-------
 src/client/pull.rs               |  4 +---
 src/config/acl.rs                |  8 ++++---
 src/config/network.rs            |  2 +-
 10 files changed, 50 insertions(+), 40 deletions(-)

diff --git a/examples/download-speed.rs b/examples/download-speed.rs
index 3ccf4ce7..a4afb7ba 100644
--- a/examples/download-speed.rs
+++ b/examples/download-speed.rs
@@ -28,7 +28,7 @@ async fn run() -> Result<(), Error> {
 
     let auth_id = Authid::root_auth_id();
 
-    let options = HttpClientOptions::new()
+    let options = HttpClientOptions::default()
         .interactive(true)
         .ticket_cache(true);
 
diff --git a/examples/upload-speed.rs b/examples/upload-speed.rs
index 641ed952..05e44aaf 100644
--- a/examples/upload-speed.rs
+++ b/examples/upload-speed.rs
@@ -10,7 +10,7 @@ async fn upload_speed() -> Result<f64, Error> {
 
     let auth_id = Authid::root_auth_id();
 
-    let options = HttpClientOptions::new()
+    let options = HttpClientOptions::default()
         .interactive(true)
         .ticket_cache(true);
 
diff --git a/src/api2/config/remote.rs b/src/api2/config/remote.rs
index fe7dc451..28221358 100644
--- a/src/api2/config/remote.rs
+++ b/src/api2/config/remote.rs
@@ -310,9 +310,7 @@ pub fn delete_remote(name: String, digest: Option<String>) -> Result<(), Error>
 
 /// Helper to get client for remote.cfg entry
 pub async fn remote_client(remote: remote::Remote) -> Result<HttpClient, Error> {
-    let options = HttpClientOptions::new()
-        .password(Some(remote.password.clone()))
-        .fingerprint(remote.fingerprint.clone());
+    let options = HttpClientOptions::new_non_interactive(remote.password.clone(), remote.fingerprint.clone());
 
     let client = HttpClient::new(
         &remote.host,
diff --git a/src/backup/prune.rs b/src/backup/prune.rs
index baec57d6..dd038055 100644
--- a/src/backup/prune.rs
+++ b/src/backup/prune.rs
@@ -67,6 +67,7 @@ fn remove_incomplete_snapshots(
     }
 }
 
+#[derive(Default)]
 pub struct PruneOptions {
     pub keep_last: Option<u64>,
     pub keep_hourly: Option<u64>,
diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs
index d31e47ae..fe305f63 100644
--- a/src/bin/proxmox-backup-client.rs
+++ b/src/bin/proxmox-backup-client.rs
@@ -211,13 +211,7 @@ fn connect_do(server: &str, port: u16, auth_id: &Authid) -> Result<HttpClient, E
         Err(NotPresent) => None,
     };
 
-    let options = HttpClientOptions::new()
-        .prefix(Some("proxmox-backup".to_string()))
-        .password(password)
-        .interactive(true)
-        .fingerprint(fingerprint)
-        .fingerprint_cache(true)
-        .ticket_cache(true);
+    let options = HttpClientOptions::new_interactive(password, fingerprint);
 
     HttpClient::new(server, port, auth_id, options)
 }
@@ -1565,13 +1559,9 @@ async fn try_get(repo: &BackupRepository, url: &str) -> Value {
     let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok();
     let password = std::env::var(ENV_VAR_PBS_PASSWORD).ok();
 
-    let options = HttpClientOptions::new()
-        .prefix(Some("proxmox-backup".to_string()))
-        .password(password)
-        .interactive(false)
-        .fingerprint(fingerprint)
-        .fingerprint_cache(true)
-        .ticket_cache(true);
+    // ticket cache, but no questions asked
+    let options = HttpClientOptions::new_interactive(password, fingerprint)
+        .interactive(false);
 
     let client = match HttpClient::new(repo.host(), repo.port(), repo.auth_id(), options) {
         Ok(v) => v,
diff --git a/src/client.rs b/src/client.rs
index 8c4542b6..d50c26c2 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -49,17 +49,16 @@ pub fn connect_to_localhost() -> Result<HttpClient, Error> {
 
     let uid = nix::unistd::Uid::current();
 
-    let mut options = HttpClientOptions::new()
-        .prefix(Some("proxmox-backup".to_string()))
-        .verify_cert(false); // not required for connection to localhost
-
     let client = if uid.is_root()  {
         let ticket = Ticket::new("PBS", Userid::root_userid())?
             .sign(private_auth_key(), None)?;
-        options = options.password(Some(ticket));
+        let fingerprint = crate::tools::cert::CertInfo::new()?.fingerprint()?;
+        let options = HttpClientOptions::new_non_interactive(ticket, Some(fingerprint));
+
         HttpClient::new("localhost", 8007, Authid::root_auth_id(), options)?
     } else {
-        options = options.ticket_cache(true).interactive(true);
+        let options = HttpClientOptions::new_interactive(None, None);
+
         HttpClient::new("localhost", 8007, Authid::root_auth_id(), options)?
     };
 
diff --git a/src/client/http_client.rs b/src/client/http_client.rs
index f279d9dd..9fd1c013 100644
--- a/src/client/http_client.rs
+++ b/src/client/http_client.rs
@@ -52,15 +52,23 @@ pub struct HttpClientOptions {
 
 impl HttpClientOptions {
 
-    pub fn new() -> Self {
+    pub fn new_interactive(password: Option<String>, fingerprint: Option<String>) -> Self {
         Self {
-            prefix: None,
-            password: None,
-            fingerprint: None,
-            interactive: false,
-            ticket_cache: false,
-            fingerprint_cache: false,
-            verify_cert: true,
+            password,
+            fingerprint,
+            fingerprint_cache: true,
+            ticket_cache: true,
+            interactive: true,
+            prefix: Some("proxmox-backup".to_string()),
+            ..Self::default()
+        }
+    }
+
+    pub fn new_non_interactive(password: String, fingerprint: Option<String>) -> Self {
+        Self {
+            password: Some(password),
+            fingerprint,
+            ..Self::default()
         }
     }
 
@@ -100,6 +108,20 @@ impl HttpClientOptions {
     }
 }
 
+impl Default for HttpClientOptions {
+    fn default() -> Self {
+        Self {
+            prefix: None,
+            password: None,
+            fingerprint: None,
+            interactive: false,
+            ticket_cache: false,
+            fingerprint_cache: false,
+            verify_cert: true,
+        }
+    }
+}
+
 /// HTTP(S) API client
 pub struct HttpClient {
     client: Client<HttpsConnector>,
diff --git a/src/client/pull.rs b/src/client/pull.rs
index 15514374..95720973 100644
--- a/src/client/pull.rs
+++ b/src/client/pull.rs
@@ -502,9 +502,7 @@ pub async fn pull_group(
         // get updated auth_info (new tickets)
         let auth_info = client.login().await?;
 
-        let options = HttpClientOptions::new()
-            .password(Some(auth_info.ticket.clone()))
-            .fingerprint(fingerprint.clone());
+        let options = HttpClientOptions::new_non_interactive(auth_info.ticket.clone(), fingerprint.clone());
 
         let new_client = HttpClient::new(
             src_repo.host(),
diff --git a/src/config/acl.rs b/src/config/acl.rs
index 6ef54e30..e02ac5c7 100644
--- a/src/config/acl.rs
+++ b/src/config/acl.rs
@@ -299,6 +299,7 @@ pub fn check_acl_path(path: &str) -> Result<(), Error> {
 }
 
 /// Tree representing a parsed acl.cfg
+#[derive(Default)]
 pub struct AclTree {
     /// Root node of the tree.
     ///
@@ -308,6 +309,7 @@ pub struct AclTree {
 }
 
 /// Node representing ACLs for a certain ACL path.
+#[derive(Default)]
 pub struct AclTreeNode {
     /// [User](crate::config::user::User) or
     /// [Token](crate::config::user::ApiToken) ACLs for this node.
@@ -412,7 +414,7 @@ impl AclTreeNode {
     }
 
     fn insert_group_role(&mut self, group: String, role: String, propagate: bool) {
-        let map = self.groups.entry(group).or_insert_with(HashMap::new);
+        let map = self.groups.entry(group).or_default();
         if role == ROLE_NAME_NO_ACCESS {
             map.clear();
             map.insert(role, propagate);
@@ -423,7 +425,7 @@ impl AclTreeNode {
     }
 
     fn insert_user_role(&mut self, auth_id: Authid, role: String, propagate: bool) {
-        let map = self.users.entry(auth_id).or_insert_with(HashMap::new);
+        let map = self.users.entry(auth_id).or_default();
         if role == ROLE_NAME_NO_ACCESS {
             map.clear();
             map.insert(role, propagate);
@@ -465,7 +467,7 @@ impl AclTree {
             node = node
                 .children
                 .entry(String::from(*comp))
-                .or_insert_with(AclTreeNode::new);
+                .or_default();
         }
         node
     }
diff --git a/src/config/network.rs b/src/config/network.rs
index 4241261a..99ea0d08 100644
--- a/src/config/network.rs
+++ b/src/config/network.rs
@@ -318,7 +318,7 @@ enum NetworkOrderEntry {
     Option(String),
 }
 
-#[derive(Debug)]
+#[derive(Debug, Default)]
 pub struct NetworkConfig {
     pub interfaces: BTreeMap<String, Interface>,
     order: Vec<NetworkOrderEntry>,
-- 
2.20.1






More information about the pbs-devel mailing list