Skip to content

Commit 1ec063b

Browse files
feat: support named admin tokens (#26434)
* feat: support named admin tokens - `--name` and `--expiry` are now allowed for `--admin` subcommand - `--regenerate` is restricted to operator token only - the endpoint is not allowed if auth is disabled closes: influxdata/influxdb_pro#854 This is a port of influxdata/influxdb_pro#850 (hash:156981e4a1) * refactor: address PR feedback
1 parent bf83e7f commit 1ec063b

File tree

8 files changed

+694
-199
lines changed

8 files changed

+694
-199
lines changed

influxdb3/src/commands/create/token.rs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use std::{error::Error, io, path::PathBuf};
22

3-
use clap::{Arg, Args, Command as ClapCommand, CommandFactory, FromArgMatches, Parser, ValueEnum};
3+
use clap::{
4+
Arg, Args, Command as ClapCommand, CommandFactory, Error as ClapError, FromArgMatches, Parser,
5+
ValueEnum, error::ErrorKind,
6+
};
47
use influxdb3_client::Client;
58
use influxdb3_types::http::CreateTokenWithPermissionsResponse;
9+
use owo_colors::OwoColorize;
610
use secrecy::Secret;
711
use url::Url;
812

@@ -11,7 +15,13 @@ pub(crate) async fn handle_token_creation_with_config(
1115
config: CreateTokenConfig,
1216
) -> Result<CreateTokenWithPermissionsResponse, Box<dyn Error>> {
1317
match config.admin_config {
14-
Some(admin_config) => handle_admin_token_creation(client, admin_config).await,
18+
Some(admin_config) => {
19+
if admin_config.name.is_some() {
20+
handle_named_admin_token_creation(client, admin_config).await
21+
} else {
22+
handle_admin_token_creation(client, admin_config).await
23+
}
24+
}
1525
_ => Err(
1626
"cannot create token, error with parameters run `influxdb3 create token --help`".into(),
1727
),
@@ -43,6 +53,20 @@ pub(crate) async fn handle_admin_token_creation(
4353
Ok(json_body)
4454
}
4555

56+
pub(crate) async fn handle_named_admin_token_creation(
57+
client: Client,
58+
config: CreateAdminTokenConfig,
59+
) -> Result<CreateTokenWithPermissionsResponse, Box<dyn Error>> {
60+
let json_body = client
61+
.api_v3_configure_create_named_admin_token(
62+
config.name.expect("token name to be present"),
63+
config.expiry.map(|expiry| expiry.as_secs()),
64+
)
65+
.await?
66+
.expect("token creation to return full token info");
67+
Ok(json_body)
68+
}
69+
4670
#[derive(Debug, ValueEnum, Clone)]
4771
pub enum TokenOutputFormat {
4872
Json,
@@ -72,10 +96,21 @@ pub struct InfluxDb3ServerConfig {
7296

7397
#[derive(Parser, Debug)]
7498
pub struct CreateAdminTokenConfig {
75-
/// Admin token will be regenerated when this is set
99+
/// Operator token will be regenerated when this is set
76100
#[clap(name = "regenerate", long = "regenerate")]
77101
pub regenerate: bool,
78102

103+
// for named admin and permission tokens this is mandatory but not for admin tokens
104+
/// Name of the token
105+
#[clap(long)]
106+
pub name: Option<String>,
107+
108+
/// Expires in `duration`,
109+
/// e.g 10d for 10 days
110+
/// 1y for 1 year
111+
#[clap(long)]
112+
pub expiry: Option<humantime::Duration>,
113+
79114
#[clap(flatten)]
80115
pub host: InfluxDb3ServerConfig,
81116

@@ -131,11 +166,26 @@ impl CreateTokenConfig {
131166

132167
impl FromArgMatches for CreateTokenConfig {
133168
fn from_arg_matches(matches: &clap::ArgMatches) -> Result<Self, clap::Error> {
134-
let admin_matches = matches
169+
let admin_subcmd_matches = matches
135170
.subcommand_matches("--admin")
136171
.expect("--admin must be present");
172+
let name = admin_subcmd_matches.get_one::<String>("name");
173+
let regenerate = admin_subcmd_matches
174+
.get_one::<bool>("regenerate")
175+
.cloned()
176+
.unwrap_or_default();
177+
178+
if name.is_some() && regenerate {
179+
return Err(ClapError::raw(
180+
ErrorKind::ArgumentConflict,
181+
"--regenerate cannot be used with --name, --regenerate only applies for operator token".yellow(),
182+
));
183+
}
184+
137185
Ok(Self {
138-
admin_config: Some(CreateAdminTokenConfig::from_arg_matches(admin_matches)?),
186+
admin_config: Some(CreateAdminTokenConfig::from_arg_matches(
187+
admin_subcmd_matches,
188+
)?),
139189
})
140190
}
141191

0 commit comments

Comments
 (0)