Skip to content

Commit

Permalink
Migrate callers to ServerCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
ejona86 committed Nov 13, 2020
1 parent 2261227 commit c0137cc
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 58 deletions.
15 changes: 10 additions & 5 deletions benchmarks/src/jmh/java/io/grpc/benchmarks/TransportBenchmark.java
Expand Up @@ -19,10 +19,13 @@
import static io.grpc.benchmarks.Utils.pickUnusedPort;

import com.google.protobuf.ByteString;
import io.grpc.InsecureChannelCredentials;
import io.grpc.InsecureServerCredentials;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerCredentials;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.benchmarks.proto.BenchmarkServiceGrpc;
Expand Down Expand Up @@ -80,6 +83,7 @@ public enum Transport {

@Setup
public void setUp() throws Exception {
ServerCredentials serverCreds = InsecureServerCredentials.create();
ServerBuilder<?> serverBuilder;
ManagedChannelBuilder<?> channelBuilder;
switch (transport) {
Expand All @@ -93,7 +97,7 @@ public void setUp() throws Exception {
case NETTY:
{
InetSocketAddress address = new InetSocketAddress("localhost", pickUnusedPort());
serverBuilder = NettyServerBuilder.forAddress(address);
serverBuilder = NettyServerBuilder.forAddress(address, serverCreds);
channelBuilder = NettyChannelBuilder.forAddress(address)
.negotiationType(NegotiationType.PLAINTEXT);
break;
Expand All @@ -103,7 +107,7 @@ public void setUp() throws Exception {
String name = "bench" + Math.random();
LocalAddress address = new LocalAddress(name);
EventLoopGroup group = new DefaultEventLoopGroup();
serverBuilder = NettyServerBuilder.forAddress(address)
serverBuilder = NettyServerBuilder.forAddress(address, serverCreds)
.bossEventLoopGroup(group)
.workerEventLoopGroup(group)
.channelType(LocalServerChannel.class);
Expand All @@ -125,7 +129,7 @@ public void setUp() throws Exception {
Class<? extends ServerChannel> serverChannelClass =
Class.forName("io.netty.channel.epoll.EpollServerSocketChannel")
.asSubclass(ServerChannel.class);
serverBuilder = NettyServerBuilder.forAddress(address)
serverBuilder = NettyServerBuilder.forAddress(address, serverCreds)
.bossEventLoopGroup(group)
.workerEventLoopGroup(group)
.channelType(serverChannelClass);
Expand All @@ -143,8 +147,9 @@ public void setUp() throws Exception {
{
int port = pickUnusedPort();
InetSocketAddress address = new InetSocketAddress("localhost", port);
serverBuilder = NettyServerBuilder.forAddress(address);
channelBuilder = OkHttpChannelBuilder.forAddress("localhost", port).usePlaintext();
serverBuilder = NettyServerBuilder.forAddress(address, serverCreds);
channelBuilder = OkHttpChannelBuilder
.forAddress("localhost", port, InsecureChannelCredentials.create());
break;
}
default:
Expand Down
Expand Up @@ -18,13 +18,15 @@

import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.InsecureServerCredentials;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.Server;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerCredentials;
import io.grpc.ServerServiceDefinition;
import io.grpc.ServiceDescriptor;
import io.grpc.Status;
Expand Down Expand Up @@ -195,11 +197,12 @@ public void setup(ExecutorType clientExecutor,
ChannelType channelType,
int maxConcurrentStreams,
int channelCount) throws Exception {
ServerCredentials serverCreds = InsecureServerCredentials.create();
NettyServerBuilder serverBuilder;
NettyChannelBuilder channelBuilder;
if (channelType == ChannelType.LOCAL) {
LocalAddress address = new LocalAddress("netty-e2e-benchmark");
serverBuilder = NettyServerBuilder.forAddress(address);
serverBuilder = NettyServerBuilder.forAddress(address, serverCreds);
serverBuilder.channelType(LocalServerChannel.class);
channelBuilder = NettyChannelBuilder.forAddress(address);
channelBuilder.channelType(LocalChannel.class);
Expand All @@ -209,8 +212,8 @@ public void setup(ExecutorType clientExecutor,
sock.bind(new InetSocketAddress(BENCHMARK_ADDR, 0));
SocketAddress address = sock.getLocalSocketAddress();
sock.close();
serverBuilder =
NettyServerBuilder.forAddress(address).channelType(NioServerSocketChannel.class);
serverBuilder = NettyServerBuilder.forAddress(address, serverCreds)
.channelType(NioServerSocketChannel.class);
channelBuilder = NettyChannelBuilder.forAddress(address).channelType(NioSocketChannel.class);
}

Expand Down
Expand Up @@ -17,6 +17,7 @@
package io.grpc.benchmarks.driver;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.Status;
import io.grpc.benchmarks.proto.Control;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class LoadWorker {
.setDaemon(true)
.setNameFormat("load-worker-%d")
.build());
this.driverServer = NettyServerBuilder.forPort(driverPort)
this.driverServer = NettyServerBuilder.forPort(driverPort, InsecureServerCredentials.create())
.directExecutor()
.channelType(NioServerSocketChannel.class)
.workerEventLoopGroup(singleThreadGroup)
Expand Down
Expand Up @@ -18,13 +18,14 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.Grpc;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.ServerCredentials;
import io.grpc.ServerInterceptors;
import io.grpc.alts.AltsServerBuilder;
import io.grpc.TlsServerCredentials;
import io.grpc.alts.AltsServerCredentials;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyServerBuilder;
import io.netty.handler.ssl.SslContext;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -129,32 +130,22 @@ void parseArgs(String[] args) {
@VisibleForTesting
void start() throws Exception {
executor = Executors.newSingleThreadScheduledExecutor();
SslContext sslContext = null;
ServerCredentials serverCreds;
if (useAlts) {
server =
AltsServerBuilder.forPort(port)
.addService(
ServerInterceptors.intercept(
new TestServiceImpl(executor), TestServiceImpl.interceptors()))
.build()
.start();
serverCreds = AltsServerCredentials.create();
} else if (useTls) {
serverCreds = TlsServerCredentials.create(
TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"));
} else {
if (useTls) {
sslContext =
GrpcSslContexts.forServer(
TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"))
.build();
}
server =
NettyServerBuilder.forPort(port)
.sslContext(sslContext)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.addService(
ServerInterceptors.intercept(
new TestServiceImpl(executor), TestServiceImpl.interceptors()))
.build()
.start();
serverCreds = InsecureServerCredentials.create();
}
server = Grpc.newServerBuilderForPort(port, serverCreds)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.addService(
ServerInterceptors.intercept(
new TestServiceImpl(executor), TestServiceImpl.interceptors()))
.build()
.start();
}

@VisibleForTesting
Expand Down
Expand Up @@ -16,6 +16,7 @@

package io.grpc.testing.integration;

import io.grpc.InsecureServerCredentials;
import io.grpc.ServerBuilder;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.InternalNettyServerBuilder;
Expand All @@ -30,7 +31,7 @@ public class AutoWindowSizingOnTest extends AbstractInteropTest {

@Override
protected ServerBuilder<?> getServerBuilder() {
NettyServerBuilder builder = NettyServerBuilder.forPort(0)
NettyServerBuilder builder = NettyServerBuilder.forPort(0, InsecureServerCredentials.create())
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
// Disable the default census stats tracer, use testing tracer instead.
InternalNettyServerBuilder.setStatsEnabled(builder, false);
Expand Down
Expand Up @@ -18,19 +18,20 @@

import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.Server;
import io.grpc.ServerCredentials;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.netty.NettySslContextChannelCredentials;
import io.grpc.netty.NettySslContextServerCredentials;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.integration.Messages.ResponseParameters;
import io.grpc.testing.integration.Messages.StreamingOutputCallRequest;
import io.grpc.testing.integration.Messages.StreamingOutputCallResponse;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContext;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateException;
Expand Down Expand Up @@ -191,14 +192,13 @@ private Server newServer() throws CertificateException, IOException {
TestUtils.loadX509Cert("ca.pem")
};

SslContext sslContext =
ServerCredentials serverCreds = NettySslContextServerCredentials.create(
GrpcSslContexts.forServer(serverCertChainFile, serverPrivateKeyFile)
.trustManager(serverTrustedCaCerts)
.clientAuth(ClientAuth.REQUIRE)
.build();
.build());

return NettyServerBuilder.forPort(0)
.sslContext(sslContext)
return Grpc.newServerBuilderForPort(0, serverCreds)
.addService(new TestServiceImpl(serverExecutor))
.build()
.start();
Expand All @@ -211,16 +211,14 @@ private ManagedChannel newClientChannel() throws CertificateException, IOExcepti
TestUtils.loadX509Cert("ca.pem")
};

SslContext sslContext =
ChannelCredentials channelCreds = NettySslContextChannelCredentials.create(
GrpcSslContexts.forClient()
.keyManager(clientCertChainFile, clientPrivateKeyFile)
.trustManager(clientTrustedCaCerts)
.build();
.build());

return NettyChannelBuilder.forAddress("localhost", server.getPort())
return Grpc.newChannelBuilder("localhost:" + server.getPort(), channelCreds)
.overrideAuthority(TestUtils.TEST_SERVER_HOST)
.negotiationType(NegotiationType.TLS)
.sslContext(sslContext)
.build();
}
}
Expand Up @@ -20,12 +20,14 @@
import static org.junit.Assert.assertNotEquals;

import io.grpc.ServerBuilder;
import io.grpc.ServerCredentials;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.InternalNettyServerBuilder;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.netty.NettySslContextServerCredentials;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import java.io.IOException;
Expand All @@ -45,15 +47,16 @@ public class Http2NettyTest extends AbstractInteropTest {
protected ServerBuilder<?> getServerBuilder() {
// Starts the server with HTTPS.
try {
NettyServerBuilder builder = NettyServerBuilder.forPort(0)
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.sslContext(GrpcSslContexts
ServerCredentials serverCreds = NettySslContextServerCredentials.create(
GrpcSslContexts
.forServer(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"))
.clientAuth(ClientAuth.REQUIRE)
.trustManager(TestUtils.loadCert("ca.pem"))
.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE)
.build());
NettyServerBuilder builder = NettyServerBuilder.forPort(0, serverCreds)
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
// Disable the default census stats tracer, use testing tracer instead.
InternalNettyServerBuilder.setStatsEnabled(builder, false);
return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
Expand Down
Expand Up @@ -24,12 +24,14 @@
import com.squareup.okhttp.ConnectionSpec;
import io.grpc.ManagedChannel;
import io.grpc.ServerBuilder;
import io.grpc.ServerCredentials;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.testing.StreamRecorder;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.InternalNettyServerBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.netty.NettySslContextServerCredentials;
import io.grpc.okhttp.InternalOkHttpChannelBuilder;
import io.grpc.okhttp.OkHttpChannelBuilder;
import io.grpc.okhttp.internal.Platform;
Expand Down Expand Up @@ -78,10 +80,11 @@ protected ServerBuilder<?> getServerBuilder() {
.forServer(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"));
GrpcSslContexts.configure(contextBuilder, sslProvider);
contextBuilder.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE);
NettyServerBuilder builder = NettyServerBuilder.forPort(0)
ServerCredentials serverCreds =
NettySslContextServerCredentials.create(contextBuilder.build());
NettyServerBuilder builder = NettyServerBuilder.forPort(0, serverCreds)
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.sslContext(contextBuilder.build());
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
// Disable the default census stats tracer, use testing tracer instead.
InternalNettyServerBuilder.setStatsEnabled(builder, false);
return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
Expand Down
Expand Up @@ -29,6 +29,7 @@
import io.grpc.DecompressorRegistry;
import io.grpc.ForwardingClientCall;
import io.grpc.ForwardingClientCallListener;
import io.grpc.InsecureServerCredentials;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.ServerBuilder;
Expand Down Expand Up @@ -85,7 +86,7 @@ public static void registerCompressors() {

@Override
protected ServerBuilder<?> getServerBuilder() {
NettyServerBuilder builder = NettyServerBuilder.forPort(0)
NettyServerBuilder builder = NettyServerBuilder.forPort(0, InsecureServerCredentials.create())
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.compressorRegistry(compressors)
.decompressorRegistry(decompressors)
Expand Down

0 comments on commit c0137cc

Please sign in to comment.