Skip to content

Commit 3d066ee

Browse files
authored
fix: support AWS_DEFAULT_REGION env var (#599)
AWS_DEFAULT_REGION env var should be checked after AWS_REGION. See b/182491887.
1 parent bf3507c commit 3d066ee

3 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,19 @@ private String buildSubjectToken(AwsRequestSignature signature)
243243
return URLEncoder.encode(token.toString(), "UTF-8");
244244
}
245245

246-
private String getAwsRegion() throws IOException {
246+
@VisibleForTesting
247+
String getAwsRegion() throws IOException {
247248
// For AWS Lambda, the region is retrieved through the AWS_REGION environment variable.
248249
String region = getEnvironmentProvider().getEnv("AWS_REGION");
249250
if (region != null) {
250251
return region;
251252
}
252253

254+
String defaultRegion = getEnvironmentProvider().getEnv("AWS_DEFAULT_REGION");
255+
if (defaultRegion != null) {
256+
return defaultRegion;
257+
}
258+
253259
if (awsCredentialSource.regionUrl == null || awsCredentialSource.regionUrl.isEmpty()) {
254260
throw new IOException(
255261
"Unable to determine the AWS region. The credential source does not contain the region URL.");

‎oauth2_http/javatests/com/google/auth/oauth2/AwsCredentialsTest.java‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,73 @@ public void getAwsSecurityCredentials_fromMetadataServer_noUrlProvided() {
345345
}
346346
}
347347

348+
@Test
349+
public void getAwsRegion_awsRegionEnvironmentVariable() throws IOException {
350+
TestEnvironmentProvider environmentProvider = new TestEnvironmentProvider();
351+
environmentProvider.setEnv("AWS_REGION", "region");
352+
environmentProvider.setEnv("AWS_DEFAULT_REGION", "defaultRegion");
353+
354+
MockExternalAccountCredentialsTransportFactory transportFactory =
355+
new MockExternalAccountCredentialsTransportFactory();
356+
AwsCredentials awsCredentials =
357+
(AwsCredentials)
358+
AwsCredentials.newBuilder(AWS_CREDENTIAL)
359+
.setHttpTransportFactory(transportFactory)
360+
.setCredentialSource(buildAwsCredentialSource(transportFactory))
361+
.setEnvironmentProvider(environmentProvider)
362+
.build();
363+
364+
String region = awsCredentials.getAwsRegion();
365+
366+
// Should attempt to retrieve the region from AWS_REGION env var first.
367+
// Metadata server would return us-east-1b.
368+
assertEquals("region", region);
369+
}
370+
371+
@Test
372+
public void getAwsRegion_awsDefaultRegionEnvironmentVariable() throws IOException {
373+
TestEnvironmentProvider environmentProvider = new TestEnvironmentProvider();
374+
environmentProvider.setEnv("AWS_DEFAULT_REGION", "defaultRegion");
375+
376+
MockExternalAccountCredentialsTransportFactory transportFactory =
377+
new MockExternalAccountCredentialsTransportFactory();
378+
AwsCredentials awsCredentials =
379+
(AwsCredentials)
380+
AwsCredentials.newBuilder(AWS_CREDENTIAL)
381+
.setHttpTransportFactory(transportFactory)
382+
.setCredentialSource(buildAwsCredentialSource(transportFactory))
383+
.setEnvironmentProvider(environmentProvider)
384+
.build();
385+
386+
String region = awsCredentials.getAwsRegion();
387+
388+
// Should attempt to retrieve the region from DEFAULT_AWS_REGION before calling the metadata
389+
// server. Metadata server would return us-east-1b.
390+
assertEquals("defaultRegion", region);
391+
}
392+
393+
@Test
394+
public void getAwsRegion_metadataServer() throws IOException {
395+
MockExternalAccountCredentialsTransportFactory transportFactory =
396+
new MockExternalAccountCredentialsTransportFactory();
397+
AwsCredentials awsCredentials =
398+
(AwsCredentials)
399+
AwsCredentials.newBuilder(AWS_CREDENTIAL)
400+
.setHttpTransportFactory(transportFactory)
401+
.setCredentialSource(buildAwsCredentialSource(transportFactory))
402+
.build();
403+
404+
String region = awsCredentials.getAwsRegion();
405+
406+
// Should retrieve the region from the Metadata server.
407+
String expectedRegion =
408+
transportFactory
409+
.transport
410+
.getAwsRegion()
411+
.substring(0, transportFactory.transport.getAwsRegion().length() - 1);
412+
assertEquals(expectedRegion, region);
413+
}
414+
348415
@Test
349416
public void createdScoped_clonedCredentialWithAddedScopes() {
350417
AwsCredentials credentials =

‎oauth2_http/javatests/com/google/auth/oauth2/MockExternalAccountCredentialsTransport.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class MockExternalAccountCredentialsTransport extends MockHttpTransport {
7474
private static final String TOKEN_TYPE = "Bearer";
7575
private static final String ACCESS_TOKEN = "accessToken";
7676
private static final String SERVICE_ACCOUNT_ACCESS_TOKEN = "serviceAccountAccessToken";
77+
private static final String AWS_REGION = "us-east-1b";
7778
private static final Long EXPIRES_IN = 3600L;
7879

7980
private static final JsonFactory JSON_FACTORY = new GsonFactory();
@@ -120,7 +121,7 @@ public LowLevelHttpResponse execute() throws IOException {
120121
if (AWS_REGION_URL.equals(url)) {
121122
return new MockLowLevelHttpResponse()
122123
.setContentType("text/html")
123-
.setContent("us-east-1b");
124+
.setContent(AWS_REGION);
124125
}
125126
if (AWS_CREDENTIALS_URL.equals(url)) {
126127
return new MockLowLevelHttpResponse()
@@ -245,6 +246,10 @@ public String getAwsRegionUrl() {
245246
return AWS_REGION_URL;
246247
}
247248

249+
public String getAwsRegion() {
250+
return AWS_REGION;
251+
}
252+
248253
public String getStsUrl() {
249254
return STS_URL;
250255
}

0 commit comments

Comments
 (0)