mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-01 10:16:47 +00:00
Make account deletion a blocking operation
This commit is contained in:
1 parent
405b04f07b
commit
ec2cb8581c
12 files changed
+79
-68
No files matched your search
+2
-2
@@ -558,11 +558,11 @@ public class AccountController {
|
||||
|
||||
@DELETE
|
||||
@Path("/me")
|
||||
public CompletableFuture<Response> deleteAccount(@Auth AuthenticatedDevice auth) {
|
||||
public void deleteAccount(@Auth AuthenticatedDevice auth) {
|
||||
final Account account = accounts.getByAccountIdentifier(auth.accountIdentifier())
|
||||
.orElseThrow(() -> new WebApplicationException(Status.UNAUTHORIZED));
|
||||
|
||||
return accounts.delete(account, AccountsManager.DeletionReason.USER_REQUEST).thenApply(Util.ASYNC_EMPTY_RESPONSE);
|
||||
accounts.delete(account, AccountsManager.DeletionReason.USER_REQUEST);
|
||||
}
|
||||
|
||||
private void clearUsernameLink(final Account account) {
|
||||
|
||||
+1
-2
@@ -96,8 +96,7 @@ public class AccountsGrpcService extends SimpleAccountsGrpc.AccountsImplBase {
|
||||
@Override
|
||||
public DeleteAccountResponse deleteAccount(final DeleteAccountRequest request) {
|
||||
accountsManager.delete(getAuthenticatedAccount(AuthenticationUtil.requireAuthenticatedPrimaryDevice()),
|
||||
AccountsManager.DeletionReason.USER_REQUEST)
|
||||
.join();
|
||||
AccountsManager.DeletionReason.USER_REQUEST);
|
||||
|
||||
return DeleteAccountResponse.getDefaultInstance();
|
||||
}
|
||||
|
||||
@@ -1176,30 +1176,39 @@ public class Accounts {
|
||||
.map(UUID::fromString);
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> delete(final UUID uuid, final List<TransactWriteItem> additionalWriteItems) {
|
||||
public void delete(final UUID uuid, final List<TransactWriteItem> additionalWriteItems) {
|
||||
final Timer.Sample sample = Timer.start();
|
||||
|
||||
return getByAccountIdentifierAsync(uuid)
|
||||
.thenCompose(maybeAccount -> maybeAccount.map(account -> {
|
||||
final List<TransactWriteItem> transactWriteItems = new ArrayList<>(List.of(
|
||||
buildDelete(phoneNumberConstraintTableName, ATTR_ACCOUNT_E164, account.getNumber()),
|
||||
buildDelete(accountsTableName, KEY_ACCOUNT_UUID, uuid),
|
||||
buildDelete(phoneNumberIdentifierConstraintTableName, ATTR_PNI_UUID, account.getPhoneNumberIdentifier()),
|
||||
buildPutDeletedAccount(uuid, account.getPhoneNumberIdentifier())
|
||||
));
|
||||
try {
|
||||
final Account account;
|
||||
{
|
||||
final Optional<Account> maybeAccount = getByAccountIdentifier(uuid);
|
||||
|
||||
account.getUsernameHash().ifPresent(usernameHash -> transactWriteItems.add(
|
||||
buildDelete(usernamesConstraintTableName, UsernameTable.KEY_USERNAME_HASH, usernameHash)));
|
||||
if (maybeAccount.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
transactWriteItems.addAll(additionalWriteItems);
|
||||
account = maybeAccount.get();
|
||||
}
|
||||
|
||||
return dynamoDbAsyncClient.transactWriteItems(TransactWriteItemsRequest.builder()
|
||||
.transactItems(transactWriteItems)
|
||||
.build())
|
||||
.thenRun(Util.NOOP);
|
||||
})
|
||||
.orElseGet(() -> CompletableFuture.completedFuture(null)))
|
||||
.thenRun(() -> sample.stop(DELETE_TIMER));
|
||||
final List<TransactWriteItem> transactWriteItems = new ArrayList<>(List.of(
|
||||
buildDelete(phoneNumberConstraintTableName, ATTR_ACCOUNT_E164, account.getNumber()),
|
||||
buildDelete(accountsTableName, KEY_ACCOUNT_UUID, uuid),
|
||||
buildDelete(phoneNumberIdentifierConstraintTableName, ATTR_PNI_UUID, account.getPhoneNumberIdentifier()),
|
||||
buildPutDeletedAccount(uuid, account.getPhoneNumberIdentifier())
|
||||
));
|
||||
|
||||
account.getUsernameHash().ifPresent(usernameHash -> transactWriteItems.add(
|
||||
buildDelete(usernamesConstraintTableName, UsernameTable.KEY_USERNAME_HASH, usernameHash)));
|
||||
|
||||
transactWriteItems.addAll(additionalWriteItems);
|
||||
|
||||
dynamoDbClient.transactWriteItems(TransactWriteItemsRequest.builder()
|
||||
.transactItems(transactWriteItems)
|
||||
.build());
|
||||
} finally {
|
||||
sample.stop(DELETE_TIMER);
|
||||
}
|
||||
}
|
||||
|
||||
Flux<Account> getAll(final int segments, final Scheduler scheduler) {
|
||||
|
||||
+37
-28
@@ -747,7 +747,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
if (maybeExistingAccount.get().getIdentifier(IdentityType.ACI).equals(account.getIdentifier(IdentityType.ACI))) {
|
||||
maybeDisplacedUuid = Optional.empty();
|
||||
} else {
|
||||
delete(maybeExistingAccount.get()).join();
|
||||
delete(maybeExistingAccount.get());
|
||||
maybeDisplacedUuid = maybeExistingAccount.map(Account::getUuid);
|
||||
}
|
||||
} else {
|
||||
@@ -1133,36 +1133,42 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
return accounts.getAll(segments, scheduler);
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> delete(final Account account, final DeletionReason deletionReason) {
|
||||
public void delete(final Account account, final DeletionReason deletionReason) {
|
||||
final Timer.Sample sample = Timer.start();
|
||||
|
||||
return accountLockManager.withLockAsync(Set.of(account.getPhoneNumberIdentifier()), () -> delete(account),
|
||||
accountLockExecutor)
|
||||
.whenComplete((ignored, throwable) -> {
|
||||
sample.stop(deleteTimer);
|
||||
try {
|
||||
accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()), () -> {
|
||||
delete(account);
|
||||
return null;
|
||||
}, accountLockExecutor);
|
||||
|
||||
if (throwable == null) {
|
||||
Metrics.counter(DELETE_COUNTER_NAME,
|
||||
COUNTRY_CODE_TAG_NAME, Util.getCountryCode(account.getNumber()),
|
||||
DELETION_REASON_TAG_NAME, deletionReason.tagValue)
|
||||
.increment();
|
||||
} else {
|
||||
logger.warn("Failed to delete account", throwable);
|
||||
}
|
||||
});
|
||||
Metrics.counter(DELETE_COUNTER_NAME,
|
||||
COUNTRY_CODE_TAG_NAME, Util.getCountryCode(account.getNumber()),
|
||||
DELETION_REASON_TAG_NAME, deletionReason.tagValue)
|
||||
.increment();
|
||||
} catch (final Exception e) {
|
||||
logger.warn("Failed to delete account", e);
|
||||
|
||||
if (e instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
sample.stop(deleteTimer);
|
||||
}
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> delete(final Account account) {
|
||||
final List<TransactWriteItem> additionalWriteItems =
|
||||
account.getDevices().stream()
|
||||
.flatMap(device -> keysManager.buildWriteItemsForRemovedDevice(
|
||||
account.getIdentifier(IdentityType.ACI),
|
||||
account.getIdentifier(IdentityType.PNI),
|
||||
device.getId())
|
||||
.stream())
|
||||
.toList();
|
||||
private void delete(final Account account) {
|
||||
final List<TransactWriteItem> additionalWriteItems = account.getDevices().stream()
|
||||
.flatMap(device -> keysManager.buildWriteItemsForRemovedDevice(
|
||||
account.getIdentifier(IdentityType.ACI),
|
||||
account.getIdentifier(IdentityType.PNI),
|
||||
device.getId())
|
||||
.stream())
|
||||
.toList();
|
||||
|
||||
return CompletableFuture.allOf(
|
||||
CompletableFuture.allOf(
|
||||
secureStorageClient.deleteStoredData(account.getUuid()),
|
||||
secureValueRecovery2Client.removeData(account.getUuid()),
|
||||
keysManager.deleteSingleUsePreKeys(account.getUuid()),
|
||||
@@ -1170,9 +1176,12 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
messagesManager.clear(account.getUuid()),
|
||||
profilesManager.deleteAll(account.getUuid(), true),
|
||||
registrationRecoveryPasswordsManager.remove(account.getIdentifier(IdentityType.PNI)))
|
||||
.thenCompose(ignored -> accounts.delete(account.getUuid(), additionalWriteItems))
|
||||
.thenCompose(ignored -> redisDeleteAsync(account))
|
||||
.thenRun(() -> disconnectionRequestManager.requestDisconnection(account));
|
||||
.join();
|
||||
|
||||
accounts.delete(account.getUuid(), additionalWriteItems);
|
||||
redisDelete(account);
|
||||
|
||||
disconnectionRequestManager.requestDisconnection(account);
|
||||
}
|
||||
|
||||
private String getAccountMapKey(String key) {
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ public class DeleteUserCommand extends AbstractCommandWithDependencies {
|
||||
Optional<Account> account = accountsManager.getByE164(user);
|
||||
|
||||
if (account.isPresent()) {
|
||||
accountsManager.delete(account.get(), DeletionReason.ADMIN_DELETED).join();
|
||||
accountsManager.delete(account.get(), DeletionReason.ADMIN_DELETED);
|
||||
logger.warn("Removed " + account.get().getNumber());
|
||||
} else {
|
||||
logger.warn("Account not found");
|
||||
|
||||
+4
-1
@@ -20,6 +20,7 @@ import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import reactor.util.retry.Retry;
|
||||
|
||||
public class RemoveExpiredAccountsCommand extends AbstractSinglePassCrawlAccountsCommand {
|
||||
@@ -66,7 +67,9 @@ public class RemoveExpiredAccountsCommand extends AbstractSinglePassCrawlAccount
|
||||
.flatMap(expiredAccount -> {
|
||||
final Mono<Void> deleteAccountMono = isDryRun
|
||||
? Mono.empty()
|
||||
: Mono.fromFuture(() -> getCommandDependencies().accountsManager().delete(expiredAccount, AccountsManager.DeletionReason.EXPIRED));
|
||||
: Mono.fromRunnable(() -> getCommandDependencies().accountsManager().delete(expiredAccount, AccountsManager.DeletionReason.EXPIRED))
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.then();
|
||||
|
||||
return deleteAccountMono
|
||||
.doOnSuccess(ignored -> deletedAccountCounter.increment())
|
||||
|
||||
+2
-4
@@ -92,7 +92,6 @@ import org.whispersystems.textsecuregcm.storage.UsernameHashNotAvailableExceptio
|
||||
import org.whispersystems.textsecuregcm.storage.UsernameReservationNotFoundException;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AccountsHelper;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.util.CompletableFutureTestUtil;
|
||||
import org.whispersystems.textsecuregcm.util.MockUtils;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
@@ -846,8 +845,6 @@ class AccountControllerTest {
|
||||
|
||||
@Test
|
||||
void testDeleteAccount() {
|
||||
when(accountsManager.delete(any(), any())).thenReturn(CompletableFutureTestUtil.almostCompletedFuture(null));
|
||||
|
||||
try (final Response response = resources.getJerseyTest()
|
||||
.target("/v1/accounts/me")
|
||||
.request()
|
||||
@@ -861,7 +858,8 @@ class AccountControllerTest {
|
||||
|
||||
@Test
|
||||
void testDeleteAccountException() {
|
||||
when(accountsManager.delete(any(), any())).thenReturn(CompletableFuture.failedFuture(new RuntimeException("OH NO")));
|
||||
doThrow(new RuntimeException("OH NO"))
|
||||
.when(accountsManager).delete(any(), any());
|
||||
|
||||
try (final Response response = resources.getJerseyTest()
|
||||
.target("/v1/accounts/me")
|
||||
|
||||
-3
@@ -154,9 +154,6 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
|
||||
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI))
|
||||
.thenReturn(Optional.of(account));
|
||||
|
||||
when(accountsManager.delete(any(), any()))
|
||||
.thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
final DeleteAccountResponse ignored =
|
||||
authenticatedServiceStub().deleteAccount(DeleteAccountRequest.newBuilder().build());
|
||||
|
||||
|
||||
+1
-1
@@ -463,7 +463,7 @@ public class AccountCreationDeletionIntegrationTest {
|
||||
|
||||
assertTrue(accountsManager.getByAccountIdentifier(aci).isPresent());
|
||||
|
||||
accountsManager.delete(account, AccountsManager.DeletionReason.ADMIN_DELETED).join();
|
||||
accountsManager.delete(account, AccountsManager.DeletionReason.ADMIN_DELETED);
|
||||
|
||||
assertFalse(accountsManager.getByAccountIdentifier(aci).isPresent());
|
||||
assertFalse(keysManager.getEcSignedPreKey(account.getUuid(), Device.PRIMARY_ID).join().isPresent());
|
||||
|
||||
-2
@@ -160,8 +160,6 @@ class AccountsManagerTest {
|
||||
when(asyncClusterCommands.set(any(), any(), any())).thenReturn(MockRedisFuture.completedFuture("OK"));
|
||||
when(asyncClusterCommands.setex(any(), anyLong(), any())).thenReturn(MockRedisFuture.completedFuture("OK"));
|
||||
|
||||
when(accounts.delete(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
doAnswer((Answer<Void>) invocation -> {
|
||||
final Account account = invocation.getArgument(0, Account.class);
|
||||
final String number = invocation.getArgument(1, String.class);
|
||||
|
||||
@@ -214,7 +214,7 @@ class AccountsTest {
|
||||
assertPhoneNumberConstraintExists("+14151112222", account.getUuid());
|
||||
assertPhoneNumberIdentifierConstraintExists(account.getPhoneNumberIdentifier(), account.getUuid());
|
||||
|
||||
accounts.delete(originalUuid, Collections.emptyList()).join();
|
||||
accounts.delete(originalUuid, Collections.emptyList());
|
||||
assertThat(accounts.findRecentlyDeletedAccountIdentifier(account.getPhoneNumberIdentifier())).hasValue(originalUuid);
|
||||
|
||||
freshUser = createAccount(account);
|
||||
@@ -773,7 +773,7 @@ class AccountsTest {
|
||||
assertThat(accounts.getByAccountIdentifier(deletedAccount.getUuid())).isPresent();
|
||||
assertThat(accounts.getByAccountIdentifier(retainedAccount.getUuid())).isPresent();
|
||||
|
||||
accounts.delete(deletedAccount.getUuid(), Collections.emptyList()).join();
|
||||
accounts.delete(deletedAccount.getUuid(), Collections.emptyList());
|
||||
|
||||
assertThat(accounts.getByAccountIdentifier(deletedAccount.getUuid())).isNotPresent();
|
||||
assertThat(accounts.findRecentlyDeletedAccountIdentifier(deletedAccount.getPhoneNumberIdentifier())).hasValue(deletedAccount.getUuid());
|
||||
@@ -1813,7 +1813,7 @@ class AccountsTest {
|
||||
.build())
|
||||
.items());
|
||||
|
||||
accounts.delete(account.getIdentifier(IdentityType.ACI), Collections.emptyList()).join();
|
||||
accounts.delete(account.getIdentifier(IdentityType.ACI), Collections.emptyList());
|
||||
|
||||
writeAccountRecordWithoutConstraints(account);
|
||||
accounts.regenerateConstraints(account).join();
|
||||
|
||||
-2
@@ -16,7 +16,6 @@ import static org.mockito.Mockito.when;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Stream;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -61,7 +60,6 @@ class RemoveExpiredAccountsCommandTest {
|
||||
final Clock clock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
|
||||
|
||||
final AccountsManager accountsManager = mock(AccountsManager.class);
|
||||
when(accountsManager.delete(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
final RemoveExpiredAccountsCommand removeExpiredAccountsCommand =
|
||||
new TestRemoveExpiredAccountsCommand(clock, accountsManager, isDryRun);
|
||||
|
||||
Reference in new issue
Block a user