-
Notifications
You must be signed in to change notification settings - Fork 879
AmazonDynamoDBEndpointResolver throws NullReferenceException with AnonymousAWSCredentials #3776
Description
Describe the bug
AmazonDynamoDBEndpointResolver.MapEndpointsParameters() (and possibly other implementations of BaseEndpointResolver) do not handle the case where the AWS credentials associated with the request are an instance of AnonymousAWSCredentials:
aws-sdk-net/sdk/src/Services/DynamoDBv2/Generated/Internal/AmazonDynamoDBEndpointResolver.cs
Line 57 in 1060dd1
| result.AccountId = requestContext.Identity is AWSCredentials credentials ? credentials.GetCredentials().AccountId : null; |
The GetCredentials() method returns null, so the attempt to get the AWS account ID fails:
aws-sdk-net/sdk/src/Core/Amazon.Runtime/Credentials/AnonymousAWSCredentials.cs
Lines 31 to 34 in 1060dd1
| public override ImmutableCredentials GetCredentials() | |
| { | |
| return null; | |
| } |
While using AnonymousAWSCredentials with a real services that require credentials is unlikely for production applications, this is a commonly used type within tests such as this one (which is how I found this).
Regression Issue
- Select this option if this issue appears to be a regression.
Expected Behavior
Either the code handles the case successfully, or throws a meaningful exception (preferably the former).
Current Behavior
A NullReferenceException is thrown.
Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
AmazonDynamoDBEndpointResolver.MapEndpointsParameters(IRequestContext requestContext)
BaseEndpointResolver.ProcessRequestHandlers(IExecutionContext executionContext)
BaseEndpointResolver.PreInvoke(IExecutionContext executionContext)
BaseEndpointResolver.InvokeSync(IExecutionContext executionContext)
AWSTracingPipelineHandler.InvokeSync(IExecutionContext executionContext) line 35
CallbackHandler.InvokeSync(IExecutionContext executionContext)
ErrorCallbackHandler.InvokeSync(IExecutionContext executionContext)
MetricsHandler.InvokeSync(IExecutionContext executionContext)
BindingRedirectCheckHandler.InvokeSync(IExecutionContext executionContext)
RuntimePipeline.InvokeSync(IExecutionContext executionContext)
AmazonServiceClient.Invoke[TResponse](AmazonWebServiceRequest request, InvokeOptionsBase options)
TestAWSClientInstrumentation.TestDDBScanSuccessful() line 58
Reproduction Steps
var credentials = new AnonymousAWSCredentials();
var ddb = new AmazonDynamoDBClient(credentials, RegionEndpoint.USEast1);
var request = new ScanRequest
{
TableName = "SampleProduct",
AttributesToGet = ["Id", "Name"],
};
await ddb.ScanAsync(request);Possible Solution
- result.AccountId = requestContext.Identity is AWSCredentials credentials ? credentials.GetCredentials().AccountId : null;
+ result.AccountId = requestContext.Identity is AWSCredentials credentials ? credentials.GetCredentials()?.AccountId : null;Additional Information/Context
Found while attempting to implement open-telemetry/opentelemetry-dotnet-contrib#2719.
AWS .NET SDK and/or Package version used
AWSSDK.DynamoDBv2 4.0.0.
Targeted .NET Platform
.NET 8
Operating System and version
Windows 11