@@ -454,8 +454,6 @@ static CertInfo loadAndVerifyCredentials(final String certPath, final String key
454454 if (currentKeyMeta != null && currentKeyMeta .matches (cached .keyMetadata )) {
455455 return cached .certInfo ;
456456 }
457- } else if (cached .keyMetadata == null ) {
458- return cached .certInfo ;
459457 }
460458 }
461459 } catch (IOException ignored ) {
@@ -478,13 +476,21 @@ static CertInfo loadAndVerifyCredentials(final String certPath, final String key
478476 }
479477
480478 if (Strings .isNullOrEmpty (keyPath )) {
481- // Certificate-only configuration (e.g. sidecar/proxy mTLS termination where the
482- // private key is isolated from the application container).
483- CertInfo info = new CertInfo (cert , certContent );
484- if (certMeta != null && certMeta .matches (FileMetadata .of (certPath ))) {
485- cachedCredentials = new CachedCredentials (certMeta , null , true , info );
479+ boolean explicitConfigOrMtls =
480+ !Strings .isNullOrEmpty (envReader .getEnv (GOOGLE_API_CERTIFICATE_CONFIG ))
481+ || "true" .equalsIgnoreCase (envReader .getEnv (GOOGLE_API_USE_CLIENT_CERTIFICATE ));
482+ if (explicitConfigOrMtls ) {
483+ throw new IOException (
484+ "Private key is required for Agent Identity bound token request, but key path is"
485+ + " missing." );
486486 }
487- return info ;
487+ Slf4jUtils .log (
488+ LOGGER ,
489+ org .slf4j .event .Level .WARN ,
490+ Collections .emptyMap (),
491+ "Private key path is missing for Agent Identity certificate. Falling back to unbound"
492+ + " token." );
493+ return null ;
488494 }
489495
490496 FileMetadata keyMeta = FileMetadata .of (keyPath );
@@ -514,6 +520,11 @@ static CertInfo loadAndVerifyCredentials(final String certPath, final String key
514520 "Permission denied reading certificate or key files. Falling back to"
515521 + " unbound token." );
516522 return null ;
523+ } catch (IOException e ) {
524+ if (Strings .isNullOrEmpty (keyPath )) {
525+ throw e ;
526+ }
527+ LOGGER .warn ("Failed to read or verify cert/key, retrying..." , e );
517528 } catch (Exception e ) {
518529 LOGGER .warn ("Failed to read or verify cert/key, retrying..." , e );
519530 }
0 commit comments