Convert VerificationSessions 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 defbc1c853
commit 66b0ed16d1
10 files changed
+127 -217

No files matched your search

@@ -519,7 +519,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
dynamoDbAsyncClient,
clock);
final VerificationSessions verificationSessions = new VerificationSessions(dynamoDbAsyncClient,
final VerificationSessions verificationSessions = new VerificationSessions(dynamoDbClient,
config.getDynamoDbTables().getVerificationSessions().getTableName(), clock);
final ClientResources sharedClientResources = ClientResources.builder()
@@ -61,7 +61,6 @@ import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.Strings;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
@@ -116,7 +115,6 @@ public class VerificationController {
private static final Logger logger = LoggerFactory.getLogger(VerificationController.class);
private static final Duration REGISTRATION_RPC_TIMEOUT = Duration.ofSeconds(15);
private static final Duration DYNAMODB_TIMEOUT = Duration.ofSeconds(5);
private static final SecureRandom RANDOM = new SecureRandom();
@@ -264,7 +262,7 @@ public class VerificationController {
// if a push challenge sent in `handlePushToken` doesn't arrive in time
verificationSession.requestedInformation().add(VerificationSession.Information.CAPTCHA);
storeVerificationSession(verificationSession);
verificationSessionManager.insert(verificationSession);
return buildResponse(registrationServiceSession, verificationSession);
}
@@ -337,24 +335,12 @@ public class VerificationController {
} finally {
// Each of the handle* methods may update requestedInformation, submittedInformation, and allowedToRequestCode,
// and we want to be sure to store a changes, even if a later method throws
updateStoredVerificationSession(verificationSession);
verificationSessionManager.update(verificationSession);
}
return buildResponse(registrationServiceSession, verificationSession);
}
private void storeVerificationSession(final VerificationSession verificationSession) {
verificationSessionManager.insert(verificationSession)
.orTimeout(DYNAMODB_TIMEOUT.toSeconds(), TimeUnit.SECONDS)
.join();
}
private void updateStoredVerificationSession(final VerificationSession verificationSession) {
verificationSessionManager.update(verificationSession)
.orTimeout(DYNAMODB_TIMEOUT.toSeconds(), TimeUnit.SECONDS)
.join();
}
/**
* If {@code pushTokenAndType} values are not {@code null}, sends a push challenge. If there is no existing push
* challenge in the session, one will be created, set on the returned session record, and
@@ -907,8 +893,7 @@ public class VerificationController {
private VerificationSession retrieveVerificationSession(final RegistrationServiceSession registrationServiceSession) {
return verificationSessionManager.findForId(registrationServiceSession.encodedSessionId())
.orTimeout(5, TimeUnit.SECONDS)
.join().orElseThrow(NotFoundException::new);
.orElseThrow(NotFoundException::new);
}
/**
@@ -14,16 +14,16 @@ import java.time.Clock;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.util.AttributeValues;
import org.whispersystems.textsecuregcm.util.SystemMapper;
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.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
public abstract class SerializedExpireableJsonDynamoStore<T> {
@@ -34,7 +34,7 @@ public abstract class SerializedExpireableJsonDynamoStore<T> {
long getExpirationEpochSeconds();
}
private final DynamoDbAsyncClient dynamoDbClient;
private final DynamoDbClient dynamoDbClient;
private final String tableName;
private final Clock clock;
private final Class<T> deserializationTargetClass;
@@ -47,7 +47,7 @@ public abstract class SerializedExpireableJsonDynamoStore<T> {
private final Logger log = LoggerFactory.getLogger(getClass());
public SerializedExpireableJsonDynamoStore(final DynamoDbAsyncClient dynamoDbClient, final String tableName,
public SerializedExpireableJsonDynamoStore(final DynamoDbClient dynamoDbClient, final String tableName,
final Clock clock) {
this.dynamoDbClient = dynamoDbClient;
this.tableName = tableName;
@@ -67,18 +67,16 @@ public abstract class SerializedExpireableJsonDynamoStore<T> {
}
}
public CompletableFuture<Void> insert(final String key, final T v) {
return put(key, v, builder -> builder.expressionAttributeNames(Map.of(
"#key", KEY_KEY
)).conditionExpression("attribute_not_exists(#key)"));
public void insert(final String key, final T v) {
put(key, v, builder -> builder.expressionAttributeNames(Map.of("#key", KEY_KEY))
.conditionExpression("attribute_not_exists(#key)"));
}
public CompletableFuture<Void> update(final String key, final T v) {
return put(key, v, ignored -> {
});
public void update(final String key, final T v) {
put(key, v, _ -> {});
}
private CompletableFuture<Void> put(final String key, final T v,
private void put(final String key, final T v,
final Consumer<PutItemRequest.Builder> putRequestCustomizer) {
try {
final Map<String, AttributeValue> attributeValueMap = new HashMap<>(Map.of(
@@ -93,9 +91,7 @@ public abstract class SerializedExpireableJsonDynamoStore<T> {
.item(attributeValueMap);
putRequestCustomizer.accept(builder);
return dynamoDbClient.putItem(builder.build())
.thenRun(() -> {
});
dynamoDbClient.putItem(builder.build());
} catch (final JsonProcessingException e) {
// This should never happen when writing directly to a string except in cases of serious misconfiguration, which
// would be caught by tests.
@@ -107,24 +103,23 @@ public abstract class SerializedExpireableJsonDynamoStore<T> {
return v.getExpirationEpochSeconds();
}
public CompletableFuture<Optional<T>> findForKey(final String key) {
return dynamoDbClient.getItem(GetItemRequest.builder()
public Optional<T> findForKey(final String key) {
final GetItemResponse response = dynamoDbClient.getItem(GetItemRequest.builder()
.tableName(tableName)
.consistentRead(true)
.key(Map.of(KEY_KEY, AttributeValues.fromString(key)))
.build())
.thenApply(response -> {
try {
return response.hasItem()
? filterMaybeExpiredValue(
SystemMapper.jsonMapper()
.readValue(response.item().get(ATTR_SERIALIZED_VALUE).s(), deserializationTargetClass))
: Optional.empty();
} catch (final JsonProcessingException e) {
log.error("Failed to parse stored value", e);
return Optional.empty();
}
});
.build());
try {
return response.hasItem()
? filterMaybeExpiredValue(
SystemMapper.jsonMapper()
.readValue(response.item().get(ATTR_SERIALIZED_VALUE).s(), deserializationTargetClass))
: Optional.empty();
} catch (final JsonProcessingException e) {
log.error("Failed to parse stored value", e);
return Optional.empty();
}
}
private Optional<T> filterMaybeExpiredValue(T v) {
@@ -139,13 +134,11 @@ public abstract class SerializedExpireableJsonDynamoStore<T> {
return Optional.of(v);
}
public CompletableFuture<Void> remove(final String key) {
return dynamoDbClient.deleteItem(DeleteItemRequest.builder()
public void remove(final String key) {
dynamoDbClient.deleteItem(DeleteItemRequest.builder()
.tableName(tableName)
.key(Map.of(KEY_KEY, AttributeValues.fromString(key)))
.build())
.thenRun(() -> {
});
.build());
}
}
@@ -6,7 +6,6 @@
package org.whispersystems.textsecuregcm.storage;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.whispersystems.textsecuregcm.registration.VerificationSession;
public class VerificationSessionManager {
@@ -17,15 +16,15 @@ public class VerificationSessionManager {
this.verificationSessions = verificationSessions;
}
public CompletableFuture<Void> insert(final VerificationSession verificationSession) {
return verificationSessions.insert(verificationSession.sessionId(), verificationSession);
public void insert(final VerificationSession verificationSession) {
verificationSessions.insert(verificationSession.sessionId(), verificationSession);
}
public CompletableFuture<Void> update(final VerificationSession verificationSession) {
return verificationSessions.update(verificationSession.sessionId(), verificationSession);
public void update(final VerificationSession verificationSession) {
verificationSessions.update(verificationSession.sessionId(), verificationSession);
}
public CompletableFuture<Optional<VerificationSession>> findForId(final String encodedSessionId) {
public Optional<VerificationSession> findForId(final String encodedSessionId) {
return verificationSessions.findForKey(encodedSessionId);
}
@@ -7,11 +7,11 @@ package org.whispersystems.textsecuregcm.storage;
import java.time.Clock;
import org.whispersystems.textsecuregcm.registration.VerificationSession;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
public class VerificationSessions extends SerializedExpireableJsonDynamoStore<VerificationSession> {
public VerificationSessions(final DynamoDbAsyncClient dynamoDbClient, final String tableName, final Clock clock) {
public VerificationSessions(final DynamoDbClient dynamoDbClient, final String tableName, final Clock clock) {
super(dynamoDbClient, tableName, clock);
}
}