fix(ec2): apply filters to transit gateway route table lookups (#3820)

* fix(ec2): apply filters to transit gateway route table lookups

matchesFilter had no arm for TransitGatewayRouteTable, so every filter fell
through to the permissive default and DescribeTransitGatewayRouteTables answered
with every table in the region regardless of transit-gateway-id.
GetTransitGatewayRouteTableAssociations never read its filters at all.

A caller that filters associations down to its own attachment therefore sees
somebody else's, concludes its own association is missing, and tries to create
one, which then fails as Resource.AlreadyAssociated once a route table has more
than one attachment.

* fix(ec2): compare the association state in the associations filter

GetTransitGatewayRouteTableAssociations reports the association's state, so
routing its "state" filter through the shared attachment matcher compared the
attachment lifecycle state instead. Filtering on "associated" matched nothing
while "available" matched every association on the table.

* fix(ec2): limit the associations listing to its documented filters

GetTransitGatewayRouteTableAssociations models resource-id, resource-type and
transit-gateway-attachment-id. Forwarding the rest of the request into the
attachment matcher also honored transit-gateway-id, vpc-id, vpc-owner-id and the
tag filters, none of which AWS exposes on this action, and honored a state filter
it does not model at all.

Retaining only the three modeled names drops that surface, which also removes the
association state handling added earlier in this branch: the fix there was to stop
answering a filter AWS does not document rather than to answer it correctly.

* fix(ec2): apply the documented filters to the propagations listing

GetTransitGatewayRouteTablePropagations accepts resource-id, resource-type and
transit-gateway-attachment-id, but propagationsOf ignored filters entirely and
matchesFilter had no arm for TransitGatewayRouteTablePropagation. A caller that
narrows the listing to its own attachment got every propagation on the table
back instead.

Retaining only the modeled names also keeps the tag filters out. Those are
handled ahead of the per-resource arms and read getResourceTags, which has no
propagation case, so a tag:Name filter emptied the listing rather than being
ignored as an unmodeled filter. The associations listing models the same three
names, so the allowlist is one constant shared by both.

* chore(ec2): cover the transit gateway filter arms that had no test

The route table listing's state and transit-gateway-route-table-id arms, and
resource-id and resource-type on both the association and propagation listings,
read the right fields but nothing exercised them over the Query protocol.

Each now gets a matching and a non-matching value, so an arm that falls through
to the permissive default cannot pass. Verified by pointing each arm at a
plausible wrong getter and confirming the suite fails.
This commit is contained in:
Matthew Ingle authored and GitHub committed 2026-09-18 10:53:38 -04:00
1 parent 677cdbe053
commit d15b388aca
3 files changed
+247 -3

No files matched your search

@@ -2442,7 +2442,8 @@ public class Ec2QueryHandler {
private Response handleGetTransitGatewayRouteTableAssociations(
MultivaluedMap<String, String> p, String region) {
String routeTableId = p.getFirst("TransitGatewayRouteTableId");
List<TransitGatewayVpcAttachment> associated = service.associationsOf(region, routeTableId);
List<TransitGatewayVpcAttachment> associated =
service.associationsOf(region, routeTableId, getFilters(p));
XmlBuilder xml = new XmlBuilder()
.start("GetTransitGatewayRouteTableAssociationsResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -2481,7 +2482,7 @@ public class Ec2QueryHandler {
private Response handleGetTransitGatewayRouteTablePropagations(
MultivaluedMap<String, String> p, String region) {
List<TransitGatewayRouteTablePropagation> propagations =
service.propagationsOf(region, p.getFirst("TransitGatewayRouteTableId"));
service.propagationsOf(region, p.getFirst("TransitGatewayRouteTableId"), getFilters(p));
XmlBuilder xml = new XmlBuilder()
.start("GetTransitGatewayRouteTablePropagationsResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -141,6 +141,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
Pattern.compile("^tgw-rtb-[0-9a-f]{8}([0-9a-f]{9})?$");
private static final Pattern TRANSIT_GATEWAY_ATTACHMENT_ID_PATTERN =
Pattern.compile("^tgw-attach-[0-9a-f]{8}([0-9a-f]{9})?$");
private static final Set<String> ROUTE_TABLE_MEMBERSHIP_FILTER_NAMES =
Set.of("resource-id", "resource-type", "transit-gateway-attachment-id");
// A first launch may need to pull a large AMI-backed image. Keep a finite CloudFormation
// bound, but allow enough time for that legitimate cold-start path before cancellation.
private static final Duration CONTAINER_LAUNCH_TIMEOUT = Duration.ofMinutes(5);
@@ -2062,11 +2064,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
}
/** The attachments associated with a route table, which is where an association is recorded. */
public List<TransitGatewayVpcAttachment> associationsOf(String region, String routeTableId) {
return associationsOf(region, routeTableId, Map.of());
}
/** The attachments associated with a route table, which is where an association is recorded. */
public List<TransitGatewayVpcAttachment> associationsOf(
String region, String routeTableId, Map<String, List<String>> filters) {
Map<String, List<String>> modeledFilters = new LinkedHashMap<>(filters);
modeledFilters.keySet().retainAll(ROUTE_TABLE_MEMBERSHIP_FILTER_NAMES);
return transitGatewayVpcAttachments.scan(k -> true).stream()
.filter(attachment -> region.equals(attachment.getRegion()))
.filter(attachment -> routeTableId.equals(attachment.getAssociationRouteTableId()))
.filter(attachment -> matchesFilters(attachment, modeledFilters, region))
.collect(Collectors.toList());
}
@@ -2111,9 +2121,17 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
public List<TransitGatewayRouteTablePropagation> propagationsOf(String region, String routeTableId) {
return propagationsOf(region, routeTableId, Map.of());
}
public List<TransitGatewayRouteTablePropagation> propagationsOf(
String region, String routeTableId, Map<String, List<String>> filters) {
Map<String, List<String>> modeledFilters = new LinkedHashMap<>(filters);
modeledFilters.keySet().retainAll(ROUTE_TABLE_MEMBERSHIP_FILTER_NAMES);
return transitGatewayPropagations.scan(k -> true).stream()
.filter(propagation -> region.equals(propagation.getRegion()))
.filter(propagation -> routeTableId.equals(propagation.getTransitGatewayRouteTableId()))
.filter(propagation -> matchesFilters(propagation, modeledFilters, region))
.collect(Collectors.toList());
}
@@ -7418,6 +7436,27 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
default -> true;
};
}
if (resource instanceof TransitGatewayRouteTable routeTable) {
return switch (filterName) {
case "transit-gateway-route-table-id" ->
matchesValue(values, routeTable.getTransitGatewayRouteTableId());
case "transit-gateway-id" -> matchesValue(values, routeTable.getTransitGatewayId());
case "state" -> matchesValue(values, routeTable.getState());
case "default-association-route-table" ->
matchesValue(values, String.valueOf(routeTable.isDefaultAssociationRouteTable()));
case "default-propagation-route-table" ->
matchesValue(values, String.valueOf(routeTable.isDefaultPropagationRouteTable()));
default -> true;
};
}
if (resource instanceof TransitGatewayRouteTablePropagation propagation) {
return switch (filterName) {
case "transit-gateway-attachment-id" -> matchesValue(values, propagation.getTransitGatewayAttachmentId());
case "resource-id" -> matchesValue(values, propagation.getResourceId());
case "resource-type" -> matchesValue(values, propagation.getResourceType());
default -> true;
};
}
if (resource instanceof TransitGatewayVpcAttachment attachment) {
return switch (filterName) {
case "transit-gateway-attachment-id" -> matchesValue(values, attachment.getTransitGatewayAttachmentId());
@@ -10,6 +10,7 @@ import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.not;
/**
@@ -88,6 +89,36 @@ class Ec2TransitGatewayRouteTableIntegrationTest {
+ ".transitGatewayRouteTableId");
}
private String routeTablesFilteredBy(String filterName, String value) {
return given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("Filter.1.Name", filterName)
.formParam("Filter.1.Value.1", value)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
}
/** Both listings of what a route table holds take the same three filters. */
private String spokeTableMembersFilteredBy(String action, String filterName, String value) {
return given()
.formParam("Action", action)
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("Filter.1.Name", filterName)
.formParam("Filter.1.Value.1", value)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
}
private String associationsFilteredBy(String filterName, String value) {
return spokeTableMembersFilteredBy("GetTransitGatewayRouteTableAssociations", filterName, value);
}
private String propagationsFilteredBy(String filterName, String value) {
return spokeTableMembersFilteredBy("GetTransitGatewayRouteTablePropagations", filterName, value);
}
private String extract(String body, String element) {
int start = body.indexOf("<" + element + ">") + element.length() + 2;
return body.substring(start, body.indexOf("</" + element + ">", start));
@@ -248,4 +279,177 @@ class Ec2TransitGatewayRouteTableIntegrationTest {
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidRouteTableId.Malformed"));
}
@Test
@Order(6)
void routeTableFiltersNarrowTheListing() {
given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("Filter.1.Name", "transit-gateway-id")
.formParam("Filter.1.Value.1", transitGatewayId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("DescribeTransitGatewayRouteTablesResponse.transitGatewayRouteTables.item.transitGatewayId",
everyItem(equalTo(transitGatewayId)));
String otherGateway = given()
.formParam("Action", "CreateTransitGateway")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
String otherRouteTableId = extract(otherGateway, "associationDefaultRouteTableId");
String ours = given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("Filter.1.Name", "transit-gateway-id")
.formParam("Filter.1.Value.1", transitGatewayId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(ours, containsString(defaultRouteTableId));
assertThat(ours, not(containsString(otherRouteTableId)));
String unmatched = given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("Filter.1.Name", "transit-gateway-id")
.formParam("Filter.1.Value.1", "tgw-0123456789abcdef0")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(unmatched, not(containsString("<item>")));
String defaultOnly = given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("Filter.1.Name", "transit-gateway-id")
.formParam("Filter.1.Value.1", transitGatewayId)
.formParam("Filter.2.Name", "default-association-route-table")
.formParam("Filter.2.Value.1", "true")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(defaultOnly, containsString(defaultRouteTableId));
assertThat(defaultOnly, not(containsString(routeTableId)));
String byId = routeTablesFilteredBy("transit-gateway-route-table-id", routeTableId);
assertThat(byId, containsString(routeTableId));
assertThat(byId, not(containsString(defaultRouteTableId)));
assertThat(routeTablesFilteredBy("transit-gateway-route-table-id", "tgw-rtb-0123456789abcdef0"),
not(containsString("<item>")));
assertThat(routeTablesFilteredBy("state", "available"), containsString(routeTableId));
assertThat(routeTablesFilteredBy("state", "deleting"), not(containsString("<item>")));
}
@Test
@Order(7)
void associationFiltersPickOutASingleAttachment() {
given()
.formParam("Action", "GetTransitGatewayRouteTableAssociations")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("Filter.1.Name", "transit-gateway-attachment-id")
.formParam("Filter.1.Value.1", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("GetTransitGatewayRouteTableAssociationsResponse.associations.item.transitGatewayAttachmentId",
equalTo(attachmentId));
String unmatched = given()
.formParam("Action", "GetTransitGatewayRouteTableAssociations")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("Filter.1.Name", "transit-gateway-attachment-id")
.formParam("Filter.1.Value.1", "tgw-attach-0123456789abcdef0")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(unmatched, not(containsString("<item>")));
assertThat(associationsFilteredBy("resource-id", vpcId), containsString(attachmentId));
assertThat(associationsFilteredBy("resource-id", "vpc-0123456789abcdef0"),
not(containsString("<item>")));
assertThat(associationsFilteredBy("resource-type", "vpc"), containsString(attachmentId));
assertThat(associationsFilteredBy("resource-type", "vpn"), not(containsString("<item>")));
}
@Test
@Order(8)
void associationsIgnoreFiltersTheActionDoesNotModel() {
String unmodeled = given()
.formParam("Action", "GetTransitGatewayRouteTableAssociations")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("Filter.1.Name", "vpc-id")
.formParam("Filter.1.Value.1", "vpc-0123456789abcdef0")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(unmodeled, containsString(attachmentId));
}
@Test
@Order(9)
void theTwoDefaultTableFiltersReadTheirOwnFlag() {
String created = given()
.formParam("Action", "CreateTransitGateway")
.formParam("Options.DefaultRouteTableAssociation", "enable")
.formParam("Options.DefaultRouteTablePropagation", "disable")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
String gatewayId = extract(created, "transitGatewayId");
String associationOnlyTable = extract(created, "associationDefaultRouteTableId");
String byAssociation = given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("Filter.1.Name", "transit-gateway-id")
.formParam("Filter.1.Value.1", gatewayId)
.formParam("Filter.2.Name", "default-association-route-table")
.formParam("Filter.2.Value.1", "true")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(byAssociation, containsString(associationOnlyTable));
String byPropagation = given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("Filter.1.Name", "transit-gateway-id")
.formParam("Filter.1.Value.1", gatewayId)
.formParam("Filter.2.Name", "default-propagation-route-table")
.formParam("Filter.2.Value.1", "true")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(byPropagation, not(containsString("<item>")));
}
@Test
@Order(10)
void propagationFiltersPickOutASingleAttachment() {
given()
.formParam("Action", "GetTransitGatewayRouteTablePropagations")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("Filter.1.Name", "transit-gateway-attachment-id")
.formParam("Filter.1.Value.1", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("GetTransitGatewayRouteTablePropagationsResponse.transitGatewayRouteTablePropagations"
+ ".item.transitGatewayAttachmentId", equalTo(attachmentId));
String unmatched = given()
.formParam("Action", "GetTransitGatewayRouteTablePropagations")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("Filter.1.Name", "transit-gateway-attachment-id")
.formParam("Filter.1.Value.1", "tgw-attach-0123456789abcdef0")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
assertThat(unmatched, not(containsString("<item>")));
assertThat(propagationsFilteredBy("resource-id", vpcId), containsString(attachmentId));
assertThat(propagationsFilteredBy("resource-id", "vpc-0123456789abcdef0"),
not(containsString("<item>")));
assertThat(propagationsFilteredBy("resource-type", "vpc"), containsString(attachmentId));
assertThat(propagationsFilteredBy("resource-type", "vpn"), not(containsString("<item>")));
}
}