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

DeleteRoute removed the local route CreateRouteTable seeds for the VPC CIDR
and reported success. Since CreateRoute answers RouteAlreadyExists for that
destination, the route table was left without intra-VPC routing and with no
way to put the route back. DeleteRoute also reported success for a
destination the table does not hold, hiding caller mistakes.

Reject the seeded local route with InvalidParameterValue and report an
unknown destination with InvalidRoute.NotFound, matching AWS. The route is
identified by its CreateRouteTable origin, so a route repointed by
ReplaceRoute stays undeletable while repointed.

The CloudFormation route restore path now tolerates the already-gone
InvalidRoute.NotFound case through CfnDeletes.safeDelete.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-15 07:34:56 -04:00
1 parent fc5e1aea0a
commit 340e727ede
5 files changed
+142 -4

No files matched your search

@@ -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();
}
}
@@ -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),