From fea4300d7d1f902de13b17b244b564234d478d2e Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Tue, 19 May 2026 17:47:05 -0500 Subject: [PATCH] Convert RegistrationRecoveryPasswords to sync DynamoDB client --- .../signal/integration/IntegrationTools.java | 21 +++-- .../org/signal/integration/Operations.java | 2 +- .../textsecuregcm/WhisperServerService.java | 2 +- .../auth/PhoneVerificationTokenManager.java | 6 +- .../RegistrationLockVerificationManager.java | 2 +- .../controllers/AccountController.java | 3 +- .../controllers/VerificationController.java | 4 +- .../grpc/AccountsGrpcService.java | 3 +- .../storage/AccountsManager.java | 8 +- .../RegistrationRecoveryPasswords.java | 72 ++++++++-------- .../RegistrationRecoveryPasswordsManager.java | 36 ++------ .../workers/CommandDependencies.java | 2 +- .../PhoneVerificationTokenManagerTest.java | 17 ++-- ...gistrationLockVerificationManagerTest.java | 3 - .../controllers/AccountControllerTest.java | 2 - .../VerificationControllerTest.java | 10 --- .../grpc/AccountsGrpcServiceTest.java | 4 - ...ccountCreationDeletionIntegrationTest.java | 5 +- ...ntsManagerChangeNumberIntegrationTest.java | 3 - .../storage/AccountsManagerTest.java | 2 - .../AddRemoveDeviceIntegrationTest.java | 3 - .../storage/RegistrationRecoveryTest.java | 86 +++++++++---------- 22 files changed, 125 insertions(+), 171 deletions(-) diff --git a/integration-tests/src/main/java/org/signal/integration/IntegrationTools.java b/integration-tests/src/main/java/org/signal/integration/IntegrationTools.java index 11b441824..b736abd22 100644 --- a/integration-tests/src/main/java/org/signal/integration/IntegrationTools.java +++ b/integration-tests/src/main/java/org/signal/integration/IntegrationTools.java @@ -8,7 +8,10 @@ package org.signal.integration; import java.time.Clock; import java.time.Duration; import java.util.Optional; -import java.util.concurrent.CompletableFuture; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.signal.integration.config.Config; import org.whispersystems.textsecuregcm.metrics.NoopAwsSdkMetricPublisher; import org.whispersystems.textsecuregcm.registration.VerificationSession; @@ -18,7 +21,6 @@ import org.whispersystems.textsecuregcm.storage.RegistrationRecoveryPasswords; import org.whispersystems.textsecuregcm.storage.RegistrationRecoveryPasswordsManager; import org.whispersystems.textsecuregcm.storage.VerificationSessionManager; import org.whispersystems.textsecuregcm.storage.VerificationSessions; -import org.whispersystems.textsecuregcm.util.Util; import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; @@ -44,7 +46,7 @@ public class IntegrationTools { config.dynamoDbClient().buildSyncClient(credentialsProvider, new NoopAwsSdkMetricPublisher()); final RegistrationRecoveryPasswords registrationRecoveryPasswords = new RegistrationRecoveryPasswords( - config.dynamoDbTables().registrationRecovery(), Duration.ofDays(1), dynamoDbAsyncClient, Clock.systemUTC()); + config.dynamoDbTables().registrationRecovery(), Duration.ofDays(1), dynamoDbClient, Clock.systemUTC()); final VerificationSessions verificationSessions = new VerificationSessions( dynamoDbClient, config.dynamoDbTables().verificationSessions(), Clock.systemUTC()); @@ -68,11 +70,14 @@ public class IntegrationTools { this.changeNumberWaitingPeriods = changeNumberWaitingPeriods; } - public CompletableFuture populateRecoveryPassword(final String phoneNumber, final byte[] password) { - return phoneNumberIdentifiers - .getPhoneNumberIdentifier(phoneNumber) - .thenCompose(pni -> registrationRecoveryPasswordsManager.store(pni, password)) - .thenRun(Util.NOOP); + public void populateRecoveryPassword(final String phoneNumber, final byte[] password) { + try { + final UUID pni = phoneNumberIdentifiers + .getPhoneNumberIdentifier(phoneNumber).get(5, TimeUnit.SECONDS); + registrationRecoveryPasswordsManager.store(pni, password); + } catch (ExecutionException | InterruptedException | TimeoutException e) { + throw new RuntimeException("failed to get pni", e); + } } public Optional peekVerificationSessionPushChallenge(final String sessionId) { diff --git a/integration-tests/src/main/java/org/signal/integration/Operations.java b/integration-tests/src/main/java/org/signal/integration/Operations.java index 04ce4960c..8fcc758a1 100644 --- a/integration-tests/src/main/java/org/signal/integration/Operations.java +++ b/integration-tests/src/main/java/org/signal/integration/Operations.java @@ -123,7 +123,7 @@ public final class Operations { public static byte[] populateRandomRecoveryPassword(final String number) { final byte[] recoveryPassword = randomBytes(32); - INTEGRATION_TOOLS.populateRecoveryPassword(number, recoveryPassword).join(); + INTEGRATION_TOOLS.populateRecoveryPassword(number, recoveryPassword); return recoveryPassword; } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 201de29f5..e9d972e9d 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -516,7 +516,7 @@ public class WhisperServerService extends Application registrationRecoveryPasswordsManager.verify(phoneNumberIdentifier, recoveryPassword)) + final UUID phoneNumberIdentifier = phoneNumberIdentifiers.getPhoneNumberIdentifier(number) .get(VERIFICATION_TIMEOUT_SECONDS, TimeUnit.SECONDS); + final boolean verified = registrationRecoveryPasswordsManager.verify(phoneNumberIdentifier, recoveryPassword); + if (!verified) { throw new ForbiddenException("recoveryPassword couldn't be verified"); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManager.java index 6e290423b..aca66c815 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManager.java @@ -152,7 +152,7 @@ public class RegistrationLockVerificationManager { // This allows users to re-register via registration recovery password // instead of always being forced to fall back to SMS verification. if (!phoneVerificationType.equals(PhoneVerificationRequest.VerificationType.RECOVERY_PASSWORD) || clientRegistrationLock != null) { - registrationRecoveryPasswordsManager.remove(updatedAccount.getIdentifier(IdentityType.PNI)).join(); + registrationRecoveryPasswordsManager.remove(updatedAccount.getIdentifier(IdentityType.PNI)); } final List deviceIds = updatedAccount.getDevices().stream().map(Device::getId).toList(); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java index 75ae21e3a..c41c1a26b 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java @@ -244,8 +244,7 @@ public class AccountController { // if registration recovery password was sent to us, store it (or refresh its expiration) attributes.recoveryPassword().ifPresent(registrationRecoveryPassword -> { final boolean rrpCreated = registrationRecoveryPasswordsManager - .store(updatedAccount.getIdentifier(IdentityType.PNI), registrationRecoveryPassword) - .join(); + .store(updatedAccount.getIdentifier(IdentityType.PNI), registrationRecoveryPassword); Metrics.counter(RECOVERY_PASSWORD_SET_COUNTER_NAME, Tags.of( UserAgentTagUtil.getPlatformTag(userAgent), Tag.of("outcome", rrpCreated ? "created" : "updated"))) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java index 43a740441..6e914e5ff 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/VerificationController.java @@ -793,7 +793,7 @@ public class VerificationController { // the RRP. It's possible the client will not actually be able to register (e.g. failed reglock challenge), and // so we will have removed the RRP unnecessarily. The impact of this is low, since the owner of the RRP // can always just fallback to session-based verification. - existingRRP = registrationRecoveryPasswordsManager.remove(phoneNumberIdentifiers.getPhoneNumberIdentifier(registrationServiceSession.number()).join()).join(); + existingRRP = registrationRecoveryPasswordsManager.remove(phoneNumberIdentifiers.getPhoneNumberIdentifier(registrationServiceSession.number()).join()); } Optional maybeExistingAccount; @@ -869,7 +869,7 @@ public class VerificationController { // the RRP. It's possible the client will not actually be able to register (e.g. failed reglock challenge), and // so we will have removed the RRP unnecessarily. The impact of this is low, since the owner of the RRP // can always just fallback to session-based verification. - registrationRecoveryPasswordsManager.remove(phoneNumberIdentifiers.getPhoneNumberIdentifier(registrationServiceSession.number()).join()).join(); + registrationRecoveryPasswordsManager.remove(phoneNumberIdentifiers.getPhoneNumberIdentifier(registrationServiceSession.number()).join()); } return registrationServiceSession; diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java index 7c3775aea..8a2ce1c65 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcService.java @@ -263,8 +263,7 @@ public class AccountsGrpcService extends SimpleAccountsGrpc.AccountsImplBase { @Override public SetRegistrationRecoveryPasswordResponse setRegistrationRecoveryPassword(final SetRegistrationRecoveryPasswordRequest request) { registrationRecoveryPasswordsManager.store(getAuthenticatedAccount().getIdentifier(IdentityType.PNI), - request.getRegistrationRecoveryPassword().toByteArray()) - .join(); + request.getRegistrationRecoveryPassword().toByteArray()); return SetRegistrationRecoveryPasswordResponse.getDefaultInstance(); } 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 a69f091c1..d4392e272 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -413,8 +413,7 @@ public class AccountsManager extends RedisPubSubAdapter implemen final boolean rrpCreated = accountAttributes.recoveryPassword().map(registrationRecoveryPassword -> registrationRecoveryPasswordsManager - .store(account.getIdentifier(IdentityType.PNI), registrationRecoveryPassword) - .join()) + .store(account.getIdentifier(IdentityType.PNI), registrationRecoveryPassword)) .orElse(false); changeNumberWaitingPeriodManager.handleAccountCreated(account.getUuid(), clock.instant()); @@ -1214,10 +1213,11 @@ public class AccountsManager extends RedisPubSubAdapter implemen keysManager.deleteSingleUsePreKeys(account.getUuid()), keysManager.deleteSingleUsePreKeys(account.getPhoneNumberIdentifier()), messagesManager.clear(account.getUuid()), - profilesManager.deleteAll(account.getUuid(), true), - registrationRecoveryPasswordsManager.remove(account.getIdentifier(IdentityType.PNI))) + profilesManager.deleteAll(account.getUuid(), true)) .join(); + registrationRecoveryPasswordsManager.remove(account.getIdentifier(IdentityType.PNI)); + accounts.delete(account.getUuid(), additionalWriteItems); redisDelete(account); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswords.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswords.java index 45fa0a6f2..a9ff277a4 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswords.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswords.java @@ -13,15 +13,16 @@ import java.time.Duration; import java.util.Map; import java.util.Optional; import java.util.UUID; -import java.util.concurrent.CompletableFuture; import org.whispersystems.textsecuregcm.auth.SaltedTokenHash; import org.whispersystems.textsecuregcm.util.AttributeValues; -import org.whispersystems.textsecuregcm.util.Util; -import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest; +import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; +import software.amazon.awssdk.services.dynamodb.model.GetItemResponse; import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; +import software.amazon.awssdk.services.dynamodb.model.PutItemResponse; import software.amazon.awssdk.services.dynamodb.model.ReturnValue; public class RegistrationRecoveryPasswords { @@ -36,31 +37,32 @@ public class RegistrationRecoveryPasswords { private final Duration expiration; - private final DynamoDbAsyncClient asyncClient; + private final DynamoDbClient dynamoDbClient; private final Clock clock; public RegistrationRecoveryPasswords( final String tableName, final Duration expiration, - final DynamoDbAsyncClient asyncClient, + final DynamoDbClient dynamoDbClient, final Clock clock) { this.tableName = requireNonNull(tableName); this.expiration = requireNonNull(expiration); - this.asyncClient = requireNonNull(asyncClient); + this.dynamoDbClient = requireNonNull(dynamoDbClient); this.clock = requireNonNull(clock); } - public CompletableFuture> lookup(final UUID phoneNumberIdentifier) { - return asyncClient.getItem(GetItemRequest.builder() - .tableName(tableName) - .key(Map.of(KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString()))) - .consistentRead(true) - .build()) - .thenApply(getItemResponse -> Optional.ofNullable(getItemResponse.item()) - .filter(item -> item.containsKey(ATTR_SALT)) - .filter(item -> item.containsKey(ATTR_HASH)) - .map(RegistrationRecoveryPasswords::saltedTokenHashFromItem)); + public Optional lookup(final UUID phoneNumberIdentifier) { + final GetItemResponse getItemResponse = dynamoDbClient.getItem(GetItemRequest.builder() + .tableName(tableName) + .key(Map.of(KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString()))) + .consistentRead(true) + .build()); + + return Optional.ofNullable(getItemResponse.item()) + .filter(item -> item.containsKey(ATTR_SALT)) + .filter(item -> item.containsKey(ATTR_HASH)) + .map(RegistrationRecoveryPasswords::saltedTokenHashFromItem); } /// Add a PNI -> RRP mapping, or replace the current one if it already exists @@ -68,31 +70,33 @@ public class RegistrationRecoveryPasswords { /// @param phoneNumberIdentifier The PNI to associate the salted RRP with /// @param data The salted registration recovery password /// @return true if a new mapping was added, false if an existing mapping was updated - public CompletableFuture addOrReplace(final UUID phoneNumberIdentifier, final SaltedTokenHash data) { + public boolean addOrReplace(final UUID phoneNumberIdentifier, final SaltedTokenHash data) { final long expirationSeconds = expirationSeconds(); - return asyncClient.putItem(PutItemRequest.builder() - .tableName(tableName) - .returnValues(ReturnValue.ALL_OLD) - .item(Map.of( - KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString()), - ATTR_EXP, AttributeValues.fromLong(expirationSeconds), - ATTR_SALT, AttributeValues.fromString(data.salt()), - ATTR_HASH, AttributeValues.fromString(data.hash()))) - .build()) - .thenApply(response -> response.attributes() == null || response.attributes().isEmpty()); + final PutItemResponse response = dynamoDbClient.putItem(PutItemRequest.builder() + .tableName(tableName) + .returnValues(ReturnValue.ALL_OLD) + .item(Map.of( + KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString()), + ATTR_EXP, AttributeValues.fromLong(expirationSeconds), + ATTR_SALT, AttributeValues.fromString(data.salt()), + ATTR_HASH, AttributeValues.fromString(data.hash()))) + .build()); + + return response.attributes() == null || response.attributes().isEmpty(); } /// Remove the entry associated with the provided PNI /// /// @return true if an entry was removed, false if no entry existed - public CompletableFuture removeEntry(final UUID phoneNumberIdentifier) { - return asyncClient.deleteItem(DeleteItemRequest.builder() - .tableName(tableName) - .returnValues(ReturnValue.ALL_OLD) - .key(Map.of(KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString()))) - .build()) - .thenApply(response -> response.attributes() != null && !response.attributes().isEmpty()); + public boolean removeEntry(final UUID phoneNumberIdentifier) { + final DeleteItemResponse response = dynamoDbClient.deleteItem(DeleteItemRequest.builder() + .tableName(tableName) + .returnValues(ReturnValue.ALL_OLD) + .key(Map.of(KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString()))) + .build()); + + return response.attributes() != null && !response.attributes().isEmpty(); } @VisibleForTesting diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswordsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswordsManager.java index 713095f15..1fe3e040a 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswordsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryPasswordsManager.java @@ -7,56 +7,32 @@ package org.whispersystems.textsecuregcm.storage; import static java.util.Objects.requireNonNull; -import java.lang.invoke.MethodHandles; import java.util.HexFormat; -import java.util.Optional; import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.whispersystems.textsecuregcm.auth.SaltedTokenHash; -import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; public class RegistrationRecoveryPasswordsManager { - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private final RegistrationRecoveryPasswords registrationRecoveryPasswords; public RegistrationRecoveryPasswordsManager(final RegistrationRecoveryPasswords registrationRecoveryPasswords) { this.registrationRecoveryPasswords = requireNonNull(registrationRecoveryPasswords); } - public CompletableFuture verify(final UUID phoneNumberIdentifier, final byte[] password) { + public boolean verify(final UUID phoneNumberIdentifier, final byte[] password) { return registrationRecoveryPasswords.lookup(phoneNumberIdentifier) - .thenApply(maybeHash -> maybeHash.filter(hash -> hash.verify(bytesToString(password)))) - .whenComplete((_, error) -> { - if (error != null) { - logger.warn("Failed to lookup Registration Recovery Password", error); - } - }) - .thenApply(Optional::isPresent); + .filter(hash -> hash.verify(bytesToString(password))).isPresent(); } - public CompletableFuture store(final UUID phoneNumberIdentifier, final byte[] password) { + public boolean store(final UUID phoneNumberIdentifier, final byte[] password) { final String token = bytesToString(password); final SaltedTokenHash tokenHash = SaltedTokenHash.generateFor(token); - return registrationRecoveryPasswords.addOrReplace(phoneNumberIdentifier, tokenHash) - .whenComplete((_, error) -> { - if (error != null) { - logger.warn("Failed to store Registration Recovery Password", error); - } - }); + return registrationRecoveryPasswords.addOrReplace(phoneNumberIdentifier, tokenHash); } - public CompletableFuture remove(final UUID phoneNumberIdentifier) { - return registrationRecoveryPasswords.removeEntry(phoneNumberIdentifier) - .whenComplete((_, error) -> { - if (error != null) { - logger.warn("Failed to remove Registration Recovery Password", error); - } - }); + public boolean remove(final UUID phoneNumberIdentifier) { + return registrationRecoveryPasswords.removeEntry(phoneNumberIdentifier); } private static String bytesToString(final byte[] bytes) { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java b/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java index 3c4d4e71b..5af83a5ad 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java @@ -208,7 +208,7 @@ public record CommandDependencies( RegistrationRecoveryPasswords registrationRecoveryPasswords = new RegistrationRecoveryPasswords( configuration.getDynamoDbTables().getRegistrationRecovery().getTableName(), configuration.getDynamoDbTables().getRegistrationRecovery().getExpiration(), - dynamoDbAsyncClient, + dynamoDbClient, clock); Accounts accounts = new Accounts( diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java index d21cc4cda..42d1e0006 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/auth/PhoneVerificationTokenManagerTest.java @@ -45,6 +45,7 @@ class PhoneVerificationTokenManagerTest { private RegistrationServiceClient registrationServiceClient; private RegistrationRecoveryPasswordsManager registrationRecoveryPasswordsManager; private RegistrationRecoveryChecker registrationRecoveryChecker; + private PhoneNumberIdentifiers phoneNumberIdentifiers; private PhoneVerificationTokenManager phoneVerificationTokenManager; @@ -55,7 +56,7 @@ class PhoneVerificationTokenManagerTest { @BeforeEach void setUp() { - final PhoneNumberIdentifiers phoneNumberIdentifiers = mock(PhoneNumberIdentifiers.class); + phoneNumberIdentifiers = mock(PhoneNumberIdentifiers.class); when(phoneNumberIdentifiers.getPhoneNumberIdentifier(PHONE_NUMBER)) .thenReturn(CompletableFuture.completedFuture(PHONE_NUMBER_IDENTIFIER)); @@ -166,7 +167,7 @@ class PhoneVerificationTokenManagerTest { .thenReturn(true); when(registrationRecoveryPasswordsManager.verify(PHONE_NUMBER_IDENTIFIER, recoveryPassword)) - .thenReturn(CompletableFuture.completedFuture(true)); + .thenReturn(true); assertDoesNotThrow(() -> phoneVerificationTokenManager.verify(containerRequestContext, PHONE_NUMBER, @@ -183,7 +184,7 @@ class PhoneVerificationTokenManagerTest { .thenReturn(false); when(registrationRecoveryPasswordsManager.verify(PHONE_NUMBER_IDENTIFIER, recoveryPassword)) - .thenReturn(CompletableFuture.completedFuture(true)); + .thenReturn(true); assertThrows(ForbiddenException.class, () -> phoneVerificationTokenManager.verify(containerRequestContext, PHONE_NUMBER, @@ -200,7 +201,7 @@ class PhoneVerificationTokenManagerTest { .thenReturn(true); when(registrationRecoveryPasswordsManager.verify(PHONE_NUMBER_IDENTIFIER, recoveryPassword)) - .thenReturn(CompletableFuture.completedFuture(false)); + .thenReturn(false); assertThrows(ForbiddenException.class, () -> phoneVerificationTokenManager.verify(containerRequestContext, PHONE_NUMBER, @@ -210,7 +211,7 @@ class PhoneVerificationTokenManagerTest { @ParameterizedTest @MethodSource - void verifyRecoveryPasswordManagerException(final Throwable recoveryPasswordManagerException) { + void verifyPhoneNumberIdentifiersException(final Throwable phoneNumberIdentifiersException) { final ContainerRequestContext containerRequestContext = mock(ContainerRequestContext.class); final byte[] recoveryPassword = TestRandomUtil.nextBytes(16); @@ -218,8 +219,8 @@ class PhoneVerificationTokenManagerTest { when(registrationRecoveryChecker.checkRegistrationRecoveryAttempt(containerRequestContext, PHONE_NUMBER)) .thenReturn(true); - when(registrationRecoveryPasswordsManager.verify(PHONE_NUMBER_IDENTIFIER, recoveryPassword)) - .thenReturn(CompletableFuture.failedFuture(recoveryPasswordManagerException)); + when(phoneNumberIdentifiers.getPhoneNumberIdentifier(PHONE_NUMBER)) + .thenReturn(CompletableFuture.failedFuture(phoneNumberIdentifiersException)); assertThrows(ServerErrorException.class, () -> phoneVerificationTokenManager.verify(containerRequestContext, PHONE_NUMBER, @@ -227,7 +228,7 @@ class PhoneVerificationTokenManagerTest { recoveryPassword)); } - private static List verifyRecoveryPasswordManagerException() { + private static List verifyPhoneNumberIdentifiersException() { return List.of(new ExecutionException(new RuntimeException()), new TimeoutException()); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManagerTest.java index 3d1200578..357aaff2a 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/auth/RegistrationLockVerificationManagerTest.java @@ -22,7 +22,6 @@ import static org.mockito.Mockito.when; import jakarta.ws.rs.WebApplicationException; import java.util.List; import java.util.UUID; -import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.stream.Stream; import javax.annotation.Nullable; @@ -99,8 +98,6 @@ class RegistrationLockVerificationManagerTest { when(account.hasLockedCredentials()).thenReturn(alreadyLocked); doThrow(new NotPushRegisteredException()).when(pushNotificationManager).sendAttemptLoginNotification(any(), any()); - when(registrationRecoveryPasswordsManager.remove(any())).thenReturn(CompletableFuture.completedFuture(true)); - final Pair, Consumer> exceptionType = switch (error) { case MISMATCH -> { when(existingRegistrationLock.verify(clientRegistrationLock)).thenReturn(false); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java index 068d18764..9c58b3876 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java @@ -833,8 +833,6 @@ class AccountControllerTest { void testAccountsAttributesUpdateRecoveryPassword() { final byte[] recoveryPassword = TestRandomUtil.nextBytes(32); - when(registrationRecoveryPasswordsManager.store(any(), any())) - .thenReturn(CompletableFuture.completedFuture(true)); try (final Response response = resources.getJerseyTest() .target("/v1/accounts/attributes/") .request() diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java index d76f9400b..e1439af6f 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/VerificationControllerTest.java @@ -618,9 +618,6 @@ class VerificationControllerTest { .thenReturn(Optional.of(new VerificationSession(encodedSessionId, "challenge", null, List.of(), List.of(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); - when(registrationRecoveryPasswordsManager.remove(PNI)) - .thenReturn(CompletableFuture.completedFuture(null)); - final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId) .request() @@ -918,8 +915,6 @@ class VerificationControllerTest { registrationServiceSession))); when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(mock(VerificationSession.class))); - when(registrationRecoveryPasswordsManager.remove(PNI)) - .thenReturn(CompletableFuture.completedFuture(null)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId) @@ -945,7 +940,6 @@ class VerificationControllerTest { clock.millis(), clock.millis(), registrationServiceSession.expiration()))); when(registrationServiceClient.sendVerificationCode(any(), any(), any(), any(), any(), any())) .thenReturn(CompletableFuture.completedFuture(registrationServiceSession)); - when(registrationRecoveryPasswordsManager.remove(PNI)).thenReturn(CompletableFuture.completedFuture(null)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1247,8 +1241,6 @@ class VerificationControllerTest { when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); - when(registrationRecoveryPasswordsManager.remove(PNI)) - .thenReturn(CompletableFuture.completedFuture(null)); final Invocation.Builder request = resources.getJerseyTest() .target("/v1/verification/session/" + encodedSessionId + "/code") @@ -1375,8 +1367,6 @@ class VerificationControllerTest { when(verificationSessionManager.findForId(any())) .thenReturn(Optional.of(new VerificationSession(encodedSessionId, null, null, Collections.emptyList(), Collections.emptyList(), null, null, true, clock.millis(), clock.millis(), registrationServiceSession.expiration()))); - when(registrationRecoveryPasswordsManager.remove(any())) - .thenReturn(CompletableFuture.completedFuture(true)); final RegistrationServiceSession verifiedSession = new RegistrationServiceSession(SESSION_ID, NUMBER, true, null, null, 0L, diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java index 799e97618..93c6b75f2 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AccountsGrpcServiceTest.java @@ -26,7 +26,6 @@ import java.util.HexFormat; import java.util.List; import java.util.Optional; import java.util.UUID; -import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -106,9 +105,6 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest assertNull(primaryDevice.getApnId())); maybeGcmRegistrationId.ifPresentOrElse( - gcmRegistrationId -> assertEquals(deliveryChannels.fcmToken(), primaryDevice.getGcmId()), + _ -> assertEquals(deliveryChannels.fcmToken(), primaryDevice.getGcmId()), () -> assertNull(primaryDevice.getGcmId())); assertTrue(account.getRegistrationLock().verify(registrationLockSecret)); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerChangeNumberIntegrationTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerChangeNumberIntegrationTest.java index e2cbe3723..efc6670a0 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerChangeNumberIntegrationTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerChangeNumberIntegrationTest.java @@ -126,9 +126,6 @@ class AccountsManagerChangeNumberIntegrationTest { final RegistrationRecoveryPasswordsManager registrationRecoveryPasswordsManager = mock(RegistrationRecoveryPasswordsManager.class); - when(registrationRecoveryPasswordsManager.remove(any())) - .thenReturn(CompletableFuture.completedFuture(null)); - accountsManager = new AccountsManager( accounts, phoneNumberIdentifiers, diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java index c1aa3e0f8..5d9708225 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java @@ -197,8 +197,6 @@ class AccountsManagerTest { final RegistrationRecoveryPasswordsManager registrationRecoveryPasswordsManager = mock(RegistrationRecoveryPasswordsManager.class); - when(registrationRecoveryPasswordsManager.remove(any())).thenReturn(CompletableFuture.completedFuture(null)); - when(keysManager.deleteSingleUsePreKeys(any())).thenReturn(CompletableFuture.completedFuture(null)); when(messagesManager.clear(any())).thenReturn(CompletableFuture.completedFuture(null)); when(profilesManager.deleteAll(any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null)); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java index ef7eaa002..6406591bb 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AddRemoveDeviceIntegrationTest.java @@ -131,9 +131,6 @@ public class AddRemoveDeviceIntegrationTest { final RegistrationRecoveryPasswordsManager registrationRecoveryPasswordsManager = mock(RegistrationRecoveryPasswordsManager.class); - when(registrationRecoveryPasswordsManager.remove(any())) - .thenReturn(CompletableFuture.completedFuture(null)); - PUBSUB_SERVER_EXTENSION.getRedisClient().useConnection(connection -> { connection.sync().flushall(); connection.sync().configSet("notify-keyspace-events", "K$"); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryTest.java index 92ab3b1bf..a75f284d0 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/RegistrationRecoveryTest.java @@ -15,7 +15,6 @@ import java.time.Duration; import java.util.Map; import java.util.Optional; import java.util.UUID; -import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -27,6 +26,7 @@ import org.whispersystems.textsecuregcm.util.MockUtils; import org.whispersystems.textsecuregcm.util.MutableClock; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; +import software.amazon.awssdk.services.dynamodb.model.GetItemResponse; public class RegistrationRecoveryTest { @@ -51,7 +51,7 @@ public class RegistrationRecoveryTest { registrationRecoveryPasswords = new RegistrationRecoveryPasswords( Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(), EXPIRATION, - DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(), + DYNAMO_DB_EXTENSION.getDynamoDbClient(), CLOCK ); @@ -60,12 +60,12 @@ public class RegistrationRecoveryTest { @Test public void testLookupAfterWrite() throws Exception { - assertTrue(registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH).get()); + assertTrue(registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH)); final long initialExp = fetchTimestamp(PNI); final long expectedExpiration = CLOCK.instant().getEpochSecond() + EXPIRATION.getSeconds(); assertEquals(expectedExpiration, initialExp); - final Optional saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI).get(); + final Optional saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI); assertTrue(saltedTokenHashByPni.isPresent()); assertEquals(ORIGINAL_HASH.salt(), saltedTokenHashByPni.get().salt()); assertEquals(ORIGINAL_HASH.hash(), saltedTokenHashByPni.get().hash()); @@ -73,83 +73,81 @@ public class RegistrationRecoveryTest { @Test public void testLookupAfterRefresh() throws Exception { - registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH).get(); + registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH); CLOCK.increment(50, TimeUnit.SECONDS); - registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH).get(); + registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH); final long updatedExp = fetchTimestamp(PNI); final long expectedExp = CLOCK.instant().getEpochSecond() + EXPIRATION.getSeconds(); assertEquals(expectedExp, updatedExp); - final Optional saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI).get(); + final Optional saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI); assertTrue(saltedTokenHashByPni.isPresent()); assertEquals(ORIGINAL_HASH.salt(), saltedTokenHashByPni.get().salt()); assertEquals(ORIGINAL_HASH.hash(), saltedTokenHashByPni.get().hash()); } @Test - public void testReplace() throws Exception { - assertTrue(registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH).get()); - assertFalse(registrationRecoveryPasswords.addOrReplace(PNI, ANOTHER_HASH).get()); + public void testReplace() { + assertTrue(registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH)); + assertFalse(registrationRecoveryPasswords.addOrReplace(PNI, ANOTHER_HASH)); - final Optional saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI).get(); + final Optional saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI); assertTrue(saltedTokenHashByPni.isPresent()); assertEquals(ANOTHER_HASH.salt(), saltedTokenHashByPni.get().salt()); assertEquals(ANOTHER_HASH.hash(), saltedTokenHashByPni.get().hash()); } @Test - public void testRemove() throws Exception { - assertFalse(registrationRecoveryPasswords.removeEntry(PNI).join()); + public void testRemove() { + assertFalse(registrationRecoveryPasswords.removeEntry(PNI)); - registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH).get(); - assertTrue(registrationRecoveryPasswords.lookup(PNI).get().isPresent()); + registrationRecoveryPasswords.addOrReplace(PNI, ORIGINAL_HASH); + assertTrue(registrationRecoveryPasswords.lookup(PNI).isPresent()); - assertTrue(registrationRecoveryPasswords.removeEntry(PNI).get()); - assertTrue(registrationRecoveryPasswords.lookup(PNI).get().isEmpty()); + assertTrue(registrationRecoveryPasswords.removeEntry(PNI)); + assertTrue(registrationRecoveryPasswords.lookup(PNI).isEmpty()); } @Test - public void testManagerFlow() throws Exception { + public void testManagerFlow() { final byte[] password = "password".getBytes(StandardCharsets.UTF_8); final byte[] updatedPassword = "udpate".getBytes(StandardCharsets.UTF_8); final byte[] wrongPassword = "qwerty123".getBytes(StandardCharsets.UTF_8); // initial store - manager.store(PNI, password).get(); - assertTrue(manager.verify(PNI, password).get()); - assertFalse(manager.verify(PNI, wrongPassword).get()); + manager.store(PNI, password); + assertTrue(manager.verify(PNI, password)); + assertFalse(manager.verify(PNI, wrongPassword)); // update - manager.store(PNI, password).get(); - assertTrue(manager.verify(PNI, password).get()); - assertFalse(manager.verify(PNI, wrongPassword).get()); + manager.store(PNI, password); + assertTrue(manager.verify(PNI, password)); + assertFalse(manager.verify(PNI, wrongPassword)); // replace - manager.store(PNI, updatedPassword).get(); - assertTrue(manager.verify(PNI, updatedPassword).get()); - assertFalse(manager.verify(PNI, password).get()); - assertFalse(manager.verify(PNI, wrongPassword).get()); + manager.store(PNI, updatedPassword); + assertTrue(manager.verify(PNI, updatedPassword)); + assertFalse(manager.verify(PNI, password)); + assertFalse(manager.verify(PNI, wrongPassword)); - manager.remove(PNI).get(); - assertFalse(manager.verify(PNI, updatedPassword).get()); - assertFalse(manager.verify(PNI, password).get()); - assertFalse(manager.verify(PNI, wrongPassword).get()); + manager.remove(PNI); + assertFalse(manager.verify(PNI, updatedPassword)); + assertFalse(manager.verify(PNI, password)); + assertFalse(manager.verify(PNI, wrongPassword)); } - private static long fetchTimestamp(final UUID phoneNumberIdentifier) throws ExecutionException, InterruptedException { - return DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient().getItem(GetItemRequest.builder() + private static long fetchTimestamp(final UUID phoneNumberIdentifier) { + final GetItemResponse getItemResponse = DYNAMO_DB_EXTENSION.getDynamoDbClient().getItem(GetItemRequest.builder() .tableName(Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName()) .key(Map.of(RegistrationRecoveryPasswords.KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString()))) - .build()) - .thenApply(getItemResponse -> { - final Map item = getItemResponse.item(); - if (item == null || !item.containsKey(RegistrationRecoveryPasswords.ATTR_EXP)) { - throw new RuntimeException("Data not found"); - } - final String exp = item.get(RegistrationRecoveryPasswords.ATTR_EXP).n(); - return Long.parseLong(exp); - }) - .get(); + .build()); + + final Map item = getItemResponse.item(); + if (item == null || !item.containsKey(RegistrationRecoveryPasswords.ATTR_EXP)) { + throw new RuntimeException("Data not found"); + } + final String exp = item.get(RegistrationRecoveryPasswords.ATTR_EXP).n(); + return Long.parseLong(exp); } }