Skip to content
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
39 changes: 30 additions & 9 deletions influxdb3/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use influxdb3_types::http::LastCacheSize;
use influxdb3_types::http::LastCacheTtl;
use secrecy::ExposeSecret;
use secrecy::Secret;
use serde_json::json;
use std::error::Error;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::str;
use token::AdminTokenConfig;
use token::TokenCommands;
use token::TokenSubCommand;
use token::handle_token_creation;
use url::Url;

Expand Down Expand Up @@ -376,17 +378,36 @@ pub async fn command(config: Config) -> Result<(), Box<dyn Error>> {
);
}
SubCommand::Token(token_commands) => {
let output_format = match token_commands.commands {
TokenSubCommand::Admin(AdminTokenConfig { ref format, .. }) => format.clone(),
}
.unwrap_or(token::TokenOutputFormat::Text);

match handle_token_creation(client, token_commands).await {
Ok(response) => {
println!(
"\n\
Token: {token}\n\
\n\
HTTP requests require the following header: \"Authorization: Bearer {token}\"\n\
This will grant you access to HTTP/GRPC API.
",
token = response.token,
);
match output_format {
token::TokenOutputFormat::Json => {
let help_msg = format!(
"HTTP requests require the following header: \"Authorization: Bearer {}\"",
response.token
);
let json = json!({"token": response.token, "help_msg": help_msg});
let stringified = serde_json::to_string_pretty(&json)
.expect("token details to be parseable");
println!("{}", stringified);
}
token::TokenOutputFormat::Text => {
println!(
"\n\
Token: {token}\n\
\n\
HTTP requests require the following header: \"Authorization: Bearer {token}\"\n\
This will grant you access to HTTP/GRPC API.
",
token = response.token,
);
}
};
}
Err(err) => {
println!("Failed to create token, error: {:?}", err);
Expand Down
12 changes: 11 additions & 1 deletion influxdb3/src/commands/create/token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{error::Error, io, path::PathBuf};

use clap::Parser;
use clap::{Parser, ValueEnum};
use influxdb3_client::Client;
use influxdb3_types::http::CreateTokenWithPermissionsResponse;
use secrecy::Secret;
Expand Down Expand Up @@ -40,6 +40,16 @@ pub struct AdminTokenConfig {
/// An optional arg to use a custom ca for useful for testing with self signed certs
#[clap(long = "tls-ca", env = "INFLUXDB3_TLS_CA")]
pub ca_cert: Option<PathBuf>,

/// Output format for token, supports just json or text
#[clap(long)]
pub format: Option<TokenOutputFormat>,
}

#[derive(Debug, ValueEnum, Clone)]
pub enum TokenOutputFormat {
Json,
Text,
}

pub(crate) async fn handle_token_creation(
Expand Down
40 changes: 40 additions & 0 deletions influxdb3/tests/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,46 @@ async fn test_create_admin_token() {
assert_contains!(&result, "This will grant you access to HTTP/GRPC API");
}

#[test_log::test(tokio::test)]
async fn test_create_admin_token_json_format() {
let server = TestServer::configure()
.with_auth()
.with_no_admin_token()
.spawn()
.await;
let args = &[
"--tls-ca",
"../testing-certs/rootCA.pem",
"--format",
"json",
];
let result = server
.run(vec!["create", "token", "--admin"], args)
.unwrap();

let value: Value =
serde_json::from_str(&result).expect("token creation response should be in json format");
let token = value
.get("token")
.expect("token to be present")
.as_str()
.expect("token to be a str");
// check if the token generated works by using it to regenerate
let result = server
.run_with_confirmation(
vec!["create", "token", "--admin"],
&[
"--regenerate",
"--tls-ca",
"../testing-certs/rootCA.pem",
"--token",
token,
],
)
.unwrap();
assert_contains!(&result, "This will grant you access to HTTP/GRPC API");
}

#[test_log::test(tokio::test)]
async fn test_create_admin_token_allowed_once() {
let server = TestServer::configure()
Expand Down