Skip to content

Commit a42dcbd

Browse files
committed
pass File to TlsChannelCredentials.
1 parent 7d7b233 commit a42dcbd

File tree

2 files changed

+12
-31
lines changed

2 files changed

+12
-31
lines changed

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@
6464
import io.grpc.auth.MoreCallCredentials;
6565
import io.grpc.s2a.S2AChannelCredentials;
6666
import java.io.File;
67-
import java.io.FileInputStream;
68-
import java.io.FileNotFoundException;
6967
import java.io.IOException;
70-
import java.io.InputStream;
7168
import java.nio.charset.StandardCharsets;
7269
import java.security.GeneralSecurityException;
7370
import java.security.KeyStore;
@@ -452,7 +449,7 @@ ChannelCredentials createMtlsChannelCredentials() throws IOException, GeneralSec
452449
*/
453450
@VisibleForTesting
454451
ChannelCredentials createMtlsToS2AChannelCredentials(
455-
InputStream trustBundle, InputStream privateKey, InputStream certChain) throws IOException {
452+
File trustBundle, File privateKey, File certChain) throws IOException {
456453
if (trustBundle == null || privateKey == null || certChain == null) {
457454
return null;
458455
}
@@ -508,24 +505,9 @@ ChannelCredentials createS2ASecuredChannelCredentials() {
508505
if (!rootFile.isFile() || !certKeyFile.isFile()) {
509506
// Try to connect to S2A using mTLS.
510507
ChannelCredentials mtlsToS2AChannelCredentials = null;
511-
InputStream trustBundle = null;
512-
InputStream privateKey = null;
513-
InputStream certChain = null;
514-
try {
515-
trustBundle = new FileInputStream(MTLS_MDS_ROOT);
516-
privateKey = new FileInputStream(MTLS_MDS_CERT_CHAIN_AND_KEY);
517-
certChain = new FileInputStream(MTLS_MDS_CERT_CHAIN_AND_KEY);
518-
} catch (FileNotFoundException ignore) {
519-
// Fallback to plaintext-to-S2A connection on error.
520-
LOG.log(
521-
Level.WARNING,
522-
"Cannot establish an mTLS connection to S2A due to error loading MTLS to MDS credentials, falling back to plaintext connection to S2A: "
523-
+ ignore.getMessage());
524-
return createPlaintextToS2AChannelCredentials(plaintextAddress);
525-
}
526508
try {
527509
mtlsToS2AChannelCredentials =
528-
createMtlsToS2AChannelCredentials(trustBundle, privateKey, certChain);
510+
createMtlsToS2AChannelCredentials(rootFile, certKeyFile, certKeyFile);
529511
} catch (IOException ignore) {
530512
// Fallback to plaintext-to-S2A connection on error.
531513
LOG.log(

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
import io.grpc.ManagedChannelBuilder;
5959
import io.grpc.TlsChannelCredentials;
6060
import io.grpc.alts.ComputeEngineChannelBuilder;
61+
import java.io.File;
6162
import java.io.IOException;
62-
import java.io.InputStream;
6363
import java.security.GeneralSecurityException;
6464
import java.time.Duration;
6565
import java.util.ArrayList;
@@ -993,37 +993,36 @@ void createMtlsToS2AChannelCredentials_missingAllFiles_throws() throws IOExcepti
993993
void createMtlsToS2AChannelCredentials_missingRootFile_throws() throws IOException {
994994
InstantiatingGrpcChannelProvider provider =
995995
InstantiatingGrpcChannelProvider.newBuilder().build();
996-
InputStream privateKey = this.getClass().getClassLoader().getResourceAsStream("client_key.pem");
997-
InputStream certChain = this.getClass().getClassLoader().getResourceAsStream("client_cert.pem");
996+
File privateKey = new File("src/test/resources/client_key.pem");
997+
File certChain = new File("src/test/resources/client_cert.pem");
998998
assertThat(provider.createMtlsToS2AChannelCredentials(null, privateKey, certChain)).isNull();
999999
}
10001000

10011001
@Test
10021002
void createMtlsToS2AChannelCredentials_missingKeyFile_throws() throws IOException {
10031003
InstantiatingGrpcChannelProvider provider =
10041004
InstantiatingGrpcChannelProvider.newBuilder().build();
1005-
InputStream trustBundle = this.getClass().getClassLoader().getResourceAsStream("root_cert.pem");
1006-
InputStream certChain = this.getClass().getClassLoader().getResourceAsStream("client_cert.pem");
1005+
File trustBundle = new File("src/test/resources/root_cert.pem");
1006+
File certChain = new File("src/test/resources/client_cert.pem");
10071007
assertThat(provider.createMtlsToS2AChannelCredentials(trustBundle, null, certChain)).isNull();
10081008
}
10091009

10101010
@Test
10111011
void createMtlsToS2AChannelCredentials_missingCertChainFile_throws() throws IOException {
10121012
InstantiatingGrpcChannelProvider provider =
10131013
InstantiatingGrpcChannelProvider.newBuilder().build();
1014-
InputStream trustBundle = this.getClass().getClassLoader().getResourceAsStream("root_cert.pem");
1015-
InputStream privateKey = this.getClass().getClassLoader().getResourceAsStream("client_key.pem");
1014+
File trustBundle = new File("src/test/resources/root_cert.pem");
1015+
File privateKey = new File("src/test/resources/client_key.pem");
10161016
assertThat(provider.createMtlsToS2AChannelCredentials(trustBundle, privateKey, null)).isNull();
10171017
}
10181018

10191019
@Test
10201020
void createMtlsToS2AChannelCredentials_success() throws IOException {
10211021
InstantiatingGrpcChannelProvider provider =
10221022
InstantiatingGrpcChannelProvider.newBuilder().build();
1023-
InputStream trustBundle = this.getClass().getClassLoader().getResourceAsStream("root_cert.pem");
1024-
InputStream privateKey = this.getClass().getClassLoader().getResourceAsStream("client_key.pem");
1025-
InputStream certChain = this.getClass().getClassLoader().getResourceAsStream("client_cert.pem");
1026-
assertThat(trustBundle).isNotNull();
1023+
File trustBundle = new File("src/test/resources/root_cert.pem");
1024+
File privateKey = new File("src/test/resources/client_key.pem");
1025+
File certChain = new File("src/test/resources/client_cert.pem");
10271026
assertEquals(
10281027
provider.createMtlsToS2AChannelCredentials(trustBundle, privateKey, certChain).getClass(),
10291028
TlsChannelCredentials.class);

0 commit comments

Comments
 (0)