fix(ec2): store and report a route's IPv6 destination, and stop DeleteRoute NPEing (#2492)

* fix(ec2): store and report a route's IPv6 destination

CreateRoute accepted DestinationIpv6CidrBlock and dropped it. Route had no
member to put it in, so an IPv6 route was stored with no destination at all,
serialized with no destination element, and could never be matched again.
The Terraform provider's create-waiter polls DescribeRouteTables for the
route it just made and never finds it, so every `aws_route` with an IPv6
destination fails the apply at its 5-minute deadline.

A second defect falls out of the same gap. The destination comparison in
deleteRoute and replaceRoute read the stored side —
`r.getDestinationCidrBlock().equals(dest)` — so once any IPv6 route existed
in a table, every DeleteRoute against that table threw InternalFailure,
IPv4 ones included. That breaks `terraform destroy` independently of the
apply failure, which is why the comparison is now made from the request
side through a shared matchesDestination helper.

Per https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_Route.html
the Route type carries destinationCidrBlock, destinationIpv6CidrBlock and
egressOnlyInternetGatewayId as separate members, all optional. This adds
the latter two to the model, reads DestinationIpv6CidrBlock and
EgressOnlyInternetGatewayId in CreateRoute, emits both in the routeSet, and
accepts either destination in ReplaceRoute and DeleteRoute.
DescribeRouteTables also gains the route.destination-ipv6-cidr-block
filter, which previously fell through to the catch-all and so matched every
route table rather than the ones holding that route.

CreateRoute, ReplaceRoute and DeleteRoute now require exactly one
destination: neither is MissingParameter, both is
InvalidParameterCombination. This is a behaviour change for two requests
that used to be accepted — a CreateRoute with no destination (which stored
the unmatchable route this commit is about) and a DeleteRoute with no
destination (which silently removed nothing). A prefix-list destination is
still not modelled and now lands on MissingParameter rather than being
quietly dropped.

The egress-only internet gateway actions themselves
(CreateEgressOnlyInternetGateway and its Describe/Delete) remain
unimplemented; this only keeps the id a caller sends from being discarded,
so the route reports its target instead of appearing targetless. An
egress-only target is still rejected in ReplaceRoute, which is unchanged.

Not verified: that this clears the Terraform failures end to end. It was
developed against the reproduction in the report and the API reference, not
against a Terraform run, and a private-subnet IPv6 configuration will still
need the egress-only gateway actions before it reaches a route at all.
Whether real AWS answers MissingParameter or InvalidParameterCombination
when both destinations are given is inferred from the reference listing
them as separate optional members, not confirmed against AWS.

Tests: Ec2Ipv6RouteTest covers the IPv6 round trip, the IPv4 route beside
it, both coexisting, the IPv6 filter, ReplaceRoute on IPv6, the IPv4-delete
regression, the IPv6 delete, and the rejected destination combinations;
9 of its 11 fail without this change. Ec2ReplaceRouteIntegrationTest's
"no IPv4 destination is rejected" case now uses a prefix list, since an
IPv6 destination is no longer rejected there.

* fix(ec2): treat a prefix list as the third route destination, not as none

The previous commit rejected CreateRoute/DeleteRoute/ReplaceRoute with
DestinationPrefixListId as MissingParameter, and a test asserted that as AWS
behaviour. It is not. The CreateRoute reference says "You must specify either a
destination CIDR block or a prefix list ID", and DestinationPrefixListId is a
declared member of all three inputs and of the Route output shape in
ec2/service-2.json. A prefix-list route is a route with a destination.

So store and report it like the other two. It was hitting exactly the bug this
PR is about: persisted with no destination at all, unmatchable by DeleteRoute
or ReplaceRoute, and its null IPv4 destination made every DeleteRoute against
the same table throw. The removeIf null-safety now covers all three kinds.

Also reject EgressOnlyInternetGatewayId combined with GatewayId or NatGatewayId,
which would persist and report a route with two targets. Scoped to the parameter
this PR newly accepts: CreateRoute has never enforced exclusivity between
GatewayId and NatGatewayId, and starting to would be a behaviour change beyond
this fix. Raised by Greptile.

On the error code for a genuinely destination-less request: AWS declares no
operation-specific error for these three actions — the API reference Errors
section is empty and the service model carries no error shapes — so
MissingParameter is chosen from EC2's common client error codes, not confirmed
against the real service. Now documented as such rather than asserted.

Ec2Ipv6RouteTest: 11 -> 13 tests.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-08-24 00:05:34 -04:00
1 parent b6418dbfee
commit e18a262261
6 files changed
+567 -19

No files matched your search

@@ -820,9 +820,13 @@ public class CloudFormationResourceProvisioner {
private void provisionRoute(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, String region) {
String routeTableId = resolveOptional(props, "RouteTableId", engine);
String destinationCidr = resolveOptional(props, "DestinationCidrBlock", engine);
String destinationIpv6Cidr = resolveOptional(props, "DestinationIpv6CidrBlock", engine);
String destinationPrefixListId = resolveOptional(props, "DestinationPrefixListId", engine);
String gatewayId = resolveOptional(props, "GatewayId", engine);
String natGatewayId = resolveOptional(props, "NatGatewayId", engine);
ec2Service.createRoute(region, routeTableId, destinationCidr, gatewayId, natGatewayId);
String egressOnlyInternetGatewayId = resolveOptional(props, "EgressOnlyInternetGatewayId", engine);
ec2Service.createRoute(region, routeTableId, destinationCidr, destinationIpv6Cidr,
destinationPrefixListId, gatewayId, natGatewayId, egressOnlyInternetGatewayId);
r.setPhysicalId(r.getLogicalId() + "-" + UUID.randomUUID().toString().substring(0, 8));
}
@@ -2405,9 +2405,18 @@ public class Ec2QueryHandler {
private Response handleCreateRoute(MultivaluedMap<String, String> p, String region) {
String rtId = p.getFirst("RouteTableId");
String dest = p.getFirst("DestinationCidrBlock");
String destIpv6 = p.getFirst("DestinationIpv6CidrBlock");
// A prefix list is a destination in its own right per the CreateRoute reference. The
// prefix list itself is not modelled, but the id is stored and reported so the route
// stays addressable by DeleteRoute and ReplaceRoute.
String destPrefixList = p.getFirst("DestinationPrefixListId");
String gwId = p.getFirst("GatewayId");
String natGwId = p.getFirst("NatGatewayId");
service.createRoute(region, rtId, dest, gwId, natGwId);
// The gateway itself is not modelled (CreateEgressOnlyInternetGateway is still
// unimplemented), but the id the caller sent is stored and reported back so an IPv6
// egress route is not silently rewritten into a targetless one.
String eigwId = p.getFirst("EgressOnlyInternetGatewayId");
service.createRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId, eigwId);
return booleanResponse("CreateRoute");
}
@@ -2423,6 +2432,8 @@ public class Ec2QueryHandler {
}
String rtId = p.getFirst("RouteTableId");
String dest = p.getFirst("DestinationCidrBlock");
String destIpv6 = p.getFirst("DestinationIpv6CidrBlock");
String destPrefixList = p.getFirst("DestinationPrefixListId");
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
@@ -2434,14 +2445,16 @@ public class Ec2QueryHandler {
}
gwId = LOCAL_GATEWAY_ID;
}
service.replaceRoute(region, rtId, dest, gwId, natGwId);
service.replaceRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId);
return booleanResponse("ReplaceRoute");
}
private Response handleDeleteRoute(MultivaluedMap<String, String> p, String region) {
String rtId = p.getFirst("RouteTableId");
String dest = p.getFirst("DestinationCidrBlock");
service.deleteRoute(region, rtId, dest);
String destIpv6 = p.getFirst("DestinationIpv6CidrBlock");
String destPrefixList = p.getFirst("DestinationPrefixListId");
service.deleteRoute(region, rtId, dest, destIpv6, destPrefixList);
return booleanResponse("DeleteRoute");
}
@@ -3197,8 +3210,11 @@ public class Ec2QueryHandler {
for (Route r : rt.getRoutes()) {
xml.start("item")
.elem("destinationCidrBlock", r.getDestinationCidrBlock())
.elem("destinationIpv6CidrBlock", r.getDestinationIpv6CidrBlock())
.elem("destinationPrefixListId", r.getDestinationPrefixListId())
.elem("gatewayId", r.getGatewayId())
.elem("natGatewayId", r.getNatGatewayId())
.elem("egressOnlyInternetGatewayId", r.getEgressOnlyInternetGatewayId())
.elem("state", r.getState())
.elem("origin", r.getOrigin())
.end("item");
@@ -4493,29 +4493,118 @@ public class Ec2Service implements ContainerTeardown {
}
}
public void createRoute(String region, String routeTableId, String destinationCidrBlock, String gatewayId, String natGatewayId) {
private static boolean isSet(String value) {
return value != null && !value.isBlank();
}
/**
* A route is addressed by exactly one destination, and a table can hold an IPv4 and an IPv6
* route side by side, so the member the route does not carry is null. Comparing from the
* request side rather than the stored side keeps a null destination from being dereferenced:
* an IPv6 route in the table used to make every DeleteRoute against that table throw, IPv4
* ones included.
*/
private static boolean matchesDestination(Route route, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId) {
if (isSet(destinationCidrBlock) && destinationCidrBlock.equals(route.getDestinationCidrBlock())) {
return true;
}
if (isSet(destinationIpv6CidrBlock) && destinationIpv6CidrBlock.equals(route.getDestinationIpv6CidrBlock())) {
return true;
}
return isSet(destinationPrefixListId) && destinationPrefixListId.equals(route.getDestinationPrefixListId());
}
/** The destination naming a route, for error messages. */
private static String destinationLabel(String destinationCidrBlock, String destinationIpv6CidrBlock,
String destinationPrefixListId) {
if (isSet(destinationCidrBlock)) {
return destinationCidrBlock;
}
return isSet(destinationIpv6CidrBlock) ? destinationIpv6CidrBlock : destinationPrefixListId;
}
/**
* AWS takes one destination per route, and there are three kinds of it: DestinationCidrBlock,
* DestinationIpv6CidrBlock and DestinationPrefixListId. The CreateRoute reference is explicit
* that a prefix list is a destination in its own right — "You must specify either a destination
* CIDR block or a prefix list ID" — and all three are members of the Route output shape, so a
* prefix-list route is stored and reported like any other rather than rejected.
*
* <p>Naming none of them is the case that has no valid reading: the route could never be
* addressed again by DeleteRoute or ReplaceRoute, which match on the destination. AWS declares
* no operation-specific error for CreateRoute, DeleteRoute or ReplaceRoute — the API reference
* Errors section is empty and the service model carries no error shapes — so the code here is
* chosen from EC2's common client error codes rather than confirmed against the real service.
* MissingParameter ("the request is missing a required parameter") is the closest fit.
*/
private static void requireExactlyOneDestination(String action, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId) {
int given = (isSet(destinationCidrBlock) ? 1 : 0)
+ (isSet(destinationIpv6CidrBlock) ? 1 : 0)
+ (isSet(destinationPrefixListId) ? 1 : 0);
if (given == 0) {
throw new AwsException("MissingParameter",
"The request must include DestinationCidrBlock, DestinationIpv6CidrBlock or "
+ "DestinationPrefixListId; routes are matched on their destination.", 400);
}
if (given > 1) {
throw new AwsException("InvalidParameterCombination",
action + " takes one destination: DestinationCidrBlock, DestinationIpv6CidrBlock or "
+ "DestinationPrefixListId, not several.", 400);
}
}
/**
* An egress-only internet gateway is IPv6-only and is a target in its own right, so it cannot
* be combined with the IPv4 targets. Only the newly accepted parameter is validated here:
* CreateRoute has never enforced exclusivity between GatewayId and NatGatewayId, and starting
* to would be a behaviour change beyond this fix.
*/
private static void requireEgressOnlyGatewayIsTheOnlyTarget(String gatewayId, String natGatewayId,
String egressOnlyInternetGatewayId) {
if (!isSet(egressOnlyInternetGatewayId)) {
return;
}
if (isSet(gatewayId) || isSet(natGatewayId)) {
throw new AwsException("InvalidParameterCombination",
"EgressOnlyInternetGatewayId cannot be combined with GatewayId or NatGatewayId; "
+ "a route takes one target.", 400);
}
}
public void createRoute(String region, String routeTableId, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId,
String gatewayId, String natGatewayId,
String egressOnlyInternetGatewayId) {
requireExactlyOneDestination("CreateRoute", destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId);
requireEgressOnlyGatewayIsTheOnlyTarget(gatewayId, natGatewayId, egressOnlyInternetGatewayId);
ensureDefaultResources(region);
synchronized (lockFor(key(region, routeTableId))) {
RouteTable current = getRequiredRouteTable(region, routeTableId);
List<Route> next = new ArrayList<>(current.getRoutes());
Route route = new Route(destinationCidrBlock, gatewayId, "CreateRoute");
route.setDestinationIpv6CidrBlock(destinationIpv6CidrBlock);
route.setDestinationPrefixListId(destinationPrefixListId);
route.setNatGatewayId(natGatewayId);
route.setEgressOnlyInternetGatewayId(egressOnlyInternetGatewayId);
next.add(route);
current.setRoutes(next);
routeTables.put(key(region, routeTableId), current);
}
}
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);
}
public void replaceRoute(String region, String routeTableId, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId,
String gatewayId, String natGatewayId) {
requireExactlyOneDestination("ReplaceRoute", destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId);
// 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();
boolean hasGateway = isSet(gatewayId);
boolean hasNatGateway = isSet(natGatewayId);
if (hasGateway == hasNatGateway) {
throw new AwsException("InvalidParameterCombination",
"ReplaceRoute takes exactly one target, and only GatewayId or NatGatewayId is supported.", 400);
@@ -4526,14 +4615,20 @@ public class Ec2Service implements ContainerTeardown {
RouteTable current = getRequiredRouteTable(region, routeTableId);
List<Route> next = new ArrayList<>(current.getRoutes());
Route existing = next.stream()
.filter(r -> destinationCidrBlock.equals(r.getDestinationCidrBlock()))
.filter(r -> matchesDestination(r, destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId))
.findFirst()
.orElseThrow(() -> new AwsException("InvalidRoute.NotFound",
"The route identified by " + destinationCidrBlock + " does not exist", 400));
"The route identified by "
+ destinationLabel(destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId)
+ " 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.setDestinationIpv6CidrBlock(destinationIpv6CidrBlock);
replacement.setDestinationPrefixListId(destinationPrefixListId);
replacement.setNatGatewayId(hasNatGateway ? natGatewayId : null);
next.set(next.indexOf(existing), replacement);
current.setRoutes(next);
@@ -4541,12 +4636,16 @@ public class Ec2Service implements ContainerTeardown {
}
}
public void deleteRoute(String region, String routeTableId, String destinationCidrBlock) {
public void deleteRoute(String region, String routeTableId, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId) {
requireExactlyOneDestination("DeleteRoute", destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId);
ensureDefaultResources(region);
synchronized (lockFor(key(region, routeTableId))) {
RouteTable current = getRequiredRouteTable(region, routeTableId);
List<Route> next = new ArrayList<>(current.getRoutes());
next.removeIf(r -> r.getDestinationCidrBlock().equals(destinationCidrBlock));
next.removeIf(r -> matchesDestination(r, destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId));
current.setRoutes(next);
routeTables.put(key(region, routeTableId), current);
}
@@ -4931,6 +5030,12 @@ public class Ec2Service implements ContainerTeardown {
.anyMatch(a -> a.getGatewayId() != null && matchesValue(values, a.getGatewayId()));
case "association.main" -> rt.getAssociations().stream()
.anyMatch(a -> matchesValue(values, String.valueOf(a.isMain())));
case "route.destination-ipv6-cidr-block" -> rt.getRoutes().stream()
.anyMatch(r -> r.getDestinationIpv6CidrBlock() != null
&& matchesValue(values, r.getDestinationIpv6CidrBlock()));
case "route.destination-prefix-list-id" -> rt.getRoutes().stream()
.anyMatch(r -> r.getDestinationPrefixListId() != null
&& matchesValue(values, r.getDestinationPrefixListId()));
default -> true;
};
}
@@ -8,8 +8,11 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
public class Route {
private String destinationCidrBlock;
private String destinationIpv6CidrBlock;
private String destinationPrefixListId;
private String gatewayId;
private String natGatewayId;
private String egressOnlyInternetGatewayId;
private String state = "active";
private String origin;
@@ -24,12 +27,21 @@ public class Route {
public String getDestinationCidrBlock() { return destinationCidrBlock; }
public void setDestinationCidrBlock(String destinationCidrBlock) { this.destinationCidrBlock = destinationCidrBlock; }
public String getDestinationIpv6CidrBlock() { return destinationIpv6CidrBlock; }
public void setDestinationIpv6CidrBlock(String destinationIpv6CidrBlock) { this.destinationIpv6CidrBlock = destinationIpv6CidrBlock; }
public String getDestinationPrefixListId() { return destinationPrefixListId; }
public void setDestinationPrefixListId(String destinationPrefixListId) { this.destinationPrefixListId = destinationPrefixListId; }
public String getGatewayId() { return gatewayId; }
public void setGatewayId(String gatewayId) { this.gatewayId = gatewayId; }
public String getNatGatewayId() { return natGatewayId; }
public void setNatGatewayId(String natGatewayId) { this.natGatewayId = natGatewayId; }
public String getEgressOnlyInternetGatewayId() { return egressOnlyInternetGatewayId; }
public void setEgressOnlyInternetGatewayId(String egressOnlyInternetGatewayId) { this.egressOnlyInternetGatewayId = egressOnlyInternetGatewayId; }
public String getState() { return state; }
public void setState(String state) { this.state = state; }