mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ec2): honor CopyImageTags on CopyImage (#7097)
* fix(ec2): tag an AMI with the tags RegisterImage is given RegisterImage never read TagSpecification, so an AMI registered with image tags came back from DescribeImages untagged: its tag filters matched nothing and DescribeTags listed nothing for it. A tag specification for another resource type was accepted as well, although AWS fails that request. RegisterImage now reads its image tag specifications and refuses any other resource type with InvalidParameterValue. It checks the tags as CreateTags does (no aws: prefix, the key and value lengths, at most 50 tags) before registering anything, so a refused request leaves no AMI behind. The tags are stored before the AMI is, so DescribeImages never lists it without them, and a key given twice keeps its last value, as Floci's CreateTags keeps it. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(ec2): copy an AMI's tags when CopyImage sets CopyImageTags CopyImage never read CopyImageTags, so a copy asked to carry its source's tags came out untagged, and DescribeImages could not find it by them. With CopyImageTags=true the copy now gets the caller's tags on the source, read in the source region, less the aws: system tags, which AWS does not copy either. Without it, or with false, no tags are copied, which is AWS's default. The source keeps its tags, and the copy's are stored in the destination region only. The tags are read under the source AMI's own id, so a catalog AMI's tags on that id are copied too, while tags put on one of its aliases are not. Floci keeps each account's tags apart, so another account's tags never reach the copy. CopyImage still neither checks nor applies TagSpecification, and the docs now say so. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): stub the CopyImage overload the handler calls Upstream's handler test for CopyImage tag specifications stubbed the five-argument copyImage. On this branch the handler passes CopyImageTags as well, so the stub never matched, the mock returned null and the test failed with a NullPointerException. It now stubs the six-argument call with CopyImageTags false, which is what a request without it sends. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * refactor(ec2): leave RegisterImage tag specifications to upstream's handling Upstream now reads RegisterImage's TagSpecification in the Query handler, refuses other resource types and checks the tags before the AMI is registered, so this branch's own path for the same thing is a duplicate. The seven-argument registerImage overload and the tag code in registerImageLocked are removed, and RegisterImage goes back to upstream's code. CopyImage stores the tags it copies straight after it registers the copy instead. The branch's RegisterImage tests go too: upstream's Ec2CreateTagSpecificationIntegrationTest and Ec2QueryHandlerTest cover the same behaviour. What the branch adds is CopyImageTags alone. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): pin a TagSpecification tag over a copied tag of the same key With CopyImageTags=true and an image TagSpecification on the same request, the copy carries both sets. When both name the same key, the TagSpecification value is the one the copy keeps, since the handler applies the specification after the service copies the source's tags. The docs row says so; this test holds it. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(ec2): refuse a CopyImage over the tag limit before creating the copy With CopyImageTags=true and an image TagSpecification, the copied and the specified tags were each checked against the 50-tag limit on their own. The service then created the AMI and its snapshots and stored the copied tags, and only the handler's CreateTags of the specification saw the combined set: past 50 it answered TagLimitExceeded while the copy it had just made stayed behind, so a retry under the same Name failed with InvalidAMIName.Duplicate. The handler now passes the requested image tags to the service, which checks the union of copied and requested keys before it registers anything. The upstream handler test stubs the new signature. The docs and the test of a key given both ways now say that the TagSpecification value winning is Floci's behaviour: the public CopyImage reference does not say how AWS settles that. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * refactor(ec2): keep one CopyImage service signature --------- Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
dbb1ff6d89
commit
d173d65eb4
9 files changed
+243
-18
No files matched your search
@@ -4014,7 +4014,9 @@ public class Ec2QueryHandler {
|
||||
p.getFirst("SourceRegion"),
|
||||
p.getFirst("SourceImageId"),
|
||||
p.getFirst("Name"),
|
||||
p.getFirst("Description"));
|
||||
p.getFirst("Description"),
|
||||
Boolean.parseBoolean(p.getFirst("CopyImageTags")),
|
||||
imageTags);
|
||||
applyResourceTags(imageTags, region, image.getImageId());
|
||||
applyImageSnapshotTags(snapshotTags, region, image);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
|
||||
@@ -9254,10 +9254,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* {@code available} immediately, consistent with what CreateImage and RegisterImage already
|
||||
* report. A caller that waits for {@code available} therefore returns on its first poll.
|
||||
*
|
||||
* <p>Tags: "Default: Your user-defined AMI tags are not copied." With {@code copyImageTags}
|
||||
* the copy gets the caller's tags on the source, less the {@code aws:} system tags, which AWS
|
||||
* does not copy either. Floci keeps each account's tags apart and does not model shared
|
||||
* ({@code ec2:SharedTag/}) tags, so no other account's tag is visible to the caller or copied.
|
||||
* {@code requestedImageTags} are the image tags the request's TagSpecification will add after
|
||||
* the copy; with the copied ones they must stay within the per-resource limit, which is
|
||||
* checked before anything is created so a refused copy leaves no AMI or snapshot behind.
|
||||
*
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CopyImage.html">CopyImage</a>
|
||||
*/
|
||||
public Image copyImage(String destinationRegion, String sourceRegion, String sourceImageId,
|
||||
String name, String description) {
|
||||
String name, String description, boolean copyImageTags,
|
||||
List<Tag> requestedImageTags) {
|
||||
if (sourceImageId == null || sourceImageId.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter SourceImageId", 400);
|
||||
}
|
||||
@@ -9290,11 +9299,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
"The image id '[" + sourceImageId + "]' does not exist in region " + sourceRegion, 400);
|
||||
}
|
||||
|
||||
List<Tag> copiedTags = copyImageTags ? copiedImageTags(sourceRegion, source) : List.of();
|
||||
if (!copiedTags.isEmpty() && requestedImageTags != null && !requestedImageTags.isEmpty()) {
|
||||
Set<String> userKeys = userTagKeys(copiedTags);
|
||||
userKeys.addAll(userTagKeys(requestedImageTags));
|
||||
requireWithinTagLimit(userKeys);
|
||||
}
|
||||
// Fresh snapshot ids: two AMIs sharing one snapshot would make deleting either appear
|
||||
// to take the other's backing with it, and the copy's snapshots live in the
|
||||
// destination region anyway.
|
||||
Image copy = registerImage(destinationRegion, name, description, source.getArchitecture(),
|
||||
source.getRootDeviceName(), sourceImageMappings(source));
|
||||
if (!copiedTags.isEmpty()) {
|
||||
copy.setTags(new ArrayList<>(copiedTags));
|
||||
putTags(destinationRegion, copy.getImageId(), new ArrayList<>(copiedTags));
|
||||
}
|
||||
copy.setVirtualizationType(source.getVirtualizationType());
|
||||
copy.setRootDeviceType(source.getRootDeviceType());
|
||||
copy.setPlatform(source.getPlatform());
|
||||
@@ -9317,6 +9336,20 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The caller's tags on the source in its own region, as CreateTags and DeleteTags keep them,
|
||||
* less the system tags. Looked up by the source's own id: tags that CreateTags filed under a
|
||||
* catalog alias stay with the alias.
|
||||
*/
|
||||
private List<Tag> copiedImageTags(String sourceRegion, Image source) {
|
||||
List<Tag> sourceTags = tagsOf(sourceRegion, source.getImageId())
|
||||
.orElseGet(() -> source.getTags() == null ? List.of() : source.getTags());
|
||||
return sourceTags.stream()
|
||||
.filter(tag -> !isAwsTagKey(tag.getKey()))
|
||||
.map(tag -> new Tag(tag.getKey(), tag.getValue()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/** Rejects an AMI id that has been deregistered; unknown ids fall through to the resolver. */
|
||||
private void requireNotDeregistered(String region, String imageId) {
|
||||
Image image = registeredImages.get(key(region, imageId)).orElse(null);
|
||||
|
||||
Reference in new issue
Block a user