mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
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:
1 parent
aeb798939b
commit
9f99c7c2c2
9 files changed
+512
-7
No files matched your search
@@ -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");
|
||||
|
||||
@@ -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<String> 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<Tag> currentTags = tagsOf(image.getRegion(), image.getImageId())
|
||||
.orElseGet(() -> image.getTags() == null ? List.of() : image.getTags());
|
||||
|
||||
@@ -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; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user