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 access-control 6/19] api: token: allow extending tokens on secret regeneration
Date: Sun, 15 Nov 2026 20:40:27 +0100	[thread overview]
Message-ID: <20261115194043.3947825-7-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261115194043.3947825-1-t.lamprecht@proxmox.com>

Honor the new 'allow-extension-on-regenerate' policy setting: 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, so a rejected extension does not rotate it.

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

new in v2

 src/PVE/API2/User.pm              |  1 +
 src/PVE/AccessControl.pm          |  8 +++++-
 src/test/token-policy-api-test.pl | 11 ++++++++
 src/test/token-policy-test.pl     | 45 +++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/src/PVE/API2/User.pm b/src/PVE/API2/User.pm
index 6490060..453d317 100644
--- a/src/PVE/API2/User.pm
+++ b/src/PVE/API2/User.pm
@@ -932,6 +932,7 @@ __PACKAGE__->register_method({
                     expire => $expire_change,
                     'privilege-separation' => $privilege_separation_change,
                     'current-expire' => $old_expire,
+                    regenerate => $regenerate,
                 });
 
                 my $deletable = {
diff --git a/src/PVE/AccessControl.pm b/src/PVE/AccessControl.pm
index 930cb5a..3192fed 100644
--- a/src/PVE/AccessControl.pm
+++ b/src/PVE/AccessControl.pm
@@ -700,6 +700,8 @@ sub check_user_enabled {
 #   date checks
 # - privilege-separation: the privilege separation flag to check; undef skips that check
 # - current-expire: the expiration date of an existing token before the update, 0 for none
+# - regenerate: whether the update also regenerates the secret, see
+#   'allow-extension-on-regenerate'
 # - ctime, policy: override the reference time for the maximum lifetime and the policy from the
 #   datacenter config, for testing
 #
@@ -719,10 +721,14 @@ sub assert_token_policy {
     return if !$policy;
 
     if (defined($expire)) {
-        # 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
         my $current_expire = $param->{'current-expire'};
+        my $regenerate_allows_extension =
+            $param->{regenerate} && $policy->{'allow-extension-on-regenerate'};
         if (
             $policy->{'disallow-expiry-extension'}
+            && !$regenerate_allows_extension
             && $current_expire
             && (!$expire || $expire > $current_expire)
         ) {
diff --git a/src/test/token-policy-api-test.pl b/src/test/token-policy-api-test.pl
index ebaece7..090d576 100644
--- a/src/test/token-policy-api-test.pl
+++ b/src/test/token-policy-api-test.pl
@@ -134,4 +134,15 @@ $reset->({ 'default-to-max-lifetime' => 1, 'require-expiry' => 1 });
 ($ok, $err) = $invoke->('generate_token', { tokenid => 'new' });
 ok(!$ok && $err =~ /requires an expiration date/, 'no default without a maximum lifetime');
 
+# allow-extension-on-regenerate: extending needs the secret regenerated in the same request
+$reset->({ 'disallow-expiry-extension' => 1, 'allow-extension-on-regenerate' => 1 }, $now + 600);
+($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => $now + 1200 });
+ok(!$ok && $err =~ /does not allow extending/, 'extending without regeneration is rejected');
+is($secret_calls, 0, 'the rejected update does not touch the secret');
+($ok, $err) =
+    $invoke->('update_token_info', { tokenid => 'old', expire => $now + 1200, regenerate => 1 });
+ok($ok, 'extending together with regeneration is accepted') or diag($err);
+is($secret_calls, 1, 'the accepted update regenerates the secret');
+like($user_cfg_raw, qr/^token:test\@pve!old:${\($now + 1200)}:/m, 'the extended expiry is stored');
+
 done_testing();
diff --git a/src/test/token-policy-test.pl b/src/test/token-policy-test.pl
index f43fc8c..2310fdc 100644
--- a/src/test/token-policy-test.pl
+++ b/src/test/token-policy-test.pl
@@ -177,6 +177,51 @@ check(
     0,
 );
 
+# allow-extension-on-regenerate lifts the extension ban only together with a secret regeneration
+my $rotation = { 'disallow-expiry-extension' => 1, 'allow-extension-on-regenerate' => 1 };
+check(
+    'allow-extension-on-regenerate, extension with regeneration',
+    $rotation,
+    { expire => $now + 200, 'current-expire' => $now + 100, regenerate => 1 },
+    0,
+);
+check(
+    'allow-extension-on-regenerate, removal with regeneration',
+    $rotation,
+    { expire => 0, 'current-expire' => $now + 100, regenerate => 1 },
+    0,
+);
+check(
+    'allow-extension-on-regenerate, extension without regeneration',
+    $rotation,
+    { expire => $now + 200, 'current-expire' => $now + 100 },
+    1,
+);
+check(
+    'allow-extension-on-regenerate, extension with regenerate=0',
+    $rotation,
+    { expire => $now + 200, 'current-expire' => $now + 100, regenerate => 0 },
+    1,
+);
+check(
+    'disallow-expiry-extension alone ignores regeneration',
+    $no_extension,
+    { expire => $now + 200, 'current-expire' => $now + 100, regenerate => 1 },
+    1,
+);
+check(
+    'allow-extension-on-regenerate, max-lifetime still bounds the extension',
+    { $rotation->%*, 'max-lifetime' => $max },
+    { expire => $now + $max + 1, 'current-expire' => $now + 100, regenerate => 1 },
+    1,
+);
+check(
+    'allow-extension-on-regenerate, extension within max-lifetime with regeneration',
+    { $rotation->%*, 'max-lifetime' => $max },
+    { expire => $now + $max, 'current-expire' => $now + 100, regenerate => 1 },
+    0,
+);
+
 # require-privilege-separation
 my $require_privilege_separation = { 'require-privilege-separation' => 1 };
 check(
-- 
2.47.3





  parent reply	other threads:[~2026-11-15 19:41 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 ` Thomas Lamprecht [this message]
2026-11-15 19:40 ` [PATCH v2 docs 7/19] user management: document the API " 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 ` [PATCH v2 proxmox 19/19] access-control: allow extending tokens on secret regeneration Thomas Lamprecht
2026-11-17 13:29   ` 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-7-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