mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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:
1 parent
fc5e1aea0a
commit
340e727ede
5 files changed
+142
-4
No files matched your search
@@ -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
|
||||
|
||||
|
||||
+2
-1
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user