From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pdm-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 03FE91FF15C for <inbox@lore.proxmox.com>; Fri, 16 May 2025 15:36:28 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4F478DBEF; Fri, 16 May 2025 15:36:52 +0200 (CEST) From: Dominik Csapak <d.csapak@proxmox.com> To: pdm-devel@lists.proxmox.com Date: Fri, 16 May 2025 15:36:10 +0200 Message-Id: <20250516133611.3499075-21-d.csapak@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250516133611.3499075-1-d.csapak@proxmox.com> References: <20250516133611.3499075-1-d.csapak@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.022 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 20/21] ui: pve wizard: connect: use scan api endpoint instead of realms X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion <pdm-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pdm-devel>, <mailto:pdm-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pdm-devel/> List-Post: <mailto:pdm-devel@lists.proxmox.com> List-Help: <mailto:pdm-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel>, <mailto:pdm-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox Datacenter Manager development discussion <pdm-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" <pdm-devel-bounces@lists.proxmox.com> Since we don't need to query the realms anymore for the next page, we can now use the scan api endpoint without credentials to probe the connection to see if TLS works and if the certificate is trusted and/or if the fingerprint is correct. Signed-off-by: Dominik Csapak <d.csapak@proxmox.com> --- ui/src/remotes/wizard_page_connect.rs | 166 ++++++++++++-------------- 1 file changed, 79 insertions(+), 87 deletions(-) diff --git a/ui/src/remotes/wizard_page_connect.rs b/ui/src/remotes/wizard_page_connect.rs index d5d9708..9f73779 100644 --- a/ui/src/remotes/wizard_page_connect.rs +++ b/ui/src/remotes/wizard_page_connect.rs @@ -2,22 +2,19 @@ use std::rc::Rc; use anyhow::{bail, Error}; use serde::{Deserialize, Serialize}; -use serde_json::json; use yew::html::IntoEventCallback; use yew::virtual_dom::{Key, VComp, VNode}; use pwt::css::FlexFit; use pwt::widget::form::{Field, FormContext, FormContextObserver}; use pwt::widget::{error_message, Column, InputPanel, Mask}; -use pwt::{prelude::*, AsyncPool}; +use pwt::{prelude::*, AsyncAbortGuard}; +use pwt_macros::builder; use proxmox_yew_comp::{SchemaValidation, WizardPageRenderInfo}; -use pdm_api_types::remotes::RemoteType; +use pdm_api_types::remotes::{RemoteType, ScanResult}; use pdm_api_types::CERT_FINGERPRINT_SHA256_SCHEMA; -use pdm_client::types::ListRealm; - -use pwt_macros::builder; #[derive(Clone, PartialEq, Properties)] #[builder] @@ -37,69 +34,46 @@ impl WizardPageConnect { } } -async fn list_realms( - hostname: String, - fingerprint: Option<String>, -) -> Result<Vec<ListRealm>, Error> { - let mut params = json!({ - "hostname": hostname, - }); - if let Some(fp) = fingerprint { - params["fingerprint"] = fp.into(); - } - let result: Vec<ListRealm> = proxmox_yew_comp::http_get("/pve/realms", Some(params)).await?; - - Ok(result) -} - #[derive(PartialEq, Clone, Deserialize, Serialize)] /// Parameters for connect call. pub struct ConnectParams { pub hostname: String, #[serde(skip_serializing_if = "Option::is_none")] pub fingerprint: Option<String>, - #[serde(default)] - pub realms: Vec<ListRealm>, } -async fn connect(form_ctx: FormContext, remote_type: RemoteType) -> Result<ConnectParams, Error> { - let data = form_ctx.get_submit_data(); - let mut data: ConnectParams = serde_json::from_value(data.clone())?; - data.hostname = normalize_hostname(data.hostname); +async fn connect(form_ctx: FormContext, remote_type: RemoteType) -> Result<ScanResult, Error> { + match remote_type { + RemoteType::Pve => { + let hostname = normalize_hostname(form_ctx.read().get_field_text("hostname")); + let fingerprint = get_fingerprint(&form_ctx); + let res = crate::pdm_client() + .pve_probe_tls(&hostname, fingerprint.as_deref()) + .await + .map_err(Error::from); + + if let Ok(ScanResult::TlsResult(Some(_))) = &res { + bail!("Untrusted Certificate, please enter fingerprint"); + } - let realms = match remote_type { - RemoteType::Pve => list_realms(data.hostname.clone(), data.fingerprint.clone()).await?, + res + } RemoteType::Pbs => bail!("not implemented"), - }; - - data.realms = realms; - Ok(data) + } } pub enum Msg { FormChange, Connect, - ConnectResult(Result<ConnectParams, Error>), + ConnectResult(Result<ScanResult, Error>), } pub struct PdmWizardPageConnect { - connect_info: Option<ConnectParams>, _form_observer: FormContextObserver, - form_valid: bool, loading: bool, - last_error: Option<Error>, - async_pool: AsyncPool, + scan_result: Option<Result<ScanResult, Error>>, + scan_guard: Option<AsyncAbortGuard>, } -impl PdmWizardPageConnect { - fn update_connect_info(&mut self, ctx: &Context<Self>, info: Option<ConnectParams>) { - let props = ctx.props(); - self.connect_info = info.clone(); - props.info.page_lock(info.is_none()); - if let Some(on_connect_change) = &props.on_connect_change { - on_connect_change.emit(info); - } - } -} impl Component for PdmWizardPageConnect { type Message = Msg; type Properties = WizardPageConnect; @@ -122,12 +96,10 @@ impl Component for PdmWizardPageConnect { }); Self { - connect_info: None, _form_observer, - form_valid: false, loading: false, - last_error: None, - async_pool: AsyncPool::new(), + scan_result: None, + scan_guard: None, } } @@ -135,47 +107,44 @@ impl Component for PdmWizardPageConnect { let props = ctx.props(); match msg { Msg::FormChange => { - self.form_valid = props.info.form_ctx.read().is_valid(); - match props.remote_type { - RemoteType::Pve => { - self.update_connect_info(ctx, None); - } - RemoteType::Pbs => { - return <Self as yew::Component>::update(self, ctx, Msg::Connect) - } - } - props.info.page_lock(!self.form_valid); + props.info.page_lock(!props.info.form_ctx.read().is_valid()); + props.info.reset_remaining_valid_pages(); + self.scan_result = None; } Msg::Connect => { - let link = ctx.link().clone(); - self.update_connect_info(ctx, None); - let form_ctx = props.info.form_ctx.clone(); self.loading = true; - self.last_error = None; + props.info.page_lock(true); + + self.scan_guard = Some(AsyncAbortGuard::spawn({ + let link = ctx.link().clone(); + let form_ctx = props.info.form_ctx.clone(); + let remote_type = props.remote_type; - let remote_type = props.remote_type; - self.async_pool.spawn(async move { - let result = connect(form_ctx, remote_type).await; - link.send_message(Msg::ConnectResult(result)); - }); + async move { + let result = connect(form_ctx, remote_type).await; + link.send_message(Msg::ConnectResult(result)); + } + })); } - Msg::ConnectResult(server_info) => { + Msg::ConnectResult(scan_result) => { self.loading = false; - match server_info { - Ok(connect_info) => { - self.update_connect_info(ctx, Some(connect_info)); - } - Err(err) => { - self.last_error = Some(err); + props.info.page_lock(false); + self.scan_result = Some(scan_result); + match &self.scan_result { + Some(Ok(ScanResult::TlsResult(None))) => { + call_on_connect_change(props); + for page in ["nodes", "info"] { + if let Some(form_ctx) = props.info.lookup_form_context(&Key::from(page)) + { + form_ctx.write().reset_form(); + } + } + self.scan_result = None; + props.info.reset_remaining_valid_pages(); + props.info.go_to_next_page(); } - } - - if let Some(form_ctx) = props.info.lookup_form_context(&Key::from("nodes")) { - form_ctx.write().reset_form(); - } - props.info.reset_remaining_valid_pages(); - if self.connect_info.is_some() { - props.info.go_to_next_page(); + Some(Err(_)) => props.info.page_lock(true), + _ => {} } } } @@ -183,7 +152,10 @@ impl Component for PdmWizardPageConnect { } fn view(&self, _ctx: &Context<Self>) -> Html { - let error = self.last_error.as_ref(); + let error = match &self.scan_result { + Some(Err(err)) => Some(err), + _ => None, + }; let input_panel = InputPanel::new() .class(FlexFit) // FIXME: input panel css style is not optimal here... @@ -215,6 +187,26 @@ impl Component for PdmWizardPageConnect { } } +fn get_fingerprint(form_ctx: &FormContext) -> Option<String> { + let fingerprint = form_ctx.read().get_field_text("fingerprint"); + let fingerprint = if fingerprint.is_empty() { + None + } else { + Some(fingerprint) + }; + fingerprint +} + +fn call_on_connect_change(props: &WizardPageConnect) { + if let Some(on_connect_change) = &props.on_connect_change { + let fingerprint = get_fingerprint(&props.info.form_ctx); + on_connect_change.emit(Some(ConnectParams { + hostname: normalize_hostname(props.info.form_ctx.read().get_field_text("hostname")), + fingerprint, + })); + } +} + fn normalize_hostname(hostname: String) -> String { let mut result = hostname; if let Some(hostname) = result.strip_prefix("http://") { -- 2.39.5 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel