From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH v2 access-control 5/19] api: token: apply the default lifetime from the token policy
Date: Sun, 15 Nov 2026 20:40:26 +0100 [thread overview]
Message-ID: <20261115194043.3947825-6-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261115194043.3947825-1-t.lamprecht@proxmox.com>
Give tokens created without an expiration date the maximum lifetime
when the policy opts into that with 'default-to-max-lifetime', instead
of rejecting them. Tools unaware of the policy, like the remote wizard
of Proxmox Datacenter Manager, then no longer fail just for omitting
the expiration date, and the effective expiration date is part of the
creation response as before. Updates are not affected, removing an
expiration date there is an explicit request and stays rejected.
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 | 5 +++++
src/PVE/AccessControl.pm | 12 ++++++++++++
src/test/token-policy-api-test.pl | 22 ++++++++++++++++++++++
src/test/token-policy-test.pl | 17 +++++++++++++++++
4 files changed, 56 insertions(+)
diff --git a/src/PVE/API2/User.pm b/src/PVE/API2/User.pm
index 78e5fc9..6490060 100644
--- a/src/PVE/API2/User.pm
+++ b/src/PVE/API2/User.pm
@@ -811,6 +811,11 @@ __PACKAGE__->register_method({
$token->{expire} = $param->{expire} if defined($param->{expire});
$token->{comment} = $param->{comment} if $param->{comment};
+ if (!int($token->{expire} // 0)) {
+ my $default_expire = PVE::AccessControl::token_policy_default_expire();
+ $token->{expire} = $default_expire if $default_expire;
+ }
+
# check policy before generating the secret to avoid leaving an orphan in token.cfg
PVE::AccessControl::assert_token_policy({
expire => $token->{expire} // 0,
diff --git a/src/PVE/AccessControl.pm b/src/PVE/AccessControl.pm
index e2027cf..930cb5a 100644
--- a/src/PVE/AccessControl.pm
+++ b/src/PVE/AccessControl.pm
@@ -753,6 +753,18 @@ sub assert_token_policy {
}
}
+# Returns the expiration date the datacenter token policy assigns to a new token created without
+# one, or undef if the policy does not default it. 'ctime' and 'policy' can be overridden like
+# for assert_token_policy.
+sub token_policy_default_expire {
+ my ($param) = @_;
+
+ my $policy = $param->{policy} // cfs_read_file('datacenter.cfg')->{'token-policy'};
+ return undef if !$policy || !$policy->{'default-to-max-lifetime'} || !$policy->{'max-lifetime'};
+
+ return ($param->{ctime} // time()) + $policy->{'max-lifetime'};
+}
+
sub check_token_exist {
my ($usercfg, $username, $tokenid, $noerr) = @_;
diff --git a/src/test/token-policy-api-test.pl b/src/test/token-policy-api-test.pl
index 5af4e58..ebaece7 100644
--- a/src/test/token-policy-api-test.pl
+++ b/src/test/token-policy-api-test.pl
@@ -112,4 +112,26 @@ $reset->({ 'disallow-expiry-extension' => 1 }, 0);
($ok, $err) = $invoke->('update_token_info', { tokenid => 'old', expire => $now + 600 });
ok($ok, 'setting an expiry on a token without one stays allowed') or diag($err);
+# default-to-max-lifetime gives tokens created without an expiration date the maximum lifetime
+my $defaulting = { 'max-lifetime' => 86400, 'default-to-max-lifetime' => 1 };
+for my $case (['absent', {}], ['zero', { expire => 0 }], ['zero spelling', { expire => '00' }]) {
+ my ($desc, $param) = $case->@*;
+ $reset->($defaulting);
+ my $before = time();
+ ($ok, $err) = $invoke->('generate_token', { tokenid => 'new', %$param });
+ ok($ok, "create with $desc expiry is accepted under default-to-max-lifetime") or diag($err);
+ my ($expire) = $user_cfg_raw =~ /^token:test\@pve!new:(\d+):/m;
+ ok(
+ defined($expire) && $expire >= $before + 86400 && $expire <= time() + 86400,
+ "create with $desc expiry stores the maximum lifetime as expiration date",
+ ) or diag("stored expire: " . ($expire // 'none'));
+}
+$reset->($defaulting);
+($ok, $err) = $invoke->('generate_token', { tokenid => 'new', expire => $now + 600 });
+ok($ok, 'an explicit expiry is kept under default-to-max-lifetime') or diag($err);
+like($user_cfg_raw, qr/^token:test\@pve!new:${\($now + 600)}:/m, 'the explicit expiry is stored');
+$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');
+
done_testing();
diff --git a/src/test/token-policy-test.pl b/src/test/token-policy-test.pl
index d29c781..f43fc8c 100644
--- a/src/test/token-policy-test.pl
+++ b/src/test/token-policy-test.pl
@@ -239,4 +239,21 @@ check(
0,
);
+# default-to-max-lifetime only yields a default with a maximum lifetime to derive it from
+my $default_expire = sub {
+ my ($policy) = @_;
+ return PVE::AccessControl::token_policy_default_expire({
+ ctime => $now,
+ policy => $policy,
+ });
+};
+is(
+ $default_expire->({ 'max-lifetime' => $max, 'default-to-max-lifetime' => 1 }),
+ $now + $max,
+ 'default-to-max-lifetime yields now plus the maximum lifetime',
+);
+is($default_expire->({ 'max-lifetime' => $max }), undef, 'no default without opting in');
+is($default_expire->({ 'default-to-max-lifetime' => 1 }), undef, 'no default without a maximum');
+is($default_expire->(undef), undef, 'no default without a policy');
+
done_testing();
--
2.47.3
next prev parent reply other threads:[~2026-11-15 19:42 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 ` Thomas Lamprecht [this message]
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 ` [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-6-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox