|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 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 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.google.auth.mtls; |
| 32 | + |
| 33 | +import com.google.api.core.InternalApi; |
| 34 | +import com.google.auth.oauth2.EnvironmentProvider; |
| 35 | +import com.google.auth.oauth2.PropertyProvider; |
| 36 | +import com.google.common.base.Strings; |
| 37 | +import java.io.File; |
| 38 | +import java.io.FileInputStream; |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.InputStream; |
| 41 | +import java.util.Locale; |
| 42 | + |
| 43 | +/** |
| 44 | + * Utility class for mTLS related operations. |
| 45 | + * |
| 46 | + * <p>For internal use only. |
| 47 | + */ |
| 48 | +@InternalApi |
| 49 | +public class MtlsUtils { |
| 50 | + static final String CERTIFICATE_CONFIGURATION_ENV_VARIABLE = "GOOGLE_API_CERTIFICATE_CONFIG"; |
| 51 | + static final String WELL_KNOWN_CERTIFICATE_CONFIG_FILE = "certificate_config.json"; |
| 52 | + static final String CLOUDSDK_CONFIG_DIRECTORY = "gcloud"; |
| 53 | + |
| 54 | + private MtlsUtils() { |
| 55 | + // Prevent instantiation for Utility class |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the path to the client certificate file specified by the loaded workload certificate |
| 60 | + * configuration. |
| 61 | + * |
| 62 | + * @return The path to the certificate file. |
| 63 | + * @throws IOException if the certificate configuration cannot be found or loaded. |
| 64 | + */ |
| 65 | + public static String getCertificatePath( |
| 66 | + EnvironmentProvider envProvider, PropertyProvider propProvider, String certConfigPathOverride) |
| 67 | + throws IOException { |
| 68 | + String certPath = |
| 69 | + getWorkloadCertificateConfiguration(envProvider, propProvider, certConfigPathOverride) |
| 70 | + .getCertPath(); |
| 71 | + if (Strings.isNullOrEmpty(certPath)) { |
| 72 | + throw new CertificateSourceUnavailableException( |
| 73 | + "Certificate configuration loaded successfully, but does not contain a 'certificate_file' path."); |
| 74 | + } |
| 75 | + return certPath; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Resolves and loads the workload certificate configuration. |
| 80 | + * |
| 81 | + * <p>The configuration file is resolved in the following order of precedence: 1. The provided |
| 82 | + * certConfigPathOverride (if not null). 2. The path specified by the |
| 83 | + * GOOGLE_API_CERTIFICATE_CONFIG environment variable. 3. The well-known certificate configuration |
| 84 | + * file in the gcloud config directory. |
| 85 | + * |
| 86 | + * @param envProvider the environment provider to use for resolving environment variables |
| 87 | + * @param propProvider the property provider to use for resolving system properties |
| 88 | + * @param certConfigPathOverride optional override path for the configuration file |
| 89 | + * @return the loaded WorkloadCertificateConfiguration |
| 90 | + * @throws IOException if the configuration file cannot be found, read, or parsed |
| 91 | + */ |
| 92 | + static WorkloadCertificateConfiguration getWorkloadCertificateConfiguration( |
| 93 | + EnvironmentProvider envProvider, PropertyProvider propProvider, String certConfigPathOverride) |
| 94 | + throws IOException { |
| 95 | + File certConfig; |
| 96 | + if (certConfigPathOverride != null) { |
| 97 | + certConfig = new File(certConfigPathOverride); |
| 98 | + } else { |
| 99 | + String envCredentialsPath = envProvider.getEnv(CERTIFICATE_CONFIGURATION_ENV_VARIABLE); |
| 100 | + if (!Strings.isNullOrEmpty(envCredentialsPath)) { |
| 101 | + certConfig = new File(envCredentialsPath); |
| 102 | + } else { |
| 103 | + certConfig = getWellKnownCertificateConfigFile(envProvider, propProvider); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (!certConfig.isFile()) { |
| 108 | + throw new CertificateSourceUnavailableException( |
| 109 | + "Certificate configuration file does not exist or is not a file: " |
| 110 | + + certConfig.getAbsolutePath()); |
| 111 | + } |
| 112 | + try (InputStream certConfigStream = new FileInputStream(certConfig)) { |
| 113 | + return WorkloadCertificateConfiguration.fromCertificateConfigurationStream(certConfigStream); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static File getWellKnownCertificateConfigFile( |
| 118 | + EnvironmentProvider envProvider, PropertyProvider propProvider) throws IOException { |
| 119 | + File cloudConfigPath; |
| 120 | + String envPath = envProvider.getEnv("CLOUDSDK_CONFIG"); |
| 121 | + if (envPath != null) { |
| 122 | + cloudConfigPath = new File(envPath); |
| 123 | + } else { |
| 124 | + String osName = propProvider.getProperty("os.name", "").toLowerCase(Locale.US); |
| 125 | + if (osName.indexOf("windows") >= 0) { |
| 126 | + String appData = envProvider.getEnv("APPDATA"); |
| 127 | + if (Strings.isNullOrEmpty(appData)) { |
| 128 | + throw new CertificateSourceUnavailableException( |
| 129 | + "APPDATA environment variable is not set on Windows."); |
| 130 | + } |
| 131 | + File appDataPath = new File(appData); |
| 132 | + cloudConfigPath = new File(appDataPath, CLOUDSDK_CONFIG_DIRECTORY); |
| 133 | + } else { |
| 134 | + File configPath = new File(propProvider.getProperty("user.home", ""), ".config"); |
| 135 | + cloudConfigPath = new File(configPath, CLOUDSDK_CONFIG_DIRECTORY); |
| 136 | + } |
| 137 | + } |
| 138 | + return new File(cloudConfigPath, WELL_KNOWN_CERTIFICATE_CONFIG_FILE); |
| 139 | + } |
| 140 | +} |
0 commit comments