Skip to content

Commit 80c45bb

Browse files
dscpinheiromuhammad-othman
authored andcommitted
Add feature IDs for credential providers (#3717)
1 parent b761ae5 commit 80c45bb

19 files changed

+197
-109
lines changed

sdk/src/Core/Amazon.Runtime/CredentialManagement/AWSCredentialsFactory.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15-
using Amazon.Runtime.Internal;
1615
using Amazon.Runtime.CredentialManagement.Internal;
16+
using Amazon.Runtime.Credentials.Internal;
17+
using Amazon.Runtime.Internal.Settings;
18+
using Amazon.Runtime.Internal.UserAgent;
1719
using Amazon.Util;
20+
using Amazon.Util.Internal;
1821
using System;
1922
using System.Collections.Generic;
2023
using System.Globalization;
2124
using System.IO;
2225
using System.Linq;
23-
using Amazon.Runtime.Credentials.Internal;
24-
using Amazon.Runtime.Internal.Settings;
25-
using Amazon.Util.Internal;
2626

2727
namespace Amazon.Runtime.CredentialManagement
2828
{
@@ -202,7 +202,9 @@ private static AWSCredentials GetAWSCredentialsInternal(
202202
switch (profileType)
203203
{
204204
case CredentialProfileType.Basic:
205-
return new BasicAWSCredentials(options.AccessKey, options.SecretKey, options.AwsAccountId);
205+
var basicCredentials = new BasicAWSCredentials(options.AccessKey, options.SecretKey, options.AwsAccountId);
206+
basicCredentials.FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_PROFILE);
207+
return basicCredentials;
206208
case CredentialProfileType.Session:
207209
return new SessionAWSCredentials(options.AccessKey, options.SecretKey, options.Token, options.AwsAccountId);
208210
case CredentialProfileType.AssumeRole:
@@ -249,7 +251,11 @@ private static AWSCredentials GetAWSCredentialsInternal(
249251
ExternalId = options.ExternalID,
250252
MfaSerialNumber = options.MfaSerial
251253
};
252-
return new AssumeRoleAWSCredentials(sourceCredentials, options.RoleArn, roleSessionName, assumeRoleOptions);
254+
255+
var assumeRoleCredentials = new AssumeRoleAWSCredentials(sourceCredentials, options.RoleArn, roleSessionName, assumeRoleOptions);
256+
assumeRoleCredentials.FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_PROFILE_SOURCE_PROFILE);
257+
return assumeRoleCredentials;
258+
253259
case CredentialProfileType.AssumeRoleCredentialSource:
254260
case CredentialProfileType.AssumeRoleCredentialSourceSessionName:
255261
// get credentials specified by credentialSource
@@ -269,10 +275,16 @@ private static AWSCredentials GetAWSCredentialsInternal(
269275

270276
roleSessionName = options.RoleSessionName ?? RoleSessionNamePrefix + AWSSDKUtils.CorrectedUtcNow.Ticks;
271277
assumeRoleOptions = new AssumeRoleAWSCredentialsOptions();
272-
return new AssumeRoleAWSCredentials(sourceCredentials, options.RoleArn, roleSessionName, assumeRoleOptions);
278+
279+
var assumeRoleSourceCredentials = new AssumeRoleAWSCredentials(sourceCredentials, options.RoleArn, roleSessionName, assumeRoleOptions);
280+
assumeRoleSourceCredentials.FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_PROFILE_NAMED_PROVIDER);
281+
return assumeRoleSourceCredentials;
282+
273283
case CredentialProfileType.AssumeRoleWithWebIdentity:
274284
case CredentialProfileType.AssumeRoleWithWebIdentitySessionName:
275-
return new AssumeRoleWithWebIdentityCredentials(options.WebIdentityTokenFile, options.RoleArn, options.RoleSessionName);
285+
var assumeRoleWebIdentityCredentials = new AssumeRoleWithWebIdentityCredentials(options.WebIdentityTokenFile, options.RoleArn, options.RoleSessionName);
286+
assumeRoleWebIdentityCredentials.FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_PROFILE_STS_WEB_ID_TOKEN);
287+
return assumeRoleWebIdentityCredentials;
276288

277289
case CredentialProfileType.SSO:
278290
{
@@ -282,11 +294,11 @@ private static AWSCredentials GetAWSCredentialsInternal(
282294
Scopes = options.SsoRegistrationScopes?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList()
283295
};
284296

285-
return new SSOAWSCredentials(
286-
options.SsoAccountId, options.SsoRegion,
287-
options.SsoRoleName, options.SsoStartUrl,
288-
ssoCredentialsOptions
289-
);
297+
var isLegacyFormat = string.IsNullOrEmpty(options.SsoSession);
298+
var ssoCredentials = new SSOAWSCredentials(options.SsoAccountId, options.SsoRegion, options.SsoRoleName, options.SsoStartUrl, ssoCredentialsOptions);
299+
ssoCredentials.FeatureIdSources.Add(isLegacyFormat ? UserAgentFeatureId.CREDENTIALS_PROFILE_SSO_LEGACY : UserAgentFeatureId.CREDENTIALS_PROFILE_SSO);
300+
301+
return ssoCredentials;
290302
}
291303

292304
case CredentialProfileType.SAMLRole:
@@ -308,7 +320,9 @@ private static AWSCredentials GetAWSCredentialsInternal(
308320
return ThrowOrReturnNull("Federated credentials are not available on this platform.", null, throwIfInvalid);
309321
}
310322
case CredentialProfileType.CredentialProcess:
311-
return new ProcessAWSCredentials(options.CredentialProcess, options.AwsAccountId);
323+
var processCredentials = new ProcessAWSCredentials(options.CredentialProcess, options.AwsAccountId);
324+
processCredentials.FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_PROFILE_PROCESS);
325+
return processCredentials;
312326

313327
default:
314328
var defaultMessage = profileName == null

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
1716
using Amazon.Runtime.Identity;
17+
using Amazon.Runtime.Internal.UserAgent;
18+
using System.Collections.Generic;
1819

1920
namespace Amazon.Runtime
2021
{
@@ -23,6 +24,16 @@ namespace Amazon.Runtime
2324
/// </summary>
2425
public abstract class AWSCredentials : BaseIdentity
2526
{
27+
/// <summary>
28+
/// Internal property that can be used to specify how this instance of AWS credentials were resolved.
29+
/// </summary>
30+
/// <remarks>
31+
/// Credential providers MUST add to this property to have their specific feature ID tracked.
32+
/// <para />
33+
/// If empty, no value will be included in the user agent header.
34+
/// </remarks>
35+
internal HashSet<UserAgentFeatureId> FeatureIdSources { get; set; } = new();
36+
2637
/// <summary>
2738
/// Returns a copy of ImmutableCredentials
2839
/// </summary>

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
* permissions and limitations under the License.
1414
*/
1515
using Amazon.Runtime.Internal;
16+
using Amazon.Runtime.Internal.UserAgent;
1617
using Amazon.Runtime.Internal.Util;
1718
using Amazon.Runtime.SharedInterfaces;
1819
using Amazon.RuntimeDependencies;
1920
using Amazon.Util.Internal;
2021
using System;
22+
using System.Diagnostics.CodeAnalysis;
2123
using System.Globalization;
2224
using System.Net;
23-
using System.Diagnostics.CodeAnalysis;
24-
using ThirdParty.RuntimeBackports;
2525
using System.Threading.Tasks;
26+
using ThirdParty.RuntimeBackports;
2627

2728
namespace Amazon.Runtime
2829
{
@@ -33,7 +34,6 @@ namespace Amazon.Runtime
3334
public class AssumeRoleAWSCredentials : RefreshingAWSCredentials
3435
{
3536
private RegionEndpoint DefaultSTSClientRegion = RegionEndpoint.USEast1;
36-
3737
private Logger _logger = Logger.GetLogger(typeof(AssumeRoleAWSCredentials));
3838

3939
/// <summary>
@@ -85,6 +85,7 @@ public AssumeRoleAWSCredentials(AWSCredentials sourceCredentials, string roleArn
8585
RoleArn = roleArn;
8686
RoleSessionName = roleSessionName;
8787
Options = options;
88+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_STS_ASSUME_ROLE);
8889

8990
// Make sure to fetch new credentials well before the current credentials expire to avoid
9091
// any request being made with expired credentials.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System.Net;
2727
using System.Text.RegularExpressions;
2828
using System.Threading.Tasks;
29+
using Amazon.Runtime.Internal.UserAgent;
2930

3031
namespace Amazon.Runtime
3132
{
@@ -65,6 +66,7 @@ public partial class AssumeRoleWithWebIdentityCredentials : RefreshingAWSCredent
6566
private AssumeRoleWithWebIdentityCredentialsOptions _options;
6667

6768
#region Properties
69+
6870
/// <summary>
6971
/// The absolute path to the file on disk containing an OIDC token
7072
/// </summary>
@@ -79,7 +81,8 @@ public partial class AssumeRoleWithWebIdentityCredentials : RefreshingAWSCredent
7981
/// An identifier for the assumed role session.
8082
/// </summary>
8183
public string RoleSessionName { get; }
82-
#endregion Properties
84+
85+
#endregion Properties
8386

8487
/// <summary>
8588
/// Constructs an AssumeRoleWithWebIdentityCredentials object.
@@ -122,6 +125,7 @@ public AssumeRoleWithWebIdentityCredentials(string webIdentityTokenFile, string
122125
RoleArn = roleArn;
123126
RoleSessionName = string.IsNullOrEmpty(roleSessionName) ? _roleSessionNameDefault : roleSessionName;
124127
_options = options;
128+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_STS_ASSUME_ROLE_WEB_ID);
125129

126130
// Make sure to fetch new credentials well before the current credentials expire to avoid
127131
// any request being made with expired credentials.
@@ -138,7 +142,11 @@ public static AssumeRoleWithWebIdentityCredentials FromEnvironmentVariables()
138142
var webIdentityTokenFile = Environment.GetEnvironmentVariable(WebIdentityTokenFileEnvVariable);
139143
var roleArn = Environment.GetEnvironmentVariable(RoleArnEnvVariable);
140144
var roleSessionName = Environment.GetEnvironmentVariable(RoleSessionNameEnvVariable);
141-
return new AssumeRoleWithWebIdentityCredentials(webIdentityTokenFile, roleArn, roleSessionName);
145+
146+
var credentials = new AssumeRoleWithWebIdentityCredentials(webIdentityTokenFile, roleArn, roleSessionName);
147+
credentials.FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN);
148+
149+
return credentials;
142150
}
143151

144152
protected override CredentialsRefreshState GenerateNewCredentials()

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using Amazon.Runtime.Internal.UserAgent;
1617
using Amazon.Runtime.Internal.Util;
1718
using Amazon.Util;
1819

@@ -29,10 +30,8 @@ public class BasicAWSCredentials : AWSCredentials
2930

3031
#endregion
3132

32-
3333
#region Constructors
3434

35-
3635
/// <summary>
3736
/// Constructs a BasicAWSCredentials object for the specified accessKey and secretKey.
3837
/// </summary>
@@ -57,12 +56,12 @@ public BasicAWSCredentials(string accessKey, string secretKey, string accountId)
5756
if (!string.IsNullOrEmpty(accessKey))
5857
{
5958
_credentials = new ImmutableCredentials(accessKey, secretKey, null, accountId);
59+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_CODE);
6060
}
6161
}
6262

6363
#endregion
6464

65-
6665
#region Abstract class overrides
6766

6867
/// <summary>
@@ -92,7 +91,6 @@ public override bool Equals(object obj)
9291
new object[] { _credentials },
9392
new object[] { bac._credentials });
9493
}
95-
9694
public override int GetHashCode()
9795
{
9896
return Hashing.Hash(_credentials);

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15+
using Amazon.Runtime.Internal.UserAgent;
1516
using Amazon.Runtime.Internal.Util;
1617
using Amazon.Util;
1718
using System;
@@ -77,14 +78,18 @@ public static DefaultInstanceProfileAWSCredentials Instance
7778
private DefaultInstanceProfileAWSCredentials()
7879
{
7980
// if IMDS is turned off, no need to spin up the timer task
80-
if (!EC2InstanceMetadata.IsIMDSEnabled) return;
81+
if (!EC2InstanceMetadata.IsIMDSEnabled)
82+
{
83+
return;
84+
}
8185

8286
_logger = Logger.GetLogger(typeof(DefaultInstanceProfileAWSCredentials));
83-
8487
_credentialsRetrieverTimer = new Timer(RenewCredentials, null, TimeSpan.Zero, _neverTimespan); // This invokes synchronous calls in seperate thread.
88+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_IMDS);
8589
}
8690

8791
#region Overrides
92+
8893
/// <summary>
8994
/// Returns a copy of the most recent instance profile credentials.
9095
/// </summary>
@@ -262,7 +267,8 @@ public override async Task<ImmutableCredentials> GetCredentialsAsync()
262267

263268
return credentials;
264269
}
265-
#endregion
270+
271+
#endregion
266272

267273
#region Private members
268274
private void RenewCredentials(object unused)
@@ -366,7 +372,8 @@ private static void CheckIsIMDSEnabled()
366372
}
367373
#endregion
368374

369-
#region IDisposable Support
375+
#region IDisposable Support
376+
370377
private bool _isDisposed = false;
371378

372379
protected virtual void Dispose(bool disposing)
@@ -395,6 +402,7 @@ public void Dispose()
395402
Dispose(true);
396403
GC.SuppressFinalize(this);
397404
}
398-
#endregion
405+
406+
#endregion
399407
}
400408
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15+
using Amazon.Runtime.Internal.UserAgent;
1516
using Amazon.Runtime.Internal.Util;
1617
using System;
1718
using System.Globalization;
@@ -56,6 +57,8 @@ public EnvironmentVariablesAWSCredentials()
5657

5758
// We need to do an initial fetch to validate that we can use environment variables to get the credentials.
5859
FetchCredentials();
60+
61+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_ENV_VARS);
5962
}
6063

6164
#endregion

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414
*/
1515

1616
using Amazon.Runtime.CredentialManagement;
17-
using Amazon.Runtime.Internal;
1817
using Amazon.Runtime.CredentialManagement.Internal;
18+
using Amazon.Runtime.Internal;
19+
using Amazon.Runtime.Internal.UserAgent;
1920
using Amazon.Runtime.Internal.Util;
2021
using Amazon.Runtime.SharedInterfaces;
21-
using Amazon.Util;
2222
using Amazon.RuntimeDependencies;
2323
using Amazon.Util.Internal;
24-
using ThirdParty.RuntimeBackports;
2524
using System;
2625
using System.Diagnostics.CodeAnalysis;
27-
using System.Collections.Generic;
2826
using System.Globalization;
2927
using System.Net;
3028
using System.Threading.Tasks;
29+
using ThirdParty.RuntimeBackports;
3130

3231
namespace Amazon.Runtime
3332
{
@@ -45,7 +44,6 @@ public class FederatedAWSCredentials : RefreshingAWSCredentials
4544
private static readonly RegionEndpoint DefaultSTSClientRegion = RegionEndpoint.USEast1;
4645
private static readonly TimeSpan MaximumCredentialTimespan = TimeSpan.FromHours(1);
4746
private static readonly TimeSpan DefaultPreemptExpiryTime = TimeSpan.FromMinutes(15);
48-
4947
private readonly SAMLRoleSessionManager sessionManager = new SAMLRoleSessionManager();
5048

5149
/// <summary>
@@ -79,6 +77,7 @@ public FederatedAWSCredentials(SAMLEndpoint samlEndpoint, string roleArn,
7977
SAMLEndpoint = samlEndpoint ?? throw new ArgumentNullException("samlEndpoint");
8078
RoleArn = roleArn;
8179
PreemptExpiryTime = DefaultPreemptExpiryTime;
80+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_STS_FEDERATION_TOKEN);
8281
}
8382

8483
/// <summary>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using Amazon.Runtime.Internal.UserAgent;
1617
using Amazon.Util;
1718
using Amazon.Util.Internal;
1819
using System;
@@ -69,6 +70,7 @@ public GenericContainerCredentials()
6970
{
7071
PreemptExpiryTime = TimeSpan.FromMinutes(15);
7172
DetermineEndpoint();
73+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_HTTP);
7274
}
7375

7476
protected override CredentialsRefreshState GenerateNewCredentials()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Diagnostics;
2020
using System.Threading.Tasks;
2121
using Amazon.Runtime.Internal;
22+
using Amazon.Runtime.Internal.UserAgent;
2223
using Amazon.Runtime.Internal.Util;
2324
using Amazon.Util.Internal;
2425
using System.Diagnostics.CodeAnalysis;
@@ -100,6 +101,8 @@ public ProcessAWSCredentials(string processCredentialInfo, string accountId)
100101
// Make sure to fetch new credentials well before the current credentials expire to avoid
101102
// any request being made with expired credentials.
102103
PreemptExpiryTime = TimeSpan.FromMinutes(15);
104+
105+
FeatureIdSources.Add(UserAgentFeatureId.CREDENTIALS_PROCESS);
103106
}
104107

105108
#endregion

0 commit comments

Comments
 (0)