Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): preserve VPC endpoint IP address types (#5427)
David Phillips47d1e10e5c43
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.javaindex c1dc5102fc..0ceb22d095 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2Tests.java@@ -1027,6 +1027,8 @@ class Ec2Tests {.securityGroupIds(ownGroupId).build());endpointId = created.vpcEndpoint().vpcEndpointId();+ assertThat(created.vpcEndpoint().ipAddressType()).isEqualTo(IpAddressType.IPV4);+ assertThat(created.vpcEndpoint().stateAsString()).isEqualTo("available");// Asserted on the CREATE response and again on DESCRIBE, because Terraform// reads the ids from one and then re-reads them from the other between plan@@ -1039,6 +1041,8 @@ class Ec2Tests {DescribeVpcEndpointsResponse described = ec2.describeVpcEndpoints(DescribeVpcEndpointsRequest.builder().vpcEndpointIds(endpointId).build());VpcEndpoint ep = described.vpcEndpoints().get(0);+ assertThat(ep.ipAddressType()).isEqualTo(IpAddressType.IPV4);+ assertThat(ep.stateAsString()).isEqualTo("available");assertThat(ep.networkInterfaceIds()).as("DescribeVpcEndpoints response")diff --git a/docs/services/ec2.md b/docs/services/ec2.mdindex 1de7ad4e01..86b03076c2 100644--- a/docs/services/ec2.md+++ b/docs/services/ec2.md@@ -222,8 +222,8 @@ Floci seeds the following resources on first use in each region so Terraform, th| ModifyVpcAttribute | Updates supported VPC attributes. || DescribeVpcAttribute | Returns a supported VPC attribute. || DescribeVpcEndpointServices | Lists common AWS interface endpoint services and S3 (gateway and interface) in every availability zone, named per partition (see [Partitions](../configuration/partitions.md)). A `ServiceName` filter is echoed back as asked. |-| CreateVpcEndpoint | Creates a VPC endpoint record, including its `PolicyDocument` and the per-subnet IPv4 and IPv6 addresses named by `SubnetConfiguration.N`. An `Ipv4` value outside the named subnet's CIDR, or among the first four or the last address AWS reserves in it, is rejected with `InvalidParameterValue`. |-| DescribeVpcEndpoints | Lists or returns stored VPC endpoints. |+| CreateVpcEndpoint | Creates a VPC endpoint record, including its `PolicyDocument` and the per-subnet IPv4 and IPv6 addresses named by `SubnetConfiguration.N`. `IpAddressType` defaults to `ipv4`. Interface and gateway address types are stored as metadata. Synthesized endpoint interfaces still carry IPv4 only; IPv6 networking is not emulated. An `Ipv4` value outside the named subnet's CIDR, or among the first four or the last address AWS reserves in it, is rejected with `InvalidParameterValue`. |+| DescribeVpcEndpoints | Lists or returns stored VPC endpoints, including their IP address type. || ModifyVpcEndpoint | Associates or disassociates route tables, subnets and security groups, and sets or resets the endpoint policy. `SubnetConfiguration.N` replaces the addresses pinned for a subnet, under the same address validation as CreateVpcEndpoint. `DnsOptions` and `IpAddressType` are accepted and ignored. || DeleteVpcEndpoints | Deletes VPC endpoint records. || DescribeVpnGateways | Validates filters and returns empty discovery results; explicit IDs return not-found errors. |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 226de8b8c2..b3f0a00ff0 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@@ -2190,7 +2190,7 @@ public class Ec2QueryHandler {p.getFirst("PrivateDnsEnabled") != null ? Boolean.valueOf(p.getFirst("PrivateDnsEnabled")) : null,p.getFirst("PolicyDocument"),parseTagsForResource(p, "vpc-endpoint"),- parseSubnetConfigurations(p));+ parseSubnetConfigurations(p), p.getFirst("IpAddressType"));XmlBuilder xml = new XmlBuilder().start("CreateVpcEndpointResponse", AwsNamespaces.EC2).elem("requestId", UUID.randomUUID().toString())@@ -5829,6 +5829,7 @@ public class Ec2QueryHandler {.elem("vpcId", endpoint.getVpcId()).elem("serviceName", endpoint.getServiceName()).elem("state", endpoint.getState())+ .elem("ipAddressType", endpoint.getIpAddressType()).elem("privateDnsEnabled", String.valueOf(endpoint.isPrivateDnsEnabled()));if (endpoint.getCreationTimestamp() != null) {xml.elem("creationTimestamp", ISO_FMT.format(endpoint.getCreationTimestamp()));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 d775288824..0749dc23a9 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@@ -4867,6 +4867,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, ResettabList<String> securityGroupIds, Boolean privateDnsEnabled,String policyDocument, List<Tag> endpointTags,List<VpcEndpointSubnetConfiguration> subnetConfigurations) {+ return createVpcEndpoint(region, vpcId, serviceName, endpointType, routeTableIds, subnetIds,+ securityGroupIds, privateDnsEnabled, policyDocument, endpointTags, subnetConfigurations, null);+ }++ public VpcEndpoint createVpcEndpoint(String region, String vpcId, String serviceName, String endpointType,+ List<String> routeTableIds, List<String> subnetIds,+ List<String> securityGroupIds, Boolean privateDnsEnabled,+ String policyDocument, List<Tag> endpointTags,+ List<VpcEndpointSubnetConfiguration> subnetConfigurations, String ipAddressType) {+ if (ipAddressType != null && !Set.of("ipv4", "ipv6", "dualstack").contains(ipAddressType)) {+ throw new AwsException("InvalidParameterValue",+ "IpAddressType must be ipv4, ipv6, or dualstack.", 400);+ }ensureDefaultResources(region);getRequiredVpc(region, vpcId);for (String routeTableId : routeTableIds) {@@ -4895,6 +4908,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettabendpoint.setVpcId(vpcId);endpoint.setServiceName(serviceName);endpoint.setVpcEndpointType(endpointType != null && !endpointType.isBlank() ? endpointType : "Gateway");+ endpoint.setIpAddressType(ipAddressType != null ? ipAddressType : "ipv4");boolean isInterface = "Interface".equalsIgnoreCase(endpoint.getVpcEndpointType());endpoint.setPrivateDnsEnabled(privateDnsEnabled != null ? privateDnsEnabled : isInterface);endpoint.setCreationTimestamp(Instant.now());diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpoint.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpoint.javaindex 0b731af7bb..de63856ce0 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpoint.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpoint.java@@ -16,6 +16,7 @@ public class VpcEndpoint {private String serviceName;private String vpcEndpointType = "Gateway";private String state = "available";+ private String ipAddressType = "ipv4";private Instant creationTimestamp;private String region;private List<String> routeTableIds = new ArrayList<>();@@ -49,6 +50,9 @@ public class VpcEndpoint {public String getState() { return state; }public void setState(String state) { this.state = state; }+ public String getIpAddressType() { return ipAddressType; }+ public void setIpAddressType(String ipAddressType) { this.ipAddressType = ipAddressType; }+public Instant getCreationTimestamp() { return creationTimestamp; }public void setCreationTimestamp(Instant creationTimestamp) { this.creationTimestamp = creationTimestamp; }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 a93cc01723..0d9f36cb9d 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@@ -2797,6 +2797,7 @@ class Ec2IntegrationTest {.formParam("VpcId", vpcId).formParam("ServiceName", "com.amazonaws.us-east-1.s3").formParam("VpcEndpointType", "Gateway")+ .formParam("IpAddressType", "ipv4").formParam("RouteTableId.1", routeTableId).formParam("TagSpecification.1.ResourceType", "vpc-endpoint").formParam("TagSpecification.1.Tag.1.Key", "Name")@@ -2808,6 +2809,7 @@ class Ec2IntegrationTest {.statusCode(200).body("CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId", startsWith("vpce-")).body("CreateVpcEndpointResponse.vpcEndpoint.vpcId", equalTo(vpcId))+ .body("CreateVpcEndpointResponse.vpcEndpoint.ipAddressType", equalTo("ipv4")).body("CreateVpcEndpointResponse.vpcEndpoint.routeTableIdSet.item",equalTo(routeTableId)).extract().path("CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId");@@ -2827,7 +2829,9 @@ class Ec2IntegrationTest {.body("DescribeVpcEndpointsResponse.vpcEndpointSet.item.vpcEndpointId",equalTo(vpcEndpointId)).body("DescribeVpcEndpointsResponse.vpcEndpointSet.item.serviceName",- equalTo("com.amazonaws.us-east-1.s3"));+ equalTo("com.amazonaws.us-east-1.s3"))+ .body("DescribeVpcEndpointsResponse.vpcEndpointSet.item.ipAddressType",+ equalTo("ipv4"));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ModifyVpcEndpointTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ModifyVpcEndpointTest.javaindex c94fecda73..b6e1d398a4 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ModifyVpcEndpointTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ModifyVpcEndpointTest.java@@ -1,9 +1,14 @@package io.github.hectorvent.floci.services.ec2;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;import io.quarkus.test.junit.QuarkusTest;import io.restassured.specification.RequestSpecification;import jakarta.inject.Inject;import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.NullAndEmptySource;+import org.junit.jupiter.params.provider.ValueSource;import java.util.ArrayList;import java.util.List;@@ -18,6 +23,7 @@ import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.equalTo;import static org.hamcrest.xml.HasXPath.hasXPath;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNull;/*** ModifyVpcEndpoint. A gateway endpoint is normally declared with its route tables and@@ -32,6 +38,9 @@ class Ec2ModifyVpcEndpointTest {@InjectEc2Service service;+ @Inject+ ObjectMapper objectMapper;+private static final String AUTH_HEADER ="AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";@@ -68,6 +77,90 @@ class Ec2ModifyVpcEndpointTest {return ec2("DescribeVpcEndpoints", "VpcEndpointId.1", endpointId);}+ @Test+ void endpointStatesUseAwsWireValues() {+ Fixture f = fixture("10.89.0.0/16");+ assertEquals("available", xmlValue(describe(f.endpointId()), "state"));+ assertEquals("deleted", service.deleteVpcEndpoints("us-east-1", List.of(f.endpointId())).getFirst().getState());+ }++ @Test+ void savedEndpointStateAndDefaultIpTypeRemainCompatible() throws Exception {+ Fixture f = fixture("10.86.0.0/16");+ VpcEndpoint saved = service.describeVpcEndpoints("us-east-1", List.of(f.endpointId()), Map.of()).getFirst();+ objectMapper.readerForUpdating(saved).readValue("{\"state\":\"available\"}");+ assertEquals("available", xmlValue(describe(f.endpointId()), "state"));+ String filtered = ec2("DescribeVpcEndpoints", "VpcEndpointId.1", f.endpointId(),+ "Filter.1.Name", "vpc-endpoint-state", "Filter.1.Value.1", "available");+ assertEquals(f.endpointId(), xmlValue(filtered, "vpcEndpointId"));+ assertEquals("ipv4", xmlValue(filtered, "ipAddressType"));+ }++ @Test+ void endpointStateFilterPreservesDocumentedSpelling() throws Exception {+ Fixture f = fixture("10.82.0.0/16");+ VpcEndpoint saved = service.describeVpcEndpoints("us-east-1", List.of(f.endpointId()), Map.of()).getFirst();+ objectMapper.readerForUpdating(saved).readValue("{\"state\":\"pendingAcceptance\"}");+ String filtered = ec2("DescribeVpcEndpoints", "VpcEndpointId.1", f.endpointId(),+ "Filter.1.Name", "vpc-endpoint-state", "Filter.1.Value.1", "pendingAcceptance");+ assertEquals(f.endpointId(), xmlValue(filtered, "vpcEndpointId"));+ }++ @ParameterizedTest+ @NullAndEmptySource+ void missingSavedEndpointStateDoesNotBreakFilters(String state) throws Exception {+ Fixture f = fixture("10.84.0.0/16");+ VpcEndpoint saved = service.describeVpcEndpoints("us-east-1", List.of(f.endpointId()), Map.of()).getFirst();+ objectMapper.readerForUpdating(saved).readValue(state == null ? "{\"state\":null}" : "{\"state\":\"\"}");+ String filtered = ec2("DescribeVpcEndpoints", "VpcEndpointId.1", f.endpointId(),+ "Filter.1.Name", "vpc-endpoint-state", "Filter.1.Value.1", "available");+ assertNull(xmlValue(filtered, "vpcEndpointId"));+ }++ @ParameterizedTest+ @ValueSource(strings = {"ipv4", "ipv6", "dualstack"})+ void interfaceAddressTypeRoundTripsAsMetadata(String ipAddressType) {+ String vpcId = xmlValue(ec2("CreateVpc", "CidrBlock", "10.83.0.0/16"), "vpcId");+ String subnetId = xmlValue(ec2("CreateSubnet", "VpcId", vpcId,+ "CidrBlock", "10.83.1.0/24"), "subnetId");+ String created = ec2("CreateVpcEndpoint", "VpcId", vpcId,+ "ServiceName", "com.amazonaws.us-east-1.s3", "VpcEndpointType", "Interface",+ "SubnetId.1", subnetId, "IpAddressType", ipAddressType);+ String endpointId = xmlValue(created, "vpcEndpointId");+ assertEquals(ipAddressType, xmlValue(created, "ipAddressType"));+ assertEquals(ipAddressType, xmlValue(describe(endpointId), "ipAddressType"));+ assertEquals(1, service.endpointNetworkInterfaces("us-east-1").stream()+ .filter(networkInterface -> networkInterface.getSubnetId().equals(subnetId)).count());+ }++ @ParameterizedTest+ @ValueSource(strings = {"ipv4", "ipv6", "dualstack"})+ void endpointIpAddressTypeRoundTrips(String ipAddressType) {+ String vpcId = xmlValue(ec2("CreateVpc", "CidrBlock", "10.87.0.0/16"), "vpcId");+ String created = ec2("CreateVpcEndpoint", "VpcId", vpcId,+ "ServiceName", "com.amazonaws.us-east-1.s3", "IpAddressType", ipAddressType);+ assertEquals(ipAddressType, xmlValue(created, "ipAddressType"));+ assertEquals(ipAddressType, xmlValue(describe(xmlValue(created, "vpcEndpointId")), "ipAddressType"));+ }++ @ParameterizedTest+ @ValueSource(strings = {"bogus", "IPv4", "", " "})+ void invalidEndpointIpAddressTypeDoesNotCreateResources(String ipAddressType) {+ String vpcId = xmlValue(ec2("CreateVpc", "CidrBlock", "10.88.0.0/16"), "vpcId");+ int before = service.describeVpcEndpoints("us-east-1", List.of(), Map.of()).size();+ given().header("Authorization", AUTH_HEADER)+ .formParam("Action", "CreateVpcEndpoint")+ .formParam("VpcId", vpcId)+ .formParam("ServiceName", "com.amazonaws.us-east-1.s3")+ .formParam("IpAddressType", ipAddressType)+ .formParam("TagSpecification.1.ResourceType", "vpc-endpoint")+ .formParam("TagSpecification.1.Tag.1.Key", "Name")+ .formParam("TagSpecification.1.Tag.1.Value", "invalid-ip-type")+ .when().post("/").then().statusCode(400)+ .body(hasXPath("//*[local-name()='Code']", equalTo("InvalidParameterValue")));+ assertEquals(before, service.describeVpcEndpoints("us-east-1", List.of(), Map.of()).size());+ }+@Testvoid addRouteTableIdAssociatesTheRouteTable() {Fixture f = fixture("10.90.0.0/16");