Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: respect RATTLER_AUTH_FILE when using AuthenticationStorage::default() #636

Merged
merged 8 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ sysinfo = "0.30.10"
tar = "0.4.40"
tempdir = "0.3.7"
tempfile = "3.10.1"
temp-env = "0.3.6"
test-log = "0.2.15"
thiserror = "1.0"
tokio = { version = "1.37.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/rattler_networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ tokio = { workspace = true, features = ["macros"] }
axum = { workspace = true }
reqwest-retry = { workspace = true }
sha2 = { workspace = true }
temp-env = { workspace = true }
29 changes: 29 additions & 0 deletions crates/rattler_networking/src/authentication_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,33 @@ mod tests {

Ok(())
}

#[test]
fn test_rattler_auth_file_env_var_handling() -> anyhow::Result<()> {
let tdir = tempdir()?;

let storage = temp_env::with_var(
"RATTLER_AUTH_FILE",
Some(
tdir.path()
.to_path_buf()
.join("auth.json")
.to_str()
.unwrap(),
),
|| AuthenticationStorage::from_env().unwrap(),
);

let host = "test.example.com";
let authentication = Authentication::CondaToken("testtoken".to_string());
storage.store(host, &authentication)?;

let file = tdir.path().join("auth.json");
assert_eq!(
std::fs::read_to_string(file)?,
"{\"test.example.com\":{\"CondaToken\":\"testtoken\"}}"
);

Ok(())
}
}
29 changes: 29 additions & 0 deletions crates/rattler_networking/src/authentication_storage/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ impl AuthenticationStorage {
}
}

/// Create a new authentication storage with the default backends
/// respecting the `RATTLER_AUTH_FILE` environment variable.
/// If the variable is set, the file storage backend will be used
/// with the path specified in the variable
pub fn from_env() -> Result<Self> {
if let Ok(auth_file) = std::env::var("RATTLER_AUTH_FILE") {
let mut storage = Self::new();
let path = std::path::Path::new(&auth_file);

tracing::info!(
"\"RATTLER_AUTH_FILE\" environment variable set, using file storage at {}",
auth_file
);

let backend = FileStorage::new(path.to_path_buf()).map_err(|e| {
anyhow!(
"Error creating file storage backend for RATTLER_AUTH_FILE ({}): {}",
auth_file,
e
)
})?;

storage.add_backend(Arc::from(backend));
Ok(storage)
} else {
Ok(Self::default())
}
}

/// Add a new storage backend to the authentication storage
/// (backends are tried in the order they are added)
pub fn add_backend(&mut self, backend: Arc<dyn StorageBackend + Send + Sync>) {
Expand Down
Loading