Skip to content

Commit c895cb9

Browse files
committed
feat(mtls): Rename to isAvailable() and minor comment tweaks.
1 parent dcab27f commit c895cb9

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

oauth2_http/java/com/google/auth/mtls/DefaultMtlsProviderFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class DefaultMtlsProviderFactory {
3838
* Creates an instance of {@link MtlsProvider}. It first attempts to create an {@link
3939
* com.google.auth.mtls.X509Provider}. If the certificate source is unavailable, it falls back to
4040
* creating a {@link SecureConnectProvider}. If the secure connect provider also fails, it throws
41-
* the original {@link com.google.auth.mtls.CertificateSourceUnavailableException}.
41+
* a {@link com.google.auth.mtls.CertificateSourceUnavailableException}.
4242
*
4343
* <p>This is only meant to be used internally by Google Cloud libraries, and the public facing
4444
* methods may be changed without notice, and have no guarantee of backwards compatibility.
@@ -49,12 +49,15 @@ public class DefaultMtlsProviderFactory {
4949
* @throws IOException if an I/O error occurs during provider creation.
5050
*/
5151
public static MtlsProvider create() throws IOException {
52+
// Note: The caller should handle CertificateSourceUnavailableException gracefully, since
53+
// it is an expected error case. All other IOExceptions are unexpected and should be surfaced
54+
// up the call stack.
5255
MtlsProvider mtlsProvider = new X509Provider();
53-
if (mtlsProvider.isCertificateSourceAvailable()) {
56+
if (mtlsProvider.isAvailable()) {
5457
return mtlsProvider;
5558
}
5659
mtlsProvider = new SecureConnectProvider();
57-
if (mtlsProvider.isCertificateSourceAvailable()) {
60+
if (mtlsProvider.isAvailable()) {
5861
return mtlsProvider;
5962
}
6063
throw new CertificateSourceUnavailableException(

oauth2_http/java/com/google/auth/mtls/MtlsProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ public interface MtlsProvider {
5353
KeyStore getKeyStore() throws CertificateSourceUnavailableException, IOException;
5454

5555
/**
56-
* Returns true if the underlying certificate source is available.
56+
* Returns true if the underlying mTLS provider is available.
5757
*
58-
* @throws IOException if a general I/O error occurs while determining certificate source
59-
* availability.
58+
* @throws IOException if a general I/O error occurs while determining availability.
6059
*/
61-
boolean isCertificateSourceAvailable() throws IOException;
60+
boolean isAvailable() throws IOException;
6261
}

oauth2_http/java/com/google/auth/mtls/SecureConnectProvider.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public KeyStore getKeyStore() throws CertificateSourceUnavailableException, IOEx
105105
try (InputStream stream = new FileInputStream(metadataPath)) {
106106
return getKeyStore(stream, processProvider);
107107
} catch (InterruptedException e) {
108-
throw new IOException("Interrupted executing certificate provider command", e);
108+
throw new IOException("SecureConnect: Interrupted executing certificate provider command", e);
109109
} catch (GeneralSecurityException e) {
110110
throw new CertificateSourceUnavailableException(
111111
"SecureConnect encountered GeneralSecurityException:", e);
@@ -117,13 +117,12 @@ public KeyStore getKeyStore() throws CertificateSourceUnavailableException, IOEx
117117
}
118118

119119
/**
120-
* Returns true if the SecureConnect certificate source is available.
120+
* Returns true if the SecureConnect mTLS provider is available.
121121
*
122-
* @throws IOException if a general I/O error occurs while determining certificate source
123-
* availability
122+
* @throws IOException if a general I/O error occurs while determining availability.
124123
*/
125124
@Override
126-
public boolean isCertificateSourceAvailable() throws IOException {
125+
public boolean isAvailable() throws IOException {
127126
try {
128127
this.getKeyStore();
129128
} catch (CertificateSourceUnavailableException e) {
@@ -142,7 +141,8 @@ static KeyStore getKeyStore(InputStream metadata, ProcessProvider processProvide
142141
// so 1000 milliseconds is plenty of time.
143142
int exitCode = runCertificateProviderCommand(process, 1000);
144143
if (exitCode != 0) {
145-
throw new IOException("Cert provider command failed with exit code: " + exitCode);
144+
throw new IOException(
145+
"SecureConnect: Cert provider command failed with exit code: " + exitCode);
146146
}
147147

148148
// Create mTLS key store with the input certificates from shell command.
@@ -163,7 +163,7 @@ static int runCertificateProviderCommand(Process commandProcess, long timeoutMil
163163
boolean terminated = commandProcess.waitFor(timeoutMilliseconds, TimeUnit.MILLISECONDS);
164164
if (!terminated) {
165165
commandProcess.destroy();
166-
throw new IOException("Cert provider command timed out");
166+
throw new IOException("SecureConnect: Cert provider command timed out");
167167
}
168168
return commandProcess.exitValue();
169169
}

oauth2_http/java/com/google/auth/mtls/X509Provider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,17 @@ public KeyStore getKeyStore() throws CertificateSourceUnavailableException, IOEx
131131
throw e;
132132
} catch (Exception e) {
133133
// Wrap all other exception types to an IOException.
134-
throw new IOException(e);
134+
throw new IOException("X509Provider: Unexpected IOException:", e);
135135
}
136136
}
137137

138138
/**
139-
* Returns true if the X509 certificate source is available.
139+
* Returns true if the X509 mTLS provider is available.
140140
*
141-
* @throws IOException if a general I/O error occurs while determining certificate source
142-
* availability
141+
* @throws IOException if a general I/O error occurs while determining availability.
143142
*/
144143
@Override
145-
public boolean isCertificateSourceAvailable() throws IOException {
144+
public boolean isAvailable() throws IOException {
146145
try {
147146
this.getKeyStore();
148147
} catch (CertificateSourceUnavailableException e) {

oauth2_http/javatests/com/google/auth/mtls/SecureConnectProviderTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public void testGetKeyStoreNonZeroExitCode()
113113
() -> SecureConnectProvider.getKeyStore(metadata, new TestProcessProvider(1)));
114114
assertTrue(
115115
"expected to fail with nonzero exit code",
116-
actual.getMessage().contains("Cert provider command failed with exit code: 1"));
116+
actual
117+
.getMessage()
118+
.contains("SecureConnect: Cert provider command failed with exit code: 1"));
117119
}
118120

119121
@Test
@@ -142,7 +144,7 @@ public void testRunCertificateProviderCommandTimeout() throws InterruptedExcepti
142144
() -> SecureConnectProvider.runCertificateProviderCommand(certCommandProcess, 100));
143145
assertTrue(
144146
"expected to fail with timeout",
145-
actual.getMessage().contains("Cert provider command timed out"));
147+
actual.getMessage().contains("SecureConnect: Cert provider command timed out"));
146148
}
147149

148150
@Test

0 commit comments

Comments
 (0)