diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2ImageProvenanceTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2ImageProvenanceTest.java new file mode 100644 index 0000000000..3f005d5996 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2ImageProvenanceTest.java @@ -0,0 +1,44 @@ +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.Image; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("EC2 image creation provenance") +class Ec2ImageProvenanceTest { + + @Test + @DisplayName("A copied image reports its source image and region, and the source filters find it") + void aCopiedImageReportsItsSourceAndTheSourceFiltersFindIt() { + String region = TestFixtures.region().id(); + try (Ec2Client ec2 = TestFixtures.ec2Client()) { + String sourceId = ec2.registerImage(r -> r.name(TestFixtures.uniqueName("provenance-source"))) + .imageId(); + try { + String copyId = ec2.copyImage(r -> r.sourceImageId(sourceId).sourceRegion(region) + .name(TestFixtures.uniqueName("provenance-copy"))).imageId(); + try { + Image copy = ec2.describeImages(r -> r.imageIds(copyId)).images().get(0); + + assertThat(copy.sourceImageId()).isEqualTo(sourceId); + assertThat(copy.sourceImageRegion()).isEqualTo(region); + assertThat(copy.sourceInstanceId()).isNull(); + assertThat(ec2.describeImages(r -> r.filters(Filter.builder() + .name("source-image-id").values(sourceId).build())).images()) + .extracting(Image::imageId).containsExactly(copyId); + assertThat(ec2.describeImages(r -> r.imageIds(sourceId)).images().get(0).sourceImageId()) + .isNull(); + } finally { + // The copy gets its own root snapshot, which deregistering alone would leave behind. + ec2.deregisterImage(r -> r.imageId(copyId).deleteAssociatedSnapshots(true)); + } + } finally { + ec2.deregisterImage(r -> r.imageId(sourceId)); + } + } + } +} diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 5372586c04..29c862db69 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -297,6 +297,8 @@ private gateway lifecycles are not modelled. | DescribeImages | Returns AMI metadata known to the local EC2 service. | | CreateImage | Captures an instance as a new AMI. Reboots the source unless `NoReboot=true`. | | RegisterImage | Registers an AMI from supplied metadata and block device mappings. | +| CopyImage | Copies an AMI from `SourceRegion` into the request's region as a new AMI the caller owns. | +| DeregisterImage | Deregisters an AMI. Its snapshots are kept unless `DeleteAssociatedSnapshots=true`, and a snapshot another AMI still uses is kept either way. | Every resource EC2 creates is owned by the account the request resolves to, the same account [STS](sts.md) reports for those credentials, and that account is what `ownerId` and the resource ARN @@ -306,6 +308,16 @@ registered, and `--owners self` resolves to the same account. This is what lets `000000000000`. The `amazon` and `aws-marketplace` aliases still resolve to the AWS-owned accounts that publish those images. +`DescribeImages` reports where an AMI came from. An AMI made by `CreateImage` reports the instance +it captured as `SourceInstanceId`, and the AMI that instance was launched from, with its region, as +`SourceImageId` and `SourceImageRegion`. An AMI made by `CopyImage` reports the AMI it copied and +that AMI's region, and no instance. Each names its immediate source by AMI ID, even when the +instance was launched, or the copy requested, with a catalog alias such as `ami-amazonlinux2`, and +keeps naming it after the source is deregistered. AMIs made by `RegisterImage`, catalog AMIs and +AMIs saved by an earlier Floci version report none. The `source-image-id`, `source-image-region` and +`source-instance-id` filters match these fields, and `source-image-id` also takes a catalog alias, +as `image-id` does. + ### Tags | Action | Description | 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 2838a5f054..6100762998 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 @@ -3506,6 +3506,9 @@ public class Ec2QueryHandler { .elem("hypervisor", img.getHypervisor()) .elem("imageOwnerAlias", img.getImageOwnerAlias()) .elem("creationDate", img.getCreationDate()) + .elem("sourceImageId", img.getCreationSourceImageId()) + .elem("sourceImageRegion", img.getCreationSourceImageRegion()) + .elem("sourceInstanceId", img.getCreationSourceInstanceId()) .raw(blockDeviceMappingXml(img.getBlockDeviceMappings())) .raw(tagSetXml(img.getTags())) .end("item"); 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 232d070a04..049cf973dc 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 @@ -7393,6 +7393,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab // of falling through to the catalog default. This is also the fallback when the file // system cannot be captured below. image.setSourceImageId(resolveLaunchableImageId(region, source.getImageId())); + // The resolved image rather than the instance's id: an instance launched by a catalog alias + // records the alias. + image.setCreationSourceImageId(sourceImage != null ? sourceImage.getImageId() : source.getImageId()); + image.setCreationSourceImageRegion(region); + image.setCreationSourceInstanceId(source.getInstanceId()); // Capture the instance's file system. Without this the AMI is a metadata record that // launches the *base* image, so everything provisioned on the source instance is @@ -7912,6 +7917,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab // chain of CreateImage parents lives; it bottoms out at a catalog id, which is // region-agnostic. copy.setSourceImageId(resolveLaunchableImageId(sourceRegion, sourceImageId)); + // The resolved id rather than the request's: a catalog image may be named by an alias. + copy.setCreationSourceImageId(source.getImageId()); + copy.setCreationSourceImageRegion(sourceRegion); // The captured file system is the point of a CreateImage AMI, and the ancestry the // copy inherits does not carry it: sourceImageId is flattened to a launchable catalog // id, and the chain in between lives in the source region where the copy cannot see @@ -8431,6 +8439,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab case "owner-id" -> matchesFilterValue(values, image.getOwnerId()); case "root-device-name" -> matchesFilterValue(values, image.getRootDeviceName()); case "root-device-type" -> matchesFilterValue(values, image.getRootDeviceType()); + case "source-image-id" -> matchesSourceImageId(values, image.getCreationSourceImageId()); + case "source-image-region" -> matchesFilterValue(values, image.getCreationSourceImageRegion()); + case "source-instance-id" -> matchesFilterValue(values, image.getCreationSourceInstanceId()); case "state" -> matchesFilterValue(values, image.getState()); case "virtualization-type" -> matchesFilterValue(values, image.getVirtualizationType()); default -> true; @@ -8513,12 +8524,30 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab case "owner-id" -> matchesFilterValue(values, image.getOwnerId()); case "root-device-name" -> matchesFilterValue(values, image.getRootDeviceName()); case "root-device-type" -> matchesFilterValue(values, image.getRootDeviceType()); + case "source-image-id" -> matchesSourceImageId(values, image.getCreationSourceImageId()); + case "source-image-region" -> matchesFilterValue(values, image.getCreationSourceImageRegion()); + case "source-instance-id" -> matchesFilterValue(values, image.getCreationSourceInstanceId()); case "state" -> matchesFilterValue(values, image.getState()); case "virtualization-type" -> matchesFilterValue(values, image.getVirtualizationType()); default -> true; }; } + /** + * A catalog source is recorded by its AMI id, but an instance launched by one of its aliases + * reports the alias as its ImageId, so the filter takes either, as image-id does. + */ + private boolean matchesSourceImageId(List values, String sourceImageId) { + if (sourceImageId == null) { + return matchesFilterValue(values, null); + } + return imageCatalog.findByIdOrAlias(sourceImageId) + .map(Ec2ImageCatalog.CatalogImage::idsAndAliases) + .orElse(List.of(sourceImageId)) + .stream() + .anyMatch(id -> matchesFilterValue(values, id)); + } + private Image withCurrentImageTags(Image image) { List currentTags = tagsOf(image.getRegion(), image.getImageId()) .orElseGet(() -> image.getTags() == null ? List.of() : image.getTags()); diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Image.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Image.java index 8349b43593..3810046684 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Image.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Image.java @@ -26,12 +26,24 @@ public class Image { private String creationDate; private String region; /** - * For images produced by CreateImage: the AMI the source instance was launched from. Not an - * EC2 field — Floci uses it to resolve the guest image to run, since a generated ami-* id is - * not in the catalog and would otherwise fall back to the default guest. + * For images produced by CreateImage or CopyImage: the launchable ancestor, the catalog or + * registered image the chain of sources ends at. Floci uses it to resolve the guest image to + * run, since a generated ami-* id is not in the catalog and would otherwise fall back to the + * default guest. This is not the SourceImageId EC2 reports, which is + * {@link #creationSourceImageId}. */ private String sourceImageId; + /** + * What DescribeImages reports as SourceImageId, SourceImageRegion and SourceInstanceId: the AMI + * a CreateImage or CopyImage image was made from, that AMI's region, and the instance a + * CreateImage image captured. Null for RegisterImage and catalog images, and for images saved + * before Floci kept them. + */ + private String creationSourceImageId; + private String creationSourceImageRegion; + private String creationSourceInstanceId; + /** * For images produced by CreateImage: the Docker image the source container was committed * to, which is what actually captures the instance's file system. Not an EC2 field. Null @@ -93,6 +105,15 @@ public class Image { public String getSourceImageId() { return sourceImageId; } public void setSourceImageId(String sourceImageId) { this.sourceImageId = sourceImageId; } + public String getCreationSourceImageId() { return creationSourceImageId; } + public void setCreationSourceImageId(String creationSourceImageId) { this.creationSourceImageId = creationSourceImageId; } + + public String getCreationSourceImageRegion() { return creationSourceImageRegion; } + public void setCreationSourceImageRegion(String creationSourceImageRegion) { this.creationSourceImageRegion = creationSourceImageRegion; } + + public String getCreationSourceInstanceId() { return creationSourceInstanceId; } + public void setCreationSourceInstanceId(String creationSourceInstanceId) { this.creationSourceInstanceId = creationSourceInstanceId; } + public String getDockerImage() { return dockerImage; } public void setDockerImage(String dockerImage) { this.dockerImage = dockerImage; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java index 6f7439c687..4edc70d408 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.ec2; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.XmlParser; import io.github.hectorvent.floci.services.ec2.model.BlockDeviceMapping; import io.github.hectorvent.floci.services.ec2.model.EbsBlockDevice; import io.github.hectorvent.floci.services.ec2.model.Image; @@ -273,14 +274,18 @@ class Ec2DeregisterAndCopyImageIntegrationTest { assertNotEquals(sourceId, copyId); assertTrue(copyId.startsWith("ami-")); + // Compared by imageId rather than anywhere in the XML: the copy names its source in + // sourceImageId. String west = ec2(WEST_AUTH, "DescribeImages", "Owner.1", "self"); - assertTrue(west.contains(copyId), "the copy must be visible in the destination region"); + List westIds = XmlParser.extractAll(west, "imageId"); + assertTrue(westIds.contains(copyId), "the copy must be visible in the destination region"); assertTrue(west.contains(copyName)); - assertFalse(west.contains(sourceId), "the source AMI stays in its own region"); + assertFalse(westIds.contains(sourceId), "the source AMI stays in its own region"); String east = ec2(EAST_AUTH, "DescribeImages", "Owner.1", "self"); - assertTrue(east.contains(sourceId)); - assertFalse(east.contains(copyId), "the copy must not leak back into the source region"); + List eastIds = XmlParser.extractAll(east, "imageId"); + assertTrue(eastIds.contains(sourceId)); + assertFalse(eastIds.contains(copyId), "the copy must not leak back into the source region"); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ImageCreationProvenanceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ImageCreationProvenanceIntegrationTest.java new file mode 100644 index 0000000000..b93140e71a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ImageCreationProvenanceIntegrationTest.java @@ -0,0 +1,265 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.github.hectorvent.floci.core.common.XmlParser; +import io.github.hectorvent.floci.services.ec2.model.Image; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Creation provenance through the public EC2 Query protocol: the {@code sourceImageId}, + * {@code sourceImageRegion} and {@code sourceInstanceId} DescribeImages reports, and the three + * filters on them. + */ +@QuarkusTest +class Ec2ImageCreationProvenanceIntegrationTest { + private static final String EAST = "us-east-1"; + private static final String WEST = "us-west-2"; + private static final String CATALOG = "ami-0abcdef1234567890"; + + @Inject + Ec2Service service; + + private final List images = new ArrayList<>(); + private final List instances = new ArrayList<>(); + + /** + * Each step stands alone and checks no status, so one that fails cannot leave the rest behind in + * the emulator every test class shares. + */ + @AfterEach + void cleanCreatedResources() { + for (OwnedResource instance : instances) { + request(instance.region(), "TerminateInstances", "InstanceId.1", instance.id()).post("/"); + } + for (OwnedResource image : images.reversed()) { + request(image.region(), "DeregisterImage", "ImageId", image.id(), + "DeleteAssociatedSnapshots", "true").post("/"); + } + } + + @Test + void registrationAndCatalogImagesOmitCreationProvenance() { + String registered = rememberImage(EAST, query(EAST, "RegisterImage", "Name", uniqueName())); + + assertNoProvenance(describe(EAST, registered)); + assertNoProvenance(describe(EAST, CATALOG)); + } + + @ParameterizedTest + @ValueSource(strings = {EAST, WEST}) + void copyReportsTheRequestedSourceAndItsRegion(String destination) { + String registered = rememberImage(EAST, query(EAST, "RegisterImage", "Name", uniqueName())); + String copied = copy(destination, EAST, registered); + String xml = describe(destination, copied); + + assertProvenance(xml, registered, EAST, null); + assertNoProvenance(describe(EAST, registered)); + } + + @Test + void copyOfCopyReportsImmediateSourceWhileRuntimeAncestorStaysFlattened() { + String first = copy(EAST, EAST, CATALOG); + String second = copy(WEST, EAST, first); + + assertProvenance(describe(WEST, second), first, EAST, null); + assertProvenance(describe(EAST, first), CATALOG, EAST, null); + Image stored = service.describeImages(WEST, List.of(second), List.of()).getFirst(); + assertEquals(CATALOG, stored.getSourceImageId()); + } + + @Test + void createImageReportsTheActualInstanceAndImmediateLaunchImage() { + String copied = copy(WEST, EAST, CATALOG); + String instance = launch(WEST, copied); + String captured = capture(WEST, instance); + + assertProvenance(describe(WEST, captured), copied, WEST, instance); + Image stored = service.describeImages(WEST, List.of(captured), List.of()).getFirst(); + assertEquals(CATALOG, stored.getSourceImageId()); + } + + @Test + void copyingACreatedImageDoesNotInheritItsSourceInstance() { + String instance = launch(EAST, CATALOG); + String captured = capture(EAST, instance); + String copied = copy(WEST, EAST, captured); + + assertProvenance(describe(EAST, captured), CATALOG, EAST, instance); + assertProvenance(describe(WEST, copied), captured, EAST, null); + } + + @Test + void copyingACatalogAliasRecordsTheAmiIdItNames() { + String copied = copy(EAST, EAST, "ami-amazonlinux2"); + + assertProvenance(describe(EAST, copied), CATALOG, EAST, null); + assertTrue(filteredImageIds(EAST, "source-image-id", "ami-amazonlinux2").contains(copied)); + } + + /** + * The instance keeps the alias as its ImageId, so filtering on that ImageId finds the capture, + * as it would on AWS, where the two are always the same id. + */ + @Test + void aCaptureOfAnInstanceLaunchedByAliasRecordsTheAmiIdItNames() { + String instance = launch(EAST, "ami-amazonlinux2"); + String captured = capture(EAST, instance); + + assertProvenance(describe(EAST, captured), CATALOG, EAST, instance); + assertTrue(filteredImageIds(EAST, "source-image-id", "ami-amazonlinux2").contains(captured)); + assertTrue(filteredImageIds(EAST, "source-image-id", "ami-amazonlinux?").contains(captured)); + assertTrue(filteredImageIds(EAST, "source-image-id", CATALOG).contains(captured)); + assertFalse(filteredImageIds(EAST, "source-image-id", "ami-ubuntu2204").contains(captured)); + } + + /** + * The EC2 user guide: "The source AMI information is preserved when ... Source AMIs are + * deregistered (deleted)." + */ + @Test + void aCopyStillNamesItsSourceAfterTheSourceIsDeregistered() { + String registered = rememberImage(EAST, query(EAST, "RegisterImage", "Name", uniqueName())); + String copied = copy(WEST, EAST, registered); + + query(EAST, "DeregisterImage", "ImageId", registered); + images.remove(new OwnedResource(EAST, registered)); + + assertProvenance(describe(WEST, copied), registered, EAST, null); + assertEquals(List.of(copied), filteredImageIds(WEST, "source-image-id", registered)); + } + + @Test + void theSourceFiltersMatchOnlyImagesWithThatProvenance() { + String instance = launch(EAST, CATALOG); + String captured = capture(EAST, instance); + String copied = copy(WEST, EAST, captured); + + assertEquals(List.of(captured), filteredImageIds(EAST, "source-instance-id", instance)); + assertEquals(List.of(copied), filteredImageIds(WEST, "source-image-id", captured)); + List fromEast = filteredImageIds(WEST, "source-image-region", EAST); + assertTrue(fromEast.contains(copied)); + assertFalse(fromEast.contains(CATALOG)); + assertFalse(filteredImageIds(WEST, "source-image-region", WEST).contains(copied)); + assertEquals(List.of(), filteredImageIds(EAST, "source-image-id", "ami-0000000000000none")); + assertEquals(List.of(captured), filteredImageIds(EAST, "source-instance-id", + instance.substring(0, instance.length() - 1) + "?")); + assertEquals(List.of(copied), filteredImageIds(WEST, "source-image-id", "ami-0000000000000none", captured)); + } + + /** + * DescribeImages makes up an image for a name wildcard that matches nothing, so that a CDK + * lookup resolves, and for an unknown id that RunInstances would still launch. Neither was made + * from anything, so a source filter leaves it out rather than being ignored. + */ + @Test + void anImageMadeUpForALookupOrAnUnknownIdMatchesNoSourceFilter() { + String pattern = "image-provenance-lookup-" + UUID.randomUUID() + "-*"; + assertEquals(1, imageIds(query(EAST, "DescribeImages", + "Filter.1.Name", "name", "Filter.1.Value.1", pattern)).size()); + assertEquals(List.of(), imageIds(query(EAST, "DescribeImages", + "Filter.1.Name", "name", "Filter.1.Value.1", pattern, + "Filter.2.Name", "source-image-id", "Filter.2.Value.1", "*"))); + + String unknown = "ami-" + UUID.randomUUID().toString().replace("-", "").substring(0, 17); + assertEquals(List.of(unknown), imageIds(query(EAST, "DescribeImages", "ImageId.1", unknown))); + assertEquals(List.of(), imageIds(query(EAST, "DescribeImages", "ImageId.1", unknown, + "Filter.1.Name", "source-image-region", "Filter.1.Value.1", EAST))); + } + + private String copy(String destination, String sourceRegion, String source) { + return rememberImage(destination, query(destination, "CopyImage", + "SourceRegion", sourceRegion, "SourceImageId", source, "Name", uniqueName())); + } + + private String capture(String region, String instance) { + return rememberImage(region, query(region, "CreateImage", + "InstanceId", instance, "Name", uniqueName(), "NoReboot", "true")); + } + + private String launch(String region, String image) { + String xml = query(region, "RunInstances", "ImageId", image, + "InstanceType", "t3.micro", "MinCount", "1", "MaxCount", "1"); + String id = only(xml, "instanceId"); + instances.add(new OwnedResource(region, id)); + return id; + } + + private List filteredImageIds(String region, String filter, String... values) { + List parameters = new ArrayList<>(List.of("Filter.1.Name", filter)); + for (int i = 0; i < values.length; i++) { + parameters.add("Filter.1.Value." + (i + 1)); + parameters.add(values[i]); + } + return imageIds(query(region, "DescribeImages", parameters.toArray(String[]::new))); + } + + private String rememberImage(String region, String xml) { + String id = only(xml, "imageId"); + images.add(new OwnedResource(region, id)); + return id; + } + + private String describe(String region, String image) { + String xml = query(region, "DescribeImages", "ImageId.1", image); + assertEquals(image, only(xml, "imageId")); + assertFalse(xml.contains("creationSource"), "Persistence property names must not leak into EC2 XML"); + return xml; + } + + private String query(String region, String action, String... parameters) { + return request(region, action, parameters).post("/").then().statusCode(200).extract().asString(); + } + + private static RequestSpecification request(String region, String action, String... parameters) { + RequestSpecification request = given().formParam("Action", action) + .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261008/" + + region + "/ec2/aws4_request"); + for (int i = 0; i < parameters.length; i += 2) { + request.formParam(parameters[i], parameters[i + 1]); + } + return request; + } + + private static void assertProvenance(String xml, String image, String region, String instance) { + assertEquals(image, only(xml, "sourceImageId")); + assertEquals(region, only(xml, "sourceImageRegion")); + assertEquals(instance == null ? List.of() : List.of(instance), + XmlParser.extractAll(xml, "sourceInstanceId")); + } + + private static void assertNoProvenance(String xml) { + for (String field : List.of("sourceImageId", "sourceImageRegion", "sourceInstanceId")) { + assertEquals(List.of(), XmlParser.extractAll(xml, field)); + } + } + + private static List imageIds(String xml) { + return XmlParser.extractAll(xml, "imageId"); + } + + private static String only(String xml, String field) { + List values = XmlParser.extractAll(xml, field); + assertEquals(1, values.size(), "Expected one " + field); + return values.getFirst(); + } + + private static String uniqueName() { + return "image-provenance-" + UUID.randomUUID(); + } + + private record OwnedResource(String region, String id) {} +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java index 8d8f884d45..f9e8b187b3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java @@ -175,6 +175,129 @@ class Ec2ServicePersistenceTest { assertEquals(12, snapshots.getFirst().getVolumeSize()); } + /** + * Creation provenance is stored beside the runtime ancestor rather than derived from it, so a + * restart reads both back from the image files and a copy still names its immediate source. + */ + @Test + void imageCreationProvenanceSurvivesRestartApartFromTheRuntimeAncestor(@TempDir Path dir) { + Ec2Service first = newService(dir); + String instanceId = first.runInstances(REGION, "ami-0abcdef1234567890", "t3.micro", 1, 1, null, + List.of(), null, null, List.of(), null, null).getInstances().getFirst().getInstanceId(); + Image captured = first.createImage(REGION, instanceId, "provenance-capture", null, true); + Image copied = first.copyImage("us-west-2", REGION, captured.getImageId(), "provenance-copy", null); + + Ec2Service restarted = newService(dir); + + Image restoredCapture = restarted.describeImages(REGION, List.of(captured.getImageId()), List.of(), Map.of()) + .getFirst(); + assertEquals("ami-0abcdef1234567890", restoredCapture.getCreationSourceImageId()); + assertEquals(REGION, restoredCapture.getCreationSourceImageRegion()); + assertEquals(instanceId, restoredCapture.getCreationSourceInstanceId()); + Image restoredCopy = restarted.describeImages("us-west-2", List.of(copied.getImageId()), List.of(), Map.of()) + .getFirst(); + assertEquals(captured.getImageId(), restoredCopy.getCreationSourceImageId()); + assertEquals(REGION, restoredCopy.getCreationSourceImageRegion()); + assertNull(restoredCopy.getCreationSourceInstanceId()); + assertEquals("ami-0abcdef1234567890", restoredCopy.getSourceImageId()); + } + + /** + * An image store as Floci wrote it before creation provenance was kept: a CreateImage AMI and its + * cross-region copy, each holding only its launchable ancestor, the catalog AMI, under + * {@code sourceImageId}. Both load and report no provenance, rather than passing that ancestor + * off as the image they were made from, and a copy made after the upgrade names its source. + */ + @Test + void imagesSavedBeforeCreationProvenanceWasKeptLoadWithoutIt(@TempDir Path dir) throws IOException { + String capture = "ami-d62643c08afd5b4dd"; + String copy = "ami-a6f93de002cd6d39b"; + String legacyJson = """ + { + "us-west-2::%2$s" : { + "imageId" : "%2$s", + "name" : "legacy-copy", + "description" : "legacy-copy", + "state" : "available", + "ownerId" : "000000000000", + "architecture" : "x86_64", + "rootDeviceType" : "ebs", + "rootDeviceName" : "/dev/xvda", + "virtualizationType" : "hvm", + "hypervisor" : "xen", + "platform" : null, + "imageOwnerAlias" : null, + "creationDate" : "2026-10-20T01:38:17.112Z", + "region" : "us-west-2", + "sourceImageId" : "ami-0abcdef1234567890", + "dockerImage" : null, + "blockDeviceMappings" : [ { + "deviceName" : "/dev/xvda", + "ebs" : { + "snapshotId" : "snap-551e5e74928952115", + "volumeSize" : 8, + "volumeType" : "gp2", + "deleteOnTermination" : true, + "encrypted" : null + } + } ], + "tags" : [ ], + "public" : false + }, + "us-east-1::%1$s" : { + "imageId" : "%1$s", + "name" : "legacy-capture", + "description" : "legacy-capture", + "state" : "available", + "ownerId" : "000000000000", + "architecture" : "x86_64", + "rootDeviceType" : "ebs", + "rootDeviceName" : "/dev/xvda", + "virtualizationType" : "hvm", + "hypervisor" : "xen", + "platform" : null, + "imageOwnerAlias" : null, + "creationDate" : "2026-10-20T01:38:17.105Z", + "region" : "us-east-1", + "sourceImageId" : "ami-0abcdef1234567890", + "dockerImage" : null, + "blockDeviceMappings" : [ { + "deviceName" : "/dev/xvda", + "ebs" : { + "snapshotId" : "snap-932ddc3942d093a00", + "volumeSize" : 8, + "volumeType" : "gp2", + "deleteOnTermination" : true, + "encrypted" : null + } + } ], + "tags" : [ ], + "public" : false + } + } + """.formatted(capture, copy); + Files.writeString(dir.resolve("ec2-registered-images.json"), legacyJson); + + Ec2Service restarted = newService(dir); + + Image restoredCapture = restarted.describeImages(REGION, List.of(capture), List.of(), Map.of()).getFirst(); + Image restoredCopy = restarted.describeImages("us-west-2", List.of(copy), List.of(), Map.of()).getFirst(); + assertEquals("legacy-capture", restoredCapture.getName()); + assertEquals("ami-0abcdef1234567890", restoredCopy.getSourceImageId()); + for (Image restored : List.of(restoredCapture, restoredCopy)) { + assertNull(restored.getCreationSourceImageId()); + assertNull(restored.getCreationSourceImageRegion()); + assertNull(restored.getCreationSourceInstanceId()); + } + assertTrue(restarted.describeImages("us-west-2", List.of(), List.of(), + Map.of("source-image-id", List.of("*"))).isEmpty()); + + Image copiedNow = restarted.copyImage("us-west-2", REGION, capture, "copied-after-upgrade", null); + assertEquals(capture, copiedNow.getCreationSourceImageId()); + assertEquals(REGION, copiedNow.getCreationSourceImageRegion()); + assertEquals("ami-0abcdef1234567890", copiedNow.getSourceImageId()); + } + @Test void managedPrefixListAndItsVersionHistorySurviveRestart(@TempDir Path dir) { Ec2Service first = newService(dir); 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 983448419b..5633277087 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 @@ -7238,6 +7238,9 @@ class Ec2ServiceTest { copy.setCreationDate(source.getCreationDate()); copy.setRegion(source.getRegion()); copy.setSourceImageId(source.getSourceImageId()); + copy.setCreationSourceImageId(source.getCreationSourceImageId()); + copy.setCreationSourceImageRegion(source.getCreationSourceImageRegion()); + copy.setCreationSourceInstanceId(source.getCreationSourceInstanceId()); copy.setDockerImage(source.getDockerImage()); copy.setBlockDeviceMappings(source.getBlockDeviceMappings() == null ? List.of() : new java.util.ArrayList<>(source.getBlockDeviceMappings()));