Add SandboxAccountPoolCommand

This commit is contained in:
Ravi Khadiwala authored and ravi-signal committed 2026-10-07 14:55:33 -05:00
1 parent a0c321557f
commit a15d5cdbb9
4 files changed
+151

No files matched your search

@@ -346,6 +346,7 @@ import org.whispersystems.textsecuregcm.workers.RemoveExpiredLinkedDevicesComman
import org.whispersystems.textsecuregcm.workers.RemoveExpiredSandboxAccountsCommand;
import org.whispersystems.textsecuregcm.workers.RemoveExpiredUsernameHoldsCommand;
import org.whispersystems.textsecuregcm.workers.RemoveOrphanedPreKeyPagesCommand;
import org.whispersystems.textsecuregcm.workers.SandboxAccountPoolCommand;
import org.whispersystems.textsecuregcm.workers.ScheduledApnPushNotificationSenderServiceCommand;
import org.whispersystems.textsecuregcm.workers.ServerVersionCommand;
import org.whispersystems.textsecuregcm.workers.SetRequestLoggingEnabledTask;
@@ -400,6 +401,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
bootstrap.addCommand(new MessagePersisterServiceCommand());
bootstrap.addCommand(new RemoveExpiredAccountsCommand(Clock.systemUTC()));
bootstrap.addCommand(new RemoveExpiredSandboxAccountsCommand(Clock.systemUTC()));
bootstrap.addCommand(new SandboxAccountPoolCommand());
bootstrap.addCommand(new RemoveExpiredUsernameHoldsCommand(Clock.systemUTC()));
bootstrap.addCommand(new RemoveExpiredBackupsCommand(Clock.systemUTC()));
bootstrap.addCommand(new RemoveOrphanedPreKeyPagesCommand(Clock.systemUTC()));
@@ -14,7 +14,10 @@ import org.whispersystems.textsecuregcm.util.AttributeValues;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.TransactWriteItem;
import software.amazon.awssdk.services.dynamodb.model.Update;
@@ -124,4 +127,22 @@ public class SandboxAccounts {
// Already in the pool
}
}
/// Remove an ACI from the pool if no account currently holds it
///
/// @return true if the ACI was removed, or false if it is currently claimed or did not exist
public boolean removeIfAvailable(final UUID uuid) {
try {
final DeleteItemResponse resp = dynamoDbClient.deleteItem(DeleteItemRequest.builder()
.tableName(tableName)
.key(Map.of(KEY_ACCOUNT_UUID, AttributeValues.fromUUID(uuid)))
.conditionExpression("attribute_not_exists(#registeredAt)")
.expressionAttributeNames(Map.of("#registeredAt", ATTR_REGISTERED_AT))
.returnValues(ReturnValue.ALL_OLD)
.build());
return resp.hasAttributes();
} catch (final ConditionalCheckFailedException _) {
return false;
}
}
}
@@ -0,0 +1,114 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.workers;
import io.dropwizard.core.Application;
import io.dropwizard.core.setup.Environment;
import java.util.UUID;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import net.sourceforge.argparse4j.inf.Subparsers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.WhisperServerConfiguration;
import org.whispersystems.textsecuregcm.storage.SandboxAccounts;
/// Manages the pool of ACIs available for sandbox accounts
public class SandboxAccountPoolCommand extends AbstractCommandWithDependencies {
private static final Logger log = LoggerFactory.getLogger(SandboxAccountPoolCommand.class);
private static final String SUBCOMMAND_ARGUMENT = "subcommand";
private static final String COUNT_ARGUMENT = "count";
@FunctionalInterface
private interface Subcommand {
void run(SandboxAccounts sandboxAccounts, Namespace namespace);
}
public SandboxAccountPoolCommand() {
super(new Application<>() {
@Override
public void run(final WhisperServerConfiguration configuration, final Environment environment) {
}
}, "sandbox-account-pool", "Adds, removes, or lists the ACIs available for sandbox accounts");
}
@Override
public void configure(final Subparser subparser) {
super.configure(subparser);
final Subparsers subparsers = subparser.addSubparsers();
final Subparser addParser = subparsers.addParser("add")
.help("Add random ACIs to the pool")
.setDefault(SUBCOMMAND_ARGUMENT, (Subcommand) SandboxAccountPoolCommand::add);
addCountArgument(addParser, "The number of ACIs to add");
final Subparser removeParser = subparsers.addParser("remove")
.help("Remove unclaimed ACIs from the pool")
.setDefault(SUBCOMMAND_ARGUMENT, (Subcommand) SandboxAccountPoolCommand::remove);
addCountArgument(removeParser, "The number of ACIs to remove");
subparsers.addParser("list")
.help("List all ACIs in the pool")
.setDefault(SUBCOMMAND_ARGUMENT, (Subcommand) SandboxAccountPoolCommand::list);
}
private static void addCountArgument(final Subparser subparser, final String help) {
subparser.addArgument("count")
.type(Integer.class)
.dest(COUNT_ARGUMENT)
.choices(Arguments.range(1, Integer.MAX_VALUE))
.help(help);
}
@Override
protected void run(final Environment environment, final Namespace namespace,
final WhisperServerConfiguration configuration, final CommandDependencies commandDependencies) throws Exception {
final Subcommand subcommand = namespace.get(SUBCOMMAND_ARGUMENT);
subcommand.run(commandDependencies.sandboxAccounts(), namespace);
}
private static void add(final SandboxAccounts sandboxAccounts, final Namespace namespace) {
final int count = namespace.getInt(COUNT_ARGUMENT);
for (int i = 0; i < count; i++) {
final UUID aci = UUID.randomUUID();
sandboxAccounts.add(aci);
log.info("Added sandbox ACI {}", aci);
}
}
private static void remove(final SandboxAccounts sandboxAccounts, final Namespace namespace) {
final int count = namespace.getInt(COUNT_ARGUMENT);
int removed = 0;
for (final UUID aci : sandboxAccounts.getAvailable()) {
if (removed == count) {
break;
}
if (sandboxAccounts.removeIfAvailable(aci)) {
log.info("Removed sandbox ACI {}", aci);
removed++;
} else {
log.info("Not removing sandbox ACI {} because it is currently claimed", aci);
}
}
if (removed < count) {
throw new RuntimeException(
"Requested removal of %d sandbox ACIs, but only %d were unclaimed".formatted(count, removed));
}
}
private static void list(final SandboxAccounts sandboxAccounts, final Namespace namespace) {
for (final SandboxAccounts.SandboxAccountReservation sandboxAccount : sandboxAccounts.getAll()) {
log.info("{} {}", sandboxAccount.uuid(), sandboxAccount.registeredAt()
.map(registeredAt -> "expires " + registeredAt.plus(RemoveExpiredSandboxAccountsCommand.MAX_SANDBOX_ACCOUNT_AGE))
.orElse("available"));
}
}
}
@@ -69,6 +69,20 @@ class SandboxAccountsTest {
assertThrows(TransactionCanceledException.class, () -> claim(notInPool, now));
}
@Test
void removeIfAvailable() {
final UUID available = UUID.randomUUID();
final UUID claimed = UUID.randomUUID();
sandboxAccounts.add(available);
sandboxAccounts.add(claimed);
claim(claimed, Instant.now());
assertThat(sandboxAccounts.removeIfAvailable(available)).isTrue();
assertThat(sandboxAccounts.removeIfAvailable(available)).isFalse();
assertThat(sandboxAccounts.removeIfAvailable(claimed)).isFalse();
assertThat(sandboxAccounts.getAll()).extracting(SandboxAccounts.SandboxAccountReservation::uuid).containsExactly(claimed);
}
private void claim(final UUID aci, final Instant registeredAt) {
DYNAMO_DB_EXTENSION.getDynamoDbClient().transactWriteItems(TransactWriteItemsRequest.builder()
.transactItems(sandboxAccounts.buildClaimWriteItem(aci, registeredAt))