Skip to content

Commit f41795b

Browse files
committed
Replacing AtomicReference fields and updating test file
1 parent c5e41b5 commit f41795b

File tree

2 files changed

+14
-36
lines changed

2 files changed

+14
-36
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Collections;
2828
import java.util.Map;
2929
import java.util.Optional;
30-
import java.util.concurrent.atomic.AtomicReference;
3130
import java.util.function.Supplier;
3231
import software.amazon.awssdk.annotations.SdkPublicApi;
3332
import software.amazon.awssdk.annotations.SdkTestInternalApi;
@@ -83,8 +82,10 @@ private enum ApiVersion {
8382

8483
private static final String EC2_METADATA_TOKEN_TTL_HEADER = "x-aws-ec2-metadata-token-ttl-seconds";
8584
private static final String DEFAULT_TOKEN_TTL = "21600";
86-
private final AtomicReference<ApiVersion> apiVersion = new AtomicReference<>(ApiVersion.UNKNOWN);
87-
private final AtomicReference<String> resolvedProfile = new AtomicReference<>();
85+
86+
// These fields are accessed from methods called by CachedSupplier which provides thread safety through its ReentrantLock
87+
private ApiVersion apiVersion = ApiVersion.UNKNOWN;
88+
private String resolvedProfile = null;
8889

8990
private final Clock clock;
9091
private final String endpoint;
@@ -179,7 +180,7 @@ private RefreshResult<AwsCredentials> refreshCredentials() {
179180
} catch (Ec2MetadataClientException e) {
180181
if (e.statusCode() == 404) {
181182
log.debug(() -> "Resolved profile is no longer available. Resetting it and trying again.");
182-
resolvedProfile.set(null);
183+
resolvedProfile = null;
183184
return refreshCredentials();
184185
}
185186
throw SdkClientException.create("Failed to load credentials from IMDS.", e);
@@ -227,7 +228,7 @@ public String toString() {
227228
}
228229

229230
private String getSecurityCredentialsResource() {
230-
return apiVersion.get() == ApiVersion.LEGACY ?
231+
return apiVersion == ApiVersion.LEGACY ?
231232
SECURITY_CREDENTIALS_RESOURCE :
232233
SECURITY_CREDENTIALS_EXTENDED_RESOURCE;
233234
}
@@ -310,9 +311,8 @@ private boolean isInsecureFallbackDisabled() {
310311
}
311312

312313
private String[] getSecurityCredentials(String imdsHostname, String metadataToken) {
313-
String profile = resolvedProfile.get();
314-
if (profile != null) {
315-
return new String[]{profile};
314+
if (resolvedProfile != null) {
315+
return new String[]{resolvedProfile};
316316
}
317317

318318
String urlBase = getSecurityCredentialsResource();
@@ -332,12 +332,15 @@ private String[] getSecurityCredentials(String imdsHostname, String metadataToke
332332
throw SdkClientException.builder().message("Unable to load credentials path").build();
333333
}
334334

335-
apiVersion.compareAndSet(ApiVersion.UNKNOWN, ApiVersion.EXTENDED);
336-
resolvedProfile.set(securityCredentials[0]);
335+
if (apiVersion == ApiVersion.UNKNOWN) {
336+
apiVersion = ApiVersion.EXTENDED;
337+
}
338+
resolvedProfile = securityCredentials[0];
337339
return securityCredentials;
338340

339341
} catch (Ec2MetadataClientException e) {
340-
if (e.statusCode() == 404 && apiVersion.compareAndSet(ApiVersion.UNKNOWN, ApiVersion.LEGACY)) {
342+
if (e.statusCode() == 404 && apiVersion == ApiVersion.UNKNOWN) {
343+
apiVersion = ApiVersion.LEGACY;
341344
log.debug(() -> "Instance does not support IMDS extended API. Falling back to legacy API.");
342345
return getSecurityCredentials(imdsHostname, metadataToken);
343346
}

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,6 @@ void resolveCredentials_withImdsDisabled_returnsNoCredentials() {
133133
verify(0, getRequestedFor(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH)));
134134
}
135135

136-
137-
@Test
138-
void resolveCredentials_withInvalidProfile_throwsException() {
139-
String invalidProfile = "my-profile-0004";
140-
141-
wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH))
142-
.willReturn(aResponse().withBody(invalidProfile)));
143-
wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH))
144-
.willReturn(aResponse().withBody(invalidProfile)));
145-
146-
wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH + invalidProfile))
147-
.willReturn(aResponse().withStatus(404)));
148-
wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + invalidProfile))
149-
.willReturn(aResponse().withStatus(404)));
150-
151-
wireMockServer.stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH))
152-
.willReturn(aResponse().withBody(TOKEN_STUB)));
153-
154-
InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build();
155-
156-
assertThatThrownBy(() -> provider.resolveCredentials())
157-
.isInstanceOf(SdkClientException.class)
158-
.hasMessageContaining("Failed to load credentials from IMDS.");
159-
}
160-
161136
@Test
162137
void resolveCredentials_cachesProfile_maintainsAccountId() {
163138
String credentialsWithAccountId = String.format(

0 commit comments

Comments
 (0)