Skip to content

Commit 8be89c6

Browse files
fix(api): Fix panic that occurred when sentry-cli login called with --auth-token (#1893)
This PR fixes the bug in #1885 that led us to revert that commit. The bug, reported in #1888, caused the CLI to panic when the sentry-cli login command was called with the --auth-token argument. We determined that the CLI panicked because the auth_token was being downcast to a String instead of to an AuthToken type when being read from the matches within the login command. This PR corrects the login command, so that the auth_token is correctly obtained from the matches as an AuthToken. We also checked whether the auth_token argument was being read as a String anywhere else in the code, and found that the login command was the only place the argument was being read as a String. Therefore, we would expect this PR to fix this class of bug completely. Fixes GH-1859 Fixes GH-1888
1 parent 5957802 commit 8be89c6

23 files changed

+645
-86
lines changed

Cargo.lock

Lines changed: 162 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ prettytable-rs = "0.10.0"
5454
proguard = { version = "5.0.0", features = ["uuid"] }
5555
r2d2 = "0.8.10"
5656
rayon = "1.6.1"
57-
regex = "1.7.1"
57+
regex = "1.7.3"
5858
runas = "1.0.0"
5959
rust-ini = "0.18.0"
6060
semver = "1.0.16"
@@ -83,7 +83,9 @@ chrono-tz = "0.8.4"
8383
insta = { version = "1.26.0", features = ["redactions", "yaml"] }
8484
mockito = "0.31.1"
8585
predicates = "2.1.5"
86+
rstest = "0.18.2"
8687
tempfile = "3.8.1"
88+
testing_logger = "0.1.1"
8789
trycmd = "0.14.11"
8890

8991
[features]

src/commands/login.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use url::Url;
66

77
use crate::api::Api;
88
use crate::config::{Auth, Config};
9+
use crate::utils::auth_token::AuthToken;
910
use crate::utils::ui::{prompt, prompt_to_continue};
1011

1112
pub fn make_command(command: Command) -> Command {
@@ -18,9 +19,9 @@ pub fn make_command(command: Command) -> Command {
1819
)
1920
}
2021

21-
fn update_config(config: &Config, token: &str) -> Result<()> {
22+
fn update_config(config: &Config, token: AuthToken) -> Result<()> {
2223
let mut new_cfg = config.clone();
23-
new_cfg.set_auth(Auth::Token(token.to_string()))?;
24+
new_cfg.set_auth(Auth::Token(token))?;
2425
new_cfg.save()?;
2526
Ok(())
2627
}
@@ -31,7 +32,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
3132
"{}/orgredirect/organizations/:orgslug/settings/auth-tokens/",
3233
config.get_base_url()?
3334
);
34-
let predefined_token = matches.get_one::<String>("auth_token");
35+
let predefined_token = matches.get_one::<AuthToken>("auth_token");
3536
let has_predefined_token = predefined_token.is_some();
3637

3738
println!("This helps you signing in your sentry-cli with an authentication token.");
@@ -62,13 +63,13 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
6263
let mut token;
6364
loop {
6465
token = if let Some(token) = predefined_token {
65-
token.to_string()
66+
token.to_owned()
6667
} else {
67-
prompt("Enter your token")?
68+
prompt("Enter your token")?.into()
6869
};
6970

7071
let test_cfg = config.make_copy(|cfg| {
71-
cfg.set_auth(Auth::Token(token.to_string()))?;
72+
cfg.set_auth(Auth::Token(token.clone()))?;
7273
Ok(())
7374
})?;
7475

@@ -103,7 +104,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
103104
Config::from_cli_config()?
104105
};
105106

106-
update_config(&config_to_update, &token)?;
107+
update_config(&config_to_update, token)?;
107108
println!();
108109
println!(
109110
"Stored token in {}",

src/commands/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use log::{debug, info, set_logger, set_max_level, LevelFilter};
1212
use crate::api::Api;
1313
use crate::config::{Auth, Config};
1414
use crate::constants::{ARCH, PLATFORM, VERSION};
15+
use crate::utils::auth_token::AuthToken;
1516
use crate::utils::logging::set_quiet_mode;
1617
use crate::utils::logging::Logger;
1718
use crate::utils::system::{init_backtrace, load_dotenv, print_error, QuietExit};
@@ -107,7 +108,7 @@ fn configure_args(config: &mut Config, matches: &ArgMatches) -> Result<()> {
107108
config.set_auth(Auth::Key(api_key.to_owned()))?;
108109
}
109110

110-
if let Some(auth_token) = matches.get_one::<String>("auth_token") {
111+
if let Some(auth_token) = matches.get_one::<AuthToken>("auth_token") {
111112
config.set_auth(Auth::Token(auth_token.to_owned()))?;
112113
}
113114

@@ -161,6 +162,7 @@ fn app() -> Command {
161162
.value_name("AUTH_TOKEN")
162163
.long("auth-token")
163164
.global(true)
165+
.value_parser(value_parser!(AuthToken))
164166
.help("Use the given Sentry auth token."),
165167
)
166168
.arg(

0 commit comments

Comments
 (0)