2020import static io .grpc .s2a .internal .handshaker .TLSVersion .TLS_VERSION_1_3 ;
2121
2222import com .google .common .collect .ImmutableMap ;
23+ import com .google .common .io .CharStreams ;
2324import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2425import com .google .protobuf .ByteString ;
2526import io .grpc .stub .StreamObserver ;
2627import io .grpc .util .CertificateUtils ;
27- import java .io .File ;
28- import java .io .FileInputStream ;
2928import java .io .FileNotFoundException ;
3029import java .io .IOException ;
30+ import java .io .InputStream ;
31+ import java .io .InputStreamReader ;
3132import java .io .UnsupportedEncodingException ;
3233import java .nio .charset .StandardCharsets ;
33- import java .nio .file .Files ;
3434import java .security .NoSuchAlgorithmException ;
3535import java .security .PrivateKey ;
3636import java .security .Signature ;
@@ -54,14 +54,7 @@ enum VerificationResult {
5454 FAILURE
5555 }
5656
57- public static final File leafCertFile =
58- new File ("src/test/resources/leaf_cert_ec.pem" );
59- public static final File cert2File =
60- new File ("src/test/resources/int_cert2_ec.pem" );
61- public static final File cert1File =
62- new File ("src/test/resources/int_cert1_ec.pem" );
63- public static final File keyFile =
64- new File ("src/test/resources/leaf_key_ec.pem" );
57+ private static final ClassLoader classLoader = FakeWriter .class .getClassLoader ();
6558 private static final ImmutableMap <SignatureAlgorithm , String >
6659 ALGORITHM_TO_SIGNATURE_INSTANCE_IDENTIFIER =
6760 ImmutableMap .of (
@@ -79,6 +72,10 @@ enum VerificationResult {
7972 private String failureReason ;
8073 private PrivateKey privateKey ;
8174
75+ public static String convertInputStreamToString (InputStream is ) throws IOException {
76+ return CharStreams .toString (new InputStreamReader (is , StandardCharsets .UTF_8 ));
77+ }
78+
8279 @ CanIgnoreReturnValue
8380 FakeWriter setReader (StreamObserver <SessionResp > reader ) {
8481 this .reader = reader ;
@@ -106,11 +103,10 @@ FakeWriter setFailureReason(String failureReason) {
106103 @ CanIgnoreReturnValue
107104 FakeWriter initializePrivateKey () throws InvalidKeySpecException , NoSuchAlgorithmException ,
108105 IOException , FileNotFoundException , UnsupportedEncodingException {
109- FileInputStream keyInputStream = new FileInputStream (keyFile );
110- try {
106+ try (
107+ InputStream keyInputStream = classLoader .getResourceAsStream ("leaf_key_ec.pem" );
108+ ) {
111109 privateKey = CertificateUtils .getPrivateKey (keyInputStream );
112- } finally {
113- keyInputStream .close ();
114110 }
115111 return this ;
116112 }
@@ -130,32 +126,39 @@ void sendIoError() {
130126 }
131127
132128 void sendGetTlsConfigResp () {
133- try {
134- reader .onNext (
135- SessionResp .newBuilder ()
136- .setGetTlsConfigurationResp (
137- GetTlsConfigurationResp .newBuilder ()
138- .setClientTlsConfiguration (
139- GetTlsConfigurationResp .ClientTlsConfiguration .newBuilder ()
140- .addCertificateChain (new String (Files .readAllBytes (
141- FakeWriter .leafCertFile .toPath ()), StandardCharsets .UTF_8 ))
142- .addCertificateChain (new String (Files .readAllBytes (
143- FakeWriter .cert1File .toPath ()), StandardCharsets .UTF_8 ))
144- .addCertificateChain (new String (Files .readAllBytes (
145- FakeWriter .cert2File .toPath ()), StandardCharsets .UTF_8 ))
146- .setMinTlsVersion (TLS_VERSION_1_3 )
147- .setMaxTlsVersion (TLS_VERSION_1_3 )
148- .addCiphersuites (
149- Ciphersuite .CIPHERSUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 )
150- .addCiphersuites (
151- Ciphersuite .CIPHERSUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 )
152- .addCiphersuites (
153- Ciphersuite
154- .CIPHERSUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 )))
155- .build ());
129+ String leafCertString = "" ;
130+ String cert2String = "" ;
131+ String cert1String = "" ;
132+ try (
133+ InputStream leafCert = classLoader .getResourceAsStream ("leaf_cert_ec.pem" );
134+ InputStream cert2 = classLoader .getResourceAsStream ("int_cert2_ec.pem" );
135+ InputStream cert1 = classLoader .getResourceAsStream ("int_cert1_ec.pem" );
136+ ) {
137+ leafCertString = FakeWriter .convertInputStreamToString (leafCert );
138+ cert2String = FakeWriter .convertInputStreamToString (cert2 );
139+ cert1String = FakeWriter .convertInputStreamToString (cert1 );
156140 } catch (IOException e ) {
157141 reader .onError (e );
158142 }
143+ reader .onNext (
144+ SessionResp .newBuilder ()
145+ .setGetTlsConfigurationResp (
146+ GetTlsConfigurationResp .newBuilder ()
147+ .setClientTlsConfiguration (
148+ GetTlsConfigurationResp .ClientTlsConfiguration .newBuilder ()
149+ .addCertificateChain (leafCertString )
150+ .addCertificateChain (cert1String )
151+ .addCertificateChain (cert2String )
152+ .setMinTlsVersion (TLS_VERSION_1_3 )
153+ .setMaxTlsVersion (TLS_VERSION_1_3 )
154+ .addCiphersuites (
155+ Ciphersuite .CIPHERSUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 )
156+ .addCiphersuites (
157+ Ciphersuite .CIPHERSUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 )
158+ .addCiphersuites (
159+ Ciphersuite
160+ .CIPHERSUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 )))
161+ .build ());
159162 }
160163
161164 boolean isFakeWriterClosed () {
@@ -191,25 +194,32 @@ public void onNext(SessionReq sessionReq) {
191194 reader .onCompleted ();
192195 break ;
193196 case BAD_TLS_VERSION_RESPONSE :
194- try {
195- reader .onNext (
196- SessionResp .newBuilder ()
197- .setGetTlsConfigurationResp (
198- GetTlsConfigurationResp .newBuilder ()
199- .setClientTlsConfiguration (
200- GetTlsConfigurationResp .ClientTlsConfiguration .newBuilder ()
201- .addCertificateChain (new String (Files .readAllBytes (
202- FakeWriter .leafCertFile .toPath ()), StandardCharsets .UTF_8 ))
203- .addCertificateChain (new String (Files .readAllBytes (
204- FakeWriter .cert1File .toPath ()), StandardCharsets .UTF_8 ))
205- .addCertificateChain (new String (Files .readAllBytes (
206- FakeWriter .cert2File .toPath ()), StandardCharsets .UTF_8 ))
207- .setMinTlsVersion (TLS_VERSION_1_3 )
208- .setMaxTlsVersion (TLS_VERSION_1_2 )))
209- .build ());
197+ String leafCertString = "" ;
198+ String cert2String = "" ;
199+ String cert1String = "" ;
200+ try (
201+ InputStream leafCert = classLoader .getResourceAsStream ("leaf_cert_ec.pem" );
202+ InputStream cert2 = classLoader .getResourceAsStream ("int_cert2_ec.pem" );
203+ InputStream cert1 = classLoader .getResourceAsStream ("int_cert1_ec.pem" );
204+ ) {
205+ leafCertString = FakeWriter .convertInputStreamToString (leafCert );
206+ cert2String = FakeWriter .convertInputStreamToString (cert2 );
207+ cert1String = FakeWriter .convertInputStreamToString (cert1 );
210208 } catch (IOException e ) {
211209 reader .onError (e );
212210 }
211+ reader .onNext (
212+ SessionResp .newBuilder ()
213+ .setGetTlsConfigurationResp (
214+ GetTlsConfigurationResp .newBuilder ()
215+ .setClientTlsConfiguration (
216+ GetTlsConfigurationResp .ClientTlsConfiguration .newBuilder ()
217+ .addCertificateChain (leafCertString )
218+ .addCertificateChain (cert1String )
219+ .addCertificateChain (cert2String )
220+ .setMinTlsVersion (TLS_VERSION_1_3 )
221+ .setMaxTlsVersion (TLS_VERSION_1_2 )))
222+ .build ());
213223 break ;
214224 default :
215225 try {
@@ -249,17 +259,28 @@ private SessionResp handleGetTlsConfigurationReq(GetTlsConfigurationReq req)
249259 .setDetails ("No TLS configuration for the server side." ))
250260 .build ();
251261 }
262+ String leafCertString = "" ;
263+ String cert2String = "" ;
264+ String cert1String = "" ;
265+ try (
266+ InputStream leafCert = classLoader .getResourceAsStream ("leaf_cert_ec.pem" );
267+ InputStream cert2 = classLoader .getResourceAsStream ("int_cert2_ec.pem" );
268+ InputStream cert1 = classLoader .getResourceAsStream ("int_cert1_ec.pem" );
269+ ) {
270+ leafCertString = FakeWriter .convertInputStreamToString (leafCert );
271+ cert2String = FakeWriter .convertInputStreamToString (cert2 );
272+ cert1String = FakeWriter .convertInputStreamToString (cert1 );
273+ } catch (IOException e ) {
274+ reader .onError (e );
275+ }
252276 return SessionResp .newBuilder ()
253277 .setGetTlsConfigurationResp (
254278 GetTlsConfigurationResp .newBuilder ()
255279 .setClientTlsConfiguration (
256280 GetTlsConfigurationResp .ClientTlsConfiguration .newBuilder ()
257- .addCertificateChain (new String (Files .readAllBytes (
258- FakeWriter .leafCertFile .toPath ()), StandardCharsets .UTF_8 ))
259- .addCertificateChain (new String (Files .readAllBytes (
260- FakeWriter .cert1File .toPath ()), StandardCharsets .UTF_8 ))
261- .addCertificateChain (new String (Files .readAllBytes (
262- FakeWriter .cert2File .toPath ()), StandardCharsets .UTF_8 ))
281+ .addCertificateChain (leafCertString )
282+ .addCertificateChain (cert1String )
283+ .addCertificateChain (cert2String )
263284 .setMinTlsVersion (TLS_VERSION_1_3 )
264285 .setMaxTlsVersion (TLS_VERSION_1_3 )
265286 .addCiphersuites (
0 commit comments