From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 683C86AFB8 for ; Mon, 25 Jan 2021 14:44:18 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 64B87B0C4 for ; Mon, 25 Jan 2021 14:43:48 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 5D985AFFD for ; Mon, 25 Jan 2021 14:43:47 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 2A2AC460E9 for ; Mon, 25 Jan 2021 14:43:47 +0100 (CET) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Mon, 25 Jan 2021 14:42:57 +0100 Message-Id: <20210125134302.3394328-13-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210125134302.3394328-1-f.gruenbichler@proxmox.com> References: <20210125134302.3394328-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.026 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 12/15] derive/impl and use Default for some structs X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jan 2021 13:44:18 -0000 and revamp HttpClientOptions with two constructors for the common use cases Signed-off-by: Fabian Grünbichler --- 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 { 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) -> Result<(), Error> /// Helper to get client for remote.cfg entry pub async fn remote_client(remote: remote::Remote) -> Result { - 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, pub keep_hourly: Option, 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 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 { 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, fingerprint: Option) -> 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) -> 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, 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, order: Vec, -- 2.20.1