Convert RegistrationRecoveryPasswords to sync DynamoDB client

This commit is contained in:
Chris Eager authored and Jon Chambers committed 2026-05-21 13:08:49 -04:00
1 parent 66b0ed16d1
commit fea4300d7d
22 files changed
+125 -171

No files matched your search

@@ -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<Throwable> verifyRecoveryPasswordManagerException() {
private static List<Throwable> verifyPhoneNumberIdentifiersException() {
return List.of(new ExecutionException(new RuntimeException()), new TimeoutException());
}
}
@@ -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<Class<? extends Exception>, Consumer<Exception>> exceptionType = switch (error) {
case MISMATCH -> {
when(existingRegistrationLock.verify(clientRegistrationLock)).thenReturn(false);
@@ -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()
@@ -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,
@@ -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<AccountsGrpcService, Ac
when(rateLimiters.getSetZkCredentialKeyLimiter()).thenReturn(rateLimiter);
when(registrationRecoveryPasswordsManager.store(any(), any()))
.thenReturn(CompletableFuture.completedFuture(null));
return new AccountsGrpcService(accountsManager,
rateLimiters,
usernameHashZkProofVerifier,
@@ -134,9 +134,6 @@ public class AccountCreationDeletionIntegrationTest {
final RegistrationRecoveryPasswordsManager registrationRecoveryPasswordsManager =
mock(RegistrationRecoveryPasswordsManager.class);
when(registrationRecoveryPasswordsManager.remove(any()))
.thenReturn(CompletableFuture.completedFuture(null));
disconnectionRequestManager = mock(DisconnectionRequestManager.class);
when(disconnectionRequestManager.requestDisconnection(any())).thenReturn(CompletableFuture.completedFuture(null));
@@ -513,7 +510,7 @@ public class AccountCreationDeletionIntegrationTest {
() -> assertNull(primaryDevice.getApnId()));
maybeGcmRegistrationId.ifPresentOrElse(
gcmRegistrationId -> assertEquals(deliveryChannels.fcmToken(), primaryDevice.getGcmId()),
_ -> assertEquals(deliveryChannels.fcmToken(), primaryDevice.getGcmId()),
() -> assertNull(primaryDevice.getGcmId()));
assertTrue(account.getRegistrationLock().verify(registrationLockSecret));
@@ -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,
@@ -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));
@@ -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$");
@@ -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<SaltedTokenHash> saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI).get();
final Optional<SaltedTokenHash> 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<SaltedTokenHash> saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI).get();
final Optional<SaltedTokenHash> 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<SaltedTokenHash> saltedTokenHashByPni = registrationRecoveryPasswords.lookup(PNI).get();
final Optional<SaltedTokenHash> 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<String, AttributeValue> 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<String, AttributeValue> 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);
}
}