Skip to content

Commit 0904173

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

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
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/external.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ fn installer_for(plugin_name: &str) -> Install {
209209
remote_manifest_src: None,
210210
override_compatibility_check: false,
211211
version: None,
212+
auth_header_value: None,
212213
}
213214
}
214215

src/commands/plugins.rs

Lines changed: 35 additions & 3 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.clone(),
129134
)
130135
.await?;
131136
try_install(
@@ -135,6 +140,7 @@ impl Install {
135140
self.override_compatibility_check,
136141
downgrade,
137142
&manifest_location,
143+
self.auth_header_value.clone(),
138144
)
139145
.await?;
140146
Ok(())
@@ -207,6 +213,10 @@ pub struct Upgrade {
207213
#[clap(short = 'y', long = "yes", takes_value = false)]
208214
pub yes_to_all: bool,
209215

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

289299
// Attempt to get the manifest to check eligibility to upgrade
290300
if let Ok(manifest) = manager
291-
.get_manifest(&manifest_location, false, SPIN_VERSION)
301+
.get_manifest(
302+
&manifest_location,
303+
false,
304+
SPIN_VERSION,
305+
self.auth_header_value.clone(),
306+
)
292307
.await
293308
{
294309
// Check if upgraded candidates have a newer version and if are compatible
@@ -341,7 +356,16 @@ impl Upgrade {
341356
None,
342357
));
343358

344-
try_install(&manifest, &manager, true, false, false, &manifest_location).await?;
359+
try_install(
360+
&manifest,
361+
&manager,
362+
true,
363+
false,
364+
false,
365+
&manifest_location,
366+
self.auth_header_value.clone(),
367+
)
368+
.await?;
345369
}
346370

347371
Ok(())
@@ -365,6 +389,7 @@ impl Upgrade {
365389
&manifest_location,
366390
self.override_compatibility_check,
367391
SPIN_VERSION,
392+
self.auth_header_value.clone(),
368393
)
369394
.await
370395
{
@@ -382,6 +407,7 @@ impl Upgrade {
382407
self.override_compatibility_check,
383408
self.downgrade,
384409
&manifest_location,
410+
self.auth_header_value.clone(),
385411
)
386412
.await?;
387413
}
@@ -405,6 +431,7 @@ impl Upgrade {
405431
&manifest_location,
406432
self.override_compatibility_check,
407433
SPIN_VERSION,
434+
self.auth_header_value.clone(),
408435
)
409436
.await?;
410437
try_install(
@@ -414,6 +441,7 @@ impl Upgrade {
414441
self.override_compatibility_check,
415442
self.downgrade,
416443
&manifest_location,
444+
self.auth_header_value,
417445
)
418446
.await?;
419447
Ok(())
@@ -434,6 +462,7 @@ impl Show {
434462
&ManifestLocation::PluginsRepository(PluginLookup::new(&self.name, None)),
435463
false,
436464
SPIN_VERSION,
465+
None,
437466
)
438467
.await?;
439468

@@ -789,6 +818,7 @@ async fn try_install(
789818
override_compatibility_check: bool,
790819
downgrade: bool,
791820
source: &ManifestLocation,
821+
auth_header_value: Option<String>,
792822
) -> Result<bool> {
793823
let install_action = manager.check_manifest(
794824
manifest,
@@ -804,7 +834,9 @@ async fn try_install(
804834

805835
let package = manager::get_package(manifest)?;
806836
if continue_to_install(manifest, package, yes_to_all)? {
807-
let installed = manager.install(manifest, package, source).await?;
837+
let installed = manager
838+
.install(manifest, package, source, auth_header_value)
839+
.await?;
808840
println!("Plugin '{installed}' was installed successfully!");
809841

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

0 commit comments

Comments
 (0)