From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id AD1901FF09D for ; Sun, 15 Nov 2026 20:43:13 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 4F3AD218BC; Sun, 15 Nov 2026 20:41:10 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v2 proxmox 18/19] access-control: apply a default token lifetime from the policy Date: Sun, 15 Nov 2026 20:40:39 +0100 Message-ID: <20261115194043.3947825-19-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: 1794771650304 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.226 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) 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: HPK2SEDHFBE5XSWC55LG4T3TCA2F6J4A X-Message-ID-Hash: HPK2SEDHFBE5XSWC55LG4T3TCA2F6J4A 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: Counterpart of the new 'default-to-max-lifetime' token policy setting in PVE: tokens created without an expiration date get the maximum lifetime when the policy opts into that, instead of being rejected, so tools unaware of the policy still get a conforming token. Updates are not affected, removing an expiration date there stays rejected. Suggested-by: Shannon Sterz Signed-off-by: Thomas Lamprecht --- new in v2 proxmox-access-control/src/api/tokens.rs | 14 ++++++-- proxmox-access-control/src/types.rs | 44 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/proxmox-access-control/src/api/tokens.rs b/proxmox-access-control/src/api/tokens.rs index e2d144eb..0ddcfdd3 100644 --- a/proxmox-access-control/src/api/tokens.rs +++ b/proxmox-access-control/src/api/tokens.rs @@ -97,10 +97,20 @@ pub fn generate_token( ); } + let policy = crate::init::access_conf().token_policy()?; + let now = proxmox_time::epoch_i64(); + + let expire = match expire { + Some(expire) if expire > 0 => Some(expire), + _ => policy + .and_then(|policy| policy.default_expire(now)) + .or(expire), + }; + // check the policy before generating the secret to avoid leaving an orphaned entry in the // token shadow file on rejection - if let Some(policy) = crate::init::access_conf().token_policy()? { - policy.check_new_expiry(expire.unwrap_or(0), proxmox_time::epoch_i64())?; + if let Some(policy) = policy { + policy.check_new_expiry(expire.unwrap_or(0), now)?; } let secret = token_shadow::generate_and_set_secret(&tokenid)?; diff --git a/proxmox-access-control/src/types.rs b/proxmox-access-control/src/types.rs index ce117376..1377bc50 100644 --- a/proxmox-access-control/src/types.rs +++ b/proxmox-access-control/src/types.rs @@ -189,6 +189,10 @@ pub struct TokenApiEntry { optional: true, default: false, }, + "default-to-max-lifetime": { + optional: true, + default: false, + }, }, )] #[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize, Updater)] @@ -214,9 +218,27 @@ pub struct TokenPolicy { /// recreated. #[serde(default, skip_serializing_if = "Option::is_none")] pub disallow_expiry_extension: Option, + + /// Set the expiration date of API tokens created without one to the maximum lifetime instead + /// of rejecting them, for example for tools unaware of the policy. Has no effect without + /// `max-lifetime`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub default_to_max_lifetime: Option, } impl TokenPolicy { + /// The expiration date for a token created without one: `now` plus the maximum lifetime if the + /// policy opts into defaulting, `None` otherwise. + pub fn default_expire(&self, now: i64) -> Option { + if self.default_to_max_lifetime.unwrap_or_default() + && let Some(max_lifetime) = self.max_lifetime + { + Some(now.saturating_add_unsigned(max_lifetime)) + } else { + None + } + } + /// Check the expiration date of a new token against the policy, 0 meaning none. pub fn check_new_expiry(&self, expire: i64, now: i64) -> Result<(), Error> { self.check_expiry_bounds(expire, now) @@ -422,6 +444,7 @@ mod tests { require_expiry: None, max_lifetime: None, disallow_expiry_extension: None, + default_to_max_lifetime: None, }; const REQUIRE_EXPIRY: TokenPolicy = TokenPolicy { @@ -499,6 +522,27 @@ mod tests { assert!(update_ok(&MAX_LIFETIME, NOW + MAX + 1, NOW + MAX + 1)); } + #[test] + fn default_to_max_lifetime() { + let policy = TokenPolicy { + default_to_max_lifetime: Some(true), + ..MAX_LIFETIME + }; + assert_eq!(policy.default_expire(NOW), Some(NOW + MAX)); + // only with a maximum lifetime to derive the default from, and only when opted into + assert_eq!(MAX_LIFETIME.default_expire(NOW), None); + let without_maximum = TokenPolicy { + default_to_max_lifetime: Some(true), + ..EMPTY + }; + assert_eq!(without_maximum.default_expire(NOW), None); + let extreme = TokenPolicy { + max_lifetime: Some(u64::MAX), + ..policy + }; + assert_eq!(extreme.default_expire(NOW), Some(i64::MAX)); + } + #[test] fn disallow_expiry_extension() { // moving the expiration date later or removing it is rejected -- 2.47.3