Don't assume presence of phone numbers when regenerating secondary tables

This commit is contained in:
Jon Chambers authored and GitHub committed 2026-07-29 15:42:13 -07:00
1 parent f55a0af55e
commit 850286dbfc
3 files changed
+22 -17

No files matched your search

@@ -39,7 +39,6 @@ import org.apache.commons.lang3.StringUtils;
import org.signal.libsignal.zkgroup.backups.BackupCredentialType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.util.AsyncTimerUtil;
import org.whispersystems.textsecuregcm.util.AttributeValues;
import org.whispersystems.textsecuregcm.util.ExceptionUtils;
@@ -1585,23 +1584,25 @@ public class Accounts {
CompletableFuture<Void> regenerateConstraints(final Account account) {
final List<CompletableFuture<?>> constraintFutures = new ArrayList<>();
constraintFutures.add(writeConstraint(phoneNumberConstraintTableName,
account.getIdentifier(IdentityType.ACI),
ATTR_ACCOUNT_E164,
AttributeValues.fromString(account.getNumber())));
account.getNumberOptional().ifPresent(phoneNumber ->
constraintFutures.add(writeConstraint(phoneNumberConstraintTableName,
account.getAccountIdentifier(),
ATTR_ACCOUNT_E164,
AttributeValues.fromString(phoneNumber))));
constraintFutures.add(writeConstraint(phoneNumberIdentifierConstraintTableName,
account.getIdentifier(IdentityType.ACI),
ATTR_PNI_UUID,
AttributeValues.fromUUID(account.getPhoneNumberIdentifier())));
account.getPhoneNumberIdentifierOptional().ifPresent(phoneNumberIdentifier ->
constraintFutures.add(writeConstraint(phoneNumberIdentifierConstraintTableName,
account.getAccountIdentifier(),
ATTR_PNI_UUID,
AttributeValues.fromUUID(phoneNumberIdentifier))));
account.getUsernameHash().ifPresent(usernameHash ->
constraintFutures.add(writeUsernameConstraint(account.getIdentifier(IdentityType.ACI),
constraintFutures.add(writeUsernameConstraint(account.getAccountIdentifier(),
usernameHash,
Optional.empty())));
account.getUsernameHolds().forEach(usernameHold ->
constraintFutures.add(writeUsernameConstraint(account.getIdentifier(IdentityType.ACI),
constraintFutures.add(writeUsernameConstraint(account.getAccountIdentifier(),
usernameHold.usernameHash(),
Optional.of(Instant.ofEpochSecond(usernameHold.expirationSecs())))));
@@ -18,7 +18,6 @@ import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.util.AttributeValues;
import org.whispersystems.textsecuregcm.util.ExceptionUtils;
import org.whispersystems.textsecuregcm.util.Util;
@@ -246,7 +245,12 @@ public class PhoneNumberIdentifiers {
}
CompletableFuture<Void> regeneratePhoneNumberIdentifierMappings(final Account account) {
return setPni(account.getNumber(), Util.getAlternateForms(account.getNumber()), account.getIdentifier(IdentityType.PNI))
.thenRun(Util.NOOP);
return account.getNumberOptional()
.map(phoneNumber -> setPni(phoneNumber,
Util.getAlternateForms(phoneNumber),
account.getPhoneNumberIdentifierOptional()
.orElseThrow(() -> new AssertionError("Account has a phone number, but no PNI")))
.thenRun(Util.NOOP))
.orElseGet(() -> CompletableFuture.completedFuture(null));
}
}
@@ -17,6 +17,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@@ -25,7 +26,6 @@ import java.util.function.Supplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.storage.DynamoDbExtensionSchema.Tables;
import org.whispersystems.textsecuregcm.util.CompletableFutureTestUtil;
import software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException;
@@ -216,8 +216,8 @@ class PhoneNumberIdentifiersTest {
final UUID phoneNumberIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(newFormatBeninE164);
when(account.getIdentifier(IdentityType.PNI)).thenReturn(phoneNumberIdentifier);
when(account.getNumberOptional()).thenReturn(Optional.of(newFormatBeninE164));
when(account.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(phoneNumberIdentifier));
phoneNumberIdentifiers.regeneratePhoneNumberIdentifierMappings(account).join();