Skip to content

feat(mtls): Add support for X.509-based mTLS-transport in Java GAX lib #3852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

andyrzhao
Copy link

Fixes #3851

  • Refactors mTLS code path to use new CertificateBasedAccess class to determine mTLS behavior based on env vars.
  • Refactors mTLS code path to use DefaultMtlsProviderFactory from Java auth lib for creating a default mTLS provider using either the legacy SecureConnect mtls provider or the newer X.509 mtls provider, depending on availability.

@product-auto-label product-auto-label bot added the size: l Pull request size is large. label Jun 25, 2025
@@ -150,6 +153,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
@Nullable private final Boolean allowNonDefaultServiceAccount;
@VisibleForTesting final ImmutableMap<String, ?> directPathServiceConfig;
@Nullable private final MtlsProvider mtlsProvider;
@Nullable private final CertificateBasedAccess certificateBasedAccess;
Copy link
Contributor

Choose a reason for hiding this comment

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

qq, I believe below has a default value for the certificateBasedAccess. What's the reason this should be Nullable?

Copy link
Author

@andyrzhao andyrzhao Jul 2, 2025

Choose a reason for hiding this comment

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

So the "CertificateBasedAccess" is essentially a couple of helper functions that was moved out of the original MtlsProvider implementation, and since the previous MtlsProvider was Nullable, this was also marked Nullable to retain the same semantics - my hunch is that we should keep it Nullable for maximum compatibility/flexibility.

KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
factory.init(mtlsKeyStore, new char[] {});
return TlsChannelCredentials.newBuilder().keyManager(factory.getKeyManagers()).build();
if (certificateBasedAccess != null && certificateBasedAccess.useMtlsClientCertificate()) {
Copy link
Contributor

@lqiu96 lqiu96 Jun 30, 2025

Choose a reason for hiding this comment

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

similar to above, do we need the null check for certificateBasedAccess?

Copy link
Author

Choose a reason for hiding this comment

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

See above, I think I'd rather keep the @nullable and null-check to retain original semantics. (Hypothetically, if we get back a null from CertificateBasedAccess.createWithSystemEnv, or a different implementation, we would want to fallback gracefully here.)

try {
mtlsProvider = DefaultMtlsProviderFactory.create();
} catch (CertificateSourceUnavailableException e) {
// This is okay. Leave mtlsProvider as null;
Copy link
Contributor

Choose a reason for hiding this comment

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

qq, can you elaborate why we can leave mtlsProvider as null here? What happens if leave this as null?

Copy link
Author

Choose a reason for hiding this comment

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

This ties back to https://google.aip.dev/auth/4114. If mtlsProvider is null, the SDK will not automatically use mTLS endpoints. Let me know if you'd like me to elaborate this (or mention AIP 4114) in the code comment itself.

factory.init(mtlsKeyStore, new char[] {});
return TlsChannelCredentials.newBuilder().keyManager(factory.getKeyManagers()).build();
if (certificateBasedAccess != null && certificateBasedAccess.useMtlsClientCertificate()) {
if (mtlsProvider != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we refactor the null checks to fail fast to reduce nesting?

i.e. if (mtlsProvider == null) return null

Copy link
Author

Choose a reason for hiding this comment

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

makes sense, updating.

@@ -92,6 +93,7 @@ class InstantiatingGrpcChannelProviderTest extends AbstractMtlsTransportChannelT
private static final String API_KEY_AUTH_HEADER_KEY = "x-goog-api-key";
private static String originalOSName;
private ComputeEngineCredentials computeEngineCredentials;
private CertificateBasedAccess cba;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can you spell out the field variable name

Copy link
Author

Choose a reason for hiding this comment

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

will do!

import com.google.api.gax.rpc.internal.EnvironmentProvider;

/** Utility class for handling certificate-based access configurations. */
public class CertificateBasedAccess {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this class be package-private?

Regarding this class: Is the plan for this class to be a wrapper to be able to read the user env var GOOGLE_API_USE_CLIENT_CERTIFICATE to determine mtls usage policy? If so, can you add that to the javadocs for this class?

Copy link
Author

Choose a reason for hiding this comment

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

Will add documentation and reference to AIP 4114 here. But this class cannot be made package-private AFAIK, since it is being widely used in unit tests (and across packages).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement X.509 WIF mTLS-transport support in Java Gax lib
2 participants