Skip to content

Commit

Permalink
update to use cli flag along with env var
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <[email protected]>
  • Loading branch information
karthik2804 committed Oct 28, 2024
1 parent 59d9052 commit 8d0bd82
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
34 changes: 26 additions & 8 deletions crates/plugins/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl PluginManager {
plugin_manifest: &PluginManifest,
plugin_package: &PluginPackage,
source: &ManifestLocation,
auth_header_value: Option<String>,
) -> Result<String> {
let target = plugin_package.url.to_owned();
let target_url = Url::parse(&target)?;
Expand All @@ -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)?;

Expand Down Expand Up @@ -186,14 +195,15 @@ impl PluginManager {
manifest_location: &ManifestLocation,
skip_compatibility_check: bool,
spin_version: &str,
auth_header_value: Option<String>,
) -> PluginLookupResult<PluginManifest> {
let plugin_manifest = match manifest_location {
ManifestLocation::Remote(url) => {
tracing::info!("Pulling manifest for plugin from {url}");
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| {
Expand Down Expand Up @@ -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<PathBuf> {
async fn download_plugin(
name: &str,
temp_dir: &TempDir,
target_url: &str,
auth_header_value: Option<String>,
) -> Result<PathBuf> {
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() {
Expand Down Expand Up @@ -374,11 +389,13 @@ fn verify_checksum(plugin_file: &Path, expected_sha256: &str) -> Result<()> {
}
}

fn maybe_get_auth_header() -> Result<HeaderMap> {
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<String>) -> Result<HeaderMap> {
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)
}
Expand All @@ -404,6 +421,7 @@ mod tests {
&ManifestLocation::Local(PathBuf::from(
"../tests/nonexistent-url/nonexistent-url.json",
)),
None,
)
.await;

Expand Down
23 changes: 21 additions & 2 deletions src/commands/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Specific version of a plugin to be install from the centralized plugins
/// repository.
#[clap(
Expand Down Expand Up @@ -126,6 +130,7 @@ impl Install {
&manifest_location,
self.override_compatibility_check,
SPIN_VERSION,
self.auth_header_value,
)
.await?;
try_install(
Expand Down Expand Up @@ -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<String>,

/// 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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -365,6 +379,7 @@ impl Upgrade {
&manifest_location,
self.override_compatibility_check,
SPIN_VERSION,
self.auth_header_value,
)
.await
{
Expand Down Expand Up @@ -405,6 +420,7 @@ impl Upgrade {
&manifest_location,
self.override_compatibility_check,
SPIN_VERSION,
self.auth_header_value,
)
.await?;
try_install(
Expand Down Expand Up @@ -434,6 +450,7 @@ impl Show {
&ManifestLocation::PluginsRepository(PluginLookup::new(&self.name, None)),
false,
SPIN_VERSION,
self.auth_header_value,
)
.await?;

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 8d0bd82

Please sign in to comment.