Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(ec2): preserve EIP tags and IPv6 ACL entries (#1805)
Ederson Brilhante9ab9cd59c433
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 61aaef7e..a7d6fc95 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed+- **EC2 — Elastic IP tags and IPv6 network ACL entries survive a read** — `DescribeAddresses` omitted EIP tags, so Terraform repeatedly planned `tags` and `tags_all`; network ACL entries always stored and returned an IPv4 CIDR, so an IPv6 rule was read back as a changed IPv4 rule on every plan. Tags and `Ipv6CidrBlock` now round-trip through the EC2 API. Reported by @edersonbrilhante.- **KMS — imported key material for HMAC and asymmetric keys** — `CreateKey` with `Origin=EXTERNAL` refused every spec but `SYMMETRIC_DEFAULT`, where AWS supports imported material for symmetric encryption, HMAC and asymmetric keys, ML-DSA excepted. HMAC material is the raw bytes of the spec's length and asymmetric material is the private key alone, DER-encoded PKCS#8, from which the public key is derived; material that does not match the key's `KeySpec` answers `IncorrectKeyMaterialException`, and `GetPublicKey` on a key still awaiting material answers `KMSInvalidStateException`. Reported by @guymahieu.- **RDS — the server certificate carries the endpoint AWS puts in it** — certificate generation raised `ValueError` once an endpoint passed the X.509 64-byte common-name bound, so a database with a long identifier could not start. The common name is now the advertised endpoint at its full length, the subject carries `OU=RDS, O=Amazon.com, L=Seattle, ST=Washington, C=US`, and the signing CA is named `Amazon RDS <region> Root CA RSA2048 G1`, matching a certificate captured from a real instance. Contributed by @jayjanssen.- **API Gateway — an OpenAPI body's `securityDefinitions` reach the methods** — the import discarded security outright, so a SAM API declaring a Cognito authorizer created none and every method imported as `authorizationType: NONE`, serving anonymous callers where AWS answers 401. Each scheme carrying `x-amazon-apigateway-authorizer` now becomes an authorizer, and an operation's `security` — or the document's — sets the method's `authorizationType`, `authorizerId` and `authorizationScopes`, so the enforcement added in 1.5.10 engages for body-defined APIs. Contributed by @maximoosemine.diff --git a/ministack/services/ec2.py b/ministack/services/ec2.pyindex 4a932483..47b615ea 100644--- a/ministack/services/ec2.py+++ b/ministack/services/ec2.py@@ -3594,6 +3594,7 @@ def _allocate_address(p):"NetworkInterfaceId": None,"PrivateIpAddress": None,}+ _parse_tag_specs(p, "elastic-ip", allocation_id)return _xml(200, "AllocateAddressResponse", f"""<publicIp>{public_ip}</publicIp><domain>{domain}</domain>@@ -3647,6 +3648,7 @@ def _describe_addresses(p):<publicIp>{addr['PublicIp']}</publicIp><domain>{addr['Domain']}</domain>{assoc}{inst}+ {_tag_set_xml(addr['AllocationId'])}</item>"""return _xml(200, "DescribeAddressesResponse", f"<addressesSet>{items}</addressesSet>")@@ -4875,6 +4877,20 @@ def _delete_nat_gateway(params):# Network ACLs# ---------------------------------------------------------------------------+def _network_acl_entry_xml(entry):+ cidr = (+ f"<cidrBlock>{entry['CidrBlock']}</cidrBlock>"+ if entry.get("CidrBlock")+ else f"<ipv6CidrBlock>{entry['Ipv6CidrBlock']}</ipv6CidrBlock>"+ )+ return f"""<item>+ <ruleNumber>{entry['RuleNumber']}</ruleNumber>+ <protocol>{entry['Protocol']}</protocol>+ <ruleAction>{entry['RuleAction']}</ruleAction>+ <egress>{'true' if entry['Egress'] else 'false'}</egress>+ {cidr}+ </item>"""+def _create_network_acl(params):vpc_id = _p(params, "VpcId")if not vpc_id:@@ -4921,13 +4937,7 @@ def _describe_network_acls(params):want_default = filters["default"][0].lower() == "true"if acl.get("IsDefault", False) != want_default:continue- entries = "".join(f"""<item>- <ruleNumber>{e['RuleNumber']}</ruleNumber>- <protocol>{e['Protocol']}</protocol>- <ruleAction>{e['RuleAction']}</ruleAction>- <egress>{'true' if e['Egress'] else 'false'}</egress>- <cidrBlock>{e.get('CidrBlock','0.0.0.0/0')}</cidrBlock>- </item>""" for e in acl["Entries"])+ entries = "".join(_network_acl_entry_xml(e) for e in acl["Entries"])assocs = "".join(f"""<item><networkAclAssociationId>{a['NetworkAclAssociationId']}</networkAclAssociationId><networkAclId>{acl['NetworkAclId']}</networkAclId>@@ -4963,8 +4973,15 @@ def _create_network_acl_entry(params):"Protocol": _p(params, "Protocol") or "-1","RuleAction": _p(params, "RuleAction") or "allow","Egress": _p(params, "Egress") == "true",- "CidrBlock": _p(params, "CidrBlock") or "0.0.0.0/0",}+ cidr_block = _p(params, "CidrBlock")+ ipv6_cidr_block = _p(params, "Ipv6CidrBlock")+ if cidr_block:+ entry["CidrBlock"] = cidr_block+ elif ipv6_cidr_block:+ entry["Ipv6CidrBlock"] = ipv6_cidr_block+ else:+ entry["CidrBlock"] = "0.0.0.0/0"_network_acls[acl_id]["Entries"].append(entry)return _xml(200, "CreateNetworkAclEntryResponse", "<return>true</return>")@@ -4990,13 +5007,21 @@ def _replace_network_acl_entry(params):acl = _network_acls[acl_id]acl["Entries"] = [e for e in acl["Entries"]if not (e["RuleNumber"] == rule_num and e["Egress"] == egress)]- acl["Entries"].append({+ entry = {"RuleNumber": rule_num,"Protocol": _p(params, "Protocol") or "-1","RuleAction": _p(params, "RuleAction") or "allow","Egress": egress,- "CidrBlock": _p(params, "CidrBlock") or "0.0.0.0/0",- })+ }+ cidr_block = _p(params, "CidrBlock")+ ipv6_cidr_block = _p(params, "Ipv6CidrBlock")+ if cidr_block:+ entry["CidrBlock"] = cidr_block+ elif ipv6_cidr_block:+ entry["Ipv6CidrBlock"] = ipv6_cidr_block+ else:+ entry["CidrBlock"] = "0.0.0.0/0"+ acl["Entries"].append(entry)return _xml(200, "ReplaceNetworkAclEntryResponse", "<return>true</return>")diff --git a/tests/test_ec2.py b/tests/test_ec2.pyindex d2022891..83793083 100644--- a/tests/test_ec2.py+++ b/tests/test_ec2.py@@ -1217,6 +1217,10 @@ def test_ec2_elastic_ip_crud(ec2):assert alloc_id.startswith("eipalloc-")assert "PublicIp" in alloc+ ec2.create_tags(Resources=[alloc_id], Tags=[{"Key": "Name", "Value": "nat-eip"}])+ tagged = ec2.describe_addresses(AllocationIds=[alloc_id])["Addresses"][0]+ assert tagged["Tags"] == [{"Key": "Name", "Value": "nat-eip"}]+resp = ec2.run_instances(ImageId="ami-00000000", MinCount=1, MaxCount=1)iid = resp["Instances"][0]["InstanceId"]@@ -1640,6 +1644,36 @@ def test_ec2_network_acl_replace_entry(ec2):assert len(entries) == 1assert entries[0]["RuleAction"] == "allow"++def test_ec2_network_acl_ipv6_entry(ec2):+ vpc_id = ec2.create_vpc(CidrBlock="10.105.0.0/16")["Vpc"]["VpcId"]+ try:+ acl_id = ec2.create_network_acl(VpcId=vpc_id)["NetworkAcl"]["NetworkAclId"]+ ec2.create_network_acl_entry(+ NetworkAclId=acl_id,+ RuleNumber=101,+ Protocol="-1",+ RuleAction="allow",+ Egress=True,+ Ipv6CidrBlock="::/0",+ )+ ec2.replace_network_acl_entry(+ NetworkAclId=acl_id,+ RuleNumber=101,+ Protocol="-1",+ RuleAction="allow",+ Egress=True,+ Ipv6CidrBlock="2001:db8::/32",+ )++ entries = ec2.describe_network_acls(NetworkAclIds=[acl_id])["NetworkAcls"][0]["Entries"]+ ipv6_entry = next(entry for entry in entries if entry["RuleNumber"] == 101)+ assert ipv6_entry["Ipv6CidrBlock"] == "2001:db8::/32"+ assert "CidrBlock" not in ipv6_entry+ finally:+ ec2.delete_network_acl(NetworkAclId=acl_id)+ ec2.delete_vpc(VpcId=vpc_id)+def test_ec2_flow_logs_crud(ec2):vpc = ec2.create_vpc(CidrBlock="10.104.0.0/16")vpc_id = vpc["Vpc"]["VpcId"]