|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.auth.credentials;
|
17 | 17 |
|
18 |
| -import static java.time.temporal.ChronoUnit.HOURS; |
19 | 18 | import static java.time.temporal.ChronoUnit.MINUTES;
|
| 19 | +import static java.time.temporal.ChronoUnit.SECONDS; |
| 20 | +import static software.amazon.awssdk.utils.ComparableUtils.minimum; |
20 | 21 |
|
21 | 22 | import java.io.IOException;
|
22 | 23 | import java.net.URI;
|
@@ -156,24 +157,39 @@ private boolean isLocalCredentialLoadingDisabled() {
|
156 | 157 |
|
157 | 158 | private Instant prefetchTime(Instant expiration) {
|
158 | 159 | Instant now = clock.instant();
|
159 |
| - Instant oneHourFromNow = now.plus(1, HOURS); |
160 | 160 |
|
161 |
| - // If expiration time is infinite or farther out than an hour, wait an hour before refreshing |
162 |
| - if (expiration == null || expiration.isAfter(oneHourFromNow)) { |
163 |
| - return oneHourFromNow; |
| 161 | + // If expiration time doesn't exist, refresh in 60 minutes |
| 162 | + if (expiration == null) { |
| 163 | + return now.plus(60, MINUTES); |
164 | 164 | }
|
165 | 165 |
|
166 |
| - // If expiration time is within 15 minutes (or in the past), wait 15 minutes and warn the customer that they'll be using |
167 |
| - // expired credentials. |
168 |
| - Instant fifteenMinutesFromNow = now.plus(15, MINUTES); |
169 |
| - if (expiration.isBefore(fifteenMinutesFromNow)) { |
170 |
| - log.warn(() -> "IMDS credential expiration has been extended due to an IMDS availability outage. A refresh" |
171 |
| - + " of these credentials will be attempted again in 15 minutes."); |
172 |
| - return fifteenMinutesFromNow; |
| 166 | + // If expiration time is 60+ minutes from now, refresh in 60 minutes or 60 minutes before expiration, whichever is |
| 167 | + // sooner. This is the average case, where customers are using IMDS and there is no IMDS outage. |
| 168 | + Instant sixtyMinutesBeforeExpiration = expiration.minus(60, MINUTES); |
| 169 | + if (now.isBefore(sixtyMinutesBeforeExpiration)) { |
| 170 | + return minimum(sixtyMinutesBeforeExpiration, now.plus(60, MINUTES)); |
173 | 171 | }
|
174 | 172 |
|
175 |
| - // Otherwise, just refresh 15 minutes before the credentials expire. |
176 |
| - return expiration.minus(15, MINUTES); |
| 173 | + // If expiration time is 5-60 minutes from now, refresh in 30 minutes or 5 minutes before expiration, whatever is |
| 174 | + // sooner. This is an unusual case: IMDS is either having an outage or the customer is using a mock IMDS with shorter |
| 175 | + // default session durations. |
| 176 | + Instant fiveMinutesBeforeExpiration = expiration.minus(5, MINUTES); |
| 177 | + if (now.isBefore(fiveMinutesBeforeExpiration)) { |
| 178 | + return minimum(fiveMinutesBeforeExpiration, now.plus(30, MINUTES)); |
| 179 | + } |
| 180 | + |
| 181 | + // If expiration time is 0.25-5 minutes from now, refresh 15 seconds before expiration. This is an unusual case: IMDS is |
| 182 | + // either having an outage or the customer is using a mock IMDS with very aggressive session durations. |
| 183 | + Instant fifteenSecondsBeforeExpiration = expiration.minus(15, SECONDS); |
| 184 | + if (now.isBefore(fifteenSecondsBeforeExpiration)) { |
| 185 | + return fifteenSecondsBeforeExpiration; |
| 186 | + } |
| 187 | + |
| 188 | + // These credentials are expired. Try refreshing again in 5 minutes. We can't be more aggressive than that, because we |
| 189 | + // don't want to overload the IMDS endpoint. |
| 190 | + log.warn(() -> "IMDS credential expiration has been extended due to an IMDS availability outage. A refresh " |
| 191 | + + "of these credentials will be attempted again in 5 minutes."); |
| 192 | + return now.plus(5, MINUTES); |
177 | 193 | }
|
178 | 194 |
|
179 | 195 | @Override
|
|
0 commit comments