Skip to content

chore: Refactor x509Provider to create a shared Utils class for Mtls#1907

Open
vverman wants to merge 10 commits intogoogleapis:mainfrom
vverman:refactor-x509-provider
Open

chore: Refactor x509Provider to create a shared Utils class for Mtls#1907
vverman wants to merge 10 commits intogoogleapis:mainfrom
vverman:refactor-x509-provider

Conversation

@vverman
Copy link
Copy Markdown
Contributor

@vverman vverman commented Mar 24, 2026

Fixes #1745

Addressed a concern raised by Andy refer.

Now X509Provider only exposes the necessary methods needed by the MtlsProvider interface

@vverman vverman requested review from a team as code owners March 24, 2026 02:24
@product-auto-label product-auto-label bot added the size: l Pull request size is large. label Mar 24, 2026
@vverman vverman force-pushed the refactor-x509-provider branch from 1500bea to f78ed98 Compare March 24, 2026 03:02
@vverman vverman requested a review from lqiu96 March 24, 2026 03:04
*
* <p>For internal use only.
*/
public class SystemEnvironmentProvider implements EnvironmentProvider, Serializable {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

qq, what is the difference between SystemEnvironmentProvider and EnvironmentProvider?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

An EnvProvider is an interface which can be implemented by test-classes to pass in their own env variables.

The SystemEnvProvider implements this EnvProvider and implements the associated functions such that the env-variables are fetched from the system env.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Got it. Can you add @internalapi to this class and for SystemPropertyProvider?

@lqiu96
Copy link
Copy Markdown
Member

lqiu96 commented Mar 24, 2026

feat: Refactor x509Provider

Since this is a refactor, I'm going to change the title to chore: ... to reflect the type of change in the release notes. Thank you for helping with our backlog!

@lqiu96 lqiu96 changed the title feat: Refactor x509Provider chore: Refactor x509Provider to create a shared Utils class for Mtls Mar 24, 2026
@marcosgtz7
Copy link
Copy Markdown

marcosgtz7 commented Mar 24, 2026 via email

import java.util.HashMap;
import java.util.Map;

public class TestPropertyProvider implements PropertyProvider {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

qq, is this TestPropertyProvider used anywhere in the tests? If not, can we remove it?

Copy link
Copy Markdown
Contributor Author

@vverman vverman Mar 27, 2026

Choose a reason for hiding this comment

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

Since this is used to test the System Properties, added in a test for the windows OS default file path in X509ProviderTest.

} else {
String osName = propProvider.getProperty("os.name", "").toLowerCase(Locale.US);
if (osName.indexOf("windows") >= 0) {
File appDataPath = new File(envProvider.getEnv("APPDATA"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If APPDATA is unset on Windows, envProvider.getEnv("APPDATA") returns null. Creating a new File(null) throws a NullPointerException.

We can align with how Linux handles its user.home fallback by checking user.home as a fallback when APPDATA is missing.

Copy link
Copy Markdown
Contributor Author

@vverman vverman Mar 27, 2026

Choose a reason for hiding this comment

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

I believe this is the same logic to detect the default 'directory' where the ADC file is, within the GoogleAuthUtils.getWellKnownCredentialsFile

I believe this should stay as is since the discovery for ADC and cert config should be the same and having a default for windows might lead to an unexpected outcome for cert_config as compared to ADC.

Do lmk your thoughts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That is fair, but I think we should at least check for null here, even if we don't want to fallback on user.home.
I've opened #1910 to fix it the potential nullpointer exception in the ADC flow too.

@@ -0,0 +1,52 @@
/*
* Copyright 2025 Google LLC
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Copyright 2025 Google LLC
* Copyright 2026 Google LLC

@@ -0,0 +1,43 @@
/*
* Copyright 2025 Google LLC
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Copyright 2025 Google LLC
* Copyright 2026 Google LLC

@@ -0,0 +1,56 @@
/*
* Copyright 2025 Google LLC
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Copyright 2025 Google LLC
* Copyright 2026 Google LLC


/** Interface for an environment provider. */
interface EnvironmentProvider {
import com.google.api.core.InternalApi;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know this is an existing file, but it's missing the copyright header and maybe we can add it in this PR.


private X509Provider getX509Provider(
Builder builder, IdentityPoolCredentialSource credentialSource) {
final IdentityPoolCredentialSource.CertificateConfig certConfig =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

with the changes to this method, now certConfig is not used anymore and should be removed.

private static String getExplicitCertConfigPath(IdentityPoolCredentialSource credentialSource) {
IdentityPoolCredentialSource.CertificateConfig certConfig =
credentialSource.getCertificateConfig();
return certConfig.useDefaultCertificateConfig()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Potential null pointer access: The variable certConfig may be null at this location

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since MtlsUtils is a new utility class that is now consumed by multiple classes (IdentityPoolCredentials and X509Provider), it would be great if we could add a dedicated MtlsUtilsTest.java file and ensure that MtlsUtils was fully tested.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current tests verify the success path and the 'missing config' path, but we could add test coverage for when the config exists but the referenced certificate/key files are missing or malformed.
Let's add tests for these two scenarios:
What happens if cert.pem doesn't exist?
What happens if the .pem contains invalid text?

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.

Auth: Refactor X509Provider

4 participants