Skip to content

Commit 5c444d1

Browse files
committed
fix(oauth2): strictly require key_path and verifyKeyPair for bound token credentials
1 parent b95a5da commit 5c444d1

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/AgentIdentityUtils.java‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/AgentIdentityUtilsTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ public void getAgentIdentityCertInfo_explicitMtlsEnabled_missingPrivateKey_throw
10011001
}
10021002

10031003
@Test
1004-
public void getAgentIdentityCertInfo_certificateOnlyConfig_omittedKeyPath_returnsCertInfo()
1004+
public void getAgentIdentityCertInfo_certificateOnlyConfig_omittedKeyPath_throwsIOException()
10051005
throws Exception {
10061006
URL certUrl = getClass().getClassLoader().getResource("agent/agent_spiffe_cert.pem");
10071007
assertNotNull(certUrl);
@@ -1024,13 +1024,13 @@ public void getAgentIdentityCertInfo_certificateOnlyConfig_omittedKeyPath_return
10241024
FakeTimeService fakeTime = new FakeTimeService();
10251025
AgentIdentityUtils.setTimeService(fakeTime);
10261026

1027-
AgentIdentityUtils.CertInfo info1 = AgentIdentityUtils.getAgentIdentityCertInfo();
1028-
assertNotNull(info1);
1027+
IOException e = assertThrows(IOException.class, AgentIdentityUtils::getAgentIdentityCertInfo);
1028+
assertTrue(
1029+
e.getMessage()
1030+
.contains(
1031+
"Private key is required for Agent Identity bound token request, but key path is"
1032+
+ " missing."));
10291033
assertEquals(0, fakeTime.getSleepCount());
1030-
1031-
// Subsequent call returns cached instance
1032-
AgentIdentityUtils.CertInfo info2 = AgentIdentityUtils.getAgentIdentityCertInfo();
1033-
assertSame(info1, info2);
10341034
}
10351035

10361036
@Test

0 commit comments

Comments
 (0)