public inbox for pve-devel@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 18/19] access-control: apply a default token lifetime from the policy
Date: Sun, 15 Nov 2026 20:40:39 +0100	[thread overview]
Message-ID: <20261115194043.3947825-19-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261115194043.3947825-1-t.lamprecht@proxmox.com>

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 <s.sterz@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---

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<bool>,
+
+    /// 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<bool>,
 }
 
 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<i64> {
+        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





  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 ` Thomas Lamprecht [this message]
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-19-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal