mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ec2): add ReplaceRoute (#2178)
Terraform calls ReplaceRoute whenever an existing route changes target, so route tables could be created but never updated; the only workaround was to destroy and recreate the table. The action replaces the target of the route matching DestinationCidrBlock. AWS takes exactly one target per call, so the target a request does not name is cleared rather than carried over; a merge would leave a route holding two targets at once, which AWS cannot produce. Only GatewayId and NatGatewayId are modelled by Route, plus LocalTarget, which needs no new field because `local` is the gateway id a route table's built-in route already carries. The other ten AWS targets keep the UnsupportedOperation they returned before this action existed rather than being accepted and silently clearing the route they were meant to repoint. Errors follow the EC2 reference: InvalidRoute.NotFound when no route matches the destination, MissingParameter when DestinationCidrBlock is absent (an IPv6 or prefix-list request), and InvalidParameterCombination for two targets or none. Closes #2087
This commit is contained in:
1 parent
8b8f837728
commit
d943e09a73
5 files changed
+412
-1
No files matched your search
@@ -156,7 +156,7 @@ public class AwsQueryController {
|
||||
"CreateInternetGateway", "DescribeInternetGateways", "DeleteInternetGateway",
|
||||
"AttachInternetGateway", "DetachInternetGateway",
|
||||
"CreateRouteTable", "DescribeRouteTables", "DeleteRouteTable",
|
||||
"AssociateRouteTable", "DisassociateRouteTable", "CreateRoute", "DeleteRoute",
|
||||
"AssociateRouteTable", "DisassociateRouteTable", "CreateRoute", "ReplaceRoute", "DeleteRoute",
|
||||
"CreateNetworkAcl", "DescribeNetworkAcls", "DeleteNetworkAcl",
|
||||
"CreateNetworkAclEntry", "ReplaceNetworkAclEntry", "DeleteNetworkAclEntry",
|
||||
"ReplaceNetworkAclAssociation",
|
||||
|
||||
@@ -28,6 +28,15 @@ public class Ec2QueryHandler {
|
||||
private static final DateTimeFormatter ISO_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
|
||||
.withZone(ZoneOffset.UTC);
|
||||
|
||||
/** ReplaceRoute targets AWS accepts that a stored {@code Route} cannot represent here. */
|
||||
private static final List<String> UNSUPPORTED_ROUTE_TARGETS = List.of(
|
||||
"CarrierGatewayId", "CoreNetworkArn", "EgressOnlyInternetGatewayId", "InstanceId",
|
||||
"LocalGatewayId", "NetworkInterfaceId", "OdbNetworkArn",
|
||||
"TransitGatewayId", "VpcEndpointId", "VpcPeeringConnectionId");
|
||||
|
||||
/** The gateway id a route table's built-in route carries (see Ec2Service#createRouteTable). */
|
||||
private static final String LOCAL_GATEWAY_ID = "local";
|
||||
|
||||
private final Ec2Service service;
|
||||
private final EmulatorConfig config;
|
||||
private final FlowLogService flowLogService;
|
||||
@@ -123,6 +132,7 @@ public class Ec2QueryHandler {
|
||||
case "AssociateRouteTable" -> handleAssociateRouteTable(params, region);
|
||||
case "DisassociateRouteTable" -> handleDisassociateRouteTable(params, region);
|
||||
case "CreateRoute" -> handleCreateRoute(params, region);
|
||||
case "ReplaceRoute" -> handleReplaceRoute(params, region);
|
||||
case "DeleteRoute" -> handleDeleteRoute(params, region);
|
||||
// Network ACLs
|
||||
case "CreateNetworkAcl" -> handleCreateNetworkAcl(params, region);
|
||||
@@ -1499,6 +1509,33 @@ public class Ec2QueryHandler {
|
||||
return booleanResponse("CreateRoute");
|
||||
}
|
||||
|
||||
private Response handleReplaceRoute(MultivaluedMap<String, String> p, String region) {
|
||||
// A route holds only a gateway or a NAT gateway here. Every other AWS target keeps the
|
||||
// UnsupportedOperation it returned before this action existed, rather than being accepted
|
||||
// and quietly clearing the route it was meant to repoint.
|
||||
for (String target : UNSUPPORTED_ROUTE_TARGETS) {
|
||||
if (p.getFirst(target) != null) {
|
||||
throw new AwsException("UnsupportedOperation",
|
||||
"ReplaceRoute with " + target + " is not supported.", 400);
|
||||
}
|
||||
}
|
||||
String rtId = p.getFirst("RouteTableId");
|
||||
String dest = p.getFirst("DestinationCidrBlock");
|
||||
String gwId = p.getFirst("GatewayId");
|
||||
String natGwId = p.getFirst("NatGatewayId");
|
||||
// Resetting a route to the local target is expressible: `local` is the gateway id the
|
||||
// route table's built-in route already carries, so it needs no new field on Route.
|
||||
if (Boolean.parseBoolean(p.getFirst("LocalTarget"))) {
|
||||
if (gwId != null || natGwId != null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"ReplaceRoute takes exactly one target.", 400);
|
||||
}
|
||||
gwId = LOCAL_GATEWAY_ID;
|
||||
}
|
||||
service.replaceRoute(region, rtId, dest, gwId, natGwId);
|
||||
return booleanResponse("ReplaceRoute");
|
||||
}
|
||||
|
||||
private Response handleDeleteRoute(MultivaluedMap<String, String> p, String region) {
|
||||
String rtId = p.getFirst("RouteTableId");
|
||||
String dest = p.getFirst("DestinationCidrBlock");
|
||||
|
||||
@@ -2418,6 +2418,41 @@ public class Ec2Service implements ContainerTeardown {
|
||||
}
|
||||
}
|
||||
|
||||
public void replaceRoute(String region, String routeTableId, String destinationCidrBlock, String gatewayId, String natGatewayId) {
|
||||
if (destinationCidrBlock == null || destinationCidrBlock.isBlank()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The request must include DestinationCidrBlock; routes are matched on their IPv4 destination.", 400);
|
||||
}
|
||||
// AWS takes exactly one target. Rejecting both-or-neither also keeps the targets this
|
||||
// emulator cannot model (transit gateway, network interface, peering connection, ...) from
|
||||
// silently clearing the route and reporting success.
|
||||
boolean hasGateway = gatewayId != null && !gatewayId.isBlank();
|
||||
boolean hasNatGateway = natGatewayId != null && !natGatewayId.isBlank();
|
||||
if (hasGateway == hasNatGateway) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"ReplaceRoute takes exactly one target, and only GatewayId or NatGatewayId is supported.", 400);
|
||||
}
|
||||
|
||||
ensureDefaultResources(region);
|
||||
synchronized (lockFor(key(region, routeTableId))) {
|
||||
RouteTable current = getRequiredRouteTable(region, routeTableId);
|
||||
List<Route> next = new ArrayList<>(current.getRoutes());
|
||||
Route existing = next.stream()
|
||||
.filter(r -> destinationCidrBlock.equals(r.getDestinationCidrBlock()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("InvalidRoute.NotFound",
|
||||
"The route identified by " + destinationCidrBlock + " does not exist", 400));
|
||||
|
||||
// The target the request does not name is cleared rather than carried over from the
|
||||
// route being replaced.
|
||||
Route replacement = new Route(destinationCidrBlock, hasGateway ? gatewayId : null, existing.getOrigin());
|
||||
replacement.setNatGatewayId(hasNatGateway ? natGatewayId : null);
|
||||
next.set(next.indexOf(existing), replacement);
|
||||
current.setRoutes(next);
|
||||
routeTables.put(key(region, routeTableId), current);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteRoute(String region, String routeTableId, String destinationCidrBlock) {
|
||||
ensureDefaultResources(region);
|
||||
synchronized (lockFor(key(region, routeTableId))) {
|
||||
|
||||
Reference in new issue
Block a user