feat(ec2): DescribeImages synthesizes an AMI for an unmatched wildcard name lookup (#2009)

* feat(ec2): DescribeImages synthesizes an AMI for an unmatched wildcard name lookup

CDK MachineImage.lookup is a synth-time context provider that queries by
a name wildcard and aborts cdk deploy on an empty response. When a
wildcard name filter matches no seeded AMI, synthesize a deterministic
one satisfying the name/owner/architecture filters so the lookup
resolves — the exact id is a runtime detail.

Fixes #2008

* fix(ec2): synthesized lookup AMI id must be 17 hex chars (was substring OOB)

* fix(ec2): a synthesized lookup AMI must satisfy the request that produced it

The synthesized name was built by truncating the pattern at its first wildcard,
so an infix pattern like ubuntu-*-20.04-* produced "ubuntu-20260101", which does
not satisfy it. Each wildcard is now substituted instead, and the scalar fields
a request can filter on (architecture, owner alias, state, root device,
virtualization type, hypervisor) take the requested value rather than a
hardcoded one.

Synthesis is also no longer unconditional: the result is checked against the
full filter set before being returned, and withheld when it cannot satisfy it.
An empty result is what the caller would otherwise have got, and it is better
than an AMI that contradicts the request without the caller being able to tell.

* fix(ec2): a name filter reads ? as the single-character wildcard AWS defines

AWS filter values take two wildcards, * for any run of characters and ? for
exactly one. wildcardMatches expanded only *, quoting everything else, so a
pattern like ubuntu-? matched nothing. wildcardToRegex further down the same
file already read both, so the two disagreed on the same syntax. That half is
not specific to lookup synthesis and affects any name filter.

Synthesis in turn substituted * and left ? in place, which handed back a name
carrying a literal ? that the requesting filter no longer matches once the
matcher reads ? properly. It now substitutes a single character for each one.

* fix(ec2): a synthesized lookup image resolves its owner and faces the owner scope

Owner.N takes aliases, and synthesizeLookupImage wrote the requested value
through verbatim, so Owner.1=self with no owner-id filter produced an image
owned by the literal "self". Ec2Service matches self by comparing the image
owner against the account id, so nothing resolves that. It now maps self to the
account id and passes anything else through.

The guard below it only re-checked Filter.N, so the image never faced the owner
scope that produced it and a foreign Owner.N still got an AMI back.
matchesImageOwners already existed privately on Ec2Service and is now exposed
next to imageMatchesFilters, rather than restating the rule in the handler.

Two tests, neither of which passed before, since nothing in the suite sent
Owner.N.

* fix(ec2): DescribeImages owner aliases resolve to the accounts they name

Resolving only self left amazon and aws-marketplace written through verbatim, so
a synthesized lookup image came back owned by the literal alias. The owner check
added alongside it then accepted that by string equality, which made the result
self-consistently wrong rather than merely unmatched.

Both aliases now resolve to their accounts in the synthesized image, and
matchesImageOwners resolves them too, so Owner.1=amazon matches an
Amazon-owned image rather than only one owned by the literal string.

* fix(ec2): the seeded Amazon Linux images carry their owner

matchesImageOwners learning the amazon alias only helped registered images. The
catalog matches on its own owner metadata, and five of the ten seeded entries
carry none, so Owner.1=amazon omitted Amazon Linux 2 and 2023 regardless.

Both now carry ownerId 137112412989 and the amazon alias, matching how the
Canonical entries are already seeded. Debian, Alpine and Windows Server are
still without owner metadata, which is the same pre-existing gap and needs their
real account ids rather than a guess.

* fix(ec2): lookup synthesis reads the wildcard value, not the first one

A filter's values are an OR, and the name pattern was taken with findFirst on the
list, so an exact value ahead of a wildcard meant the wildcard check failed and
synthesis never ran. The lookup then got an empty set that one of its own values
could have been satisfied by.

It now picks the first value carrying a wildcard. This predates the ? support,
which only widened which values qualify.

* fix(ec2): a synthesized image's owner id and alias agree with each other

The alias defaulted to amazon whatever the owner resolved to, so Owner.1=self
reported account 000000000000 beside imageOwnerAlias amazon. The alias is now
derived from the resolved account, and an account with no alias emits no element
at all, which is what AWS does.

The mirror was wrong too and is fixed with it. An owner-alias filter names an
account as much as Owner.N does, but was consulted by nothing, so filtering on
owner-alias alone left the id at the Amazon default and reported that account
beside someone else's alias. Owner resolution now reads the owner-id filter,
then Owner.N, then the owner-alias filter, then the default.

Asserted as a pair across all four owner scopes rather than per field. Each half
was confirmed by reverting the source change with the test in place and watching
it fail on the exact contradiction.
This commit is contained in:
lex00 authored and GitHub committed 2026-08-16 20:47:15 -05:00
1 parent 9280369b64
commit cdb6811bbf
4 files changed
+416 -7

No files matched your search

@@ -2,16 +2,18 @@ package io.github.hectorvent.floci.services.ec2;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.containsString;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -223,6 +225,257 @@ class Ec2IntegrationTest {
.body("DescribeImagesResponse.imagesSet.item.architecture", equalTo("x86_64"));
}
@Test
@Order(9)
void synthesizedLookupImageSatisfiesAnInfixWildcardName() {
// Truncating at the first wildcard produced "unmatched-vendor-20260101", which does not
// satisfy the pattern the caller asked for.
String name = given()
.formParam("Action", "DescribeImages")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-vendor-*-20.04-*")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.extract().path("DescribeImagesResponse.imagesSet.item.name");
assertThat(name, startsWith("unmatched-vendor-"));
assertThat(name, containsString("-20.04-"));
}
@Test
@Order(9)
void synthesizedLookupImageSatisfiesASingleCharacterWildcard() {
// ? matches exactly one character in an AWS filter. Leaving it in the synthesized name
// handed back "unmatched-vendor-?", which the requesting filter does not match once the
// matcher reads ? as a wildcard rather than a literal.
String name = given()
.formParam("Action", "DescribeImages")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-vendor-?-single")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.extract().path("DescribeImagesResponse.imagesSet.item.name");
assertThat(name, not(containsString("?")));
assertThat(name, startsWith("unmatched-vendor-"));
assertThat(name, endsWith("-single"));
assertEquals("unmatched-vendor-".length() + 1 + "-single".length(), name.length());
}
@Test
@Order(9)
void synthesizedLookupImageResolvesTheSelfOwnerAlias() {
// Owner.N takes aliases. Writing "self" through verbatim produced an image owned by the
// literal string, which no owner scope resolves to, and the guard never re-checked the
// owner because it only looked at Filter.N.
String owner = given()
.formParam("Action", "DescribeImages")
.formParam("Owner.1", "self")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-self-owned-*")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.extract().path("DescribeImagesResponse.imagesSet.item.imageOwnerId");
assertEquals("000000000000", owner);
}
/**
* Owner scope, then the id and alias the synthesized image must report together. A null
* expectedAlias means AWS reports no alias for that account, so the element is asserted
* absent from the body rather than compared as a value.
*/
private void assertSynthesizedOwnership(String ownerScope, String expectedId, String expectedAlias) {
var response = given()
.formParam("Action", "DescribeImages")
.formParam("Owner.1", ownerScope)
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-coherence-" + ownerScope + "-*")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.extract();
assertEquals(expectedId, response.path("DescribeImagesResponse.imagesSet.item.imageOwnerId"),
"imageOwnerId for Owner.1=" + ownerScope);
if (expectedAlias == null) {
assertThat("imageOwnerAlias for Owner.1=" + ownerScope,
response.asString(), not(containsString("<imageOwnerAlias>")));
} else {
assertEquals(expectedAlias,
response.path("DescribeImagesResponse.imagesSet.item.imageOwnerAlias").toString(),
"imageOwnerAlias for Owner.1=" + ownerScope);
}
}
@Test
@Order(9)
void synthesizedOwnershipIsSelfConsistentAcrossOwnerScopes() {
// Every earlier round here fixed one field and left the one beside it, so this asserts the
// pair together. An id resolved from the scope with an alias defaulted to amazon reports
// ownership that contradicts itself.
assertSynthesizedOwnership("self", "000000000000", null);
assertSynthesizedOwnership("amazon", "137112412989", "amazon");
assertSynthesizedOwnership("aws-marketplace", "679593333241", "aws-marketplace");
assertSynthesizedOwnership("099720109477", "099720109477", null);
}
@Test
@Order(9)
void anOwnerAliasFilterAloneStillAgreesWithTheOwnerId() {
// The mirror of the above. Filtering on the alias with no Owner.N left the id at the
// Amazon default, so the image reported aws-marketplace beside Amazon's account.
var response = given()
.formParam("Action", "DescribeImages")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-alias-filter-only-*")
.formParam("Filter.2.Name", "owner-alias")
.formParam("Filter.2.Value.1", "aws-marketplace")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.extract();
assertEquals("679593333241",
response.path("DescribeImagesResponse.imagesSet.item.imageOwnerId"));
assertEquals("aws-marketplace",
response.path("DescribeImagesResponse.imagesSet.item.imageOwnerAlias").toString());
}
@Test
@Order(9)
void aWildcardBehindAnExactNameValueStillSynthesizes() {
// A filter's values are an OR. Taking the first value outright picked the exact one, the
// wildcard check failed, and the lookup got an empty set it could have satisfied.
String name = given()
.formParam("Action", "DescribeImages")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-exact-name-no-wildcard")
.formParam("Filter.1.Value.2", "unmatched-second-value-*")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.extract().path("DescribeImagesResponse.imagesSet.item.name");
assertThat(name, startsWith("unmatched-second-value-"));
}
@Test
@Order(9)
void amazonOwnerScopeFindsTheSeededAmazonLinuxImages() {
// The catalog matches on its own owner metadata, and the two Amazon Linux entries carried
// none, so Owner.1=amazon omitted them however the registered-image matcher read the alias.
given()
.formParam("Action", "DescribeImages")
.formParam("Owner.1", "amazon")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("amzn2-ami-hvm"))
.body(containsString("al2023-ami"));
}
@Test
@Order(9)
void synthesizedLookupImageResolvesTheAmazonOwnerAlias() {
// imageOwnerId is always an account id in AWS. Writing the alias through left the image
// owned by the literal "amazon", and the owner check accepted it by string equality.
String owner = given()
.formParam("Action", "DescribeImages")
.formParam("Owner.1", "amazon")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-amazon-owned-*")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.extract().path("DescribeImagesResponse.imagesSet.item.imageOwnerId");
assertEquals("137112412989", owner);
}
@Test
@Order(9)
void synthesizedLookupImageIsWithheldFromAnOwnerScopeItCannotSatisfy() {
// A foreign owner scope cannot be satisfied by anything we synthesize, so the lookup gets
// the empty result rather than an AMI that contradicts the request.
given()
.formParam("Action", "DescribeImages")
.formParam("Owner.1", "999999999999")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-foreign-owner-*")
.formParam("Filter.2.Name", "owner-id")
.formParam("Filter.2.Value.1", "137112412989")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(not(containsString("<imageId>")));
}
@Test
@Order(9)
void synthesizedLookupImageHonorsOtherRequestedFilters() {
given()
.formParam("Action", "DescribeImages")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-vendor-arm-*")
.formParam("Filter.2.Name", "architecture")
.formParam("Filter.2.Value.1", "arm64")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
.body("DescribeImagesResponse.imagesSet.item.architecture", equalTo("arm64"));
}
@Test
@Order(9)
void synthesizedLookupImageIsWithheldWhenItCannotSatisfyTheRequest() {
// The synthesized description is fixed, so a description filter it cannot meet must yield
// an empty result rather than an AMI that violates the request.
given()
.formParam("Action", "DescribeImages")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "unmatched-vendor-none-*")
.formParam("Filter.2.Name", "description")
.formParam("Filter.2.Value.1", "a description no synthesized image carries")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(not(containsString("<imageId>")));
}
@Test
@Order(9)
void describeImagesWithUbuntu2404Arm64Filters() {