-
Notifications
You must be signed in to change notification settings - Fork 4k
Add GRPC + TLS integration tests (notably, test important security properties) #1327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
07a0a93
Add some initial integration tests for GRPC's TLS support.
matthild 7525b99
Check in generated code.
matthild 4b2115f
Use existing server1.pem cert instead of creating localhost_server.pem.
matthild 1aac04b
Remove unnecessary code to make client trust its own cert.
matthild e9b368e
Use TestService.emptyCall instead of introducing a new EchoService.
matthild 057297d
Run integration tests without Netty tcnative.
matthild File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
256 changes: 256 additions & 0 deletions
256
interop-testing/src/test/java/io/grpc/testing/integration/TlsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| /* | ||
| * Copyright 2015, Google Inc. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * | ||
| * * Neither the name of Google Inc. nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package io.grpc.testing.integration; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import com.google.common.util.concurrent.MoreExecutors; | ||
| import com.google.protobuf.EmptyProtos.Empty; | ||
|
|
||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.Server; | ||
| import io.grpc.ServerBuilder; | ||
| import io.grpc.Status; | ||
| import io.grpc.StatusRuntimeException; | ||
| import io.grpc.netty.GrpcSslContexts; | ||
| import io.grpc.netty.NegotiationType; | ||
| import io.grpc.netty.NettyChannelBuilder; | ||
| import io.grpc.netty.NettyServerBuilder; | ||
| import io.grpc.testing.TestUtils; | ||
| import io.netty.handler.ssl.ClientAuth; | ||
| import io.netty.handler.ssl.SslContext; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Ignore; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
|
|
||
| /** | ||
| * Integration tests for GRPC's TLS support. | ||
| */ | ||
| // TODO: Use @RunWith(Parameterized.class) to run these tests for all TLS providers, probably via | ||
| // GrpcSslContexts.configure(SslContextBuilder, SslProvider). | ||
| @RunWith(JUnit4.class) | ||
| public class TlsTest { | ||
| @Before | ||
| public void setUp() { | ||
| executor = Executors.newSingleThreadScheduledExecutor(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| MoreExecutors.shutdownAndAwaitTermination(executor, 5, TimeUnit.SECONDS); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Tests that a client and a server configured using GrpcSslContexts can successfully | ||
| * communicate with each other. | ||
| */ | ||
| // TODO: Fix whatever causes this test to fail, then remove the @Ignore annotation. | ||
| @Ignore | ||
| @Test | ||
| public void basicClientServerIntegrationTest() throws Exception { | ||
| int port = TestUtils.pickUnusedPort(); | ||
|
|
||
| // Create & start a server. | ||
| File serverCertFile = TestUtils.loadCert("server1.pem"); | ||
| File serverPrivateKeyFile = TestUtils.loadCert("server1.key"); | ||
| X509Certificate[] serverTrustedCaCerts = { | ||
| TestUtils.loadX509Cert("ca.pem") | ||
| }; | ||
| Server server = serverBuilder(port, serverCertFile, serverPrivateKeyFile, serverTrustedCaCerts) | ||
| .addService(TestServiceGrpc.bindService(new TestServiceImpl(executor))) | ||
| .build() | ||
| .start(); | ||
|
|
||
| try { | ||
| // Create a client. | ||
| File clientCertFile = TestUtils.loadCert("client.pem"); | ||
| File clientPrivateKeyFile = TestUtils.loadCert("client.key"); | ||
| X509Certificate[] clientTrustedCaCerts = { | ||
| TestUtils.loadX509Cert("ca.pem") | ||
| }; | ||
| ManagedChannel channel = clientChannel("localhost", port, clientCertFile, | ||
| clientPrivateKeyFile, clientTrustedCaCerts); | ||
| TestServiceGrpc.TestServiceBlockingStub client = TestServiceGrpc.newBlockingStub(channel); | ||
|
|
||
| // Send an actual request, via the full GRPC & network stack, and check that a proper | ||
| // response comes back. | ||
| Empty request = Empty.getDefaultInstance(); | ||
| client.emptyCall(request); | ||
| } finally { | ||
| server.shutdown(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Tests that a server configured to require client authentication refuses to accept connections | ||
| * from a client that has an untrusted certificate. | ||
| */ | ||
| // TODO: Fix whatever causes this test to fail, then remove the @Ignore annotation. | ||
| @Ignore | ||
| @Test | ||
| public void serverRejectsUntrustedClientCert() throws Exception { | ||
| int port = TestUtils.pickUnusedPort(); | ||
|
|
||
| // Create & start a server. It requires client authentication and trusts only the test CA. | ||
| File serverCertFile = TestUtils.loadCert("server1.pem"); | ||
| File serverPrivateKeyFile = TestUtils.loadCert("server1.key"); | ||
| X509Certificate[] serverTrustedCaCerts = { | ||
| TestUtils.loadX509Cert("ca.pem") | ||
| }; | ||
| Server server = serverBuilder(port, serverCertFile, serverPrivateKeyFile, serverTrustedCaCerts) | ||
| .addService(TestServiceGrpc.bindService(new TestServiceImpl(executor))) | ||
| .build() | ||
| .start(); | ||
|
|
||
| try { | ||
| // Create a client. Its credentials come from a CA that the server does not trust. The client | ||
| // trusts both test CAs, so we can be sure that the handshake failure is due to the server | ||
| // rejecting the client's cert, not the client rejecting the server's cert. | ||
| File clientCertFile = TestUtils.loadCert("badclient.pem"); | ||
| File clientPrivateKeyFile = TestUtils.loadCert("badclient.key"); | ||
| X509Certificate[] clientTrustedCaCerts = { | ||
| TestUtils.loadX509Cert("ca.pem") | ||
| }; | ||
| ManagedChannel channel = clientChannel("localhost", port, clientCertFile, | ||
| clientPrivateKeyFile, clientTrustedCaCerts); | ||
| TestServiceGrpc.TestServiceBlockingStub client = TestServiceGrpc.newBlockingStub(channel); | ||
|
|
||
| // Check that the TLS handshake fails. | ||
| Empty request = Empty.getDefaultInstance(); | ||
| try { | ||
| client.emptyCall(request); | ||
| fail("TLS handshake should have failed, but didn't; received RPC response"); | ||
| } catch (StatusRuntimeException e) { | ||
| // GRPC reports this situation by throwing a StatusRuntimeException that wraps either a | ||
| // javax.net.ssl.SSLHandshakeException or a java.nio.channels.ClosedChannelException. | ||
| // Thus, reliably detecting the underlying cause is not feasible. | ||
| assertEquals(Status.Code.UNAVAILABLE, e.getStatus().getCode()); | ||
| } | ||
| } finally { | ||
| server.shutdown(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Tests that a server configured to require client authentication actually does require client | ||
| * authentication. | ||
| */ | ||
| @Test | ||
| public void noClientAuthFailure() throws Exception { | ||
| int port = TestUtils.pickUnusedPort(); | ||
|
|
||
| // Create & start a server. | ||
| File serverCertFile = TestUtils.loadCert("server1.pem"); | ||
| File serverPrivateKeyFile = TestUtils.loadCert("server1.key"); | ||
| X509Certificate[] serverTrustedCaCerts = { | ||
| TestUtils.loadX509Cert("ca.pem") | ||
| }; | ||
| Server server = serverBuilder(port, serverCertFile, serverPrivateKeyFile, serverTrustedCaCerts) | ||
| .addService(TestServiceGrpc.bindService(new TestServiceImpl(executor))) | ||
| .build() | ||
| .start(); | ||
|
|
||
| try { | ||
| // Create a client. It has no credentials. | ||
| ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", port) | ||
| .overrideAuthority(TestUtils.TEST_SERVER_HOST) | ||
| .negotiationType(NegotiationType.TLS) | ||
| .build(); | ||
| TestServiceGrpc.TestServiceBlockingStub client = TestServiceGrpc.newBlockingStub(channel); | ||
|
|
||
| // Check that the TLS handshake fails. | ||
| Empty request = Empty.getDefaultInstance(); | ||
| try { | ||
| client.emptyCall(request); | ||
| fail("TLS handshake should have failed, but didn't; received RPC response"); | ||
| } catch (StatusRuntimeException e) { | ||
| // GRPC reports this situation by throwing a StatusRuntimeException that wraps either a | ||
| // javax.net.ssl.SSLHandshakeException or a java.nio.channels.ClosedChannelException. | ||
| // Thus, reliably detecting the underlying cause is not feasible. | ||
| assertEquals(Status.Code.UNAVAILABLE, e.getStatus().getCode()); | ||
| } | ||
| } finally { | ||
| server.shutdown(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private static ServerBuilder<?> serverBuilder(int port, File serverCertChainFile, | ||
| File serverPrivateKeyFile, | ||
| X509Certificate[] serverTrustedCaCerts) | ||
| throws IOException { | ||
| SslContext sslContext = GrpcSslContexts.forServer(serverCertChainFile, serverPrivateKeyFile) | ||
| .trustManager(serverTrustedCaCerts) | ||
| .clientAuth(ClientAuth.REQUIRE) | ||
| .build(); | ||
|
|
||
| return NettyServerBuilder.forPort(port) | ||
| .sslContext(sslContext); | ||
| } | ||
|
|
||
|
|
||
| private static ManagedChannel clientChannel(String serverHost, int serverPort, | ||
| File clientCertChainFile, | ||
| File clientPrivateKeyFile, | ||
| X509Certificate[] clientTrustedCaCerts) | ||
| throws IOException { | ||
| SslContext sslContext = GrpcSslContexts.forClient() | ||
| .keyManager(clientCertChainFile, clientPrivateKeyFile) | ||
| .trustManager(clientTrustedCaCerts) | ||
| .build(); | ||
|
|
||
| return NettyChannelBuilder.forAddress(serverHost, serverPort) | ||
| .overrideAuthority(TestUtils.TEST_SERVER_HOST) | ||
| .negotiationType(NegotiationType.TLS) | ||
| .sslContext(sslContext) | ||
| .build(); | ||
| } | ||
|
|
||
|
|
||
| private ScheduledExecutorService executor; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just filed #1330 for what I expect to be the same problem as here.