From 785c2bff561eb9a71c421f6a0889566cc50232eb Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 6 Aug 2024 16:21:29 +0200 Subject: [PATCH] feat: Prefer org auth token URL over manually provided URL Previously, we would error when the auth token's embedded URL differed from the manually provided URL. Now, we instead always use the token URL and log a warning. --- src/commands/mod.rs | 2 +- src/config.rs | 33 +++++++++++-------- .../_cases/org_tokens/url-mismatch.trycmd | 6 ++-- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b224d3e126..c125bc4f22 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -120,7 +120,7 @@ fn configure_args(config: &mut Config, matches: &ArgMatches) -> Result<()> { } if let Some(url) = matches.get_one::("url") { - config.set_base_url(url)?; + config.set_base_url(url); } if let Some(headers) = matches.get_many::("headers") { diff --git a/src/config.rs b/src/config.rs index b128c7a71f..c70c5e21cc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -71,13 +71,16 @@ impl Config { .map(|td| td.url.as_str()) .unwrap_or_default(); - let url = match (default_url.as_str(), token_url) { - (_, "") => default_url, - _ if default_url == token_url => default_url, - (DEFAULT_URL | "", _) => String::from(token_url), - _ => bail!( - "Two different url values supplied: `{token_url}` (from token), `{default_url}`." - ), + let url = if token_url.is_empty() { + default_url + } 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." + ); + } + token_url.into() }; Ok(Config { @@ -211,7 +214,7 @@ impl Config { } /// Sets the URL - pub fn set_base_url(&mut self, url: &str) -> Result<()> { + pub fn set_base_url(&mut self, url: &str) { let token_url = self .cached_token_data .as_ref() @@ -219,13 +222,15 @@ impl Config { .unwrap_or_default(); if !token_url.is_empty() && url != token_url { - bail!("Two different url values supplied: `{token_url}` (from token), `{url}`."); + log::warn!( + "Using {token_url} (embedded in token) rather than manually-configured URL {url}. \ + To use {url}, please provide an auth token for this URL." + ); + } else { + url.clone_into(&mut self.cached_base_url); + self.ini + .set_to(Some("defaults"), "url".into(), self.cached_base_url.clone()); } - - url.clone_into(&mut self.cached_base_url); - self.ini - .set_to(Some("defaults"), "url".into(), self.cached_base_url.clone()); - Ok(()) } /// Sets headers that should be attached to all requests diff --git a/tests/integration/_cases/org_tokens/url-mismatch.trycmd b/tests/integration/_cases/org_tokens/url-mismatch.trycmd index 6009f05c9a..f77e7788a1 100644 --- a/tests/integration/_cases/org_tokens/url-mismatch.trycmd +++ b/tests/integration/_cases/org_tokens/url-mismatch.trycmd @@ -1,9 +1,7 @@ ``` $ sentry-cli --auth-token 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 --url http://example.com sourcemaps upload test_path ? failed -error: Two different url values supplied: `http://localhost:8000` (from token), `http://example.com`. - -Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output. -Please attach the full debug log to all bug reports. +[..]WARN[..] Using http://localhost:8000 (embedded in token) rather than manually-configured URL http://example.com. To use http://example.com, please provide an auth token for this URL. +... ```