Skip to content

Commit 55cef63

Browse files
authored
s2a: Load resources from classpath instead of from disk
1 parent 229a010 commit 55cef63

File tree

7 files changed

+156
-117
lines changed

7 files changed

+156
-117
lines changed

s2a/src/test/java/io/grpc/s2a/S2AChannelCredentialsTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import io.grpc.ChannelCredentials;
2323
import io.grpc.InsecureChannelCredentials;
2424
import io.grpc.TlsChannelCredentials;
25-
import java.io.File;
25+
import java.io.InputStream;
2626
import org.junit.Test;
2727
import org.junit.runner.RunWith;
2828
import org.junit.runners.JUnit4;
@@ -124,15 +124,13 @@ public void build_withUseMtlsToS2AWithLocalUid_success() throws Exception {
124124
}
125125

126126
private static ChannelCredentials getTlsChannelCredentials() throws Exception {
127-
String privateKeyPath = "src/test/resources/client_key.pem";
128-
String certChainPath = "src/test/resources/client_cert.pem";
129-
String trustBundlePath = "src/test/resources/root_cert.pem";
130-
File privateKeyFile = new File(privateKeyPath);
131-
File certChainFile = new File(certChainPath);
132-
File trustBundleFile = new File(trustBundlePath);
127+
ClassLoader classLoader = S2AChannelCredentialsTest.class.getClassLoader();
128+
InputStream privateKey = classLoader.getResourceAsStream("client_key.pem");
129+
InputStream certChain = classLoader.getResourceAsStream("client_cert.pem");
130+
InputStream trustBundle = classLoader.getResourceAsStream("root_cert.pem");
133131
return TlsChannelCredentials.newBuilder()
134-
.keyManager(certChainFile, privateKeyFile)
135-
.trustManager(trustBundleFile)
132+
.keyManager(certChain, privateKey)
133+
.trustManager(trustBundle)
136134
.build();
137135
}
138136
}

s2a/src/test/java/io/grpc/s2a/internal/channel/S2AHandshakerServiceChannelTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import io.grpc.testing.protobuf.SimpleRequest;
3838
import io.grpc.testing.protobuf.SimpleResponse;
3939
import io.grpc.testing.protobuf.SimpleServiceGrpc;
40-
import java.io.File;
40+
import java.io.InputStream;
4141
import org.junit.Before;
4242
import org.junit.ClassRule;
4343
import org.junit.Test;
@@ -218,9 +218,10 @@ public void create_mtlsSucceedsAfterCloseIsCalledOnce() throws Exception {
218218

219219
private static Server createMtlsServer() throws Exception {
220220
SimpleServiceImpl service = new SimpleServiceImpl();
221-
File serverCert = new File("src/test/resources/server_cert.pem");
222-
File serverKey = new File("src/test/resources/server_key.pem");
223-
File rootCert = new File("src/test/resources/root_cert.pem");
221+
ClassLoader classLoader = S2AHandshakerServiceChannelTest.class.getClassLoader();
222+
InputStream serverCert = classLoader.getResourceAsStream("server_cert.pem");
223+
InputStream serverKey = classLoader.getResourceAsStream("server_key.pem");
224+
InputStream rootCert = classLoader.getResourceAsStream("root_cert.pem");
224225
ServerCredentials creds =
225226
TlsServerCredentials.newBuilder()
226227
.keyManager(serverCert, serverKey)
@@ -238,9 +239,10 @@ private static Server createPlaintextServer() {
238239
}
239240

240241
private static ChannelCredentials getTlsChannelCredentials() throws Exception {
241-
File clientCert = new File("src/test/resources/client_cert.pem");
242-
File clientKey = new File("src/test/resources/client_key.pem");
243-
File rootCert = new File("src/test/resources/root_cert.pem");
242+
ClassLoader classLoader = S2AHandshakerServiceChannelTest.class.getClassLoader();
243+
InputStream clientCert = classLoader.getResourceAsStream("client_cert.pem");
244+
InputStream clientKey = classLoader.getResourceAsStream("client_key.pem");
245+
InputStream rootCert = classLoader.getResourceAsStream("root_cert.pem");
244246
return TlsChannelCredentials.newBuilder()
245247
.keyManager(clientCert, clientKey)
246248
.trustManager(rootCert)

s2a/src/test/java/io/grpc/s2a/internal/handshaker/FakeS2AServerTest.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import io.grpc.s2a.internal.handshaker.ValidatePeerCertificateChainReq.VerificationMode;
3131
import io.grpc.stub.StreamObserver;
3232
import java.io.IOException;
33+
import java.io.InputStream;
3334
import java.nio.charset.StandardCharsets;
34-
import java.nio.file.Files;
3535
import java.util.concurrent.CountDownLatch;
3636
import java.util.concurrent.ExecutorService;
3737
import java.util.concurrent.Executors;
@@ -118,18 +118,29 @@ public void onCompleted() {
118118
executor.awaitTermination(1, SECONDS);
119119
}
120120

121+
String leafCertString = "";
122+
String cert2String = "";
123+
String cert1String = "";
124+
ClassLoader classLoader = FakeS2AServerTest.class.getClassLoader();
125+
try (
126+
InputStream leafCert = classLoader.getResourceAsStream("leaf_cert_ec.pem");
127+
InputStream cert2 = classLoader.getResourceAsStream("int_cert2_ec.pem");
128+
InputStream cert1 = classLoader.getResourceAsStream("int_cert1_ec.pem");
129+
) {
130+
leafCertString = FakeWriter.convertInputStreamToString(leafCert);
131+
cert2String = FakeWriter.convertInputStreamToString(cert2);
132+
cert1String = FakeWriter.convertInputStreamToString(cert1);
133+
}
134+
121135
SessionResp expected =
122136
SessionResp.newBuilder()
123137
.setGetTlsConfigurationResp(
124138
GetTlsConfigurationResp.newBuilder()
125139
.setClientTlsConfiguration(
126140
GetTlsConfigurationResp.ClientTlsConfiguration.newBuilder()
127-
.addCertificateChain(new String(Files.readAllBytes(
128-
FakeWriter.leafCertFile.toPath()), StandardCharsets.UTF_8))
129-
.addCertificateChain(new String(Files.readAllBytes(
130-
FakeWriter.cert1File.toPath()), StandardCharsets.UTF_8))
131-
.addCertificateChain(new String(Files.readAllBytes(
132-
FakeWriter.cert2File.toPath()), StandardCharsets.UTF_8))
141+
.addCertificateChain(leafCertString)
142+
.addCertificateChain(cert1String)
143+
.addCertificateChain(cert2String)
133144
.setMinTlsVersion(TLSVersion.TLS_VERSION_1_3)
134145
.setMaxTlsVersion(TLSVersion.TLS_VERSION_1_3)
135146
.addCiphersuites(

s2a/src/test/java/io/grpc/s2a/internal/handshaker/FakeWriter.java

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
import static io.grpc.s2a.internal.handshaker.TLSVersion.TLS_VERSION_1_3;
2121

2222
import com.google.common.collect.ImmutableMap;
23+
import com.google.common.io.CharStreams;
2324
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2425
import com.google.protobuf.ByteString;
2526
import io.grpc.stub.StreamObserver;
2627
import io.grpc.util.CertificateUtils;
27-
import java.io.File;
28-
import java.io.FileInputStream;
2928
import java.io.FileNotFoundException;
3029
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.io.InputStreamReader;
3132
import java.io.UnsupportedEncodingException;
3233
import java.nio.charset.StandardCharsets;
33-
import java.nio.file.Files;
3434
import java.security.NoSuchAlgorithmException;
3535
import java.security.PrivateKey;
3636
import 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

Comments
 (0)