Skip to content

Commit 5a14498

Browse files
committed
Addressed comments.
1 parent cabb263 commit 5a14498

File tree

14 files changed

+120
-140
lines changed

14 files changed

+120
-140
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
public interface HttpCredentialsProvider extends AwsCredentialsProvider, SdkAutoCloseable {
2929
interface Builder<TypeToBuildT extends HttpCredentialsProvider, BuilderT extends Builder<?, ?>> {
3030
/**
31-
* Configure whether this provider should fetch credentials asynchronously in the background. If this is true, threads are
32-
* less likely to block when {@link #resolveCredentials()} is called, but additional resources are used to maintain the
33-
* provider.
31+
* Configure whether the provider should fetch credentials asynchronously in the background. If this is true,
32+
* threads are less likely to block when credentials are loaded, but additional resources are used to maintain
33+
* the provider.
3434
*
35-
* <p>
36-
* By default, this is disabled.
35+
* <p>By default, this is disabled.</p>
3736
*/
3837
BuilderT asyncCredentialUpdateEnabled(Boolean asyncCredentialUpdateEnabled);
3938

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

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
package software.amazon.awssdk.auth.credentials;
1717

1818
import static java.time.temporal.ChronoUnit.MINUTES;
19-
import static java.time.temporal.ChronoUnit.SECONDS;
2019
import static software.amazon.awssdk.utils.ComparableUtils.minimum;
2120

2221
import java.io.IOException;
2322
import java.net.URI;
2423
import java.time.Clock;
24+
import java.time.Duration;
2525
import java.time.Instant;
2626
import java.util.Collections;
2727
import java.util.Map;
@@ -151,7 +151,7 @@ private RefreshResult<AwsCredentials> refreshCredentials() {
151151
// Choose whether to report this failure at the debug or warn level based on how much time is left on the
152152
// credentials before expiration.
153153
Supplier<String> errorMessage = () -> "Failure encountered when attempting to refresh credentials from IMDS.";
154-
Instant fifteenMinutesFromNow = Instant.now().plus(15, MINUTES);
154+
Instant fifteenMinutesFromNow = clock.instant().plus(15, MINUTES);
155155
if (expiration.isBefore(fifteenMinutesFromNow)) {
156156
log.warn(errorMessage, e);
157157
} else {
@@ -164,7 +164,7 @@ private RefreshResult<AwsCredentials> refreshCredentials() {
164164
}
165165

166166
return RefreshResult.builder(credentials.getAwsCredentials())
167-
.staleTime(null) // Allow use of expired credentials - they may still work
167+
.staleTime(Instant.MAX) // Allow use of expired credentials - they may still work
168168
.prefetchTime(prefetchTime(credentials.getExpiration().orElse(null)))
169169
.build();
170170
}
@@ -180,35 +180,18 @@ private boolean isLocalCredentialLoadingDisabled() {
180180
private Instant prefetchTime(Instant expiration) {
181181
Instant now = clock.instant();
182182

183-
// If expiration time doesn't exist, refresh in 60 minutes
184183
if (expiration == null) {
185184
return now.plus(60, MINUTES);
186185
}
187186

188-
// If expiration time is 60+ minutes from now, refresh in 30 minutes.
189-
Instant sixtyMinutesBeforeExpiration = expiration.minus(60, MINUTES);
190-
if (now.isBefore(sixtyMinutesBeforeExpiration)) {
191-
return now.plus(30, MINUTES);
187+
Duration timeUntilExpiration = Duration.between(now, expiration);
188+
if (timeUntilExpiration.isNegative()) {
189+
log.warn(() -> "IMDS credential expiration has been extended due to an IMDS availability outage. A refresh "
190+
+ "of these credentials will be attempted again in ~5 minutes.");
191+
return now.plus(5, MINUTES);
192192
}
193193

194-
// If expiration time is 15 minutes or more from now, refresh in 10 minutes.
195-
Instant fifteenMinutesBeforeExpiration = expiration.minus(15, MINUTES);
196-
if (now.isBefore(fifteenMinutesBeforeExpiration)) {
197-
return now.plus(10, MINUTES);
198-
}
199-
200-
// If expiration time is 0.25-15 minutes from now, refresh in 5 minutes, or 15 seconds before expiration, whichever is
201-
// sooner.
202-
Instant fifteenSecondsBeforeExpiration = expiration.minus(15, SECONDS);
203-
if (now.isBefore(fifteenSecondsBeforeExpiration)) {
204-
return minimum(now.plus(5, MINUTES), fifteenSecondsBeforeExpiration);
205-
}
206-
207-
// These credentials are expired. Try refreshing again in 5 minutes. We can't be more aggressive than that, because we
208-
// don't want to overload the IMDS endpoint.
209-
log.warn(() -> "IMDS credential expiration has been extended due to an IMDS availability outage. A refresh "
210-
+ "of these credentials will be attempted again in 5 minutes.");
211-
return now.plus(5, MINUTES);
194+
return now.plus(minimum(timeUntilExpiration.abs().dividedBy(2), Duration.ofMinutes(5)));
212195
}
213196

214197
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ private Builder(ProcessCredentialsProvider provider) {
249249
}
250250

251251
/**
252-
* Configure whether the provider should fetch credentials asynchronously in the background. If this is true, threads are
253-
* less likely to block when credentials are loaded, but additional resources are used to maintain the provider.
252+
* Configure whether the provider should fetch credentials asynchronously in the background. If this is true,
253+
* threads are less likely to block when credentials are loaded, but additional resources are used to maintain
254+
* the provider.
254255
*
255256
* <p>By default, this is disabled.</p>
256257
*/

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,18 +361,18 @@ public void resolveCredentials_callsImdsIfCredentialsWithin5MinutesOfExpiration(
361361
stubCredentialsResponse(aResponse().withBody(successfulCredentialsResponse1));
362362
AwsCredentials credentials24HoursAgo = credentialsProvider.resolveCredentials();
363363

364-
// Set the time to 3 minutes before expiration, and fail to call IMDS
365-
clock.time = now.minus(3, MINUTES);
364+
// Set the time to 10 minutes before expiration, and fail to call IMDS
365+
clock.time = now.minus(10, MINUTES);
366366
stubCredentialsResponse(aResponse().withStatus(500));
367-
AwsCredentials credentials3MinutesAgo = credentialsProvider.resolveCredentials();
367+
AwsCredentials credentials10MinutesAgo = credentialsProvider.resolveCredentials();
368368

369369
// Set the time to 10 seconds before expiration, and verify that we still call IMDS to try to get credentials in at the
370370
// last moment before expiration
371371
clock.time = now.minus(10, SECONDS);
372372
stubCredentialsResponse(aResponse().withBody(successfulCredentialsResponse2));
373373
AwsCredentials credentials10SecondsAgo = credentialsProvider.resolveCredentials();
374374

375-
assertThat(credentials24HoursAgo).isEqualTo(credentials3MinutesAgo);
375+
assertThat(credentials24HoursAgo).isEqualTo(credentials10MinutesAgo);
376376
assertThat(credentials24HoursAgo.secretAccessKey()).isEqualTo("SECRET_ACCESS_KEY");
377377
assertThat(credentials10SecondsAgo.secretAccessKey()).isEqualTo("SECRET_ACCESS_KEY2");
378378
}

services/sso/src/main/java/software/amazon/awssdk/services/sso/auth/SsoCredentialsProvider.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,16 @@
3838
import software.amazon.awssdk.utils.cache.RefreshResult;
3939

4040
/**
41-
* <p>
42-
* An implementation of {@link AwsCredentialsProvider} that is extended within this package to provide support for
43-
* periodically updating session credentials. This credential provider maintains a {@link Supplier<GetRoleCredentialsRequest>}
44-
* for a {@link SsoClient#getRoleCredentials(Consumer)} call to retrieve the credentials needed.
45-
* </p>
41+
* An implementation of {@link AwsCredentialsProvider} that periodically sends a {@link GetRoleCredentialsRequest} to the AWS
42+
* Single Sign-On Service to maintain short-lived sessions to use for authentication. These sessions are updated using a single
43+
* calling thread (by default) or asynchronously (if {@link Builder#asyncCredentialUpdateEnabled(Boolean)} is set).
4644
*
47-
* <p>
48-
* While creating the {@link GetRoleCredentialsRequest}, an access token is needed to be resolved from a token file.
49-
* In default, the token is assumed unexpired, and if it's expired then an {@link ExpiredTokenException} will be thrown.
50-
* If the users want to change the behavior of this, please implement your own token resolving logic and override the
51-
* {@link Builder#refreshRequest).
52-
* </p>
45+
* If the credentials are not successfully updated before expiration, calls to {@link #resolveCredentials()} will block until
46+
* they are updated successfully.
5347
*
54-
* <p>
55-
* When credentials get close to expiration, this class will attempt to update them asynchronously. If the credentials
56-
* end up expiring, this class will block all calls to {@link #resolveCredentials()} until the credentials can be updated.
57-
* </p>
48+
* Users of this provider must {@link #close()} it when they are finished using it.
49+
*
50+
* This is created using {@link SsoCredentialsProvider#builder()}.
5851
*/
5952
@SdkPublicApi
6053
public final class SsoCredentialsProvider implements AwsCredentialsProvider, SdkAutoCloseable,
@@ -186,7 +179,10 @@ public interface Builder extends CopyableBuilder<Builder, SsoCredentialsProvider
186179

187180
/**
188181
* Configure the amount of time, relative to SSO session token expiration, that the cached credentials are considered
189-
* close to stale and should be updated. See {@link #asyncCredentialUpdateEnabled}.
182+
* close to stale and should be updated.
183+
*
184+
* Prefetch updates will occur between the specified time and the stale time of the provider. Prefetch updates may be
185+
* asynchronous. See {@link #asyncCredentialUpdateEnabled}.
190186
*
191187
* <p>By default, this is 5 minutes.</p>
192188
*/

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsAssumeRoleCredentialsProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030

3131
/**
3232
* An implementation of {@link AwsCredentialsProvider} that periodically sends an {@link AssumeRoleRequest} to the AWS
33-
* Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated asynchronously
34-
* in the background as they get close to expiring. If the credentials are not successfully updated asynchronously in the
35-
* background, calls to {@link #resolveCredentials()} will begin to block in an attempt to update the credentials synchronously.
33+
* Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated using a single
34+
* calling thread (by default) or asynchronously (if {@link Builder#asyncCredentialUpdateEnabled(Boolean)} is set).
3635
*
37-
* This provider creates a thread in the background to periodically update credentials. If this provider is no longer needed,
38-
* the background thread can be shut down using {@link #close()}.
36+
* If the credentials are not successfully updated before expiration, calls to {@link #resolveCredentials()} will block until
37+
* they are updated successfully.
38+
*
39+
* Users of this provider must {@link #close()} it when they are finished using it.
3940
*
4041
* This is created using {@link StsAssumeRoleCredentialsProvider#builder()}.
4142
*/

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsAssumeRoleWithSamlCredentialsProvider.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
3030

3131
/**
32-
* An implementation of {@link AwsCredentialsProvider} that periodically sends a {@link AssumeRoleWithSamlRequest}
33-
* to the AWS Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated
34-
* asynchronously in the background as they get close to expiring. If the credentials are not successfully updated asynchronously
35-
* in the background, calls to {@link #resolveCredentials()} will begin to block in an attempt to update the credentials
36-
* synchronously.
32+
* An implementation of {@link AwsCredentialsProvider} that periodically sends an {@link AssumeRoleWithSamlRequest} to the AWS
33+
* Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated using a single
34+
* calling thread (by default) or asynchronously (if {@link Builder#asyncCredentialUpdateEnabled(Boolean)} is set).
3735
*
38-
* This provider creates a thread in the background to periodically update credentials. If this provider is no longer needed,
39-
* the background thread can be shut down using {@link #close()}.
36+
* If the credentials are not successfully updated before expiration, calls to {@link #resolveCredentials()} will block until
37+
* they are updated successfully.
38+
*
39+
* Users of this provider must {@link #close()} it when they are finished using it.
4040
*
4141
* This is created using {@link StsAssumeRoleWithSamlCredentialsProvider#builder()}.
4242
*/

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsAssumeRoleWithWebIdentityCredentialsProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
3131

3232
/**
33-
* An implementation of {@link AwsCredentialsProvider} that periodically sends a {@link AssumeRoleWithWebIdentityRequest}
34-
* to the AWS Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated
35-
* asynchronously in the background as they get close to expiring. If the credentials are not successfully updated asynchronously
36-
* in the background, calls to {@link #resolveCredentials()} will begin to block in an attempt to update the credentials
37-
* synchronously.
33+
* An implementation of {@link AwsCredentialsProvider} that periodically sends an {@link AssumeRoleWithWebIdentityRequest} to the
34+
* AWS Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated using a
35+
* single calling thread (by default) or asynchronously (if {@link Builder#asyncCredentialUpdateEnabled(Boolean)} is set).
3836
*
39-
* This provider creates a thread in the background to periodically update credentials. If this provider is no longer needed,
40-
* the background thread can be shut down using {@link #close()}.
37+
* If the credentials are not successfully updated before expiration, calls to {@link #resolveCredentials()} will block until
38+
* they are updated successfully.
4139
*
42-
* This is created using {@link StsAssumeRoleWithWebIdentityCredentialsProvider#builder()}.
40+
* Users of this provider must {@link #close()} it when they are finished using it.
41+
*
42+
* This is created using {@link #builder()}.
4343
*/
4444
@SdkPublicApi
4545
@ThreadSafe

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsCredentialsProvider.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@
3737

3838
/**
3939
* An implementation of {@link AwsCredentialsProvider} that is extended within this package to provide support for periodically-
40-
* updating session credentials. When credentials get close to expiration, this class will attempt to update them asynchronously
41-
* using {@link #getUpdatedCredentials(StsClient)}. If the credentials end up expiring, this class will block all calls to
42-
* {@link #resolveCredentials()} until the credentials can be updated.
40+
* updating session credentials.
41+
*
42+
* When credentials get close to expiration, this class will attempt to update them automatically either with a single calling
43+
* thread (by default) or asynchronously (if {@link #asyncCredentialUpdateEnabled} is true). If the credentials expire, this
44+
* class will block all calls to {@link #resolveCredentials()} until the credentials are updated.
45+
*
46+
* Users of this provider must {@link #close()} it when they are finished using it.
4347
*/
4448
@ThreadSafe
4549
@SdkInternalApi
@@ -49,12 +53,12 @@ abstract class StsCredentialsProvider implements AwsCredentialsProvider, SdkAuto
4953
private static final Duration DEFAULT_PREFETCH_TIME = Duration.ofMinutes(5);
5054

5155
/**
52-
* The STS client that should be used for periodically updating the session credentials in the background.
56+
* The STS client that should be used for periodically updating the session credentials.
5357
*/
5458
final StsClient stsClient;
5559

5660
/**
57-
* The session cache that will update the credentials asynchronously in the background when they get close to expiring.
61+
* The session cache that handles automatically updating the credentials when they get close to expiring.
5862
*/
5963
private final CachedSupplier<SessionCredentialsHolder> sessionCache;
6064

@@ -174,7 +178,7 @@ public B asyncCredentialUpdateEnabled(Boolean asyncCredentialUpdateEnabled) {
174178

175179
/**
176180
* Configure the amount of time, relative to STS token expiration, that the cached credentials are considered
177-
* stale and should no longer be used. All threads will block until the value is updated.
181+
* stale and must be updated. All threads will block until the value is updated.
178182
*
179183
* <p>By default, this is 1 minute.</p>
180184
*/
@@ -186,7 +190,10 @@ public B staleTime(Duration staleTime) {
186190

187191
/**
188192
* Configure the amount of time, relative to STS token expiration, that the cached credentials are considered
189-
* close to stale and should be updated. See {@link #asyncCredentialUpdateEnabled}.
193+
* close to stale and should be updated.
194+
*
195+
* Prefetch updates will occur between the specified time and the stale time of the provider. Prefetch updates may be
196+
* asynchronous. See {@link #asyncCredentialUpdateEnabled}.
190197
*
191198
* <p>By default, this is 5 minutes.</p>
192199
*/

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsGetFederationTokenCredentialsProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
2929

3030
/**
31-
* An implementation of {@link AwsCredentialsProvider} that periodically sends a {@link GetFederationTokenRequest} to the
32-
* AWS Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated
33-
* asynchronously in the background as they get close to expiring. If the credentials are not successfully updated asynchronously
34-
* in the background, calls to {@link #resolveCredentials()} will begin to block in an attempt to update the credentials
35-
* synchronously.
31+
* An implementation of {@link AwsCredentialsProvider} that periodically sends a {@link GetFederationTokenRequest} to the AWS
32+
* Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated using a single
33+
* calling thread (by default) or asynchronously (if {@link Builder#asyncCredentialUpdateEnabled(Boolean)} is set).
3634
*
37-
* This provider creates a thread in the background to periodically update credentials. If this provider is no longer needed,
38-
* the background thread can be shut down using {@link #close()}.
35+
* If the credentials are not successfully updated before expiration, calls to {@link #resolveCredentials()} will block until
36+
* they are updated successfully.
3937
*
40-
* This is created using {@link StsGetFederationTokenCredentialsProvider#builder()}.
38+
* Users of this provider must {@link #close()} it when they are finished using it.
39+
*
40+
* This is created using {@link #builder()}.
4141
*/
4242
@SdkPublicApi
4343
@ThreadSafe

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsGetSessionTokenCredentialsProvider.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929

3030
/**
3131
* An implementation of {@link AwsCredentialsProvider} that periodically sends a {@link GetSessionTokenRequest} to the AWS
32-
* Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated asynchronously
33-
* in the background as they get close to expiring. If the credentials are not successfully updated asynchronously in the
34-
* background, calls to {@link #resolveCredentials()} will begin to block in an attempt to update the credentials synchronously.
32+
* Security Token Service to maintain short-lived sessions to use for authentication. These sessions are updated using a single
33+
* calling thread (by default) or asynchronously (if {@link Builder#asyncCredentialUpdateEnabled(Boolean)} is set).
3534
*
36-
* This provider creates a thread in the background to periodically update credentials. If this provider is no longer needed,
37-
* the background thread can be shut down using {@link #close()}.
35+
* If the credentials are not successfully updated before expiration, calls to {@link #resolveCredentials()} will block until
36+
* they are updated successfully.
3837
*
39-
* This is created using {@link StsGetSessionTokenCredentialsProvider#builder()}.
38+
* Users of this provider must {@link #close()} it when they are finished using it.
39+
*
40+
* This is created using {@link #builder()}.
4041
*/
4142
@SdkPublicApi
4243
@ThreadSafe

0 commit comments

Comments
 (0)