Skip to content

Commit 7e4e060

Browse files
committed
update to use cli flag along with env var
Signed-off-by: karthik2804 <[email protected]>
1 parent 59d9052 commit 7e4e060

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

crates/plugins/src/manager.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl PluginManager {
8888
plugin_manifest: &PluginManifest,
8989
plugin_package: &PluginPackage,
9090
source: &ManifestLocation,
91+
auth_header_value: Option<String>,
9192
) -> Result<String> {
9293
let target = plugin_package.url.to_owned();
9394
let target_url = Url::parse(&target)?;
@@ -106,7 +107,15 @@ impl PluginManager {
106107
);
107108
}
108109
}
109-
_ => download_plugin(&plugin_manifest.name(), &temp_dir, &target).await?,
110+
_ => {
111+
download_plugin(
112+
&plugin_manifest.name(),
113+
&temp_dir,
114+
&target,
115+
auth_header_value,
116+
)
117+
.await?
118+
}
110119
};
111120
verify_checksum(&plugin_tarball_path, &plugin_package.sha256)?;
112121

@@ -186,14 +195,15 @@ impl PluginManager {
186195
manifest_location: &ManifestLocation,
187196
skip_compatibility_check: bool,
188197
spin_version: &str,
198+
auth_header_value: Option<String>,
189199
) -> PluginLookupResult<PluginManifest> {
190200
let plugin_manifest = match manifest_location {
191201
ManifestLocation::Remote(url) => {
192202
tracing::info!("Pulling manifest for plugin from {url}");
193203
let client = Client::new();
194204
client
195205
.get(url.as_ref())
196-
.headers(maybe_get_auth_header()?)
206+
.headers(request_headers(auth_header_value)?)
197207
.send()
198208
.await
199209
.map_err(|e| {
@@ -339,12 +349,17 @@ pub fn get_package(plugin_manifest: &PluginManifest) -> Result<&PluginPackage> {
339349
})
340350
}
341351

342-
async fn download_plugin(name: &str, temp_dir: &TempDir, target_url: &str) -> Result<PathBuf> {
352+
async fn download_plugin(
353+
name: &str,
354+
temp_dir: &TempDir,
355+
target_url: &str,
356+
auth_header_value: Option<String>,
357+
) -> Result<PathBuf> {
343358
tracing::trace!("Trying to get tar file for plugin '{name}' from {target_url}");
344359
let client = Client::new();
345360
let plugin_bin = client
346361
.get(target_url)
347-
.headers(maybe_get_auth_header()?)
362+
.headers(request_headers(auth_header_value)?)
348363
.send()
349364
.await?;
350365
if !plugin_bin.status().is_success() {
@@ -374,11 +389,13 @@ fn verify_checksum(plugin_file: &Path, expected_sha256: &str) -> Result<()> {
374389
}
375390
}
376391

377-
fn maybe_get_auth_header() -> Result<HeaderMap> {
378-
let token = std::env::var("SPIN_PLUGIN_AUTH_HEADER").ok();
392+
/// Get the request headers for a call to the plugin API
393+
///
394+
/// If set, this will include the user provided authorization header.
395+
fn request_headers(auth_header_value: Option<String>) -> Result<HeaderMap> {
379396
let mut headers = HeaderMap::new();
380-
if token.is_some() {
381-
headers.insert(reqwest::header::AUTHORIZATION, token.unwrap().parse()?);
397+
if let Some(auth_value) = auth_header_value {
398+
headers.insert(reqwest::header::AUTHORIZATION, auth_value.parse()?);
382399
}
383400
Ok(headers)
384401
}
@@ -404,6 +421,7 @@ mod tests {
404421
&ManifestLocation::Local(PathBuf::from(
405422
"../tests/nonexistent-url/nonexistent-url.json",
406423
)),
424+
None,
407425
)
408426
.await;
409427

src/commands/plugins.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ pub struct Install {
9898
#[clap(long = PLUGIN_OVERRIDE_COMPATIBILITY_CHECK_FLAG, takes_value = false)]
9999
pub override_compatibility_check: bool,
100100

101+
/// Provide the auth header value to be able to install a plugin from a private repository.
102+
#[clap(long = "auth-header-value", requires = PLUGIN_REMOTE_PLUGIN_MANIFEST_OPT)]
103+
pub auth_header_value: Option<String>,
104+
101105
/// Specific version of a plugin to be install from the centralized plugins
102106
/// repository.
103107
#[clap(
@@ -126,6 +130,7 @@ impl Install {
126130
&manifest_location,
127131
self.override_compatibility_check,
128132
SPIN_VERSION,
133+
self.auth_header_value,
129134
)
130135
.await?;
131136
try_install(
@@ -207,6 +212,10 @@ pub struct Upgrade {
207212
#[clap(short = 'y', long = "yes", takes_value = false)]
208213
pub yes_to_all: bool,
209214

215+
/// Auth header value to be able to install a plugin from a private repository.
216+
#[clap(long = "auth-header-value", requires = PLUGIN_REMOTE_PLUGIN_MANIFEST_OPT)]
217+
pub auth_header_value: Option<String>,
218+
210219
/// Overrides a failed compatibility check of the plugin with the current version of Spin.
211220
#[clap(long = PLUGIN_OVERRIDE_COMPATIBILITY_CHECK_FLAG, takes_value = false)]
212221
pub override_compatibility_check: bool,
@@ -288,7 +297,12 @@ impl Upgrade {
288297

289298
// Attempt to get the manifest to check eligibility to upgrade
290299
if let Ok(manifest) = manager
291-
.get_manifest(&manifest_location, false, SPIN_VERSION)
300+
.get_manifest(
301+
&manifest_location,
302+
false,
303+
SPIN_VERSION,
304+
self.auth_header_value,
305+
)
292306
.await
293307
{
294308
// Check if upgraded candidates have a newer version and if are compatible
@@ -365,6 +379,7 @@ impl Upgrade {
365379
&manifest_location,
366380
self.override_compatibility_check,
367381
SPIN_VERSION,
382+
self.auth_header_value,
368383
)
369384
.await
370385
{
@@ -405,6 +420,7 @@ impl Upgrade {
405420
&manifest_location,
406421
self.override_compatibility_check,
407422
SPIN_VERSION,
423+
self.auth_header_value,
408424
)
409425
.await?;
410426
try_install(
@@ -434,6 +450,7 @@ impl Show {
434450
&ManifestLocation::PluginsRepository(PluginLookup::new(&self.name, None)),
435451
false,
436452
SPIN_VERSION,
453+
self.auth_header_value,
437454
)
438455
.await?;
439456

@@ -804,7 +821,9 @@ async fn try_install(
804821

805822
let package = manager::get_package(manifest)?;
806823
if continue_to_install(manifest, package, yes_to_all)? {
807-
let installed = manager.install(manifest, package, source).await?;
824+
let installed = manager
825+
.install(manifest, package, source, self.auth_header_value)
826+
.await?;
808827
println!("Plugin '{installed}' was installed successfully!");
809828

810829
if let Some(description) = manifest.description() {

0 commit comments

Comments
 (0)