Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): apply filters to transit gateway route table lookups (#3820)
Matthew Ingled15b388aca73
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.javaindex f6f9579b99..e345b6a8cd 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java@@ -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())diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.javaindex 94d117aa71..ef0e46b42b 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java@@ -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());diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2TransitGatewayRouteTableIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2TransitGatewayRouteTableIntegrationTest.javaindex 9fe0897420..a02ab6709f 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2TransitGatewayRouteTableIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2TransitGatewayRouteTableIntegrationTest.java@@ -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>")));+ }}