Repository navigation
fix(auth): align mTLS transport and bound token certificate startup polling and well-known discovery - #14748
fix(auth): align mTLS transport and bound token certificate startup polling and well-known discovery#14748macastelaz wants to merge 1 commit into
Conversation
…olling and well-known discovery
There was a problem hiding this comment.
Code Review
This pull request introduces container startup polling in MtlsUtils to wait for credential file readiness when configuration or certificate files reside in the well-known SPIFFE directory, and unifies startup state tracking across mTLS transport initialization and Agent Identity token binding. The feedback suggests minor code simplifications by removing redundant checks in MtlsUtils (specifically in isExistingNonFileOrUnreadable and the exception handling block) and proposes a concurrency optimization to check the startup completion flag inside the polling loop to prevent unnecessary sleep cycles.
| private static boolean isExistingNonFileOrUnreadable(File file) { | ||
| return file.exists() && (!file.isFile() || (!file.canRead() && file.exists())); | ||
| } |
There was a problem hiding this comment.
The expression (!file.canRead() && file.exists()) contains a redundant file.exists() check because file.exists() is already verified at the beginning of the method. This can be simplified to !file.isFile() || !file.canRead().
| private static boolean isExistingNonFileOrUnreadable(File file) { | |
| return file.exists() && (!file.isFile() || (!file.canRead() && file.exists())); | |
| } | |
| private static boolean isExistingNonFileOrUnreadable(File file) { | |
| return file.exists() && (!file.isFile() || !file.canRead()); | |
| } |
References
- When implementing property parsing or validation logic, ensure that null checks and validation steps are not redundant with checks already performed by upstream callers or preceding logic in the same method.
| boolean shouldPoll = isPathInWellKnownSpiffeDir(configFile.getPath()); | ||
| int maxCycles = shouldPoll ? TOTAL_POLL_CYCLES : 1; | ||
|
|
||
| for (int cycle = 0; cycle < maxCycles; cycle++) { |
There was a problem hiding this comment.
In a highly concurrent startup environment, multiple threads might call waitForExplicitConfigStartupIfNeeded simultaneously. Once one thread successfully completes the polling and sets initialStartupCompleted = true, other polling threads can immediately stop polling and return null instead of continuing to sleep and check. This avoids redundant disk I/O and sleep cycles.
for (int cycle = 0; cycle < maxCycles; cycle++) {
if (initialStartupCompleted) {
return null;
}| if (configFile.length() > 0) { | ||
| // Valid JSON missing required cert_configs, cert_path, or key_path; do not poll. | ||
| initialStartupCompleted = true; | ||
| throw e; | ||
| } |
There was a problem hiding this comment.
The check configFile.length() > 0 is redundant here because isNonEmptyReadableFile(configFile) was already verified on line 227, which guarantees that configFile.length() > 0 is true. We can safely remove this check and directly mark startup as completed and rethrow the exception.
// Valid JSON missing required cert_configs, cert_path, or key_path; do not poll.
initialStartupCompleted = true;
throw e;References
- When implementing property parsing or validation logic, ensure that null checks and validation steps are not redundant with checks already performed by upstream callers or preceding logic in the same method.
Summary
Fixes two certificate discovery asymmetries between mTLS transport initialization (
MtlsUtils/X509Provider) and Agent Identity bound-token acquisition (AgentIdentityUtils):Add 30-second initial startup polling to
MtlsUtilsfor the well-known SPIFFE directory:GOOGLE_API_CERTIFICATE_CONFIG(orcertConfigPathOverride) or its referencedcert_path/key_pathresides in/var/run/secrets/workload-spiffe-credentials/(resolving ancestor symlinks viaresolveRealPathAllowingMissing),MtlsUtilsnow polls for up to 30 seconds (50 × 100ms+50 × 500ms) on initial container startup (!isInitialStartupCompleted()) until the configuration file,cert_path, andkey_pathare non-empty readable regular files and the JSON configuration parses cleanly.MtlsUtilsandAgentIdentityUtilsshare theinitialStartupCompletedstate so the JVM performs the 30-second cold-start poll at most once across both transport and token initialization.cert_path/key_path) fail immediately with 0 sleeps.Remove unconfigured legacy
credentialbundle.pemandcertificates.pem+private_key.pemfallbacks inAgentIdentityUtils:GOOGLE_API_CERTIFICATE_CONFIGis unset,AgentIdentityUtils.getWellKnownCertificatePathWithRetry()now checks only the GKE Pod Certificate bundle (/var/run/secrets/workload-spiffe-credentials/x509.credential-bundle.private-key.pem), matchingMtlsUtils.getWorkloadCertPath()andX509Provider1:1 soAgentIdentityUtilsnever mints a certificate-bound token for a certificate that the transport ignores.Testing
MtlsUtilsTest,X509ProviderTest, andAgentIdentityUtilsTestcovering:/var/run/secrets/workload-spiffe-credentials/cert_path/key_pathinside the well-known SPIFFE directoryinitialStartupCompletedstate acrossMtlsUtilsandAgentIdentityUtils(0 sleeps on subsequent calls)credentialbundle.pemandcertificates.pem+private_key.pemwhenGOOGLE_API_CERTIFICATE_CONFIGis unset