Skip to content

Commit 30a1d87

Browse files
committed
fix: Improve error message when no credentials are found
1 parent 451358f commit 30a1d87

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"core": {
3+
"changeLogMessages": [
4+
"Improved error message when searching for AWS credentials and no credentials were found."
5+
],
6+
"type": "patch",
7+
"updateMinimum": true
8+
}
9+
}

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,12 @@ public AWSCredentials ResolveIdentity()
157157

158158
using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
159159
{
160-
writer.WriteLine("Unable to find credentials");
160+
writer.WriteLine("Failed to resolve AWS credentials. The credential providers used to search for credentials returned the following errors:");
161161
writer.WriteLine();
162162
for (int i = 0; i < errors.Count; i++)
163163
{
164164
Exception e = errors[i];
165-
writer.WriteLine("Exception {0} of {1}:", i + 1, errors.Count);
166-
writer.WriteLine(e.ToString());
167-
writer.WriteLine();
165+
writer.WriteLine("Exception {0} of {1}: {2}", i + 1, errors.Count, e.Message);
168166
}
169167

170168
throw new AmazonClientException(writer.ToString());
@@ -241,7 +239,25 @@ private static AWSCredentials ContainerEC2CredentialsWrapper()
241239
Logger.GetLogger(typeof(GenericContainerCredentials)).Error(e, $"Failed to access environment variables {GenericContainerCredentials.RelativeURIEnvVariable} and {GenericContainerCredentials.FullURIEnvVariable}." +
242240
$" Either {GenericContainerCredentials.RelativeURIEnvVariable} or {GenericContainerCredentials.FullURIEnvVariable} environment variables must be set.");
243241
}
244-
return DefaultInstanceProfileAWSCredentials.Instance;
242+
243+
try
244+
{
245+
var credentials = DefaultInstanceProfileAWSCredentials.Instance;
246+
247+
// Test that credentials can be resolved using EC2 instance metadata. Assuming the
248+
// credentials are successfully resolved the credentials will be cached so the
249+
// later use of getting credentials for making the request will not have to redo
250+
// the network calls to EC2 instance metadata to get credentials.
251+
credentials.GetCredentials();
252+
253+
return credentials;
254+
}
255+
catch (AmazonServiceException e)
256+
{
257+
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
258+
"Failed to connect to EC2 instance metadata to retrieve credentials: {0}.",
259+
e.Message), e);
260+
}
245261
}
246262

247263
/// <summary>

0 commit comments

Comments
 (0)