Skip to content

Commit 889015c

Browse files
committed
responding to review comments
1 parent c54fedb commit 889015c

File tree

3 files changed

+110
-17
lines changed

3 files changed

+110
-17
lines changed

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

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
1+
/*
2+
* Copyright 2025, Google Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
132
package com.google.auth.mtls;
233

334
import com.google.api.client.json.GenericJson;
435
import com.google.api.client.json.JsonFactory;
536
import com.google.api.client.json.JsonObjectParser;
637
import com.google.api.client.json.gson.GsonFactory;
38+
import com.google.common.base.Strings;
739
import java.io.IOException;
840
import java.io.InputStream;
941
import java.nio.charset.StandardCharsets;
1042
import java.util.Map;
1143

12-
public class WorkloadCertificateConfiguration {
44+
class WorkloadCertificateConfiguration {
1345

1446
private String certPath;
1547
private String privateKeyPath;
1648

17-
public WorkloadCertificateConfiguration(String certPath, String privateKeyPath) {
49+
private static JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
50+
private static JsonObjectParser parser = new JsonObjectParser(jsonFactory);
51+
52+
WorkloadCertificateConfiguration(String certPath, String privateKeyPath) {
1853
this.certPath = certPath;
1954
this.privateKeyPath = privateKeyPath;
2055
}
2156

22-
public String getCertPath() {
57+
String getCertPath() {
2358
return certPath;
2459
}
2560

26-
public String getPrivateKeyPath() {
61+
String getPrivateKeyPath() {
2762
return privateKeyPath;
2863
}
2964

30-
public static WorkloadCertificateConfiguration fromCertificateConfigurationStream(
65+
66+
67+
static WorkloadCertificateConfiguration fromCertificateConfigurationStream(
3168
InputStream certConfigStream) throws IOException {
32-
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
33-
JsonObjectParser parser = new JsonObjectParser(jsonFactory);
69+
if (certConfigStream == null){
70+
throw new IllegalArgumentException("certConfigStream must not be null.");
71+
}
3472

3573
GenericJson fileContents =
3674
parser.parseAndClose(certConfigStream, StandardCharsets.UTF_8, GenericJson.class);
@@ -48,13 +86,13 @@ public static WorkloadCertificateConfiguration fromCertificateConfigurationStrea
4886
}
4987

5088
String certPath = (String) workloadConfig.get("cert_path");
51-
if (certPath.isEmpty() || certPath == null) {
89+
if (Strings.isNullOrEmpty(certPath)) {
5290
throw new IllegalArgumentException(
5391
"The cert_path field must be provided in the workload certificate configuration.");
5492
}
5593

5694
String privateKeyPath = (String) workloadConfig.get("key_path");
57-
if (privateKeyPath.isEmpty() || privateKeyPath == null) {
95+
if (Strings.isNullOrEmpty(privateKeyPath)) {
5896
throw new IllegalArgumentException(
5997
"The key_path field must be provided in the workload certificate configuration.");
6098
}

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
/*
2+
* Copyright 2025, Google Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
132
package com.google.auth.mtls;
233

334
import com.google.api.client.util.SecurityUtils;
@@ -21,20 +52,34 @@ public X509Provider(String certConfigPathOverride) {
2152
this.certConfigPathOverride = certConfigPathOverride;
2253
}
2354

24-
public X509Provider() {
25-
super(null);
55+
public X509Provider(){
56+
this(null);
2657
}
2758

59+
/**
60+
* Finds the certificate configuration file, then builds a Keystore using the X.509 certificate
61+
* and private key pointed to by the configuration. This will check the following locations in
62+
* order.
63+
* <ul>
64+
* <li>The certificate config override path, if set.
65+
* <li>The path pointed to by the "GOOGLE_API_CERTIFICATE_CONFIG" environment variable
66+
* <li>The well known gcloud location for the certificate configuration file.
67+
* </ul>
68+
* @return a KeyStore containing the X.509 certificate specified by the certificate configuration.
69+
* @throws IOException if there is an error retrieving the certificate configuration.
70+
*/
2871
public KeyStore getKeyStore() throws IOException {
2972

3073
WorkloadCertificateConfiguration workloadCertConfig = getWorkloadCertificateConfiguration();
3174

75+
InputStream certStream = null;
76+
InputStream privateKeyStream = null;
3277
try {
3378
// Read the certificate and private key file paths into separate streams.
3479
File certFile = new File(workloadCertConfig.getCertPath());
3580
File privateKeyFile = new File(workloadCertConfig.getPrivateKeyPath());
36-
InputStream certStream = readStream(certFile);
37-
InputStream privateKeyStream = readStream(privateKeyFile);
81+
certStream = readStream(certFile);
82+
privateKeyStream = readStream(privateKeyFile);
3883

3984
// Merge the two streams into a single stream.
4085
SequenceInputStream certAndPrivateKeyStream =
@@ -44,6 +89,13 @@ public KeyStore getKeyStore() throws IOException {
4489
return SecurityUtils.createMtlsKeyStore(certAndPrivateKeyStream);
4590
} catch (Exception e) {
4691
throw new IOException(e);
92+
} finally {
93+
if (certStream != null) {
94+
certStream.close();
95+
}
96+
if (privateKeyStream != null) {
97+
privateKeyStream.close();
98+
}
4799
}
48100
}
49101

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,17 @@ public void x509Provider_succeeds_withWellKnownPath()
199199
}
200200

201201
static class TestX509Provider extends X509Provider {
202-
private final Map<String, InputStream> files = new HashMap<>();
203-
private final Map<String, String> variables = new HashMap<>();
204-
private final Map<String, String> properties = new HashMap<>();
202+
private Map<String, InputStream> files;
203+
private Map<String, String> variables;
204+
private Map<String, String> properties;
205205

206-
TestX509Provider() {}
206+
TestX509Provider() {this(null);}
207207

208208
TestX509Provider(String filePathOverride) {
209209
super(filePathOverride);
210+
this.files = new HashMap<>();
211+
this.variables = new HashMap<>();
212+
this.properties = new HashMap<>();
210213
}
211214

212215
void addFile(String file, InputStream stream) {

0 commit comments

Comments
 (0)