mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:16:50 +00:00
Add RemoveExpiredSandboxAccountsCommand
This commit is contained in:
1 parent
4765492415
commit
a0c321557f
10 files changed
+219
-4
No files matched your search
@@ -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<WhisperServerConfiguration
|
||||
bootstrap.addCommand(new ScheduledApnPushNotificationSenderServiceCommand());
|
||||
bootstrap.addCommand(new MessagePersisterServiceCommand());
|
||||
bootstrap.addCommand(new RemoveExpiredAccountsCommand(Clock.systemUTC()));
|
||||
bootstrap.addCommand(new RemoveExpiredSandboxAccountsCommand(Clock.systemUTC()));
|
||||
bootstrap.addCommand(new RemoveExpiredUsernameHoldsCommand(Clock.systemUTC()));
|
||||
bootstrap.addCommand(new RemoveExpiredBackupsCommand(Clock.systemUTC()));
|
||||
bootstrap.addCommand(new RemoveOrphanedPreKeyPagesCommand(Clock.systemUTC()));
|
||||
|
||||
+4
-2
@@ -133,7 +133,8 @@ public record CommandDependencies(
|
||||
PhoneNumberIdentifiers phoneNumberIdentifiers,
|
||||
DynamoDbRecoveryManager dynamoDbRecoveryManager,
|
||||
FDB fdb,
|
||||
AccountLockManager accountLockManager) {
|
||||
AccountLockManager accountLockManager,
|
||||
SandboxAccounts sandboxAccounts) {
|
||||
|
||||
static CommandDependencies build(
|
||||
final String name,
|
||||
@@ -499,7 +500,8 @@ public record CommandDependencies(
|
||||
phoneNumberIdentifiers,
|
||||
dynamoDbRecoveryManager,
|
||||
fdb,
|
||||
accountLockManager);
|
||||
accountLockManager,
|
||||
sandboxAccounts);
|
||||
}
|
||||
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.workers;
|
||||
|
||||
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import io.dropwizard.core.Application;
|
||||
import io.dropwizard.core.setup.Environment;
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.WhisperServerConfiguration;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.SandboxAccounts;
|
||||
|
||||
/// Deletes sandbox accounts that are past [#MAX_SANDBOX_ACCOUNT_AGE]
|
||||
public class RemoveExpiredSandboxAccountsCommand extends AbstractCommandWithDependencies {
|
||||
private static final Logger log = LoggerFactory.getLogger(RemoveExpiredSandboxAccountsCommand.class);
|
||||
public static final Duration MAX_SANDBOX_ACCOUNT_AGE = Duration.ofDays(1);
|
||||
|
||||
private static final String RELEASED_ACI_COUNTER_NAME =
|
||||
name(RemoveExpiredSandboxAccountsCommand.class, "releasedAci");
|
||||
|
||||
@VisibleForTesting
|
||||
static final String DRY_RUN_ARGUMENT = "dry-run";
|
||||
|
||||
private final Clock clock;
|
||||
|
||||
public RemoveExpiredSandboxAccountsCommand(final Clock clock) {
|
||||
super(new 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> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -89,6 +89,7 @@ class FinishPushNotificationExperimentCommandTest {
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
//noinspection unchecked
|
||||
|
||||
+1
@@ -69,6 +69,7 @@ class NotifyIdleDevicesCommandTest {
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
this.idleDeviceNotificationScheduler = idleDeviceNotificationScheduler;
|
||||
|
||||
+1
@@ -56,6 +56,7 @@ class RegenerateSecondaryDynamoDbTableDataCommandTest {
|
||||
null,
|
||||
dynamoDbRecoveryManager,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
namespace = new Namespace(Map.of(
|
||||
|
||||
+106
@@ -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<Arguments> 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);
|
||||
}
|
||||
}
|
||||
+1
@@ -77,6 +77,7 @@ class StartPushNotificationExperimentCommandTest {
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user