Skip to content

Commit 9695798

Browse files
committed
Addressed comments.
1 parent a674758 commit 9695798

6 files changed

Lines changed: 105 additions & 4 deletions

File tree

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountAuthorizedUserCredentials.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ public AccessToken refreshAccessToken() throws IOException {
237237
@InternalApi
238238
@Override
239239
public String getRegionalAccessBoundaryUrl() throws IOException {
240-
Matcher matcher = WORKFORCE_AUDIENCE_PATTERN.matcher(getAudience());
240+
String audience = getAudience();
241+
if (audience == null) {
242+
throw new IllegalStateException(
243+
"The audience is null, which is not in the correct format for a workforce pool.");
244+
}
245+
Matcher matcher = WORKFORCE_AUDIENCE_PATTERN.matcher(audience);
241246
if (!matcher.matches()) {
242247
throw new IllegalStateException(
243248
"The provided audience is not in the correct format for a workforce pool. "

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,14 +639,20 @@ public String getRegionalAccessBoundaryUrl() throws IOException {
639639
getServiceAccountEmail());
640640
}
641641

642-
Matcher workforceMatcher = WORKFORCE_AUDIENCE_PATTERN.matcher(getAudience());
642+
String audience = getAudience();
643+
if (audience == null) {
644+
throw new IllegalStateException(
645+
"The audience is null, which is not in a valid format for either a workload identity pool or a workforce pool.");
646+
}
647+
648+
Matcher workforceMatcher = WORKFORCE_AUDIENCE_PATTERN.matcher(audience);
643649
if (workforceMatcher.matches()) {
644650
String poolId = workforceMatcher.group("pool");
645651
return String.format(
646652
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKFORCE_POOL, poolId);
647653
}
648654

649-
Matcher workloadMatcher = WORKLOAD_AUDIENCE_PATTERN.matcher(getAudience());
655+
Matcher workloadMatcher = WORKLOAD_AUDIENCE_PATTERN.matcher(audience);
650656
if (workloadMatcher.matches()) {
651657
String projectNumber = workloadMatcher.group("project");
652658
String poolId = workloadMatcher.group("pool");

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/RegionalAccessBoundary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ final class RegionalAccessBoundary implements Serializable {
102102
this.locations =
103103
locations == null
104104
? Collections.<String>emptyList()
105-
: Collections.unmodifiableList(locations);
105+
: Collections.unmodifiableList(new java.util.ArrayList<>(locations));
106106
this.refreshTime = refreshTime;
107107
this.clock = clock != null ? clock : Clock.SYSTEM;
108108
}

google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountAuthorizedUserCredentialsTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,69 @@ void testRefresh_regionalAccessBoundarySuccess() throws IOException, Interrupted
12701270
Arrays.asList(TestUtils.REGIONAL_ACCESS_BOUNDARY_ENCODED_LOCATION));
12711271
}
12721272

1273+
@Test
1274+
void getRegionalAccessBoundaryUrl_workforce() throws IOException {
1275+
ExternalAccountAuthorizedUserCredentials credentials =
1276+
ExternalAccountAuthorizedUserCredentials.newBuilder()
1277+
.setClientId(CLIENT_ID)
1278+
.setClientSecret(CLIENT_SECRET)
1279+
.setRefreshToken(REFRESH_TOKEN)
1280+
.setTokenUrl(TOKEN_URL)
1281+
.setAudience(
1282+
"//iam.googleapis.com/locations/global/workforcePools/my-pool/providers/my-provider")
1283+
.build();
1284+
1285+
String expectedUrl =
1286+
"https://iamcredentials.googleapis.com/v1/locations/global/workforcePools/my-pool/allowedLocations";
1287+
assertEquals(expectedUrl, credentials.getRegionalAccessBoundaryUrl());
1288+
}
1289+
1290+
@Test
1291+
void getRegionalAccessBoundaryUrl_invalidAudience_throws() {
1292+
ExternalAccountAuthorizedUserCredentials credentials =
1293+
ExternalAccountAuthorizedUserCredentials.newBuilder()
1294+
.setClientId(CLIENT_ID)
1295+
.setClientSecret(CLIENT_SECRET)
1296+
.setRefreshToken(REFRESH_TOKEN)
1297+
.setTokenUrl(TOKEN_URL)
1298+
.setAudience("invalid-audience")
1299+
.build();
1300+
1301+
IllegalStateException exception =
1302+
assertThrows(
1303+
IllegalStateException.class,
1304+
() -> {
1305+
credentials.getRegionalAccessBoundaryUrl();
1306+
});
1307+
1308+
assertEquals(
1309+
"The provided audience is not in the correct format for a workforce pool. "
1310+
+ "Refer: https://docs.cloud.google.com/iam/docs/principal-identifiers",
1311+
exception.getMessage());
1312+
}
1313+
1314+
@Test
1315+
void getRegionalAccessBoundaryUrl_nullAudience_throws() {
1316+
ExternalAccountAuthorizedUserCredentials credentials =
1317+
ExternalAccountAuthorizedUserCredentials.newBuilder()
1318+
.setClientId(CLIENT_ID)
1319+
.setClientSecret(CLIENT_SECRET)
1320+
.setRefreshToken(REFRESH_TOKEN)
1321+
.setTokenUrl(TOKEN_URL)
1322+
.build();
1323+
1324+
IllegalStateException exception =
1325+
assertThrows(
1326+
IllegalStateException.class,
1327+
() -> {
1328+
credentials.getRegionalAccessBoundaryUrl();
1329+
});
1330+
1331+
assertEquals(
1332+
"The audience is null, which is not in the correct format for a workforce pool.",
1333+
exception.getMessage());
1334+
}
1335+
12731336
private void waitForRegionalAccessBoundary(GoogleCredentials credentials)
12741337
throws InterruptedException {
12751338
long deadline = System.currentTimeMillis() + 5000;

google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,32 @@ public void getRegionalAccessBoundaryUrl_invalidAudience_throws() {
12971297
exception.getMessage());
12981298
}
12991299

1300+
@Test
1301+
public void getRegionalAccessBoundaryUrl_nullAudience_throws() {
1302+
ExternalAccountCredentials credentials =
1303+
new TestExternalAccountCredentials(
1304+
TestExternalAccountCredentials.newBuilder()
1305+
.setAudience("any-audience-to-pass-constructor-check")
1306+
.setSubjectTokenType("subject_token_type")
1307+
.setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP))) {
1308+
@Override
1309+
public String getAudience() {
1310+
return null;
1311+
}
1312+
};
1313+
1314+
IllegalStateException exception =
1315+
assertThrows(
1316+
IllegalStateException.class,
1317+
() -> {
1318+
credentials.getRegionalAccessBoundaryUrl();
1319+
});
1320+
1321+
assertEquals(
1322+
"The audience is null, which is not in a valid format for either a workload identity pool or a workforce pool.",
1323+
exception.getMessage());
1324+
}
1325+
13001326
@Test
13011327
public void refresh_workload_regionalAccessBoundarySuccess()
13021328
throws IOException, InterruptedException {

java-dataplex/grpc-google-cloud-dataplex-v1/src/main/java/com/google/cloud/dataplex/v1/ContentServiceGrpc.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.cloud.dataplex.v1;
1717

18+
1819
/**
1920
*
2021
*

0 commit comments

Comments
 (0)