From 0df236746548ed001bae41c8a08d8eca45c703d5 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Mon, 16 Sep 2024 20:30:49 +0330 Subject: [PATCH 1/4] apply the proxy setting in wasmer config --- lib/backend-api/src/client.rs | 26 +++++-------------- lib/cli/src/commands/package/download.rs | 16 ++---------- lib/cli/src/config/env.rs | 33 ++++++++++-------------- lib/cli/src/config/mod.rs | 6 ++++- tests/wasmer-argus/src/argus/mod.rs | 2 +- 5 files changed, 29 insertions(+), 54 deletions(-) diff --git a/lib/backend-api/src/client.rs b/lib/backend-api/src/client.rs index 6407ab0edd1..46ddaeaf220 100644 --- a/lib/backend-api/src/client.rs +++ b/lib/backend-api/src/client.rs @@ -75,10 +75,10 @@ impl WasmerClient { }) } - pub fn new_with_proxy( + pub fn new( graphql_endpoint: Url, user_agent: &str, - proxy: reqwest::Proxy, + proxy: Option, ) -> Result { let builder = { #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] @@ -89,7 +89,11 @@ impl WasmerClient { .connect_timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(90)); - builder.proxy(proxy) + if let Some(proxy) = proxy { + builder.proxy(proxy) + } else { + builder + } }; let client = builder.build().context("failed to create reqwest client")?; @@ -97,22 +101,6 @@ impl WasmerClient { Self::new_with_client(client, graphql_endpoint, user_agent) } - pub fn new(graphql_endpoint: Url, user_agent: &str) -> Result { - #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] - let client = reqwest::Client::builder() - .build() - .context("could not construct http client")?; - - #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] - let client = reqwest::Client::builder() - .connect_timeout(Duration::from_secs(10)) - .timeout(Duration::from_secs(90)) - .build() - .context("could not construct http client")?; - - Self::new_with_client(client, graphql_endpoint, user_agent) - } - pub fn with_auth_token(mut self, auth_token: String) -> Self { self.auth_token = Some(auth_token); self diff --git a/lib/cli/src/commands/package/download.rs b/lib/cli/src/commands/package/download.rs index bc168fae95e..97cc9650a4a 100644 --- a/lib/cli/src/commands/package/download.rs +++ b/lib/cli/src/commands/package/download.rs @@ -5,7 +5,6 @@ use dialoguer::console::{style, Emoji}; use indicatif::{ProgressBar, ProgressStyle}; use tempfile::NamedTempFile; use wasmer_config::package::{PackageIdent, PackageSource}; -use wasmer_wasix::http::reqwest::get_proxy; use crate::config::WasmerEnv; @@ -28,10 +27,6 @@ pub struct PackageDownload { #[clap(long)] pub quiet: bool, - /// proxy to use for downloading - #[clap(long)] - pub proxy: Option, - /// The package to download. package: PackageSource, } @@ -101,13 +96,7 @@ impl PackageDownload { // caveat: client_unauthennticated will use a token if provided, it // just won't fail if none is present. So, _unauthenticated() can actually // produce an authenticated client. - let client = if let Some(proxy) = &self.proxy { - let proxy = reqwest::Proxy::all(proxy)?; - - self.env.client_unauthennticated_with_proxy(proxy)? - } else { - self.env.client_unauthennticated()? - }; + let client = self.env.client_unauthennticated()?; let version = id.version_or_default().to_string(); let version = if version == "*" { @@ -171,7 +160,7 @@ impl PackageDownload { let builder = { let mut builder = reqwest::blocking::ClientBuilder::new(); - if let Some(proxy) = get_proxy()? { + if let Some(proxy) = self.env.proxy()? { builder = builder.proxy(proxy); } builder @@ -309,7 +298,6 @@ mod tests { out_path: Some(out_path.clone()), package: "wasmer/hello@0.1.0".parse().unwrap(), quiet: true, - proxy: None, }; cmd.execute().unwrap(); diff --git a/lib/cli/src/config/env.rs b/lib/cli/src/config/env.rs index 496962c14f7..f202d7eed7c 100644 --- a/lib/cli/src/config/env.rs +++ b/lib/cli/src/config/env.rs @@ -91,6 +91,17 @@ impl WasmerEnv { }) } + /// Returns the proxy specified in wasmer config if present + pub fn proxy(&self) -> Result, Error> { + self.config()? + .proxy + .url + .as_ref() + .map(reqwest::Proxy::all) + .transpose() + .map_err(Into::into) + } + /// The directory all Wasmer artifacts are stored in. pub fn dir(&self) -> &Path { &self.wasmer_dir @@ -127,27 +138,11 @@ impl WasmerEnv { pub fn client_unauthennticated(&self) -> Result { let registry_url = self.registry_endpoint()?; - let client = wasmer_api::WasmerClient::new(registry_url, &DEFAULT_WASMER_CLI_USER_AGENT)?; - let client = if let Some(token) = self.token() { - client.with_auth_token(token) - } else { - client - }; + let proxy = self.proxy()?; - Ok(client) - } - - pub fn client_unauthennticated_with_proxy( - &self, - proxy: reqwest::Proxy, - ) -> Result { - let registry_url = self.registry_endpoint()?; - let client = wasmer_api::WasmerClient::new_with_proxy( - registry_url, - &DEFAULT_WASMER_CLI_USER_AGENT, - proxy, - )?; + let client = + wasmer_api::WasmerClient::new(registry_url, &DEFAULT_WASMER_CLI_USER_AGENT, proxy)?; let client = if let Some(token) = self.token() { client.with_auth_token(token) diff --git a/lib/cli/src/config/mod.rs b/lib/cli/src/config/mod.rs index 23c95f21f3a..8ecb38df7db 100644 --- a/lib/cli/src/config/mod.rs +++ b/lib/cli/src/config/mod.rs @@ -110,7 +110,11 @@ fn endpoint_from_domain_name(domain_name: &str) -> String { } async fn test_if_registry_present(registry: &str) -> anyhow::Result<()> { - let client = WasmerClient::new(url::Url::parse(registry)?, &DEFAULT_WASMER_CLI_USER_AGENT)?; + let client = WasmerClient::new( + url::Url::parse(registry)?, + &DEFAULT_WASMER_CLI_USER_AGENT, + None, + )?; wasmer_api::query::current_user(&client).await.map(|_| ()) } diff --git a/tests/wasmer-argus/src/argus/mod.rs b/tests/wasmer-argus/src/argus/mod.rs index 63260730592..fde65e51e38 100644 --- a/tests/wasmer-argus/src/argus/mod.rs +++ b/tests/wasmer-argus/src/argus/mod.rs @@ -28,7 +28,7 @@ impl TryFrom for Argus { type Error = anyhow::Error; fn try_from(config: ArgusConfig) -> Result { - let client = WasmerClient::new(Url::parse(&config.registry_url)?, "wasmer-argus")?; + let client = WasmerClient::new(Url::parse(&config.registry_url)?, "wasmer-argus", None)?; let client = client.with_auth_token(config.auth_token.clone()); Ok(Argus { client, config }) From f0506e1933ac4612eaf932702b4dc36e4c0685e0 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Mon, 16 Sep 2024 21:08:41 +0330 Subject: [PATCH 2/4] propagate proxy config to upload --- lib/cli/src/commands/package/common/mod.rs | 19 ++++++++++++++----- lib/cli/src/commands/package/push.rs | 10 +++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/cli/src/commands/package/common/mod.rs b/lib/cli/src/commands/package/common/mod.rs index 5641f96bfde..76f3e2c0bf6 100644 --- a/lib/cli/src/commands/package/common/mod.rs +++ b/lib/cli/src/commands/package/common/mod.rs @@ -44,6 +44,7 @@ pub(super) async fn upload( timeout: humantime::Duration, package: &Package, pb: ProgressBar, + proxy: Option, ) -> anyhow::Result { let hash_str = hash.to_string(); let hash_str = hash_str.trim_start_matches("sha256:"); @@ -68,11 +69,19 @@ pub(super) async fn upload( tracing::info!("signed url is: {session_uri}"); - let client = reqwest::Client::builder() - .default_headers(reqwest::header::HeaderMap::default()) - .timeout(timeout.into()) - .build() - .unwrap(); + let client = { + let builder = reqwest::Client::builder() + .default_headers(reqwest::header::HeaderMap::default()) + .timeout(timeout.into()); + + let builder = if let Some(proxy) = proxy { + builder.proxy(proxy) + } else { + builder + }; + + builder.build().unwrap() + }; let res = client .post(&session_uri) diff --git a/lib/cli/src/commands/package/push.rs b/lib/cli/src/commands/package/push.rs index 03de8e9aad7..7c2628949ea 100644 --- a/lib/cli/src/commands/package/push.rs +++ b/lib/cli/src/commands/package/push.rs @@ -128,7 +128,15 @@ impl PackagePush { ) -> anyhow::Result<()> { let pb = make_spinner!(self.quiet, "Uploading the package.."); - let signed_url = upload(client, package_hash, self.timeout, package, pb.clone()).await?; + let signed_url = upload( + client, + package_hash, + self.timeout, + package, + pb.clone(), + self.env.proxy()?, + ) + .await?; spinner_ok!(pb, "Package correctly uploaded"); let pb = make_spinner!(self.quiet, "Waiting for package to become available..."); From 95e46782d9b49abce4b23e57ebd34f7393cfcb71 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Mon, 16 Sep 2024 21:28:42 +0330 Subject: [PATCH 3/4] prevent api breakage --- lib/backend-api/src/client.rs | 6 +++++- lib/cli/src/config/env.rs | 2 +- lib/cli/src/config/mod.rs | 6 +----- tests/wasmer-argus/src/argus/mod.rs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/backend-api/src/client.rs b/lib/backend-api/src/client.rs index 46ddaeaf220..d3579939f72 100644 --- a/lib/backend-api/src/client.rs +++ b/lib/backend-api/src/client.rs @@ -75,7 +75,11 @@ impl WasmerClient { }) } - pub fn new( + pub fn new(graphql_endpoint: Url, user_agent: &str) -> Result { + Self::new_with_proxy(graphql_endpoint, user_agent, None) + } + + pub fn new_with_proxy( graphql_endpoint: Url, user_agent: &str, proxy: Option, diff --git a/lib/cli/src/config/env.rs b/lib/cli/src/config/env.rs index f202d7eed7c..e5dfc052dc9 100644 --- a/lib/cli/src/config/env.rs +++ b/lib/cli/src/config/env.rs @@ -142,7 +142,7 @@ impl WasmerEnv { let proxy = self.proxy()?; let client = - wasmer_api::WasmerClient::new(registry_url, &DEFAULT_WASMER_CLI_USER_AGENT, proxy)?; + wasmer_api::WasmerClient::new_with_proxy(registry_url, &DEFAULT_WASMER_CLI_USER_AGENT, proxy)?; let client = if let Some(token) = self.token() { client.with_auth_token(token) diff --git a/lib/cli/src/config/mod.rs b/lib/cli/src/config/mod.rs index 8ecb38df7db..23c95f21f3a 100644 --- a/lib/cli/src/config/mod.rs +++ b/lib/cli/src/config/mod.rs @@ -110,11 +110,7 @@ fn endpoint_from_domain_name(domain_name: &str) -> String { } async fn test_if_registry_present(registry: &str) -> anyhow::Result<()> { - let client = WasmerClient::new( - url::Url::parse(registry)?, - &DEFAULT_WASMER_CLI_USER_AGENT, - None, - )?; + let client = WasmerClient::new(url::Url::parse(registry)?, &DEFAULT_WASMER_CLI_USER_AGENT)?; wasmer_api::query::current_user(&client).await.map(|_| ()) } diff --git a/tests/wasmer-argus/src/argus/mod.rs b/tests/wasmer-argus/src/argus/mod.rs index fde65e51e38..63260730592 100644 --- a/tests/wasmer-argus/src/argus/mod.rs +++ b/tests/wasmer-argus/src/argus/mod.rs @@ -28,7 +28,7 @@ impl TryFrom for Argus { type Error = anyhow::Error; fn try_from(config: ArgusConfig) -> Result { - let client = WasmerClient::new(Url::parse(&config.registry_url)?, "wasmer-argus", None)?; + let client = WasmerClient::new(Url::parse(&config.registry_url)?, "wasmer-argus")?; let client = client.with_auth_token(config.auth_token.clone()); Ok(Argus { client, config }) From fe1b72a3b23722dbf4766e837ee88d558bf68197 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Mon, 16 Sep 2024 21:35:00 +0330 Subject: [PATCH 4/4] fix lint --- lib/cli/src/config/env.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/cli/src/config/env.rs b/lib/cli/src/config/env.rs index e5dfc052dc9..a328f66d40c 100644 --- a/lib/cli/src/config/env.rs +++ b/lib/cli/src/config/env.rs @@ -141,8 +141,11 @@ impl WasmerEnv { let proxy = self.proxy()?; - let client = - wasmer_api::WasmerClient::new_with_proxy(registry_url, &DEFAULT_WASMER_CLI_USER_AGENT, proxy)?; + let client = wasmer_api::WasmerClient::new_with_proxy( + registry_url, + &DEFAULT_WASMER_CLI_USER_AGENT, + proxy, + )?; let client = if let Some(token) = self.token() { client.with_auth_token(token)