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+
132package com .google .auth .mtls ;
233
334import com .google .api .client .json .GenericJson ;
435import com .google .api .client .json .JsonFactory ;
536import com .google .api .client .json .JsonObjectParser ;
637import com .google .api .client .json .gson .GsonFactory ;
38+ import com .google .common .base .Strings ;
739import java .io .IOException ;
840import java .io .InputStream ;
941import java .nio .charset .StandardCharsets ;
1042import 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 }
0 commit comments