A domain's queued tasks follow it: deleted, set to manual, or replaced #332
No files matched your search
@@ -13,7 +13,9 @@ use common::network::{dkim::generate_dkim_selector, dns::update::DnsUpdater};
|
||||
use jmap_proto::error::set::{SetError, SetErrorType};
|
||||
use registry::{
|
||||
schema::{
|
||||
enums::{AcmeChallengeType, DkimSignatureType, DnsRecordType, TenantStorageQuota},
|
||||
enums::{
|
||||
AcmeChallengeType, DkimSignatureType, DnsRecordType, TaskType, TenantStorageQuota,
|
||||
},
|
||||
prelude::{ObjectType, Property},
|
||||
structs::{
|
||||
AcmeProvider, CertificateManagement, DkimManagement, DkimManagementProperties,
|
||||
@@ -211,6 +213,46 @@ pub(crate) async fn validate_domain(
|
||||
Ok(Ok(response))
|
||||
}
|
||||
|
||||
/// inbuxa: the queued tasks a saved domain should lose (#327): those for
|
||||
/// something it stopped managing itself, and its DKIM task when the save
|
||||
/// queues a new one, which would otherwise run alongside it for good.
|
||||
pub(crate) fn domain_tasks_to_clear(
|
||||
domain: &Domain,
|
||||
old_domain: &Domain,
|
||||
tasks: &[Task],
|
||||
) -> Vec<TaskType> {
|
||||
let mut types = Vec::new();
|
||||
let stopped = |was: bool, is: bool| was && !is;
|
||||
if stopped(
|
||||
matches!(old_domain.dkim_management, DkimManagement::Automatic(_)),
|
||||
matches!(domain.dkim_management, DkimManagement::Automatic(_)),
|
||||
) || tasks
|
||||
.iter()
|
||||
.any(|task| matches!(task, Task::DkimManagement(_)))
|
||||
{
|
||||
types.push(TaskType::DkimManagement);
|
||||
}
|
||||
if stopped(
|
||||
matches!(old_domain.dns_management, DnsManagement::Automatic(_)),
|
||||
matches!(domain.dns_management, DnsManagement::Automatic(_)),
|
||||
) {
|
||||
types.push(TaskType::DnsManagement);
|
||||
}
|
||||
if stopped(
|
||||
matches!(
|
||||
old_domain.certificate_management,
|
||||
CertificateManagement::Automatic(_)
|
||||
),
|
||||
matches!(
|
||||
domain.certificate_management,
|
||||
CertificateManagement::Automatic(_)
|
||||
),
|
||||
) {
|
||||
types.push(TaskType::AcmeRenewal);
|
||||
}
|
||||
types
|
||||
}
|
||||
|
||||
/// inbuxa: whether a save changed what the domain's certificate is ordered
|
||||
/// for. The renewal task decides what to do about it: a certificate that
|
||||
/// already has these names just waits for its renewal date (#290).
|
||||
|
||||
@@ -15,7 +15,7 @@ use crate::registry::{
|
||||
action::action_set,
|
||||
bootstrap::bootstrap_set,
|
||||
dkim::validate_dkim_signature,
|
||||
domain::{validate_dns_server, validate_domain},
|
||||
domain::{domain_tasks_to_clear, validate_dns_server, validate_domain},
|
||||
map_bootstrap_error,
|
||||
principal::{
|
||||
AccountUpdate, build_set_error, schedule_account_destruction, validate_account,
|
||||
@@ -48,7 +48,7 @@ use jmap_tools::{JsonPointer, JsonPointerItem, Key};
|
||||
use registry::{
|
||||
jmap::{JmapValue, JsonPointerPatch, MaybeUnpatched, RegistryValue},
|
||||
schema::{
|
||||
enums::{Permission, TenantStorageQuota},
|
||||
enums::{Permission, TaskType, TenantStorageQuota},
|
||||
prelude::{
|
||||
OBJ_FILTER_ACCOUNT, OBJ_FILTER_TENANT, OBJ_SINGLETON, Object, ObjectInner, ObjectType,
|
||||
Property,
|
||||
@@ -60,7 +60,10 @@ use registry::{
|
||||
},
|
||||
types::id::ObjectId,
|
||||
};
|
||||
use services::task_manager::{domain_check::clear_queued_renewals, lock::TaskLockManager};
|
||||
use services::task_manager::{
|
||||
domain_check::{clear_queued_domain_tasks, clear_queued_renewals},
|
||||
lock::TaskLockManager,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
use store::{
|
||||
registry::{
|
||||
@@ -776,6 +779,20 @@ impl RegistrySet for Server {
|
||||
}
|
||||
};
|
||||
|
||||
// inbuxa: #327: the queued tasks this save leaves the domain
|
||||
// without, cleared below once it's written
|
||||
let cleared_types = match (&modification, &new_object.inner) {
|
||||
(Modification::Update { object, .. }, ObjectInner::Domain(domain)) => {
|
||||
match &object.inner {
|
||||
ObjectInner::Domain(old_domain) => {
|
||||
domain_tasks_to_clear(domain, old_domain, &tasks)
|
||||
}
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
_ => Vec::new(),
|
||||
};
|
||||
|
||||
let object_id = match (modification, result) {
|
||||
(Modification::Update { id, object }, RegistryWriteResult::Success(_)) => {
|
||||
cache_invalidator.process_update(id, &object, &new_object);
|
||||
@@ -863,14 +880,14 @@ impl RegistrySet for Server {
|
||||
};
|
||||
|
||||
// Dispatch tasks
|
||||
if !tasks.is_empty()
|
||||
if (!tasks.is_empty() || !cleared_types.is_empty())
|
||||
&& let Some(object_id) = object_id
|
||||
{
|
||||
let mut batch = BatchBuilder::new();
|
||||
|
||||
// inbuxa: a new renewal replaces the domain's queued
|
||||
// one, or both would keep renewing from here on (#290)
|
||||
let replaced = if tasks.iter().any(|task| {
|
||||
let mut replaced = if tasks.iter().any(|task| {
|
||||
matches!(
|
||||
task,
|
||||
Task::AcmeRenewal(_)
|
||||
@@ -884,6 +901,15 @@ impl RegistrySet for Server {
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
replaced.extend(
|
||||
clear_queued_domain_tasks(
|
||||
set.server,
|
||||
object_id,
|
||||
&cleared_types,
|
||||
&mut batch,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
|
||||
for mut task in tasks.drain(..) {
|
||||
match &mut task {
|
||||
@@ -992,6 +1018,28 @@ impl RegistrySet for Server {
|
||||
);
|
||||
}
|
||||
|
||||
// inbuxa: #327: a domain's queued tasks go with it
|
||||
if matches!(object.inner, ObjectInner::Domain(_)) {
|
||||
let mut batch = BatchBuilder::new();
|
||||
let cleared = clear_queued_domain_tasks(
|
||||
set.server,
|
||||
id,
|
||||
&[
|
||||
TaskType::DkimManagement,
|
||||
TaskType::DnsManagement,
|
||||
TaskType::AcmeRenewal,
|
||||
],
|
||||
&mut batch,
|
||||
)
|
||||
.await?;
|
||||
if !cleared.is_empty() {
|
||||
set.server.store().write(batch.build_all()).await?;
|
||||
for id in cleared {
|
||||
set.server.remove_index_lock(id).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// inbuxa: ME-3
|
||||
if let ObjectInner::MaskedEmail(mask) = &object.inner {
|
||||
crate::inbuxa::masked_email::destroyed(self, id, mask).await?;
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
* 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 crate::task_manager::{TaskFailureType, TaskResult};
|
||||
use common::{Server, network::acme::AcmeError};
|
||||
use registry::schema::structs::TaskDomainManagement;
|
||||
use registry::schema::structs::{CertificateManagement, Domain, TaskDomainManagement};
|
||||
use std::time::Duration;
|
||||
use store::write::now;
|
||||
|
||||
@@ -41,6 +43,21 @@ const MAX_RETRIES: u32 = 5;
|
||||
|
||||
#[allow(unused_variables)]
|
||||
async fn acme_management(server: &Server, task: &TaskDomainManagement) -> trc::Result<TaskResult> {
|
||||
// inbuxa: a domain that's gone, or no longer orders its own certificate,
|
||||
// has nothing for this task to do: it goes quietly instead of failing (#327)
|
||||
if !server
|
||||
.registry()
|
||||
.object::<Domain>(task.domain_id)
|
||||
.await?
|
||||
.is_some_and(|domain| {
|
||||
matches!(
|
||||
domain.certificate_management,
|
||||
CertificateManagement::Automatic(_)
|
||||
)
|
||||
})
|
||||
{
|
||||
return Ok(TaskResult::Ignored);
|
||||
}
|
||||
let mut last_temporary_error = Ok(TaskResult::temporary(""));
|
||||
for retry in 0..MAX_RETRIES {
|
||||
last_temporary_error = match Box::pin(server.acme_renew(task.domain_id)).await {
|
||||
|
||||
@@ -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 crate::task_manager::TaskResult;
|
||||
@@ -60,13 +62,13 @@ impl DkimManagementTask for Server {
|
||||
}
|
||||
|
||||
async fn dkim_management(server: &Server, task: &TaskDomainManagement) -> trc::Result<TaskResult> {
|
||||
// inbuxa: a domain that's gone, or no longer rotates its own keys, has
|
||||
// nothing for this task to do: it goes quietly instead of failing (#327)
|
||||
let Some(domain) = server.registry().object::<Domain>(task.domain_id).await? else {
|
||||
return Ok(TaskResult::permanent("Domain not found".to_string()));
|
||||
return Ok(TaskResult::Ignored);
|
||||
};
|
||||
let DkimManagement::Automatic(dkim) = domain.dkim_management else {
|
||||
return Ok(TaskResult::permanent(
|
||||
"Domain is not set to automatic DKIM management".to_string(),
|
||||
));
|
||||
return Ok(TaskResult::Ignored);
|
||||
};
|
||||
let configured = dkim.algorithms.into_inner();
|
||||
if configured.is_empty() {
|
||||
|
||||
@@ -110,13 +110,13 @@ async fn dns_management(server: &Server, task: &TaskDnsManagement) -> trc::Resul
|
||||
"No DNS records to update".to_string(),
|
||||
));
|
||||
}
|
||||
// inbuxa: a domain that's gone, or no longer publishes its own records,
|
||||
// has nothing for this task to do: it goes quietly instead of failing (#327)
|
||||
let Some(domain) = server.registry().object::<Domain>(task.domain_id).await? else {
|
||||
return Ok(TaskResult::permanent("Domain not found".to_string()));
|
||||
return Ok(TaskResult::Ignored);
|
||||
};
|
||||
let DnsManagement::Automatic(props) = &domain.dns_management else {
|
||||
return Ok(TaskResult::permanent(
|
||||
"Domain is not set to automatic DNS management".to_string(),
|
||||
));
|
||||
return Ok(TaskResult::Ignored);
|
||||
};
|
||||
let dns_updater = match server.build_dns_updater(props.dns_server_id).await? {
|
||||
Ok(updater) => updater,
|
||||
|
||||
@@ -166,9 +166,27 @@ pub async fn clear_queued_renewals(
|
||||
server: &Server,
|
||||
domain_id: Id,
|
||||
batch: &mut BatchBuilder,
|
||||
) -> trc::Result<Vec<u64>> {
|
||||
clear_queued_domain_tasks(server, domain_id, &[TaskType::AcmeRenewal], batch).await
|
||||
}
|
||||
|
||||
/// inbuxa: adds to `batch` the removal of a domain's queued tasks of the
|
||||
/// given types (DKIM management, DNS management, certificate renewal),
|
||||
/// whatever their state. For a domain that's gone, a task about to be
|
||||
/// replaced, or one the domain no longer runs (#327). Like
|
||||
/// [`clear_queued_renewals`], it returns the tasks it locked, and a task
|
||||
/// running right now stays.
|
||||
pub async fn clear_queued_domain_tasks(
|
||||
server: &Server,
|
||||
domain_id: Id,
|
||||
types: &[TaskType],
|
||||
batch: &mut BatchBuilder,
|
||||
) -> trc::Result<Vec<u64>> {
|
||||
let mut locked = Vec::new();
|
||||
for (due, id) in queued_tasks(server, &[TaskType::AcmeRenewal]).await? {
|
||||
if types.is_empty() {
|
||||
return Ok(locked);
|
||||
}
|
||||
for (due, id) in queued_tasks(server, types).await? {
|
||||
let task = server
|
||||
.store()
|
||||
.get_value::<Task>(ValueKey::from(ValueClass::TaskQueue(
|
||||
@@ -176,9 +194,12 @@ pub async fn clear_queued_renewals(
|
||||
)))
|
||||
.await
|
||||
.caused_by(trc::location!())?;
|
||||
if matches!(task, Some(Task::AcmeRenewal(task)) if task.domain_id == domain_id)
|
||||
&& server.try_lock_task(id).await
|
||||
{
|
||||
let task_domain_id = match &task {
|
||||
Some(Task::AcmeRenewal(task)) | Some(Task::DkimManagement(task)) => task.domain_id,
|
||||
Some(Task::DnsManagement(task)) => task.domain_id,
|
||||
_ => continue,
|
||||
};
|
||||
if task_domain_id == domain_id && server.try_lock_task(id).await {
|
||||
batch
|
||||
.clear(ValueClass::TaskQueue(TaskQueueClass::Task { id }))
|
||||
.clear(ValueClass::TaskQueue(TaskQueueClass::Due { id, due }));
|
||||
|
||||
@@ -43,6 +43,7 @@ pub async fn test(test: &TestServer) {
|
||||
.await;
|
||||
automatic_to_manual_dns_test(test).await;
|
||||
orphaned_keys_test(test).await;
|
||||
domain_tasks_follow_settings_test(test).await;
|
||||
|
||||
println!("Running DKIM Management tests...");
|
||||
let account = test.account("[email protected]");
|
||||
@@ -717,6 +718,87 @@ async fn automatic_to_manual_dns_test(test: &TestServer) {
|
||||
account.registry_destroy_all(ObjectType::DnsServer).await;
|
||||
}
|
||||
|
||||
/// #327: a domain's DKIM task goes when DKIM is set to manual, isn't doubled
|
||||
/// when it's set back to automatic, and goes with the domain.
|
||||
async fn domain_tasks_follow_settings_test(test: &TestServer) {
|
||||
println!("Running DKIM task lifecycle tests...");
|
||||
let account = test.account("[email protected]");
|
||||
DNS_RECORDS.lock().unwrap().clear();
|
||||
account.dkim_signatures().await.assert_total(0, 0);
|
||||
let dns_server_id = account.create_memory_dns_server().await;
|
||||
|
||||
let domain_id = account
|
||||
.registry_create_object(Domain {
|
||||
name: "dkim-tasks.org".to_string(),
|
||||
certificate_management: CertificateManagement::Manual,
|
||||
dkim_management: dkim_management(LONG_ROTATION_MS),
|
||||
dns_management: dns_management(dns_server_id),
|
||||
..Default::default()
|
||||
})
|
||||
.await;
|
||||
account
|
||||
.wait_for_dkim_stages(&[(DkimRotationStage::Active, 2)])
|
||||
.await;
|
||||
account.wait_for_dkim_task_count(domain_id, 1).await;
|
||||
|
||||
// Manual: the queued rotation goes now, not when it's due
|
||||
account
|
||||
.registry_update_object(
|
||||
ObjectType::Domain,
|
||||
domain_id,
|
||||
json!({ Property::DkimManagement: DkimManagement::Manual }),
|
||||
)
|
||||
.await;
|
||||
account.wait_for_dkim_task_count(domain_id, 0).await;
|
||||
|
||||
// Automatic again: one task, not a second beside a leftover
|
||||
account
|
||||
.registry_update_object(
|
||||
ObjectType::Domain,
|
||||
domain_id,
|
||||
json!({ Property::DkimManagement: dkim_management(LONG_ROTATION_MS) }),
|
||||
)
|
||||
.await;
|
||||
account.wait_for_dkim_task_count(domain_id, 1).await;
|
||||
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
||||
account.wait_for_dkim_task_count(domain_id, 1).await;
|
||||
|
||||
// Saving it again, changed, still leaves one
|
||||
account
|
||||
.registry_update_object(
|
||||
ObjectType::Domain,
|
||||
domain_id,
|
||||
json!({ Property::DkimManagement: dkim_management(LONG_ROTATION_MS + 1000) }),
|
||||
)
|
||||
.await;
|
||||
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
||||
account.wait_for_dkim_task_count(domain_id, 1).await;
|
||||
|
||||
// Deleted: its tasks go with it
|
||||
account
|
||||
.registry_destroy(ObjectType::Domain, [domain_id])
|
||||
.await
|
||||
.assert_destroyed(&[domain_id]);
|
||||
account.wait_for_dkim_task_count(domain_id, 0).await;
|
||||
assert!(
|
||||
account
|
||||
.tasks()
|
||||
.await
|
||||
.is_none_or(|tasks| !tasks.iter().any(|task| matches!(
|
||||
&task.task,
|
||||
Task::DnsManagement(task) if task.domain_id == domain_id
|
||||
))),
|
||||
"a deleted domain's DNS tasks stay queued"
|
||||
);
|
||||
|
||||
// Cleanup
|
||||
account.registry_destroy_all(ObjectType::Task).await;
|
||||
account
|
||||
.registry_destroy_all(ObjectType::DkimSignature)
|
||||
.await;
|
||||
account.registry_destroy_all(ObjectType::DnsServer).await;
|
||||
}
|
||||
|
||||
async fn manual_dns_pending_test(test: &TestServer) {
|
||||
println!("Running DKIM pending key under manual DNS tests...");
|
||||
let account = test.account("[email protected]");
|
||||
|
||||
Reference in new issue
Block a user