Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds universe domain for DownscopedCredentials and ExternalAccountAuthorizedUserCredentials #1355

Merged
merged 11 commits into from
Jan 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ private DownscopedCredentials(Builder builder) {
} else {
this.universeDomain = builder.universeDomain;
}

// Ensure source credential's universe domain matches.
try {
if (!this.universeDomain.equals(sourceCredential.getUniverseDomain())) {
throw new IllegalArgumentException(
"The downscoped credential's universe domain must be the same as the source "
+ "credential.");
}
} catch (IOException e) {
throw new IllegalStateException(
lsirac marked this conversation as resolved.
Show resolved Hide resolved
lsirac marked this conversation as resolved.
Show resolved Hide resolved
"Error occurred when attempting to retrieve source credential universe domain.");
}
this.tokenExchangeEndpoint =
TOKEN_EXCHANGE_URL_FORMAT.replace("{universe_domain}", universeDomain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,19 @@ public void refreshAccessToken() throws IOException {
@Test
public void refreshAccessToken_withCustomUniverseDomain() throws IOException {
MockStsTransportFactory transportFactory = new MockStsTransportFactory();

String universeDomain = "foobar";
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ true);
getServiceAccountSourceCredentials(/* canRefresh= */ true)
.toBuilder()
.setUniverseDomain(universeDomain)
.build();

DownscopedCredentials downscopedCredentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setHttpTransportFactory(transportFactory)
.setUniverseDomain("universe_domain")
.setUniverseDomain(universeDomain)
.build();

AccessToken accessToken = downscopedCredentials.refreshAccessToken();
Expand All @@ -144,7 +147,7 @@ public void refreshAccessToken_withCustomUniverseDomain() throws IOException {

// Verify domain.
String url = transportFactory.transport.getRequest().getUrl();
assertEquals(url, String.format(TOKEN_EXCHANGE_URL_FORMAT, "universe_domain"));
assertEquals(url, String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain));
}

@Test
Expand Down Expand Up @@ -259,6 +262,44 @@ public void builder_noUniverseDomain_defaults() throws IOException {
assertEquals(GOOGLE_DEFAULT_UNIVERSE, credentials.getUniverseDomain());
}

@Test
public void builder_universeDomainMismatch_throws() throws IOException {
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ true);

try {
DownscopedCredentials.newBuilder()
.setHttpTransportFactory(OAuth2Utils.HTTP_TRANSPORT_FACTORY)
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setUniverseDomain("differentUniverseDomain")
.build();
fail("Should fail with universe domain mismatch.");
} catch (IllegalArgumentException e) {
assertEquals(
"The downscoped credential's universe domain must be the same as the source credential.",
e.getMessage());
}
}

@Test
public void builder_sourceUniverseDomainUnavailable_throws() throws IOException {
GoogleCredentials sourceCredentials = new MockSourceCredentialWithoutUniverseDomain();

try {
DownscopedCredentials.newBuilder()
.setHttpTransportFactory(OAuth2Utils.HTTP_TRANSPORT_FACTORY)
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.build();
fail("Should fail to retrieve source credential universe domain.");
} catch (IllegalStateException e) {
assertEquals(
"Error occurred when attempting to retrieve source credential universe domain.",
e.getMessage());
}
}

private static GoogleCredentials getServiceAccountSourceCredentials(boolean canRefresh)
throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
Expand Down Expand Up @@ -296,4 +337,11 @@ private static GoogleCredentials getUserSourceCredentials() {
.setHttpTransportFactory(transportFactory)
.build();
}

static class MockSourceCredentialWithoutUniverseDomain extends GoogleCredentials {
@Override
public String getUniverseDomain() throws IOException {
throw new IOException();
}
}
}