public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v4 4/7] api2: add missing token list match_all property
Date: Tue, 21 Sep 2021 12:11:15 +0200	[thread overview]
Message-ID: <20210921101118.2640200-5-d.csapak@proxmox.com> (raw)
In-Reply-To: <20210921101118.2640200-1-d.csapak@proxmox.com>

to have the proper link between the token list and the sub routes
in the api, include the 'tokenname' property in the token listing

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/api2/access/user.rs                | 66 +++++++++++++++++---------
 src/bin/proxmox_backup_manager/user.rs |  6 +--
 2 files changed, 47 insertions(+), 25 deletions(-)

diff --git a/src/api2/access/user.rs b/src/api2/access/user.rs
index 75071cf1..1d866214 100644
--- a/src/api2/access/user.rs
+++ b/src/api2/access/user.rs
@@ -395,7 +395,7 @@ pub fn delete_user(userid: Userid, digest: Option<String>) -> Result<(), Error>
             userid: {
                 type: Userid,
             },
-            tokenname: {
+            "token-name": {
                 type: Tokenname,
             },
         },
@@ -411,14 +411,14 @@ pub fn delete_user(userid: Userid, digest: Option<String>) -> Result<(), Error>
 /// Read user's API token metadata
 pub fn read_token(
     userid: Userid,
-    tokenname: Tokenname,
+    token_name: Tokenname,
     _info: &ApiMethod,
     mut rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<ApiToken, Error> {
 
     let (config, digest) = pbs_config::user::config()?;
 
-    let tokenid = Authid::from((userid, Some(tokenname)));
+    let tokenid = Authid::from((userid, Some(token_name)));
 
     rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
     config.lookup("token", &tokenid.to_string())
@@ -431,7 +431,7 @@ pub fn read_token(
             userid: {
                 type: Userid,
             },
-            tokenname: {
+            "token-name": {
                 type: Tokenname,
             },
             comment: {
@@ -475,7 +475,7 @@ pub fn read_token(
 /// Generate a new API token with given metadata
 pub fn generate_token(
     userid: Userid,
-    tokenname: Tokenname,
+    token_name: Tokenname,
     comment: Option<String>,
     enable: Option<bool>,
     expire: Option<i64>,
@@ -491,11 +491,11 @@ pub fn generate_token(
         crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
     }
 
-    let tokenid = Authid::from((userid.clone(), Some(tokenname.clone())));
+    let tokenid = Authid::from((userid.clone(), Some(token_name.clone())));
     let tokenid_string = tokenid.to_string();
 
     if config.sections.get(&tokenid_string).is_some() {
-        bail!("token '{}' for user '{}' already exists.", tokenname.as_str(), userid);
+        bail!("token '{}' for user '{}' already exists.", token_name.as_str(), userid);
     }
 
     let secret = format!("{:x}", proxmox::tools::uuid::Uuid::generate());
@@ -525,7 +525,7 @@ pub fn generate_token(
             userid: {
                 type: Userid,
             },
-            tokenname: {
+            "token-name": {
                 type: Tokenname,
             },
             comment: {
@@ -556,7 +556,7 @@ pub fn generate_token(
 /// Update user's API token metadata
 pub fn update_token(
     userid: Userid,
-    tokenname: Tokenname,
+    token_name: Tokenname,
     comment: Option<String>,
     enable: Option<bool>,
     expire: Option<i64>,
@@ -572,7 +572,7 @@ pub fn update_token(
         crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
     }
 
-    let tokenid = Authid::from((userid, Some(tokenname)));
+    let tokenid = Authid::from((userid, Some(token_name)));
     let tokenid_string = tokenid.to_string();
 
     let mut data: ApiToken = config.lookup("token", &tokenid_string)?;
@@ -608,7 +608,7 @@ pub fn update_token(
             userid: {
                 type: Userid,
             },
-            tokenname: {
+            "token-name": {
                 type: Tokenname,
             },
             digest: {
@@ -627,7 +627,7 @@ pub fn update_token(
 /// Delete a user's API token
 pub fn delete_token(
     userid: Userid,
-    tokenname: Tokenname,
+    token_name: Tokenname,
     digest: Option<String>,
 ) -> Result<(), Error> {
 
@@ -640,12 +640,12 @@ pub fn delete_token(
         crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
     }
 
-    let tokenid = Authid::from((userid.clone(), Some(tokenname.clone())));
+    let tokenid = Authid::from((userid.clone(), Some(token_name.clone())));
     let tokenid_string = tokenid.to_string();
 
     match config.sections.get(&tokenid_string) {
         Some(_) => { config.sections.remove(&tokenid_string); },
-        None => bail!("token '{}' of user '{}' does not exist.", tokenname.as_str(), userid),
+        None => bail!("token '{}' of user '{}' does not exist.", token_name.as_str(), userid),
     }
 
     token_shadow::delete_secret(&tokenid)?;
@@ -655,6 +655,22 @@ pub fn delete_token(
     Ok(())
 }
 
+#[api(
+    properties: {
+        "token-name": { type: Tokenname },
+        token: { type: ApiToken },
+    }
+)]
+#[derive(Serialize, Deserialize)]
+#[serde(rename_all="kebab-case")]
+/// A Token Entry that contains the token-name
+pub struct TokenApiEntry {
+    /// The Token name
+    pub token_name: Tokenname,
+    #[serde(flatten)]
+    pub token: ApiToken,
+}
+
 #[api(
     input: {
         properties: {
@@ -666,7 +682,7 @@ pub fn delete_token(
     returns: {
         description: "List user's API tokens (with config digest).",
         type: Array,
-        items: { type: ApiToken },
+        items: { type: TokenApiEntry },
     },
     access: {
         permission: &Permission::Or(&[
@@ -680,7 +696,7 @@ pub fn list_tokens(
     userid: Userid,
     _info: &ApiMethod,
     mut rpcenv: &mut dyn RpcEnvironment,
-) -> Result<Vec<ApiToken>, Error> {
+) -> Result<Vec<TokenApiEntry>, Error> {
 
     let (config, digest) = pbs_config::user::config()?;
 
@@ -688,15 +704,21 @@ pub fn list_tokens(
 
     rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
 
-    let filter_by_owner = |token: &ApiToken| {
-        if token.tokenid.is_token() {
-           token.tokenid.user() == &userid
+    let filter_by_owner = |token: ApiToken| {
+        if token.tokenid.is_token() && token.tokenid.user() == &userid {
+            let token_name = token.tokenid.tokenname().unwrap().to_owned();
+            Some(TokenApiEntry {
+                token_name,
+                token,
+            })
         } else {
-            false
+            None
         }
     };
 
-    Ok(list.into_iter().filter(filter_by_owner).collect())
+    let res = list.into_iter().filter_map(filter_by_owner).collect();
+
+    Ok(res)
 }
 
 const TOKEN_ITEM_ROUTER: Router = Router::new()
@@ -707,7 +729,7 @@ const TOKEN_ITEM_ROUTER: Router = Router::new()
 
 const TOKEN_ROUTER: Router = Router::new()
     .get(&API_METHOD_LIST_TOKENS)
-    .match_all("tokenname", &TOKEN_ITEM_ROUTER);
+    .match_all("token-name", &TOKEN_ITEM_ROUTER);
 
 const USER_SUBDIRS: SubdirMap = &[
     ("token", &TOKEN_ROUTER),
diff --git a/src/bin/proxmox_backup_manager/user.rs b/src/bin/proxmox_backup_manager/user.rs
index 9681ba79..cff66ce7 100644
--- a/src/bin/proxmox_backup_manager/user.rs
+++ b/src/bin/proxmox_backup_manager/user.rs
@@ -198,15 +198,15 @@ pub fn user_commands() -> CommandLineInterface {
         .insert(
             "generate-token",
             CliCommand::new(&api2::access::user::API_METHOD_GENERATE_TOKEN)
-                .arg_param(&["userid", "tokenname"])
+                .arg_param(&["userid", "token-name"])
                 .completion_cb("userid", pbs_config::user::complete_userid)
         )
         .insert(
             "delete-token",
             CliCommand::new(&api2::access::user::API_METHOD_DELETE_TOKEN)
-                .arg_param(&["userid", "tokenname"])
+                .arg_param(&["userid", "token-name"])
                 .completion_cb("userid", pbs_config::user::complete_userid)
-                .completion_cb("tokenname", pbs_config::user::complete_token_name)
+                .completion_cb("token-name", pbs_config::user::complete_token_name)
         )
         .insert(
             "permissions",
-- 
2.30.2





  parent reply	other threads:[~2021-09-21 10:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-21 10:11 [pbs-devel] [PATCH proxmox-backup v4 0/7] add 'proxmox-backup-debug api' commands Dominik Csapak
2021-09-21 10:11 ` [pbs-devel] [PATCH proxmox-backup v4 1/7] server: refactor abort_local_worker Dominik Csapak
2021-09-21 10:11 ` [pbs-devel] [PATCH proxmox-backup v4 2/7] move proxmox-backup-debug back to main crate Dominik Csapak
2021-09-21 10:11 ` [pbs-devel] [PATCH proxmox-backup v4 3/7] proxmox-backup-debug: add 'api' subcommands Dominik Csapak
2021-09-21 10:11 ` Dominik Csapak [this message]
2021-09-21 10:11 ` [pbs-devel] [PATCH proxmox-backup v4 5/7] api2: make some workers log on CLI Dominik Csapak
2021-09-21 10:11 ` [pbs-devel] [PATCH proxmox-backup v4 6/7] docs: add proxmox-backup-debug to the list of command line tools Dominik Csapak
2021-09-21 10:11 ` [pbs-devel] [PATCH proxmox-backup v4 7/7] docs: proxmox-backup-debug: add info about the 'api' subcommand Dominik Csapak
2021-09-21 13:45 ` [pbs-devel] applied-series: [PATCH proxmox-backup v4 0/7] add 'proxmox-backup-debug api' commands Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210921101118.2640200-5-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal