Skip to content

Commit 396c5ef

Browse files
committed
fix: Update credentials resolver to handle empty profile names in config
1 parent 185a9bc commit 396c5ef

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
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+
"Fix regression in V4 where the credentials resolver would attempt to fetch a profile even when its name was empty or null (https://github.com/aws/aws-sdk-net/issues/4028)"
5+
],
6+
"type": "patch",
7+
"updateMinimum": true
8+
}
9+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ BaseIdentity IIdentityResolver.ResolveIdentity(IClientConfig clientConfig)
101101
public AWSCredentials ResolveIdentity(IClientConfig clientConfig)
102102
{
103103
var profile = clientConfig?.Profile;
104-
if (profile != null)
104+
if (!string.IsNullOrEmpty(profile?.Name))
105105
{
106106
var source = new CredentialProfileStoreChain(profile.Location);
107107
if (source.TryGetProfile(profile.Name, out CredentialProfile storedProfile))

sdk/test/UnitTests/Custom/Runtime/Credentials/DefaultCredentialsResolverTests.cs

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

16+
using Amazon;
1617
using Amazon.Runtime;
1718
using Amazon.Runtime.CredentialManagement;
1819
using Amazon.Runtime.Credentials;
20+
using Amazon.S3;
1921
using Microsoft.VisualStudio.TestTools.UnitTesting;
2022
using System;
2123

@@ -88,5 +90,31 @@ public void CredentialsAreReevaluatedWhenProfileChanges()
8890
Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, "non-existent-profile");
8991
Assert.ThrowsException<ProfileNotFoundException>(() => identityResolver.ResolveIdentity(clientConfig: null));
9092
}
93+
94+
/// <summary>
95+
/// Reported in https://github.com/aws/aws-sdk-net/issues/4028
96+
/// <para />
97+
/// If the name property in the "Profile" property is not set (which can happen when its value
98+
/// is only set in a non-production environment), the resolver should not throw an exception and
99+
/// move to the next provider in the chain (which in this test will be the access / secret key pair
100+
/// set in the environment variables).
101+
/// </summary>
102+
[DataTestMethod]
103+
[DataRow("")]
104+
[DataRow(null)]
105+
public void CredentialsResolver_HandlesEmptyProfileNameInConfig(string profileName)
106+
{
107+
Environment.SetEnvironmentVariable(EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_ACCESSKEY, "foo");
108+
Environment.SetEnvironmentVariable(EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_SECRETKEY, "bar");
109+
110+
var identityResolver = new DefaultAWSCredentialsIdentityResolver();
111+
var resolvedIdentity = identityResolver.ResolveIdentity(new AmazonS3Config
112+
{
113+
Profile = new Profile(profileName)
114+
});
115+
116+
Assert.IsNotNull(resolvedIdentity);
117+
Assert.IsTrue(resolvedIdentity is EnvironmentVariablesAWSCredentials);
118+
}
91119
}
92120
}

0 commit comments

Comments
 (0)