Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): reject a DescribeVpcs filter name the service does not serve (#4366)
lex00fde0688ef361
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 6c1e405dd0..84d39d3df4 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@@ -172,6 +172,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {private static final Set<String> VPN_GATEWAY_FILTERS = Set.of("amazon-side-asn", "attachment.state", "attachment.vpc-id", "availability-zone","state", "tag-key", "tag-value", "type", "vpn-gateway-id");+ /**+ * The filter names DescribeVpcs documents, plus three undocumented aliases matchesFilters+ * already honours and real EC2 accepts: "cidr-block", "isDefault" and "tag-value". Being more+ * permissive than the documentation is the safe direction, since the cost of refusing a name+ * callers really use is higher than the cost of serving one the docs omit.+ *+ * <p>Every name here has a matching arm in matchesFilters. Accepting a name the matcher does+ * not implement would be worse than refusing it, because the filter would then be accepted and+ * silently match everything.+ */+ private static final Set<String> VPC_FILTERS = Set.of(+ "cidr", "cidr-block",+ "cidr-block-association.association-id", "cidr-block-association.cidr-block",+ "cidr-block-association.state", "dhcp-options-id",+ "ipv6-cidr-block-association.association-id", "ipv6-cidr-block-association.ipv6-cidr-block",+ "ipv6-cidr-block-association.ipv6-pool", "ipv6-cidr-block-association.state",+ "is-default", "isDefault", "owner-id", "state", "tag-key", "tag-value", "vpc-id");private static final Set<String> EGRESS_ONLY_INTERNET_GATEWAY_FILTERS = Set.of("attachment.state", "attachment.vpc-id","egress-only-internet-gateway-id", "tag-key", "tag-value");@@ -4022,6 +4039,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {public List<Vpc> describeVpcs(String region, List<String> vpcIds, Map<String, List<String>> filters) {ensureDefaultResources(region);+ // matchesFilters answers true for a name it does not recognise, so without this an+ // unsupported filter matches every VPC instead of narrowing anything. Real EC2 rejects the+ // name outright, which is the difference between a wrong result and an error.+ requireSupportedFilters(filters, VPC_FILTERS);if (!vpcIds.isEmpty()) {for (String id : vpcIds) {getRequiredVpc(region, id);@@ -8162,6 +8183,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {.anyMatch(a -> matchesValue(values, a.getCidrBlock()));case "cidr-block-association.state" -> vpc.getCidrBlockAssociationSet().stream().anyMatch(a -> matchesValue(values, a.getCidrBlockState()));+ case "dhcp-options-id" -> matchesValue(values, vpc.getDhcpOptionsId());+ case "owner-id" -> matchesValue(values, vpc.getOwnerId());+ case "ipv6-cidr-block-association.ipv6-pool" -> vpc.getIpv6CidrBlockAssociationSet()+ .stream().anyMatch(a -> matchesValue(values, a.getIpv6Pool()));case "ipv6-cidr-block-association.association-id" -> vpc.getIpv6CidrBlockAssociationSet().stream().anyMatch(a -> matchesValue(values, a.getAssociationId()));case "ipv6-cidr-block-association.ipv6-cidr-block" -> vpc.getIpv6CidrBlockAssociationSet().stream()diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeVpcsUnknownFilterIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeVpcsUnknownFilterIntegrationTest.javanew file mode 100644index 0000000000..183eb0ad2b--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeVpcsUnknownFilterIntegrationTest.java@@ -0,0 +1,88 @@+package io.github.hectorvent.floci.services.ec2;++import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.ValidatableResponse;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.not;++/**+ * DescribeVpcs rejects a filter name it does not serve.+ *+ * <p>{@code matchesFilters} answers true for an unrecognised name, so an unsupported filter used to+ * match every VPC in the account rather than narrowing anything. A caller reading that result sees+ * a wrong answer with no indication it was wrong, which is worse than an error. Real EC2 refuses+ * the name.+ */+@QuarkusTest+class Ec2DescribeVpcsUnknownFilterIntegrationTest {++ private static final String AUTH_HEADER =+ "AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/ec2/aws4_request";++ private static ValidatableResponse describeVpcs(String filterName, String value) {+ return given()+ .formParam("Action", "DescribeVpcs")+ .formParam("Filter.1.Name", filterName)+ .formParam("Filter.1.Value.1", value)+ .header("Authorization", AUTH_HEADER)+ .when().post("/").then();+ }++ @Test+ void anUnknownFilterNameIsRefused() {+ describeVpcs("not-a-filter", "anything")+ .statusCode(400)+ .body(containsString("InvalidParameterValue"))+ .body(containsString("not-a-filter"));+ }++ @Test+ void theDocumentedFilterNamesAreAccepted() {+ for (String name : new String[]{"cidr", "cidr-block-association.state", "dhcp-options-id",+ "ipv6-cidr-block-association.ipv6-pool", "is-default", "owner-id", "state",+ "tag-key", "vpc-id"}) {+ describeVpcs(name, "whatever").statusCode(200);+ }+ }++ /** The undocumented alias matchesFilters already honours, which real EC2 accepts too. */+ @Test+ void theCidrBlockAliasStaysAccepted() {+ describeVpcs("cidr-block", "10.0.0.0/16").statusCode(200);+ }++ /** A tag filter names the tag in the filter name, so the prefix has to pass the check. */+ @Test+ void aTagFilterIsAccepted() {+ describeVpcs("tag:Owner", "TeamA").statusCode(200);+ }++ /**+ * Accepting a name the matcher does not implement is worse than refusing it, because the filter+ * then matches every VPC rather than narrowing. A 200 alone does not catch that, so these check+ * the result actually narrows.+ */+ @Test+ void everyAcceptedNameNarrowsRatherThanMatchingEverything() {+ String vpcId = given()+ .formParam("Action", "CreateVpc").formParam("CidrBlock", "10.91.0.0/16")+ .header("Authorization", AUTH_HEADER)+ .when().post("/").then().statusCode(200)+ .extract().path("CreateVpcResponse.vpc.vpcId");++ // Values that cannot belong to any VPC, so a working matcher returns none.+ describeVpcs("dhcp-options-id", "dopt-no-such-option")+ .statusCode(200).body(not(containsString(vpcId)));+ describeVpcs("owner-id", "999999999999")+ .statusCode(200).body(not(containsString(vpcId)));+ describeVpcs("ipv6-cidr-block-association.ipv6-pool", "no-such-pool")+ .statusCode(200).body(not(containsString(vpcId)));++ // And the matching value still finds it.+ describeVpcs("dhcp-options-id", "dopt-default")+ .statusCode(200).body(containsString(vpcId));+ }+}