diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index b00f6afabf..6e8cdd1169 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -3170,6 +3170,7 @@ public class Ec2QueryHandler { .elem("imageOwnerAlias", img.getImageOwnerAlias()) .elem("creationDate", img.getCreationDate()) .raw(blockDeviceMappingXml(img.getBlockDeviceMappings())) + .raw(tagSetXml(img.getTags())) .end("item"); } xml.end("imagesSet").end("DescribeImagesResponse"); 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.java index 54e0905433..41edeed3c9 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 @@ -20,6 +20,7 @@ import java.util.Random; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiPredicate; import java.util.function.Function; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -4713,6 +4714,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { // A deregistered AMI is retained only as a tombstone, so that ancestry and // repeat-deregistration still resolve; DescribeImages must not report it. .filter(img -> !DEREGISTERED_STATE.equals(img.getState())) + .map(this::withCurrentImageTags) .filter(img -> matchesImageIds(img, imageIds)) .filter(img -> matchesImageOwners(img, owners)) .filter(img -> matchesRegisteredImageFilters(img, filters)) @@ -5684,6 +5686,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { private boolean matchesImageFilter(Ec2ImageCatalog.CatalogImage catalogImage, String name, List values) { Image image = catalogImage.toImage(); + if (isImageTagFilter(name)) { + return matchesImageTagFilter(image, name, values); + } return switch (name) { case "architecture" -> matchesFilterValue(values, image.getArchitecture()); case "hypervisor" -> matchesFilterValue(values, image.getHypervisor()); @@ -5752,6 +5757,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { } private boolean matchesRegisteredImageFilter(Image image, String name, List values) { + if (isImageTagFilter(name)) { + return matchesImageTagFilter(image, name, values); + } return switch (name) { case "architecture" -> matchesFilterValue(values, image.getArchitecture()); case "block-device-mapping.snapshot-id" -> image.getBlockDeviceMappings().stream() @@ -5775,6 +5783,35 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { }; } + private Image withCurrentImageTags(Image image) { + List currentTags = tags.get(image.getImageId()) + .orElseGet(() -> image.getTags() == null ? List.of() : image.getTags()); + image.setTags(new ArrayList<>(currentTags)); + // Persistent backends may return detached values from scan(), so do not rely on + // in-place mutation to retain CreateTags/DeleteTags changes across reads or restarts. + registeredImages.put(key(image.getRegion(), image.getImageId()), image); + return image; + } + + private boolean isImageTagFilter(String name) { + return name != null && (name.startsWith("tag:") || "tag-key".equals(name)); + } + + private boolean matchesImageTagFilter(Image image, String name, List values) { + return matchesTagFilter(image.getTags(), name, values, this::matchesFilterValue); + } + + private boolean matchesTagFilter(List resourceTags, String filterName, List values, + BiPredicate, String> valueMatcher) { + List tags = resourceTags == null ? List.of() : resourceTags; + if (filterName.startsWith("tag:")) { + String key = filterName.substring("tag:".length()); + return tags.stream().anyMatch(tag -> key.equals(tag.getKey()) + && valueMatcher.test(values, tag.getValue())); + } + return tags.stream().anyMatch(tag -> valueMatcher.test(values, tag.getKey())); + } + private Snapshot snapshotFrom(String region, String snapshotId, Image image, BlockDeviceMapping mapping) { EbsBlockDevice ebs = mapping.getEbs(); Snapshot snapshot = new Snapshot(); @@ -7264,15 +7301,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { } private boolean matchesFilter(Object resource, String filterName, List values, String region) { - if (filterName.startsWith("tag:")) { - String tagKey = filterName.substring(4); - List resourceTags = getResourceTags(resource); - return resourceTags.stream() - .anyMatch(t -> t.getKey().equals(tagKey) && matchesValue(values, t.getValue())); - } - if ("tag-key".equals(filterName)) { - List resourceTags = getResourceTags(resource); - return resourceTags.stream().anyMatch(t -> matchesValue(values, t.getKey())); + if (filterName.startsWith("tag:") || "tag-key".equals(filterName)) { + return matchesTagFilter(getResourceTags(resource), filterName, values, this::matchesValue); } if ("tag-value".equals(filterName)) { List resourceTags = getResourceTags(resource); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ImageTagIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ImageTagIntegrationTest.java new file mode 100644 index 0000000000..537524de7b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ImageTagIntegrationTest.java @@ -0,0 +1,99 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.startsWith; +import static org.hamcrest.xml.HasXPath.hasXPath; + +@QuarkusTest +class Ec2ImageTagIntegrationTest { + + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=test/20260917/us-east-1/ec2/aws4_request"; + + @Test + void describeImagesReturnsAndFiltersCurrentTags() { + String suffix = UUID.randomUUID().toString(); + String imageName = "tagged-image-" + suffix; + String tagKey = "Origin-" + suffix; + String tagValue = "Packer-" + suffix; + + String imageId = given() + .formParam("Action", "RegisterImage") + .formParam("Name", imageName) + .formParam("Architecture", "x86_64") + .formParam("RootDeviceName", "/dev/xvda") + .formParam("VirtualizationType", "hvm") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200) + .body("RegisterImageResponse.imageId", startsWith("ami-")) + .extract().path("RegisterImageResponse.imageId"); + + given() + .formParam("Action", "CreateTags") + .formParam("ResourceId.1", imageId) + .formParam("Tag.1.Key", tagKey) + .formParam("Tag.1.Value", tagValue) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200) + .body("CreateTagsResponse.return", equalTo("true")); + + given() + .formParam("Action", "DescribeImages") + .formParam("ImageId.1", imageId) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200) + .body("DescribeImagesResponse.imagesSet.item.imageId", equalTo(imageId)) + .body("DescribeImagesResponse.imagesSet.item.tagSet.item.key", equalTo(tagKey)) + .body("DescribeImagesResponse.imagesSet.item.tagSet.item.value", equalTo(tagValue)); + + given() + .formParam("Action", "DescribeImages") + .formParam("Filter.1.Name", "tag:" + tagKey) + .formParam("Filter.1.Value.1", tagValue) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("count(//*[local-name()='imagesSet']/*[local-name()='item'])", equalTo("1"))) + .body("DescribeImagesResponse.imagesSet.item.imageId", equalTo(imageId)); + + given() + .formParam("Action", "DescribeImages") + .formParam("Filter.1.Name", "tag:" + tagKey) + .formParam("Filter.1.Value.1", "does-not-match-" + suffix) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("count(//*[local-name()='imagesSet']/*[local-name()='item'])", equalTo("0"))); + + given() + .formParam("Action", "DescribeImages") + .formParam("Filter.1.Name", "tag-key") + .formParam("Filter.1.Value.1", tagKey) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200) + .body(hasXPath("count(//*[local-name()='imagesSet']/*[local-name()='item'])", equalTo("1"))) + .body("DescribeImagesResponse.imagesSet.item.imageId", equalTo(imageId)); + } +} 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.java index ab5ee18ecd..4ea8531528 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 @@ -58,6 +58,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Predicate; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -698,6 +699,40 @@ class Ec2ServiceTest { assertEquals("al2023-ami-2023.0.20230315.0-kernel-6.1-x86_64", catalog.getFirst().getName()); } + @Test + void describeImagesWritesRefreshedTagsBackWhenStorageReturnsDetachedImages() { + DetachedImageStorage imageStorage = new DetachedImageStorage(); + Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog(); + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), new AmiImageResolver(imageCatalog), imageCatalog, + new Ec2InstanceTypeCatalog(), + new InMemoryStorageFactory(Map.of("ec2-registered-images.json", imageStorage))); + + Image stored = new Image(); + stored.setImageId("ami-detached-tags"); + stored.setName("detached-tags"); + stored.setOwnerId("000000000000"); + stored.setPublic(false); + stored.setImageOwnerAlias(null); + stored.setRegion("us-east-1"); + imageStorage.put("us-east-1::ami-detached-tags", stored); + int putsBeforeDescribe = imageStorage.putCount(); + + Tag tag = new Tag(); + tag.setKey("ManagedBy"); + tag.setValue("test"); + service.createTags("us-east-1", List.of(stored.getImageId()), List.of(tag)); + + List described = service.describeImages( + "us-east-1", List.of(stored.getImageId()), List.of(), Map.of()); + + assertEquals(1, described.size()); + assertEquals("test", described.getFirst().getTags().getFirst().getValue()); + assertTrue(imageStorage.putCount() > putsBeforeDescribe, + "DescribeImages must persist refreshed tags even when scan() returns detached values"); + assertEquals("test", imageStorage.storedImage().getTags().getFirst().getValue()); + } + @Test void describeInstanceTypesUsesExactCatalogMatches() { Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), @@ -3899,4 +3934,66 @@ class Ec2ServiceTest { return AccountAwareStorageBackend.inMemory("000000000000"); } } + + private static final class DetachedImageStorage extends AccountAwareStorageBackend { + private final Map values = new HashMap<>(); + private int putCount; + + private DetachedImageStorage() { + super(new io.github.hectorvent.floci.core.storage.InMemoryStorage<>(), null, "000000000000"); + } + + @Override + public void put(String key, Image value) { + putCount++; + values.put(key, copy(value)); + } + + @Override + public Optional get(String key) { + Image value = values.get(key); + return value == null ? Optional.empty() : Optional.of(copy(value)); + } + + @Override + public List scan(Predicate keyFilter) { + return values.entrySet().stream() + .filter(entry -> keyFilter.test(entry.getKey())) + .map(entry -> copy(entry.getValue())) + .toList(); + } + + private int putCount() { + return putCount; + } + + private Image storedImage() { + return copy(values.get("us-east-1::ami-detached-tags")); + } + + private static Image copy(Image source) { + Image copy = new Image(); + copy.setImageId(source.getImageId()); + copy.setName(source.getName()); + copy.setDescription(source.getDescription()); + copy.setState(source.getState()); + copy.setOwnerId(source.getOwnerId()); + copy.setPublic(source.isPublic()); + copy.setArchitecture(source.getArchitecture()); + copy.setRootDeviceType(source.getRootDeviceType()); + copy.setRootDeviceName(source.getRootDeviceName()); + copy.setVirtualizationType(source.getVirtualizationType()); + copy.setHypervisor(source.getHypervisor()); + copy.setPlatform(source.getPlatform()); + copy.setImageOwnerAlias(source.getImageOwnerAlias()); + copy.setCreationDate(source.getCreationDate()); + copy.setRegion(source.getRegion()); + copy.setSourceImageId(source.getSourceImageId()); + copy.setDockerImage(source.getDockerImage()); + copy.setBlockDeviceMappings(source.getBlockDeviceMappings() == null + ? List.of() : new java.util.ArrayList<>(source.getBlockDeviceMappings())); + copy.setTags(source.getTags() == null ? List.of() : new java.util.ArrayList<>(source.getTags())); + return copy; + } + } }