all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH v2 proxmox 19/19] access-control: allow extending tokens on secret regeneration
Date: Sun, 15 Nov 2026 20:40:40 +0100	[thread overview]
Message-ID: <20261115194043.3947825-20-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261115194043.3947825-1-t.lamprecht@proxmox.com>

Counterpart of the new 'allow-extension-on-regenerate' token policy
setting in PVE: with it, an update that regenerates the secret may
also extend the expiration date despite 'disallow-expiry-extension',
as the old credential stops working just like it would on recreating
the token. The maximum lifetime still bounds the new date, and the
check still runs before the secret is regenerated.

Suggested-by: Shannon Sterz <s.sterz@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---

new in v2

 proxmox-access-control/src/api/tokens.rs |  8 +++-
 proxmox-access-control/src/types.rs      | 56 ++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/proxmox-access-control/src/api/tokens.rs b/proxmox-access-control/src/api/tokens.rs
index 0ddcfdd3..0c4d408f 100644
--- a/proxmox-access-control/src/api/tokens.rs
+++ b/proxmox-access-control/src/api/tokens.rs
@@ -220,8 +220,12 @@ pub fn update_token(
 
     if let Some(expire) = expire {
         if let Some(policy) = crate::init::access_conf().token_policy()? {
-            let current = data.expire.unwrap_or(0);
-            policy.check_expiry_update(expire, current, proxmox_time::epoch_i64())?;
+            policy.check_expiry_update(
+                expire,
+                data.expire.unwrap_or(0),
+                regenerate == Some(true),
+                proxmox_time::epoch_i64(),
+            )?;
         }
         data.expire = if expire > 0 { Some(expire) } else { None };
     }
diff --git a/proxmox-access-control/src/types.rs b/proxmox-access-control/src/types.rs
index 1377bc50..7b8bb5ca 100644
--- a/proxmox-access-control/src/types.rs
+++ b/proxmox-access-control/src/types.rs
@@ -193,6 +193,10 @@ pub struct TokenApiEntry {
             optional: true,
             default: false,
         },
+        "allow-extension-on-regenerate": {
+            optional: true,
+            default: false,
+        },
     },
 )]
 #[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize, Updater)]
@@ -224,6 +228,12 @@ pub struct TokenPolicy {
     /// `max-lifetime`.
     #[serde(default, skip_serializing_if = "Option::is_none")]
     pub default_to_max_lifetime: Option<bool>,
+
+    /// Allow extending the expiration date of an existing API token when its secret is
+    /// regenerated in the same request, despite `disallow-expiry-extension`. The maximum lifetime
+    /// still applies.
+    #[serde(default, skip_serializing_if = "Option::is_none")]
+    pub allow_extension_on_regenerate: Option<bool>,
 }
 
 impl TokenPolicy {
@@ -248,13 +258,22 @@ impl TokenPolicy {
     ///
     /// `expire` is the submitted and `current` the stored expiration date, 0 meaning none for both.
     /// An unchanged date always passes, so updates of tokens that predate the policy keep working.
-    pub fn check_expiry_update(&self, expire: i64, current: i64, now: i64) -> Result<(), Error> {
+    /// `regenerate` tells whether the update also regenerates the secret.
+    pub fn check_expiry_update(
+        &self,
+        expire: i64,
+        current: i64,
+        regenerate: bool,
+        now: i64,
+    ) -> Result<(), Error> {
         if expire == current {
             return Ok(());
         }
 
-        // removing the expiration date or moving it later extends the token
+        // removing the expiration date or moving it later extends the token; regenerating the
+        // secret along with it is like recreating the token, which the policy may allow
         if self.disallow_expiry_extension.unwrap_or_default()
+            && !(regenerate && self.allow_extension_on_regenerate.unwrap_or_default())
             && current != 0
             && (expire == 0 || expire > current)
         {
@@ -445,6 +464,7 @@ mod tests {
         max_lifetime: None,
         disallow_expiry_extension: None,
         default_to_max_lifetime: None,
+        allow_extension_on_regenerate: None,
     };
 
     const REQUIRE_EXPIRY: TokenPolicy = TokenPolicy {
@@ -467,7 +487,16 @@ mod tests {
     }
 
     fn update_ok(policy: &TokenPolicy, expire: i64, current: i64) -> bool {
-        policy.check_expiry_update(expire, current, NOW).is_ok()
+        policy
+            .check_expiry_update(expire, current, false, NOW)
+            .is_ok()
+    }
+
+    // an update that also regenerates the secret
+    fn rotate_ok(policy: &TokenPolicy, expire: i64, current: i64) -> bool {
+        policy
+            .check_expiry_update(expire, current, true, NOW)
+            .is_ok()
     }
 
     #[test]
@@ -558,6 +587,27 @@ mod tests {
         assert!(create_ok(&NO_EXTENSION, 0));
     }
 
+    #[test]
+    fn allow_extension_on_regenerate() {
+        let policy = TokenPolicy {
+            allow_extension_on_regenerate: Some(true),
+            ..NO_EXTENSION
+        };
+        // extending or removing the date passes only together with a secret regeneration
+        assert!(rotate_ok(&policy, NOW + 200, CURRENT));
+        assert!(rotate_ok(&policy, 0, CURRENT));
+        assert!(!update_ok(&policy, NOW + 200, CURRENT));
+        // a policy without the setting ignores the regeneration
+        assert!(!rotate_ok(&NO_EXTENSION, NOW + 200, CURRENT));
+        // the maximum lifetime still bounds the extension
+        let bounded = TokenPolicy {
+            max_lifetime: Some(MAX as u64),
+            ..policy
+        };
+        assert!(rotate_ok(&bounded, NOW + MAX, CURRENT));
+        assert!(!rotate_ok(&bounded, NOW + MAX + 1, CURRENT));
+    }
+
     #[test]
     fn disallow_expiry_extension_combined() {
         let policy = TokenPolicy {
-- 
2.47.3





  parent reply	other threads:[~2026-11-15 19:43 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 19:40 [PATCH v2 cluster/access-control/docs/manager/proxmox 0/19] fix #7805: add a datacenter-wide API token policy Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 cluster 1/19] datacenter config: add token-policy option Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 cluster 2/19] datacenter config: token policy: add default-to-max-lifetime setting Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 cluster 3/19] datacenter config: token policy: allow extension on regeneration Thomas Lamprecht
2026-11-17 13:30   ` Shannon Sterz
2026-11-15 19:40 ` [PATCH v2 access-control 4/19] fix #7805: api: token: enforce datacenter token policy Thomas Lamprecht
2026-11-17 13:30   ` Shannon Sterz
2026-11-15 19:40 ` [PATCH v2 access-control 5/19] api: token: apply the default lifetime from the " Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 access-control 6/19] api: token: allow extending tokens on secret regeneration Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 docs 7/19] user management: document the API token policy Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 docs 8/19] user management: document the default-to-max-lifetime setting Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 docs 9/19] user management: document extending tokens on secret regeneration Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 manager 10/19] ui: token edit: only submit the expiration date when changed Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 manager 11/19] api: cluster options: return token-policy without Sys.Audit Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 manager 12/19] ui: dc options: allow editing the API token policy Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 manager 13/19] ui: token edit: adapt to the datacenter " Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 manager 14/19] ui: token policy: expose the default-to-max-lifetime setting Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 manager 15/19] ui: token policy: allow extension on secret regeneration Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 proxmox 16/19] access-control: add API token policy type with expiry checks Thomas Lamprecht
2026-11-17 13:29   ` Shannon Sterz
2026-11-15 19:40 ` [PATCH v2 proxmox 17/19] access-control: enforce token policy on token create and update Thomas Lamprecht
2026-11-15 19:40 ` [PATCH v2 proxmox 18/19] access-control: apply a default token lifetime from the policy Thomas Lamprecht
2026-11-15 19:40 ` Thomas Lamprecht [this message]
2026-11-17 13:29   ` [PATCH v2 proxmox 19/19] access-control: allow extending tokens on secret regeneration Shannon Sterz
2026-11-17 13:29 ` [PATCH v2 cluster/access-control/docs/manager/proxmox 0/19] fix #7805: add a datacenter-wide API token policy Shannon Sterz
2026-11-18  2:17 ` applied: " 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=20261115194043.3947825-20-t.lamprecht@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=pve-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal