manager: add token commands
to generate, list and delete tokens. adding them to ACLs already works out of the box. Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
parent
16245d540c
commit
2156dec5a9
@ -6,6 +6,7 @@ use proxmox::api::{api, cli::*, RpcEnvironment, ApiHandler};
|
||||
use proxmox_backup::config;
|
||||
use proxmox_backup::tools;
|
||||
use proxmox_backup::api2;
|
||||
use proxmox_backup::api2::types::Userid;
|
||||
|
||||
#[api(
|
||||
input: {
|
||||
@ -48,6 +49,48 @@ fn list_users(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Er
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
#[api(
|
||||
input: {
|
||||
properties: {
|
||||
"output-format": {
|
||||
schema: OUTPUT_FORMAT,
|
||||
optional: true,
|
||||
},
|
||||
userid: {
|
||||
type: Userid,
|
||||
}
|
||||
}
|
||||
}
|
||||
)]
|
||||
/// List tokens associated with user.
|
||||
fn list_tokens(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
|
||||
|
||||
let output_format = get_output_format(¶m);
|
||||
|
||||
let info = &api2::access::user::API_METHOD_LIST_TOKENS;
|
||||
let mut data = match info.handler {
|
||||
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let options = default_table_format_options()
|
||||
.column(ColumnConfig::new("tokenid"))
|
||||
.column(
|
||||
ColumnConfig::new("enable")
|
||||
.renderer(tools::format::render_bool_with_default_true)
|
||||
)
|
||||
.column(
|
||||
ColumnConfig::new("expire")
|
||||
.renderer(tools::format::render_epoch)
|
||||
)
|
||||
.column(ColumnConfig::new("comment"));
|
||||
|
||||
format_and_print_result_full(&mut data, info.returns, &output_format, &options);
|
||||
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
|
||||
pub fn user_commands() -> CommandLineInterface {
|
||||
|
||||
let cmd_def = CliCommandMap::new()
|
||||
@ -69,6 +112,25 @@ pub fn user_commands() -> CommandLineInterface {
|
||||
CliCommand::new(&api2::access::user::API_METHOD_DELETE_USER)
|
||||
.arg_param(&["userid"])
|
||||
.completion_cb("userid", config::user::complete_userid)
|
||||
)
|
||||
.insert(
|
||||
"list-tokens",
|
||||
CliCommand::new(&&API_METHOD_LIST_TOKENS)
|
||||
.arg_param(&["userid"])
|
||||
.completion_cb("userid", config::user::complete_userid)
|
||||
)
|
||||
.insert(
|
||||
"generate-token",
|
||||
CliCommand::new(&api2::access::user::API_METHOD_GENERATE_TOKEN)
|
||||
.arg_param(&["userid", "tokenname"])
|
||||
.completion_cb("userid", config::user::complete_userid)
|
||||
)
|
||||
.insert(
|
||||
"delete-token",
|
||||
CliCommand::new(&api2::access::user::API_METHOD_DELETE_TOKEN)
|
||||
.arg_param(&["userid", "tokenname"])
|
||||
.completion_cb("userid", config::user::complete_userid)
|
||||
.completion_cb("tokenname", config::user::complete_token_name)
|
||||
);
|
||||
|
||||
cmd_def.into()
|
||||
|
@ -265,3 +265,34 @@ pub fn complete_authid(_arg: &str, _param: &HashMap<String, String>) -> Vec<Stri
|
||||
Err(_) => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
// shell completion helper
|
||||
pub fn complete_token_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
||||
let data = match config() {
|
||||
Ok((data, _digest)) => data,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
|
||||
match param.get("userid") {
|
||||
Some(userid) => {
|
||||
let user = data.lookup::<User>("user", userid);
|
||||
let tokens = data.convert_to_typed_array("token");
|
||||
match (user, tokens) {
|
||||
(Ok(_), Ok(tokens)) => {
|
||||
tokens
|
||||
.into_iter()
|
||||
.filter_map(|token: ApiToken| {
|
||||
let tokenid = token.tokenid;
|
||||
if tokenid.is_token() && tokenid.user() == userid {
|
||||
Some(tokenid.tokenname().unwrap().as_str().to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}).collect()
|
||||
},
|
||||
_ => vec![],
|
||||
}
|
||||
},
|
||||
None => vec![],
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user