Repository navigation
feat(auth): support GKE agent identity credential bundle for bound tokens and mTLS #14595
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
Changes from 1 commit
2247ddd
fb43614
6b1f7c7
0c92971
d4200d9
45df129
d764396
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| import com.google.auth.oauth2.SystemPropertyProvider; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.SequenceInputStream; | ||
|
|
@@ -54,7 +55,8 @@ | |
| public class X509Provider implements MtlsProvider { | ||
| private final EnvironmentProvider envProvider; | ||
| private final PropertyProvider propProvider; | ||
| private final String certConfigPathOverride; | ||
| private final @Nullable String certConfigPathOverride; | ||
| private final boolean allowGkeCredentialBundle; | ||
|
|
||
| /** | ||
| * Creates an X509 provider with an override path for the certificate configuration, bypassing the | ||
|
|
@@ -70,9 +72,39 @@ public X509Provider( | |
| EnvironmentProvider envProvider, | ||
| PropertyProvider propProvider, | ||
| String certConfigPathOverride) { | ||
| this(envProvider, propProvider, certConfigPathOverride, false); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an X509 provider that optionally falls back to the GKE credential bundle when no | ||
| * certificate configuration is present. | ||
| * | ||
| * @param envProvider environment provider used for environment variables | ||
| * @param propProvider property provider used for system properties | ||
| * @param certConfigPathOverride the path to read the certificate configuration from. | ||
| * @param allowGkeCredentialBundle whether to fall back to the GKE credential bundle when no | ||
| * certificate configuration is present and {@code certConfigPathOverride} is null. | ||
| */ | ||
| X509Provider( | ||
| EnvironmentProvider envProvider, | ||
| PropertyProvider propProvider, | ||
| @Nullable String certConfigPathOverride, | ||
| boolean allowGkeCredentialBundle) { | ||
| this.envProvider = envProvider; | ||
| this.propProvider = propProvider; | ||
| this.certConfigPathOverride = certConfigPathOverride; | ||
| this.allowGkeCredentialBundle = allowGkeCredentialBundle; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an X.509 provider for the client transport layer. In addition to the locations checked | ||
| * by {@link #X509Provider()}, it falls back to the GKE credential bundle when no certificate | ||
| * configuration is present, so that the transport uses the same certificate that {@link | ||
| * MtlsUtils#getWorkloadCertPath} reports for certificate-bound tokens. | ||
| */ | ||
| static X509Provider createForTransport() { | ||
| return new X509Provider( | ||
| SystemEnvironmentProvider.getInstance(), SystemPropertyProvider.getInstance(), null, true); | ||
|
lqiu96 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -105,6 +137,10 @@ public X509Provider() { | |
| * <li>The certificate config override path, if set. | ||
| * <li>The path pointed to by the "GOOGLE_API_CERTIFICATE_CONFIG" environment variable | ||
| * <li>The well known gcloud location for the certificate configuration file. | ||
| * <li>If none of the above is configured or present, and this provider was created with {@link | ||
| * #createForTransport()}, the GKE credential bundle at {@code | ||
| * /var/run/secrets/workload-spiffe-credentials/x509.credential-bundle.private-key.pem}, | ||
| * which contains both the certificate chain and the private key. | ||
|
Comment on lines
+140
to
+143
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIURc, this doesn't seem to follow the order described here. getKeyStore immediate checks for the gkeCredentialBundle path when using DefaultMtlsProviderFactory (which looks be the default way it's instantiated in GAX) while default on for agentic identities if an opt-in choice, the configs here look like it will always check for the gke bundle for all client libraries regardless if they have opted in (I don't think this should be the case unless it's opt-in for agentic identities)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in On the opt-in point: in GAX ( |
||
| * </ul> | ||
| * | ||
| * @return a KeyStore containing the X.509 certificate specified by the certificate configuration. | ||
|
|
@@ -114,6 +150,23 @@ public X509Provider() { | |
| */ | ||
| @Override | ||
| public KeyStore getKeyStore() throws CertificateSourceUnavailableException, IOException { | ||
| if (allowGkeCredentialBundle && certConfigPathOverride == null) { | ||
| String gkeCredentialBundlePath = | ||
| MtlsUtils.getGkeCredentialBundlePath(envProvider, propProvider); | ||
| if (gkeCredentialBundlePath != null) { | ||
| try (InputStream bundleStream = new FileInputStream(new File(gkeCredentialBundlePath))) { | ||
| return SecurityUtils.createMtlsKeyStore(bundleStream); | ||
| } catch (FileNotFoundException e) { | ||
| // The bundle was removed (e.g. mid-rotation) after the availability check. | ||
| throw new CertificateSourceUnavailableException( | ||
| "GKE credential bundle is no longer available: " + gkeCredentialBundlePath, e); | ||
| } catch (Exception e) { | ||
| throw new IOException( | ||
| "X509Provider: Unable to load GKE credential bundle: " + gkeCredentialBundlePath, e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| WorkloadCertificateConfiguration workloadCertConfig = | ||
| MtlsUtils.getWorkloadCertificateConfiguration( | ||
| envProvider, propProvider, certConfigPathOverride); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow what this is for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This holds the GKE credential bundle path (initialized to
GKE_CREDENTIAL_BUNDLE_PATH) so unit tests can point it at a@TempDirfile viasetGkeCredentialBundlePathForTestinginstead of/var/run/secrets/...(similar towellKnownDirinAgentIdentityUtils). Added a comment above the field in0c929719c69to make this clear.