From a0c321557fa83094d2b9edbd903620f9a8b64668 Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Thu, 1 Oct 2026 16:18:15 -0500 Subject: [PATCH] Add RemoveExpiredSandboxAccountsCommand --- .../textsecuregcm/WhisperServerService.java | 2 + .../workers/CommandDependencies.java | 6 +- .../RemoveExpiredSandboxAccountsCommand.java | 101 +++++++++++++++++ ...xpiredFoundationDbMessagesCommandTest.java | 2 +- ...PushNotificationExperimentCommandTest.java | 1 + .../workers/NotifyIdleDevicesCommandTest.java | 1 + ...SecondaryDynamoDbTableDataCommandTest.java | 1 + ...moveExpiredSandboxAccountsCommandTest.java | 106 ++++++++++++++++++ ...PushNotificationExperimentCommandTest.java | 1 + ...dFoundationDbMessageQueuesCommandTest.java | 2 +- 10 files changed, 219 insertions(+), 4 deletions(-) create mode 100644 service/src/main/java/org/whispersystems/textsecuregcm/workers/RemoveExpiredSandboxAccountsCommand.java create mode 100644 service/src/test/java/org/whispersystems/textsecuregcm/workers/RemoveExpiredSandboxAccountsCommandTest.java diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index bd364bbfe..51cedb1ff 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -343,6 +343,7 @@ import org.whispersystems.textsecuregcm.workers.RegenerateSecondaryDynamoDbTable import org.whispersystems.textsecuregcm.workers.RemoveExpiredAccountsCommand; import org.whispersystems.textsecuregcm.workers.RemoveExpiredBackupsCommand; import org.whispersystems.textsecuregcm.workers.RemoveExpiredLinkedDevicesCommand; +import org.whispersystems.textsecuregcm.workers.RemoveExpiredSandboxAccountsCommand; import org.whispersystems.textsecuregcm.workers.RemoveExpiredUsernameHoldsCommand; import org.whispersystems.textsecuregcm.workers.RemoveOrphanedPreKeyPagesCommand; import org.whispersystems.textsecuregcm.workers.ScheduledApnPushNotificationSenderServiceCommand; @@ -398,6 +399,7 @@ public class WhisperServerService extends Application() { + @Override + public void run(final WhisperServerConfiguration configuration, final Environment environment) { + } + }, "remove-expired-sandbox-accounts", "Removes expired sandbox accounts and returns their ACIs to the pool"); + this.clock = clock; + } + + @Override + public void configure(final Subparser subparser) { + super.configure(subparser); + + subparser.addArgument("--dry-run") + .type(Boolean.class) + .dest(DRY_RUN_ARGUMENT) + .required(false) + .setDefault(true) + .help("If true, don't actually delete accounts or release ACIs"); + } + + @Override + protected void run(final Environment environment, final Namespace namespace, + final WhisperServerConfiguration configuration, final CommandDependencies commandDependencies) throws Exception { + final boolean isDryRun = namespace.getBoolean(DRY_RUN_ARGUMENT); + final AccountsManager accountsManager = commandDependencies.accountsManager(); + final SandboxAccounts sandboxAccounts = commandDependencies.sandboxAccounts(); + final Instant expirationThreshold = clock.instant().minus(MAX_SANDBOX_ACCOUNT_AGE); + + for (final SandboxAccounts.SandboxAccountReservation sandboxAccount : sandboxAccounts.getAll()) { + if (sandboxAccount.registeredAt().isEmpty()) { + continue; + } + final Instant registeredAt = sandboxAccount.registeredAt().get(); + final boolean expired = registeredAt.isBefore(expirationThreshold); + if (!expired) { + log.info("Skipping unexpired sandbox account {}", sandboxAccount); + continue; + } + + try { + final Optional account = accountsManager.getByAccountIdentifier(sandboxAccount.uuid()); + if (!isDryRun) { + account.ifPresent(_ -> accountsManager.delete(sandboxAccount.uuid(), AccountsManager.DeletionReason.EXPIRED)); + if (!sandboxAccounts.releaseAci(sandboxAccount.uuid(), registeredAt)) { + log.warn("Registration for sandbox ACI {} changed before it could be released", sandboxAccount.uuid()); + continue; + } + } + + log.info("Released sandbox ACI {} registered at {} (exists: {}, dry run: {})", + sandboxAccount.uuid(), registeredAt, account.isPresent(), isDryRun); + Metrics.counter(RELEASED_ACI_COUNTER_NAME, + "exists", String.valueOf(account.isPresent()), + "dryRun", String.valueOf(isDryRun)) + .increment(); + } catch (final Exception e) { + log.warn("Failed to remove sandbox account {}", sandboxAccount.uuid(), e); + } + } + } +} diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/workers/ClearExpiredFoundationDbMessagesCommandTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/workers/ClearExpiredFoundationDbMessagesCommandTest.java index 162a18ef1..9870be4a4 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/workers/ClearExpiredFoundationDbMessagesCommandTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/workers/ClearExpiredFoundationDbMessagesCommandTest.java @@ -45,7 +45,7 @@ class ClearExpiredFoundationDbMessagesCommandTest { super(clock); this.commandDependencies = new CommandDependencies(null, null, null, null, messagesManager, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, - null); + null, null); this.dryRun = dryRun; } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/workers/FinishPushNotificationExperimentCommandTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/workers/FinishPushNotificationExperimentCommandTest.java index f00867e07..31424c1b3 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/workers/FinishPushNotificationExperimentCommandTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/workers/FinishPushNotificationExperimentCommandTest.java @@ -89,6 +89,7 @@ class FinishPushNotificationExperimentCommandTest { null, null, null, + null, null); //noinspection unchecked diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/workers/NotifyIdleDevicesCommandTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/workers/NotifyIdleDevicesCommandTest.java index 3501f7cdf..c3b78c11b 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/workers/NotifyIdleDevicesCommandTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/workers/NotifyIdleDevicesCommandTest.java @@ -69,6 +69,7 @@ class NotifyIdleDevicesCommandTest { null, null, null, + null, null); this.idleDeviceNotificationScheduler = idleDeviceNotificationScheduler; diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/workers/RegenerateSecondaryDynamoDbTableDataCommandTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/workers/RegenerateSecondaryDynamoDbTableDataCommandTest.java index 95f7fd117..00565f511 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/workers/RegenerateSecondaryDynamoDbTableDataCommandTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/workers/RegenerateSecondaryDynamoDbTableDataCommandTest.java @@ -56,6 +56,7 @@ class RegenerateSecondaryDynamoDbTableDataCommandTest { null, dynamoDbRecoveryManager, null, + null, null); namespace = new Namespace(Map.of( diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/workers/RemoveExpiredSandboxAccountsCommandTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/workers/RemoveExpiredSandboxAccountsCommandTest.java new file mode 100644 index 000000000..9e17d82af --- /dev/null +++ b/service/src/test/java/org/whispersystems/textsecuregcm/workers/RemoveExpiredSandboxAccountsCommandTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.workers; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.dropwizard.core.setup.Environment; +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import net.sourceforge.argparse4j.inf.Namespace; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.whispersystems.textsecuregcm.WhisperServerConfiguration; +import org.whispersystems.textsecuregcm.storage.Account; +import org.whispersystems.textsecuregcm.storage.AccountsManager; +import org.whispersystems.textsecuregcm.storage.SandboxAccounts; +import org.whispersystems.textsecuregcm.util.TestClock; +import org.whispersystems.textsecuregcm.storage.SandboxAccounts.SandboxAccountReservation; + +class RemoveExpiredSandboxAccountsCommandTest { + + private static final Instant NOW = Instant.now(); + private static final Instant EXPIRED_REGISTRATION = NOW + .minus(RemoveExpiredSandboxAccountsCommand.MAX_SANDBOX_ACCOUNT_AGE) + .minusSeconds(1); + private static final Instant FRESH_REGISTRATION = NOW.minus(Duration.ofHours(1)); + + private AccountsManager accountsManager; + private SandboxAccounts sandboxAccounts; + + @BeforeEach + void setUp() { + accountsManager = mock(AccountsManager.class); + sandboxAccounts = mock(SandboxAccounts.class); + when(sandboxAccounts.releaseAci(any(), any())).thenReturn(true); + } + + + static Stream processSandboxAccounts() { + final Account account = mock(Account.class); + return Stream.of( + Arguments.argumentSet("stale-account", account, EXPIRED_REGISTRATION, true, true), + Arguments.argumentSet("stale-deleted-account", null, EXPIRED_REGISTRATION, true, false), + Arguments.argumentSet("fresh-account", account, FRESH_REGISTRATION, false, false), + Arguments.argumentSet("fresh-deleted-account", null, FRESH_REGISTRATION, false, false), + Arguments.argumentSet("untracked-account", account, null, false, false) + ); + } + + @ParameterizedTest + @MethodSource + void processSandboxAccounts(final @Nullable Account account, @Nullable final Instant registrationTime, final boolean expectRelease, final boolean expectDelete) throws Exception { + final UUID aci = UUID.randomUUID(); + when(sandboxAccounts.getAll()) + .thenReturn(List.of(new SandboxAccountReservation(aci, Optional.ofNullable(registrationTime)))); + when(accountsManager.getByAccountIdentifier(aci)) + .thenReturn(Optional.ofNullable(account)); + + runCommand(false); + + verify(sandboxAccounts, times(expectRelease ? 1 : 0)).releaseAci(aci, registrationTime); + verify(accountsManager, times(expectDelete ? 1 : 0)).delete(any(), any()); + } + + @Test + void dryRun() throws Exception { + final UUID aci = UUID.randomUUID(); + when(sandboxAccounts.getAll()) + .thenReturn(List.of(new SandboxAccountReservation(aci, Optional.of(EXPIRED_REGISTRATION)))); + when(accountsManager.getByAccountIdentifier(aci)) + .thenReturn(Optional.of(mock(Account.class))); + + runCommand(true); + + verify(accountsManager, never()).delete(any(), any()); + verify(sandboxAccounts, never()).releaseAci(any(), any()); + } + + private void runCommand(final boolean isDryRun) throws Exception { + final CommandDependencies commandDependencies = mock(CommandDependencies.class); + when(commandDependencies.accountsManager()).thenReturn(accountsManager); + when(commandDependencies.sandboxAccounts()).thenReturn(sandboxAccounts); + + final Namespace namespace = mock(Namespace.class); + when(namespace.getBoolean(RemoveExpiredSandboxAccountsCommand.DRY_RUN_ARGUMENT)).thenReturn(isDryRun); + + new RemoveExpiredSandboxAccountsCommand(TestClock.pinned(NOW)) + .run(mock(Environment.class), namespace, mock(WhisperServerConfiguration.class), commandDependencies); + } +} diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/workers/StartPushNotificationExperimentCommandTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/workers/StartPushNotificationExperimentCommandTest.java index d1de0c423..53a6f4f43 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/workers/StartPushNotificationExperimentCommandTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/workers/StartPushNotificationExperimentCommandTest.java @@ -77,6 +77,7 @@ class StartPushNotificationExperimentCommandTest { null, null, null, + null, null); } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/workers/TrimOversizedFoundationDbMessageQueuesCommandTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/workers/TrimOversizedFoundationDbMessageQueuesCommandTest.java index 821a3a0b2..a4f85ebf5 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/workers/TrimOversizedFoundationDbMessageQueuesCommandTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/workers/TrimOversizedFoundationDbMessageQueuesCommandTest.java @@ -39,7 +39,7 @@ public class TrimOversizedFoundationDbMessageQueuesCommandTest { final boolean dryRun) { super(); - this.commandDependencies = new CommandDependencies(null, null, null, null, messagesManager, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); + this.commandDependencies = new CommandDependencies(null, null, null, null, messagesManager, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); this.dryRun = dryRun; }