fix(ec2): add missing instance response fields to fix Terraform nil pointer dereference (#871) (#883)

* fix(ec2): add missing instance response fields to fix Terraform nil pointer dereference (#871)

The ENI <attachment> block was absent from Floci's RunInstances and
DescribeInstances XML responses. Terraform provider AWS v5.x iterates
network interfaces and reads iface.Attachment.DeviceIndex without nil-checking
Attachment, causing a panic in resourceInstanceRead.

Additional fields required by the provider were also missing: cpuOptions,
metadataOptions, maintenanceOptions, enclaveOptions, hibernationOptions,
privateDnsNameOptions, capacityReservationSpecification, stateReason,
and blockDeviceMapping.

- Add attachmentId and deviceIndex to InstanceNetworkInterface model
- Populate both fields at ENI creation time in Ec2Service.runInstances
- Emit <attachment> and <privateIpAddressesSet> inside each ENI item in instanceXml()
- Emit all missing instance-level fields in instanceXml()
- Add Ec2InstanceResponseShapeTest compatibility test (16 assertions)
* fix(ec2): fix ec2 unit tests
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-05-18 22:55:22 -05:00
1 parent b65b7aa07f
commit fd660e03ed
7 files changed
+377 -3

No files matched your search

@@ -251,6 +251,12 @@ public class Ec2ContainerManager {
} catch (Exception e) {
LOG.warnv("Error removing EC2 container {0}: {1}", containerId, e.getMessage());
}
try {
// iptables/veth teardown lags behind container removal; prevents port-reuse conflicts.
Thread.sleep(500);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
if (sshHostPort > 0) {
portAllocator.release(sshHostPort);
@@ -1176,9 +1176,66 @@ public class Ec2QueryHandler {
.elem("groupName", gi.getGroupName())
.end("item");
}
xml.end("groupSet").end("item");
xml.end("groupSet")
.start("attachment")
.elem("attachmentId", eni.getAttachmentId())
.elem("deviceIndex", String.valueOf(eni.getDeviceIndex()))
.elem("status", "attached")
.elem("deleteOnTermination", "true")
.end("attachment")
.start("privateIpAddressesSet")
.start("item")
.elem("privateIpAddress", eni.getPrivateIpAddress())
.elem("privateDnsName", eni.getPrivateDnsName())
.elem("primary", "true")
.end("item")
.end("privateIpAddressesSet")
.end("item");
}
xml.end("networkInterfaceSet");
xml.elem("clientToken", inst.getClientToken())
.start("stateReason")
.elem("code", "")
.elem("message", "")
.end("stateReason")
.start("cpuOptions")
.elem("coreCount", "1")
.elem("threadsPerCore", "1")
.end("cpuOptions")
.start("metadataOptions")
.elem("state", "applied")
.elem("httpTokens", "optional")
.elem("httpPutResponseHopLimit", "1")
.elem("httpEndpoint", "enabled")
.elem("httpProtocolIpv6", "disabled")
.elem("instanceMetadataTags", "disabled")
.end("metadataOptions")
.start("maintenanceOptions")
.elem("autoRecovery", "default")
.end("maintenanceOptions")
.start("enclaveOptions")
.elem("enabled", "false")
.end("enclaveOptions")
.start("hibernationOptions")
.elem("configured", "false")
.end("hibernationOptions")
.start("privateDnsNameOptions")
.elem("hostnameType", "ip-name")
.elem("enableResourceNameDnsARecord", "false")
.elem("enableResourceNameDnsAAAARecord", "false")
.end("privateDnsNameOptions")
.start("capacityReservationSpecification")
.elem("capacityReservationPreference", "open")
.end("capacityReservationSpecification")
.start("blockDeviceMapping")
.start("item")
.elem("deviceName", inst.getRootDeviceName())
.start("ebs")
.elem("status", "attached")
.elem("deleteOnTermination", "true")
.end("ebs")
.end("item")
.end("blockDeviceMapping");
xml.raw(tagSetXml(inst.getTags()));
return xml.build();
}
@@ -237,7 +237,7 @@ public class Ec2Service {
Instance inst = new Instance();
inst.setInstanceId(instanceId);
inst.setImageId(imageId != null ? imageId : "ami-default");
inst.setState(InstanceState.running());
inst.setState(InstanceState.pending());
inst.setInstanceType(instanceType != null ? instanceType : "t2.micro");
inst.setPlacement(new Placement(az));
inst.setSubnetId(finalSubnetId);
@@ -267,6 +267,8 @@ public class Ec2Service {
eni.setPrivateIpAddress(privateIp);
eni.setPrivateDnsName(inst.getPrivateDnsName());
eni.setGroups(new ArrayList<>(sgIdentifiers));
eni.setAttachmentId("eni-attach-" + randomHex(17));
eni.setDeviceIndex(0);
inst.getNetworkInterfaces().add(eni);
instances.put(key(region, instanceId), inst);
@@ -315,6 +317,11 @@ public class Ec2Service {
}
}
}
if (config.services().ec2().mock()) {
instances.values().stream()
.filter(i -> i.getRegion().equals(region) && "pending".equals(i.getState().getName()))
.forEach(i -> i.setState(InstanceState.running()));
}
List<Instance> matched = instances.values().stream()
.filter(i -> i.getRegion().equals(region))
.filter(i -> instanceIds.isEmpty() || instanceIds.contains(i.getInstanceId()))
@@ -341,6 +348,9 @@ public class Ec2Service {
if (inst == null) {
throw new AwsException("InvalidInstanceID.NotFound", "The instance ID '" + id + "' does not exist", 400);
}
if (config.services().ec2().mock() && "pending".equals(inst.getState().getName())) {
inst.setState(InstanceState.running());
}
InstanceState prev = inst.getState();
if (config.services().ec2().mock()) {
inst.setState(InstanceState.terminated());
@@ -367,6 +377,9 @@ public class Ec2Service {
if (inst == null) {
throw new AwsException("InvalidInstanceID.NotFound", "The instance ID '" + id + "' does not exist", 400);
}
if (config.services().ec2().mock() && "pending".equals(inst.getState().getName())) {
inst.setState(InstanceState.running());
}
InstanceState prev = inst.getState();
if (config.services().ec2().mock()) {
inst.setState(InstanceState.stopped());
@@ -439,6 +452,12 @@ public class Ec2Service {
public List<Instance> describeInstanceStatus(String region, List<String> instanceIds) {
ensureDefaultResources(region);
if (config.services().ec2().mock()) {
instances.values().stream()
.filter(i -> i.getRegion().equals(region) && "pending".equals(i.getState().getName()))
.filter(i -> instanceIds.isEmpty() || instanceIds.contains(i.getInstanceId()))
.forEach(i -> i.setState(InstanceState.running()));
}
return instances.values().stream()
.filter(i -> i.getRegion().equals(region))
.filter(i -> instanceIds.isEmpty() || instanceIds.contains(i.getInstanceId()))
@@ -21,6 +21,8 @@ public class InstanceNetworkInterface {
private String privateDnsName;
private boolean sourceDestCheck = true;
private List<GroupIdentifier> groups = new ArrayList<>();
private String attachmentId;
private int deviceIndex;
public InstanceNetworkInterface() {}
@@ -56,4 +58,10 @@ public class InstanceNetworkInterface {
public List<GroupIdentifier> getGroups() { return groups; }
public void setGroups(List<GroupIdentifier> groups) { this.groups = groups; }
public String getAttachmentId() { return attachmentId; }
public void setAttachmentId(String attachmentId) { this.attachmentId = attachmentId; }
public int getDeviceIndex() { return deviceIndex; }
public void setDeviceIndex(int deviceIndex) { this.deviceIndex = deviceIndex; }
}