Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@
* external source for authentication to Google Cloud Platform, you must validate it before
* providing it to any Google API or library. Providing an unvalidated credential configuration to
* Google APIs can compromise the security of your systems and data. For more information, refer
* to {@see <a

Check failure on line 461 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java

View workflow job for this annotation

GitHub Actions / BomContentAssertionsTest (Test for assertion logic in BomContentTest)

no tag name after @

Check failure on line 461 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java

View workflow job for this annotation

GitHub Actions / bom-content-test

no tag name after @
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}.
*
* @param credentialsStream the stream with the credential definition.
Expand All @@ -478,7 +478,7 @@
* external source for authentication to Google Cloud Platform, you must validate it before
* providing it to any Google API or library. Providing an unvalidated credential configuration to
* Google APIs can compromise the security of your systems and data. For more information, refer
* to {@see <a

Check failure on line 481 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java

View workflow job for this annotation

GitHub Actions / BomContentAssertionsTest (Test for assertion logic in BomContentTest)

no tag name after @

Check failure on line 481 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java

View workflow job for this annotation

GitHub Actions / bom-content-test

no tag name after @
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}.
*
* @param credentialsStream the stream with the credential definition.
Expand Down Expand Up @@ -874,6 +874,7 @@
.setPrivateKeyId(privateKeyId)
.setJwtClaims(claimsBuilder.build().merge(newClaims))
.setClock(clock)
.setUniverseDomain(getUniverseDomainOrThrow())
.build();
}

Expand Down Expand Up @@ -1035,9 +1036,18 @@
.setPrivateKeyId(privateKeyId)
.setJwtClaims(claimsBuilder.build())
.setClock(clock)
.setUniverseDomain(getUniverseDomainOrThrow())
.build();
}

private String getUniverseDomainOrThrow() {
try {
return getUniverseDomain();
} catch (IOException e) {
throw new IllegalStateException("Failed to retrieve universe domain.", e);
}
}
Comment on lines +1043 to +1049

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When wrapping a checked IOException in a runtime exception, it is more idiomatic in Java 8+ to use java.io.UncheckedIOException instead of IllegalStateException. This provides a more specific exception type for I/O-related failures and allows callers to easily catch and handle it specifically. Ensure that the original exception is preserved as the cause to maintain the exception chain.

Suggested change
private String getUniverseDomainOrThrow() {
try {
return getUniverseDomain();
} catch (IOException e) {
throw new IllegalStateException("Failed to retrieve universe domain.", e);
}
}
private String getUniverseDomainOrThrow() {
try {
return getUniverseDomain();
} catch (IOException e) {
throw new java.io.UncheckedIOException("Failed to retrieve universe domain.", e);
}
}
References
  1. When catching an exception and throwing a new exception, preserve the original exception as the cause to maintain the exception chain and aid in debugging.


@Override
public void getRequestMetadata(
final URI uri, Executor executor, final RequestMetadataCallback callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ private ServiceAccountJwtAccessCredentials(
this.privateKey = Preconditions.checkNotNull(privateKey);
this.privateKeyId = privateKeyId;
this.defaultAudience = defaultAudience;
this.credentialsCache = createCache();
this.quotaProjectId = quotaProjectId;
if (universeDomain == null || universeDomain.trim().isEmpty()) {
this.universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE;
} else {
this.universeDomain = universeDomain;
}
this.credentialsCache = createCache();
}

/**
Expand Down Expand Up @@ -331,6 +331,7 @@ public JwtCredentials load(JwtClaims claims) throws Exception {
.setJwtClaims(claims)
.setLifeSpanSeconds(LIFE_SPAN_SECS)
.setClock(clock)
.setUniverseDomain(universeDomain)
.build();
}
});
Expand All @@ -355,6 +356,7 @@ public JwtCredentials jwtWithClaims(JwtClaims newClaims) {
.setJwtClaims(claimsBuilder.build().merge(newClaims))
.setLifeSpanSeconds(LIFE_SPAN_SECS)
.setClock(clock)
.setUniverseDomain(universeDomain)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,32 @@ void getRequestMetadata_withMultipleScopes_selfSignedJWT() throws IOException {
verifyJwtAccess(credentials.getRequestMetadata(CALL_URI), "scope1 scope2");
}

@Test
void jwtWithClaims_propagatesUniverseDomain() throws IOException {
JwtClaims newClaims =
JwtClaims.newBuilder().setAudience("https://service.example.com/").build();
for (String universeDomain : Arrays.asList(null, "example.com")) {
ServiceAccountCredentials credentials =
createDefaultBuilder().setUniverseDomain(universeDomain).build();

JwtCredentials jwtCredentials = credentials.jwtWithClaims(newClaims);
assertEquals(credentials.getUniverseDomain(), jwtCredentials.getUniverseDomain());
}
}

@Test
void createSelfSignedJwtCredentials_propagatesUniverseDomain() throws IOException {
for (String universeDomain : Arrays.asList(null, "example.com")) {
ServiceAccountCredentials credentials =
createDefaultBuilder().setUniverseDomain(universeDomain).build();

JwtCredentials jwtCredentials =
credentials.createSelfSignedJwtCredentials(
URI.create("https://service.example.com/v1/resource"));
assertEquals(credentials.getUniverseDomain(), jwtCredentials.getUniverseDomain());
}
}

private void verifyJwtAccess(Map<String, List<String>> metadata, String expectedScopeClaim)
throws IOException {
assertNotNull(metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -900,6 +901,26 @@ void builder_customUniverseDomain() throws IOException {
assertEquals("example.com", credentials.getUniverseDomain());
}

@Test
void jwtWithClaims_propagatesUniverseDomain() throws IOException {
PrivateKey privateKey = OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
JwtClaims newClaims = JwtClaims.newBuilder().setAudience("new-audience").build();
for (String universeDomain : Arrays.asList(null, "example.com")) {
ServiceAccountJwtAccessCredentials credentials =
ServiceAccountJwtAccessCredentials.newBuilder()
.setClientId(SA_CLIENT_ID)
.setClientEmail(SA_CLIENT_EMAIL)
.setPrivateKey(privateKey)
.setPrivateKeyId(SA_PRIVATE_KEY_ID)
.setDefaultAudience(URI.create("default-audience"))
.setUniverseDomain(universeDomain)
.build();

JwtCredentials jwtCredentials = credentials.jwtWithClaims(newClaims);
assertEquals(credentials.getUniverseDomain(), jwtCredentials.getUniverseDomain());
}
}

private void verifyJwtAccess(Map<String, List<String>> metadata, URI expectedAudience)
throws IOException {
assertNotNull(metadata);
Expand Down
Loading