|
| 1 | +/* |
| 2 | + * Copyright 2025 Signal Messenger, LLC |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package org.whispersystems.textsecuregcm.workers; |
| 7 | + |
| 8 | +import com.google.common.annotations.VisibleForTesting; |
| 9 | +import io.micrometer.core.instrument.Metrics; |
| 10 | +import java.time.Duration; |
| 11 | +import net.sourceforge.argparse4j.inf.Subparser; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | +import org.whispersystems.textsecuregcm.identity.IdentityType; |
| 15 | +import org.whispersystems.textsecuregcm.metrics.DevicePlatformUtil; |
| 16 | +import org.whispersystems.textsecuregcm.metrics.MetricsUtil; |
| 17 | +import org.whispersystems.textsecuregcm.storage.Account; |
| 18 | +import org.whispersystems.textsecuregcm.storage.AccountsManager; |
| 19 | +import reactor.core.publisher.Flux; |
| 20 | +import reactor.core.publisher.Mono; |
| 21 | +import reactor.util.retry.Retry; |
| 22 | + |
| 23 | +public class RemoveAccountsWithoutPqKeysCommand extends AbstractSinglePassCrawlAccountsCommand { |
| 24 | + |
| 25 | + @VisibleForTesting |
| 26 | + static final String DRY_RUN_ARGUMENT = "dry-run"; |
| 27 | + |
| 28 | + @VisibleForTesting |
| 29 | + static final String MAX_CONCURRENCY_ARGUMENT = "max-concurrency"; |
| 30 | + |
| 31 | + @VisibleForTesting |
| 32 | + static final String RETRIES_ARGUMENT = "retries"; |
| 33 | + |
| 34 | + @VisibleForTesting |
| 35 | + static final String MAX_ACCOUNTS_ARGUMENT = "max-accounts"; |
| 36 | + |
| 37 | + private static final String REMOVED_ACCOUNT_COUNTER_NAME = |
| 38 | + MetricsUtil.name(RemoveAccountsWithoutPqKeysCommand.class, "removedAccount"); |
| 39 | + |
| 40 | + private static final Logger log = LoggerFactory.getLogger(RemoveAccountsWithoutPqKeysCommand.class); |
| 41 | + |
| 42 | + public RemoveAccountsWithoutPqKeysCommand() { |
| 43 | + super("remove-accounts-without-pq-keys", "Removes accounts with primary devices that don't have PQ keys"); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void configure(final Subparser subparser) { |
| 48 | + super.configure(subparser); |
| 49 | + |
| 50 | + subparser.addArgument("--dry-run") |
| 51 | + .type(Boolean.class) |
| 52 | + .dest(DRY_RUN_ARGUMENT) |
| 53 | + .required(false) |
| 54 | + .setDefault(true) |
| 55 | + .help("If true, don’t actually modify accounts with expired linked devices"); |
| 56 | + |
| 57 | + subparser.addArgument("--max-concurrency") |
| 58 | + .type(Integer.class) |
| 59 | + .dest(MAX_CONCURRENCY_ARGUMENT) |
| 60 | + .setDefault(16) |
| 61 | + .help("Max concurrency for DynamoDB operations"); |
| 62 | + |
| 63 | + subparser.addArgument("--retries") |
| 64 | + .type(Integer.class) |
| 65 | + .dest(RETRIES_ARGUMENT) |
| 66 | + .setDefault(3) |
| 67 | + .help("Maximum number of DynamoDB retries permitted per device"); |
| 68 | + |
| 69 | + subparser.addArgument("--max-accounts") |
| 70 | + .type(Integer.class) |
| 71 | + .required(true) |
| 72 | + .dest(MAX_ACCOUNTS_ARGUMENT) |
| 73 | + .help("Maximum number of accounts to remove per run"); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void crawlAccounts(final Flux<Account> accounts) { |
| 78 | + final boolean dryRun = getNamespace().getBoolean(DRY_RUN_ARGUMENT); |
| 79 | + final int maxConcurrency = getNamespace().getInt(MAX_CONCURRENCY_ARGUMENT); |
| 80 | + final int maxRetries = getNamespace().getInt(RETRIES_ARGUMENT); |
| 81 | + final int maxAccounts = getNamespace().getInt(MAX_ACCOUNTS_ARGUMENT); |
| 82 | + |
| 83 | + final AccountsManager accountsManager = getCommandDependencies().accountsManager(); |
| 84 | + final PqKeysUtil pqKeysUtil = new PqKeysUtil(getCommandDependencies().keysManager(), maxConcurrency, maxRetries); |
| 85 | + |
| 86 | + accounts |
| 87 | + .transform(pqKeysUtil::getAccountsWithoutPqKeys) |
| 88 | + .take(maxAccounts) |
| 89 | + .filter(accountWithoutPqKeys -> { |
| 90 | + if (!accountWithoutPqKeys.hasLockedCredentials()) { |
| 91 | + log.warn("Account {} is not locked", accountWithoutPqKeys.getIdentifier(IdentityType.ACI)); |
| 92 | + } |
| 93 | + |
| 94 | + return accountWithoutPqKeys.hasLockedCredentials(); |
| 95 | + }) |
| 96 | + .flatMap(accountWithoutPqKeys -> { |
| 97 | + final String platform = DevicePlatformUtil.getDevicePlatform(accountWithoutPqKeys.getPrimaryDevice()) |
| 98 | + .map(Enum::name) |
| 99 | + .orElse("unknown"); |
| 100 | + |
| 101 | + return dryRun |
| 102 | + ? Mono.just(platform) |
| 103 | + : Mono.fromFuture(() -> accountsManager.delete(accountWithoutPqKeys, AccountsManager.DeletionReason.ADMIN_DELETED)) |
| 104 | + .retryWhen(Retry.backoff(maxRetries, Duration.ofSeconds(1)) |
| 105 | + .onRetryExhaustedThrow((spec, rs) -> rs.failure())) |
| 106 | + .thenReturn(platform) |
| 107 | + .onErrorResume(throwable -> { |
| 108 | + log.warn("Failed to remove account without PQ keys {}", accountWithoutPqKeys.getIdentifier(IdentityType.ACI), throwable); |
| 109 | + return Mono.empty(); |
| 110 | + }); |
| 111 | + }) |
| 112 | + .doOnNext(deletedAccountPlatform -> { |
| 113 | + Metrics.counter(REMOVED_ACCOUNT_COUNTER_NAME, |
| 114 | + "dryRun", String.valueOf(dryRun), |
| 115 | + "platform", deletedAccountPlatform) |
| 116 | + .increment(); |
| 117 | + }) |
| 118 | + .then() |
| 119 | + .block(); |
| 120 | + } |
| 121 | +} |
0 commit comments