use of io.grpc.ServerCredentials in project grpc-java by grpc.
the class AdvancedTlsTest method trustManagerInsecurelySkipAllTest.
@Test
public void trustManagerInsecurelySkipAllTest() throws Exception {
AdvancedTlsX509KeyManager serverKeyManager = new AdvancedTlsX509KeyManager();
// Even if we provide bad credentials for the server, the test should still pass, because we
// will configure the client to skip all checks later.
serverKeyManager.updateIdentityCredentials(serverKeyBad, serverCertBad);
AdvancedTlsX509TrustManager serverTrustManager = AdvancedTlsX509TrustManager.newBuilder().setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION).setSslSocketAndEnginePeerVerifier(new SslSocketAndEnginePeerVerifier() {
@Override
public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authType, Socket socket) throws CertificateException {
}
@Override
public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authType, SSLEngine engine) throws CertificateException {
}
}).build();
serverTrustManager.updateTrustCredentials(caCert);
ServerCredentials serverCredentials = TlsServerCredentials.newBuilder().keyManager(serverKeyManager).trustManager(serverTrustManager).clientAuth(ClientAuth.REQUIRE).build();
server = Grpc.newServerBuilderForPort(0, serverCredentials).addService(new SimpleServiceImpl()).build().start();
AdvancedTlsX509KeyManager clientKeyManager = new AdvancedTlsX509KeyManager();
clientKeyManager.updateIdentityCredentials(clientKey0, clientCert0);
// Set the client to skip all checks, including traditional certificate verification.
// Note this is very dangerous in production environment - only do so if you are confident on
// what you are doing!
AdvancedTlsX509TrustManager clientTrustManager = AdvancedTlsX509TrustManager.newBuilder().setVerification(Verification.INSECURELY_SKIP_ALL_VERIFICATION).setSslSocketAndEnginePeerVerifier(new SslSocketAndEnginePeerVerifier() {
@Override
public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authType, Socket socket) throws CertificateException {
}
@Override
public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authType, SSLEngine engine) throws CertificateException {
}
}).build();
clientTrustManager.updateTrustCredentials(caCert);
ChannelCredentials channelCredentials = TlsChannelCredentials.newBuilder().keyManager(clientKeyManager).trustManager(clientTrustManager).build();
channel = Grpc.newChannelBuilderForAddress("localhost", server.getPort(), channelCredentials).build();
// Start the connection.
try {
SimpleServiceGrpc.SimpleServiceBlockingStub client = SimpleServiceGrpc.newBlockingStub(channel);
client.unaryRpc(SimpleRequest.getDefaultInstance());
} catch (StatusRuntimeException e) {
fail("Failed to make a connection");
e.printStackTrace();
}
}
use of io.grpc.ServerCredentials in project grpc-java by grpc.
the class AdvancedTlsTest method onFileReloadingKeyManagerTrustManagerTest.
@Test
public void onFileReloadingKeyManagerTrustManagerTest() throws Exception {
// Create & start a server.
AdvancedTlsX509KeyManager serverKeyManager = new AdvancedTlsX509KeyManager();
Closeable serverKeyShutdown = serverKeyManager.updateIdentityCredentialsFromFile(serverKey0File, serverCert0File, 100, TimeUnit.MILLISECONDS, executor);
AdvancedTlsX509TrustManager serverTrustManager = AdvancedTlsX509TrustManager.newBuilder().setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION).build();
Closeable serverTrustShutdown = serverTrustManager.updateTrustCredentialsFromFile(caCertFile, 100, TimeUnit.MILLISECONDS, executor);
ServerCredentials serverCredentials = TlsServerCredentials.newBuilder().keyManager(serverKeyManager).trustManager(serverTrustManager).clientAuth(ClientAuth.REQUIRE).build();
server = Grpc.newServerBuilderForPort(0, serverCredentials).addService(new SimpleServiceImpl()).build().start();
// Create a client to connect.
AdvancedTlsX509KeyManager clientKeyManager = new AdvancedTlsX509KeyManager();
Closeable clientKeyShutdown = clientKeyManager.updateIdentityCredentialsFromFile(clientKey0File, clientCert0File, 100, TimeUnit.MILLISECONDS, executor);
AdvancedTlsX509TrustManager clientTrustManager = AdvancedTlsX509TrustManager.newBuilder().setVerification(Verification.CERTIFICATE_AND_HOST_NAME_VERIFICATION).build();
Closeable clientTrustShutdown = clientTrustManager.updateTrustCredentialsFromFile(caCertFile, 100, TimeUnit.MILLISECONDS, executor);
ChannelCredentials channelCredentials = TlsChannelCredentials.newBuilder().keyManager(clientKeyManager).trustManager(clientTrustManager).build();
channel = Grpc.newChannelBuilderForAddress("localhost", server.getPort(), channelCredentials).overrideAuthority("foo.test.google.com.au").build();
// Start the connection.
try {
SimpleServiceGrpc.SimpleServiceBlockingStub client = SimpleServiceGrpc.newBlockingStub(channel);
// Send an actual request, via the full GRPC & network stack, and check that a proper
// response comes back.
client.unaryRpc(SimpleRequest.getDefaultInstance());
} catch (StatusRuntimeException e) {
e.printStackTrace();
fail("Find error: " + e.getMessage());
}
// Clean up.
serverKeyShutdown.close();
serverTrustShutdown.close();
clientKeyShutdown.close();
clientTrustShutdown.close();
}
use of io.grpc.ServerCredentials in project grpc-java by grpc.
the class AltsHandshakerTest method startAltsServer.
private void startAltsServer() throws Exception {
ServerCredentials serverCredentials = AltsServerCredentials.newBuilder().enableUntrustedAltsForTesting().setHandshakerAddressForTesting("localhost:" + handshakerServer.getPort()).build();
testServer = grpcCleanup.register(Grpc.newServerBuilderForPort(0, serverCredentials).addService(new TestServiceGrpc.TestServiceImplBase() {
@Override
public void unaryCall(SimpleRequest request, StreamObserver<SimpleResponse> so) {
so.onNext(SimpleResponse.getDefaultInstance());
so.onCompleted();
}
}).build()).start();
}
use of io.grpc.ServerCredentials in project grpc-java by grpc.
the class Http2OkHttpTest method getServerBuilder.
@Override
protected ServerBuilder<?> getServerBuilder() {
// Starts the server with HTTPS.
try {
ServerCredentials serverCreds = TlsServerCredentials.create(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"));
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());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of io.grpc.ServerCredentials in project grpc-java by grpc.
the class ShadingTest method tcnative.
@Test
public void tcnative() throws Exception {
ServerCredentials serverCreds = TlsServerCredentials.create(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"));
server = Grpc.newServerBuilderForPort(0, serverCreds).addService(new SimpleServiceImpl()).build().start();
ChannelCredentials creds = NettySslContextChannelCredentials.create(GrpcSslContexts.configure(SslContextBuilder.forClient(), SslProvider.OPENSSL).trustManager(TestUtils.loadCert("ca.pem")).build());
channel = Grpc.newChannelBuilder("localhost:" + server.getPort(), creds).overrideAuthority("foo.test.google.fr").build();
SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
assertThat(SimpleResponse.getDefaultInstance()).isEqualTo(stub.unaryRpc(SimpleRequest.getDefaultInstance()));
}
Aggregations