Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(ec2): evaluate filters in DescribeInternetGateways (#1269)

Maximilian Bandledc55a2824ac6
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.py
index d2ed0431..340de520 100644
--- a/ministack/services/ec2.py
+++ b/ministack/services/ec2.py
@@ -1896,6 +1896,7 @@ def _delete_internet_gateway(p):
def _describe_internet_gateways(p):
filter_ids = _parse_member_list(p, "InternetGatewayId")
+ filters = _parse_filters(p)
if filter_ids:
for gid in filter_ids:
if gid not in _internet_gateways:
@@ -1904,6 +1905,17 @@ def _describe_internet_gateways(p):
for igw in _internet_gateways.values():
if filter_ids and igw["InternetGatewayId"] not in filter_ids:
continue
+ if not _resource_matches_tag_filters(igw["InternetGatewayId"], filters):
+ continue
+ if filters.get("internet-gateway-id") and igw["InternetGatewayId"] not in filters["internet-gateway-id"]:
+ continue
+ if filters.get("owner-id") and igw["OwnerId"] not in filters["owner-id"]:
+ continue
+ attachments = igw.get("Attachments", [])
+ if filters.get("attachment.vpc-id") and not any(a.get("VpcId") in filters["attachment.vpc-id"] for a in attachments):
+ continue
+ if filters.get("attachment.state") and not any(a.get("State") in filters["attachment.state"] for a in attachments):
+ continue
items += _igw_xml(igw)
return _xml(200, "DescribeInternetGatewaysResponse",
f"<internetGatewaySet>{items}</internetGatewaySet>")
diff --git a/tests/test_ec2.py b/tests/test_ec2.py
index d6119a13..88e2df7c 100644
--- a/tests/test_ec2.py
+++ b/tests/test_ec2.py
@@ -896,6 +896,47 @@ def test_ec2_internet_gateway_crud(ec2):
ec2.delete_internet_gateway(InternetGatewayId=igw_id)
ec2.delete_vpc(VpcId=vpc_id)
+def test_ec2_describe_internet_gateways_honors_filters(ec2):
+ """Filters must narrow the result. Test filtering on different parameters"""
+ import uuid as _uuid
+
+ tag = _uuid.uuid4().hex[:8]
+ vpc_id = ec2.create_vpc(CidrBlock="10.39.0.0/16")["Vpc"]["VpcId"]
+ attached = ec2.create_internet_gateway(
+ TagSpecifications=[{"ResourceType": "internet-gateway",
+ "Tags": [{"Key": "check", "Value": tag}]}],
+ )["InternetGateway"]["InternetGatewayId"]
+ # A second, unattached gateway: Should get ignored by the filters
+ spare = ec2.create_internet_gateway()["InternetGateway"]["InternetGatewayId"]
+ ec2.attach_internet_gateway(InternetGatewayId=attached, VpcId=vpc_id)
+
+ def ids(**kwargs):
+ return sorted(g["InternetGatewayId"]
+ for g in ec2.describe_internet_gateways(**kwargs)["InternetGateways"])
+
+ try:
+ assert ids(Filters=[{"Name": "attachment.vpc-id", "Values": [vpc_id]}]) == [attached]
+ # Any other attached gateway in the account matches this one too; what it must exclude is
+ # the gateway attached to nothing.
+ state_matched = ids(Filters=[{"Name": "attachment.state", "Values": ["available"]}])
+ assert attached in state_matched and spare not in state_matched
+ assert ids(Filters=[{"Name": "internet-gateway-id", "Values": [spare]}]) == [spare]
+ assert ids(Filters=[{"Name": "tag:check", "Values": [tag]}]) == [attached]
+ # Read the owner off the gateway rather than hardcoding it, so this holds against AWS too.
+ owner = ec2.describe_internet_gateways(
+ InternetGatewayIds=[attached])["InternetGateways"][0]["OwnerId"]
+ assert attached in ids(Filters=[{"Name": "owner-id", "Values": [owner]}])
+ assert ids(Filters=[{"Name": "owner-id", "Values": ["999999999999"]}]) == []
+ # Two values for one name is an OR; no match is an empty list, not "everything".
+ assert ids(Filters=[{"Name": "internet-gateway-id",
+ "Values": [attached, spare]}]) == sorted([attached, spare])
+ assert ids(Filters=[{"Name": "attachment.vpc-id", "Values": ["vpc-00000000"]}]) == []
+ finally:
+ ec2.detach_internet_gateway(InternetGatewayId=attached, VpcId=vpc_id)
+ ec2.delete_internet_gateway(InternetGatewayId=attached)
+ ec2.delete_internet_gateway(InternetGatewayId=spare)
+ ec2.delete_vpc(VpcId=vpc_id)
+
def test_ec2_elastic_ip_crud(ec2):
alloc = ec2.allocate_address(Domain="vpc")
alloc_id = alloc["AllocationId"]