Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): persist VPC DNS attributes and support DescribeVpcEndpointServices (#624)

Hector Venturad28b175cb0a2
diff --git a/compatibility-tests/compat-opentofu/main.tf b/compatibility-tests/compat-opentofu/main.tf
index 6a318b67c5..64cc90d5f4 100644
--- a/compatibility-tests/compat-opentofu/main.tf
+++ b/compatibility-tests/compat-opentofu/main.tf
@@ -129,3 +129,19 @@ output "table_name" {
output "secret_arn" {
value = aws_secretsmanager_secret.db_creds.arn
}
+
+# ── VPC (issue #468: ModifyVpcAttribute / DescribeVpcAttribute) ────────────
+resource "aws_vpc" "compat" {
+ cidr_block = "10.0.0.0/16"
+ enable_dns_support = false
+ enable_dns_hostnames = false
+
+ tags = {
+ Name = "floci-compat-vpc"
+ Environment = "compat-test"
+ }
+}
+
+output "vpc_id" {
+ value = aws_vpc.compat.id
+}
diff --git a/compatibility-tests/compat-opentofu/provider.tf b/compatibility-tests/compat-opentofu/provider.tf
index bd5b08cbc3..0c248cf4ed 100644
--- a/compatibility-tests/compat-opentofu/provider.tf
+++ b/compatibility-tests/compat-opentofu/provider.tf
@@ -34,5 +34,6 @@ provider "aws" {
sts = var.endpoint
ssm = var.endpoint
secretsmanager = var.endpoint
+ ec2 = var.endpoint
}
}
diff --git a/compatibility-tests/compat-opentofu/test/opentofu.bats b/compatibility-tests/compat-opentofu/test/opentofu.bats
index 0873cb65a0..532acee988 100644
--- a/compatibility-tests/compat-opentofu/test/opentofu.bats
+++ b/compatibility-tests/compat-opentofu/test/opentofu.bats
@@ -95,3 +95,31 @@ setup() {
assert_success
assert_output --partial "floci-compat"
}
+
+@test "OpenTofu: VPC created with custom DNS settings" {
+ run aws_cmd ec2 describe-vpcs \
+ --filters "Name=tag:Name,Values=floci-compat-vpc"
+ assert_success
+ assert_output --partial "floci-compat-vpc"
+ assert_output --partial "10.0.0.0/16"
+}
+
+@test "OpenTofu: VPC enableDnsSupport persisted as false" {
+ VPC_ID=$(aws_cmd ec2 describe-vpcs \
+ --filters "Name=tag:Name,Values=floci-compat-vpc" \
+ --query 'Vpcs[0].VpcId' --output text)
+ run aws_cmd ec2 describe-vpc-attribute \
+ --vpc-id "$VPC_ID" --attribute enableDnsSupport
+ assert_success
+ assert_output --partial '"Value": false'
+}
+
+@test "OpenTofu: VPC enableDnsHostnames persisted as false" {
+ VPC_ID=$(aws_cmd ec2 describe-vpcs \
+ --filters "Name=tag:Name,Values=floci-compat-vpc" \
+ --query 'Vpcs[0].VpcId' --output text)
+ run aws_cmd ec2 describe-vpc-attribute \
+ --vpc-id "$VPC_ID" --attribute enableDnsHostnames
+ assert_success
+ assert_output --partial '"Value": false'
+}
diff --git a/compatibility-tests/compat-terraform/main.tf b/compatibility-tests/compat-terraform/main.tf
index ca31e0b68d..0043743334 100644
--- a/compatibility-tests/compat-terraform/main.tf
+++ b/compatibility-tests/compat-terraform/main.tf
@@ -207,3 +207,19 @@ resource "aws_cloudwatch_metric_alarm" "cpu" {
output "alarm_arn" {
value = aws_cloudwatch_metric_alarm.cpu.arn
}
+
+# -- VPC (issue #468: ModifyVpcAttribute / DescribeVpcAttribute) ---------------
+resource "aws_vpc" "compat" {
+ cidr_block = "10.0.0.0/16"
+ enable_dns_support = false
+ enable_dns_hostnames = false
+
+ tags = {
+ Name = "floci-compat-vpc"
+ Environment = "compat-test"
+ }
+}
+
+output "vpc_id" {
+ value = aws_vpc.compat.id
+}
diff --git a/compatibility-tests/compat-terraform/provider.tf b/compatibility-tests/compat-terraform/provider.tf
index 4a79e021f4..8e5df9b206 100644
--- a/compatibility-tests/compat-terraform/provider.tf
+++ b/compatibility-tests/compat-terraform/provider.tf
@@ -37,5 +37,6 @@ provider "aws" {
cognitoidp = var.endpoint
rds = var.endpoint
cloudwatch = var.endpoint
+ ec2 = var.endpoint
}
}
diff --git a/compatibility-tests/compat-terraform/test/terraform.bats b/compatibility-tests/compat-terraform/test/terraform.bats
index c02ecd5df9..09e52148b5 100644
--- a/compatibility-tests/compat-terraform/test/terraform.bats
+++ b/compatibility-tests/compat-terraform/test/terraform.bats
@@ -114,3 +114,31 @@ setup() {
assert_success
assert_output --partial "compat-test"
}
+
+@test "Terraform: VPC created with custom DNS settings" {
+ run aws_cmd ec2 describe-vpcs \
+ --filters "Name=tag:Name,Values=floci-compat-vpc"
+ assert_success
+ assert_output --partial "floci-compat-vpc"
+ assert_output --partial "10.0.0.0/16"
+}
+
+@test "Terraform: VPC enableDnsSupport persisted as false" {
+ VPC_ID=$(aws_cmd ec2 describe-vpcs \
+ --filters "Name=tag:Name,Values=floci-compat-vpc" \
+ --query 'Vpcs[0].VpcId' --output text)
+ run aws_cmd ec2 describe-vpc-attribute \
+ --vpc-id "$VPC_ID" --attribute enableDnsSupport
+ assert_success
+ assert_output --partial '"Value": false'
+}
+
+@test "Terraform: VPC enableDnsHostnames persisted as false" {
+ VPC_ID=$(aws_cmd ec2 describe-vpcs \
+ --filters "Name=tag:Name,Values=floci-compat-vpc" \
+ --query 'Vpcs[0].VpcId' --output text)
+ run aws_cmd ec2 describe-vpc-attribute \
+ --vpc-id "$VPC_ID" --attribute enableDnsHostnames
+ assert_success
+ assert_output --partial '"Value": false'
+}
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.java
index f2ea33e57f..ebe7a3cf8b 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
@@ -613,6 +613,37 @@ class Ec2Tests {
@Test
@Order(47)
+ @DisplayName("ModifyVpcAttribute - set enableDnsSupport=false")
+ void modifyVpcAttributeDnsSupport() {
+ ec2.modifyVpcAttribute(r -> r.vpcId(vpcId)
+ .enableDnsSupport(a -> a.value(false)));
+ }
+
+ @Test
+ @Order(48)
+ @DisplayName("DescribeVpcAttribute - enableDnsSupport round-trip")
+ void describeVpcAttributeDnsSupport() {
+ DescribeVpcAttributeResponse resp = ec2.describeVpcAttribute(r -> r
+ .vpcId(vpcId)
+ .attribute(VpcAttributeName.ENABLE_DNS_SUPPORT));
+
+ assertThat(resp.vpcId()).isEqualTo(vpcId);
+ assertThat(resp.enableDnsSupport().value()).isFalse();
+ }
+
+ @Test
+ @Order(49)
+ @DisplayName("DescribeVpcEndpointServices - returns empty list")
+ void describeVpcEndpointServices() {
+ DescribeVpcEndpointServicesResponse resp = ec2.describeVpcEndpointServices(
+ DescribeVpcEndpointServicesRequest.builder().build());
+
+ assertThat(resp.serviceNames()).isEmpty();
+ assertThat(resp.serviceDetails()).isEmpty();
+ }
+
+ @Test
+ @Order(50)
@DisplayName("DeleteVpc - delete VPC")
void deleteVpc() {
ec2.deleteVpc(DeleteVpcRequest.builder().vpcId(vpcId).build());
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java
index 8e02cb19d8..8097eb63f7 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java
@@ -121,6 +121,7 @@ public class AwsQueryController {
"RunInstances", "DescribeInstances", "TerminateInstances", "StartInstances", "StopInstances",
"RebootInstances", "DescribeInstanceStatus", "DescribeInstanceAttribute", "ModifyInstanceAttribute",
"CreateVpc", "DescribeVpcs", "DeleteVpc", "ModifyVpcAttribute", "DescribeVpcAttribute",
+ "DescribeVpcEndpointServices",
"CreateDefaultVpc", "AssociateVpcCidrBlock", "DisassociateVpcCidrBlock",
"CreateSubnet", "DescribeSubnets", "DeleteSubnet", "ModifySubnetAttribute",
"CreateSecurityGroup", "DescribeSecurityGroups", "DeleteSecurityGroup",
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.java
index a9acfd6bea..93e7b9003e 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
@@ -50,6 +50,7 @@ public class Ec2QueryHandler {
case "DeleteVpc" -> handleDeleteVpc(params, region);
case "ModifyVpcAttribute" -> handleModifyVpcAttribute(params, region);
case "DescribeVpcAttribute" -> handleDescribeVpcAttribute(params, region);
+ case "DescribeVpcEndpointServices" -> handleDescribeVpcEndpointServices(params, region);
case "CreateDefaultVpc" -> handleCreateDefaultVpc(params, region);
case "AssociateVpcCidrBlock" -> handleAssociateVpcCidrBlock(params, region);
case "DisassociateVpcCidrBlock" -> handleDisassociateVpcCidrBlock(params, region);
@@ -423,6 +424,22 @@ public class Ec2QueryHandler {
private Response handleCreateVpc(MultivaluedMap<String, String> p, String region) {
String cidrBlock = p.getFirst("CidrBlock");
Vpc vpc = service.createVpc(region, cidrBlock, false);
+ List<Tag> vpcTags = new ArrayList<>();
+ for (int i = 1; ; i++) {
+ String resType = p.getFirst("TagSpecification." + i + ".ResourceType");
+ if (resType == null) break;
+ if ("vpc".equals(resType)) {
+ for (int j = 1; ; j++) {
+ String k = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Key");
+ if (k == null) break;
+ String v = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Value");
+ vpcTags.add(new Tag(k, v));
+ }
+ }
+ }
+ if (!vpcTags.isEmpty()) {
+ service.createTags(region, List.of(vpc.getVpcId()), vpcTags);
+ }
XmlBuilder xml = new XmlBuilder()
.start("CreateVpcResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -453,7 +470,13 @@ public class Ec2QueryHandler {
private Response handleModifyVpcAttribute(MultivaluedMap<String, String> p, String region) {
String vpcId = p.getFirst("VpcId");
- service.modifyVpcAttribute(region, vpcId, "attribute", "value");
+ if (p.containsKey("EnableDnsSupport.Value")) {
+ service.modifyVpcAttribute(region, vpcId, "enableDnsSupport", p.getFirst("EnableDnsSupport.Value"));
+ } else if (p.containsKey("EnableDnsHostnames.Value")) {
+ service.modifyVpcAttribute(region, vpcId, "enableDnsHostnames", p.getFirst("EnableDnsHostnames.Value"));
+ } else if (p.containsKey("EnableNetworkAddressUsageMetrics.Value")) {
+ service.modifyVpcAttribute(region, vpcId, "enableNetworkAddressUsageMetrics", p.getFirst("EnableNetworkAddressUsageMetrics.Value"));
+ }
return booleanResponse("ModifyVpcAttribute");
}
@@ -466,14 +489,26 @@ public class Ec2QueryHandler {
.elem("requestId", UUID.randomUUID().toString())
.elem("vpcId", vpcId);
if ("enableDnsSupport".equals(attribute)) {
- xml.start("enableDnsSupport").elem("value", "true").end("enableDnsSupport");
+ xml.start("enableDnsSupport").elem("value", String.valueOf(vpc.isEnableDnsSupport())).end("enableDnsSupport");
} else if ("enableDnsHostnames".equals(attribute)) {
- xml.start("enableDnsHostnames").elem("value", "true").end("enableDnsHostnames");
+ xml.start("enableDnsHostnames").elem("value", String.valueOf(vpc.isEnableDnsHostnames())).end("enableDnsHostnames");
+ } else if ("enableNetworkAddressUsageMetrics".equals(attribute)) {
+ xml.start("enableNetworkAddressUsageMetrics").elem("value", String.valueOf(vpc.isEnableNetworkAddressUsageMetrics())).end("enableNetworkAddressUsageMetrics");
}
xml.end("DescribeVpcAttributeResponse");
return xmlResponse(xml.build());
}
+ private Response handleDescribeVpcEndpointServices(MultivaluedMap<String, String> p, String region) {
+ XmlBuilder xml = new XmlBuilder()
+ .start("DescribeVpcEndpointServicesResponse", AwsNamespaces.EC2)
+ .elem("requestId", UUID.randomUUID().toString())
+ .start("serviceNameSet").end("serviceNameSet")
+ .start("serviceDetailSet").end("serviceDetailSet")
+ .end("DescribeVpcEndpointServicesResponse");
+ return xmlResponse(xml.build());
+ }
+
private Response handleCreateDefaultVpc(MultivaluedMap<String, String> p, String region) {
Vpc vpc = service.createDefaultVpc(region);
XmlBuilder xml = new XmlBuilder()
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.java
index a640a2ab8c..41a64decbc 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
@@ -436,7 +436,12 @@ public class Ec2Service {
if (vpc == null) {
throw new AwsException("InvalidVpcID.NotFound", "The vpc ID '" + vpcId + "' does not exist", 400);
}
- // no-op for mock
+ switch (attribute) {
+ case "enableDnsSupport" -> vpc.setEnableDnsSupport(Boolean.parseBoolean(value));
+ case "enableDnsHostnames" -> vpc.setEnableDnsHostnames(Boolean.parseBoolean(value));
+ case "enableNetworkAddressUsageMetrics" -> vpc.setEnableNetworkAddressUsageMetrics(Boolean.parseBoolean(value));
+ }
+ vpcs.put(key(region, vpcId), vpc);
}
public Vpc describeVpcAttribute(String region, String vpcId, String attribute) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Vpc.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Vpc.java
index ec5cd8dee0..7861cb8f31 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Vpc.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Vpc.java
@@ -19,6 +19,9 @@ public class Vpc {
private String ownerId;
private String region;
private List<VpcCidrBlockAssociation> cidrBlockAssociationSet = new ArrayList<>();
+ private boolean enableDnsSupport = true;
+ private boolean enableDnsHostnames = false;
+ private boolean enableNetworkAddressUsageMetrics = false;
private List<Tag> tags = new ArrayList<>();
public Vpc() {}
@@ -50,6 +53,15 @@ public class Vpc {
public List<VpcCidrBlockAssociation> getCidrBlockAssociationSet() { return cidrBlockAssociationSet; }
public void setCidrBlockAssociationSet(List<VpcCidrBlockAssociation> cidrBlockAssociationSet) { this.cidrBlockAssociationSet = cidrBlockAssociationSet; }
+ public boolean isEnableDnsSupport() { return enableDnsSupport; }
+ public void setEnableDnsSupport(boolean enableDnsSupport) { this.enableDnsSupport = enableDnsSupport; }
+
+ public boolean isEnableDnsHostnames() { return enableDnsHostnames; }
+ public void setEnableDnsHostnames(boolean enableDnsHostnames) { this.enableDnsHostnames = enableDnsHostnames; }
+
+ public boolean isEnableNetworkAddressUsageMetrics() { return enableNetworkAddressUsageMetrics; }
+ public void setEnableNetworkAddressUsageMetrics(boolean enableNetworkAddressUsageMetrics) { this.enableNetworkAddressUsageMetrics = enableNetworkAddressUsageMetrics; }
+
public List<Tag> getTags() { return tags; }
public void setTags(List<Tag> tags) { this.tags = tags; }
}
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.java
index acaa8e6998..eed34acc47 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
@@ -205,7 +205,7 @@ class Ec2IntegrationTest {
given()
.formParam("Action", "ModifyVpcAttribute")
.formParam("VpcId", vpcId)
- .formParam("EnableDnsSupport.Value", "true")
+ .formParam("EnableDnsSupport.Value", "false")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
@@ -225,7 +225,20 @@ class Ec2IntegrationTest {
.post("/")
.then()
.statusCode(200)
- .body("DescribeVpcAttributeResponse.vpcId", equalTo(vpcId));
+ .body("DescribeVpcAttributeResponse.vpcId", equalTo(vpcId))
+ .body("DescribeVpcAttributeResponse.enableDnsSupport.value", equalTo("false"));
+ }
+
+ @Test
+ @Order(14)
+ void describeVpcEndpointServices() {
+ given()
+ .formParam("Action", "DescribeVpcEndpointServices")
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
}
// =========================================================================