fix(ec2): report image creation provenance and its three filters (#6059)

* fix(ec2): report image creation provenance and its three filters

DescribeImages never returned SourceImageId, SourceImageRegion or
SourceInstanceId, and the source-image-id, source-image-region and
source-instance-id filters matched every image.

- A CreateImage AMI reports the instance it captured, and the AMI that
  instance was launched from with its region.
- A CopyImage AMI reports the AMI it copied and its SourceRegion, and no
  instance.
- Both name the source by its AMI id, also when the request used a
  catalog alias, and keep naming it after the source is deregistered.
- Registered and catalog AMIs report none. The filters match only
  images that carry the field, so the image DescribeImages makes up for
  a CDK lookup or an unknown launchable id no longer passes them.

The fields are stored apart from the flattened ancestor Floci launches
from, so a copy of a copy names its immediate source and still boots
the right base image. Images saved before this change load without
provenance.

Split out of #5663 at review.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): accept a catalog alias in the source-image-id filter

An instance launched by a catalog alias such as ami-amazonlinux2 keeps
the alias as its ImageId, while the AMI captured from it records the
AMI id the alias names. Filtering on the instance's ImageId therefore
found nothing, where on AWS the two are always the same id.
source-image-id now matches the recorded id or any alias of it, as
image-id already does for catalog images.

Review follow-ups in the same area:
- a test pins the catalog branch of source-image-region, and the
  restart test names the flattened ancestor it expects;
- the detached-storage test double copies the provenance fields;
- the provenance test cleans up each resource independently, and the
  compatibility test deletes the copy's snapshot;
- the AMI docs list CopyImage and DeregisterImage and say the filter
  takes an alias.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(ec2): pin wildcard alias matching in source-image-id

The provenance test now checks that a wildcard on a catalog alias finds
a capture, as it does for image-id, and reads the source-image-region
result once. The DeregisterImage row in the AMI docs says a snapshot
another AMI still uses is kept even with DeleteAssociatedSnapshots.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-20 07:12:21 -04:00
1 parent aeb798939b
commit 9f99c7c2c2
9 files changed
+512 -7

No files matched your search

@@ -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<String> 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<String> eastIds = XmlParser.extractAll(east, "imageId");
assertTrue(eastIds.contains(sourceId));
assertFalse(eastIds.contains(copyId), "the copy must not leak back into the source region");
}
@Test
@@ -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<OwnedResource> images = new ArrayList<>();
private final List<OwnedResource> 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<String> 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<String> filteredImageIds(String region, String filter, String... values) {
List<String> 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<String> imageIds(String xml) {
return XmlParser.extractAll(xml, "imageId");
}
private static String only(String xml, String field) {
List<String> 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) {}
}
@@ -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);
@@ -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()));