@@ -53,38 +53,69 @@ public DefaultAWSCredentialsIdentityResolver()
53
53
#endif
54
54
( ) => new EnvironmentVariablesAWSCredentials( ) , // Look for credentials set in environment vars.
55
55
( ) => AssumeRoleWithWebIdentityCredentials . FromEnvironmentVariables ( ) ,
56
- ( ) => GetAWSCredentials ( _credentialProfileChain ) ,
56
+ ( ) => GetProfileCredentials ( _credentialProfileChain ) ,
57
57
( ) => ContainerEC2CredentialsWrapper ( ) , // either get ECS / EKS credentials or instance profile credentials
58
58
} ;
59
59
}
60
60
61
+ #region Helper methods for migration from FallbackCredentialsFactory
62
+
61
63
/// <summary>
62
64
/// Search the environment for configured AWS credentials. The search includes
63
65
/// environment variables, "default" AWS credentials profiles and EC2 instance metadata.
64
66
/// </summary>
67
+ /// <param name="clientConfig">
68
+ /// Optional config object that can be used to specify a different profile programatically (via the <see cref="Profile"/> property).
69
+ /// </param>
65
70
/// <returns>AWSCredentials that can be used when creating AWS service clients.</returns>
66
- public static AWSCredentials GetCredentials ( )
71
+ public static AWSCredentials GetCredentials ( IClientConfig clientConfig = null )
67
72
{
68
- return _defaultInstance . Value . ResolveIdentity ( ) ;
73
+ return _defaultInstance . Value . ResolveIdentity ( clientConfig ) ;
69
74
}
70
75
71
76
/// <summary>
72
77
/// Search the environment for configured AWS credentials. The search includes
73
78
/// environment variables, "default" AWS credentials profiles and EC2 instance metadata.
74
79
/// </summary>
80
+ /// <param name="clientConfig">
81
+ /// Optional config object that can be used to specify a different profile programatically (via the <see cref="Profile"/> property).
82
+ /// </param>
75
83
/// <returns>AWSCredentials that can be used when creating AWS service clients.</returns>
76
- public static Task < AWSCredentials > GetCredentialsAsync ( )
84
+ public static Task < AWSCredentials > GetCredentialsAsync ( IClientConfig clientConfig = null )
77
85
{
78
- return _defaultInstance . Value . ResolveIdentityAsync ( ) ;
86
+ return _defaultInstance . Value . ResolveIdentityAsync ( clientConfig ) ;
79
87
}
80
88
81
- BaseIdentity IIdentityResolver . ResolveIdentity ( )
89
+ #endregion
90
+
91
+ BaseIdentity IIdentityResolver . ResolveIdentity ( IClientConfig clientConfig )
92
+ => ResolveIdentity ( clientConfig ) ;
93
+
94
+ public AWSCredentials ResolveIdentity ( IClientConfig clientConfig )
82
95
{
83
- return ResolveIdentity ( ) ;
96
+ var profile = clientConfig ? . Profile ;
97
+ if ( profile != null )
98
+ {
99
+ var source = new CredentialProfileStoreChain ( profile . Location ) ;
100
+ if ( source . TryGetProfile ( profile . Name , out CredentialProfile storedProfile ) )
101
+ {
102
+ return storedProfile . GetAWSCredentials ( source , true ) ;
103
+ }
104
+
105
+ throw new AmazonClientException ( $ "Unable to find the \" { profile . Name } \" profile specified in the client configuration.") ;
106
+ }
107
+
108
+ return InternalGetCredentials ( ) ;
84
109
}
85
110
111
+ Task < BaseIdentity > IIdentityResolver . ResolveIdentityAsync ( IClientConfig clientConfig , CancellationToken cancellationToken ) =>
112
+ Task . FromResult < BaseIdentity > ( ResolveIdentity ( clientConfig ) ) ;
113
+
114
+ public Task < AWSCredentials > ResolveIdentityAsync ( IClientConfig clientConfig , CancellationToken cancellationToken = default ) =>
115
+ Task . FromResult ( ResolveIdentity ( clientConfig ) ) ;
116
+
86
117
[ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Design" , "CA1031:Do not catch general exception types" , Justification = "We need to catch all exceptions to be able to move the the next generator." ) ]
87
- public AWSCredentials ResolveIdentity ( )
118
+ private AWSCredentials InternalGetCredentials ( )
88
119
{
89
120
var hasEnvironmentChanged = false ;
90
121
@@ -95,7 +126,7 @@ public AWSCredentials ResolveIdentity()
95
126
{
96
127
hasEnvironmentChanged = _lastKnownEnvironmentState . HasEnvironmentChanged ( ) ;
97
128
if ( ! hasEnvironmentChanged )
98
- {
129
+ {
99
130
return _cachedCredentials ;
100
131
}
101
132
}
@@ -155,37 +186,24 @@ public AWSCredentials ResolveIdentity()
155
186
return _cachedCredentials ;
156
187
}
157
188
158
- using ( StringWriter writer = new StringWriter ( CultureInfo . InvariantCulture ) )
189
+ using var writer = new StringWriter ( CultureInfo . InvariantCulture ) ;
190
+ writer . WriteLine ( "Failed to resolve AWS credentials. The credential providers used to search for credentials returned the following errors:" ) ;
191
+ writer . WriteLine ( ) ;
192
+ for ( int i = 0 ; i < errors . Count ; i ++ )
159
193
{
160
- writer . WriteLine ( "Failed to resolve AWS credentials. The credential providers used to search for credentials returned the following errors:" ) ;
161
- writer . WriteLine ( ) ;
162
- for ( int i = 0 ; i < errors . Count ; i ++ )
163
- {
164
- Exception e = errors [ i ] ;
165
- writer . WriteLine ( "Exception {0} of {1}: {2}" , i + 1 , errors . Count , e . Message ) ;
166
- }
167
-
168
- throw new AmazonClientException ( writer . ToString ( ) ) ;
194
+ Exception e = errors [ i ] ;
195
+ writer . WriteLine ( "Exception {0} of {1}: {2}" , i + 1 , errors . Count , e . Message ) ;
169
196
}
197
+
198
+ throw new AmazonClientException ( writer . ToString ( ) ) ;
170
199
}
171
200
finally
172
201
{
173
202
_cachedCredentialsLock . ExitWriteLock ( ) ;
174
203
}
175
204
}
176
205
177
- async Task < BaseIdentity > IIdentityResolver . ResolveIdentityAsync ( CancellationToken cancellationToken )
178
- {
179
- var identity = await ResolveIdentityAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
180
- return identity ;
181
- }
182
-
183
- public async Task < AWSCredentials > ResolveIdentityAsync ( CancellationToken cancellationToken = default )
184
- {
185
- return await Task . Run ( ( ) => ResolveIdentity ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
186
- }
187
-
188
- private static AWSCredentials GetAWSCredentials ( ICredentialProfileSource source )
206
+ private static AWSCredentials GetProfileCredentials ( ICredentialProfileSource source )
189
207
{
190
208
var profileName = GetProfileName ( ) ;
191
209
if ( source . TryGetProfile ( profileName , out CredentialProfile profile ) )
0 commit comments