Skip to content

Commit 38b7379

Browse files
committed
test: enable gax-grpc
1 parent fe4099e commit 38b7379

3 files changed

Lines changed: 74 additions & 49 deletions

File tree

sdk-platform-java/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
import java.util.concurrent.TimeUnit;
8383
import java.util.logging.Level;
8484
import java.util.logging.Logger;
85+
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
86+
import io.grpc.netty.shaded.io.netty.handler.ssl.ApplicationProtocolConfig;
87+
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
88+
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
89+
import io.grpc.netty.shaded.io.netty.handler.ssl.SslProvider;
8590
import javax.annotation.Nullable;
8691
import javax.net.ssl.KeyManagerFactory;
8792

@@ -816,6 +821,8 @@ public ManagedChannelBuilder<?> createDecoratedChannelBuilder() throws IOExcepti
816821
builder = channelConfigurator.apply(builder);
817822
}
818823

824+
configurePqc(builder);
825+
819826
return builder;
820827
}
821828

@@ -829,6 +836,29 @@ private ManagedChannel createSingleChannel() throws IOException {
829836
return managedChannel;
830837
}
831838

839+
private void configurePqc(ManagedChannelBuilder<?> builder) {
840+
NettyChannelBuilder nettyBuilder = (NettyChannelBuilder) builder;
841+
try {
842+
ApplicationProtocolConfig apn =
843+
new ApplicationProtocolConfig(
844+
ApplicationProtocolConfig.Protocol.ALPN,
845+
ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE,
846+
ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT,
847+
"h2");
848+
849+
SslContext shadedSslContext =
850+
SslContextBuilder.forClient()
851+
.sslProvider(SslProvider.JDK)
852+
.protocols("TLSv1.3")
853+
.applicationProtocolConfig(apn)
854+
.build();
855+
856+
nettyBuilder.sslContext(shadedSslContext);
857+
} catch (Exception e) {
858+
LOG.log(Level.WARNING, "Failed to configure shaded gRPC Netty channel for PQC", e);
859+
}
860+
}
861+
832862
/* Remove provided headers that will also get set by {@link com.google.auth.ApiKeyCredentials}. They will be added as part of the grpc call when performing auth
833863
* {@link io.grpc.auth.GoogleAuthLibraryCallCredentials#applyRequestMetadata}. GRPC does not dedup headers {@link https://github.com/grpc/grpc-java/blob/a140e1bb0cfa662bcdb7823d73320eb8d49046f1/api/src/main/java/io/grpc/Metadata.java#L504} so we must before initiating the call.
834864
*

sdk-platform-java/pqc-test/pqc-test-common/src/main/java/com/google/api/gax/httpjson/PqcConnectivityTest.java

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
import static org.junit.jupiter.api.Assertions.fail;
3636

3737
import com.google.api.gax.core.NoCredentialsProvider;
38-
import com.google.api.gax.rpc.ApiException;
39-
import com.google.api.gax.rpc.StatusCode;
40-
import com.google.cloud.NoCredentials;
41-
import com.google.cloud.bigquery.*;
42-
import com.google.cloud.translate.v3.*;
4338
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
4439
import io.grpc.netty.shaded.io.netty.handler.ssl.ApplicationProtocolConfig;
4540
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
@@ -272,51 +267,38 @@ public static void teardown() {
272267
}
273268

274269
private void runGrpcTest(int port, boolean shouldSucceed) throws Exception {
275-
TranslationServiceSettings.Builder settingsBuilder =
276-
TranslationServiceSettings.newBuilder()
270+
com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.Builder channelProviderBuilder =
271+
com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.newBuilder()
277272
.setEndpoint("localhost:" + port)
278-
.setCredentialsProvider(NoCredentialsProvider.create());
273+
.setHeaderProvider(() -> java.util.Collections.emptyMap());
279274

280-
com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.Builder channelProviderBuilder =
281-
((com.google.api.gax.grpc.InstantiatingGrpcChannelProvider)
282-
settingsBuilder.getTransportChannelProvider())
283-
.toBuilder();
284-
channelProviderBuilder.setChannelConfigurator(
285-
new com.google.api.core.ApiFunction<
286-
io.grpc.ManagedChannelBuilder, io.grpc.ManagedChannelBuilder>() {
287-
@Override
288-
public io.grpc.ManagedChannelBuilder apply(io.grpc.ManagedChannelBuilder builder) {
289-
if (clientSupportsPqc()) {
290-
configureGrpcChannelForPqc(builder);
291-
} else {
292-
configureGrpcChannelForClassical(builder);
293-
}
294-
return builder;
295-
}
296-
});
297-
settingsBuilder.setTransportChannelProvider(channelProviderBuilder.build());
298-
299-
TranslationServiceSettings settings = settingsBuilder.build();
300-
301-
try (TranslationServiceClient client = TranslationServiceClient.create(settings)) {
302-
List<String> contents = new ArrayList<>();
303-
contents.add("house");
304-
TranslateTextRequest request =
305-
TranslateTextRequest.newBuilder()
306-
.setParent("projects/test-project")
307-
.addAllContents(contents)
275+
276+
com.google.api.gax.grpc.InstantiatingGrpcChannelProvider channelProvider = channelProviderBuilder.build();
277+
com.google.api.gax.rpc.TransportChannel transportChannel = channelProvider.getTransportChannel();
278+
io.grpc.Channel channel = ((com.google.api.gax.grpc.GrpcTransportChannel) transportChannel).getChannel();
279+
280+
try {
281+
io.grpc.MethodDescriptor<byte[], byte[]> method =
282+
io.grpc.MethodDescriptor.<byte[], byte[]>newBuilder()
283+
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
284+
.setFullMethodName("Greeter/SayHello")
285+
.setRequestMarshaller(new ByteMarshaller())
286+
.setResponseMarshaller(new ByteMarshaller())
308287
.build();
309288

310-
try {
311-
TranslateTextResponse response = client.translateText(request);
312-
if (!shouldSucceed) {
313-
fail("Expected gRPC call to fail!");
314-
}
315-
assertNotNull(response);
316-
} catch (ApiException e) {
317-
if (shouldSucceed) {
318-
fail("Expected gRPC call to succeed, but failed: " + e.getMessage(), e);
319-
}
289+
byte[] response = io.grpc.stub.ClientCalls.blockingUnaryCall(channel, method, io.grpc.CallOptions.DEFAULT, "request".getBytes());
290+
if (!shouldSucceed) {
291+
fail("Expected gRPC call to fail!");
292+
}
293+
assertNotNull(response);
294+
assertEquals("PQC gRPC OK", new String(response));
295+
} catch (Exception e) {
296+
if (shouldSucceed) {
297+
fail("Expected gRPC call to succeed, but failed: " + e.getMessage(), e);
298+
}
299+
} finally {
300+
if (channel instanceof io.grpc.ManagedChannel) {
301+
((io.grpc.ManagedChannel) channel).shutdownNow();
320302
}
321303
}
322304
}
@@ -395,4 +377,20 @@ private static void configureGrpcChannelForClassical(io.grpc.ManagedChannelBuild
395377
throw new RuntimeException("Failed to configure shaded gRPC Netty channel for Classical", e);
396378
}
397379
}
380+
381+
private static class ByteMarshaller implements io.grpc.MethodDescriptor.Marshaller<byte[]> {
382+
@Override
383+
public InputStream stream(byte[] value) {
384+
return new java.io.ByteArrayInputStream(value);
385+
}
386+
387+
@Override
388+
public byte[] parse(InputStream stream) {
389+
try {
390+
return com.google.common.io.ByteStreams.toByteArray(stream);
391+
} catch (java.io.IOException e) {
392+
throw new RuntimeException(e);
393+
}
394+
}
395+
}
398396
}

sdk-platform-java/pqc-test/pqc-test-common/src/main/java/com/google/api/gax/pqc/PqcTestServer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.io.OutputStream;
4343
import java.net.InetSocketAddress;
4444
import java.security.KeyStore;
45-
import java.security.Security;
4645
import java.util.List;
4746
import java.util.function.BiFunction;
4847
import javax.net.ssl.KeyManagerFactory;
@@ -228,8 +227,6 @@ public void stop() {
228227
if (httpServerClassical != null) httpServerClassical.stop(0);
229228
if (grpcServerPqc != null) grpcServerPqc.shutdown();
230229
if (grpcServerClassical != null) grpcServerClassical.shutdown();
231-
Security.removeProvider("BC");
232-
Security.removeProvider("BCJSSE");
233230
}
234231

235232
public int getHttpPqcPort() {

0 commit comments

Comments
 (0)