fix(ec2): align catalog image owner filtering (#3463)

* fix(ec2): align catalog image owner filtering

* fix(ec2): preserve catalog aliases in image filters

* test(ec2): pin supported image owner selectors
This commit is contained in:
Franklin G. Mendoza authored and GitHub committed 2026-09-12 23:28:52 -05:00
1 parent 505b5d286e
commit a6f1751dfb
2 files changed
+38 -1

No files matched your search

@@ -4546,9 +4546,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
List<Image> catalogImages = imageCatalog.images().stream()
.filter(Ec2ImageCatalog.CatalogImage::advertised)
.filter(img -> img.matchesIdOrAlias(imageIds))
.filter(img -> img.matchesOwner(owners))
// Catalog aliases (for example ami-amazonlinux2) are a Floci compatibility layer,
// so catalog filters must run before toImage() discards idsAndAliases(). Owner
// matching still runs on the materialized image so AWS aliases such as amazon/self
// are resolved against the real owner account id consistently with registered images.
.filter(img -> matchesImageFilters(img, filters))
.map(Ec2ImageCatalog.CatalogImage::toImage)
.filter(img -> matchesImageOwners(img, owners))
.collect(Collectors.toList());
List<Image> createdImages = registeredImages.scan(k -> true).stream()
.filter(img -> region.equals(img.getRegion()))
@@ -577,6 +577,39 @@ class Ec2ServiceTest {
assertTrue(resolved.systemd());
}
@Test
void describeImagesUsesOnlyAwsSupportedOwnerSelectorsForCatalogImages() {
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
new AmiImageResolver(imageCatalog), imageCatalog, new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
List<Image> amazon = service.describeImages(
"us-east-1", List.of(), List.of("amazon"), Map.of());
assertTrue(amazon.stream().anyMatch(image -> "amazon".equals(image.getImageOwnerAlias())));
List<Image> unsupportedAlias = service.describeImages(
"us-east-1", List.of(), List.of("canonical"), Map.of());
assertTrue(unsupportedAlias.isEmpty(),
"catalog imageOwnerAlias values must not turn arbitrary owner strings into Owners selectors");
}
@Test
void describeImagesMatchesCatalogAliasInImageIdFilter() {
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
new AmiImageResolver(imageCatalog), imageCatalog, new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
List<Image> images = service.describeImages(
"us-east-1", List.of(), List.of(), Map.of("image-id", List.of("ami-amazonlinux2")));
assertEquals(1, images.size());
assertEquals("ami-0abcdef1234567890", images.getFirst().getImageId());
}
@Test
void describeImagesResolvesUnknownLaunchableAmiId() {
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();