Port StickerController to gRPC

This commit is contained in:
Jon Chambers authored and GitHub committed 2026-06-18 08:51:00 -04:00
1 parent 1876b71d39
commit 60002a8a0f
17 files changed
+330 -205

No files matched your search

@@ -100,7 +100,6 @@ import org.whispersystems.textsecuregcm.identity.ServiceIdentifier;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.mappers.RateLimitExceededExceptionMapper;
import org.whispersystems.textsecuregcm.s3.PolicySigner;
import org.whispersystems.textsecuregcm.s3.PostPolicyGenerator;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountBadge;
@@ -129,8 +128,7 @@ class ProfileControllerTest {
private static final RateLimiter usernameRateLimiter = mock(RateLimiter.class);
private static final PostPolicyGenerator postPolicyGenerator = new PostPolicyGenerator("us-west-1", "profile-bucket",
"accessKey");
private static final PolicySigner policySigner = new PolicySigner("accessSecret", "us-west-1");
"accessKey", "accessSecret");
private static final ServerZkProfileOperations zkProfileOperations = mock(ServerZkProfileOperations.class);
private static final ServerSecretParams serverSecretParams = ServerSecretParams.generate();
@@ -172,7 +170,6 @@ class ProfileControllerTest {
new BadgeConfiguration("TEST3", "testing", List.of("l", "m", "h", "x", "xx", "xxx"), "SVG", List.of(new BadgeSvg("sl", "sd"), new BadgeSvg("ml", "md"), new BadgeSvg("ll", "ld")))
), List.of("TEST1"), Map.of(1L, "TEST1", 2L, "TEST2", 3L, "TEST3")),
postPolicyGenerator,
policySigner,
serverSecretParams,
zkProfileOperations,
Executors.newSingleThreadExecutor()))
@@ -15,6 +15,7 @@ import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import io.dropwizard.testing.junit5.ResourceExtension;
import jakarta.ws.rs.core.Response;
import java.time.Clock;
import java.util.Base64;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.junit.jupiter.api.BeforeEach;
@@ -24,6 +25,7 @@ import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice;
import org.whispersystems.textsecuregcm.entities.StickerPackFormUploadAttributes;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.s3.PostPolicyGenerator;
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
import org.whispersystems.textsecuregcm.util.SystemMapper;
@@ -38,7 +40,9 @@ class StickerControllerTest {
.addProvider(new AuthValueFactoryProvider.Binder<>(AuthenticatedDevice.class))
.setMapper(SystemMapper.jsonMapper())
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
.addResource(new StickerController(rateLimiters, "foo", "bar", "us-east-1", "mybucket"))
.addResource(new StickerController(rateLimiters,
new PostPolicyGenerator("us-east-1", "mybucket", "foo", "bar"),
Clock.systemUTC()))
.build();
@BeforeEach
@@ -24,6 +24,7 @@ import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.time.Clock;
import java.time.Duration;
import java.util.Arrays;
import java.util.Base64;
@@ -37,8 +38,11 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.signal.chat.attachments.AttachmentsGrpc;
import org.signal.chat.attachments.GetStickerUploadFormRequest;
import org.signal.chat.attachments.GetStickerUploadFormResponse;
import org.signal.chat.attachments.GetUploadFormRequest;
import org.signal.chat.attachments.GetUploadFormResponse;
import org.signal.chat.common.S3UploadForm;
import org.signal.chat.common.UploadForm;
import org.whispersystems.textsecuregcm.attachments.AttachmentUtil;
import org.whispersystems.textsecuregcm.attachments.GcsAttachmentGenerator;
@@ -49,6 +53,7 @@ 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.s3.PostPolicyGenerator;
import org.whispersystems.textsecuregcm.util.MockUtils;
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
@@ -65,6 +70,8 @@ class AttachmentsGrpcServiceTest extends
private RateLimiter countRateLimiter;
@Mock
private RateLimiter byteRateLimiter;
@Mock
private RateLimiter stickerRateLimiter;
@Override
protected AttachmentsGrpcService createServiceBeforeEachTest() {
@@ -86,10 +93,13 @@ class AttachmentsGrpcServiceTest extends
MockUtils.buildMock(RateLimiters.class, rateLimiters -> {
when(rateLimiters.getAttachmentLimiter()).thenReturn(countRateLimiter);
when(rateLimiters.getAttachmentBytesLimiter()).thenReturn(byteRateLimiter);
when(rateLimiters.getStickerPackLimiter()).thenReturn(stickerRateLimiter);
}),
gcsAttachmentGenerator,
tusAttachmentGenerator,
MAX_UPLOAD_LENGTH);
new PostPolicyGenerator("wakonda-south-7", "test", "accessId", "accessSecret"),
MAX_UPLOAD_LENGTH,
Clock.systemUTC());
} catch (NoSuchAlgorithmException | IOException | InvalidKeyException | InvalidKeySpecException e) {
throw new AssertionError(e);
}
@@ -209,4 +219,41 @@ class AttachmentsGrpcServiceTest extends
assertRateLimitExceeded(retryAfter, () ->
authenticatedServiceStub().getUploadForm(GetUploadFormRequest.newBuilder().setUploadLength(1).build()));
}
@Test
void getStickerUploadForm() {
final int stickerCount = 7;
final GetStickerUploadFormResponse response =
authenticatedServiceStub().getStickerUploadForm(GetStickerUploadFormRequest.newBuilder()
.setStickerCount(stickerCount)
.build());
assertThat(response.getPackId()).isNotBlank();
assertThat(response.getManifestUploadForm()).satisfies(AttachmentsGrpcServiceTest::s3UploadFormValid);
assertThat(response.getStickerUploadFormsCount()).isEqualTo(stickerCount);
assertThat(response.getStickerUploadFormsList()).allSatisfy(AttachmentsGrpcServiceTest::s3UploadFormValid);
}
private static void s3UploadFormValid(final S3UploadForm s3UploadForm) {
assertThat(s3UploadForm.getAcl()).isEqualTo("private");
assertThat(s3UploadForm.getAlgorithm()).isEqualTo("AWS4-HMAC-SHA256");
assertThat(s3UploadForm.getKey()).startsWith("stickers/");
assertThat(s3UploadForm.getCredential()).endsWith("/s3/aws4_request");
assertThat(s3UploadForm.getDate()).isNotBlank();
assertThat(s3UploadForm.getPolicy()).isNotBlank();
assertThat(s3UploadForm.getSignature()).isNotBlank();
}
@Test
void getStickerUploadFormRateLimited() throws RateLimitExceededException {
final Duration retryAfter = Duration.ofMinutes(13);
doThrow(new RateLimitExceededException(retryAfter)).when(stickerRateLimiter).validate(any(UUID.class));
//noinspection ResultOfMethodCallIgnored
GrpcTestUtils.assertRateLimitExceeded(retryAfter,
() -> authenticatedServiceStub().getStickerUploadForm(GetStickerUploadFormRequest.newBuilder()
.setStickerCount(7)
.build()));
}
}
@@ -93,7 +93,6 @@ import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
import org.whispersystems.textsecuregcm.identity.PniServiceIdentifier;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.s3.PolicySigner;
import org.whispersystems.textsecuregcm.s3.PostPolicyGenerator;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountBadge;
@@ -148,10 +147,11 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
@Override
protected ProfileGrpcService createServiceBeforeEachTest() {
clock = Clock.fixed(Instant.ofEpochSecond(42), ZoneId.of("Etc/UTC"));
@SuppressWarnings("unchecked") final DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager = mock(DynamicConfigurationManager.class);
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
final PolicySigner policySigner = new PolicySigner("accessSecret", "us-west-1");
final PostPolicyGenerator policyGenerator = new PostPolicyGenerator("us-west-1", "profile-bucket", "accessKey");
final PostPolicyGenerator policyGenerator = new PostPolicyGenerator("us-west-1", "profile-bucket", "accessKey", "accessSecret");
final BadgesConfiguration badgesConfiguration = new BadgesConfiguration(
List.of(new BadgeConfiguration(
"TEST",
@@ -201,8 +201,6 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
when(profilesManager.deleteAvatar(anyString())).thenReturn(CompletableFuture.completedFuture(null));
clock = Clock.fixed(Instant.ofEpochSecond(42), ZoneId.of("Etc/UTC"));
return new ProfileGrpcService(
clock,
accountsManager,
@@ -210,7 +208,6 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
dynamicConfigurationManager,
badgesConfiguration,
policyGenerator,
policySigner,
profileBadgeConverter,
rateLimiters
);
@@ -398,9 +395,9 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
final SetProfileResult result = response.getResult();
if (expectHasS3UploadPath) {
assertTrue(result.getV1AvatarUploadForm().getPath().startsWith("profiles/"));
assertTrue(result.getV1AvatarUploadForm().getKey().startsWith("profiles/"));
} else {
assertEquals("", result.getV1AvatarUploadForm().getPath());
assertEquals("", result.getV1AvatarUploadForm().getKey());
}
if (expectDeleteS3Object) {
@@ -1,5 +1,5 @@
/*
* Copyright 2013 Signal Messenger, LLC
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
@@ -12,16 +12,17 @@ import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.junit.jupiter.api.Test;
class PolicySignerTest {
class PostPolicyGeneratorTest {
@Test
void testSignature() {
final Instant time = Instant.parse("2015-12-29T00:00:00Z");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(time, ZoneOffset.UTC);
void getSignature() {
final Instant currentTime = Instant.parse("2015-12-29T00:00:00Z");
final PostPolicyGenerator postPolicyGenerator = new PostPolicyGenerator("us-east-1", "bucket", "accessId", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(currentTime, ZoneOffset.UTC);
final String encodedPolicy = "eyAiZXhwaXJhdGlvbiI6ICIyMDE1LTEyLTMwVDEyOjAwOjAwLjAwMFoiLA0KICAiY29uZGl0aW9ucyI6IFsNCiAgICB7ImJ1Y2tldCI6ICJzaWd2NGV4YW1wbGVidWNrZXQifSwNCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci91c2VyMS8iXSwNCiAgICB7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LA0KICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL3NpZ3Y0ZXhhbXBsZWJ1Y2tldC5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwNCiAgICBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sDQogICAgeyJ4LWFtei1tZXRhLXV1aWQiOiAiMTQzNjUxMjM2NTEyNzQifSwNCiAgICB7IngtYW16LXNlcnZlci1zaWRlLWVuY3J5cHRpb24iOiAiQUVTMjU2In0sDQogICAgWyJzdGFydHMtd2l0aCIsICIkeC1hbXotbWV0YS10YWciLCAiIl0sDQoNCiAgICB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQUlPU0ZPRE5ON0VYQU1QTEUvMjAxNTEyMjkvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LA0KICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwNCiAgICB7IngtYW16LWRhdGUiOiAiMjAxNTEyMjlUMDAwMDAwWiIgfQ0KICBdDQp9";
final PolicySigner policySigner = new PolicySigner("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "us-east-1");
assertEquals("8afdbf4008c03f22c2cd3cdb72e4afbb1f6a588f3255ac628749a66d7f09699e",
policySigner.getSignature(zonedDateTime, encodedPolicy));
postPolicyGenerator.getSignature(zonedDateTime, encodedPolicy));
}
}