Skip to content

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

fix(ec2): emit the public address under the real wire tags, and generate valid ones (#1424)

iot-rocketca9ae5b63ca9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fcb57a5f..da94a05a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **EC2 — instance public IP and DNS reach the SDKs, and generated addresses are addresses** — `DescribeInstances` and `RunInstances` emitted the public address under `publicIpAddress` / `publicDnsName`, which are not the tags the EC2 wire schema defines (`ipAddress` / `dnsName`, per botocore's `ec2-2016-11-15` model), so every SDK dropped both fields silently and `PublicIpAddress` came back absent on instances that had one. They now ride the real tags. Fixing that exposed a second one: `_random_ip` appended two octets whatever it was given, so a one-octet prefix produced `52.55.218` — `AllocateAddress` has been handing back that shape as `PublicIp` all along, and no address parser accepts it. The generator now completes any prefix to four octets. Contributed by @iot-rocket.
- **S3 — `CRC64NVME` checksums are computed instead of refused** — CRC-64/NVME is the algorithm current AWS SDKs and the CLI checksum uploads with by default, and MiniStack answered it with `InvalidRequest` ("requires optional native dependencies"), so a stock `aws s3 cp` or `aws s3api put-object` failed before it began unless the caller knew to set `AWS_REQUEST_CHECKSUM_CALCULATION=when_required`. The algorithm is plain arithmetic — the reflected form of polynomial `0xAD93D23594C93659` with all-ones init and xorout — so it is now computed from a byte-at-a-time table in the stdlib, adding no dependency: `PutObject` and `CopyObject` validate a client-supplied `x-amz-checksum-crc64nvme` (`BadDigest` on mismatch) and `GetObject` / `HeadObject` return it under `x-amz-checksum-mode: ENABLED`, typed `FULL_OBJECT`. The tests pin the implementation to the algorithm's published check value (`b"123456789"` → `0xAE8B14860A799888`) and to a bit-at-a-time reference rather than to itself. `CRC32C` still requires the native `google-crc32c` and is still refused.
## [1.4.19] — 2026-08-16
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.py
index 4afa65c1..5410addd 100644
--- a/ministack/services/ec2.py
+++ b/ministack/services/ec2.py
@@ -3064,8 +3064,10 @@ def _instance_xml(inst):
</placement>
<privateDnsName>{inst['PrivateDnsName']}</privateDnsName>
<privateIpAddress>{inst['PrivateIpAddress']}</privateIpAddress>
- <publicDnsName>{inst['PublicDnsName']}</publicDnsName>
- <publicIpAddress>{inst['PublicIpAddress']}</publicIpAddress>
+ <!-- public address wire tags are dnsName/ipAddress (not publicDnsName/
+ publicIpAddress) - the SDKs map those to PublicDnsName/PublicIpAddress -->
+ <dnsName>{inst['PublicDnsName']}</dnsName>
+ <ipAddress>{inst['PublicIpAddress']}</ipAddress>
<sourceDestCheck>{'true' if inst.get('SourceDestCheck', True) else 'false'}</sourceDestCheck>
<subnetId>{inst['SubnetId']}</subnetId>
<vpcId>{inst['VpcId']}</vpcId>
@@ -3600,8 +3602,17 @@ def _new_igw_id():
def _random_ip(prefix):
- sep = "" if prefix.endswith(".") else "."
- return f"{prefix}{sep}{random.randint(1,254)}.{random.randint(1,254)}"
+ """Random address completing ``prefix`` to four octets.
+
+ Two octets were appended unconditionally, which is right for a two-octet
+ prefix like ``10.0.`` and wrong for a one-octet one: ``_random_ip("52.")``
+ produced ``52.55.218``, which is not an address. Callers hand this straight
+ to ``PublicIp`` on AllocateAddress, so anything parsing the response with
+ ``ipaddress`` or an SDK validator sees a malformed value.
+ """
+ head = [octet for octet in prefix.split(".") if octet]
+ octets = head + [str(random.randint(1, 254)) for _ in range(4 - len(head))]
+ return ".".join(octets)
def _now_ts():
diff --git a/tests/test_ec2.py b/tests/test_ec2.py
index 40ef4d90..5770c371 100644
--- a/tests/test_ec2.py
+++ b/tests/test_ec2.py
@@ -1,4 +1,5 @@
import io
+import ipaddress
import json
import os
import time
@@ -3558,3 +3559,37 @@ def test_modify_security_group_rules_updates_description(ec2):
ec2.delete_security_group(GroupId=sg)
ec2.delete_vpc(VpcId=vpc)
+
+
+def test_ec2_public_ip_and_dns_surface_via_sdk(ec2):
+ """The public address must ride the real wire tags (ipAddress/dnsName) —
+ the previous publicIpAddress/publicDnsName tags are not in the EC2 schema,
+ so boto3 silently dropped both fields and PublicIpAddress was always None."""
+ iid = ec2.run_instances(ImageId="ami-0abcdef1234567890", MinCount=1,
+ MaxCount=1)["Instances"][0]["InstanceId"]
+ try:
+ inst = ec2.describe_instances(InstanceIds=[iid])["Reservations"][0]["Instances"][0]
+ assert inst.get("PublicIpAddress"), "PublicIpAddress missing from the wire"
+ assert inst.get("PublicDnsName"), "PublicDnsName missing from the wire"
+ assert inst["PrivateIpAddress"]
+ # The value has to be an address, not just present. Fixing the tags is
+ # what made this reachable: while the field never left the server, a
+ # malformed one could not be noticed.
+ ipaddress.ip_address(inst["PublicIpAddress"])
+ ipaddress.ip_address(inst["PrivateIpAddress"])
+ finally:
+ ec2.terminate_instances(InstanceIds=[iid])
+
+
+def test_ec2_allocated_address_is_a_valid_ipv4(ec2):
+ """AllocateAddress hands back `PublicIp` verbatim, so a generator that
+ completes a one-octet prefix with only two octets produces `52.55.218`,
+ which no address parser accepts."""
+ alloc = ec2.allocate_address(Domain="vpc")
+ try:
+ ipaddress.ip_address(alloc["PublicIp"])
+ described = ec2.describe_addresses(
+ AllocationIds=[alloc["AllocationId"]])["Addresses"][0]
+ ipaddress.ip_address(described["PublicIp"])
+ finally:
+ ec2.release_address(AllocationId=alloc["AllocationId"])