Skip to content

Commit

Permalink
fix: Only warn about mismatched URLs when they are different (#2143)
Browse files Browse the repository at this point in the history
Previously, we warned about a mismatch between the auth token URL and the manually configured URL even when they were the same. We also sometimes issued the warning when no URL was manually configured (i.e. when the default https://sentry.io/ was used).

Now, we only warn when the two are actually different and we also only warn if a URL was actually manually configured.

Fixes #2137
  • Loading branch information
szokeasaurusrex authored Sep 5, 2024
1 parent b790f2d commit 42e456e
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,16 @@ impl Config {
_ => None, // get_default_auth never returns Auth::Token variant
};

let default_url = get_default_url(&ini);
let manually_configured_url = configured_url(&ini);
let token_url = token_embedded_data
.as_ref()
.map(|td| td.url.as_str())
.unwrap_or_default();

let url = if token_url.is_empty() {
default_url
manually_configured_url.unwrap_or_else(|| DEFAULT_URL.to_string())
} else {
if !default_url.is_empty() {
log::warn!(
"Using {token_url} (embedded in token) rather than manually-configured URL {default_url}. \
To use {default_url}, please provide an auth token for this URL."
);
}
warn_about_conflicting_urls(token_url, manually_configured_url.as_deref());
token_url.into()
};

Expand Down Expand Up @@ -535,6 +530,18 @@ impl Config {
}
}

fn warn_about_conflicting_urls(token_url: &str, manually_configured_url: Option<&str>) {
if let Some(manually_configured_url) = manually_configured_url {
if manually_configured_url != token_url {
warn!(
"Using {token_url} (embedded in token) rather than manually-configured URL \
{manually_configured_url}. To use {manually_configured_url}, please provide an \
auth token for {manually_configured_url}."
);
}
}
}

fn find_global_config_file() -> Result<PathBuf> {
let home_dir_file = dirs::home_dir().map(|p| p.join(CONFIG_RC_FILE_NAME));
let config_dir_file = dirs::config_dir().map(|p| p.join(CONFIG_INI_FILE_PATH));
Expand Down Expand Up @@ -694,14 +701,13 @@ fn get_default_auth(ini: &Ini) -> Option<Auth> {
}
}

fn get_default_url(ini: &Ini) -> String {
if let Ok(val) = env::var("SENTRY_URL") {
val
} else if let Some(val) = ini.get_from(Some("defaults"), "url") {
val.to_owned()
} else {
DEFAULT_URL.to_owned()
}
/// Returns the URL configured in the SENTRY_URL environment variable or provided ini (in that
/// order of precedence), or returns None if neither is set.
fn configured_url(ini: &Ini) -> Option<String> {
env::var("SENTRY_URL").ok().or_else(|| {
ini.get_from(Some("defaults"), "url")
.map(|url| url.to_owned())
})
}

fn get_default_headers(ini: &Ini) -> Option<Vec<String>> {
Expand Down

0 comments on commit 42e456e

Please sign in to comment.