Skip to content

EC2: Handle filtering without Values #8570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

nik-localstack
Copy link
Contributor

When a filter is provided that doesn't have Values or has an empty list as Values moto raises TypeError

% aws ec2 describe-subnets --filters Name=vpc-id
{
    "Subnets": []
}
% aws ec2 describe-subnets --filters Name=vpc-id --endpoint-url http://127.0.0.1:5000

Unable to parse response (syntax error: line 1, column 0), invalid XML received. Further retries may succeed:
b'<!doctype html>\n<html lang=en>\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error</h1>\n<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>\n'

from logs:

  File "moto/moto/ec2/responses/_base_response.py", line 20, in <dictcomp>
    return {f["Name"]: f["Value"] for f in _filters}
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'

Same for

% aws ec2 describe-subnets --filters '[{"Name": "vpc-id", "Values": []}]'            
{
    "Subnets": []
}
% aws ec2 describe-subnets --filters '[{"Name": "vpc-id", "Values": []}]' --endpoint-url http://127.0.0.1:5000

Unable to parse response (syntax error: line 1, column 0), invalid XML received. Further retries may succeed:
b'<!doctype html>\n<html lang=en>\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error</h1>\n<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>\n'

This PR fixes this issue and also returns the same error as AWS for the case that a Name is not provided for a filter

 % aws ec2 describe-subnets --filters Values=123

An error occurred (InvalidParameterValue) when calling the DescribeSubnets operation: The filter 'null' is invalid

Tests are added for 3 cases

  • describe_images
  • describe_subnets
  • describe_availability_zones

but it affects more EC2 actions that share the same filtering logic

Issue was originally reported in localstack localstack/localstack#11765

Copy link

codecov bot commented Feb 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.65%. Comparing base (98e2730) to head (da93140).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8570   +/-   ##
=======================================
  Coverage   92.65%   92.65%           
=======================================
  Files        1231     1231           
  Lines      106851   106857    +6     
=======================================
+ Hits        99005    99011    +6     
  Misses       7846     7846           
Flag Coverage Δ
servertests 28.13% <44.44%> (-0.01%) ⬇️
unittests 92.63% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nik-localstack nik-localstack marked this pull request as ready for review February 6, 2025 13:58
Copy link
Collaborator

@bblommers bblommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense - nice to see we're handling the edge cases here. Thank you!

@bblommers bblommers added this to the 5.0.29 milestone Feb 7, 2025
@bblommers bblommers merged commit b69dc9f into getmoto:master Feb 7, 2025
56 checks passed
@bblommers bblommers modified the milestones: 5.0.29, 5.1.0 Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants