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 9A520608E8 for ; Mon, 19 Oct 2020 09:39:58 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id EFFB32984C for ; Mon, 19 Oct 2020 09:39:52 +0200 (CEST) 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 2876D29842 for ; Mon, 19 Oct 2020 09:39:52 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E582645D69 for ; Mon, 19 Oct 2020 09:39:51 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Mon, 19 Oct 2020 09:39:12 +0200 Message-Id: <20201019073919.588521-9-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201019073919.588521-1-f.gruenbichler@proxmox.com> References: <20201019073919.588521-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.032 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 Subject: [pbs-devel] [RFC proxmox-backup 08/15] api: add API token endpoints 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: Mon, 19 Oct 2020 07:39:58 -0000 beneath the user endpoint. Signed-off-by: Fabian Grünbichler --- src/api2/access/user.rs | 327 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 324 insertions(+), 3 deletions(-) diff --git a/src/api2/access/user.rs b/src/api2/access/user.rs index 6c292c2d..4197cf60 100644 --- a/src/api2/access/user.rs +++ b/src/api2/access/user.rs @@ -1,12 +1,15 @@ use anyhow::{bail, Error}; use serde_json::Value; +use std::convert::TryFrom; use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission}; +use proxmox::api::router::SubdirMap; use proxmox::api::schema::{Schema, StringSchema}; use proxmox::tools::fs::open_file_locked; use crate::api2::types::*; use crate::config::user; +use crate::config::token_shadow; use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_PERMISSIONS_MODIFY}; use crate::config::cached_user_info::CachedUserInfo; @@ -304,12 +307,330 @@ pub fn delete_user(userid: Userid, digest: Option) -> Result<(), Error> Ok(()) } -const ITEM_ROUTER: Router = Router::new() +#[api( + input: { + properties: { + userid: { + schema: PROXMOX_USER_ID_SCHEMA, + }, + tokenname: { + schema: PROXMOX_TOKEN_NAME_SCHEMA, + }, + }, + }, + returns: { + description: "Get API token metadata (with config digest).", + type: user::ApiToken, + }, + access: { + permission: &Permission::Or(&[ + &Permission::Privilege(&["access", "users"], PRIV_SYS_AUDIT, false), + &Permission::UserParam("userid"), + ]), + }, +)] +/// Read user's API token metadata +pub fn read_token( + userid: Userid, + tokenname: String, + _info: &ApiMethod, + mut rpcenv: &mut dyn RpcEnvironment, +) -> Result { + + let (config, digest) = user::config()?; + + let tokenname = Tokenname::try_from(tokenname)?; + + let tokenid = Userid::from((userid.name(), userid.realm(), tokenname.as_ref())); + + rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); + config.lookup("token", tokenid.as_str()) +} + +#[api( + protected: true, + input: { + properties: { + userid: { + schema: PROXMOX_USER_ID_SCHEMA, + }, + tokenname: { + schema: PROXMOX_TOKEN_NAME_SCHEMA, + }, + comment: { + optional: true, + schema: SINGLE_LINE_COMMENT_SCHEMA, + }, + enable: { + schema: user::ENABLE_USER_SCHEMA, + optional: true, + }, + expire: { + schema: user::EXPIRE_USER_SCHEMA, + optional: true, + }, + digest: { + optional: true, + schema: PROXMOX_CONFIG_DIGEST_SCHEMA, + }, + }, + }, + access: { + permission: &Permission::Or(&[ + &Permission::Privilege(&["access", "users"], PRIV_PERMISSIONS_MODIFY, false), + &Permission::UserParam("userid"), + ]), + }, + returns: { + description: "Secret API token value", + type: String, + }, +)] +/// Generate a new API token with given metadata +pub fn generate_token( + userid: Userid, + tokenname: String, + comment: Option, + enable: Option, + expire: Option, + digest: Option, +) -> Result { + + let _lock = open_file_locked(user::USER_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?; + + let (mut config, expected_digest) = user::config()?; + + if let Some(ref digest) = digest { + let digest = proxmox::tools::hex_to_digest(digest)?; + crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; + } + + let tokenname = Tokenname::try_from(tokenname)?; + let tokenid = Userid::from((userid.name(), userid.realm(), tokenname.as_ref())); + + if let Some(_) = config.sections.get(tokenid.as_str()) { + bail!("token '{}' for user '{}' already exists.", tokenname.as_str(), userid); + } + + let secret = format!("{:x}", proxmox::tools::uuid::Uuid::generate()); + token_shadow::set_secret(&tokenid, &secret)?; + + let token = user::ApiToken { + tokenid: tokenid.clone(), + comment, + enable, + expire, + }; + + config.set_data(tokenid.as_str(), "token", &token)?; + + user::save_config(&config)?; + + Ok(secret) +} + +#[api( + protected: true, + input: { + properties: { + userid: { + schema: PROXMOX_USER_ID_SCHEMA, + }, + tokenname: { + schema: PROXMOX_TOKEN_NAME_SCHEMA, + }, + comment: { + optional: true, + schema: SINGLE_LINE_COMMENT_SCHEMA, + }, + enable: { + schema: user::ENABLE_USER_SCHEMA, + optional: true, + }, + expire: { + schema: user::EXPIRE_USER_SCHEMA, + optional: true, + }, + digest: { + optional: true, + schema: PROXMOX_CONFIG_DIGEST_SCHEMA, + }, + }, + }, + access: { + permission: &Permission::Or(&[ + &Permission::Privilege(&["access", "users"], PRIV_PERMISSIONS_MODIFY, false), + &Permission::UserParam("userid"), + ]), + }, +)] +/// Update user's API token metadata +pub fn update_token( + userid: Userid, + tokenname: String, + comment: Option, + enable: Option, + expire: Option, + digest: Option, +) -> Result<(), Error> { + + let _lock = open_file_locked(user::USER_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?; + + let (mut config, expected_digest) = user::config()?; + + if let Some(ref digest) = digest { + let digest = proxmox::tools::hex_to_digest(digest)?; + crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; + } + + let tokenname = Tokenname::try_from(tokenname)?; + let tokenid = Userid::from((userid.name(), userid.realm(), tokenname.as_ref())); + + let mut data: user::ApiToken = config.lookup("token", tokenid.as_str())?; + + if let Some(comment) = comment { + let comment = comment.trim().to_string(); + if comment.is_empty() { + data.comment = None; + } else { + data.comment = Some(comment); + } + } + + if let Some(enable) = enable { + data.enable = if enable { None } else { Some(false) }; + } + + if let Some(expire) = expire { + data.expire = if expire > 0 { Some(expire) } else { None }; + } + + config.set_data(tokenid.as_str(), "token", &data)?; + + user::save_config(&config)?; + + Ok(()) +} + +#[api( + protected: true, + input: { + properties: { + userid: { + schema: PROXMOX_USER_ID_SCHEMA, + }, + tokenname: { + schema: PROXMOX_TOKEN_NAME_SCHEMA, + }, + digest: { + optional: true, + schema: PROXMOX_CONFIG_DIGEST_SCHEMA, + }, + }, + }, + access: { + permission: &Permission::Or(&[ + &Permission::Privilege(&["access", "users"], PRIV_PERMISSIONS_MODIFY, false), + &Permission::UserParam("userid"), + ]), + }, +)] +/// Delete a user's API token +pub fn delete_token( + userid: Userid, + tokenname: String, + digest: Option, +) -> Result<(), Error> { + + let _lock = open_file_locked(user::USER_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?; + + let (mut config, expected_digest) = user::config()?; + + if let Some(ref digest) = digest { + let digest = proxmox::tools::hex_to_digest(digest)?; + crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; + } + + let tokenname = Tokenname::try_from(tokenname)?; + let tokenid = Userid::from((userid.name(), userid.realm(), tokenname.as_ref())); + + match config.sections.get(tokenid.as_str()) { + Some(_) => { config.sections.remove(tokenid.as_str()); }, + None => bail!("token '{}' of user '{}' does not exist.", tokenname.as_str(), userid), + } + + token_shadow::delete_secret(&tokenid)?; + + user::save_config(&config)?; + + Ok(()) +} + +#[api( + input: { + properties: { + userid: { + schema: PROXMOX_USER_ID_SCHEMA, + }, + }, + }, + returns: { + description: "List user's API tokens (with config digest).", + type: Array, + items: { type: user::ApiToken }, + }, + access: { + permission: &Permission::Or(&[ + &Permission::Privilege(&["access", "users"], PRIV_SYS_AUDIT, false), + &Permission::UserParam("userid"), + ]), + }, +)] +/// List user's API tokens +pub fn list_tokens( + userid: Userid, + _info: &ApiMethod, + mut rpcenv: &mut dyn RpcEnvironment, +) -> Result, Error> { + + let (config, digest) = user::config()?; + + let list:Vec = config.convert_to_typed_array("token")?; + + rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); + + let filter_by_owner = |token: &user::ApiToken| { + if let Ok(owner) = token.tokenid.owner() { + owner == userid + } else { + false + } + }; + + Ok(list.into_iter().filter(filter_by_owner).collect()) +} + +const TOKEN_ITEM_ROUTER: Router = Router::new() + .get(&API_METHOD_READ_TOKEN) + .put(&API_METHOD_UPDATE_TOKEN) + .post(&API_METHOD_GENERATE_TOKEN) + .delete(&API_METHOD_DELETE_TOKEN); + +const TOKEN_ROUTER: Router = Router::new() + .get(&API_METHOD_LIST_TOKENS) + .match_all("tokenname", &TOKEN_ITEM_ROUTER); + +const USER_SUBDIRS: SubdirMap = &[ + ("token", &TOKEN_ROUTER), +]; + +const USER_ROUTER: Router = Router::new() .get(&API_METHOD_READ_USER) .put(&API_METHOD_UPDATE_USER) - .delete(&API_METHOD_DELETE_USER); + .delete(&API_METHOD_DELETE_USER) + .subdirs(USER_SUBDIRS); pub const ROUTER: Router = Router::new() .get(&API_METHOD_LIST_USERS) .post(&API_METHOD_CREATE_USER) - .match_all("userid", &ITEM_ROUTER); + .match_all("userid", &USER_ROUTER); -- 2.20.1