From 05bfb6506629f39aa2d920f1bfe06009fe7576df Mon Sep 17 00:00:00 2001 From: John Coffey Date: Sat, 7 Nov 2026 23:45:31 -0700 Subject: [PATCH] Delete a domain's DKIM keys with it; point kept accounts at DeletedAccount A domain with automatic DKIM couldn't be deleted: the keys it generated refer to it, so the delete came back objectIsLinked until someone found and removed them by hand. When those keys are the only things still referring to the domain, they now go first and then the domain. Anything else that's linked leaves them in place and the delete is refused as before. objectIsLinked now has a description naming what's in the way, for clients that only show the description. Cancelling a DestroyAccount task is still refused, but the message no longer says recovery isn't supported. It points to inbuxa:DeletedAccount/set, which restores a kept account or destroys it now. Fixes #313 --- crates/jmap/src/registry/mapping/task.rs | 7 ++- crates/jmap/src/registry/set.rs | 75 +++++++++++++++++++++++- tests/src/system/tenant.rs | 25 +++++++- tests/src/system/undelete.rs | 20 +++++++ 4 files changed, 120 insertions(+), 7 deletions(-) diff --git a/crates/jmap/src/registry/mapping/task.rs b/crates/jmap/src/registry/mapping/task.rs index 9dd00f16..671d8a28 100644 --- a/crates/jmap/src/registry/mapping/task.rs +++ b/crates/jmap/src/registry/mapping/task.rs @@ -297,8 +297,13 @@ pub(crate) async fn task_set( if let Task::DestroyAccount(_) = task { set.response.not_destroyed.append( id, + // inbuxa: #313: a kept account is restored or destroyed + // through its own object, which keeps this task in step SetError::forbidden().with_description( - "Account recovery is not supported in this deployment".to_string(), + "An account's destroy task can't be cancelled. If the account \ + is still being kept, restore it or destroy it now with \ + inbuxa:DeletedAccount/set." + .to_string(), ), ); continue; diff --git a/crates/jmap/src/registry/set.rs b/crates/jmap/src/registry/set.rs index d081581f..8c562280 100644 --- a/crates/jmap/src/registry/set.rs +++ b/crates/jmap/src/registry/set.rs @@ -930,6 +930,12 @@ impl RegistrySet for Server { .append(id, build_set_error(missing)); continue; } + // inbuxa: #313: a domain's own DKIM keys go with it, + // but only when nothing else holds the domain, so a + // delete that would be refused anyway loses nothing + if matches!(object.inner, ObjectInner::Domain(_)) { + release_domain_keys(self, object_id, &mut cache_invalidator).await?; + } // inbuxa: personal-data catalog: a tenant's compliance // role, while nobody holds it, goes first let role_released = matches!(object.inner, ObjectInner::Tenant(_)) @@ -1259,14 +1265,77 @@ impl Modification { } } +/// inbuxa: #313: deletes a domain's DKIM keys when they're the only objects +/// that still refer to it. Keys sign for that one domain, so they're no use +/// once it's gone; anything else that's linked leaves them in place and the +/// domain's own delete is refused as before. +async fn release_domain_keys( + server: &Server, + domain_id: ObjectId, + cache_invalidator: &mut CacheInvalidationBuilder, +) -> trc::Result<()> { + let linked = server + .registry() + .linked_objects(domain_id) + .await + .caused_by(trc::location!())?; + if linked.is_empty() + || linked + .iter() + .any(|linked| linked.object() != ObjectType::DkimSignature) + { + return Ok(()); + } + + for key_id in linked { + let Some(key) = server + .registry() + .get(key_id) + .await + .caused_by(trc::location!())? + else { + continue; + }; + if let RegistryWriteResult::Success(_) = server + .registry() + .write(RegistryWrite::delete_object(key_id, &key)) + .await? + { + cache_invalidator.process_delete(key_id.id(), &key); + } + } + Ok(()) +} + pub(crate) fn map_write_error(err: RegistryWriteResult) -> SetError { match err { RegistryWriteResult::CannotDeleteLinked { object_id, linked_objects, - } => SetError::new(SetErrorType::ObjectIsLinked) - .with_object_id(object_id) - .with_linked_objects(linked_objects), + } => { + // inbuxa: #313: say what's in the way, for clients that only + // show the description + let mut counts: Vec<(ObjectType, usize)> = Vec::new(); + for linked in &linked_objects { + match counts.iter_mut().find(|(kind, _)| *kind == linked.object()) { + Some((_, count)) => *count += 1, + None => counts.push((linked.object(), 1)), + } + } + let in_use = counts + .iter() + .map(|(kind, count)| { + format!("{count} x:{}", registry::types::EnumImpl::as_str(kind)) + }) + .collect::>() + .join(", "); + SetError::new(SetErrorType::ObjectIsLinked) + .with_description(format!( + "It's still used by {in_use}. Remove or change those first." + )) + .with_object_id(object_id) + .with_linked_objects(linked_objects) + } RegistryWriteResult::InvalidSingletonId => SetError::invalid_properties() .with_property(Property::Id) .with_description("Invalid singleton id"), diff --git a/tests/src/system/tenant.rs b/tests/src/system/tenant.rs index 5b190ec9..2079e788 100644 --- a/tests/src/system/tenant.rs +++ b/tests/src/system/tenant.rs @@ -469,10 +469,29 @@ pub async fn test(test: &mut TestServer) { "MT-9" ); } - admin - .registry_destroy(ObjectType::DkimSignature, keys) + // #313: while someone else uses the domain it stays, keys and all, and + // the refusal says what's in the way + let k_user = admin + .create_user_account("kuser@k.example.org", SECRET_T_USER, "K user", &[], vec![]) .await; - admin.registry_destroy(ObjectType::Domain, [k_domain]).await; + admin + .registry_destroy_object_expect_err(ObjectType::Domain, k_domain) + .await + .assert_type(SetErrorType::ObjectIsLinked) + .assert_description_contains("1 x:Account"); + assert_eq!( + sorted(admin.dkim_keys_of(k_domain).await), + sorted(keys), + "#313" + ); + admin.destroy_account(k_user).await; + test.wait_for_tasks().await; + // ... and once only its own DKIM keys refer to it, they go with it + admin + .registry_destroy(ObjectType::Domain, [k_domain]) + .await + .assert_destroyed(&[k_domain]); + assert!(admin.dkim_keys_of(k_domain).await.is_empty(), "#313"); // Acceptance test 8: a tenant that's still referenced can't be deleted admin diff --git a/tests/src/system/undelete.rs b/tests/src/system/undelete.rs index 3ec6486f..176b4a90 100644 --- a/tests/src/system/undelete.rs +++ b/tests/src/system/undelete.rs @@ -23,6 +23,7 @@ use email::{ use hyper::StatusCode; use imap_proto::ResponseType; use jmap_client::client::{Client, Credentials}; +use jmap_proto::error::set::SetErrorType; use registry::{ schema::{ prelude::{ObjectType, Property}, @@ -541,6 +542,25 @@ pub async fn test(test: &mut TestServer) { pending_destroy(test, gone_id).await, "test 14: destruction due" ); + // #313: its task can't be cancelled, and the refusal says where to go + let tasks = admin + .jmap_method_call("x:Task/get", json!({"ids": null})) + .await; + let task_id = tasks + .list() + .iter() + .find(|task| task["@type"] == "DestroyAccount" && task["accountId"] == gone_id.to_string()) + .unwrap_or_else(|| panic!("#313: no DestroyAccount task in {tasks:?}"))["id"] + .as_str() + .unwrap() + .parse::() + .unwrap(); + admin + .registry_destroy_object_expect_err(ObjectType::Task, task_id) + .await + .assert_type(SetErrorType::Forbidden) + .assert_description_contains("inbuxa:DeletedAccount/set"); + assert!(pending_destroy(test, gone_id).await, "#313: still due"); // Its name is held (UD-16) let domain_id = admin -- 2.54.0