mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 11:56:53 +00:00
Port AttachmentController to gRPC
This commit is contained in:
1 parent
82a3bba7ec
commit
81f2499889
10 files changed
+295
-32
No files matched your search
+3
-2
@@ -41,6 +41,7 @@ import org.whispersystems.textsecuregcm.entities.AttachmentDescriptorV3;
|
||||
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
|
||||
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
||||
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||
import org.whispersystems.textsecuregcm.attachments.AttachmentUtil;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.util.MockUtils;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
@@ -58,8 +59,8 @@ class AttachmentControllerV4Test {
|
||||
private static final String CDN3_ENABLED_CREDS = AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD);
|
||||
private static final String CDN3_DISABLED_CREDS = AuthHelper.getAuthHeader(AuthHelper.VALID_UUID_TWO, AuthHelper.VALID_PASSWORD_TWO);
|
||||
private static final ExperimentEnrollmentManager EXPERIMENT_MANAGER = MockUtils.buildMock(ExperimentEnrollmentManager.class, mgr -> {
|
||||
when(mgr.isEnrolled(AuthHelper.VALID_UUID, AttachmentControllerV4.CDN3_EXPERIMENT_NAME)).thenReturn(true);
|
||||
when(mgr.isEnrolled(AuthHelper.VALID_UUID_TWO, AttachmentControllerV4.CDN3_EXPERIMENT_NAME)).thenReturn(false);
|
||||
when(mgr.isEnrolled(AuthHelper.VALID_UUID, AttachmentUtil.CDN3_EXPERIMENT_NAME)).thenReturn(true);
|
||||
when(mgr.isEnrolled(AuthHelper.VALID_UUID_TWO, AttachmentUtil.CDN3_EXPERIMENT_NAME)).thenReturn(false);
|
||||
});
|
||||
|
||||
private static final byte[] TUS_SECRET = TestRandomUtil.nextBytes(32);
|
||||
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.grpc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.whispersystems.textsecuregcm.grpc.GrpcTestUtils.assertRateLimitExceeded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.signal.chat.attachments.AttachmentsGrpc;
|
||||
import org.signal.chat.attachments.GetUploadFormRequest;
|
||||
import org.signal.chat.attachments.GetUploadFormResponse;
|
||||
import org.signal.chat.common.UploadForm;
|
||||
import org.whispersystems.textsecuregcm.attachments.GcsAttachmentGenerator;
|
||||
import org.whispersystems.textsecuregcm.attachments.TusAttachmentGenerator;
|
||||
import org.whispersystems.textsecuregcm.attachments.TusConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.secrets.SecretBytes;
|
||||
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
|
||||
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
|
||||
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
||||
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||
import org.whispersystems.textsecuregcm.attachments.AttachmentUtil;
|
||||
import org.whispersystems.textsecuregcm.util.MockUtils;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
|
||||
class AttachmentsGrpcServiceTest extends
|
||||
SimpleBaseGrpcTest<AttachmentsGrpcService, AttachmentsGrpc.AttachmentsBlockingStub> {
|
||||
|
||||
private static final byte[] TUS_SECRET = TestRandomUtil.nextBytes(32);
|
||||
private static final String TUS_URL = "https://example.com/uploads";
|
||||
|
||||
@Mock
|
||||
private ExperimentEnrollmentManager experimentEnrollmentManager;
|
||||
@Mock
|
||||
private RateLimiter rateLimiter;
|
||||
|
||||
@Override
|
||||
protected AttachmentsGrpcService createServiceBeforeEachTest() {
|
||||
try {
|
||||
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||
keyPairGenerator.initialize(1024);
|
||||
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
|
||||
final String gcsPrivateKeyPem = "-----BEGIN PRIVATE KEY-----\n" +
|
||||
Base64.getMimeEncoder().encodeToString(keyPair.getPrivate().getEncoded()) + "\n" +
|
||||
"-----END PRIVATE KEY-----";
|
||||
final GcsAttachmentGenerator gcsAttachmentGenerator = new GcsAttachmentGenerator(
|
||||
"some-cdn.signal.org", "signal@example.com", 1000, "/attach-here", gcsPrivateKeyPem);
|
||||
final TusAttachmentGenerator tusAttachmentGenerator =
|
||||
new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL));
|
||||
|
||||
return new AttachmentsGrpcService(
|
||||
experimentEnrollmentManager,
|
||||
MockUtils.buildMock(RateLimiters.class, rateLimiters ->
|
||||
when(rateLimiters.getAttachmentLimiter()).thenReturn(rateLimiter)),
|
||||
gcsAttachmentGenerator,
|
||||
tusAttachmentGenerator);
|
||||
} catch (NoSuchAlgorithmException | IOException | InvalidKeyException | InvalidKeySpecException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUploadFormCdn3() {
|
||||
when(experimentEnrollmentManager.isEnrolled(AUTHENTICATED_ACI, AttachmentUtil.CDN3_EXPERIMENT_NAME))
|
||||
.thenReturn(true);
|
||||
|
||||
final GetUploadFormResponse response = authenticatedServiceStub()
|
||||
.getUploadForm(GetUploadFormRequest.newBuilder().build());
|
||||
|
||||
final UploadForm uploadForm = response.getUploadForm();
|
||||
assertThat(uploadForm.getCdn()).isEqualTo(3);
|
||||
assertThat(uploadForm.getKey()).isNotBlank();
|
||||
assertThat(uploadForm.getSignedUploadLocation()).isEqualTo(TUS_URL + "/attachments");
|
||||
|
||||
final String filenameb64 = uploadForm.getHeadersMap().get("Upload-Metadata").split(" ")[1];
|
||||
final String filename = new String(Base64.getDecoder().decode(filenameb64));
|
||||
assertThat(uploadForm.getKey()).isEqualTo(filename);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUploadFormCdn2() throws MalformedURLException {
|
||||
when(experimentEnrollmentManager.isEnrolled(AUTHENTICATED_ACI, AttachmentUtil.CDN3_EXPERIMENT_NAME))
|
||||
.thenReturn(false);
|
||||
|
||||
final GetUploadFormResponse response = authenticatedServiceStub()
|
||||
.getUploadForm(GetUploadFormRequest.newBuilder().build());
|
||||
|
||||
final UploadForm uploadForm = response.getUploadForm();
|
||||
assertThat(uploadForm.getCdn()).isEqualTo(2);
|
||||
assertThat(uploadForm.getKey()).isNotBlank();
|
||||
assertThat(uploadForm.getHeadersMap()).containsExactlyInAnyOrderEntriesOf(Map.of(
|
||||
"host", "some-cdn.signal.org",
|
||||
"x-goog-resumable", "start",
|
||||
"x-goog-content-length-range", "1,1000"));
|
||||
assertThat(uploadForm.getSignedUploadLocation()).isNotEmpty();
|
||||
|
||||
final URL signedUploadLocation = URI.create(uploadForm.getSignedUploadLocation()).toURL();
|
||||
assertThat(signedUploadLocation.getHost()).isEqualTo("some-cdn.signal.org");
|
||||
assertThat(signedUploadLocation.getPath()).startsWith("/attach-here/");
|
||||
final Map<String, String> queryParamMap = Arrays.stream(signedUploadLocation.getQuery().split("&"))
|
||||
.map(queryTerm -> queryTerm.split("=", 2))
|
||||
.collect(Collectors.toMap(
|
||||
arr -> URLDecoder.decode(arr[0], StandardCharsets.UTF_8),
|
||||
arr -> URLDecoder.decode(arr[1], StandardCharsets.UTF_8)));
|
||||
|
||||
assertThat(queryParamMap).hasSize(6);
|
||||
assertThat(queryParamMap).containsAllEntriesOf(Map.of(
|
||||
"X-Goog-Algorithm", "GOOG4-RSA-SHA256",
|
||||
"X-Goog-Expires", "90000",
|
||||
"X-Goog-SignedHeaders", "host;x-goog-content-length-range;x-goog-resumable"));
|
||||
assertThat(queryParamMap)
|
||||
.extractingByKey("X-Goog-Date", Assertions.as(InstanceOfAssertFactories.STRING))
|
||||
.isNotEmpty();
|
||||
assertThat(queryParamMap)
|
||||
.extractingByKey("X-Goog-Signature", Assertions.as(InstanceOfAssertFactories.STRING))
|
||||
.isNotEmpty();
|
||||
|
||||
final String credential = queryParamMap.get("X-Goog-Credential");
|
||||
final String[] credentialParts = credential.split("/");
|
||||
assertThat(credentialParts).hasSize(5);
|
||||
assertThat(credentialParts[0]).isEqualTo("signal@example.com");
|
||||
assertThat(credentialParts[2]).isEqualTo("auto");
|
||||
assertThat(credentialParts[3]).isEqualTo("storage");
|
||||
assertThat(credentialParts[4]).isEqualTo("goog4_request");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUploadFormRateLimited() throws RateLimitExceededException {
|
||||
final Duration retryAfter = Duration.ofMinutes(5);
|
||||
doThrow(new RateLimitExceededException(retryAfter)).when(rateLimiter).validate(any(UUID.class));
|
||||
|
||||
assertRateLimitExceeded(retryAfter, () ->
|
||||
authenticatedServiceStub().getUploadForm(GetUploadFormRequest.newBuilder().build()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user