Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): align catalog image owner filtering (#3463)
Franklin G. Mendozaa6f1751dfb8c
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 b53e059b4e..57cd5147f5 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@@ -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()))diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.javaindex d8b6447ab6..89911f6059 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java@@ -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());+ }+@Testvoid describeImagesResolvesUnknownLaunchableAmiId() {Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();