Skip to content

Commit 59d9052

Browse files
committed
add ability for plugins to be fetched from authenticated URLs
Signed-off-by: karthik2804 <[email protected]>
1 parent 3eaba5f commit 59d9052

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

crates/plugins/src/manager.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88

99
use anyhow::{anyhow, bail, Context, Result};
1010
use path_absolutize::Absolutize;
11+
use reqwest::{header::HeaderMap, Client};
1112
use serde::Serialize;
1213
use spin_common::sha256;
1314
use std::{
@@ -189,7 +190,11 @@ impl PluginManager {
189190
let plugin_manifest = match manifest_location {
190191
ManifestLocation::Remote(url) => {
191192
tracing::info!("Pulling manifest for plugin from {url}");
192-
reqwest::get(url.as_ref())
193+
let client = Client::new();
194+
client
195+
.get(url.as_ref())
196+
.headers(maybe_get_auth_header()?)
197+
.send()
193198
.await
194199
.map_err(|e| {
195200
Error::ConnectionFailed(ConnectionFailedError::new(
@@ -336,7 +341,12 @@ pub fn get_package(plugin_manifest: &PluginManifest) -> Result<&PluginPackage> {
336341

337342
async fn download_plugin(name: &str, temp_dir: &TempDir, target_url: &str) -> Result<PathBuf> {
338343
tracing::trace!("Trying to get tar file for plugin '{name}' from {target_url}");
339-
let plugin_bin = reqwest::get(target_url).await?;
344+
let client = Client::new();
345+
let plugin_bin = client
346+
.get(target_url)
347+
.headers(maybe_get_auth_header()?)
348+
.send()
349+
.await?;
340350
if !plugin_bin.status().is_success() {
341351
match plugin_bin.status() {
342352
reqwest::StatusCode::NOT_FOUND => bail!("The download URL specified in the plugin manifest was not found ({target_url} returned HTTP error 404). Please contact the plugin author."),
@@ -364,6 +374,15 @@ fn verify_checksum(plugin_file: &Path, expected_sha256: &str) -> Result<()> {
364374
}
365375
}
366376

377+
fn maybe_get_auth_header() -> Result<HeaderMap> {
378+
let token = std::env::var("SPIN_PLUGIN_AUTH_HEADER").ok();
379+
let mut headers = HeaderMap::new();
380+
if token.is_some() {
381+
headers.insert(reqwest::header::AUTHORIZATION, token.unwrap().parse()?);
382+
}
383+
Ok(headers)
384+
}
385+
367386
#[cfg(test)]
368387
mod tests {
369388
use super::*;

0 commit comments

Comments
 (0)