From 8098f1dd28731e624d216a7c46417307dd8272d9 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Thu, 1 Feb 2024 11:00:09 +0100 Subject: [PATCH] fix: netrc parsing into BasicAuth (#506) --- .../src/authentication_middleware.rs | 1 - .../authentication_storage/backends/netrc.rs | 20 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/crates/rattler_networking/src/authentication_middleware.rs b/crates/rattler_networking/src/authentication_middleware.rs index 748d4a3ab..a60514c19 100644 --- a/crates/rattler_networking/src/authentication_middleware.rs +++ b/crates/rattler_networking/src/authentication_middleware.rs @@ -98,7 +98,6 @@ impl AuthenticationMiddleware { let mut header_value = reqwest::header::HeaderValue::from_str(&basic_auth) .expect("base64 can always be converted to a header value"); header_value.set_sensitive(true); - req.headers_mut() .insert(reqwest::header::AUTHORIZATION, header_value); Ok(req) diff --git a/crates/rattler_networking/src/authentication_storage/backends/netrc.rs b/crates/rattler_networking/src/authentication_storage/backends/netrc.rs index 264a83a3f..95bad509c 100644 --- a/crates/rattler_networking/src/authentication_storage/backends/netrc.rs +++ b/crates/rattler_networking/src/authentication_storage/backends/netrc.rs @@ -70,9 +70,12 @@ impl NetRcStorage { } /// Retrieve the authentication information for the given host - pub fn get_password(&self, host: &str) -> Result, NetRcStorageError> { + pub fn get_password(&self, host: &str) -> Result, NetRcStorageError> { match self.machines.get(host) { - Some(machine) => Ok(machine.password.clone()), + Some(machine) => Ok(Some(Authentication::BasicHTTP { + username: machine.login.clone().unwrap_or_default(), + password: machine.password.clone().unwrap_or_default(), + })), None => Ok(None), } } @@ -89,10 +92,7 @@ impl StorageBackend for NetRcStorage { fn get(&self, host: &str) -> anyhow::Result> { match self.get_password(host) { - Ok(Some(password)) => Ok(Some(Authentication::BasicHTTP { - username: host.to_string(), - password, - })), + Ok(Some(auth)) => Ok(Some(auth)), Ok(None) => Ok(None), Err(err) => Err(anyhow::Error::new(err)), } @@ -112,13 +112,13 @@ mod tests { let mut netrc = std::fs::File::create(&path).unwrap(); netrc - .write_all(b"machine test\nlogin test\npassword password\n") + .write_all(b"machine mainmachine\nlogin test\npassword password\n") .unwrap(); netrc.flush().unwrap(); let storage = NetRcStorage::from_path(path.as_path()).unwrap(); assert_eq!( - storage.get("test").unwrap(), + storage.get("mainmachine").unwrap(), Some(Authentication::BasicHTTP { username: "test".to_string(), password: "password".to_string(), @@ -135,7 +135,7 @@ mod tests { let mut netrc = std::fs::File::create(&path).unwrap(); netrc - .write_all(b"machine test2\nlogin test2\npassword password2\n") + .write_all(b"machine supermachine\nlogin test2\npassword password2\n") .unwrap(); netrc.flush().unwrap(); @@ -145,7 +145,7 @@ mod tests { let storage = NetRcStorage::from_env().unwrap(); assert_eq!( - storage.get("test2").unwrap(), + storage.get("supermachine").unwrap(), Some(Authentication::BasicHTTP { username: "test2".to_string(), password: "password2".to_string(),