diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java index 8eb3d89e7..4051c4c43 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java @@ -28,6 +28,7 @@ import java.util.NoSuchElementException; import java.util.Objects; import java.util.Optional; import java.util.UUID; +import java.util.stream.Stream; import javax.annotation.Nullable; import org.apache.commons.lang3.StringUtils; import org.signal.libsignal.protocol.IdentityKey; @@ -38,7 +39,9 @@ import org.slf4j.LoggerFactory; import org.whispersystems.textsecuregcm.auth.SaltedTokenHash; import org.whispersystems.textsecuregcm.auth.StoredRegistrationLock; import org.whispersystems.textsecuregcm.entities.AccountAttributes; +import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; import org.whispersystems.textsecuregcm.identity.IdentityType; +import org.whispersystems.textsecuregcm.identity.PniServiceIdentifier; import org.whispersystems.textsecuregcm.identity.ServiceIdentifier; import org.whispersystems.textsecuregcm.util.ByteArrayBase64UrlAdapter; import org.whispersystems.textsecuregcm.util.IdentityKeyAdapter; diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java index 5a5ad6797..123117603 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java @@ -1388,12 +1388,17 @@ public class Accounts { account = maybeAccount.get(); } - final List transactWriteItems = new ArrayList<>(List.of( - buildConditionalDeleteAccount(account), - buildDelete(phoneNumberConstraintTableName, ATTR_ACCOUNT_E164, account.getNumber()), - buildDelete(phoneNumberIdentifierConstraintTableName, ATTR_PNI_UUID, account.getPhoneNumberIdentifier()), - buildPutDeletedAccount(uuid, account.getPhoneNumberIdentifier()) - )); + + final List transactWriteItems = new ArrayList<>(); + transactWriteItems.add(buildConditionalDeleteAccount(account)); + + account.getNumberOptional().ifPresent(e164 -> transactWriteItems.add( + buildDelete(phoneNumberConstraintTableName, ATTR_ACCOUNT_E164, e164))); + + account.getPhoneNumberIdentifierOptional().ifPresent(pni -> { + transactWriteItems.add(buildDelete(phoneNumberIdentifierConstraintTableName, ATTR_PNI_UUID, pni)); + transactWriteItems.add(buildPutDeletedAccount(uuid, pni)); + }); account.getUsernameHash().ifPresent(usernameHash -> transactWriteItems.add( buildDelete(usernamesConstraintTableName, UsernameTable.KEY_USERNAME_HASH, usernameHash))); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index 0c469e202..8d78602af 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -735,7 +735,6 @@ public class AccountsManager extends RedisPubSubAdapter implemen account.getIdentifier(IdentityType.ACI), account.getPhoneNumberIdentifierOptional(), deviceId)); - try { accounts.updateTransactionally(account, additionalWriteItems); @@ -1282,7 +1281,7 @@ public class AccountsManager extends RedisPubSubAdapter implemen final Account account = maybeAccount.get(); try { - accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()), () -> { + accountLockManager.withSingleAccountLock(account, () -> { delete(account); return null; }, accountLockExecutor); @@ -1312,16 +1311,14 @@ public class AccountsManager extends RedisPubSubAdapter implemen account.getPhoneNumberIdentifierOptional().ifPresent(phoneNumberIdentifier -> additionalWriteItems.add(phoneNumberRecoveryPasswordsManager.buildTransactWriteItemForRemovePassword(phoneNumberIdentifier))); - CompletableFuture.allOf( secureStorageClient.deleteStoredData(account.getAccountIdentifier()), secureValueRecovery2Client.removeData(account.getAccountIdentifier()), keysManager.deleteSingleUsePreKeys(account.getAccountIdentifier()), - keysManager.deleteSingleUsePreKeys(account.getPhoneNumberIdentifier()), + account.getPhoneNumberIdentifierOptional().map(keysManager::deleteSingleUsePreKeys).orElse(CompletableFuture.completedFuture(null)), messagesManager.clear(account.getAccountIdentifier()), profilesManager.deleteAll(account.getAccountIdentifier(), true)) .join(); - accounts.delete(account.getAccountIdentifier(), additionalWriteItems); redisDelete(account); @@ -1476,11 +1473,6 @@ public class AccountsManager extends RedisPubSubAdapter implemen if (StringUtils.isNotBlank(accountJson)) { Account account = SystemMapper.jsonMapper().readValue(accountJson, Account.class); account.setAccountIdentifier(uuid); - - if (account.getPhoneNumberIdentifier() == null) { - logger.warn("Account {} loaded from Redis is missing a PNI", uuid); - } - return Optional.of(account); } @@ -1497,11 +1489,16 @@ public class AccountsManager extends RedisPubSubAdapter implemen } private void redisDelete(final Account account) { + final List keysToDelete = new ArrayList<>(2); + account.getPhoneNumberIdentifierOptional() + .map(pni -> getAccountMapKey(pni.toString())) + .ifPresent(keysToDelete::add); + keysToDelete.add(getAccountEntityKey(account.getAccountIdentifier())); + ResilienceUtil.getGeneralRedisRetry(RETRY_NAME).executeRunnable(() -> redisDeleteTimer.record(() -> cacheCluster.useCluster(connection -> - connection.sync().del(getAccountMapKey(account.getPhoneNumberIdentifier().toString()), - getAccountEntityKey(account.getAccountIdentifier()))))); + connection.sync().del(keysToDelete.toArray(String[]::new))))); } public CompletableFuture> waitForNewLinkedDevice( diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountCreationDeletionIntegrationTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountCreationDeletionIntegrationTest.java index 9d954a931..daa649efc 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountCreationDeletionIntegrationTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountCreationDeletionIntegrationTest.java @@ -39,6 +39,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.junitpioneer.jupiter.cartesian.ArgumentSets; import org.junitpioneer.jupiter.cartesian.CartesianTest; import org.signal.libsignal.protocol.IdentityKey; @@ -60,6 +61,7 @@ import org.whispersystems.textsecuregcm.securestorage.SecureStorageClient; import org.whispersystems.textsecuregcm.securevaluerecovery.SecureValueRecoveryClient; import org.whispersystems.textsecuregcm.tests.util.KeysHelper; import org.whispersystems.textsecuregcm.util.TestRandomUtil; +import org.whispersystems.textsecuregcm.util.ValidBase64URLString; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; public class AccountCreationDeletionIntegrationTest { @@ -493,8 +495,10 @@ public class AccountCreationDeletionIntegrationTest { account.getAccountIdentifier().equals(existingAccountUuid) && account != reregisteredAccount)); } - @Test - void deleteAccountWithNumber() throws InterruptedException { + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void deleteAccount(final boolean hasE164) + throws InvalidInputException, VerificationFailedException, ReceiptAlreadyRedeemedException { final String number = PhoneNumberUtil.getInstance().format( PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164); @@ -531,38 +535,58 @@ public class AccountCreationDeletionIntegrationTest { final KEMSignedPreKey aciPqLastResortPreKey = KeysHelper.signedKEMPreKey(3, aciKeyPair); final KEMSignedPreKey pniPqLastResortPreKey = KeysHelper.signedKEMPreKey(4, pniKeyPair); - final Account account = accountsManager.create(number, - accountAttributes, - badges, - new IdentityKey(aciKeyPair.getPublicKey()), - new IdentityKey(pniKeyPair.getPublicKey()), - new DeviceSpec( - deviceName, - password, - signalAgent, - deviceCapabilities, - new DeviceIdentityInfo(registrationId, aciSignedPreKey, aciPqLastResortPreKey), - Optional.of(new DeviceIdentityInfo(pniRegistrationId, pniSignedPreKey, pniPqLastResortPreKey)), - true, - Optional.empty(), - Optional.empty()), - null); + final Account account; + if (hasE164) { + account = accountsManager.create(number, + accountAttributes, + badges, + new IdentityKey(aciKeyPair.getPublicKey()), + new IdentityKey(pniKeyPair.getPublicKey()), + new DeviceSpec( + deviceName, + password, + signalAgent, + deviceCapabilities, + new DeviceIdentityInfo(registrationId, aciSignedPreKey, aciPqLastResortPreKey), + Optional.of(new DeviceIdentityInfo(pniRegistrationId, pniSignedPreKey, pniPqLastResortPreKey)), + true, + Optional.empty(), + Optional.empty()), + null); + assertTrue(phoneNumberRecoveryPasswordsManager.verify(account.getPhoneNumberIdentifierOptional().orElseThrow(), + accountAttributes.recoveryPassword().orElseThrow())); + } else { + account = accountsManager.create(accountAttributes, + badges, + new IdentityKey(aciKeyPair.getPublicKey()), + receiptPresentation(CLOCK.instant().plus(Duration.ofDays(30)), 1), + new DeviceSpec( + deviceName, + password, + signalAgent, + deviceCapabilities, + new DeviceIdentityInfo(registrationId, aciSignedPreKey, aciPqLastResortPreKey), + Optional.empty(), + true, + Optional.empty(), + Optional.empty()), + null); + } final UUID aci = account.getIdentifier(IdentityType.ACI); assertTrue(accountsManager.getByAccountIdentifier(aci).isPresent()); - assertTrue(phoneNumberRecoveryPasswordsManager.verify(account.getPhoneNumberIdentifierOptional().orElseThrow(), - accountAttributes.recoveryPassword().orElseThrow())); - accountsManager.delete(account.getIdentifier(IdentityType.ACI), AccountsManager.DeletionReason.ADMIN_DELETED); assertFalse(accountsManager.getByAccountIdentifier(aci).isPresent()); assertFalse(keysManager.getEcSignedPreKey(account.getAccountIdentifier(), Device.PRIMARY_ID).join().isPresent()); - assertFalse(keysManager.getEcSignedPreKey(account.getPhoneNumberIdentifierOptional().orElseThrow(), Device.PRIMARY_ID).join().isPresent()); assertFalse(keysManager.getLastResort(account.getAccountIdentifier(), Device.PRIMARY_ID).join().isPresent()); - assertFalse(keysManager.getLastResort(account.getPhoneNumberIdentifierOptional().orElseThrow(), Device.PRIMARY_ID).join().isPresent()); - assertFalse(phoneNumberRecoveryPasswordsManager.verify(account.getPhoneNumberIdentifierOptional().orElseThrow(), - accountAttributes.recoveryPassword().orElseThrow())); + if (hasE164) { + assertFalse(keysManager.getEcSignedPreKey(account.getPhoneNumberIdentifierOptional().orElseThrow(), Device.PRIMARY_ID).join().isPresent()); + assertFalse(keysManager.getLastResort(account.getPhoneNumberIdentifierOptional().orElseThrow(), Device.PRIMARY_ID).join().isPresent()); + assertFalse(phoneNumberRecoveryPasswordsManager.verify(account.getPhoneNumberIdentifierOptional().orElseThrow(), + accountAttributes.recoveryPassword().orElseThrow())); + } verify(disconnectionRequestManager).requestDisconnection(argThat(disconnectedAccount -> disconnectedAccount.getIdentifier(IdentityType.ACI).equals(account.getIdentifier(IdentityType.ACI)))); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java index ed93ec27f..7f8b7edc6 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java @@ -63,6 +63,8 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; import org.junitpioneer.jupiter.cartesian.ArgumentSets; import org.junitpioneer.jupiter.cartesian.CartesianTest; +import org.signal.libsignal.zkgroup.InvalidInputException; +import org.signal.libsignal.zkgroup.VerificationFailedException; import org.signal.libsignal.zkgroup.ZkCredentialKeyPair; import org.signal.libsignal.zkgroup.backups.BackupCredentialType; import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialPresentation; @@ -1108,6 +1110,16 @@ class AccountsTest { } } + @Test + void testDeleteNumberless() throws InvalidInputException, VerificationFailedException { + final Account deletedAccount = generateNumberlessAccount(UUID.randomUUID()); + createNumberlessAccount(deletedAccount, receiptPresentation()); + + assertThat(accounts.getByAccountIdentifier(deletedAccount.getAccountIdentifier())).isPresent(); + accounts.delete(deletedAccount.getAccountIdentifier(), Collections.emptyList()); + assertThat(accounts.getByAccountIdentifier(deletedAccount.getAccountIdentifier())).isNotPresent(); + } + @ParameterizedTest @ValueSource(strings = {"+14151112222"}) @NullSource