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

@@ -0,0 +1,409 @@
package io.github.hectorvent.floci.services.ec2;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.xml.HasXPath.hasXPath;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import io.quarkus.test.junit.QuarkusTest;
import static io.restassured.RestAssured.given;
/**
* A route is addressed by one destination, and per
* https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_Route.html the Route type carries the
* IPv4 and IPv6 destinations as separate members. CreateRoute used to accept
* DestinationIpv6CidrBlock and drop it, storing a route with no destination at all: it could never
* be matched (so a create-waiter polls to its deadline) and its null destination made every
* DeleteRoute against the same table throw, IPv4 ones included.
*
* <p>The XPath assertions use {@code local-name()} because EC2 responses carry a default namespace.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class Ec2Ipv6RouteTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private static final String VPC_CIDR = "203.0.113.0/24";
private static final String IPV4_ROUTE = "0.0.0.0/0";
private static final String IPV6_ROUTE = "::/0";
private static final String INTERNET_GATEWAY = "igw-0ipv6route0test0";
private static final String NAT_GATEWAY = "nat-0ipv6route0test0";
private static final String ROUTE_SET =
"DescribeRouteTablesResponse.routeTableSet.item.routeSet.item";
private static final String IPV6_NODE =
ROUTE_SET + ".find { it.destinationIpv6CidrBlock == '" + IPV6_ROUTE + "' }";
private static final String IPV4_NODE =
ROUTE_SET + ".find { it.destinationCidrBlock == '" + IPV4_ROUTE + "' }";
private static String routeTableId;
private static io.restassured.specification.RequestSpecification ec2() {
return given().header("Authorization", AUTH_HEADER);
}
private static io.restassured.response.ValidatableResponse describe() {
return ec2()
.formParam("Action", "DescribeRouteTables")
.formParam("RouteTableId.1", routeTableId)
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
@Order(1)
void createARouteTableWithAnIpv4AndAnIpv6Route() {
String vpcId = ec2()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", VPC_CIDR)
.when()
.post("/")
.then()
.statusCode(200)
.extract().path("CreateVpcResponse.vpc.vpcId");
routeTableId = ec2()
.formParam("Action", "CreateRouteTable")
.formParam("VpcId", vpcId)
.when()
.post("/")
.then()
.statusCode(200)
.extract().path("CreateRouteTableResponse.routeTable.routeTableId");
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationCidrBlock", IPV4_ROUTE)
.formParam("GatewayId", INTERNET_GATEWAY)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateRouteResponse.return", equalTo("true"));
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationIpv6CidrBlock", IPV6_ROUTE)
.formParam("GatewayId", INTERNET_GATEWAY)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateRouteResponse.return", equalTo("true"));
}
/**
* The failure this fixes: the IPv6 route came back with no destination element at all, so the
* Terraform provider's create-waiter never saw the route it had just created.
*/
@Test
@Order(2)
void anIpv6RouteReportsItsDestination() {
String table = describe()
.body(IPV6_NODE + ".gatewayId", equalTo(INTERNET_GATEWAY))
.body(IPV6_NODE + ".state", equalTo("active"))
.body(IPV6_NODE + ".origin", equalTo("CreateRoute"))
.extract().asString();
assertThat(table, containsString("<destinationIpv6CidrBlock>" + IPV6_ROUTE
+ "</destinationIpv6CidrBlock>"));
}
/** The IPv4 route is unchanged, and carries no IPv6 destination member. */
@Test
@Order(3)
void anIpv4RouteStillReportsItsDestination() {
describe()
.body(IPV4_NODE + ".gatewayId", equalTo(INTERNET_GATEWAY))
// ...and carries no IPv6 destination member: the two are separate members of Route,
// not one field reused.
.body(hasXPath("count(//*[local-name()='item']"
+ "[*[local-name()='destinationCidrBlock']='" + IPV4_ROUTE + "']"
+ "/*[local-name()='destinationIpv6CidrBlock'])", equalTo("0")));
}
/**
* Both destinations live in one table as separate routes — three in total with the local route
* CreateRouteTable inserts. A model that reused a single destination field would show two.
*/
@Test
@Order(4)
void bothRoutesCoexistInTheSameTable() {
describe()
.body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'])",
equalTo("3")))
.body(hasXPath("count(//*[local-name()='destinationIpv6CidrBlock'])", equalTo("1")))
// The local route plus the IPv4 default route.
.body(hasXPath("count(//*[local-name()='destinationCidrBlock'])", equalTo("2")));
}
/** DescribeRouteTables can be filtered on the IPv6 destination, and the filter discriminates. */
@Test
@Order(5)
void routeTablesCanBeFilteredOnTheIpv6Destination() {
String matching = ec2()
.formParam("Action", "DescribeRouteTables")
.formParam("Filter.1.Name", "route.destination-ipv6-cidr-block")
.formParam("Filter.1.Value.1", IPV6_ROUTE)
.when()
.post("/")
.then()
.statusCode(200)
.extract().asString();
assertThat(matching, containsString(routeTableId));
// An unrelated IPv6 destination must not match: the branch used to fall through to
// `default -> true`, so this filter matched every route table rather than none.
String notMatching = ec2()
.formParam("Action", "DescribeRouteTables")
.formParam("Filter.1.Name", "route.destination-ipv6-cidr-block")
.formParam("Filter.1.Value.1", "2001:db8::/32")
.when()
.post("/")
.then()
.statusCode(200)
.extract().asString();
assertThat(notMatching, not(containsString(routeTableId)));
}
/** ReplaceRoute used to hard-reject any request without an IPv4 destination. */
@Test
@Order(6)
void replaceRouteRepointsTheIpv6Route() {
ec2()
.formParam("Action", "ReplaceRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationIpv6CidrBlock", IPV6_ROUTE)
.formParam("NatGatewayId", NAT_GATEWAY)
.when()
.post("/")
.then()
.statusCode(200)
.body("ReplaceRouteResponse.return", equalTo("true"));
describe()
.body(IPV6_NODE + ".natGatewayId", equalTo(NAT_GATEWAY))
// Replace, not merge: the gateway the route used to carry is gone...
.body(hasXPath("count(//*[local-name()='item']"
+ "[*[local-name()='destinationIpv6CidrBlock']='" + IPV6_ROUTE + "']"
+ "/*[local-name()='gatewayId'])", equalTo("0")))
// ...and only the addressed route moved.
.body(IPV4_NODE + ".gatewayId", equalTo(INTERNET_GATEWAY));
}
/**
* The regression that broke `terraform destroy` independently of the create failure: with an
* IPv6 route in the table, deleting an IPv4 route dereferenced the IPv6 route's null IPv4
* destination and returned InternalFailure.
*/
@Test
@Order(7)
void deletingAnIpv4RouteWorksWhileAnIpv6RouteIsPresent() {
ec2()
.formParam("Action", "DeleteRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationCidrBlock", IPV4_ROUTE)
.when()
.post("/")
.then()
.statusCode(200)
.body("DeleteRouteResponse.return", equalTo("true"));
describe()
// The local route and the IPv6 route are left; the IPv4 default route is gone.
.body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'])",
equalTo("2")))
.body(hasXPath("count(//*[local-name()='destinationCidrBlock'][text()='"
+ IPV4_ROUTE + "'])", equalTo("0")))
// The IPv6 route is untouched.
.body(IPV6_NODE + ".natGatewayId", equalTo(NAT_GATEWAY));
}
@Test
@Order(8)
void deletingTheIpv6RouteRemovesIt() {
ec2()
.formParam("Action", "DeleteRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationIpv6CidrBlock", IPV6_ROUTE)
.when()
.post("/")
.then()
.statusCode(200)
.body("DeleteRouteResponse.return", equalTo("true"));
describe()
.body(hasXPath("count(//*[local-name()='destinationIpv6CidrBlock'])", equalTo("0")))
// Only the local route CreateRouteTable inserted is left.
.body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'])",
equalTo("1")));
}
/**
* A prefix list is the third destination kind, not the absence of one: the CreateRoute
* reference says "You must specify either a destination CIDR block or a prefix list ID", and
* DestinationPrefixListId is a member of the Route output shape alongside the two CIDR
* members. It round-trips and stays addressable, like the other two.
*/
@Test
@Order(9)
void aPrefixListRouteRoundTripsAndIsDeletable() {
String prefixList = "pl-0ipv6route0test0";
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationPrefixListId", prefixList)
.formParam("GatewayId", INTERNET_GATEWAY)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateRouteResponse.return", equalTo("true"));
String prefixListNode =
ROUTE_SET + ".find { it.destinationPrefixListId == '" + prefixList + "' }";
describe()
.body(prefixListNode + ".gatewayId", equalTo(INTERNET_GATEWAY))
// It carries neither CIDR member: the three destinations are separate members.
.body(hasXPath("count(//*[local-name()='item']"
+ "[*[local-name()='destinationPrefixListId']='" + prefixList + "']"
+ "/*[local-name()='destinationCidrBlock'])", equalTo("0")))
.body(hasXPath("count(//*[local-name()='item']"
+ "[*[local-name()='destinationPrefixListId']='" + prefixList + "']"
+ "/*[local-name()='destinationIpv6CidrBlock'])", equalTo("0")));
// Addressable again — before it was stored with no destination at all, so nothing could
// ever match it, and its null IPv4 destination made every DeleteRoute on the table throw.
ec2()
.formParam("Action", "DeleteRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationPrefixListId", prefixList)
.when()
.post("/")
.then()
.statusCode(200);
describe()
.body(hasXPath("count(//*[local-name()='destinationPrefixListId'])", equalTo("0")))
.body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'])",
equalTo("1")));
}
/**
* Naming no destination at all is the case with no valid reading: nothing could ever address
* the route again. AWS documents no operation-specific error here, so the code is chosen from
* EC2's common client error codes rather than confirmed against the real service.
*/
@Test
@Order(10)
void createRouteWithNoDestinationIsRejected() {
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("GatewayId", INTERNET_GATEWAY)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
// ...and nothing was added to the table.
describe()
.body(hasXPath("count(//*[local-name()='routeSet']/*[local-name()='item'])",
equalTo("1")));
}
/** One destination per route: a request naming two would be addressable two ways. */
@Test
@Order(11)
void createRouteWithBothDestinationsIsRejected() {
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationCidrBlock", IPV4_ROUTE)
.formParam("DestinationIpv6CidrBlock", IPV6_ROUTE)
.formParam("GatewayId", INTERNET_GATEWAY)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
}
/**
* The egress-only internet gateway actions are not implemented, but the id a caller sends is
* stored and reported rather than dropped, so the route is not left targetless.
*/
@Test
@Order(12)
void anEgressOnlyInternetGatewayTargetIsReportedBack() {
String egressOnlyGateway = "eigw-0ipv6route0test";
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationIpv6CidrBlock", IPV6_ROUTE)
.formParam("EgressOnlyInternetGatewayId", egressOnlyGateway)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateRouteResponse.return", equalTo("true"));
describe()
.body(IPV6_NODE + ".egressOnlyInternetGatewayId", equalTo(egressOnlyGateway));
}
/**
* An egress-only internet gateway is IPv6-only and is a target in its own right, so combining
* it with an IPv4 target would persist a route with two targets and report both back. Only the
* newly accepted parameter is validated: CreateRoute has never enforced exclusivity between
* GatewayId and NatGatewayId, and starting to would be a behaviour change beyond this fix.
*/
@Test
@Order(13)
void anEgressOnlyInternetGatewayCannotBeCombinedWithAnotherTarget() {
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationIpv6CidrBlock", "2001:db8:1::/48")
.formParam("EgressOnlyInternetGatewayId", "eigw-0ipv6route0test")
.formParam("GatewayId", INTERNET_GATEWAY)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
ec2()
.formParam("Action", "CreateRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationIpv6CidrBlock", "2001:db8:1::/48")
.formParam("EgressOnlyInternetGatewayId", "eigw-0ipv6route0test")
.formParam("NatGatewayId", NAT_GATEWAY)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
// Nothing was persisted by either attempt.
describe()
.body(hasXPath("count(//*[local-name()='destinationIpv6CidrBlock']"
+ "[text()='2001:db8:1::/48'])", equalTo("0")));
}
}
@@ -303,14 +303,16 @@ class Ec2ReplaceRouteIntegrationTest {
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
}
/** An IPv6 or prefix-list ReplaceRoute carries no DestinationCidrBlock; it must not 500. */
/**
* A ReplaceRoute naming no destination at all cannot identify a route; it must not 500. All
* three destination kinds are now accepted — see {@code Ec2Ipv6RouteTest}.
*/
@Test
@Order(7)
void replaceRouteWithoutAnIpv4DestinationIsRejected() {
void replaceRouteWithoutADestinationIsRejected() {
given()
.formParam("Action", "ReplaceRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationIpv6CidrBlock", "::/0")
.formParam("GatewayId", INTERNET_GATEWAY)
.header("Authorization", AUTH_HEADER)
.when()