use itertools::Itertools;
use url::Url;
pub const DEFAULT_REDACTION_STR: &str = "********";
pub fn redact_known_secrets_from_url(url: &Url, redaction: &str) -> Option<Url> {
let mut segments = url.path_segments()?;
match (segments.next(), segments.next()) {
(Some("t"), Some(_)) => {
let remainder = segments.collect_vec();
let redacted_path = format!(
"t/{redaction}{seperator}{remainder}",
seperator = if remainder.is_empty() { "" } else { "/" },
remainder = remainder.iter().format("/")
);
let mut url = url.clone();
url.set_path(&redacted_path);
Some(url)
}
_ => None,
}
}
pub fn redact_known_secrets_from_error(err: reqwest::Error) -> reqwest::Error {
if let Some(url) = err.url() {
let redacted_url = redact_known_secrets_from_url(url, DEFAULT_REDACTION_STR)
.unwrap_or_else(|| url.clone());
err.with_url(redacted_url)
} else {
err
}
}
#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;
#[test]
fn test_remove_known_secrets_from_url() {
assert_eq!(
redact_known_secrets_from_url(
&Url::from_str(
"https://conda.anaconda.org/t/12345677/conda-forge/noarch/repodata.json"
)
.unwrap(),
DEFAULT_REDACTION_STR
),
Some(
Url::from_str(
&format!("https://conda.anaconda.org/t/{DEFAULT_REDACTION_STR}/conda-forge/noarch/repodata.json")
)
.unwrap()
)
);
assert_eq!(
redact_known_secrets_from_url(
&Url::from_str("https://conda.anaconda.org/conda-forge/noarch/repodata.json")
.unwrap(),
"helloworld"
),
None,
);
}
}