Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(ec2): prevent deleting the local VPC route and return InvalidRoute.NotFound for missing routes (#5603)

Matheus Oliveira340e727ede04
diff --git a/docs/services/ec2.md b/docs/services/ec2.md
index c263580c71..2dfbab9fe7 100644
--- a/docs/services/ec2.md
+++ b/docs/services/ec2.md
@@ -329,7 +329,7 @@ Nested launch-template tags follow AWS's case-sensitive check: `aws:` is rejecte
| DisassociateRouteTable | Removes a route table association. |
| CreateRoute | Adds a route to a route table. Accepts `InstanceId`, `NetworkInterfaceId`, and `VpcPeeringConnectionId` as targets (alongside `GatewayId`/`NatGatewayId`/`EgressOnlyInternetGatewayId`) and reports them back on `DescribeRouteTables`. |
| ReplaceRoute | Replaces the target of an existing route, accepting `InstanceId`, `NetworkInterfaceId`, and `VpcPeeringConnectionId`. |
-| DeleteRoute | Removes a route from a route table. |
+| DeleteRoute | Removes a route from a route table. Refuses the route to the VPC CIDR that `CreateRouteTable` seeds with `InvalidParameterValue`, and reports a destination with no route as `InvalidRoute.NotFound`. |
### VPC Peering Connections
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java
index 0e0638142b..262809b293 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2NetworkCfnProvisioner.java
@@ -180,7 +180,8 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
String prefixList = text(target, "destinationPrefixListId");
String egressOnly = text(target, "egressOnlyInternetGatewayId");
if (egressOnly != null) {
- ec2Service.deleteRoute(region, routeTableId, cidr, ipv6, prefixList);
+ CfnDeletes.safeDelete("route", routeTableId, () -> ec2Service.deleteRoute(region, routeTableId, cidr,
+ ipv6, prefixList), "InvalidRoute.NotFound");
ec2Service.createRoute(region, routeTableId, cidr, ipv6, prefixList, null, null, egressOnly, null);
} else {
ec2Service.replaceRoute(region, routeTableId, cidr, ipv6, prefixList,
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 84b5bd6325..7f087e8b93 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
@@ -8940,14 +8940,40 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
synchronized (lockFor(key(region, routeTableId))) {
RouteTable current = getRequiredRouteTable(region, routeTableId);
List<Route> next = new ArrayList<>(current.getRoutes());
- next.removeIf(r -> matchesDestination(r, canonicalDestinationCidrBlock, destinationIpv6CidrBlock,
- destinationPrefixListId));
+ List<Route> matched = next.stream()
+ .filter(r -> matchesDestination(r, canonicalDestinationCidrBlock, destinationIpv6CidrBlock,
+ destinationPrefixListId))
+ .toList();
+ if (matched.isEmpty()) {
+ throw new AwsException("InvalidRoute.NotFound",
+ "The route identified by "
+ + destinationLabel(canonicalDestinationCidrBlock, destinationIpv6CidrBlock,
+ destinationPrefixListId)
+ + " does not exist", 400);
+ }
+ for (Route route : matched) {
+ if (isLocalRoute(route)) {
+ throw new AwsException("InvalidParameterValue",
+ "cannot remove local route " + route.getDestinationCidrBlock()
+ + " in route table " + routeTableId, 400);
+ }
+ }
+ next.removeAll(matched);
current.setRoutes(next);
routeTables.put(key(region, routeTableId), current);
notifyRouteTableUpdated(region, current);
}
}
+ /**
+ * The route CreateRouteTable seeds for the VPC CIDR, which AWS refuses to delete. Keyed on the
+ * origin rather than the "local" gateway: ReplaceRoute may repoint the route (NAT gateway, ENI,
+ * peering connection) and keeps its origin, and AWS still refuses to delete it while repointed.
+ */
+ private static boolean isLocalRoute(Route route) {
+ return "CreateRouteTable".equals(route.getOrigin());
+ }
+
private RouteTable getRequiredRouteTable(String region, String routeTableId) {
RouteTable rt = routeTables.get(key(region, routeTableId)).orElse(null);
if (rt == null)
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DuplicateRouteTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DuplicateRouteTest.java
index d29dacf2f0..6aed146152 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DuplicateRouteTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DuplicateRouteTest.java
@@ -214,4 +214,85 @@ class Ec2DuplicateRouteTest {
createRoute("DestinationCidrBlock", IPV4_ROUTE).when().post("/").then().statusCode(200);
describe().body(hasXPath(ROUTE_COUNT, equalTo("4")));
}
+
+ /** Asserts the request fails with the given 400 error code. */
+ private static void expectError(io.restassured.specification.RequestSpecification request, String code) {
+ request
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("Response.Errors.Error.Code", equalTo(code));
+ }
+
+ /** The local route, read back through DescribeRouteTables, still targets "local". */
+ private static void expectLocalRouteIntact() {
+ describe()
+ .body(hasXPath(ROUTE_COUNT, equalTo("4")))
+ .body("DescribeRouteTablesResponse.routeTableSet.item.routeSet.item"
+ + ".find { it.destinationCidrBlock == '" + VPC_CIDR + "' }.gatewayId",
+ equalTo("local"));
+ }
+
+ /** AWS refuses DeleteRoute on the local route CreateRouteTable seeded. */
+ @Test
+ @Order(10)
+ void theSeededLocalRouteCannotBeDeleted() {
+ expectError(
+ ec2()
+ .formParam("Action", "DeleteRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", VPC_CIDR),
+ "InvalidParameterValue");
+ expectLocalRouteIntact();
+ }
+
+ /**
+ * ReplaceRoute may repoint the local route (AWS allows a NAT gateway, ENI or GWLB endpoint),
+ * but the route is still the one CreateRouteTable seeded and still cannot be deleted.
+ */
+ @Test
+ @Order(11)
+ void aRepointedLocalRouteStillCannotBeDeleted() {
+ ec2()
+ .formParam("Action", "ReplaceRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", VPC_CIDR)
+ .formParam("NatGatewayId", NAT_GATEWAY)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ expectError(
+ ec2()
+ .formParam("Action", "DeleteRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", VPC_CIDR),
+ "InvalidParameterValue");
+
+ ec2()
+ .formParam("Action", "ReplaceRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", VPC_CIDR)
+ .formParam("LocalTarget", "true")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ expectLocalRouteIntact();
+ }
+
+ /** DeleteRoute of a destination the table does not hold is InvalidRoute.NotFound, not a silent success. */
+ @Test
+ @Order(12)
+ void deletingAMissingRouteIsNotFound() {
+ expectError(
+ ec2()
+ .formParam("Action", "DeleteRoute")
+ .formParam("RouteTableId", routeTableId)
+ .formParam("DestinationCidrBlock", "203.0.113.0/24"),
+ "InvalidRoute.NotFound");
+ expectLocalRouteIntact();
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
index 31c749147b..e97f7dce99 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
@@ -344,6 +344,36 @@ class Ec2ServiceTest {
.noneMatch(vpc -> vpcId.equals(vpc.getVpcId())));
}
+ @Test
+ void deleteRouteRefusesTheLocalRouteAndReportsMissingDestinations() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+ String region = "us-east-1";
+ String vpcId = service.createVpc(region, "10.86.0.0/16", false).getVpcId();
+ String routeTableId = service.createRouteTable(region, vpcId).getRouteTableId();
+ service.createRoute(region, routeTableId, "0.0.0.0/0", null, null, "igw-route0test0000000",
+ null, null, null);
+
+ AwsException local = assertThrows(AwsException.class,
+ () -> service.deleteRoute(region, routeTableId, "10.86.0.0/16", null, null));
+ assertEquals("InvalidParameterValue", local.getErrorCode());
+ assertTrue(local.getMessage().contains("10.86.0.0/16"), local.getMessage());
+
+ AwsException missing = assertThrows(AwsException.class,
+ () -> service.deleteRoute(region, routeTableId, "203.0.113.0/24", null, null));
+ assertEquals("InvalidRoute.NotFound", missing.getErrorCode());
+ assertTrue(missing.getMessage().contains("203.0.113.0/24"), missing.getMessage());
+
+ service.deleteRoute(region, routeTableId, "0.0.0.0/0", null, null);
+
+ assertEquals(1, service.describeRouteTables(region, List.of(routeTableId), Map.of())
+ .getFirst().getRoutes().size(), "the local route survives");
+ assertEquals("CreateRouteTable", service.describeRouteTables(region, List.of(routeTableId), Map.of())
+ .getFirst().getRoutes().getFirst().getOrigin());
+ }
+
@Test
void deleteVpcFailsWhileAVpcEndpointRemains() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),