Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): add missing instance response fields to fix Terraform nil pointer dereference (#871) (#883)
Hector Venturafd660e03ed39
diff --git a/.github/workflows/compatibility.yml b/.github/workflows/compatibility.ymlindex 0cc6f346b8..3a96126ccf 100644--- a/.github/workflows/compatibility.yml+++ b/.github/workflows/compatibility.yml@@ -98,6 +98,7 @@ jobs:-e FLOCI_HOSTNAME=floci \-e FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED=true \-e FLOCI_TLS_ENABLED=true \+ -e FLOCI_SERVICES_EC2_MOCK=true \floci:test- name: Wait for floci to be readydiff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2InstanceResponseShapeTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2InstanceResponseShapeTest.javanew file mode 100644index 0000000000..d41c0b2cfa--- /dev/null+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2InstanceResponseShapeTest.java@@ -0,0 +1,283 @@+package com.floci.test;++import org.junit.jupiter.api.*;+import software.amazon.awssdk.services.ec2.Ec2Client;+import software.amazon.awssdk.services.ec2.model.*;++import static org.assertj.core.api.Assertions.*;++/**+ * Verifies that RunInstances / DescribeInstances responses include all fields+ * that the Terraform AWS provider (v5+) dereferences without nil-checks.+ *+ * Issue: https://github.com/floci-io/floci/issues/871+ *+ * The crash in terraform-provider-aws resourceInstanceRead is caused by nil+ * pointer dereferences on missing response fields — most critically the ENI+ * attachment block, which Terraform reads to identify the primary network+ * interface by DeviceIndex.+ */+@DisplayName("EC2 Instance Response Shape (issue #871)")+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class Ec2InstanceResponseShapeTest {++ private static Ec2Client ec2;+ private static String instanceId;+ private static String instanceIdForCleanup;+ private static Instance instance;++ @BeforeAll+ static void setup() {+ ec2 = TestFixtures.ec2Client();++ RunInstancesResponse run = ec2.runInstances(RunInstancesRequest.builder()+ .imageId("ami-0abcdef1234567890")+ .instanceType(InstanceType.T3_MICRO)+ .minCount(1)+ .maxCount(1)+ .build());++ instanceId = run.instances().get(0).instanceId();+ instanceIdForCleanup = instanceId;++ DescribeInstancesResponse describe = ec2.describeInstances(+ DescribeInstancesRequest.builder().instanceIds(instanceId).build());++ instance = describe.reservations().get(0).instances().get(0);+ }++ @AfterAll+ static void cleanup() {+ if (ec2 != null) {+ try {+ if (instanceId != null) {+ ec2.terminateInstances(TerminateInstancesRequest.builder()+ .instanceIds(instanceId).build());+ }+ if (instanceIdForCleanup != null) {+ for (int i = 0; i < 30; i++) {+ Thread.sleep(1000);+ DescribeInstancesResponse r = ec2.describeInstances(+ DescribeInstancesRequest.builder().instanceIds(instanceIdForCleanup).build());+ if (!r.reservations().isEmpty() && !r.reservations().get(0).instances().isEmpty()) {+ InstanceStateName state = r.reservations().get(0).instances().get(0).state().name();+ if (state == InstanceStateName.TERMINATED) break;+ }+ }+ }+ } catch (Exception ignored) {}+ ec2.close();+ }+ }++ // ── Core instance fields ─────────────────────────────────────────────────++ @Test+ @Order(1)+ @DisplayName("instanceId and state are present")+ void instanceIdAndState() {+ assertThat(instance.instanceId()).isNotNull().startsWith("i-");+ assertThat(instance.state()).isNotNull();+ assertThat(instance.state().name()).isNotNull();+ }++ @Test+ @Order(2)+ @DisplayName("cpuOptions is not null with coreCount and threadsPerCore")+ void cpuOptions() {+ assertThat(instance.cpuOptions())+ .as("cpuOptions must not be null — Terraform dereferences CpuOptions.CoreCount directly")+ .isNotNull();+ assertThat(instance.cpuOptions().coreCount())+ .as("cpuOptions.coreCount must not be null")+ .isNotNull()+ .isGreaterThan(0);+ assertThat(instance.cpuOptions().threadsPerCore())+ .as("cpuOptions.threadsPerCore must not be null")+ .isNotNull()+ .isGreaterThan(0);+ }++ @Test+ @Order(3)+ @DisplayName("metadataOptions is not null with required sub-fields")+ void metadataOptions() {+ assertThat(instance.metadataOptions())+ .as("metadataOptions must not be null — Terraform reads HttpTokens/HttpEndpoint")+ .isNotNull();+ assertThat(instance.metadataOptions().httpTokens())+ .as("metadataOptions.httpTokens must not be null")+ .isNotNull();+ assertThat(instance.metadataOptions().httpEndpoint())+ .as("metadataOptions.httpEndpoint must not be null")+ .isNotNull();+ assertThat(instance.metadataOptions().httpPutResponseHopLimit())+ .as("metadataOptions.httpPutResponseHopLimit must not be null")+ .isNotNull();+ }++ @Test+ @Order(4)+ @DisplayName("maintenanceOptions is not null")+ void maintenanceOptions() {+ assertThat(instance.maintenanceOptions())+ .as("maintenanceOptions must not be null — Terraform reads AutoRecovery")+ .isNotNull();+ assertThat(instance.maintenanceOptions().autoRecovery())+ .as("maintenanceOptions.autoRecovery must not be null")+ .isNotNull();+ }++ @Test+ @Order(5)+ @DisplayName("enclaveOptions is not null")+ void enclaveOptions() {+ assertThat(instance.enclaveOptions())+ .as("enclaveOptions must not be null — Terraform reads Enabled flag")+ .isNotNull();+ assertThat(instance.enclaveOptions().enabled())+ .as("enclaveOptions.enabled must not be null")+ .isNotNull();+ }++ @Test+ @Order(6)+ @DisplayName("hibernationOptions is not null")+ void hibernationOptions() {+ assertThat(instance.hibernationOptions())+ .as("hibernationOptions must not be null — Terraform reads Configured flag")+ .isNotNull();+ assertThat(instance.hibernationOptions().configured())+ .as("hibernationOptions.configured must not be null")+ .isNotNull();+ }++ @Test+ @Order(7)+ @DisplayName("privateDnsNameOptions is not null with hostnameType")+ void privateDnsNameOptions() {+ assertThat(instance.privateDnsNameOptions())+ .as("privateDnsNameOptions must not be null — Terraform reads HostnameType")+ .isNotNull();+ assertThat(instance.privateDnsNameOptions().hostnameType())+ .as("privateDnsNameOptions.hostnameType must not be null")+ .isNotNull();+ }++ @Test+ @Order(8)+ @DisplayName("capacityReservationSpecification is not null")+ void capacityReservationSpecification() {+ assertThat(instance.capacityReservationSpecification())+ .as("capacityReservationSpecification must not be null — Terraform reads Preference")+ .isNotNull();+ assertThat(instance.capacityReservationSpecification().capacityReservationPreference())+ .as("capacityReservationSpecification.preference must not be null")+ .isNotNull();+ }++ @Test+ @Order(9)+ @DisplayName("stateReason is not null")+ void stateReason() {+ assertThat(instance.stateReason())+ .as("stateReason must not be null — Terraform reads Code and Message")+ .isNotNull();+ }++ @Test+ @Order(10)+ @DisplayName("blockDeviceMappings contains root device entry")+ void blockDeviceMappings() {+ assertThat(instance.blockDeviceMappings())+ .as("blockDeviceMappings must not be empty — Terraform reads root_block_device attributes")+ .isNotEmpty();+ InstanceBlockDeviceMapping rootDevice = instance.blockDeviceMappings().get(0);+ assertThat(rootDevice.deviceName())+ .as("blockDeviceMapping deviceName must not be null")+ .isNotNull();+ assertThat(rootDevice.ebs())+ .as("blockDeviceMapping ebs must not be null")+ .isNotNull();+ assertThat(rootDevice.ebs().deleteOnTermination())+ .as("blockDeviceMapping ebs.deleteOnTermination must not be null")+ .isNotNull();+ }++ // ── Primary network interface (ENI) ──────────────────────────────────────++ @Test+ @Order(11)+ @DisplayName("networkInterfaces contains at least one ENI")+ void networkInterfacesNotEmpty() {+ assertThat(instance.networkInterfaces())+ .as("networkInterfaces must not be empty")+ .isNotEmpty();+ }++ @Test+ @Order(12)+ @DisplayName("primary ENI has non-null attachment — critical for Terraform DeviceIndex read")+ void eniAttachmentNotNull() {+ InstanceNetworkInterface primaryEni = instance.networkInterfaces().get(0);+ assertThat(primaryEni.attachment())+ .as("ENI attachment must not be null — Terraform reads Attachment.DeviceIndex " ++ "to find the primary interface; nil causes panic in resourceInstanceRead")+ .isNotNull();+ }++ @Test+ @Order(13)+ @DisplayName("primary ENI attachment has deviceIndex == 0")+ void eniAttachmentDeviceIndex() {+ InstanceNetworkInterface primaryEni = instance.networkInterfaces().get(0);+ assertThat(primaryEni.attachment().deviceIndex())+ .as("Primary ENI must have deviceIndex 0")+ .isNotNull()+ .isEqualTo(0);+ }++ @Test+ @Order(14)+ @DisplayName("primary ENI attachment has attachmentId")+ void eniAttachmentId() {+ InstanceNetworkInterface primaryEni = instance.networkInterfaces().get(0);+ assertThat(primaryEni.attachment().attachmentId())+ .as("ENI attachmentId must not be null")+ .isNotNull()+ .startsWith("eni-attach-");+ }++ @Test+ @Order(15)+ @DisplayName("primary ENI has privateIpAddresses list")+ void eniPrivateIpAddresses() {+ InstanceNetworkInterface primaryEni = instance.networkInterfaces().get(0);+ assertThat(primaryEni.privateIpAddresses())+ .as("ENI privateIpAddresses must not be empty")+ .isNotEmpty();+ assertThat(primaryEni.privateIpAddresses().get(0).privateIpAddress())+ .as("ENI primary private IP must not be null")+ .isNotNull();+ assertThat(primaryEni.privateIpAddresses().get(0).primary())+ .as("ENI first privateIpAddress must have primary=true")+ .isNotNull()+ .isTrue();+ }++ // ── TerminateInstances state transitions ─────────────────────────────────++ @Test+ @Order(20)+ @DisplayName("TerminateInstances returns correct state transition")+ void terminateInstances() {+ TerminateInstancesResponse resp = ec2.terminateInstances(+ TerminateInstancesRequest.builder().instanceIds(instanceId).build());++ assertThat(resp.terminatingInstances()).isNotEmpty();+ InstanceStateChange change = resp.terminatingInstances().get(0);+ assertThat(change.instanceId()).isEqualTo(instanceId);+ assertThat(change.currentState().name()).isEqualTo(InstanceStateName.SHUTTING_DOWN);+ instanceId = null;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.javaindex 39569f631e..ea7ed904ef 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java@@ -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);diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.javaindex 5734f59826..9b9d2b62df 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java@@ -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();}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.javaindex 59fca84fde..6c0f8212bc 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java@@ -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()))diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/InstanceNetworkInterface.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/InstanceNetworkInterface.javaindex 5d1f590192..97de239bdb 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/InstanceNetworkInterface.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/InstanceNetworkInterface.java@@ -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; }}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.javaindex 34d5a558e1..fa4e0f69f7 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java@@ -618,7 +618,7 @@ class Ec2IntegrationTest {.then().statusCode(200).body("RunInstancesResponse.instancesSet.item.instanceId", startsWith("i-"))- .body("RunInstancesResponse.instancesSet.item.instanceState.name", equalTo("running"))+ .body("RunInstancesResponse.instancesSet.item.instanceState.name", equalTo("pending")).body("RunInstancesResponse.instancesSet.item.instanceType", equalTo("t2.micro")).body("RunInstancesResponse.instancesSet.item.keyName", equalTo("test-key")).extract().path("RunInstancesResponse.instancesSet.item.instanceId");