Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(token): Separate validation warning from parsing #2113

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ mockito = "0.31.1"
predicates = "2.1.5"
rstest = "0.18.2"
tempfile = "3.8.1"
testing_logger = "0.1.1"
trycmd = "0.14.11"

[features]
Expand Down
6 changes: 3 additions & 3 deletions src/commands/derive_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::auth_token::AuthToken;
use crate::utils::value_parsers::kv_parser;
use clap::{command, value_parser, ArgAction::SetTrue, Parser, Subcommand};
use crate::utils::value_parsers::{auth_token_parser, kv_parser};
use clap::{command, ArgAction::SetTrue, Parser, Subcommand};

use super::send_metric::SendMetricArgs;

Expand All @@ -13,7 +13,7 @@ pub(super) struct SentryCLI {
#[arg(help = "Custom headers that should be attached to all requests{n}in key:value format")]
pub(super) headers: Vec<(String, String)>,

#[arg(global=true, long, value_parser=value_parser!(AuthToken))]
#[arg(global=true, long, value_parser=auth_token_parser)]
#[arg(help = "Use the given Sentry auth token")]
pub(super) auth_token: Option<AuthToken>,

Expand Down
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::utils::logging::set_quiet_mode;
use crate::utils::logging::Logger;
use crate::utils::system::{init_backtrace, load_dotenv, print_error, QuietExit};
use crate::utils::update::run_sentrycli_update_nagger;
use crate::utils::value_parsers::auth_token_parser;

mod derive_parser;

Expand Down Expand Up @@ -165,7 +166,7 @@ fn app() -> Command {
.value_name("AUTH_TOKEN")
.long("auth-token")
.global(true)
.value_parser(value_parser!(AuthToken))
.value_parser(auth_token_parser)
.help("Use the given Sentry auth token."),
)
.arg(
Expand Down
9 changes: 6 additions & 3 deletions src/utils/auth_token/auth_token_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ impl AuthToken {
fn as_str(&self) -> &str {
self.0.as_str()
}

/// Returns whether the auth token follows a recognized format. If this function returns false,
/// that indicates that the auth token might not be valid, since it failed our soft validation.
pub fn format_recognized(&self) -> bool {
!matches!(self.0, AuthTokenInner::Unknown(_))
}
}

impl From<String> for AuthToken {
Expand Down Expand Up @@ -72,9 +78,6 @@ impl AuthTokenInner {
} else if let Ok(user_auth_token) = UserAuthToken::try_from(auth_string.clone()) {
AuthTokenInner::User(user_auth_token)
} else {
log::warn!(
"Unrecognized auth token format!\n\tHint: Did you copy your token correctly?"
);
AuthTokenInner::Unknown(auth_string)
}
}
Expand Down
26 changes: 4 additions & 22 deletions src/utils/auth_token/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@

use super::AuthToken;
use rstest::rstest;
use testing_logger::CapturedLog;

/// Asserts that the logs vector is empty.
#[allow(clippy::ptr_arg)] // This function signature is required by testing_logger
fn assert_no_logs(logs: &Vec<CapturedLog>) {
assert!(logs.is_empty());
}

/// Asserts that the logs vector contains exactly one warning.
#[allow(clippy::ptr_arg)] // This function signature is required by testing_logger
fn assert_one_warning(logs: &Vec<CapturedLog>) {
assert_eq!(logs.len(), 1);
assert_eq!(logs[0].level, log::Level::Warn);
}

// Org auth token tests -----------------------------------------------------

Expand All @@ -28,7 +14,6 @@ fn test_valid_org_auth_token() {
lQ5ETt61cHhvJa35fxvxARsDXeVrd0pu4/smF4sRieA",
);

testing_logger::setup();
let token = AuthToken::from(good_token.clone());

assert!(token.payload().is_some());
Expand All @@ -39,7 +24,7 @@ fn test_valid_org_auth_token() {

assert_eq!(good_token, token.to_string());

testing_logger::validate(assert_no_logs);
assert!(token.format_recognized());
}

#[test]
Expand All @@ -51,7 +36,6 @@ fn test_valid_org_auth_token_missing_url() {
lQ5ETt61cHhvJa35fxvxARsDXeVrd0pu4/smF4sRieA",
);

testing_logger::setup();
let token = AuthToken::from(good_token.clone());

assert!(token.payload().is_some());
Expand All @@ -62,7 +46,7 @@ fn test_valid_org_auth_token_missing_url() {

assert_eq!(good_token, token.to_string());

testing_logger::validate(assert_no_logs);
assert!(token.format_recognized());
}

// User auth token tests ----------------------------------------------------
Expand All @@ -73,13 +57,12 @@ fn test_valid_org_auth_token_missing_url() {
fn test_valid_user_auth_token(#[case] token_str: &'static str) {
let good_token = String::from(token_str);

testing_logger::setup();
let token = AuthToken::from(good_token.clone());

assert!(token.payload().is_none());
assert_eq!(good_token, token.to_string());

testing_logger::validate(assert_no_logs);
assert!(token.format_recognized());
}

// Unknown auth token tests -------------------------------------------------
Expand Down Expand Up @@ -145,11 +128,10 @@ fn test_valid_user_auth_token(#[case] token_str: &'static str) {
)]
#[case::wrong_prefix("sntryt_c66aee1348a6e7a0993145d71cf8fa529ed09ee13dd5177b5f692e9f6ca38c30")]
fn test_unknown_auth_token(#[case] token_str: &'static str) {
testing_logger::setup();
let token = AuthToken::from(token_str.to_owned());

assert_eq!(token_str, token.to_string());
assert!(token.payload().is_none());

testing_logger::validate(assert_one_warning);
assert!(!token.format_recognized());
}
12 changes: 12 additions & 0 deletions src/utils/value_parsers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
use crate::utils::auth_token::AuthToken;
use anyhow::{anyhow, Result};
use std::convert::Infallible;

/// Parse key:value pair from string, used as a value_parser for Clap arguments
pub fn kv_parser(s: &str) -> Result<(String, String)> {
s.split_once(':')
.map(|(k, v)| (k.into(), v.into()))
.ok_or_else(|| anyhow!("`{s}` is missing a `:`"))
}

/// Parse an AuthToken, and warn if the format is unrecognized
pub fn auth_token_parser(s: &str) -> Result<AuthToken, Infallible> {
let token = AuthToken::from(s);
if !token.format_recognized() {
log::warn!("Unrecognized auth token format. Ensure you copied your token correctly.");
}

Ok(token)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
```
$ sentry-cli --auth-token not_a_valid_auth_token --version
? success
[..]WARN[..]Unrecognized auth token format!
Hint: Did you copy your token correctly?
sentry-cli [..]
[..]WARN[..] Unrecognized auth token format. Ensure you copied your token correctly.
...

```
```
Loading