Skip to content

AmazonDynamoDBEndpointResolver throws NullReferenceException with AnonymousAWSCredentials #3776

@martincostello

Description

@martincostello

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:

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.dynamodbpotential-regressionMarking this issue as a potential regression to be checked by team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions