2727import java .util .Collections ;
2828import java .util .Map ;
2929import java .util .Optional ;
30- import java .util .concurrent .atomic .AtomicReference ;
3130import java .util .function .Supplier ;
3231import software .amazon .awssdk .annotations .SdkPublicApi ;
3332import 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 }
0 commit comments