fix(ec2): validate subnet tags before creation (#5306)

* fix(ec2): validate subnet tags before creation

Reject invalid subnet tag specifications before creating a subnet, so
failed requests do not leave resources behind. Include the enclosing
specification in nested tag errors.

* fix(ec2): preserve subnet tag parsing and clean up failures

---------

Co-authored-by: Jason van Zyl <jason@vanzyl.ca>
This commit is contained in:
David PhillipsandJason van Zyl authored and GitHub committed 2026-10-08 15:39:31 -04:00
1 parent 8b1ece9c24
commit aa94969704
2 files changed
+129 -11

No files matched your search

@@ -535,11 +535,28 @@ public class Ec2QueryHandler {
* spellings are read, because an action that used the plural silently lost every tag.
*/
private List<Tag> parseTagsForResource(MultivaluedMap<String, String> p, String resourceType) {
return parseTagsForResource(p, resourceType, false);
}
private List<Tag> parseTagsForResource(MultivaluedMap<String, String> p, String resourceType,
boolean requireMatchingResourceType) {
List<Tag> tags = new ArrayList<>();
for (String prefix : new String[] {"TagSpecification", "TagSpecifications"}) {
for (int i = 1; ; i++) {
String resType = p.getFirst(prefix + "." + i + ".ResourceType");
if (resType == null) break;
String specificationPrefix = prefix + "." + i + ".";
String resType = p.getFirst(specificationPrefix + "ResourceType");
if (resType == null && p.keySet().stream().noneMatch(key -> key.startsWith(specificationPrefix))) {
break;
}
if (requireMatchingResourceType && !resourceType.equals(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);
}
if (resType == null) {
break;
}
if (resourceType.equals(resType)) {
for (int j = 1; ; j++) {
String key = p.getFirst(prefix + "." + i + ".Tag." + j + ".Key");
@@ -2974,8 +2991,21 @@ 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);
Subnet subnet = service.createSubnet(region, vpcId, cidrBlock, az, azId, ipv6CidrBlock);
applyResourceTags(p, region, "subnet", subnet.getSubnetId());
try {
if (!tags.isEmpty()) {
service.createTags(region, List.of(subnet.getSubnetId()), tags);
}
} catch (RuntimeException failure) {
try {
service.deleteSubnet(region, subnet.getSubnetId());
} catch (RuntimeException cleanupFailure) {
failure.addSuppressed(cleanupFailure);
LOG.warnv(cleanupFailure, "Failed to delete subnet {0} after tagging failed", subnet.getSubnetId());
}
throw failure;
}
XmlBuilder xml = new XmlBuilder()
.start("CreateSubnetResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())