mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 00:56:50 +00:00
Handle non-idempotent integration test operations
- Retry 441s during webauthn recovery - Assume a 401 during delete means the account is already gone - Check the session state to determine whether verification is complete
This commit is contained in:
1 parent
7e26b7fe0a
commit
2d6eb7951a
2 files changed
+99
-38
No files matched your search
@@ -8,6 +8,7 @@ package org.signal.integration;
|
|||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.common.io.Resources;
|
import com.google.common.io.Resources;
|
||||||
import com.google.common.net.HttpHeaders;
|
import com.google.common.net.HttpHeaders;
|
||||||
import com.webauthn4j.data.AuthenticatorAssertionResponse;
|
import com.webauthn4j.data.AuthenticatorAssertionResponse;
|
||||||
@@ -56,6 +57,7 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
@@ -121,6 +123,8 @@ public final class Operations {
|
|||||||
|
|
||||||
private static final FaultTolerantHttpClient CLIENT = buildClient();
|
private static final FaultTolerantHttpClient CLIENT = buildClient();
|
||||||
|
|
||||||
|
private static final FaultTolerantHttpClient NO_RETRY_CLIENT = buildNoRetryClient();
|
||||||
|
|
||||||
private static final WebSocketClient WEB_SOCKET_CLIENT = buildWebSocketClient();
|
private static final WebSocketClient WEB_SOCKET_CLIENT = buildWebSocketClient();
|
||||||
|
|
||||||
private static final ManagedChannel GRPC_CHANNEL = buildGrpcChannel();
|
private static final ManagedChannel GRPC_CHANNEL = buildGrpcChannel();
|
||||||
@@ -257,50 +261,66 @@ public final class Operations {
|
|||||||
Optional.empty()));
|
Optional.empty()));
|
||||||
|
|
||||||
// we expect (and need) failure with status 441, which will contain the WebAuthn challenge
|
// we expect (and need) failure with status 441, which will contain the WebAuthn challenge
|
||||||
final MfaFailureExceptionMapper.MfaFailureResponse mfaFailureResponse = apiPost("/v1/registration", initialRequest)
|
MfaFailureExceptionMapper.MfaFailureResponse mfaFailureResponse = apiPost("/v1/registration", initialRequest)
|
||||||
// For a numberless account recovery, the username is the ACI
|
// For a numberless account recovery, the username is the ACI
|
||||||
.authorized(testUser.aciUuid().toString(), testUser.accountPassword())
|
.authorized(testUser.aciUuid().toString(), testUser.accountPassword())
|
||||||
.executeExpectStatusCode(441, MfaFailureExceptionMapper.MfaFailureResponse.class);
|
.executeExpectStatusCode(441, MfaFailureExceptionMapper.MfaFailureResponse.class);
|
||||||
|
|
||||||
Validate.isTrue(mfaFailureResponse.webAuthnParameters().allowedCredentialIds()
|
// Retry a 441 to 3 times. It's possible we burned a challenge on a 500 that actually succeeded, but we didn't see
|
||||||
.stream()
|
// the response, so we can retry on the new challenge.
|
||||||
.anyMatch(allowedId -> Arrays.equals(allowedId, credentialId)),
|
int attempt = 0;
|
||||||
"Expected MFA failure to contain credentialId");
|
while (true) {
|
||||||
|
attempt++;
|
||||||
|
Validate.isTrue(mfaFailureResponse.webAuthnParameters().allowedCredentialIds()
|
||||||
|
.stream()
|
||||||
|
.anyMatch(allowedId -> Arrays.equals(allowedId, credentialId)),
|
||||||
|
"Expected MFA failure to contain credentialId");
|
||||||
|
|
||||||
final PublicKeyCredential<AuthenticatorAssertionResponse, ?> credential =
|
final PublicKeyCredential<AuthenticatorAssertionResponse, ?> credential =
|
||||||
clientPlatform.get(new PublicKeyCredentialRequestOptions(
|
clientPlatform.get(new PublicKeyCredentialRequestOptions(
|
||||||
new DefaultChallenge(mfaFailureResponse.webAuthnParameters().challenge()),
|
new DefaultChallenge(mfaFailureResponse.webAuthnParameters().challenge()),
|
||||||
null,
|
null,
|
||||||
relyingPartyId,
|
relyingPartyId,
|
||||||
List.of(new PublicKeyCredentialDescriptor(PublicKeyCredentialType.PUBLIC_KEY, credentialId, null)),
|
List.of(new PublicKeyCredentialDescriptor(PublicKeyCredentialType.PUBLIC_KEY, credentialId, null)),
|
||||||
UserVerificationRequirement.DISCOURAGED,
|
UserVerificationRequirement.DISCOURAGED,
|
||||||
null));
|
null));
|
||||||
|
|
||||||
final RegistrationRequest registrationRequest = new RegistrationRequest(null,
|
final RegistrationRequest registrationRequest = new RegistrationRequest(null,
|
||||||
testUser.registrationPassword(),
|
testUser.registrationPassword(),
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
webAuthnVerificationResponseJson(credential),
|
webAuthnVerificationResponseJson(credential),
|
||||||
recoveredUser.accountAttributes(),
|
recoveredUser.accountAttributes(),
|
||||||
true,
|
true,
|
||||||
new IdentityKey(aciIdentityKeyPair.getPublicKey()),
|
new IdentityKey(aciIdentityKeyPair.getPublicKey()),
|
||||||
new IdentityKey(pniIdentityKeyPair.getPublicKey()),
|
new IdentityKey(pniIdentityKeyPair.getPublicKey()),
|
||||||
new DeviceActivationRequest(generateSignedECPreKey(1, aciIdentityKeyPair),
|
new DeviceActivationRequest(generateSignedECPreKey(1, aciIdentityKeyPair),
|
||||||
Optional.of(generateSignedECPreKey(2, pniIdentityKeyPair)),
|
Optional.of(generateSignedECPreKey(2, pniIdentityKeyPair)),
|
||||||
generateSignedKEMPreKey(3, aciIdentityKeyPair),
|
generateSignedKEMPreKey(3, aciIdentityKeyPair),
|
||||||
Optional.of(generateSignedKEMPreKey(4, pniIdentityKeyPair)),
|
Optional.of(generateSignedKEMPreKey(4, pniIdentityKeyPair)),
|
||||||
Optional.empty(),
|
Optional.empty(),
|
||||||
Optional.empty()));
|
Optional.empty()));
|
||||||
|
|
||||||
final AccountIdentityResponse registrationResponse = apiPost("/v1/registration", registrationRequest)
|
final Pair<Integer, JsonNode> registrationResponse = apiPost("/v1/registration", registrationRequest)
|
||||||
// For a numberless account recovery, the username is the ACI
|
// For a numberless account recovery, the username is the ACI
|
||||||
.authorized(testUser.aciUuid().toString(), testUser.accountPassword())
|
.authorized(testUser.aciUuid().toString(), testUser.accountPassword())
|
||||||
.executeExpectSuccess(AccountIdentityResponse.class);
|
.execute(JsonNode.class);
|
||||||
|
|
||||||
recoveredUser.setAciUuid(registrationResponse.uuid());
|
if (registrationResponse.getLeft() == 441 && attempt < 3) {
|
||||||
return recoveredUser;
|
mfaFailureResponse = SystemMapper.jsonMapper()
|
||||||
|
.convertValue(registrationResponse.getRight(), MfaFailureExceptionMapper.MfaFailureResponse.class);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Validate.isTrue(HttpUtils.isSuccessfulResponse(registrationResponse.getLeft()),
|
||||||
|
"Unexpected response code: %d, body: %s", registrationResponse.getLeft(), registrationResponse.getRight());
|
||||||
|
recoveredUser.setAciUuid(SystemMapper.jsonMapper()
|
||||||
|
.convertValue(registrationResponse.getRight(), AccountIdentityResponse.class).uuid());
|
||||||
|
return recoveredUser;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static TestUser newRegisteredUser(final String number) {
|
public static TestUser newRegisteredUser(final String number) {
|
||||||
final byte[] registrationPassword = populateRandomRecoveryPassword(number);
|
final byte[] registrationPassword = populateRandomRecoveryPassword(number);
|
||||||
final String accountPassword = Base64.getEncoder().encodeToString(randomBytes(32));
|
final String accountPassword = Base64.getEncoder().encodeToString(randomBytes(32));
|
||||||
@@ -347,7 +367,24 @@ public final class Operations {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void deleteUser(final TestUser user) {
|
public static void deleteUser(final TestUser user) {
|
||||||
apiDelete("/v1/accounts/me").authorized(user).executeExpectSuccess();
|
final AtomicInteger attempts = new AtomicInteger();
|
||||||
|
final Retry retry = Retry.of("integration-test-delete-user",
|
||||||
|
new RetryConfiguration().<Integer>toRetryConfigBuilder()
|
||||||
|
.retryOnResult(statusCode -> statusCode >= 500)
|
||||||
|
.build());
|
||||||
|
final int statusCode = retry.executeSupplier(() -> {
|
||||||
|
attempts.incrementAndGet();
|
||||||
|
return apiDelete("/v1/accounts/me")
|
||||||
|
.authorized(user)
|
||||||
|
// disable retries, since this endpoint is not idempotent
|
||||||
|
.withoutRetries()
|
||||||
|
.execute()
|
||||||
|
.getLeft();
|
||||||
|
});
|
||||||
|
|
||||||
|
// If we had to retry a 500 where the account was already deleted, we would fail authorization
|
||||||
|
Validate.isTrue(HttpUtils.isSuccessfulResponse(statusCode) || (attempts.get() > 1 && statusCode == 401),
|
||||||
|
"Unexpected response code: %d", statusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String peekVerificationSessionPushChallenge(final String sessionId) {
|
public static String peekVerificationSessionPushChallenge(final String sessionId) {
|
||||||
@@ -486,6 +523,7 @@ public final class Operations {
|
|||||||
|
|
||||||
private final List<String> queryParams = new ArrayList<>();
|
private final List<String> queryParams = new ArrayList<>();
|
||||||
|
|
||||||
|
private boolean retryServerErrors = true;
|
||||||
|
|
||||||
private RequestBuilder(final HttpRequest.Builder builder, final String endpoint) {
|
private RequestBuilder(final HttpRequest.Builder builder, final String endpoint) {
|
||||||
this.builder = builder;
|
this.builder = builder;
|
||||||
@@ -523,6 +561,12 @@ public final class Operations {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Don't automatically retry 5xx responses, for requests that aren't safe to blindly retry
|
||||||
|
public RequestBuilder withoutRetries() {
|
||||||
|
retryServerErrors = false;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Pair<Integer, Void> execute() {
|
public Pair<Integer, Void> execute() {
|
||||||
return execute(Void.class);
|
return execute(Void.class);
|
||||||
}
|
}
|
||||||
@@ -572,7 +616,8 @@ public final class Operations {
|
|||||||
private RawResponse executeRaw() {
|
private RawResponse executeRaw() {
|
||||||
builder.uri(serverUri(endpoint, queryParams))
|
builder.uri(serverUri(endpoint, queryParams))
|
||||||
.header(HttpHeaders.USER_AGENT, USER_AGENT);
|
.header(HttpHeaders.USER_AGENT, USER_AGENT);
|
||||||
return CLIENT.sendAsync(builder.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8))
|
return (retryServerErrors ? CLIENT : NO_RETRY_CLIENT)
|
||||||
|
.sendAsync(builder.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8))
|
||||||
.whenComplete((response, error) -> {
|
.whenComplete((response, error) -> {
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
logger.error("request error", error);
|
logger.error("request error", error);
|
||||||
@@ -615,6 +660,16 @@ public final class Operations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static FaultTolerantHttpClient buildNoRetryClient() {
|
||||||
|
try {
|
||||||
|
return FaultTolerantHttpClient.newBuilder("integration-test-no-retry", Executors.newFixedThreadPool(16))
|
||||||
|
.withTrustedServerCertificates(CONFIG.rootCerts().toArray(String[]::new))
|
||||||
|
.build();
|
||||||
|
} catch (final CertificateException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static WebSocketClient buildWebSocketClient() {
|
private static WebSocketClient buildWebSocketClient() {
|
||||||
try {
|
try {
|
||||||
final KeyStore trustStore = CertificateUtil.buildKeyStoreForPem(CONFIG.rootCerts().toArray(String[]::new));
|
final KeyStore trustStore = CertificateUtil.buildKeyStoreForPem(CONFIG.rootCerts().toArray(String[]::new));
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
package org.signal.integration;
|
package org.signal.integration;
|
||||||
|
|
||||||
import io.micrometer.common.util.StringUtils;
|
import io.micrometer.common.util.StringUtils;
|
||||||
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.whispersystems.textsecuregcm.entities.CreateVerificationSessionRequest;
|
import org.whispersystems.textsecuregcm.entities.CreateVerificationSessionRequest;
|
||||||
@@ -54,8 +55,13 @@ public class RegistrationTest {
|
|||||||
// verify code
|
// verify code
|
||||||
final SubmitVerificationCodeRequest submitVerificationCodeRequest = new SubmitVerificationCodeRequest(
|
final SubmitVerificationCodeRequest submitVerificationCodeRequest = new SubmitVerificationCodeRequest(
|
||||||
params.verificationCode());
|
params.verificationCode());
|
||||||
final VerificationSessionResponse codeVerified = Operations
|
final Pair<Integer, VerificationSessionResponse> codeVerified = Operations
|
||||||
.apiPut("/v1/verification/session/%s/code".formatted(sessionId), submitVerificationCodeRequest)
|
.apiPut("/v1/verification/session/%s/code".formatted(sessionId), submitVerificationCodeRequest)
|
||||||
.executeExpectSuccess(VerificationSessionResponse.class);
|
.execute(VerificationSessionResponse.class);
|
||||||
|
|
||||||
|
// If we succeeded but then retried a 500, we might have submitted a code when we were already verified
|
||||||
|
Assertions.assertTrue(codeVerified.getLeft() == 200 || codeVerified.getLeft() == 409,
|
||||||
|
"Unexpected response code: " + codeVerified.getLeft());
|
||||||
|
Assertions.assertTrue(codeVerified.getRight().verified());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user