public class MtlsUtilsUtility class for mTLS related operations.
For internal use only.
Static Fields
STARTUP_POLL_TIMEOUT_SECONDS
public static final long STARTUP_POLL_TIMEOUT_SECONDSTotal startup polling timeout in seconds (30s).
| Field Value | |
|---|---|
| Type | Description |
long |
|
WELL_KNOWN_RUN_SPIFFE_DIR
public static final String WELL_KNOWN_RUN_SPIFFE_DIRAlternate well-known directory path where workload SPIFFE credentials and certificate
configurations may be mounted (for example, when /run/secrets is mounted directly or
referenced via resolved /run paths).
| Field Value | |
|---|---|
| Type | Description |
String |
|
WELL_KNOWN_SPIFFE_DIR
public static final String WELL_KNOWN_SPIFFE_DIRWell-known directory where workload SPIFFE credentials and certificate configurations are delivered at container startup.
| Field Value | |
|---|---|
| Type | Description |
String |
|
Static Methods
getCertificateFingerprint(@Nullable String certPath)
public static @Nullable String getCertificateFingerprint(@Nullable String certPath)Computes the lower-case SHA-256 hex fingerprint of the certificate file at certPath.
Unlike #getWorkloadCertPath, which validates configuration at channel initialization
and fails closed on errors, this method is called dynamically at runtime during active RPCs to
detect certificate rotations on disk. External certificate rotators may temporarily delete,
truncate, or rewrite the certificate file mid-RPC. Returning null on read/digest
exceptions (which callers normalize to "") allows runtime refresh checks to ignore
transient mid-write states and keep the active healthy channel without failing in-flight RPCs.
| Parameter | |
|---|---|
| Name | Description |
certPath |
@org.jspecify.annotations.Nullable java.lang.String |
| Returns | |
|---|---|
| Type | Description |
@org.jspecify.annotations.Nullable java.lang.String |
|
getCertificatePath(EnvironmentProvider envProvider, PropertyProvider propProvider, @Nullable String certConfigPathOverride)
public static String getCertificatePath(EnvironmentProvider envProvider, PropertyProvider propProvider, @Nullable String certConfigPathOverride)Returns the path to the client certificate file specified by the loaded workload certificate configuration.
| Parameters | |
|---|---|
| Name | Description |
envProvider |
EnvironmentProvider |
propProvider |
PropertyProvider |
certConfigPathOverride |
@org.jspecify.annotations.Nullable java.lang.String |
| Returns | |
|---|---|
| Type | Description |
String |
The path to the certificate file. |
| Exceptions | |
|---|---|
| Type | Description |
IOException |
if the certificate configuration cannot be found or loaded. |
getWorkloadCertPath(EnvironmentProvider envProvider, PropertyProvider propProvider)
public static @Nullable String getWorkloadCertPath(EnvironmentProvider envProvider, PropertyProvider propProvider)Resolves and returns the path to the mutual TLS client certificate, or null if none should be used.
Possible outcomes:
- Non-null String (Valid happy path): A valid workload certificate
configuration was found and both the certificate and private key files exist and are
readable; or no certificate configuration is set or present and the GKE credential bundle
(
/var/run/secrets/workload-spiffe-credentials/ x509.credential-bundle.private-key.pem) exists and is readable, in which case the bundle path is returned. - IllegalStateException (Invalid state - fail closed): An explicit
GOOGLE_API_CERTIFICATE_CONFIGpath or an existing default well-known certificate configuration file is missing, unreadable, malformed, or references missing/unreadable certificate or private key files. This is treated as an unrecoverable misconfiguration. null(Safe fallback / fail open): Client certificates are explicitly disabled viaGOOGLE_API_USE_CLIENT_CERTIFICATE(any non-empty value other than"true"), no explicit configuration is set and neither the default well-known configuration file nor a readable GKE credential bundle exists on disk, or the configuration specifies an non-workload source (e.g., ECP/PKCS11 without aworkloadsection). Callers can proceed without workload certificate file polling.
| Parameters | |
|---|---|
| Name | Description |
envProvider |
EnvironmentProvider |
propProvider |
PropertyProvider |
| Returns | |
|---|---|
| Type | Description |
@org.jspecify.annotations.Nullable java.lang.String |
|
hasCertificateConfiguration(EnvironmentProvider envProvider, PropertyProvider propProvider)
public static boolean hasCertificateConfiguration(EnvironmentProvider envProvider, PropertyProvider propProvider)Returns true if a certificate configuration is configured or present: either
GOOGLE_API_CERTIFICATE_CONFIG is set, or the well-known gcloud certificate_config.json
file exists. When this returns true, the GKE credential bundle fallback is not used, so
the certificate used for mTLS and the certificate bound to tokens always come from the same
source.
| Parameters | |
|---|---|
| Name | Description |
envProvider |
EnvironmentProvider |
propProvider |
PropertyProvider |
| Returns | |
|---|---|
| Type | Description |
boolean |
|
isGkeCredentialBundleReady(String path)
public static boolean isGkeCredentialBundleReady(String path)Returns true if the GKE credential bundle at path is a readable, non-empty
regular file.
| Parameter | |
|---|---|
| Name | Description |
path |
String |
| Returns | |
|---|---|
| Type | Description |
boolean |
|
isPathInWellKnownDir(@Nullable String pathStr, String targetWellKnownDir)
public static boolean isPathInWellKnownDir(@Nullable String pathStr, String targetWellKnownDir)Checks whether pathStr resides within targetWellKnownDir (or either of the
default well-known SPIFFE directories #WELL_KNOWN_SPIFFE_DIR and #WELL_KNOWN_RUN_SPIFFE_DIR when targetWellKnownDir is #WELL_KNOWN_SPIFFE_DIR).
| Parameters | |
|---|---|
| Name | Description |
pathStr |
@org.jspecify.annotations.Nullable java.lang.String |
targetWellKnownDir |
String |
| Returns | |
|---|---|
| Type | Description |
boolean |
|
useMtlsClientCertificate(EnvironmentProvider envProvider, PropertyProvider propProvider)
public static boolean useMtlsClientCertificate(EnvironmentProvider envProvider, PropertyProvider propProvider)Returns if mutual TLS client certificate should be used. Returns true if valid workload certificates are configured or if GOOGLE_API_USE_CLIENT_CERTIFICATE is explicitly set to true (e.g. for Enterprise Certificate Proxy or custom MtlsProviders), unless explicitly disabled via GOOGLE_API_USE_CLIENT_CERTIFICATE (set to a non-empty value other than "true").
| Parameters | |
|---|---|
| Name | Description |
envProvider |
EnvironmentProvider |
propProvider |
PropertyProvider |
| Returns | |
|---|---|
| Type | Description |
boolean |
|
verifyKeyPair(X509Certificate cert, PrivateKey privateKey)
public static void verifyKeyPair(X509Certificate cert, PrivateKey privateKey)Verifies that privateKey corresponds to the public key in cert by performing a
test signature and verification (SHA256withRSA for RSA keys, SHA256withECDSA
for EC/ECDSA keys).
Certificates with key algorithms other than RSA, EC, or ECDSA return without error so callers that accept any JCA key algorithm (such as MtlsKeyStoreUtils) pass through unchanged.
| Parameters | |
|---|---|
| Name | Description |
cert |
X509Certificatethe X.509 certificate whose public key is checked |
privateKey |
PrivateKeythe private key to verify against |
| Exceptions | |
|---|---|
| Type | Description |
GeneralSecurityException |
if |
waitForWorkloadConfigStartupIfNeeded(File configFile, String targetWellKnownDir, MtlsUtils.TimeService sleepService, @Nullable Runnable onFirstPollSleep)
public static boolean waitForWorkloadConfigStartupIfNeeded(File configFile, String targetWellKnownDir, MtlsUtils.TimeService sleepService, @Nullable Runnable onFirstPollSleep)Polls up to 30 seconds when configFile and its referenced cert_path and
key_path reside within targetWellKnownDir until the configuration and referenced
certificate/key files are non-empty readable files, or a non-retryable state is encountered.
| Parameters | |
|---|---|
| Name | Description |
configFile |
File |
targetWellKnownDir |
String |
sleepService |
MtlsUtils.TimeService |
onFirstPollSleep |
@org.jspecify.annotations.Nullable java.lang.Runnable |
| Returns | |
|---|---|
| Type | Description |
boolean |
|
| Exceptions | |
|---|---|
| Type | Description |
IOException |
if the thread is interrupted while sleeping during startup polling |