diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 048450e00..383673ec5 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -155,7 +155,7 @@ import org.whispersystems.textsecuregcm.grpc.PaymentsGrpcService; import org.whispersystems.textsecuregcm.grpc.ProfileAnonymousGrpcService; import org.whispersystems.textsecuregcm.grpc.ProfileGrpcService; import org.whispersystems.textsecuregcm.grpc.RequestAttributesInterceptor; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; import org.whispersystems.textsecuregcm.grpc.net.ManagedDefaultEventLoopGroup; import org.whispersystems.textsecuregcm.grpc.net.ManagedLocalGrpcServer; import org.whispersystems.textsecuregcm.grpc.net.ManagedNioEventLoopGroup; @@ -820,7 +820,7 @@ public class WhisperServerService extends Application getAuthenticatedDevice(final ServerCall call) { if (call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) instanceof LocalAddress localAddress) { - return clientConnectionManager.getAuthenticatedDevice(localAddress); + return grpcClientConnectionManager.getAuthenticatedDevice(localAddress); } else { throw new AssertionError("Unexpected channel type: " + call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptor.java b/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptor.java index 4de5c4d35..5a44ebe3f 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptor.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptor.java @@ -3,7 +3,7 @@ package org.whispersystems.textsecuregcm.auth.grpc; import io.grpc.Metadata; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; /** * A "prohibit authentication" interceptor ensures that requests to endpoints that should be invoked anonymously do not @@ -12,8 +12,8 @@ import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; */ public class ProhibitAuthenticationInterceptor extends AbstractAuthenticationInterceptor { - public ProhibitAuthenticationInterceptor(final ClientConnectionManager clientConnectionManager) { - super(clientConnectionManager); + public ProhibitAuthenticationInterceptor(final GrpcClientConnectionManager grpcClientConnectionManager) { + super(grpcClientConnectionManager); } @Override diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptor.java b/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptor.java index 9ef160f1a..c24b0af77 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptor.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptor.java @@ -5,7 +5,7 @@ import io.grpc.Contexts; import io.grpc.Metadata; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; /** * A "require authentication" interceptor requires that requests be issued from a connection that is associated with an @@ -14,8 +14,8 @@ import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; */ public class RequireAuthenticationInterceptor extends AbstractAuthenticationInterceptor { - public RequireAuthenticationInterceptor(final ClientConnectionManager clientConnectionManager) { - super(clientConnectionManager); + public RequireAuthenticationInterceptor(final GrpcClientConnectionManager grpcClientConnectionManager) { + super(grpcClientConnectionManager); } @Override diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesInterceptor.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesInterceptor.java index d9cdbd2d6..06c6f957a 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesInterceptor.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesInterceptor.java @@ -11,7 +11,7 @@ import io.grpc.Status; import io.netty.channel.local.LocalAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; import org.whispersystems.textsecuregcm.util.ua.UserAgent; import java.net.InetAddress; import java.util.List; @@ -20,12 +20,12 @@ import java.util.Optional; public class RequestAttributesInterceptor implements ServerInterceptor { - private final ClientConnectionManager clientConnectionManager; + private final GrpcClientConnectionManager grpcClientConnectionManager; private static final Logger log = LoggerFactory.getLogger(RequestAttributesInterceptor.class); - public RequestAttributesInterceptor(final ClientConnectionManager clientConnectionManager) { - this.clientConnectionManager = clientConnectionManager; + public RequestAttributesInterceptor(final GrpcClientConnectionManager grpcClientConnectionManager) { + this.grpcClientConnectionManager = grpcClientConnectionManager; } @Override @@ -37,7 +37,7 @@ public class RequestAttributesInterceptor implements ServerInterceptor { Context context = Context.current(); { - final Optional maybeRemoteAddress = clientConnectionManager.getRemoteAddress(localAddress); + final Optional maybeRemoteAddress = grpcClientConnectionManager.getRemoteAddress(localAddress); if (maybeRemoteAddress.isEmpty()) { // We should never have a call from a party whose remote address we can't identify @@ -52,7 +52,7 @@ public class RequestAttributesInterceptor implements ServerInterceptor { { final Optional> maybeAcceptLanguage = - clientConnectionManager.getAcceptableLanguages(localAddress); + grpcClientConnectionManager.getAcceptableLanguages(localAddress); if (maybeAcceptLanguage.isPresent()) { context = context.withValue(RequestAttributesUtil.ACCEPT_LANGUAGE_CONTEXT_KEY, maybeAcceptLanguage.get()); @@ -60,7 +60,7 @@ public class RequestAttributesInterceptor implements ServerInterceptor { } { - final Optional maybeUserAgent = clientConnectionManager.getUserAgent(localAddress); + final Optional maybeUserAgent = grpcClientConnectionManager.getUserAgent(localAddress); if (maybeUserAgent.isPresent()) { context = context.withValue(RequestAttributesUtil.USER_AGENT_CONTEXT_KEY, maybeUserAgent.get()); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/EstablishLocalGrpcConnectionHandler.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/EstablishLocalGrpcConnectionHandler.java index e06782c9e..2c95252d6 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/EstablishLocalGrpcConnectionHandler.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/EstablishLocalGrpcConnectionHandler.java @@ -22,7 +22,7 @@ import org.slf4j.LoggerFactory; */ class EstablishLocalGrpcConnectionHandler extends ChannelInboundHandlerAdapter { - private final ClientConnectionManager clientConnectionManager; + private final GrpcClientConnectionManager grpcClientConnectionManager; private final LocalAddress authenticatedGrpcServerAddress; private final LocalAddress anonymousGrpcServerAddress; @@ -31,11 +31,11 @@ class EstablishLocalGrpcConnectionHandler extends ChannelInboundHandlerAdapter { private static final Logger log = LoggerFactory.getLogger(EstablishLocalGrpcConnectionHandler.class); - public EstablishLocalGrpcConnectionHandler(final ClientConnectionManager clientConnectionManager, + public EstablishLocalGrpcConnectionHandler(final GrpcClientConnectionManager grpcClientConnectionManager, final LocalAddress authenticatedGrpcServerAddress, final LocalAddress anonymousGrpcServerAddress) { - this.clientConnectionManager = clientConnectionManager; + this.grpcClientConnectionManager = grpcClientConnectionManager; this.authenticatedGrpcServerAddress = authenticatedGrpcServerAddress; this.anonymousGrpcServerAddress = anonymousGrpcServerAddress; @@ -70,7 +70,7 @@ class EstablishLocalGrpcConnectionHandler extends ChannelInboundHandlerAdapter { .connect() .addListener((ChannelFutureListener) localChannelFuture -> { if (localChannelFuture.isSuccess()) { - clientConnectionManager.handleConnectionEstablished((LocalChannel) localChannelFuture.channel(), + grpcClientConnectionManager.handleConnectionEstablished((LocalChannel) localChannelFuture.channel(), remoteChannelContext.channel(), noiseIdentityDeterminedEvent.authenticatedDevice()); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/ClientConnectionManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/GrpcClientConnectionManager.java similarity index 92% rename from service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/ClientConnectionManager.java rename to service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/GrpcClientConnectionManager.java index 73aa895c2..f937647bc 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/ClientConnectionManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/GrpcClientConnectionManager.java @@ -30,14 +30,14 @@ import org.whispersystems.textsecuregcm.util.ua.UserAgentUtil; * authenticated identity of the device that opened the connection (for non-anonymous connections). It can also close * connections associated with a given device if that device's credentials have changed and clients must reauthenticate. */ -public class ClientConnectionManager { +public class GrpcClientConnectionManager { private final Map remoteChannelsByLocalAddress = new ConcurrentHashMap<>(); private final Map> remoteChannelsByAuthenticatedDevice = new ConcurrentHashMap<>(); @VisibleForTesting static final AttributeKey AUTHENTICATED_DEVICE_ATTRIBUTE_KEY = - AttributeKey.valueOf(ClientConnectionManager.class, "authenticatedDevice"); + AttributeKey.valueOf(GrpcClientConnectionManager.class, "authenticatedDevice"); @VisibleForTesting static final AttributeKey REMOTE_ADDRESS_ATTRIBUTE_KEY = @@ -55,7 +55,7 @@ public class ClientConnectionManager { static final AttributeKey> ACCEPT_LANGUAGE_ATTRIBUTE_KEY = AttributeKey.valueOf(WebsocketHandshakeCompleteHandler.class, "acceptLanguage"); - private static final Logger log = LoggerFactory.getLogger(ClientConnectionManager.class); + private static final Logger log = LoggerFactory.getLogger(GrpcClientConnectionManager.class); /** * Returns the authenticated device associated with the given local address, if any. An authenticated device is @@ -154,13 +154,13 @@ public class ClientConnectionManager { @Nullable final String userAgentHeader, @Nullable final String acceptLanguageHeader) { - channel.attr(ClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).set(preferredRemoteAddress); + channel.attr(GrpcClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).set(preferredRemoteAddress); if (StringUtils.isNotBlank(userAgentHeader)) { - channel.attr(ClientConnectionManager.RAW_USER_AGENT_ATTRIBUTE_KEY).set(userAgentHeader); + channel.attr(GrpcClientConnectionManager.RAW_USER_AGENT_ATTRIBUTE_KEY).set(userAgentHeader); try { - channel.attr(ClientConnectionManager.PARSED_USER_AGENT_ATTRIBUTE_KEY) + channel.attr(GrpcClientConnectionManager.PARSED_USER_AGENT_ATTRIBUTE_KEY) .set(UserAgentUtil.parseUserAgentString(userAgentHeader)); } catch (final UnrecognizedUserAgentException ignored) { } @@ -168,7 +168,7 @@ public class ClientConnectionManager { if (StringUtils.isNotBlank(acceptLanguageHeader)) { try { - channel.attr(ClientConnectionManager.ACCEPT_LANGUAGE_ATTRIBUTE_KEY).set(Locale.LanguageRange.parse(acceptLanguageHeader)); + channel.attr(GrpcClientConnectionManager.ACCEPT_LANGUAGE_ATTRIBUTE_KEY).set(Locale.LanguageRange.parse(acceptLanguageHeader)); } catch (final IllegalArgumentException e) { log.debug("Invalid Accept-Language header from User-Agent {}: {}", userAgentHeader, acceptLanguageHeader, e); } @@ -188,7 +188,7 @@ public class ClientConnectionManager { @SuppressWarnings("OptionalUsedAsFieldOrParameterType") final Optional maybeAuthenticatedDevice) { maybeAuthenticatedDevice.ifPresent(authenticatedDevice -> - remoteChannel.attr(ClientConnectionManager.AUTHENTICATED_DEVICE_ATTRIBUTE_KEY).set(authenticatedDevice)); + remoteChannel.attr(GrpcClientConnectionManager.AUTHENTICATED_DEVICE_ATTRIBUTE_KEY).set(authenticatedDevice)); remoteChannelsByLocalAddress.put(localChannel.localAddress(), remoteChannel); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServer.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServer.java index 537a2baff..fcf06beff 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServer.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServer.java @@ -50,7 +50,7 @@ public class NoiseWebSocketTunnelServer implements Managed { @Nullable final PrivateKey tlsPrivateKey, final NioEventLoopGroup eventLoopGroup, final Executor delegatedTaskExecutor, - final ClientConnectionManager clientConnectionManager, + final GrpcClientConnectionManager grpcClientConnectionManager, final ClientPublicKeysManager clientPublicKeysManager, final ECKeyPair ecKeyPair, final LocalAddress authenticatedGrpcServerAddress, @@ -109,7 +109,7 @@ public class NoiseWebSocketTunnelServer implements Managed { .addLast(new WebsocketHandshakeCompleteHandler(clientPublicKeysManager, ecKeyPair, recognizedProxySecret)) // This handler will open a local connection to the appropriate gRPC server and install a ProxyHandler // once the Noise handshake has completed - .addLast(new EstablishLocalGrpcConnectionHandler(clientConnectionManager, authenticatedGrpcServerAddress, anonymousGrpcServerAddress)) + .addLast(new EstablishLocalGrpcConnectionHandler(grpcClientConnectionManager, authenticatedGrpcServerAddress, anonymousGrpcServerAddress)) .addLast(new ErrorHandler()); } }); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandler.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandler.java index fbcfc5470..87b09d486 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandler.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandler.java @@ -74,7 +74,7 @@ class WebsocketHandshakeCompleteHandler extends ChannelInboundHandlerAdapter { preferredRemoteAddress = maybePreferredRemoteAddress.get(); } - ClientConnectionManager.handleWebSocketHandshakeComplete(context.channel(), + GrpcClientConnectionManager.handleWebSocketHandshakeComplete(context.channel(), preferredRemoteAddress, handshakeCompleteEvent.requestHeaders().getAsString(HttpHeaderNames.USER_AGENT), handshakeCompleteEvent.requestHeaders().getAsString(HttpHeaderNames.ACCEPT_LANGUAGE)); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/AbstractAuthenticationInterceptorTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/AbstractAuthenticationInterceptorTest.java index 81baf8686..95bacd076 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/AbstractAuthenticationInterceptorTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/AbstractAuthenticationInterceptorTest.java @@ -18,13 +18,13 @@ import org.signal.chat.rpc.GetAuthenticatedDeviceRequest; import org.signal.chat.rpc.GetAuthenticatedDeviceResponse; import org.signal.chat.rpc.RequestAttributesGrpc; import org.whispersystems.textsecuregcm.grpc.RequestAttributesServiceImpl; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; abstract class AbstractAuthenticationInterceptorTest { private static DefaultEventLoopGroup eventLoopGroup; - private ClientConnectionManager clientConnectionManager; + private GrpcClientConnectionManager grpcClientConnectionManager; private Server server; private ManagedChannel managedChannel; @@ -38,7 +38,7 @@ abstract class AbstractAuthenticationInterceptorTest { void setUp() throws IOException { final LocalAddress serverAddress = new LocalAddress("test-authentication-interceptor-server"); - clientConnectionManager = mock(ClientConnectionManager.class); + grpcClientConnectionManager = mock(GrpcClientConnectionManager.class); // `RequestAttributesInterceptor` operates on `LocalAddresses`, so we need to do some slightly fancy plumbing to make // sure that we're using local channels and addresses @@ -66,8 +66,8 @@ abstract class AbstractAuthenticationInterceptorTest { protected abstract AbstractAuthenticationInterceptor getInterceptor(); - protected ClientConnectionManager getClientConnectionManager() { - return clientConnectionManager; + protected GrpcClientConnectionManager getClientConnectionManager() { + return grpcClientConnectionManager; } protected GetAuthenticatedDeviceResponse getAuthenticatedDevice() { diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptorTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptorTest.java index 50f74d980..2a02d9dae 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptorTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/ProhibitAuthenticationInterceptorTest.java @@ -4,9 +4,8 @@ import io.grpc.Status; import org.junit.jupiter.api.Test; import org.signal.chat.rpc.GetAuthenticatedDeviceResponse; import org.whispersystems.textsecuregcm.grpc.GrpcTestUtils; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; import org.whispersystems.textsecuregcm.storage.Device; -import org.whispersystems.textsecuregcm.util.UUIDUtil; import java.util.Optional; import java.util.UUID; @@ -24,16 +23,16 @@ class ProhibitAuthenticationInterceptorTest extends AbstractAuthenticationInterc @Test void interceptCall() { - final ClientConnectionManager clientConnectionManager = getClientConnectionManager(); + final GrpcClientConnectionManager grpcClientConnectionManager = getClientConnectionManager(); - when(clientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.empty()); + when(grpcClientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.empty()); final GetAuthenticatedDeviceResponse response = getAuthenticatedDevice(); assertTrue(response.getAccountIdentifier().isEmpty()); assertEquals(0, response.getDeviceId()); final AuthenticatedDevice authenticatedDevice = new AuthenticatedDevice(UUID.randomUUID(), Device.PRIMARY_ID); - when(clientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.of(authenticatedDevice)); + when(grpcClientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.of(authenticatedDevice)); GrpcTestUtils.assertStatusException(Status.UNAUTHENTICATED, this::getAuthenticatedDevice); } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptorTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptorTest.java index a8b2f5e8f..e30d1735a 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptorTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/auth/grpc/RequireAuthenticationInterceptorTest.java @@ -10,7 +10,7 @@ import java.util.UUID; import org.junit.jupiter.api.Test; import org.signal.chat.rpc.GetAuthenticatedDeviceResponse; import org.whispersystems.textsecuregcm.grpc.GrpcTestUtils; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; import org.whispersystems.textsecuregcm.storage.Device; import org.whispersystems.textsecuregcm.util.UUIDUtil; @@ -23,14 +23,14 @@ class RequireAuthenticationInterceptorTest extends AbstractAuthenticationInterce @Test void interceptCall() { - final ClientConnectionManager clientConnectionManager = getClientConnectionManager(); + final GrpcClientConnectionManager grpcClientConnectionManager = getClientConnectionManager(); - when(clientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.empty()); + when(grpcClientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.empty()); GrpcTestUtils.assertStatusException(Status.UNAUTHENTICATED, this::getAuthenticatedDevice); final AuthenticatedDevice authenticatedDevice = new AuthenticatedDevice(UUID.randomUUID(), Device.PRIMARY_ID); - when(clientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.of(authenticatedDevice)); + when(grpcClientConnectionManager.getAuthenticatedDevice(any())).thenReturn(Optional.of(authenticatedDevice)); final GetAuthenticatedDeviceResponse response = getAuthenticatedDevice(); assertEquals(UUIDUtil.toByteString(authenticatedDevice.accountIdentifier()), response.getAccountIdentifier()); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesUtilTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesUtilTest.java index ef8e64e65..780469fc9 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesUtilTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/RequestAttributesUtilTest.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.util.List; import java.util.Locale; import java.util.Optional; -import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -30,7 +29,7 @@ import org.junit.jupiter.api.Test; import org.signal.chat.rpc.GetRequestAttributesRequest; import org.signal.chat.rpc.GetRequestAttributesResponse; import org.signal.chat.rpc.RequestAttributesGrpc; -import org.whispersystems.textsecuregcm.grpc.net.ClientConnectionManager; +import org.whispersystems.textsecuregcm.grpc.net.GrpcClientConnectionManager; import org.whispersystems.textsecuregcm.util.ua.UnrecognizedUserAgentException; import org.whispersystems.textsecuregcm.util.ua.UserAgent; import org.whispersystems.textsecuregcm.util.ua.UserAgentUtil; @@ -39,7 +38,7 @@ class RequestAttributesUtilTest { private static DefaultEventLoopGroup eventLoopGroup; - private ClientConnectionManager clientConnectionManager; + private GrpcClientConnectionManager grpcClientConnectionManager; private Server server; private ManagedChannel managedChannel; @@ -53,9 +52,9 @@ class RequestAttributesUtilTest { void setUp() throws IOException { final LocalAddress serverAddress = new LocalAddress("test-request-metadata-server"); - clientConnectionManager = mock(ClientConnectionManager.class); + grpcClientConnectionManager = mock(GrpcClientConnectionManager.class); - when(clientConnectionManager.getRemoteAddress(any())) + when(grpcClientConnectionManager.getRemoteAddress(any())) .thenReturn(Optional.of(InetAddresses.forString("127.0.0.1"))); // `RequestAttributesInterceptor` operates on `LocalAddresses`, so we need to do some slightly fancy plumbing to make @@ -64,7 +63,7 @@ class RequestAttributesUtilTest { .channelType(LocalServerChannel.class) .bossEventLoopGroup(eventLoopGroup) .workerEventLoopGroup(eventLoopGroup) - .intercept(new RequestAttributesInterceptor(clientConnectionManager)) + .intercept(new RequestAttributesInterceptor(grpcClientConnectionManager)) .addService(new RequestAttributesServiceImpl()) .build() .start(); @@ -89,12 +88,12 @@ class RequestAttributesUtilTest { @Test void getAcceptableLanguages() { - when(clientConnectionManager.getAcceptableLanguages(any())) + when(grpcClientConnectionManager.getAcceptableLanguages(any())) .thenReturn(Optional.empty()); assertTrue(getRequestAttributes().getAcceptableLanguagesList().isEmpty()); - when(clientConnectionManager.getAcceptableLanguages(any())) + when(grpcClientConnectionManager.getAcceptableLanguages(any())) .thenReturn(Optional.of(Locale.LanguageRange.parse("en,ja"))); assertEquals(List.of("en", "ja"), getRequestAttributes().getAcceptableLanguagesList()); @@ -102,12 +101,12 @@ class RequestAttributesUtilTest { @Test void getAvailableAcceptedLocales() { - when(clientConnectionManager.getAcceptableLanguages(any())) + when(grpcClientConnectionManager.getAcceptableLanguages(any())) .thenReturn(Optional.empty()); assertTrue(getRequestAttributes().getAvailableAcceptedLocalesList().isEmpty()); - when(clientConnectionManager.getAcceptableLanguages(any())) + when(grpcClientConnectionManager.getAcceptableLanguages(any())) .thenReturn(Optional.of(Locale.LanguageRange.parse("en,ja"))); final GetRequestAttributesResponse response = getRequestAttributes(); @@ -121,14 +120,14 @@ class RequestAttributesUtilTest { @Test void getRemoteAddress() { - when(clientConnectionManager.getRemoteAddress(any())) + when(grpcClientConnectionManager.getRemoteAddress(any())) .thenReturn(Optional.empty()); GrpcTestUtils.assertStatusException(Status.INTERNAL, this::getRequestAttributes); final String remoteAddressString = "6.7.8.9"; - when(clientConnectionManager.getRemoteAddress(any())) + when(grpcClientConnectionManager.getRemoteAddress(any())) .thenReturn(Optional.of(InetAddresses.forString(remoteAddressString))); assertEquals(remoteAddressString, getRequestAttributes().getRemoteAddress()); @@ -136,14 +135,14 @@ class RequestAttributesUtilTest { @Test void getUserAgent() throws UnrecognizedUserAgentException { - when(clientConnectionManager.getUserAgent(any())) + when(grpcClientConnectionManager.getUserAgent(any())) .thenReturn(Optional.empty()); assertFalse(getRequestAttributes().hasUserAgent()); final UserAgent userAgent = UserAgentUtil.parseUserAgentString("Signal-Desktop/1.2.3 Linux"); - when(clientConnectionManager.getUserAgent(any())) + when(grpcClientConnectionManager.getUserAgent(any())) .thenReturn(Optional.of(userAgent)); final GetRequestAttributesResponse response = getRequestAttributes(); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/ClientConnectionManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/GrpcClientConnectionManagerTest.java similarity index 64% rename from service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/ClientConnectionManagerTest.java rename to service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/GrpcClientConnectionManagerTest.java index 6921e39f2..ef132bf25 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/ClientConnectionManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/GrpcClientConnectionManagerTest.java @@ -36,7 +36,7 @@ import java.util.UUID; import static org.junit.jupiter.api.Assertions.*; -class ClientConnectionManagerTest { +class GrpcClientConnectionManagerTest { private static EventLoopGroup eventLoopGroup; @@ -45,7 +45,7 @@ class ClientConnectionManagerTest { private LocalServerChannel localServerChannel; - private ClientConnectionManager clientConnectionManager; + private GrpcClientConnectionManager grpcClientConnectionManager; @BeforeAll static void setUpBeforeAll() { @@ -56,7 +56,7 @@ class ClientConnectionManagerTest { void setUp() throws InterruptedException { eventLoopGroup = new DefaultEventLoopGroup(); - clientConnectionManager = new ClientConnectionManager(); + grpcClientConnectionManager = new GrpcClientConnectionManager(); // We have to jump through some hoops to get "real" LocalChannel instances to test with, and so we run a trivial // local server to which we can open trivial local connections @@ -100,10 +100,10 @@ class ClientConnectionManagerTest { @ParameterizedTest @MethodSource void getAuthenticatedDevice(@SuppressWarnings("OptionalUsedAsFieldOrParameterType") final Optional maybeAuthenticatedDevice) { - clientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, maybeAuthenticatedDevice); + grpcClientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, maybeAuthenticatedDevice); assertEquals(maybeAuthenticatedDevice, - clientConnectionManager.getAuthenticatedDevice(localChannel.localAddress())); + grpcClientConnectionManager.getAuthenticatedDevice(localChannel.localAddress())); } private static List> getAuthenticatedDevice() { @@ -115,62 +115,62 @@ class ClientConnectionManagerTest { @Test void getAcceptableLanguages() { - clientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); + grpcClientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); assertEquals(Optional.empty(), - clientConnectionManager.getAcceptableLanguages(localChannel.localAddress())); + grpcClientConnectionManager.getAcceptableLanguages(localChannel.localAddress())); final List acceptLanguageRanges = Locale.LanguageRange.parse("en,ja"); - remoteChannel.attr(ClientConnectionManager.ACCEPT_LANGUAGE_ATTRIBUTE_KEY).set(acceptLanguageRanges); + remoteChannel.attr(GrpcClientConnectionManager.ACCEPT_LANGUAGE_ATTRIBUTE_KEY).set(acceptLanguageRanges); assertEquals(Optional.of(acceptLanguageRanges), - clientConnectionManager.getAcceptableLanguages(localChannel.localAddress())); + grpcClientConnectionManager.getAcceptableLanguages(localChannel.localAddress())); } @Test void getRemoteAddress() { - clientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); + grpcClientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); assertEquals(Optional.empty(), - clientConnectionManager.getRemoteAddress(localChannel.localAddress())); + grpcClientConnectionManager.getRemoteAddress(localChannel.localAddress())); final InetAddress remoteAddress = InetAddresses.forString("6.7.8.9"); - remoteChannel.attr(ClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).set(remoteAddress); + remoteChannel.attr(GrpcClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).set(remoteAddress); assertEquals(Optional.of(remoteAddress), - clientConnectionManager.getRemoteAddress(localChannel.localAddress())); + grpcClientConnectionManager.getRemoteAddress(localChannel.localAddress())); } @Test void getUserAgent() throws UnrecognizedUserAgentException { - clientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); + grpcClientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); assertEquals(Optional.empty(), - clientConnectionManager.getUserAgent(localChannel.localAddress())); + grpcClientConnectionManager.getUserAgent(localChannel.localAddress())); final UserAgent userAgent = UserAgentUtil.parseUserAgentString("Signal-Desktop/1.2.3 Linux"); - remoteChannel.attr(ClientConnectionManager.PARSED_USER_AGENT_ATTRIBUTE_KEY).set(userAgent); + remoteChannel.attr(GrpcClientConnectionManager.PARSED_USER_AGENT_ATTRIBUTE_KEY).set(userAgent); assertEquals(Optional.of(userAgent), - clientConnectionManager.getUserAgent(localChannel.localAddress())); + grpcClientConnectionManager.getUserAgent(localChannel.localAddress())); } @Test void closeConnection() throws InterruptedException { final AuthenticatedDevice authenticatedDevice = new AuthenticatedDevice(UUID.randomUUID(), Device.PRIMARY_ID); - clientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.of(authenticatedDevice)); + grpcClientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.of(authenticatedDevice)); assertTrue(remoteChannel.isOpen()); - assertEquals(remoteChannel, clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertEquals(remoteChannel, grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); assertEquals(List.of(remoteChannel), - clientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); + grpcClientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); remoteChannel.close().await(); - assertNull(clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); - assertNull(clientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); + assertNull(grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertNull(grpcClientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); } @Test @@ -179,13 +179,13 @@ class ClientConnectionManagerTest { final InetAddress preferredRemoteAddress = InetAddresses.forString("192.168.1.1"); - ClientConnectionManager.handleWebSocketHandshakeComplete(embeddedChannel, + GrpcClientConnectionManager.handleWebSocketHandshakeComplete(embeddedChannel, preferredRemoteAddress, null, null); assertEquals(preferredRemoteAddress, - embeddedChannel.attr(ClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).get()); + embeddedChannel.attr(GrpcClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).get()); } @ParameterizedTest @@ -195,16 +195,16 @@ class ClientConnectionManagerTest { final EmbeddedChannel embeddedChannel = new EmbeddedChannel(); - ClientConnectionManager.handleWebSocketHandshakeComplete(embeddedChannel, + GrpcClientConnectionManager.handleWebSocketHandshakeComplete(embeddedChannel, InetAddresses.forString("127.0.0.1"), userAgentHeader, null); assertEquals(userAgentHeader, - embeddedChannel.attr(ClientConnectionManager.RAW_USER_AGENT_ATTRIBUTE_KEY).get()); + embeddedChannel.attr(GrpcClientConnectionManager.RAW_USER_AGENT_ATTRIBUTE_KEY).get()); assertEquals(expectedParsedUserAgent, - embeddedChannel.attr(ClientConnectionManager.PARSED_USER_AGENT_ATTRIBUTE_KEY).get()); + embeddedChannel.attr(GrpcClientConnectionManager.PARSED_USER_AGENT_ATTRIBUTE_KEY).get()); } private static List handleWebSocketHandshakeCompleteUserAgent() { @@ -228,13 +228,13 @@ class ClientConnectionManagerTest { final EmbeddedChannel embeddedChannel = new EmbeddedChannel(); - ClientConnectionManager.handleWebSocketHandshakeComplete(embeddedChannel, + GrpcClientConnectionManager.handleWebSocketHandshakeComplete(embeddedChannel, InetAddresses.forString("127.0.0.1"), null, acceptLanguageHeader); assertEquals(expectedLanguageRanges, - embeddedChannel.attr(ClientConnectionManager.ACCEPT_LANGUAGE_ATTRIBUTE_KEY).get()); + embeddedChannel.attr(GrpcClientConnectionManager.ACCEPT_LANGUAGE_ATTRIBUTE_KEY).get()); } private static List handleWebSocketHandshakeCompleteAcceptLanguage() { @@ -254,30 +254,30 @@ class ClientConnectionManagerTest { void handleConnectionEstablishedAuthenticated() throws InterruptedException { final AuthenticatedDevice authenticatedDevice = new AuthenticatedDevice(UUID.randomUUID(), Device.PRIMARY_ID); - assertNull(clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); - assertNull(clientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); + assertNull(grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertNull(grpcClientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); - clientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.of(authenticatedDevice)); + grpcClientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.of(authenticatedDevice)); - assertEquals(remoteChannel, clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); - assertEquals(List.of(remoteChannel), clientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); + assertEquals(remoteChannel, grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertEquals(List.of(remoteChannel), grpcClientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); remoteChannel.close().await(); - assertNull(clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); - assertNull(clientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); + assertNull(grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertNull(grpcClientConnectionManager.getRemoteChannelsByAuthenticatedDevice(authenticatedDevice)); } @Test void handleConnectionEstablishedAnonymous() throws InterruptedException { - assertNull(clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertNull(grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); - clientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); + grpcClientConnectionManager.handleConnectionEstablished(localChannel, remoteChannel, Optional.empty()); - assertEquals(remoteChannel, clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertEquals(remoteChannel, grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); remoteChannel.close().await(); - assertNull(clientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); + assertNull(grpcClientConnectionManager.getRemoteChannelByLocalAddress(localChannel.localAddress())); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServerIntegrationTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServerIntegrationTest.java index 655d17357..c637f7ac0 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServerIntegrationTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/NoiseWebSocketTunnelServerIntegrationTest.java @@ -86,7 +86,7 @@ class NoiseWebSocketTunnelServerIntegrationTest extends AbstractLeakDetectionTes private static X509Certificate serverTlsCertificate; - private ClientConnectionManager clientConnectionManager; + private GrpcClientConnectionManager grpcClientConnectionManager; private ClientPublicKeysManager clientPublicKeysManager; private ECKeyPair serverKeyPair; @@ -156,7 +156,7 @@ class NoiseWebSocketTunnelServerIntegrationTest extends AbstractLeakDetectionTes clientKeyPair = Curve.generateKeyPair(); serverKeyPair = Curve.generateKeyPair(); - clientConnectionManager = new ClientConnectionManager(); + grpcClientConnectionManager = new GrpcClientConnectionManager(); clientPublicKeysManager = mock(ClientPublicKeysManager.class); when(clientPublicKeysManager.findPublicKey(any(), anyByte())) @@ -172,8 +172,8 @@ class NoiseWebSocketTunnelServerIntegrationTest extends AbstractLeakDetectionTes @Override protected void configureServer(final ServerBuilder serverBuilder) { serverBuilder.addService(new RequestAttributesServiceImpl()) - .intercept(new RequestAttributesInterceptor(clientConnectionManager)) - .intercept(new RequireAuthenticationInterceptor(clientConnectionManager)); + .intercept(new RequestAttributesInterceptor(grpcClientConnectionManager)) + .intercept(new RequireAuthenticationInterceptor(grpcClientConnectionManager)); } }; @@ -183,8 +183,8 @@ class NoiseWebSocketTunnelServerIntegrationTest extends AbstractLeakDetectionTes @Override protected void configureServer(final ServerBuilder serverBuilder) { serverBuilder.addService(new RequestAttributesServiceImpl()) - .intercept(new RequestAttributesInterceptor(clientConnectionManager)) - .intercept(new ProhibitAuthenticationInterceptor(clientConnectionManager)); + .intercept(new RequestAttributesInterceptor(grpcClientConnectionManager)) + .intercept(new ProhibitAuthenticationInterceptor(grpcClientConnectionManager)); } }; @@ -195,7 +195,7 @@ class NoiseWebSocketTunnelServerIntegrationTest extends AbstractLeakDetectionTes serverTlsPrivateKey, nioEventLoopGroup, delegatedTaskExecutor, - clientConnectionManager, + grpcClientConnectionManager, clientPublicKeysManager, serverKeyPair, authenticatedGrpcServerAddress, @@ -209,7 +209,7 @@ class NoiseWebSocketTunnelServerIntegrationTest extends AbstractLeakDetectionTes null, nioEventLoopGroup, delegatedTaskExecutor, - clientConnectionManager, + grpcClientConnectionManager, clientPublicKeysManager, serverKeyPair, authenticatedGrpcServerAddress, @@ -569,7 +569,7 @@ class NoiseWebSocketTunnelServerIntegrationTest extends AbstractLeakDetectionTes assertEquals(UUIDUtil.toByteString(ACCOUNT_IDENTIFIER), response.getAccountIdentifier()); assertEquals(DEVICE_ID, response.getDeviceId()); - clientConnectionManager.closeConnection(new AuthenticatedDevice(ACCOUNT_IDENTIFIER, DEVICE_ID)); + grpcClientConnectionManager.closeConnection(new AuthenticatedDevice(ACCOUNT_IDENTIFIER, DEVICE_ID)); assertTrue(connectionCloseLatch.await(2, TimeUnit.SECONDS)); assertEquals(ApplicationWebSocketCloseReason.REAUTHENTICATION_REQUIRED.getStatusCode(), diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandlerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandlerTest.java index cb8f85f30..9c33db141 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandlerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/WebsocketHandshakeCompleteHandlerTest.java @@ -135,7 +135,7 @@ class WebsocketHandshakeCompleteHandlerTest extends AbstractLeakDetectionTest { embeddedChannel.pipeline().fireUserEventTriggered(handshakeCompleteEvent); assertEquals(expectedRemoteAddress, - embeddedChannel.attr(ClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).get()); + embeddedChannel.attr(GrpcClientConnectionManager.REMOTE_ADDRESS_ATTRIBUTE_KEY).get()); } private static List getRemoteAddress() {