Log netrc parsing error

This commit is contained in:
j178 2024-10-19 21:47:09 +08:00
parent 4b0a4dadb7
commit cfca400c49
No known key found for this signature in database

View file

@ -12,14 +12,21 @@ use anyhow::{anyhow, format_err};
use netrc::Netrc; use netrc::Netrc;
use reqwest::{Request, Response}; use reqwest::{Request, Response};
use reqwest_middleware::{Error, Middleware, Next}; use reqwest_middleware::{Error, Middleware, Next};
use tracing::{debug, trace}; use tracing::{debug, trace, warn};
/// Strategy for loading netrc files.
enum NetrcMode {
Automatic,
Enabled(netrc::Netrc),
Disabled,
}
/// A middleware that adds basic authentication to requests. /// A middleware that adds basic authentication to requests.
/// ///
/// Uses a cache to propagate credentials from previously seen requests and /// Uses a cache to propagate credentials from previously seen requests and
/// fetches credentials from a netrc file and the keyring. /// fetches credentials from a netrc file and the keyring.
pub struct AuthMiddleware { pub struct AuthMiddleware {
netrc: Option<Netrc>, netrc: NetrcMode,
keyring: Option<KeyringProvider>, keyring: Option<KeyringProvider>,
cache: Option<CredentialsCache>, cache: Option<CredentialsCache>,
/// We know that the endpoint needs authentication, so we don't try to send an unauthenticated /// We know that the endpoint needs authentication, so we don't try to send an unauthenticated
@ -30,19 +37,23 @@ pub struct AuthMiddleware {
impl AuthMiddleware { impl AuthMiddleware {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
netrc: Netrc::new().ok(), netrc: NetrcMode::Automatic,
keyring: None, keyring: None,
cache: None, cache: None,
only_authenticated: false, only_authenticated: false,
} }
} }
/// Configure the [`Netrc`] credential file to use. /// Configure the [`netrc::Netrc`] credential file to use.
/// ///
/// `None` disables authentication via netrc. /// `None` disables authentication via netrc.
#[must_use] #[must_use]
pub fn with_netrc(mut self, netrc: Option<Netrc>) -> Self { pub fn with_netrc(mut self, netrc: Option<netrc::Netrc>) -> Self {
self.netrc = netrc; self.netrc = if let Some(netrc) = netrc {
NetrcMode::Enabled(netrc)
} else {
NetrcMode::Disabled
};
self self
} }
@ -361,16 +372,39 @@ impl AuthMiddleware {
} }
// Netrc support based on: <https://github.com/gribouille/netrc>. // Netrc support based on: <https://github.com/gribouille/netrc>.
let credentials = if let Some(credentials) = self.netrc.as_ref().and_then(|netrc| { let credentials = if let Some(credentials) = match self.netrc {
debug!("Checking netrc for credentials for {url}"); NetrcMode::Enabled(ref netrc) => {
Credentials::from_netrc( debug!("Checking netrc for credentials for {url}");
netrc, Credentials::from_netrc(
url, netrc,
credentials url,
.as_ref() credentials
.and_then(|credentials| credentials.username()), .as_ref()
) .and_then(|credentials| credentials.username()),
}) { )
}
NetrcMode::Automatic => match Netrc::new() {
Ok(netrc) => {
debug!("Checking netrc for credentials for {url}");
Credentials::from_netrc(
&netrc,
url,
credentials
.as_ref()
.and_then(|credentials| credentials.username()),
)
}
Err(netrc::Error::Io(err)) if err.kind() == std::io::ErrorKind::NotFound => {
debug!("No netrc file found");
None
}
Err(err) => {
warn!("Error reading netrc file: {err}");
None
}
},
NetrcMode::Disabled => None,
} {
debug!("Found credentials in netrc file for {url}"); debug!("Found credentials in netrc file for {url}");
Some(credentials) Some(credentials)
// N.B. The keyring provider performs lookups for the exact URL then // N.B. The keyring provider performs lookups for the exact URL then