Skip to content

Commit ce982b0

Browse files
ppittleashovlin
authored andcommitted
Change refresh window to 5-10 minutes. Adjust refresh strategy to immediately refresh the firs time an expired IMDS cred is encountered.
1 parent eea2f1b commit ce982b0

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ internal class DefaultInstanceProfileAWSCredentials : AWSCredentials, IDisposabl
4141
private const string FailedToGetCredentialsMessage = "Failed to retrieve credentials from EC2 Instance Metadata Service.";
4242
private static readonly TimeSpan _credentialsLockTimeout = TimeSpan.FromSeconds(5);
4343

44+
/// <summary>
45+
/// Control flag: in the event IMDS returns an expired credential, a refresh must be immediately
46+
/// retried, if it continues to fail, then retry every 5-10 minutes.
47+
/// </summary>
48+
private static volatile bool _imdsRefreshFailed = false;
49+
4450
private const string _usingExpiredCredentialsFromIMDS =
4551
"Attempting credential expiration extension due to a credential service availability issue. " +
46-
"A refresh of these credentials will be attempted again in 5-15 minutes.";
52+
"A refresh of these credentials will be attempted again in 5-10 minutes.";
4753

4854
private static DefaultInstanceProfileAWSCredentials _instance;
4955

@@ -94,12 +100,24 @@ public override ImmutableCredentials GetCredentials()
94100
{
95101
if (null != _lastRetrievedCredentials)
96102
{
103+
if (_lastRetrievedCredentials.IsExpiredWithin(TimeSpan.Zero) &&
104+
!_imdsRefreshFailed)
105+
{
106+
// this is the first failure - immediately try to renew
107+
_imdsRefreshFailed = true;
108+
_lastRetrievedCredentials = FetchCredentials();
109+
}
110+
97111
// if credentials are expired, we'll still return them, but log a message about
98112
// them being expired.
99113
if (_lastRetrievedCredentials.IsExpiredWithin(TimeSpan.Zero))
100114
{
101115
_logger.InfoFormat(_usingExpiredCredentialsFromIMDS);
102116
}
117+
else
118+
{
119+
_imdsRefreshFailed = false;
120+
}
103121

104122
return _lastRetrievedCredentials?.Credentials.Copy();
105123
}
@@ -121,12 +139,24 @@ public override ImmutableCredentials GetCredentials()
121139
_lastRetrievedCredentials = FetchCredentials();
122140
}
123141

142+
if (_lastRetrievedCredentials.IsExpiredWithin(TimeSpan.Zero) &&
143+
!_imdsRefreshFailed)
144+
{
145+
// this is the first failure - immediately try to renew
146+
_imdsRefreshFailed = true;
147+
_lastRetrievedCredentials = FetchCredentials();
148+
}
149+
124150
// if credentials are expired, we'll still return them, but log a message about
125151
// them being expired.
126152
if (_lastRetrievedCredentials.IsExpiredWithin(TimeSpan.Zero))
127153
{
128154
_logger.InfoFormat(_usingExpiredCredentialsFromIMDS);
129155
}
156+
else
157+
{
158+
_imdsRefreshFailed = false;
159+
}
130160

131161
credentials = _lastRetrievedCredentials.Credentials?.Copy();
132162
}
@@ -169,10 +199,24 @@ private void RenewCredentials(object unused)
169199
// would remain unchanged and would continue to be returned in GetCredentials()
170200
_lastRetrievedCredentials = FetchCredentials();
171201

202+
// check for a first time failure
203+
if (!_imdsRefreshFailed &&
204+
_lastRetrievedCredentials.IsExpiredWithin(TimeSpan.Zero))
205+
{
206+
// this is the first failure - immediately try to renew
207+
_imdsRefreshFailed = true;
208+
_lastRetrievedCredentials = FetchCredentials();
209+
}
210+
211+
// first failure refresh failed OR subsequent refresh failed.
172212
if (_lastRetrievedCredentials.IsExpiredWithin(TimeSpan.Zero))
173213
{
174214
// relax the refresh rate to at least 5 minutes
175-
refreshRate = TimeSpan.FromMinutes(new Random().Next(5, 16));
215+
refreshRate = TimeSpan.FromMinutes(new Random().Next(5, 11));
216+
}
217+
else
218+
{
219+
_imdsRefreshFailed = false;
176220
}
177221
}
178222
catch (OperationCanceledException e)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class InstanceProfileAWSCredentials : URIBasedRefreshingCredentialHelper
4444

4545
private const string _receivedExpiredCredentialsFromIMDS =
4646
"Attempting credential expiration extension due to a credential service availability issue. " +
47-
"A refresh of these credentials will be attempted again in 5-15 minutes.";
47+
"A refresh of these credentials will be attempted again in 5-10 minutes.";
4848

4949
private Logger _logger;
5050

@@ -107,7 +107,7 @@ protected override CredentialsRefreshState GenerateNewCredentials()
107107
// use a custom refresh time
108108

109109
#pragma warning disable CS0612 // Type or member is obsolete
110-
var newExpiryTime = AWSSDKUtils.CorrectedUtcNow.ToLocalTime() + TimeSpan.FromMinutes(new Random().Next(5, 16));
110+
var newExpiryTime = AWSSDKUtils.CorrectedUtcNow.ToLocalTime() + TimeSpan.FromMinutes(new Random().Next(5, 11));
111111
#pragma warning restore CS0612 // Type or member is obsolete
112112

113113
_currentRefreshState = new CredentialsRefreshState(newState.Credentials.Copy(), newExpiryTime);

0 commit comments

Comments
 (0)