Keep an account's disk quota within its tenant's #494

Merged
jcoffey-dev merged 1 commits from fix/tenant-disk-quota into main 2026-11-20 01:54:25 +00:00
7 changed files with 255 additions and 53 deletions

No files matched your search

+29
View File
@@ -92,6 +92,35 @@ impl Server {
Ok(())
}
// inbuxa: the disk space an account can really use, for reporting it to
// clients (JMAP Quota, IMAP QUOTA, DAV quota properties). Returns what
// the account uses and, when any limit applies, the limit it will
// actually meet: its own quota or its tenant's remaining space,
// whichever comes first (MT-19). Without this a 50 GB account in a 5 GB
// tenant shows 45 GB free while its mail is refused.
pub async fn effective_disk_quota(
&self,
account: &AccountCache,
) -> trc::Result<(u64, Option<u64>)> {
let used = self.get_used_quota_account(account.id).await?.max(0) as u64;
let mut available =
(account.quota_disk > 0).then(|| account.quota_disk.saturating_sub(used));
if let Some(tenant_id) = account.id_tenant {
let tenant = self.tenant(tenant_id).await?;
if tenant.quota_disk > 0 {
let tenant_used = self.get_used_quota_tenant(tenant_id).await?.max(0) as u64;
let tenant_available = tenant.quota_disk.saturating_sub(tenant_used);
available = Some(available.map_or(tenant_available, |a| a.min(tenant_available)));
}
}
Ok((
used,
available.map(|available| used.saturating_add(available)),
))
}
// inbuxa: checks an item against the account's quota and, when it fits,
// counts it towards the rest of the request
pub async fn reserve_quota(&self, budget: &mut QuotaBudget, item_size: u64) -> trc::Result<()> {
+7 -24
View File
@@ -1194,32 +1194,15 @@ impl PropFindRequestHandler for Server {
async fn dav_quota(&self, account_id: u32) -> trc::Result<PropFindAccountQuota> {
let account = self.account(account_id).await.caused_by(trc::location!())?;
let used = self
.get_used_quota_account(account_id)
let (used, limit) = self
.effective_disk_quota(&account)
.await
.caused_by(trc::location!())?
.max(0) as u64;
let mut available =
(account.quota_disk > 0).then(|| account.quota_disk.saturating_sub(used));
.caused_by(trc::location!())?;
if let Some(tenant_id) = account.id_tenant {
let tenant = self.tenant(tenant_id).await.caused_by(trc::location!())?;
if tenant.quota_disk > 0 {
let tenant_used = self
.get_used_quota_tenant(tenant_id)
.await
.caused_by(trc::location!())?
.max(0) as u64;
let tenant_available = tenant.quota_disk.saturating_sub(tenant_used);
available = Some(available.map_or(tenant_available, |available| {
available.min(tenant_available)
}));
}
}
Ok(PropFindAccountQuota { used, available })
Ok(PropFindAccountQuota {
used,
available: limit.map(|limit| limit.saturating_sub(used)),
})
}
}
#[allow(clippy::too_many_arguments)]
+98 -1
View File
@@ -13,6 +13,8 @@
//! - MT-17: creating an object, or moving a domain in, stays within the
//! tenant's count limits, and the server emits `limit.tenant-quota` when
//! it doesn't.
//! - An account's disk quota fits within its tenant's (inbuxa-admin#257),
//! checked by `check_disk_quota` on JMAP and SCIM writes.
use crate::tenancy::{
domain_move::{self, Move, Refusal},
@@ -23,7 +25,7 @@ use ahash::AHashSet;
use jmap_proto::error::set::{SetError, SetErrorType};
use registry::{
schema::{
enums::TenantStorageQuota,
enums::{StorageQuota, TenantStorageQuota},
prelude::{Object, ObjectInner, Property},
structs::{Account, DkimManagement, Domain, Tenant},
},
@@ -63,6 +65,19 @@ pub async fn default_tenant(registry: &RegistryStore, object: &mut Object) -> tr
Ok(())
}
/// An account's own disk quota, `None` when it has none (0 is none too).
fn account_disk_quota(object: &ObjectInner) -> Option<u64> {
let quotas = match object {
ObjectInner::Account(Account::User(obj)) => &obj.quotas,
ObjectInner::Account(Account::Group(obj)) => &obj.quotas,
_ => return None,
};
quotas
.get(&StorageQuota::MaxDiskQuota)
.copied()
.filter(|quota| *quota > 0)
}
/// A tenant's count limit, `None` when it has none.
async fn limits(
registry: &RegistryStore,
@@ -146,6 +161,35 @@ pub async fn check(
Ok(Ok(after))
}
/// inbuxa-admin#257: an account's disk quota fits within its tenant's, so no
/// member gets more space than the whole tenant has. Run on a JMAP or SCIM
/// write, after `check`. Checked when the quota is set or changed, or the
/// account changes tenant; an account already over a lowered tenant limit
/// keeps its quota until someone changes it, like the count limits (MT-18).
/// No quota at all is fine: the account shares the tenant's space. Not run
/// for accounts a directory creates at sign-in or for a restored account,
/// which would otherwise be refused for a setting nobody can edit first.
pub async fn check_disk_quota(
registry: &RegistryStore,
old: Option<&Object>,
new: &Object,
) -> trc::Result<Result<(), SetError<Property>>> {
let tenant = new.inner.member_tenant_id();
if let Some(tenant_id) = tenant
&& let Some(quota) = account_disk_quota(&new.inner)
&& old.is_none_or(|old| {
account_disk_quota(&old.inner) != Some(quota) || old.inner.member_tenant_id() != tenant
})
&& let Some(tenant_quota) =
(limits(registry, tenant_id).await?)(TenantStorageQuota::MaxDiskQuota)
.filter(|limit| *limit > 0)
&& quota > tenant_quota
{
return Ok(Err(over_tenant_disk_quota(quota, tenant_quota)));
}
Ok(Ok(()))
}
/// Finishes a checked write once it's saved. Returns each other object it
/// changed, as it was and as it is now, for cache invalidation.
pub async fn after_save(
@@ -196,6 +240,59 @@ fn over_quota(reached: LimitReached, tenant_id: Id) -> SetError<Property> {
))
}
/// Refuses an account disk quota bigger than its tenant's (inbuxa-admin#257).
fn over_tenant_disk_quota(quota: u64, tenant_quota: u64) -> SetError<Property> {
SetError::invalid_properties()
.with_property(Property::Quotas)
.with_description(format!(
"The account's disk quota of {quota} bytes ({}) is more than its tenant's \
maxDiskQuota of {tenant_quota} bytes ({}). Set it to {tenant_quota} or less, \
or leave it empty to share the tenant's space.",
readable_size(quota),
readable_size(tenant_quota),
))
}
/// A byte count in binary units, for people reading an error: "50 GiB".
fn readable_size(bytes: u64) -> String {
const UNITS: [&str; 5] = ["bytes", "KiB", "MiB", "GiB", "TiB"];
let mut value = bytes as f64;
let mut unit = 0;
while value >= 1024.0 && unit < UNITS.len() - 1 {
value /= 1024.0;
unit += 1;
}
if unit == 0 || value.fract() == 0.0 {
format!("{value:.0} {}", UNITS[unit])
} else {
format!("{value:.1} {}", UNITS[unit])
}
}
#[cfg(test)]
mod disk_quota_tests {
use super::*;
#[test]
fn readable_sizes() {
assert_eq!(readable_size(512), "512 bytes");
assert_eq!(readable_size(5 * 1024 * 1024 * 1024), "5 GiB");
assert_eq!(readable_size(1536 * 1024 * 1024), "1.5 GiB");
}
#[test]
fn zero_quota_is_no_quota() {
let user = |quota: u64| {
ObjectInner::Account(Account::User(registry::schema::structs::UserAccount {
quotas: [(StorageQuota::MaxDiskQuota, quota)].into_iter().collect(),
..Default::default()
}))
};
assert_eq!(account_disk_quota(&user(0)), None);
assert_eq!(account_disk_quota(&user(10)), Some(10));
}
}
fn refused_move(refusal: Refusal, tenant: Option<Id>) -> SetError<Property> {
match refusal {
Refusal::Across => SetError::invalid_properties()
+18 -18
View File
@@ -109,20 +109,20 @@ impl<T: SessionStream> SessionData<T> {
.account(account_id)
.await
.imap_ctx(&arguments.tag, trc::location!())?;
let used_quota = self
// inbuxa: the limit the account will really meet, its own quota or
// its tenant's remaining space (inbuxa-admin#257)
let (used_quota, disk_limit) = self
.server
.get_used_quota_account(account_id)
.effective_disk_quota(&account)
.await
.imap_ctx(&arguments.tag, trc::location!())?;
let disk_limit = disk_limit.unwrap_or_default();
trc::event!(
Imap(trc::ImapEvent::GetQuota),
SpanId = self.session_id,
Id = arguments.name.clone(),
Details = vec![
trc::Value::from(used_quota),
trc::Value::from(account.disk_quota())
],
Details = vec![trc::Value::from(used_quota), trc::Value::from(disk_limit)],
Elapsed = op_start.elapsed()
);
@@ -131,11 +131,11 @@ impl<T: SessionStream> SessionData<T> {
quota_root_items: vec![],
quota_items: vec![QuotaItem {
name: arguments.name,
resources: if account.disk_quota() > 0 {
resources: if disk_limit > 0 {
vec![QuotaResource {
resource: QuotaResourceName::Storage,
total: account.disk_quota(),
used: used_quota as u64,
total: disk_limit,
used: used_quota,
}]
} else {
vec![]
@@ -173,20 +173,20 @@ impl<T: SessionStream> SessionData<T> {
.account(account_id)
.await
.imap_ctx(&arguments.tag, trc::location!())?;
let used_quota = self
// inbuxa: the limit the account will really meet, its own quota or
// its tenant's remaining space (inbuxa-admin#257)
let (used_quota, disk_limit) = self
.server
.get_used_quota_account(account_id)
.effective_disk_quota(&account)
.await
.imap_ctx(&arguments.tag, trc::location!())?;
let disk_limit = disk_limit.unwrap_or_default();
trc::event!(
Imap(trc::ImapEvent::GetQuota),
SpanId = self.session_id,
MailboxName = arguments.name.clone(),
Details = vec![
trc::Value::from(used_quota),
trc::Value::from(account.disk_quota())
],
Details = vec![trc::Value::from(used_quota), trc::Value::from(disk_limit)],
Elapsed = op_start.elapsed()
);
@@ -195,11 +195,11 @@ impl<T: SessionStream> SessionData<T> {
quota_root_items: vec![arguments.name, format!("#{account_id}")],
quota_items: vec![QuotaItem {
name: format!("#{account_id}"),
resources: if account.disk_quota() > 0 {
resources: if disk_limit > 0 {
vec![QuotaResource {
resource: QuotaResourceName::Storage,
total: account.disk_quota(),
used: used_quota as u64,
total: disk_limit,
used: used_quota,
}]
} else {
vec![]
+11 -5
View File
@@ -2,6 +2,8 @@
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*
* Modified by Coffey Labs in 2026 for INBUXA.
*/
use common::{Server, auth::AccessToken};
@@ -44,7 +46,13 @@ impl QuotaGet for Server {
]);
let account_id = request.account_id.document_id();
let account = self.account(account_id).await.caused_by(trc::location!())?;
let quota_ids = if account.disk_quota() > 0 {
// inbuxa: report the limit the account will really meet, its own
// quota or its tenant's remaining space (inbuxa-admin#257)
let (used, hard_limit) = self
.effective_disk_quota(&account)
.await
.caused_by(trc::location!())?;
let quota_ids = if hard_limit.is_some() {
vec![0u32]
} else {
vec![]
@@ -80,10 +88,8 @@ impl QuotaGet for Server {
let value = match property {
QuotaProperty::Id => Value::Element(id.into()),
QuotaProperty::ResourceType => "octets".to_string().into(),
QuotaProperty::Used => {
(self.get_used_quota_account(account_id).await?.max(0) as u64).into()
}
QuotaProperty::HardLimit => account.as_ref().disk_quota().into(),
QuotaProperty::Used => used.into(),
QuotaProperty::HardLimit => hard_limit.unwrap_or_default().into(),
QuotaProperty::Scope => "account".to_string().into(),
QuotaProperty::Name => account.as_ref().name().to_string().into(),
QuotaProperty::Description => account
+13
View File
@@ -670,6 +670,19 @@ impl RegistrySet for Server {
}
};
// inbuxa: inbuxa-admin#257: an account's disk quota fits
// within its tenant's
if let Err(err) = inbuxa_features::tenancy::writes::check_disk_quota(
self.registry(),
stored,
&new_object,
)
.await?
{
set.failed(modification, err);
continue 'outer;
}
// inbuxa: BT-3, BT-15, BT-22: logos and templates follow their rules
let before = match &modification {
Modification::Update { object, .. }
+79 -5
View File
@@ -23,7 +23,8 @@ use jmap_proto::error::set::SetErrorType;
use registry::{
schema::{
enums::{
Permission, TaskStoreMaintenanceType, TaskTenantMaintenanceType, TenantStorageQuota,
Permission, StorageQuota, TaskStoreMaintenanceType, TaskTenantMaintenanceType,
TenantStorageQuota,
},
prelude::{ObjectType, Property},
structs::{
@@ -849,17 +850,20 @@ pub async fn test(test: &mut TestServer) {
// Acceptance test 13: the tenant's disk limit bounds all its members
// together (MT-19, MT-20)
let d_id = admin.create_tenant("Tenant D").await;
admin
let d_domain = admin
.create_tenant_domain("d.example.org", Some(d_id))
.await;
let d_admin = admin
.create_user_account("[email protected]", SECRET_D, "D admin", &[], vec![])
.await;
admin.make_tenant_admin(d_admin.id()).await;
let mut d_users = Vec::new();
for name in ["[email protected]", "[email protected]", "[email protected]"] {
admin
.create_user_account(name, SECRET_D, "D user", &[], vec![])
.await;
d_users.push(
admin
.create_user_account(name, SECRET_D, "D user", &[], vec![])
.await,
);
}
let message = concat!(
"From: [email protected]\r\n",
@@ -958,6 +962,76 @@ pub async fn test(test: &mut TestServer) {
.registry_destroy(ObjectType::QueuedMessage, queued)
.await;
// inbuxa-admin#257: an account's disk quota can't exceed its tenant's,
// whoever sets it
let d_limit = (two + one / 2) as u64;
let with_quota = |name: &str, quota: u64| {
structs::Account::User(structs::UserAccount {
name: name.to_string(),
domain_id: d_domain,
quotas: VecMap::from_iter([(StorageQuota::MaxDiskQuota, quota)]),
..Default::default()
})
};
for writer in [&d_admin, &admin] {
writer
.registry_create_object_expect_err(with_quota("d4", d_limit * 10))
.await
.assert_type(SetErrorType::InvalidProperties)
.assert_description_contains("maxDiskQuota");
}
let d4 = d_admin
.registry_create_object(with_quota("d4", d_limit))
.await;
d_admin
.registry_update_object_expect_err(
ObjectType::Account,
d4,
json!({ Property::Quotas: VecMap::from_iter([(StorageQuota::MaxDiskQuota, d_limit + 1)]) }),
)
.await
.assert_type(SetErrorType::InvalidProperties);
// Lowering the tenant's limit leaves the account's quota alone, and
// other changes to it still save (MT-18)
admin
.set_tenant_quota(d_id, TenantStorageQuota::MaxDiskQuota, d_limit - 1)
.await;
d_admin
.registry_update_object(
ObjectType::Account,
d4,
json!({ Property::Description: "still here" }),
)
.await;
admin
.set_tenant_quota(d_id, TenantStorageQuota::MaxDiskQuota, d_limit)
.await;
admin
.registry_destroy(ObjectType::Account, [d4])
.await
.assert_destroyed(&[d4]);
test.wait_for_tasks().await;
// A member with no quota of its own sees the tenant's remaining space
// as its limit, the limit it would really meet
let response = d_users[0]
.jmap_method_call(
"Quota/get",
json!({ "accountId": d_users[0].id_string(), "ids": null }),
)
.await
.to_string();
let d1_used = test
.server
.get_used_quota_account(d_users[0].id().document_id())
.await
.unwrap()
.max(0) as u64;
let expected = d1_used + d_limit.saturating_sub(two as u64);
assert!(
response.contains(&format!("\"hardLimit\":{expected}")),
"inbuxa-admin#257: Quota/get {response}"
);
// Acceptance test 14: recalculateQuota restores a corrupted figure (MT-21)
test.corrupt_tenant_usage(d_id, 123_456).await;
assert_ne!(admin.tenant_usage(d_id).await, two);