16
16
17
17
package io .grpc .testing .integration ;
18
18
19
+ import static io .grpc .internal .testing .TestUtils .loadCert ;
19
20
import static org .junit .Assert .assertEquals ;
20
21
import static org .junit .Assert .fail ;
21
22
37
38
import io .netty .handler .ssl .SslContext ;
38
39
import io .netty .handler .ssl .SslContextBuilder ;
39
40
import io .netty .handler .ssl .SslProvider ;
40
- import java .io .File ;
41
41
import java .io .IOException ;
42
+ import java .io .InputStream ;
42
43
import java .security .NoSuchAlgorithmException ;
43
44
import java .security .cert .X509Certificate ;
44
45
import java .util .Arrays ;
@@ -118,24 +119,21 @@ public void tearDown() {
118
119
@ Test
119
120
public void basicClientServerIntegrationTest () throws Exception {
120
121
// Create & start a server.
121
- File serverCertFile = TestUtils .loadCert ("server1.pem" );
122
- File serverPrivateKeyFile = TestUtils .loadCert ("server1.key" );
123
122
X509Certificate [] serverTrustedCaCerts = {
124
123
TestUtils .loadX509Cert ("ca.pem" )
125
124
};
126
- server = serverBuilder (0 , serverCertFile , serverPrivateKeyFile , serverTrustedCaCerts )
125
+ server = serverBuilder (
126
+ 0 , loadCert ("server1.pem" ), loadCert ("server1.key" ), serverTrustedCaCerts )
127
127
.addService (new TestServiceImpl (executor ))
128
128
.build ()
129
129
.start ();
130
130
131
131
// Create a client.
132
- File clientCertChainFile = TestUtils .loadCert ("client.pem" );
133
- File clientPrivateKeyFile = TestUtils .loadCert ("client.key" );
134
132
X509Certificate [] clientTrustedCaCerts = {
135
133
TestUtils .loadX509Cert ("ca.pem" )
136
134
};
137
135
channel = clientChannel (server .getPort (), clientContextBuilder
138
- .keyManager (clientCertChainFile , clientPrivateKeyFile )
136
+ .keyManager (loadCert ( "client.pem" ), loadCert ( "client.key" ) )
139
137
.trustManager (clientTrustedCaCerts )
140
138
.build ());
141
139
TestServiceGrpc .TestServiceBlockingStub client = TestServiceGrpc .newBlockingStub (channel );
@@ -154,26 +152,23 @@ public void basicClientServerIntegrationTest() throws Exception {
154
152
@ Test
155
153
public void serverRejectsUntrustedClientCert () throws Exception {
156
154
// Create & start a server. It requires client authentication and trusts only the test CA.
157
- File serverCertFile = TestUtils .loadCert ("server1.pem" );
158
- File serverPrivateKeyFile = TestUtils .loadCert ("server1.key" );
159
155
X509Certificate [] serverTrustedCaCerts = {
160
156
TestUtils .loadX509Cert ("ca.pem" )
161
157
};
162
- server = serverBuilder (0 , serverCertFile , serverPrivateKeyFile , serverTrustedCaCerts )
158
+ server = serverBuilder (
159
+ 0 , loadCert ("server1.pem" ), loadCert ("server1.key" ), serverTrustedCaCerts )
163
160
.addService (new TestServiceImpl (executor ))
164
161
.build ()
165
162
.start ();
166
163
167
164
// Create a client. Its credentials come from a CA that the server does not trust. The client
168
165
// trusts both test CAs, so we can be sure that the handshake failure is due to the server
169
166
// rejecting the client's cert, not the client rejecting the server's cert.
170
- File clientCertChainFile = TestUtils .loadCert ("badclient.pem" );
171
- File clientPrivateKeyFile = TestUtils .loadCert ("badclient.key" );
172
167
X509Certificate [] clientTrustedCaCerts = {
173
168
TestUtils .loadX509Cert ("ca.pem" )
174
169
};
175
170
channel = clientChannel (server .getPort (), clientContextBuilder
176
- .keyManager (clientCertChainFile , clientPrivateKeyFile )
171
+ .keyManager (loadCert ( "badclient.pem" ), loadCert ( "badclient.key" ) )
177
172
.trustManager (clientTrustedCaCerts )
178
173
.build ());
179
174
TestServiceGrpc .TestServiceBlockingStub client = TestServiceGrpc .newBlockingStub (channel );
@@ -201,12 +196,11 @@ public void serverRejectsUntrustedClientCert() throws Exception {
201
196
@ Test
202
197
public void noClientAuthFailure () throws Exception {
203
198
// Create & start a server.
204
- File serverCertFile = TestUtils .loadCert ("server1.pem" );
205
- File serverPrivateKeyFile = TestUtils .loadCert ("server1.key" );
206
199
X509Certificate [] serverTrustedCaCerts = {
207
200
TestUtils .loadX509Cert ("ca.pem" )
208
201
};
209
- server = serverBuilder (0 , serverCertFile , serverPrivateKeyFile , serverTrustedCaCerts )
202
+ server = serverBuilder (
203
+ 0 , loadCert ("server1.pem" ), loadCert ("server1.key" ), serverTrustedCaCerts )
210
204
.addService (new TestServiceImpl (executor ))
211
205
.build ()
212
206
.start ();
@@ -243,26 +237,25 @@ public void noClientAuthFailure() throws Exception {
243
237
@ Test
244
238
public void clientRejectsUntrustedServerCert () throws Exception {
245
239
// Create & start a server.
246
- File serverCertFile = TestUtils .loadCert ("badserver.pem" );
247
- File serverPrivateKeyFile = TestUtils .loadCert ("badserver.key" );
248
240
X509Certificate [] serverTrustedCaCerts = {
249
241
TestUtils .loadX509Cert ("ca.pem" )
250
242
};
251
- server = serverBuilder (0 , serverCertFile , serverPrivateKeyFile , serverTrustedCaCerts )
243
+ server = serverBuilder (
244
+ 0 , loadCert ("badserver.pem" ), loadCert ("badserver.key" ), serverTrustedCaCerts )
252
245
.addService (new TestServiceImpl (executor ))
253
246
.build ()
254
247
.start ();
255
248
256
249
// Create a client.
257
- File clientCertChainFile = TestUtils .loadCert ("client.pem" );
258
- File clientPrivateKeyFile = TestUtils .loadCert ("client.key" );
259
250
X509Certificate [] clientTrustedCaCerts = {
260
251
TestUtils .loadX509Cert ("ca.pem" )
261
252
};
262
- channel = clientChannel (server .getPort (), clientContextBuilder
263
- .keyManager (clientCertChainFile , clientPrivateKeyFile )
264
- .trustManager (clientTrustedCaCerts )
265
- .build ());
253
+ channel =
254
+ clientChannel (
255
+ server .getPort (),
256
+ clientContextBuilder .keyManager (loadCert ("client.pem" ), loadCert ("client.key" ))
257
+ .trustManager (clientTrustedCaCerts )
258
+ .build ());
266
259
TestServiceGrpc .TestServiceBlockingStub client = TestServiceGrpc .newBlockingStub (channel );
267
260
268
261
// Check that the TLS handshake fails.
@@ -282,10 +275,10 @@ public void clientRejectsUntrustedServerCert() throws Exception {
282
275
}
283
276
284
277
285
- private ServerBuilder <?> serverBuilder (int port , File serverCertChainFile ,
286
- File serverPrivateKeyFile , X509Certificate [] serverTrustedCaCerts ) throws IOException {
278
+ private ServerBuilder <?> serverBuilder (int port , InputStream serverCertChain ,
279
+ InputStream serverPrivateKey , X509Certificate [] serverTrustedCaCerts ) throws IOException {
287
280
SslContextBuilder sslContextBuilder
288
- = SslContextBuilder .forServer (serverCertChainFile , serverPrivateKeyFile );
281
+ = SslContextBuilder .forServer (serverCertChain , serverPrivateKey );
289
282
GrpcSslContexts .configure (sslContextBuilder , sslProvider );
290
283
sslContextBuilder .trustManager (serverTrustedCaCerts )
291
284
.clientAuth (ClientAuth .REQUIRE );
0 commit comments