From 7b300332cb1ea551c3c4df33a1403fd914fc6a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Sat, 22 Mar 2025 22:31:06 +0000 Subject: [PATCH 1/3] fix(rust): do not try to connect when it is not needed * Do not build the HTTP client for those commands that do not need one. --- rust/agama-cli/src/lib.rs | 45 +++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/rust/agama-cli/src/lib.rs b/rust/agama-cli/src/lib.rs index f25562c5db..a7be1d20ad 100644 --- a/rust/agama-cli/src/lib.rs +++ b/rust/agama-cli/src/lib.rs @@ -217,29 +217,39 @@ pub fn download_file(url: &str, path: &PathBuf) -> Result<(), ServiceError> { Ok(()) } -pub async fn run_command(cli: Cli) -> Result<(), ServiceError> { - // somehow check whether we need to ask user for self-signed certificate acceptance - let api_url = cli.opts.api.trim_end_matches('/').to_string(); - +async fn build_http_client( + api_url: &str, + insecure: bool, + authenticated: bool, +) -> Result { let mut client = BaseHTTPClient::default(); - client.base_url = api_url.clone(); + client.base_url = api_url.to_string(); - if allowed_insecure_api(cli.opts.insecure, api_url.clone()).await? { + if allowed_insecure_api(insecure, api_url.to_string()).await? { client = client.insecure(); } // we need to distinguish commands on those which assume that authentication JWT is already // available and those which not (or don't need it) - client = if let Commands::Auth(_) = cli.command { - client.unauthenticated()? - } else { + if authenticated { // this deals with authentication need inside - client.authenticated()? - }; + client.authenticated() + } else { + client.unauthenticated() + } +} + +pub async fn run_command(cli: Cli) -> Result<(), ServiceError> { + // somehow check whether we need to ask user for self-signed certificate acceptance + + let api_url = cli.opts.api.trim_end_matches('/').to_string(); match cli.command { - Commands::Config(subcommand) => run_config_cmd(client, subcommand).await?, + Commands::Config(subcommand) => { + let client = build_http_client(&api_url, cli.opts.insecure, true).await?; + run_config_cmd(client, subcommand).await? + } Commands::Probe => { let manager = build_manager().await?; wait_for_services(&manager).await?; @@ -255,10 +265,17 @@ pub async fn run_command(cli: Cli) -> Result<(), ServiceError> { let method = method.unwrap_or_default(); finish(&manager, method).await?; } - Commands::Questions(subcommand) => run_questions_cmd(client, subcommand).await?, - Commands::Logs(subcommand) => run_logs_cmd(client, subcommand).await?, + Commands::Questions(subcommand) => { + let client = build_http_client(&api_url, cli.opts.insecure, true).await?; + run_questions_cmd(client, subcommand).await? + } + Commands::Logs(subcommand) => { + let client = build_http_client(&api_url, cli.opts.insecure, true).await?; + run_logs_cmd(client, subcommand).await? + } Commands::Download { url, destination } => download_file(&url, &destination)?, Commands::Auth(subcommand) => { + let client = build_http_client(&api_url, cli.opts.insecure, false).await?; run_auth_cmd(client, subcommand).await?; } }; From a5cce6149702f53fbd0851a40302df6d4218784c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Sat, 22 Mar 2025 22:59:03 +0000 Subject: [PATCH 2/3] docs(rust): update changes file --- rust/package/agama.changes | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/package/agama.changes b/rust/package/agama.changes index 144da778ad..2da42313c2 100644 --- a/rust/package/agama.changes +++ b/rust/package/agama.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Sat Mar 22 22:58:09 UTC 2025 - Imobach Gonzalez Sosa + +- Do not try to connect to the HTTP server when it is not needed + (gh#agama-project/agama#2192). + ------------------------------------------------------------------- Fri Mar 21 16:37:32 UTC 2025 - Ladislav Slezák From 14992360a0b2bf3d7629869df286eb3ed0fcdadd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Mon, 24 Mar 2025 06:38:24 +0000 Subject: [PATCH 3/3] chore(rust): updates from code review --- rust/agama-cli/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rust/agama-cli/src/lib.rs b/rust/agama-cli/src/lib.rs index a7be1d20ad..720d06a5a2 100644 --- a/rust/agama-cli/src/lib.rs +++ b/rust/agama-cli/src/lib.rs @@ -183,10 +183,10 @@ async fn build_manager<'a>() -> anyhow::Result> { /// True if use of the remote API is allowed (yes by default when the API is secure, the user is /// asked if the API is insecure - e.g. when it uses self-signed certificate) -async fn allowed_insecure_api(use_insecure: bool, api_url: String) -> Result { +async fn allowed_insecure_api(use_insecure: bool, api_url: &str) -> Result { // fake client used for remote site detection let mut ping_client = BaseHTTPClient::default(); - ping_client.base_url = api_url; + ping_client.base_url = api_url.to_string(); // decide whether access to remote site has to be insecure (self-signed certificate or not) match ping_client.get::>("/ping").await { @@ -223,10 +223,9 @@ async fn build_http_client( authenticated: bool, ) -> Result { let mut client = BaseHTTPClient::default(); - client.base_url = api_url.to_string(); - if allowed_insecure_api(insecure, api_url.to_string()).await? { + if allowed_insecure_api(insecure, &client.base_url).await? { client = client.insecure(); }