From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 5D1E21FF173 for ; Mon, 13 Jan 2025 16:46:16 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F17C04E56; Mon, 13 Jan 2025 16:45:58 +0100 (CET) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Date: Mon, 13 Jan 2025 16:45:44 +0100 Message-Id: <20250113154550.3462139-6-d.csapak@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250113154550.3462139-1-d.csapak@proxmox.com> References: <20250113154550.3462139-1-d.csapak@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.016 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pdm-devel] [PATCH datacenter-manager 3/9] server: connection: add new function that allows for explicit endpoint X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" sometimes it's necessary to connect to a specific endpoint that is configured, instead of letting it automatically choose, so add the function for that. Signed-off-by: Dominik Csapak --- server/src/connection.rs | 61 ++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/server/src/connection.rs b/server/src/connection.rs index 8c97b4c..0adeba2 100644 --- a/server/src/connection.rs +++ b/server/src/connection.rs @@ -26,11 +26,21 @@ struct ConnectInfo { /// Returns a [`proxmox_client::Client`] and a token prefix for the specified /// [`pdm_api_types::Remote`] -fn prepare_connect_client(remote: &Remote) -> Result { +fn prepare_connect_client( + remote: &Remote, + target_endpoint: Option<&str>, +) -> Result { let node = remote .nodes - .first() - .ok_or_else(|| format_err!("no nodes configured for remote"))?; + .iter() + .find(|endpoint| match target_endpoint { + Some(target) => target == endpoint.hostname, + None => true, + }) + .ok_or_else(|| match target_endpoint { + Some(endpoint) => format_err!("{endpoint} not configured for remote"), + None => format_err!("no nodes configured for remote"), + })?; let mut options = TlsOptions::default(); if let Some(fp) = &node.fingerprint { @@ -66,12 +76,12 @@ fn prepare_connect_client(remote: &Remote) -> Result { /// /// It does not actually opens a connection there, but prepares the client with the correct /// authentication information and settings for the [`RemoteType`] -fn connect(remote: &Remote) -> Result { +fn connect(remote: &Remote, target_endpoint: Option<&str>) -> Result { let ConnectInfo { client, perl_compat, prefix, - } = prepare_connect_client(remote)?; + } = prepare_connect_client(remote, target_endpoint)?; client.set_authentication(proxmox_client::Token { userid: remote.authid.to_string(), prefix, @@ -91,11 +101,14 @@ fn connect(remote: &Remote) -> Result { /// This is intended for API calls that accept a user in addition to tokens. /// /// Note: currently does not support two factor authentication. -async fn connect_or_login(remote: &Remote) -> Result { +async fn connect_or_login( + remote: &Remote, + target_endpoint: Option<&str>, +) -> Result { if remote.authid.is_token() { - connect(remote) + connect(remote, target_endpoint) } else { - let info = prepare_connect_client(remote)?; + let info = prepare_connect_client(remote, target_endpoint)?; let client = info.client; match client .login(proxmox_login::Login::new( @@ -131,6 +144,13 @@ pub trait ClientFactory { /// Create a new API client for PBS remotes fn make_pbs_client(&self, remote: &Remote) -> Result, Error>; + /// Create a new API client for PVE remotes, but with a specific endpoint. + fn make_pve_client_with_endpoint( + &self, + remote: &Remote, + target_endpoint: Option<&str>, + ) -> Result, Error>; + /// Create a new API client for PVE remotes. /// /// In case the remote has a user configured (instead of an API token), it will connect and get @@ -163,25 +183,34 @@ pub struct DefaultClientFactory; #[async_trait::async_trait] impl ClientFactory for DefaultClientFactory { fn make_pve_client(&self, remote: &Remote) -> Result, Error> { - let client = crate::connection::connect(remote)?; + let client = crate::connection::connect(remote, None)?; Ok(Box::new(PveClientImpl(client))) } fn make_pbs_client(&self, remote: &Remote) -> Result, Error> { - let client = crate::connection::connect(remote)?; + let client = crate::connection::connect(remote, None)?; Ok(Box::new(PbsClient(client))) } + fn make_pve_client_with_endpoint( + &self, + remote: &Remote, + target_endpoint: Option<&str>, + ) -> Result, Error> { + let client = crate::connection::connect(remote, target_endpoint)?; + Ok(Box::new(PveClientImpl(client))) + } + async fn make_pve_client_and_login( &self, remote: &Remote, ) -> Result, Error> { - let client = connect_or_login(remote).await?; + let client = connect_or_login(remote, None).await?; Ok(Box::new(PveClientImpl(client))) } async fn make_pbs_client_and_login(&self, remote: &Remote) -> Result, Error> { - let client = connect_or_login(remote).await?; + let client = connect_or_login(remote, None).await?; Ok(Box::new(PbsClient(client))) } } @@ -201,6 +230,14 @@ pub fn make_pve_client(remote: &Remote) -> Result, +) -> Result, Error> { + instance().make_pve_client_with_endpoint(remote, target_endpoint) +} + /// Create a new API client for PBS remotes pub fn make_pbs_client(remote: &Remote) -> Result, Error> { instance().make_pbs_client(remote) -- 2.39.5 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel