fix(ec2): AND repeated filter names on every Describe action (#6170)

* fix(ec2): AND repeated filter names on every Describe action

The Query handler read Filter.N into a map keyed by name, so a filter
whose name was already given replaced the earlier one: tag-key=Env with
tag-key=Team matched every resource tagged Team, and two vpc-id filters
naming different VPCs matched the second VPC's resources. AWS joins
every filter with an AND, a repeated name included.

Every Describe action that reads Filter.N now passes the filters in
request order to the service, as DescribeKeyPairs, DescribeTags and
DescribeAvailabilityZones already did, and each filter narrows the
answer the way a lone filter of that name does. The service keeps its
map overloads for the callers that build filters of distinct names in
code; they delegate to the ordered overloads, so what they get back
does not change. DescribeImages builds its lookup image from the first
name wildcard and returns it only when it satisfies every filter.

Four existing tests passed null or any() as the filters of a method
that now has a second overload. They pass Map.of() or anyMap() instead,
which is what the code under test sends.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(ec2): sweep transit gateway peering attachments for repeated filters

DescribeTransitGatewayPeeringAttachments goes through the same shared
matcher as the other actions the parameterized tag-key case covers, but
had no case of its own. It now has one: two tag-key filters find only
the peering attachment that carries both keys.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): satisfy every repeated filter when building a lookup image

With a filter name given twice, DescribeImages built the image it
synthesizes for an unmatched lookup from the first name wildcard and the
first concrete value of each other filter, so a later filter of the same
name could reject it and the lookup came back empty: architecture with
x86_64 or arm64 and architecture with arm64 returned nothing. Each name
filter now offers its wildcard in turn, and each other value is the
first one that every filter of its name admits. A lone filter of each
name still yields the one image it always did.

DescribeInstanceTypeOfferings kept the types of its InstanceType.N
parameter only through the first instance-type filter, so repeating that
filter dropped them. They are now kept beside the types that every
instance-type filter names.

The docs say "an action" rather than "a Describe action", since the Get
and Search actions that read Filter.N are covered too.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): let an instance-type filter without values narrow nothing

DescribeInstanceTypeOfferings looked up the types its first
instance-type filter named, so a first filter without values limited the
answer to the InstanceType.N types while a later one without values
narrowed nothing: the answer depended on the order of the filters. A
filter without values now names and narrows nothing wherever it stands,
as a lone one never added a type.

The lookup image of DescribeImages tries the first wildcard of each name
filter, which the comment now says. Every filter value the image is
built from is tested by the image matchers' own rule.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* refactor(ec2): name the per-filter checks of repeated filters

The test that every filter of one name admits a value was written out by
hand at each site. Two helpers beside everyFilterAdmits now carry it:
everyFilterContains, for exact values where a filter without values
admits anything (security group rules, prefix lists, the later
instance-type filters of DescribeInstanceTypeOfferings), and
everyFilterMatches, for values with wildcards where a filter without
values matches nothing (the location filters of offerings, and the
spot-price and timestamp filters of DescribeSpotPriceHistory). No
answer changes.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): match instance-type wildcards in DescribeInstanceTypeOfferings

AWS takes * and ? in an instance-type filter value of
DescribeInstanceTypeOfferings (the CLI reference filters on c5*), but
Floci looked each value up by its exact name, so a lone t3.* found
nothing and t3.* beside t3.micro found nothing either. A first filter
holding a wildcard now looks up the whole catalog, and every filter
matches a type with the same matcher as the location filter. Exact
values give the same answer as before.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(ec2): pin an empty lookup for overlapping name patterns

Two name filters foo-* and *-bar-* overlap, but the image built from
the first wildcard of each breaks the other filter. DescribeImages
returns no image here, as AWS returns only images that exist; before
the filters were ANDed it returned one that broke foo-*.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): match offerings instance-type values with the linear glob

The instance-type wildcards of DescribeInstanceTypeOfferings went
through the regex matcher, which backtracks on a caller-supplied
pattern of many stars: twenty stars took about sixteen seconds per
request. They now go through everyFilterAdmits, the two-pointer glob
the image filters already use. Value-less filters are dropped before
the check, so no answer changes. Also reword a test comment.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(ec2): name the matcher a many-star pattern goes through

The glob that matches instance-type values is a two-pointer match, not
a strictly linear one, so the test's Javadoc says that instead.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-21 22:44:26 -04:00
1 parent e38980b6c1
commit 7c202a8966
10 files changed
+1561 -139

No files matched your search

@@ -0,0 +1,116 @@
package com.floci.test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.Filter;
import software.amazon.awssdk.services.ec2.model.SecurityGroupRule;
import software.amazon.awssdk.services.ec2.model.Subnet;
import software.amazon.awssdk.services.ec2.model.Vpc;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* AWS joins every filter with an AND, so a filter name given twice narrows the answer twice
* instead of the second filter replacing the first.
*/
@DisplayName("EC2 Describe actions AND filters that repeat a name")
class Ec2RepeatedFilterNamesTest {
private static Filter filter(String name, String... values) {
return Filter.builder().name(name).values(values).build();
}
@Test
@DisplayName("Two tag-key filters find only the VPC that carries both keys")
void twoTagKeyFiltersFindTheVpcWithBothKeys() {
String only = TestFixtures.uniqueName("RepeatedOnly");
String shared = TestFixtures.uniqueName("RepeatedShared");
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
String both = ec2.createVpc(r -> r.cidrBlock("10.181.0.0/16")).vpc().vpcId();
String one = ec2.createVpc(r -> r.cidrBlock("10.182.0.0/16")).vpc().vpcId();
try {
ec2.createTags(r -> r.resources(both, one).tags(t -> t.key(shared).value("v")));
ec2.createTags(r -> r.resources(both).tags(t -> t.key(only).value("v")));
assertThat(vpcIds(ec2, filter("tag-key", shared))).containsExactlyInAnyOrder(both, one);
assertThat(vpcIds(ec2, filter("tag-key", only), filter("tag-key", shared))).containsExactly(both);
assertThat(vpcIds(ec2, filter("tag-key", shared), filter("tag-key", only))).containsExactly(both);
} finally {
ec2.deleteVpc(r -> r.vpcId(both));
ec2.deleteVpc(r -> r.vpcId(one));
}
}
}
@Test
@DisplayName("Two vpc-id filters naming different VPCs find no subnet")
void twoVpcIdFiltersFindNoSubnet() {
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
String first = ec2.createVpc(r -> r.cidrBlock("10.183.0.0/16")).vpc().vpcId();
String second = ec2.createVpc(r -> r.cidrBlock("10.184.0.0/16")).vpc().vpcId();
String firstSubnet = null;
String secondSubnet = null;
try {
firstSubnet = ec2.createSubnet(r -> r.vpcId(first).cidrBlock("10.183.1.0/24")).subnet().subnetId();
secondSubnet = ec2.createSubnet(r -> r.vpcId(second).cidrBlock("10.184.1.0/24")).subnet().subnetId();
assertThat(subnetIds(ec2, filter("vpc-id", second))).containsExactly(secondSubnet);
assertThat(subnetIds(ec2, filter("vpc-id", first), filter("vpc-id", second))).isEmpty();
assertThat(subnetIds(ec2, filter("vpc-id", first, second), filter("vpc-id", second)))
.containsExactly(secondSubnet);
} finally {
for (String subnet : new String[] {firstSubnet, secondSubnet}) {
if (subnet != null) {
ec2.deleteSubnet(r -> r.subnetId(subnet));
}
}
ec2.deleteVpc(r -> r.vpcId(first));
ec2.deleteVpc(r -> r.vpcId(second));
}
}
}
@Test
@DisplayName("Two group-id filters naming different groups find no security group rule")
void twoGroupIdFiltersFindNoSecurityGroupRule() {
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
String vpc = ec2.createVpc(r -> r.cidrBlock("10.185.0.0/16")).vpc().vpcId();
String first = null;
String second = null;
try {
first = ec2.createSecurityGroup(r -> r.vpcId(vpc).groupName(TestFixtures.uniqueName("repeated"))
.description("repeated filters")).groupId();
second = ec2.createSecurityGroup(r -> r.vpcId(vpc).groupName(TestFixtures.uniqueName("repeated"))
.description("repeated filters")).groupId();
String firstGroup = first;
String secondGroup = second;
assertThat(ruleGroups(ec2, filter("group-id", secondGroup))).isNotEmpty().containsOnly(secondGroup);
assertThat(ruleGroups(ec2, filter("group-id", firstGroup), filter("group-id", secondGroup))).isEmpty();
} finally {
for (String group : new String[] {first, second}) {
if (group != null) {
ec2.deleteSecurityGroup(r -> r.groupId(group));
}
}
ec2.deleteVpc(r -> r.vpcId(vpc));
}
}
}
private static List<String> vpcIds(Ec2Client ec2, Filter... filters) {
return ec2.describeVpcs(r -> r.filters(filters)).vpcs().stream().map(Vpc::vpcId).toList();
}
private static List<String> subnetIds(Ec2Client ec2, Filter... filters) {
return ec2.describeSubnets(r -> r.filters(filters)).subnets().stream().map(Subnet::subnetId).toList();
}
private static List<String> ruleGroups(Ec2Client ec2, Filter... filters) {
return ec2.describeSecurityGroupRules(r -> r.filters(filters)).securityGroupRules().stream()
.map(SecurityGroupRule::groupId).toList();
}
}