Skip to content

Commit

Permalink
feat: Prefer org auth token URL over manually provided URL (#2122)
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex authored Aug 19, 2024
1 parent 519d4e3 commit 335f9bc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn configure_args(config: &mut Config, matches: &ArgMatches) -> Result<()> {
}

if let Some(url) = matches.get_one::<String>("url") {
config.set_base_url(url)?;
config.set_base_url(url);
}

if let Some(headers) = matches.get_many::<String>("headers") {
Expand Down
33 changes: 19 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -211,21 +214,23 @@ 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()
.map(|td| td.url.as_str())
.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
Expand Down
6 changes: 2 additions & 4 deletions tests/integration/_cases/org_tokens/url-mismatch.trycmd
Original file line number Diff line number Diff line change
@@ -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.
...

```

0 comments on commit 335f9bc

Please sign in to comment.