From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 8CCF662EBC for ; Wed, 28 Oct 2020 12:37:50 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 838831ED18 for ; Wed, 28 Oct 2020 12:37:20 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id F30A81ED0E for ; Wed, 28 Oct 2020 12:37:19 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BF88F45E51 for ; Wed, 28 Oct 2020 12:37:19 +0100 (CET) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Wed, 28 Oct 2020 12:37:12 +0100 Message-Id: <20201028113717.827644-2-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201028113717.827644-1-f.gruenbichler@proxmox.com> References: <20201028113632.814586-1-f.gruenbichler@proxmox.com> <20201028113717.827644-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.028 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [user.rs] Subject: [pbs-devel] [PATCH proxmox-backup 11/16] manager: add token commands X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Oct 2020 11:37:50 -0000 to generate, list and delete tokens. adding them to ACLs already works out of the box. Signed-off-by: Fabian Grünbichler --- src/bin/proxmox_backup_manager/user.rs | 62 ++++++++++++++++++++++++++ src/config/user.rs | 31 +++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/bin/proxmox_backup_manager/user.rs b/src/bin/proxmox_backup_manager/user.rs index af05e9b5..00749cee 100644 --- a/src/bin/proxmox_backup_manager/user.rs +++ b/src/bin/proxmox_backup_manager/user.rs @@ -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 Result { + + 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() diff --git a/src/config/user.rs b/src/config/user.rs index 78571daa..5966c96d 100644 --- a/src/config/user.rs +++ b/src/config/user.rs @@ -265,3 +265,34 @@ pub fn complete_authid(_arg: &str, _param: &HashMap) -> Vec vec![], } } + +// shell completion helper +pub fn complete_token_name(_arg: &str, param: &HashMap) -> Vec { + let data = match config() { + Ok((data, _digest)) => data, + Err(_) => return Vec::new(), + }; + + match param.get("userid") { + Some(userid) => { + let user = data.lookup::("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![], + } +} -- 2.20.1