From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 369B71FF09D for ; Sun, 15 Nov 2026 20:42:04 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id BDD5C21751; Sun, 15 Nov 2026 20:41:04 +0100 (CET) From: Thomas Lamprecht 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 Message-ID: <20261115194043.3947825-6-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261115194043.3947825-1-t.lamprecht@proxmox.com> References: <20261115194043.3947825-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794771649324 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.078 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) KAM_SHORT 0.001 Use of a URL Shortener for very short URL POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes 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 Message-ID-Hash: QYOAFGUTX66PF2F4Y6ESXU3DZ2LRPUW5 X-Message-ID-Hash: QYOAFGUTX66PF2F4Y6ESXU3DZ2LRPUW5 X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 Signed-off-by: Thomas Lamprecht --- 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