fix(ec2): apply TagSpecification on key pair, network ACL and image creates (#6551)

This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-26 06:53:52 -04:00
1 parent 935922933f
commit 0634184f5e
7 files changed
+1000 -20

No files matched your search

@@ -42,6 +42,12 @@ public class Ec2QueryHandler {
/** The gateway id a route table's built-in route carries (see Ec2Service#createRouteTable). */
private static final String LOCAL_GATEWAY_ID = "local";
/**
* The resource types CreateImage and CopyImage can tag: the AMI either action creates and
* the snapshots it takes. RegisterImage creates no snapshots, so it allows only the image.
*/
private static final Set<String> IMAGE_TAG_RESOURCE_TYPES = Set.of("image", "snapshot");
private final Ec2Service service;
private final EmulatorConfig config;
private final FlowLogService flowLogService;
@@ -555,16 +561,23 @@ public class Ec2QueryHandler {
/**
* Reads a request's tag specifications. Most EC2 actions carry them under the wire name
* {@code TagSpecification}, but a handful — {@code CreateTransitGatewayVpcAttachment} among
* them — declare no {@code locationName} and so serialize as {@code TagSpecifications}. Both
* {@code TagSpecification}, but a handful, {@code CreateTransitGatewayVpcAttachment} among
* them, declare no {@code locationName} and so serialize as {@code TagSpecifications}. Both
* spellings are read, because an action that used the plural silently lost every tag.
*
* <p>Every tag constraint CreateTags enforces that does not need the resource runs here,
* before the caller creates anything, so a rejected specification leaves nothing behind:
* reserved keys, key and value lengths and the per-resource limit. A caller whose reference
* allows only some resource types passes them as {@code allowedResourceTypes}, and a
* specification naming anything else is rejected; the other callers read only the
* specification that matches, as they always did.
*/
private List<Tag> parseTagsForResource(MultivaluedMap<String, String> p, String resourceType) {
return parseTagsForResource(p, resourceType, false);
return parseTagsForResource(p, resourceType, Set.of());
}
private List<Tag> parseTagsForResource(MultivaluedMap<String, String> p, String resourceType,
boolean requireMatchingResourceType) {
Set<String> allowedResourceTypes) {
List<Tag> tags = new ArrayList<>();
for (String prefix : new String[] {"TagSpecification", "TagSpecifications"}) {
for (int i = 1; ; i++) {
@@ -573,11 +586,11 @@ public class Ec2QueryHandler {
if (resType == null && p.keySet().stream().noneMatch(key -> key.startsWith(specificationPrefix))) {
break;
}
if (requireMatchingResourceType && !resourceType.equals(resType)) {
if (!allowedResourceTypes.isEmpty() && (resType == null || !allowedResourceTypes.contains(resType))) {
throw new AwsException("InvalidParameterValue",
"Tag specification resource type '" + (resType == null ? "" : resType)
+ "' is not valid for this operation. The valid resource type is '"
+ resourceType + "'.", 400);
+ "' is not valid for this operation. "
+ validResourceTypeMessage(allowedResourceTypes), 400);
}
if (resType == null) {
break;
@@ -599,9 +612,19 @@ public class Ec2QueryHandler {
};
throw new AwsException(code, "Tag keys starting with 'aws:' are reserved for internal use", 400);
}
Ec2Service.validateNewResourceTags(tags);
return tags;
}
private static String validResourceTypeMessage(Set<String> allowedResourceTypes) {
List<String> types = new ArrayList<>(allowedResourceTypes);
Collections.sort(types);
if (types.size() == 1) {
return "The valid resource type is '" + types.getFirst() + "'.";
}
return "The valid resource types are '" + String.join("' and '", types) + "'.";
}
/**
* Reads the {@code SubnetConfiguration.N} list shared by CreateVpcEndpoint and
* ModifyVpcEndpoint. The EC2 query protocol flattens the list under the member's
@@ -631,6 +654,19 @@ public class Ec2QueryHandler {
}
}
// "The same tag is applied to all of the snapshots that are created": every Ebs mapping of
// the image Floci just recorded carries one of those fresh snapshot ids.
private void applyImageSnapshotTags(List<Tag> snapshotTags, String region, Image image) {
if (snapshotTags.isEmpty()) {
return;
}
for (BlockDeviceMapping mapping : image.getBlockDeviceMappings()) {
if (mapping.getEbs() != null && mapping.getEbs().getSnapshotId() != null) {
service.createTags(region, List.of(mapping.getEbs().getSnapshotId()), snapshotTags);
}
}
}
// Each rule gets its own copy so mutating one rule's tag list can never leak into the
// others authorized in the same batch, and the copy also feeds the response XML.
private void applySecurityGroupRuleTags(List<Tag> ruleTags, String region, List<SecurityGroupRule> rules) {
@@ -3277,7 +3313,7 @@ public class Ec2QueryHandler {
String az = p.getFirst("AvailabilityZone");
String azId = p.getFirst("AvailabilityZoneId");
String ipv6CidrBlock = p.getFirst("Ipv6CidrBlock");
List<Tag> tags = parseTagsForResource(p, "subnet", true);
List<Tag> tags = parseTagsForResource(p, "subnet", Set.of("subnet"));
Subnet subnet = service.createSubnet(region, vpcId, cidrBlock, az, azId, ipv6CidrBlock);
try {
if (!tags.isEmpty()) {
@@ -3554,7 +3590,9 @@ public class Ec2QueryHandler {
// AWS answers DryRun before it checks the name or the key type.
checkDryRun(p);
String keyName = requireKeyName(p);
List<Tag> tags = parseTagsForResource(p, "key-pair");
KeyPair kp = service.createKeyPair(region, keyName, p.getFirst("KeyType"), p.getFirst("KeyFormat"));
applyResourceTags(tags, region, kp.getKeyPairId());
XmlBuilder xml = new XmlBuilder()
.start("CreateKeyPairResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -3562,6 +3600,7 @@ public class Ec2QueryHandler {
.elem("keyFingerprint", kp.getKeyFingerprint())
.elem("keyMaterial", kp.getKeyMaterial())
.elem("keyPairId", kp.getKeyPairId())
.raw(tagSetXml(tags))
.end("CreateKeyPairResponse");
return xmlResponse(xml.build());
}
@@ -3655,13 +3694,16 @@ public class Ec2QueryHandler {
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidKey.Format", "Key is not in valid OpenSSH public key format", 400);
}
List<Tag> tags = parseTagsForResource(p, "key-pair");
KeyPair kp = service.importKeyPair(region, keyName, publicKeyMaterial);
applyResourceTags(tags, region, kp.getKeyPairId());
XmlBuilder xml = new XmlBuilder()
.start("ImportKeyPairResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.elem("keyName", kp.getKeyName())
.elem("keyFingerprint", kp.getKeyFingerprint())
.elem("keyPairId", kp.getKeyPairId())
.raw(tagSetXml(tags))
.end("ImportKeyPairResponse");
return xmlResponse(xml.build());
}
@@ -3837,12 +3879,16 @@ public class Ec2QueryHandler {
}
private Response handleCreateImage(MultivaluedMap<String, String> p, String region) {
List<Tag> imageTags = parseTagsForResource(p, "image", IMAGE_TAG_RESOURCE_TYPES);
List<Tag> snapshotTags = parseTagsForResource(p, "snapshot", IMAGE_TAG_RESOURCE_TYPES);
Image image = service.createImage(
region,
p.getFirst("InstanceId"),
p.getFirst("Name"),
p.getFirst("Description"),
Boolean.parseBoolean(p.getFirst("NoReboot")));
applyResourceTags(imageTags, region, image.getImageId());
applyImageSnapshotTags(snapshotTags, region, image);
XmlBuilder xml = new XmlBuilder()
.start("CreateImageResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -3852,6 +3898,7 @@ public class Ec2QueryHandler {
}
private Response handleRegisterImage(MultivaluedMap<String, String> p, String region) {
List<Tag> imageTags = parseTagsForResource(p, "image", Set.of("image"));
Image image = service.registerImage(
region,
p.getFirst("Name"),
@@ -3859,6 +3906,7 @@ public class Ec2QueryHandler {
p.getFirst("Architecture"),
p.getFirst("RootDeviceName"),
parseBlockDeviceMappings(p));
applyResourceTags(imageTags, region, image.getImageId());
XmlBuilder xml = new XmlBuilder()
.start("RegisterImageResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -3904,12 +3952,16 @@ public class Ec2QueryHandler {
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CopyImage.html">CopyImage</a>
*/
private Response handleCopyImage(MultivaluedMap<String, String> p, String region) {
List<Tag> imageTags = parseTagsForResource(p, "image", IMAGE_TAG_RESOURCE_TYPES);
List<Tag> snapshotTags = parseTagsForResource(p, "snapshot", IMAGE_TAG_RESOURCE_TYPES);
Image image = service.copyImage(
region,
p.getFirst("SourceRegion"),
p.getFirst("SourceImageId"),
p.getFirst("Name"),
p.getFirst("Description"));
applyResourceTags(imageTags, region, image.getImageId());
applyImageSnapshotTags(snapshotTags, region, image);
XmlBuilder xml = new XmlBuilder()
.start("CopyImageResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -4270,7 +4322,9 @@ public class Ec2QueryHandler {
// ─── Network ACL handlers ─────────────────────────────────────────────────
private Response handleCreateNetworkAcl(MultivaluedMap<String, String> p, String region) {
List<Tag> tags = parseTagsForResource(p, "network-acl");
NetworkAcl acl = service.createNetworkAcl(region, p.getFirst("VpcId"));
applyResourceTags(tags, region, acl.getNetworkAclId());
XmlBuilder xml = new XmlBuilder()
.start("CreateNetworkAclResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
@@ -9819,6 +9819,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
/**
* Every tag constraint that does not need the resource: reserved keys, key and value lengths
* and the per-resource limit, the same rules CreateTags enforces on the list it writes.
* {@code Ec2QueryHandler.parseTagsForResource} runs this on the TagSpecification it parsed,
* before the handler creates anything, so a rejected specification leaves nothing behind;
* CreateTags checks the same rules again when it applies the list.
*/
public static void validateNewResourceTags(List<Tag> tagList) {
validateUserTagKeys(tagList);
validateTagLengths(tagList);
requireWithinTagLimit(userTagKeys(tagList));
}
/**
* CreateTags and DeleteTags name at least one resource, each a well-formed EC2 id. An id Floci
* seeds without hex digits (the default VPC's, a catalog AMI's) passes only when this region
@@ -9940,17 +9953,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
/** At most 50 user tags on a resource after the change; tags with the aws: prefix do not count. */
private void requireWithinTagLimit(String region, String resourceId, List<Tag> tagList) {
Set<String> userKeys = userTagKeys(tagsOf(region, resourceId).orElse(List.of()));
userKeys.addAll(userTagKeys(tagList));
requireWithinTagLimit(userKeys);
}
/** The distinct non-reserved keys of a tag list, what counts toward the per-resource limit. */
private static Set<String> userTagKeys(List<Tag> tagList) {
Set<String> userKeys = new HashSet<>();
for (Tag tag : tagsOf(region, resourceId).orElse(List.of())) {
if (!isAwsTagKey(tag.getKey())) {
userKeys.add(tag.getKey());
}
}
for (Tag tag : tagList) {
if (!isAwsTagKey(tag.getKey())) {
userKeys.add(tag.getKey());
}
}
return userKeys;
}
private static void requireWithinTagLimit(Set<String> userKeys) {
if (userKeys.size() > MAX_USER_TAGS_PER_RESOURCE) {
throw new AwsException("TagLimitExceeded",
"The maximum number of Tags for a resource has been reached.", 400);