Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/*
* Copyright 2025, Google Inc. All rights reserved.
* Copyright 2025 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2025 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.google.auth.mtls;

import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;
import com.google.common.collect.ImmutableList;
import java.util.List;

/** Data class representing context_aware_metadata.json file. */
public class ContextAwareMetadataJson extends GenericJson {
/** Cert provider command */
@Key("cert_provider_command")
private List<String> commands;

/** Returns the cert provider command. */
public final ImmutableList<String> getCommands() {
return ImmutableList.copyOf(commands);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2025 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.google.auth.mtls;

import java.io.IOException;

public class DefaultMtlsProviderFactory {

/**
* Creates an instance of {@link MtlsProvider}. It first attempts to create an {@link
* com.google.auth.mtls.X509Provider}. If the certificate source is unavailable, it falls back to
* creating a {@link SecureConnectProvider}. If the secure connect provider also fails, it throws
* the original {@link com.google.auth.mtls.CertificateSourceUnavailableException}.
*
* @return an instance of {@link MtlsProvider}.
* @throws com.google.auth.mtls.CertificateSourceUnavailableException if neither provider can be
* created.
* @throws IOException if an I/O error occurs during provider creation.
*/
public static MtlsProvider create() throws IOException {
MtlsProvider mtlsProvider;
try {
mtlsProvider = new X509Provider();
mtlsProvider.getKeyStore();
return mtlsProvider;
} catch (CertificateSourceUnavailableException e) {
try {
mtlsProvider = new SecureConnectProvider();
mtlsProvider.getKeyStore();
return mtlsProvider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can SecureConnectProvider be added in a future PR? Or is this required for this PR? From my last discussion with Alex, the requirements was the inclusion of X.509 and having the Auth library STS functionality use the X.509 MtlsProvider.

I'm a bit concerned that SecureConnectProvider will exist in both Gax and Auth at the same time. I know that in the future that will commit to this existing in this repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look Lawrence! So this PR is complementary to Alex's work to support X.509-based ADC auth. The goal of this PR is to bring Java SDK to feature parity with Golang SDK and Python SDK, both of which supports X.509-based ADC auth as well as X.509-backed mTLS transports (alongside SecureConnect backed mTLS transports). This is basically the final checkbox that allows the CBA team to declare client-side GA w.r.t X.509 work.

I think the fact that the SecureConnectProvider will exist in both Gax and Auth at the same time (in the near term) is a small inconvenience that we can solve with documentation updates. Btw, you will note that the SecureConnectProvider implementation in this CL does not have any of the "CBA environment variable look up logic" present in the gax version - this is intentional: I've already started a separate refactoring effort on the GAX side to create a new helper class "CertificateBasedAccess" that will handle all env var checks and endpoint calculations. This is necessary to ensure that both X.509 mTLS and SecureConnect mTLS are gated behind the same env var variables, regardless of which mTLS provider is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think the SecureConnectProvider duplication is OK for now, given that the implementation isn't intended to be used by customers and we don't expose this on the public surface for customers to set anyways. There shouldn't be any confusion from their POV.

I'll need to double check this behavior in our downstream libraries, but i'm 90% confident that it can be fine for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like although we do not expose the public surface for customers to set, downstream handwritten libraries have handles to set MtlsProvider from EndpointContext or InstantiatingGrpcChannelProvider? (I don't know if that's something touched in your related GAX changes?) It is unlikely a handwritten library used it, but can you double check this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mTLSProvder references in EndpointContext and InstantiatingGrpcChannelProviders are internal logic that was authored around the same time the original mTLS support was added, and I'm 99% sure there are no one else is using the set MtlsProvider functionality (it's mainly used by unit tests from the same suite as far as I can tell.) We can check it again when working on the Gax refactoring. This PR alone should not introduce any breaking changes.

} catch (CertificateSourceUnavailableException ex) {
throw new CertificateSourceUnavailableException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it useful here to wrap the the original exception? It's usually good practice for debugging to wrap the original exception (the one from X509Provider or SecureConnectProvider) as the cause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with the update to use "isCertificateSourceAvailable()", we no longer have the raw exceptions here. I don't have a strong preference here either-way - this is a non-blocking error message for the general use-case.

"No MtlsSource is available on this device.");
}
}
}
}
46 changes: 46 additions & 0 deletions oauth2_http/java/com/google/auth/mtls/MtlsProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2025 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.google.auth.mtls;

import java.io.IOException;
import java.security.KeyStore;

/**
* MtlsProvider is used by the Gax library for configuring mutual TLS in the HTTP and GRPC transport
* layer. The source of the client certificate is up to the implementation.
*
* <p>Note: This interface will replace the identically named "MtlsProvider" implementation in the
* Gax library. The Gax library version of MtlsProvider will be marked as deprecated.
*/
public interface MtlsProvider {
/** Returns the mutual TLS key store. */
KeyStore getKeyStore() throws IOException;
}
161 changes: 161 additions & 0 deletions oauth2_http/java/com/google/auth/mtls/SecureConnectProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright 2025 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.google.auth.mtls;

import com.google.api.client.json.JsonParser;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.SecurityUtils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.List;

/**
* This class implements {@link MtlsProvider} for the Google Auth library transport layer via {@link
* ContextAwareMetadataJson}. This is only meant to be used internally by Google Cloud libraries,
* and the public facing methods may be changed without notice, and have no guarantee of backwards
* compatability.
*
* <p>Note: This implementation is derived from the existing "MtlsProvider" found in the Gax
* library, with two notable differences: 1) All logic associated with parsing environment variables
* related to "mTLS usage" are omitted - a separate helper class will be introduced in the Gax
* library to serve this purpose. 2) getKeyStore throws {@link
* com.google.auth.mtls.CertificateSourceUnavailableException} instead of returning "null" if this
* cert source is not available on the device.
*
* <p>Additionally, this implementation will replace the existing "MtlsProvider" in the Gax library.
* The Gax library version of MtlsProvider will be marked as deprecated.
*/
public class SecureConnectProvider implements MtlsProvider {
interface ProcessProvider {
public Process createProcess(InputStream metadata) throws IOException;
}

static class DefaultProcessProvider implements ProcessProvider {
@Override
public Process createProcess(InputStream metadata) throws IOException {
if (metadata == null) {
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if metadata == null a valid scenario? If so, when later calling methods on process can lead to NullPointerException?

Process process = processProvider.createProcess(metadata);
// Run the command and timeout after 1000 milliseconds.
int exitCode = runCertificateProviderCommand(process, 1000);
if (exitCode != 0) {
throw new IOException("Cert provider command failed with exit code: " + exitCode);
}
// Create mTLS key store with the input certificates from shell command.
return SecurityUtils.createMtlsKeyStore(process.getInputStream());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! "metadata == null" in createProcess is not a case we expect to reach within the current code logic, since "new FileInputStream" would result in "FileNotFoundException" first. To safeguard against null pointer possibility, I updated "return null" to "throw new IOException("Error creating Process: metadata is null");"

List<String> command = extractCertificateProviderCommand(metadata);
return new ProcessBuilder(command).start();
}
}

private static final String DEFAULT_CONTEXT_AWARE_METADATA_PATH =
System.getProperty("user.home") + "/.secureConnect/context_aware_metadata.json";

private String metadataPath;
private ProcessProvider processProvider;

@VisibleForTesting
SecureConnectProvider(ProcessProvider processProvider, String metadataPath) {
this.processProvider = processProvider;
this.metadataPath = metadataPath;
}

public SecureConnectProvider() {
this(new DefaultProcessProvider(), DEFAULT_CONTEXT_AWARE_METADATA_PATH);
}

/** The mutual TLS key store created with the default client certificate on device. */
@Override
public KeyStore getKeyStore() throws IOException {
try (InputStream stream = new FileInputStream(metadataPath)) {
return getKeyStore(stream, processProvider);
} catch (InterruptedException e) {
throw new IOException("Interrupted executing certificate provider command", e);
} catch (GeneralSecurityException e) {
throw new CertificateSourceUnavailableException(
"SecureConnect encountered GeneralSecurityException:", e);
} catch (FileNotFoundException exception) {
// If the metadata file doesn't exist, then there is no key store, so we will throw sentinel
// error
throw new CertificateSourceUnavailableException("SecureConnect metadata does not exist.");
}
}

@VisibleForTesting
static KeyStore getKeyStore(InputStream metadata, ProcessProvider processProvider)
throws IOException, InterruptedException, GeneralSecurityException {
Process process = processProvider.createProcess(metadata);

// Run the command and timeout after 1000 milliseconds.
int exitCode = runCertificateProviderCommand(process, 1000);
if (exitCode != 0) {
throw new IOException("Cert provider command failed with exit code: " + exitCode);
}

// Create mTLS key store with the input certificates from shell command.
return SecurityUtils.createMtlsKeyStore(process.getInputStream());
}

@VisibleForTesting
static ImmutableList<String> extractCertificateProviderCommand(InputStream contextAwareMetadata)
throws IOException {
JsonParser parser = new GsonFactory().createJsonParser(contextAwareMetadata);
ContextAwareMetadataJson json = parser.parse(ContextAwareMetadataJson.class);
return json.getCommands();
}

@VisibleForTesting
static int runCertificateProviderCommand(Process commandProcess, long timeoutMilliseconds)
throws IOException, InterruptedException {
long startTime = System.currentTimeMillis();
long remainTime = timeoutMilliseconds;

// In the while loop, keep checking if the process is terminated every 100 milliseconds
// until timeout is reached or process is terminated. In getKeyStore we set timeout to
// 1000 milliseconds, so 100 millisecond is a good number for the sleep.
while (remainTime > 0) {
Thread.sleep(Math.min(remainTime + 1, 100));
remainTime -= System.currentTimeMillis() - startTime;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of manual timeout logic implementation, for Java 8+, Process.waitFor(long timeout, TimeUnit unit) is the standard and much cleaner way to wait for a process with a timeout.
If check "every 100 milliseconds" itself is not a requirement, this would simplify logic here, to something along the lines of

static int runCertificateProviderCommand(Process commandProcess, long timeoutMilliseconds)
    throws IOException, InterruptedException {
  boolean terminated = commandProcess.waitFor(timeoutMilliseconds, TimeUnit.MILLISECONDS);
  if (!terminated) {
    commandProcess.destroy();
    throw new IOException("Cert provider command timed out");
  }
  return commandProcess.exitValue();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done~


try {
return commandProcess.exitValue();
} catch (IllegalThreadStateException ignored) {
// exitValue throws IllegalThreadStateException if process has not yet terminated.
// Once the process is terminated, exitValue no longer throws exception. Therefore
// in the while loop, we use exitValue to check if process is terminated. See
// https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#exitValue()
// for more details.
}
}

commandProcess.destroy();
throw new IOException("cert provider command timed out");
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/*
* Copyright 2025, Google Inc. All rights reserved.
* Copyright 2025 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
Expand Down
Loading
Loading