Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(ec2): honor DHCP options dry runs and restore default set (#6448)

Jimmy Liu Jia4244e64833af
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 71f384c1d0..fb5f5c3c71 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
@@ -1701,6 +1701,7 @@ public class Ec2QueryHandler {
configurations.add(new DhcpConfiguration(key, values));
}
List<Tag> tags = parseTagsForResource(p, "dhcp-options");
+ checkDryRun(p);
DhcpOptions options = service.createDhcpOptions(region, configurations);
try {
applyResourceTags(tags, region, options.getDhcpOptionsId());
@@ -1763,11 +1764,13 @@ public class Ec2QueryHandler {
}
private Response handleAssociateDhcpOptions(MultivaluedMap<String, String> p, String region) {
+ checkDryRun(p);
service.associateDhcpOptions(region, p.getFirst("DhcpOptionsId"), p.getFirst("VpcId"));
return booleanResponse("AssociateDhcpOptions");
}
private Response handleDeleteDhcpOptions(MultivaluedMap<String, String> p, String region) {
+ checkDryRun(p);
service.deleteDhcpOptions(region, p.getFirst("DhcpOptionsId"));
return booleanResponse("DeleteDhcpOptions");
}
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 72fcaae145..7fcb6d545a 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
@@ -1409,12 +1409,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
- public void ensureDefaultResources(String region) {
- String ownerAccountId = callerAccountId();
- String seedScope = ownerAccountId + "::" + region;
- if (!seededAccountRegions.add(seedScope)) {
- return;
- }
+ private void ensureDefaultDhcpOptions(String region, String ownerAccountId) {
String defaultOptionsId = "dopt-default";
if (dhcpOptions.get(key(region, defaultOptionsId)).isEmpty()) {
DhcpOptions defaultOptions = new DhcpOptions();
@@ -1427,11 +1422,24 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
new DhcpConfiguration("ipv6-address-preferred-lease-time", List.of("140"))));
dhcpOptions.put(key(region, defaultOptionsId), defaultOptions);
}
+ }
+
+ public void ensureDefaultResources(String region) {
+ String ownerAccountId = callerAccountId();
+ String seedScope = ownerAccountId + "::" + region;
+ if (!seededAccountRegions.add(seedScope)) {
+ return;
+ }
+ List<Vpc> existingVpcs = vpcs.scan(k -> k.startsWith(region + "::"));
// Already provisioned in a previous run and reloaded from persistent storage: the default
// VPC (and everything else) is present, so don't re-seed and create duplicates (#1297).
- if (!vpcs.scan(k -> k.startsWith(region + "::")).isEmpty()) {
+ if (!existingVpcs.isEmpty()) {
+ if (existingVpcs.stream().anyMatch(vpc -> "dopt-default".equals(vpc.getDhcpOptionsId()))) {
+ ensureDefaultDhcpOptions(region, ownerAccountId);
+ }
return;
}
+ ensureDefaultDhcpOptions(region, ownerAccountId);
LOG.debugv("Seeding default EC2 resources for region {0}", region);
// Default VPC
@@ -6094,6 +6102,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
rejectOutOfRangePrefix(cidrBlock, "InvalidVpc.Range");
rejectReservedVpcRange(cidrBlock);
ensureDefaultResources(region);
+ ensureDefaultDhcpOptions(region, callerAccountId());
String vpcId = "vpc-" + randomHex(8);
Vpc vpc = new Vpc();
vpc.setVpcId(vpcId);
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DhcpOptionsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DhcpOptionsIntegrationTest.java
index 6f57d2195d..2114a552f2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DhcpOptionsIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DhcpOptionsIntegrationTest.java
@@ -234,6 +234,136 @@ class Ec2DhcpOptionsIntegrationTest {
}
}
+ @Test
+ void dryRunDhcpOptionsWritesDoNotMutateState() {
+ String domain = "dry-run-" + System.nanoTime() + ".example";
+ String id = createOptions(domain);
+ String vpcId = given()
+ .formParam("Action", "CreateVpc")
+ .formParam("CidrBlock", "10.254.0.0/16")
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(200).extract()
+ .xmlPath().getString("CreateVpcResponse.vpc.vpcId");
+ try {
+ given()
+ .formParam("Action", "CreateDhcpOptions")
+ .formParam("DhcpConfiguration.1.Key", "domain-name")
+ .formParam("DhcpConfiguration.1.Value.1", domain)
+ .formParam("DryRun", "true")
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(412).body(containsString("DryRunOperation"));
+ given()
+ .formParam("Action", "DescribeDhcpOptions")
+ .formParam("Filter.1.Name", "value")
+ .formParam("Filter.1.Value.1", domain)
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(200)
+ .body("DescribeDhcpOptionsResponse.dhcpOptionsSet.item.dhcpOptionsId", equalTo(id));
+
+ given()
+ .formParam("Action", "AssociateDhcpOptions")
+ .formParam("DhcpOptionsId", id)
+ .formParam("VpcId", vpcId)
+ .formParam("DryRun", "true")
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(412).body(containsString("DryRunOperation"));
+ given()
+ .formParam("Action", "DescribeVpcs")
+ .formParam("VpcId.1", vpcId)
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(200)
+ .body("DescribeVpcsResponse.vpcSet.item.dhcpOptionsId", equalTo("dopt-default"));
+
+ given()
+ .formParam("Action", "DeleteDhcpOptions")
+ .formParam("DhcpOptionsId", id)
+ .formParam("DryRun", "true")
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(412).body(containsString("DryRunOperation"));
+ given()
+ .formParam("Action", "DescribeDhcpOptions")
+ .formParam("DhcpOptionsId.1", id)
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(200)
+ .body("DescribeDhcpOptionsResponse.dhcpOptionsSet.item.dhcpOptionsId", equalTo(id));
+ } finally {
+ deleteOptions(id);
+ given()
+ .formParam("Action", "DeleteVpc")
+ .formParam("VpcId", vpcId)
+ .header("Authorization", AUTH_HEADER)
+ .when().post("/").then().statusCode(200);
+ }
+ }
+
+ @Test
+ void createVpcRestoresDeletedRegionalDefaultDhcpOptions() {
+ String isolatedAuth =
+ "AWS4-HMAC-SHA256 Credential=000000000099/20260908/us-east-1/ec2/aws4_request";
+ String defaultVpcId = given()
+ .formParam("Action", "DescribeVpcs")
+ .formParam("Filter.1.Name", "is-default")
+ .formParam("Filter.1.Value.1", "true")
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200).extract()
+ .xmlPath().getString("DescribeVpcsResponse.vpcSet.item.vpcId");
+ String firstVpcId = given()
+ .formParam("Action", "CreateVpc")
+ .formParam("CidrBlock", "10.255.0.0/16")
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200).extract()
+ .xmlPath().getString("CreateVpcResponse.vpc.vpcId");
+ String secondVpcId = null;
+ try {
+ for (String vpcId : List.of(defaultVpcId, firstVpcId)) {
+ given()
+ .formParam("Action", "AssociateDhcpOptions")
+ .formParam("DhcpOptionsId", "default")
+ .formParam("VpcId", vpcId)
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200);
+ }
+ given()
+ .formParam("Action", "DeleteDhcpOptions")
+ .formParam("DhcpOptionsId", "dopt-default")
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200);
+
+ secondVpcId = given()
+ .formParam("Action", "CreateVpc")
+ .formParam("CidrBlock", "10.252.0.0/16")
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200)
+ .body("CreateVpcResponse.vpc.dhcpOptionsId", equalTo("dopt-default"))
+ .extract().xmlPath().getString("CreateVpcResponse.vpc.vpcId");
+ given()
+ .formParam("Action", "DescribeDhcpOptions")
+ .formParam("DhcpOptionsId.1", "dopt-default")
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200)
+ .body("DescribeDhcpOptionsResponse.dhcpOptionsSet.item.dhcpOptionsId", equalTo("dopt-default"));
+ } finally {
+ if (secondVpcId != null) {
+ given()
+ .formParam("Action", "DeleteVpc")
+ .formParam("VpcId", secondVpcId)
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200);
+ }
+ given()
+ .formParam("Action", "DeleteVpc")
+ .formParam("VpcId", firstVpcId)
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200);
+ given()
+ .formParam("Action", "AssociateDhcpOptions")
+ .formParam("DhcpOptionsId", "dopt-default")
+ .formParam("VpcId", defaultVpcId)
+ .header("Authorization", isolatedAuth)
+ .when().post("/").then().statusCode(200);
+ }
+ }
+
@Test
void numberedConfigurationsDoNotSilentlyDiscardLaterEntries() {
String id = given()
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.java
index bca33faf58..b9206b4dfb 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceConcurrencyTest.java
@@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
-import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.model.IpPermission;
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
index 3040d791af..df7a2da8a2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java
@@ -82,6 +82,46 @@ class Ec2ServicePersistenceTest {
assertEquals("persistent.example", restored.getConfigurations().getFirst().getValues().getFirst());
}
+ @Test
+ void startupRestoresDefaultDhcpOptionsReferencedByLegacyVpc(@TempDir Path dir) throws IOException {
+ Files.writeString(dir.resolve("ec2-vpcs.json"), """
+ {
+ "us-east-1::vpc-legacy": {
+ "vpcId": "vpc-legacy",
+ "region": "us-east-1",
+ "ownerId": "000000000000",
+ "dhcpOptionsId": "dopt-default"
+ }
+ }
+ """);
+
+ Ec2Service restarted = newService(dir);
+
+ DhcpOptions restored = restarted.describeDhcpOptions(REGION, List.of("dopt-default"), List.of(), 0, null)
+ .items().getFirst();
+ assertEquals("dopt-default", restored.getDhcpOptionsId());
+ }
+
+ @Test
+ void createVpcRestoresDeletedDefaultDhcpOptionsAfterRestart(@TempDir Path dir) {
+ Ec2Service original = newService(dir);
+ original.ensureDefaultResources(REGION);
+ String defaultVpcId = original.describeVpcs(REGION, List.of(), List.of()).stream()
+ .filter(Vpc::isDefault).findFirst().orElseThrow().getVpcId();
+ original.associateDhcpOptions(REGION, "default", defaultVpcId);
+ original.deleteDhcpOptions(REGION, "dopt-default");
+
+ Ec2Service restarted = newService(dir);
+ restarted.ensureDefaultResources(REGION);
+ assertTrue(restarted.describeDhcpOptions(REGION, List.of(), List.of(), 0, null)
+ .items().isEmpty());
+ assertEquals("dopt-default", restarted.createVpc(REGION, "10.78.0.0/16", false).getDhcpOptionsId());
+ DhcpOptions restored = restarted.describeDhcpOptions(REGION, List.of("dopt-default"), List.of(), 0, null)
+ .items().getFirst();
+ assertEquals("dopt-default", restored.getDhcpOptionsId());
+ assertEquals("default", restarted.requireVpc(REGION, defaultVpcId).getDhcpOptionsId());
+ }
+
@Test
void elasticIpMetadataSurvivesRestart(@TempDir Path dir) {
String region = "us-west-2";