feat: ask for confirmation when regenerating token (#26258)

praveen/token-help
praveen-influx 2025-04-13 09:07:09 +01:00 committed by GitHub
parent 5d7cb88f87
commit 0cb5f9077c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 7 deletions

View File

@ -1,4 +1,4 @@
use std::{error::Error, path::PathBuf};
use std::{error::Error, io, path::PathBuf};
use clap::Parser;
use influxdb3_client::Client;
@ -58,10 +58,17 @@ pub(crate) async fn handle_admin_token_creation(
config: AdminTokenConfig,
) -> Result<CreateTokenWithPermissionsResponse, Box<dyn Error>> {
let json_body = if config.regenerate {
client
.api_v3_configure_regenerate_admin_token()
.await?
.expect("token creation to return full token info")
println!("Are you sure you want to regenerate admin token? Enter 'yes' to confirm",);
let mut confirmation = String::new();
let _ = io::stdin().read_line(&mut confirmation);
if confirmation.trim() == "yes" {
client
.api_v3_configure_regenerate_admin_token()
.await?
.expect("token creation to return full token info")
} else {
return Err("Cannot regenerate token without confirmation".into());
}
} else {
client
.api_v3_configure_create_admin_token()

View File

@ -2921,7 +2921,7 @@ async fn test_regenerate_admin_token() {
// regenerating token is allowed
let result = server
.run(
.run_with_confirmation(
vec!["create", "token", "--admin"],
&["--regenerate", "--tls-ca", "../testing-certs/rootCA.pem"],
)

View File

@ -578,7 +578,8 @@ pub async fn write_lp_to_db(
pub fn parse_token(result: String) -> String {
let all_lines: Vec<&str> = result.split('\n').collect();
let token = all_lines
.first()
.iter()
.find(|line| line.starts_with("Token:"))
.expect("token line to be present")
.replace("Token: ", "");
token