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 7DE801FF0AA for ; Tue, 17 Nov 2026 14:30:12 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B763521672; Tue, 17 Nov 2026 14:30:01 +0100 (CET) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 17 Nov 2026 14:29:56 +0100 Message-Id: Subject: Re: [PATCH v2 proxmox 16/19] access-control: add API token policy type with expiry checks To: "Thomas Lamprecht" , X-Mailer: aerc 0.20.0 References: <20261115194043.3947825-1-t.lamprecht@proxmox.com> <20261115194043.3947825-17-t.lamprecht@proxmox.com> In-Reply-To: <20261115194043.3947825-17-t.lamprecht@proxmox.com> From: "Shannon Sterz" X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794922196642 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.590 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: P7JAI3CZDUCFWCM2A6K55QRZB6ZMHUFZ X-Message-ID-Hash: P7JAI3CZDUCFWCM2A6K55QRZB6ZMHUFZ X-MailFrom: s.sterz@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: On Sun Nov 15, 2026 at 8:40 PM CET, Thomas Lamprecht wrote: > Counterpart of the datacenter-wide token-policy option introduced > for PVE with #7805, so that products building on this crate can > centrally require expiration dates for API tokens, limit the maximum > token lifetime, and forbid extending the expiration date of existing > tokens. Compliance rules like PCI DSS, SOC 2, or ISO 27001 often > require such limits. > > Products opt in by overriding the new AccessControlConfig method to > return the policy from their configuration; by default, no policy is > enforced. Existing tokens are not re-validated, and an update only > gets checked if it changes the expiration date, so opting into a > policy neither invalidates deployed tokens nor blocks unrelated edits > of them. Only the expiry settings are ported: tokens in this stack > always use separate ACLs, so PVE's privilege separation setting only > matters once PVE moves over to this implementation. > > Signed-off-by: Thomas Lamprecht > --- > > changes since v1: > - adapt to 'disallow-expiry-extension' > - split the check into one for new tokens and one for updates, letting > an unchanged date pass > - use u64 for the maximum lifetime > - document that token_policy() is called with the user config locked > > proxmox-access-control/src/init.rs | 14 ++ > proxmox-access-control/src/types.rs | 215 ++++++++++++++++++++++++++++ > 2 files changed, 229 insertions(+) > > diff --git a/proxmox-access-control/src/init.rs b/proxmox-access-control/= src/init.rs > index 52c3393a..c453c55f 100644 > --- a/proxmox-access-control/src/init.rs > +++ b/proxmox-access-control/src/init.rs > @@ -6,6 +6,8 @@ use anyhow::{Error, format_err}; > use proxmox_auth_api::types::{Authid, Userid}; > use proxmox_section_config::SectionConfigData; > > +use crate::types::TokenPolicy; > + > static ACCESS_CONF: OnceLock<&'static dyn AccessControlConfig> =3D OnceL= ock::new(); > > /// This trait specifies the functions a product needs to implement to g= et ACL tree based access > @@ -101,6 +103,18 @@ pub trait AccessControlConfig: Send + Sync { > fn allow_partial_permission_match(&self) -> bool { > true > } > + > + /// Returns the token policy to enforce when API tokens are created = or updated, if any. > + /// > + /// Override this to return the policy from the product's configurat= ion. Return an error if that > + /// configuration cannot be loaded, so token creation and expiration= date updates fail instead > + /// of silently proceeding without the policy. The token API calls t= his while holding the user > + /// configuration lock, so implementations must not acquire that loc= k. > + /// > + /// Default: Returns `Ok(None)`, no policy is enforced. > + fn token_policy(&self) -> Result, Error> { > + Ok(None) > + } > } > > pub fn init_access_config(config: &'static dyn AccessControlConfig) -> R= esult<(), Error> { > diff --git a/proxmox-access-control/src/types.rs b/proxmox-access-control= /src/types.rs > index 875b3d93..ce117376 100644 > --- a/proxmox-access-control/src/types.rs > +++ b/proxmox-access-control/src/types.rs > @@ -1,3 +1,4 @@ > +use anyhow::{Error, bail}; > use serde::{Deserialize, Serialize}; > > use const_format::concatcp; > @@ -174,6 +175,95 @@ pub struct TokenApiEntry { > pub token: ApiToken, > } > > +#[api( > + properties: { > + "require-expiry": { > + optional: true, > + default: false, > + }, > + "max-lifetime": { > + optional: true, > + minimum: 1, > + }, > + "disallow-expiry-extension": { > + optional: true, > + default: false, > + }, > + }, > +)] > +#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize,= Updater)] > +#[serde(rename_all =3D "kebab-case")] > +/// Policy for the creation and update of API tokens. > +/// > +/// Only token creation and updates are checked against the policy. Exis= ting tokens stay valid, so > +/// enabling a policy does not invalidate already deployed tokens. > +pub struct TokenPolicy { > + /// Require an expiration date for new API tokens and when changing = the expiration date of > + /// existing ones. > + #[serde(default, skip_serializing_if =3D "Option::is_none")] > + pub require_expiry: Option, > + > + /// Maximum lifetime of API tokens in seconds, counted from when the= expiration date is set, > + /// that is on creation or when an update changes it. Implies `requi= re-expiry`. > + #[serde(default, skip_serializing_if =3D "Option::is_none")] > + pub max_lifetime: Option, > + > + /// Disallow moving the expiration date of existing API tokens later= or removing it, so that > + /// `max-lifetime` cannot be circumvented by extending tokens repeat= edly. Shortening it, or > + /// setting one for a token without, stays possible, and tokens can = still be deleted and > + /// recreated. > + #[serde(default, skip_serializing_if =3D "Option::is_none")] > + pub disallow_expiry_extension: Option, > +} > + > +impl TokenPolicy { > + /// Check the expiration date of a new token against the policy, 0 m= eaning none. > + pub fn check_new_expiry(&self, expire: i64, now: i64) -> Result<(), = Error> { > + self.check_expiry_bounds(expire, now) > + } > + > + /// Check an update of the expiration date of an existing token agai= nst the policy. > + /// > + /// `expire` is the submitted and `current` the stored expiration da= te, 0 meaning none for both. > + /// An unchanged date always passes, so updates of tokens that preda= te the policy keep working. > + pub fn check_expiry_update(&self, expire: i64, current: i64, now: i6= 4) -> Result<(), Error> { > + if expire =3D=3D current { > + return Ok(()); > + } > + > + // removing the expiration date or moving it later extends the t= oken > + if self.disallow_expiry_extension.unwrap_or_default() > + && current !=3D 0 > + && (expire =3D=3D 0 || expire > current) nit: here ... > + { > + bail!( > + "the token policy does not allow extending the expiratio= n date of an existing token" > + ); > + } > + > + self.check_expiry_bounds(expire, now) > + } > + > + fn check_expiry_bounds(&self, expire: i64, now: i64) -> Result<(), E= rror> { > + // a token that never expires exceeds any maximum lifetime > + if expire =3D=3D 0 && (self.require_expiry.unwrap_or_default() |= | self.max_lifetime.is_some()) { and here, might want to do a `expire <=3D 0`. while the api schema should forbid expire to be negative, if anything calls the update function directly that may not be enforced. to protect to this scenario and match the behavior of the api endpoint check for `<=3D 0` here. -->8 snip 8<--