public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH datacenter-manager 20/21] ui: pve wizard: connect: use scan api endpoint instead of realms
Date: Fri, 16 May 2025 15:36:10 +0200	[thread overview]
Message-ID: <20250516133611.3499075-21-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250516133611.3499075-1-d.csapak@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


  parent reply	other threads:[~2025-05-16 13:36 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-16 13:35 [pdm-devel] [PATCH datacenter-manager 00/21] improve remote wizard Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 01/21] server/ui: pve: change 'realm list' api call to GET Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 02/21] api types: RemoteType: put default port info to the type Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 03/21] server: connection: add probe_tls_connection helper Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 04/21] server/ui: pve api: extend 'scan' so it can probe the tls connection Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 05/21] pdm-client: add scan_remote and probe_tls methods Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 06/21] ui: remotes: node url list: add placeholder and clear trigger Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 07/21] ui: rmeotes: node url list: make column header clearer Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 08/21] ui: remotes: node url list: handle changing default Dominik Csapak
2025-05-16 13:35 ` [pdm-devel] [PATCH datacenter-manager 09/21] ui: pve wizard: rename 'realm' variable to 'info' Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 10/21] ui: pve wizard: summary: add default text for fingerprint Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 11/21] ui: pve wizard: nodes: improve info text Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 12/21] ui: pve wizard: nodes: probe hosts to verify fingerprint settings Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 13/21] ui: pve wizard: info: use pdm_client for scanning Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 14/21] ui: pve wizard: info: detect hostname and fingerprint Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 15/21] ui: pve wizard: info: remove manual scan button Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 16/21] ui: widget: add pve realm selector Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 17/21] ui: pve wizard: info: use " Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 18/21] ui: pve wizard: connect: factor out normalize_hostname Dominik Csapak
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 19/21] ui: pve wizard: connect: move connection logic to next button Dominik Csapak
2025-05-16 13:36 ` Dominik Csapak [this message]
2025-05-16 13:36 ` [pdm-devel] [PATCH datacenter-manager 21/21] ui: pve wizard: connect: add certificate confirmation dialog Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250516133611.3499075-21-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal