8000 check if files exist. · rmehta19/sdk-platform-java@91175ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 91175ef

Browse files
committed
check if files exist.
1 parent 03eb991 commit 91175ef

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

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

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -503,35 +503,45 @@ ChannelCredentials createS2ASecuredChannelCredentials() {
503503
// Currently, MTLS to MDS is only available on GCE. See:
504504
// https://cloud.google.com/compute/docs/metadata/overview#https-mds
505505
// Try to load MTLS-MDS creds.
506-
InputStream trustBundle = null;
507-
InputStream privateKey = null;
508-
InputStream certChain = null;
509-
try {
510-
trustBundle = new FileInputStream(MTLS_MDS_ROOT);
511-
privateKey = new FileInputStream(MTLS_MDS_CERT_CHAIN_AND_KEY);
512-
certChain = new FileInputStream(MTLS_MDS_CERT_CHAIN_AND_KEY);
513-
} catch (FileNotFoundException ignore) {
514-
// Fallback to plaintext-to-S2A connection.
515-
LOG.log(
516-
Level.INFO,
517-
"Cannot establish an mTLS connection to S2A due to error loading MTLS to MDS credentials, falling back to plaintext connection to S2A: "
518-
+ ignore.getMessage());
519-
return createPlaintextToS2AChannelCredentials(plaintextAddress);
520-
}
521-
ChannelCredentials mtlsToS2AChannelCredentials = null;
522-
try {
506+
File rootFile = new File(MTLS_MDS_ROOT);
507+
File certKeyFile = new File(MTLS_MDS_CERT_CHAIN_AND_KEY);
508+
if (!rootFile.isFile() || !certKeyFile.isFile()) {
523509
// Try to connect to S2A using mTLS.
524-
mtlsToS2AChannelCredentials =
525-
createMtlsToS2AChannelCredentials(trustBundle, privateKey, certChain);
526-
} catch (IOException ignore) {
527-
// Fallback to plaintext-to-S2A connection.
510+
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.INFO,
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+
}
526+
try {
527+
mtlsToS2AChannelCredentials =
528+
createMtlsToS2AChannelCredentials(trustBundle, privateKey, certChain);
529+
} catch (IOException ignore) {
530+
// Fallback to plaintext-to-S2A connection on error.
531+
LOG.log(
532+
Level.WARNING,
533+
"Cannot establish an mTLS connection to S2A due to error creating MTLS to MDS TlsChannelCredentials credentials, falling back to plaintext connection to S2A: "
534+
+ ignore.getMessage());
535+
return createPlaintextToS2AChannelCredentials(plaintextAddress);
536+
}
537+
return S2AChannelCredentials.newBuilder(mtlsAddress, mtlsToS2AChannelCredentials).build();
538+
} else {
539+
// Fallback to plaintext-to-S2A connection if MTLS-MDS creds do not exist.
528540
LOG.log(
529-
Level.WARNING,
530-
"Cannot establish an mTLS connection to S2A due to error creating MTLS to MDS TlsChannelCredentials credentials, falling back to plaintext connection to S2A: "
531-
+ ignore.getMessage());
541+
Level.INFO,
542+
"Cannot establish an mTLS connection to S2A MTLS to MDS credentials do not exist on filesystem, falling back to plaintext connection to S2A");
532543
return createPlaintextToS2AChannelCredentials(plaintextAddress);
533544
}
534-
return S2AChannelCredentials.newBuilder(mtlsAddress, mtlsToS2AChannelCredentials).build();
535545
}
536546

537547
private ManagedChannel createSingleChannel() throws IOException {

0 commit comments

Comments
 (0)
0