Skip to content

Commit 1e44507

Browse files
committed
fix: Update signers to handle anonymous credentials
1 parent 24650b4 commit 1e44507

File tree

8 files changed

+62
-8
lines changed

8 files changed

+62
-8
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+
"Update SDK signers to handle scenarios where anonymous credentials are provided."
5+
],
6+
"type": "patch",
7+
"updateMinimum": true
8+
}
9+
}

sdk/src/Core/Amazon.Runtime/Internal/Auth/AWS4Signer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,16 @@ public override void Sign(IRequest request,
118118
RequestMetrics metrics,
119119
BaseIdentity identity)
120120
{
121-
var credentials = identity as AWSCredentials;
122-
if (credentials is null)
121+
if (identity is not AWSCredentials credentials)
123122
{
124123
throw new AmazonClientException($"The identity parameter must be of type AWSCredentials for the signer {nameof(AWS4Signer)}.");
125124
}
126125

127126
var immutableCredentials = credentials.GetCredentials();
127+
if (immutableCredentials is null)
128+
{
129+
return;
130+
}
128131

129132
var signingResult = SignRequest(request, clientConfig, metrics, immutableCredentials.AccessKey, immutableCredentials.SecretKey);
130133
request.Headers[HeaderKeys.AuthorizationHeader] = signingResult.ForAuthorizationHeader;

sdk/src/Core/Amazon.Runtime/Internal/Auth/AWS4aSignerCRTWrapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public override void Sign(IRequest request, IClientConfig clientConfig, RequestM
127127
{
128128
var credentials = identity as AWSCredentials;
129129
var immutableCredentials = credentials.GetCredentials();
130+
if (immutableCredentials is null)
131+
{
132+
return;
133+
}
134+
130135
_awsSigV4AProvider.Sign(request, clientConfig, metrics, immutableCredentials);
131136
}
132137

sdk/src/Core/Amazon.Runtime/Internal/Auth/AWSEndpointAuthSchemeSigner.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ public override void Sign(IRequest request, IClientConfig clientConfig, RequestM
3838
var aws4Signer = signer as AWS4Signer;
3939
var useV4a = aws4aSigner != null;
4040
var useV4 = aws4Signer != null;
41-
var credentials = identity as AWSCredentials;
42-
if (credentials is null)
41+
42+
if (identity is not AWSCredentials credentials)
4343
{
4444
throw new AmazonClientException($"The identity parameter must be of type AWSCredentials for the signer {nameof(AWSEndpointAuthSchemeSigner)}.");
4545
}
4646

4747
var immutableCredentials = credentials.GetCredentials();
48+
if (immutableCredentials is null)
49+
{
50+
return;
51+
}
4852

4953
AWSSigningResultBase signingResult;
5054

sdk/src/Core/Amazon.Runtime/Internal/Auth/CloudFrontSigner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override void Sign(IRequest request, IClientConfig clientConfig, RequestM
4242
}
4343

4444
var immutableCredentials = credentials.GetCredentials();
45-
if (String.IsNullOrEmpty(immutableCredentials.AccessKey))
45+
if (string.IsNullOrEmpty(immutableCredentials?.AccessKey))
4646
{
4747
throw new ArgumentOutOfRangeException("awsAccessKeyId", "The AWS Access Key ID cannot be NULL or a Zero length string");
4848
}

sdk/src/Core/Amazon.Runtime/Internal/Auth/QueryStringSigner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public override void Sign(IRequest request, IClientConfig clientConfig, RequestM
5050
var credentials = identity as AWSCredentials;
5151
var immutableCredentials = credentials.GetCredentials();
5252

53-
if (String.IsNullOrEmpty(immutableCredentials.AccessKey))
53+
if (string.IsNullOrEmpty(immutableCredentials?.AccessKey))
5454
{
5555
throw new ArgumentOutOfRangeException("awsAccessKeyId", "The AWS Access Key ID cannot be NULL or a Zero length string");
5656
}

sdk/src/Core/Amazon.Runtime/Internal/Auth/S3Signer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ public override void Sign(IRequest request, IClientConfig clientConfig, RequestM
5858
var aws4aSigner = signer as AWS4aSignerCRTWrapper;
5959
var useV4 = aws4Signer != null;
6060
var useV4a = aws4aSigner != null;
61-
var credentials = identity as AWSCredentials;
62-
if (credentials is null)
61+
62+
if (identity is not AWSCredentials credentials)
6363
{
6464
throw new AmazonClientException($"The identity parameter must be of type AWSCredentials for the signer {nameof(S3Signer)}.");
6565
}
6666

6767
var immutableCredentials = credentials.GetCredentials();
68+
if (immutableCredentials is null)
69+
{
70+
return;
71+
}
6872

6973
if (useV4a)
7074
{

sdk/test/UnitTests/Custom/Runtime/SignerTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Amazon.Runtime;
77
using Amazon.Runtime.Internal;
88
using Amazon.Runtime.Internal.Auth;
9+
using Amazon.Runtime.Internal.Util;
910
using Amazon.Util;
1011
using AWSSDK_DotNet.IntegrationTests.Utils;
1112
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -226,5 +227,33 @@ public async Task TestSignerWithBasicCredentialsAsync()
226227
Assert.AreEqual(1, signer.SignCount);
227228
}
228229
#endif
230+
231+
[TestMethod]
232+
[TestCategory("Runtime")]
233+
[TestCategory("Signer")]
234+
public void TestV4SignerHandlesAnonymousCredentials()
235+
{
236+
var mock = new Moq.Mock<IRequest>().SetupAllProperties();
237+
var requestMock = new Moq.Mock<AmazonWebServiceRequest>().SetupAllProperties();
238+
var request = mock.Object;
239+
var config = new AmazonIotDataConfig();
240+
241+
mock.SetupGet(x => x.Headers).Returns(new Dictionary<string, string>());
242+
mock.SetupGet(x => x.OriginalRequest).Returns(requestMock.Object);
243+
request.Endpoint = EndpointResolver.DetermineEndpoint(config, request);
244+
245+
var signer = new AWS4Signer();
246+
var credentials = new AnonymousAWSCredentials();
247+
248+
// After the SRA changes, the signers were updated to retrieve credentials themselves (instead of
249+
// relying on a pipeline handler to place the value in the request context). One miss from the original
250+
// implementation is that customers may call the signer indirectly with anonymous credentials (for example, if
251+
// their environment is set to assume a role with web identity).
252+
signer.Sign(request, config, new RequestMetrics(), credentials);
253+
254+
// This test verifies the signer does not fail with a null reference exception, but doesn't add the authorization
255+
// header either.
256+
Assert.IsFalse(request.Headers.ContainsKey(HeaderKeys.AuthorizationHeader));
257+
}
229258
}
230259
}

0 commit comments

Comments
 (0)