Skip to content

Commit 7636c42

Browse files
committed
Add ec2InstanceProfileName additional changes:
- Refactor the code to resolve profile name
1 parent dc7e19f commit 7636c42

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/InstanceProfileCredentialsProvider.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private RefreshResult<AwsCredentials> refreshCredentials() {
205205
if (apiVersion == ApiVersion.UNKNOWN) {
206206
apiVersion = ApiVersion.LEGACY;
207207
return refreshCredentials();
208-
} else if (ec2InstanceProfileName == null && configProvider.ec2InstanceProfileName() == null) {
208+
} else if (resolveProfileName() == null) {
209209
// Resolved profile name is invalid, reset it and try again
210210
resolvedProfile = null;
211211

@@ -348,14 +348,16 @@ private boolean isInsecureFallbackDisabled() {
348348
return configProvider.isMetadataV1Disabled();
349349
}
350350

351-
private String[] getSecurityCredentials(String imdsHostname, String metadataToken) {
352-
if (ec2InstanceProfileName != null) {
353-
return new String[]{ec2InstanceProfileName};
354-
}
351+
private String resolveProfileName() {
352+
return ec2InstanceProfileName != null ?
353+
ec2InstanceProfileName :
354+
configProvider.ec2InstanceProfileName();
355+
}
355356

356-
String configuredProfileName = this.configProvider.ec2InstanceProfileName();
357-
if (configuredProfileName != null) {
358-
return new String[]{configuredProfileName};
357+
private String[] getSecurityCredentials(String imdsHostname, String metadataToken) {
358+
String profileName = resolveProfileName();
359+
if (profileName != null) {
360+
return new String[]{profileName};
359361
}
360362

361363
if (resolvedProfile != null) {
@@ -417,12 +419,9 @@ public interface Builder extends HttpCredentialsProvider.Builder<InstanceProfile
417419
* Configure the EC2 instance profile name to use for retrieving credentials.
418420
*
419421
* <p>When this is set, the provider will skip fetching the list of available instance profiles
420-
* and use this name directly. This can improve performance by reducing the number of calls to IMDS.
421-
*
422-
* <p>By default, this is not set and the provider will discover the instance profile name from IMDS.
422+
* and use this name directly.
423423
*
424424
* @param ec2InstanceProfileName The EC2 instance profile name to use
425-
* @return This builder for method chaining
426425
*/
427426
Builder ec2InstanceProfileName(String ec2InstanceProfileName);
428427

core/auth/src/test/java/software/amazon/awssdk/auth/credentials/InstanceProfileCredentialsProviderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,34 @@ void resolveCredentials_withConfigFileInstanceProfileName_skipsProfileDiscovery(
412412
WireMock.verify(0, getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)));
413413
}
414414

415+
@Test
416+
void resolveCredentials_withEnvironmentVariableInstanceProfileName_skipsProfileDiscovery() {
417+
String envVarProfileName = "env-var-profile";
418+
419+
try {
420+
environmentVariableHelper.set(SdkSystemSetting.AWS_EC2_INSTANCE_PROFILE_NAME.environmentVariable(), envVarProfileName);
421+
422+
stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody(TOKEN_STUB)));
423+
stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + envVarProfileName)).willReturn(aResponse().withBody(STUB_CREDENTIALS)));
424+
425+
InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build();
426+
AwsCredentials credentials = provider.resolveCredentials();
427+
428+
assertThat(credentials.accessKeyId()).isEqualTo("ACCESS_KEY_ID");
429+
assertThat(credentials.secretAccessKey()).isEqualTo("SECRET_ACCESS_KEY");
430+
431+
WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH))
432+
.withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600")));
433+
434+
WireMock.verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + envVarProfileName))
435+
.withHeader(TOKEN_HEADER, equalTo(TOKEN_STUB)));
436+
437+
WireMock.verify(0, getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)));
438+
} finally {
439+
environmentVariableHelper.reset();
440+
}
441+
}
442+
415443
@Test
416444
void resolveCredentials_withBlankInstanceProfileName_throwsException() {
417445
assertThatThrownBy(() -> InstanceProfileCredentialsProvider.builder()

0 commit comments

Comments
 (0)