Skip to content

Commit 5d5b50a

Browse files
dscpinheironormj
andauthored
[V4] Re-introduce background refresh behavior (#4100)
* Rework the locking in the RefreshingAWSCredentials base class * Add backwardIncompatibilitiesToIgnore to change log * refactor: Re-introduce unit tests for background refresh * refactor: Update logging to remove duplicate messages and use correct timestamps * fix: Mark method as static * refactor: Address Copilot feedback * refactor: More test cases and documentation updates * Add locking documentation for RefreshingAWSCredentials --------- Co-authored-by: Norm Johanson <[email protected]>
1 parent cc0c6a1 commit 5d5b50a

File tree

6 files changed

+566
-95
lines changed

6 files changed

+566
-95
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"core": {
3+
"updateMinimum": true,
4+
"type": "minor",
5+
"changeLogMessages": [
6+
"Re-introduce background refresh of credentials during their preempt expiry period (https://github.com/aws/aws-sdk-net/issues/4024)"
7+
],
8+
"backwardIncompatibilitiesToIgnore": [
9+
"Amazon.Runtime.RefreshingAWSCredentials/MethodAdded",
10+
"Amazon.Runtime.RefreshingAWSCredentials/FieldTypeChanged"
11+
]
12+
}
13+
}

sdk/src/Core/Amazon.Runtime/Credentials/InstanceProfileAWSCredentials.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ protected override CredentialsRefreshState GenerateNewCredentials()
9191
// but try again to refresh them in 2 minutes.
9292
if (null != _currentRefreshState)
9393
{
94-
#pragma warning disable CS0612, CS0618 // Type or member is obsolete
95-
var newExpiryTime = AWSSDKUtils.CorrectedUtcNow + TimeSpan.FromMinutes(2);
96-
#pragma warning restore CS0612,CS0618 // Type or member is obsolete
97-
94+
var newExpiryTime = _timeProvider.CorrectedUtcNow + TimeSpan.FromMinutes(2);
9895
_currentRefreshState = new CredentialsRefreshState(_currentRefreshState.Credentials, newExpiryTime);
9996
return _currentRefreshState;
10097
}
@@ -107,10 +104,7 @@ protected override CredentialsRefreshState GenerateNewCredentials()
107104

108105
// use a custom refresh time
109106

110-
#pragma warning disable CS0612, CS0618 // Type or member is obsolete
111-
var newExpiryTime = AWSSDKUtils.CorrectedUtcNow + TimeSpan.FromMinutes(new Random().Next(5, 11));
112-
#pragma warning restore CS0612, CS0618 // Type or member is obsolete
113-
107+
var newExpiryTime = _timeProvider.CorrectedUtcNow + TimeSpan.FromMinutes(new Random().Next(5, 11));
114108
_currentRefreshState = new CredentialsRefreshState(newState.Credentials, newExpiryTime);
115109

116110
return _currentRefreshState;
@@ -175,7 +169,7 @@ protected override async Task<CredentialsRefreshState> GenerateNewCredentialsAsy
175169
// but try again to refresh them in 2 minutes.
176170
if (null != _currentRefreshState)
177171
{
178-
var newExpiryTime = AWSSDKUtils.CorrectedUtcNow + TimeSpan.FromMinutes(2);
172+
var newExpiryTime = _timeProvider.CorrectedUtcNow + TimeSpan.FromMinutes(2);
179173
_currentRefreshState = new CredentialsRefreshState(_currentRefreshState.Credentials, newExpiryTime);
180174
return _currentRefreshState;
181175
}
@@ -187,7 +181,7 @@ protected override async Task<CredentialsRefreshState> GenerateNewCredentialsAsy
187181
_logger.InfoFormat(_receivedExpiredCredentialsFromIMDS);
188182

189183
// use a custom refresh time
190-
var newExpiryTime = AWSSDKUtils.CorrectedUtcNow + TimeSpan.FromMinutes(new Random().Next(5, 11));
184+
var newExpiryTime = _timeProvider.CorrectedUtcNow + TimeSpan.FromMinutes(new Random().Next(5, 11));
191185
_currentRefreshState = new CredentialsRefreshState(newState.Credentials, newExpiryTime);
192186

193187
return _currentRefreshState;
@@ -396,7 +390,7 @@ private static Uri InfoUri
396390

397391
private CredentialsRefreshState GetEarlyRefreshState(CredentialsRefreshState state)
398392
{
399-
DateTime newExpiryTime = AWSSDKUtils.CorrectedUtcNow + _refreshAttemptPeriod + PreemptExpiryTime;
393+
DateTime newExpiryTime = _timeProvider.CorrectedUtcNow + _refreshAttemptPeriod + PreemptExpiryTime;
400394

401395
// Use this only if the time is earlier than the default expiration time
402396
if (newExpiryTime > state.Expiration)

0 commit comments

Comments
 (0)