From 8d0bd824e5dcbe4507c4ba09bf66429a789674eb Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Mon, 28 Oct 2024 09:58:40 +0100 Subject: [PATCH] update to use cli flag along with env var Signed-off-by: karthik2804 --- crates/plugins/src/manager.rs | 34 ++++++++++++++++++++++++++-------- src/commands/plugins.rs | 23 +++++++++++++++++++++-- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/crates/plugins/src/manager.rs b/crates/plugins/src/manager.rs index 27bbab0c1..df8d25f43 100644 --- a/crates/plugins/src/manager.rs +++ b/crates/plugins/src/manager.rs @@ -88,6 +88,7 @@ impl PluginManager { plugin_manifest: &PluginManifest, plugin_package: &PluginPackage, source: &ManifestLocation, + auth_header_value: Option, ) -> Result { let target = plugin_package.url.to_owned(); let target_url = Url::parse(&target)?; @@ -106,7 +107,15 @@ impl PluginManager { ); } } - _ => download_plugin(&plugin_manifest.name(), &temp_dir, &target).await?, + _ => { + download_plugin( + &plugin_manifest.name(), + &temp_dir, + &target, + auth_header_value, + ) + .await? + } }; verify_checksum(&plugin_tarball_path, &plugin_package.sha256)?; @@ -186,6 +195,7 @@ impl PluginManager { manifest_location: &ManifestLocation, skip_compatibility_check: bool, spin_version: &str, + auth_header_value: Option, ) -> PluginLookupResult { let plugin_manifest = match manifest_location { ManifestLocation::Remote(url) => { @@ -193,7 +203,7 @@ impl PluginManager { let client = Client::new(); client .get(url.as_ref()) - .headers(maybe_get_auth_header()?) + .headers(request_headers(auth_header_value)?) .send() .await .map_err(|e| { @@ -339,12 +349,17 @@ pub fn get_package(plugin_manifest: &PluginManifest) -> Result<&PluginPackage> { }) } -async fn download_plugin(name: &str, temp_dir: &TempDir, target_url: &str) -> Result { +async fn download_plugin( + name: &str, + temp_dir: &TempDir, + target_url: &str, + auth_header_value: Option, +) -> Result { tracing::trace!("Trying to get tar file for plugin '{name}' from {target_url}"); let client = Client::new(); let plugin_bin = client .get(target_url) - .headers(maybe_get_auth_header()?) + .headers(request_headers(auth_header_value)?) .send() .await?; if !plugin_bin.status().is_success() { @@ -374,11 +389,13 @@ fn verify_checksum(plugin_file: &Path, expected_sha256: &str) -> Result<()> { } } -fn maybe_get_auth_header() -> Result { - let token = std::env::var("SPIN_PLUGIN_AUTH_HEADER").ok(); +/// Get the request headers for a call to the plugin API +/// +/// If set, this will include the user provided authorization header. +fn request_headers(auth_header_value: Option) -> Result { let mut headers = HeaderMap::new(); - if token.is_some() { - headers.insert(reqwest::header::AUTHORIZATION, token.unwrap().parse()?); + if let Some(auth_value) = auth_header_value { + headers.insert(reqwest::header::AUTHORIZATION, auth_value.parse()?); } Ok(headers) } @@ -404,6 +421,7 @@ mod tests { &ManifestLocation::Local(PathBuf::from( "../tests/nonexistent-url/nonexistent-url.json", )), + None, ) .await; diff --git a/src/commands/plugins.rs b/src/commands/plugins.rs index fc2385ecf..e6db042c2 100644 --- a/src/commands/plugins.rs +++ b/src/commands/plugins.rs @@ -98,6 +98,10 @@ pub struct Install { #[clap(long = PLUGIN_OVERRIDE_COMPATIBILITY_CHECK_FLAG, takes_value = false)] pub override_compatibility_check: bool, + /// Provide the auth header value to be able to install a plugin from a private repository. + #[clap(long = "auth-header-value", requires = PLUGIN_REMOTE_PLUGIN_MANIFEST_OPT, env = "SPIN_PLUGIN_AUTH_HEADER_VALUE")] + pub auth_header_value: Option, + /// Specific version of a plugin to be install from the centralized plugins /// repository. #[clap( @@ -126,6 +130,7 @@ impl Install { &manifest_location, self.override_compatibility_check, SPIN_VERSION, + self.auth_header_value, ) .await?; try_install( @@ -207,6 +212,10 @@ pub struct Upgrade { #[clap(short = 'y', long = "yes", takes_value = false)] pub yes_to_all: bool, + /// Provide the auth header value to be able to install a plugin from a private repository. + #[clap(long = "auth-header-value", requires = PLUGIN_REMOTE_PLUGIN_MANIFEST_OPT, env = "SPIN_PLUGIN_AUTH_HEADER_VALUE")] + pub auth_header_value: Option, + /// Overrides a failed compatibility check of the plugin with the current version of Spin. #[clap(long = PLUGIN_OVERRIDE_COMPATIBILITY_CHECK_FLAG, takes_value = false)] pub override_compatibility_check: bool, @@ -288,7 +297,12 @@ impl Upgrade { // Attempt to get the manifest to check eligibility to upgrade if let Ok(manifest) = manager - .get_manifest(&manifest_location, false, SPIN_VERSION) + .get_manifest( + &manifest_location, + false, + SPIN_VERSION, + self.auth_header_value, + ) .await { // Check if upgraded candidates have a newer version and if are compatible @@ -365,6 +379,7 @@ impl Upgrade { &manifest_location, self.override_compatibility_check, SPIN_VERSION, + self.auth_header_value, ) .await { @@ -405,6 +420,7 @@ impl Upgrade { &manifest_location, self.override_compatibility_check, SPIN_VERSION, + self.auth_header_value, ) .await?; try_install( @@ -434,6 +450,7 @@ impl Show { &ManifestLocation::PluginsRepository(PluginLookup::new(&self.name, None)), false, SPIN_VERSION, + self.auth_header_value, ) .await?; @@ -804,7 +821,9 @@ async fn try_install( let package = manager::get_package(manifest)?; if continue_to_install(manifest, package, yes_to_all)? { - let installed = manager.install(manifest, package, source).await?; + let installed = manager + .install(manifest, package, source, self.auth_header_value) + .await?; println!("Plugin '{installed}' was installed successfully!"); if let Some(description) = manifest.description() {