Skip to content

Commit

Permalink
fix: GCS channels and add test (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Dec 5, 2024
1 parent a8082a6 commit c6a3690
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust-compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ jobs:

- name: Run tests
if: ${{ !matrix.skip-tests }}
env:
GOOGLE_CLOUD_TEST_KEY_JSON: ${{ secrets.GOOGLE_CLOUD_TEST_KEY_JSON }}
run: >
cargo nextest run
--workspace
Expand Down
58 changes: 48 additions & 10 deletions crates/rattler_networking/src/gcs_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,14 @@ impl Middleware for GCSMiddleware {

/// Auth to GCS
async fn authenticate_with_google_cloud(mut req: Request) -> MiddlewareResult<Request> {
let audience = "https://storage.googleapis.com/";
let scopes = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/devstorage.read_only",
];
let config = Config::default()
.with_audience(audience)
.with_scopes(&scopes);
let scopes = ["https://www.googleapis.com/auth/devstorage.read_only"];

let config = Config::default().with_scopes(&scopes);

match DefaultTokenSourceProvider::new(config).await {
Ok(provider) => match provider.token_source().token().await {
Ok(token) => {
let bearer_auth = format!("Bearer {token}");
let header_value = reqwest::header::HeaderValue::from_str(&bearer_auth)
let header_value = reqwest::header::HeaderValue::from_str(&token)
.map_err(reqwest_middleware::Error::middleware)?;
req.headers_mut()
.insert(reqwest::header::AUTHORIZATION, header_value);
Expand All @@ -63,3 +57,47 @@ async fn authenticate_with_google_cloud(mut req: Request) -> MiddlewareResult<Re
Err(e) => Err(reqwest_middleware::Error::Middleware(anyhow::Error::new(e))),
}
}

#[cfg(test)]
mod tests {
use super::*;
use reqwest::Client;
use tempfile;

#[tokio::test]
async fn test_gcs_middleware() {
let credentials = match std::env::var("GOOGLE_CLOUD_TEST_KEY_JSON") {
Ok(credentials) if !credentials.is_empty() => credentials,
Err(_) | Ok(_) => {
eprintln!("Skipping test as GOOGLE_CLOUD_TEST_KEY_JSON is not set");
return;
}
};
println!("Running GCS Test");

// We have to set GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON key file
let key_file = tempfile::NamedTempFile::with_suffix(".json").unwrap();
std::fs::write(&key_file, credentials).unwrap();

let prev_value = std::env::var("GOOGLE_APPLICATION_CREDENTIALS").ok();
std::env::set_var("GOOGLE_APPLICATION_CREDENTIALS", key_file.path());

let client = reqwest_middleware::ClientBuilder::new(Client::new())
.with(GCSMiddleware)
.build();

let url = "gcs://test-channel/noarch/repodata.json";
let response = client.get(url).send().await.unwrap();
assert!(response.status().is_success());

let url = "gcs://test-channel-nonexist/noarch/repodata.json";
let response = client.get(url).send().await.unwrap();
assert!(response.status().is_client_error());

if let Some(value) = prev_value {
std::env::set_var("GOOGLE_APPLICATION_CREDENTIALS", value);
} else {
std::env::remove_var("GOOGLE_APPLICATION_CREDENTIALS");
}
}
}

0 comments on commit c6a3690

Please sign in to comment.