Implement grpc remote config

This commit is contained in:
Ravi Khadiwala authored and ravi-signal committed 2026-07-28 16:13:16 -05:00
1 parent 76ca81d9f1
commit 257dd76b68
5 files changed
+285 -4

No files matched your search

@@ -181,6 +181,7 @@ import org.whispersystems.textsecuregcm.grpc.PaymentsGrpcService;
import org.whispersystems.textsecuregcm.grpc.ProductConfigurationGrpcService;
import org.whispersystems.textsecuregcm.grpc.ProfileAnonymousGrpcService;
import org.whispersystems.textsecuregcm.grpc.ProfileGrpcService;
import org.whispersystems.textsecuregcm.grpc.RemoteConfigurationGrpcService;
import org.whispersystems.textsecuregcm.grpc.RequestAttributesInterceptor;
import org.whispersystems.textsecuregcm.grpc.SubscriptionsGrpcService;
import org.whispersystems.textsecuregcm.grpc.ValidatingInterceptor;
@@ -1103,7 +1104,8 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
new DonationsGrpcService(clock, zkReceiptOperations, redeemedReceiptsManager, accountsManager, config.getBadges(), ReceiptCredentialPresentation::new, donationPermitsManager, rateLimiters),
new ProductConfigurationGrpcService(config.getSubscription(), config.getOneTimeDonations(),
List.of(stripeManager, braintreeManager), profileBadgeConverter,
config.getBackupConfiguration().maxTotalMediaSize()))
config.getBackupConfiguration().maxTotalMediaSize()),
new RemoteConfigurationGrpcService(remoteConfigsManager))
.map(bindableService -> ServerInterceptors.intercept(bindableService,
// Note: interceptors run in the reverse order they are added; the remote deprecation filter
// depends on the user-agent context so it has to come first here!
@@ -0,0 +1,71 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.grpc;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.Message;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.UUID;
import org.signal.chat.remoteconfiguration.Configuration;
import org.signal.chat.remoteconfiguration.GetConfigurationRequest;
import org.signal.chat.remoteconfiguration.GetConfigurationResponse;
import org.signal.chat.remoteconfiguration.SimpleRemoteConfigurationGrpc;
import org.signal.chat.remoteconfiguration.TaggedConfiguration;
import org.whispersystems.textsecuregcm.auth.grpc.AuthenticationUtil;
import org.whispersystems.textsecuregcm.storage.RemoteConfigsManager;
public class RemoteConfigurationGrpcService extends SimpleRemoteConfigurationGrpc.RemoteConfigurationImplBase {
private static final GetConfigurationResponse CONFIGURATION_ETAG_MATCHED =
GetConfigurationResponse.newBuilder().setEtagMatched(true).build();
private final RemoteConfigsManager remoteConfigsManager;
public RemoteConfigurationGrpcService(final RemoteConfigsManager remoteConfigsManager) {
this.remoteConfigsManager = remoteConfigsManager;
}
@Override
public GetConfigurationResponse getConfiguration(final GetConfigurationRequest request) {
final UUID accountIdentifier = AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier();
final String userAgent = RequestAttributesUtil.getUserAgent().orElse(null);
final Map<String, String> configForAccount = remoteConfigsManager.getConfigForAccount(accountIdentifier, userAgent);
final Configuration configuration = Configuration.newBuilder()
.putAllConfiguration(configForAccount)
.build();
final ByteString etagByteString = etag(configuration);
if (etagByteString.equals(request.getEtag())) {
return CONFIGURATION_ETAG_MATCHED;
}
return GetConfigurationResponse.newBuilder()
.setTaggedConfiguration(TaggedConfiguration.newBuilder()
.setEtag(etagByteString)
.setConfiguration(configuration))
.build();
}
private static ByteString etag(final Message message) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(message.getSerializedSize());
final CodedOutputStream cos = CodedOutputStream.newInstance(baos);
cos.useDeterministicSerialization();
try {
message.writeTo(cos);
cos.flush();
return ByteString.copyFrom(MessageDigest.getInstance("SHA-256").digest(baos.toByteArray()));
} catch (final IOException e) {
throw new UncheckedIOException(e);
} catch (final NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
}
@@ -45,7 +45,7 @@ public class RemoteConfigsManager {
}
/**
* Returns all remote configuration (client and global) for the specified account and client.
* Returns all remote configuration (per-platform and global) for the specified account and client.
*
* @param accountIdentifier the identifier of the authenticated account
* @param userAgent the requester's user agent, used to select the client platform
@@ -61,10 +61,10 @@ public class RemoteConfigsManager {
throw new AssertionError(e);
}
final List<RemoteConfig> clientConfig = remoteConfigSupplier.get();
final List<RemoteConfig> perPlatformConfig = remoteConfigSupplier.get();
return Stream.concat(
clientConfig.stream()
perPlatformConfig.stream()
.filter(config -> {
final String firstNameComponent = config.getName().split("\\.", 2)[0];
return firstNameComponent.equals(platformPrefix) || !PLATFORM_PREFIXES.contains(firstNameComponent);
@@ -0,0 +1,56 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
syntax = "proto3";
option java_multiple_files = true;
package org.signal.chat.remoteconfiguration;
import "org/signal/chat/require.proto";
import "org/signal/chat/tag.proto";
// Provides remote configuration applicable to the authenticated user
service RemoteConfiguration {
option (require.auth) = AUTH_ONLY_AUTHENTICATED;
// Fetches the configuration for the authenticated user. Returned
// values are based on the authenticated account and caller's client platform,
// which is derived from the "User-Agent" header.
rpc GetConfiguration(GetConfigurationRequest) returns (GetConfigurationResponse) {}
}
message GetConfigurationRequest {
// If present, the etag from a prior GetConfigurationResponse. If the
// provided etag matches the current configuration etag, the server may elide
// the configuration response.
bytes etag = 1;
}
message GetConfigurationResponse {
oneof response {
// The full configuration and corresponding etag.
TaggedConfiguration tagged_configuration = 1;
// The etag in the request matched the current configuration etag.
bool etag_matched = 2 [(tag.reason) = "etag_match"];
}
}
message Configuration {
// A map of namespaced configuration keys to their resolved values. All
// configuration values are represented as strings. boolean values are
// represented as the strings "true" or "false".
map<string, string> configuration = 1;
}
message TaggedConfiguration {
Configuration configuration = 1;
// An entity tag for `configuration`. This may be supplied in a
// subsequent request to optimize bandwidth when the configuration has not
// changed.
bytes etag = 2;
}
@@ -0,0 +1,152 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.grpc;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import com.google.common.net.InetAddresses;
import com.google.protobuf.ByteString;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.signal.chat.remoteconfiguration.GetConfigurationRequest;
import org.signal.chat.remoteconfiguration.GetConfigurationResponse;
import org.signal.chat.remoteconfiguration.RemoteConfigurationGrpc;
import org.whispersystems.textsecuregcm.storage.RemoteConfigsManager;
class RemoteConfigurationGrpcServiceTest extends
SimpleBaseGrpcTest<RemoteConfigurationGrpcService, RemoteConfigurationGrpc.RemoteConfigurationBlockingStub> {
@Mock
private RemoteConfigsManager remoteConfigsManager;
@Override
protected RemoteConfigurationGrpcService createServiceBeforeEachTest() {
getMockRequestAttributesInterceptor().setRequestAttributes(
new RequestAttributes(InetAddresses.forString("127.0.0.1"), null, null));
return new RemoteConfigurationGrpcService(remoteConfigsManager);
}
@Test
void getConfigurationUnchanged() {
final String userAgent = "Signal-Android/7.6.2 Android/34 libsignal/0.46.0";
getMockRequestAttributesInterceptor().setRequestAttributes(
new RequestAttributes(InetAddresses.forString("127.0.0.1"), userAgent, null));
when(remoteConfigsManager.getConfigForAccount(AUTHENTICATED_ACI, userAgent))
.thenReturn(Map.of("test.test", "bar", "global.test", "false"));
final GetConfigurationResponse response = authenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().build());
assertTrue(response.hasTaggedConfiguration());
final ByteString etag = response.getTaggedConfiguration().getEtag();
final GetConfigurationResponse cachedResponse = authenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().setEtag(etag).build());
assertTrue(cachedResponse.getEtagMatched());
assertFalse(cachedResponse.hasTaggedConfiguration());
}
@Test
void getConfigurationChanged() {
final String userAgent = "Signal-Android/7.6.2 Android/34 libsignal/0.46.0";
getMockRequestAttributesInterceptor().setRequestAttributes(
new RequestAttributes(InetAddresses.forString("127.0.0.1"), userAgent, null));
final Map<String, String> config1 = Map.of(
"android.stickers", "foo",
"test.test", "bar",
"global.test", "false");
when(remoteConfigsManager.getConfigForAccount(AUTHENTICATED_ACI, userAgent)).thenReturn(config1);
final GetConfigurationResponse response = authenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().build());
assertTrue(response.hasTaggedConfiguration());
final ByteString etag = response.getTaggedConfiguration().getEtag();
final Map<String, String> config2 = new HashMap<>(config1);
config2.put("android.new.config", "true");
when(remoteConfigsManager.getConfigForAccount(AUTHENTICATED_ACI, userAgent)).thenReturn(config2);
final GetConfigurationResponse afterChange = authenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().setEtag(etag).build());
assertFalse(afterChange.getEtagMatched());
assertTrue(afterChange.hasTaggedConfiguration());
}
@ParameterizedTest
@MethodSource
void getConfigurationEtag(final boolean expectMatched, final Map<String, String> config1,
final Map<String, String> config2) {
final UUID user1 = UUID.randomUUID();
final String ua1 = "user-agent-1";
final UUID user2 = UUID.randomUUID();
final String ua2 = "user-agent-2";
when(remoteConfigsManager.getConfigForAccount(user1, ua1)).thenReturn(config1);
when(remoteConfigsManager.getConfigForAccount(user2, ua2)).thenReturn(config2);
getMockAuthenticationInterceptor().setAuthenticatedDevice(user1, AUTHENTICATED_DEVICE_ID);
getMockRequestAttributesInterceptor().setRequestAttributes(
new RequestAttributes(InetAddresses.forString("127.0.0.1"), ua1, null));
final GetConfigurationResponse response = authenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().build());
assertTrue(response.hasTaggedConfiguration());
final ByteString etag = response.getTaggedConfiguration().getEtag();
getMockAuthenticationInterceptor().setAuthenticatedDevice(user2, AUTHENTICATED_DEVICE_ID);
getMockRequestAttributesInterceptor().setRequestAttributes(
new RequestAttributes(InetAddresses.forString("127.0.0.1"), ua2, null));
final GetConfigurationResponse secondResponse = authenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().setEtag(etag).build());
if (expectMatched) {
assertTrue(secondResponse.getEtagMatched());
assertFalse(secondResponse.hasTaggedConfiguration());
} else {
assertFalse(secondResponse.getEtagMatched());
assertTrue(secondResponse.hasTaggedConfiguration());
}
}
static List<Arguments> getConfigurationEtag() {
final Map<String, String> config1 = Map.of(
"android.stickers", "foo",
"test.test", "bar",
"global.test", "false");
final Map<String, String> config2 = Map.of(
"android.stickers", "foo",
"test.test", "bar",
"global.test", "true");
return List.of(
Arguments.argumentSet("same", true, config1, config1),
Arguments.argumentSet("change", false, config1, config2),
Arguments.argumentSet("different sort", true,
new TreeMap<>(config1),
config1.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, _) -> a, () -> new TreeMap<String, String>(Comparator.reverseOrder()))))
);
}
}