diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index 60765f7c88..d5b26bf921 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -535,11 +535,28 @@ public class Ec2QueryHandler { * spellings are read, because an action that used the plural silently lost every tag. */ private List parseTagsForResource(MultivaluedMap p, String resourceType) { + return parseTagsForResource(p, resourceType, false); + } + + private List parseTagsForResource(MultivaluedMap p, String resourceType, + boolean requireMatchingResourceType) { List 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 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()) diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java index 56720db4c2..42958a78a9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java @@ -2,9 +2,15 @@ package io.github.hectorvent.floci.services.ec2; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import io.github.hectorvent.floci.config.EmulatorConfig; @@ -15,26 +21,29 @@ import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import java.util.List; class Ec2QueryHandlerTest { - @Test - void normalizesValuelessCreateSubnetTagsBeforeMutation() { + @ParameterizedTest + @ValueSource(strings = {"TagSpecification", "TagSpecifications"}) + void normalizesValuelessCreateSubnetTagsBeforeMutation(String prefix) { Ec2Service service = mock(Ec2Service.class); Subnet subnet = new Subnet(); subnet.setSubnetId("subnet-test"); when(service.createSubnet("us-east-1", "vpc-test", "10.38.1.0/24", null, null, null)) .thenReturn(subnet); MultivaluedMap params = createSubnetParams("10.38.1.0/24"); - params.putSingle("TagSpecification.1.ResourceType", "subnet"); - params.putSingle("TagSpecification.1.Tag.1.Key", "omitted-value"); - params.putSingle("TagSpecification.1.Tag.2.Key", "explicit-empty-value"); - params.putSingle("TagSpecification.1.Tag.2.Value", ""); - params.putSingle("TagSpecification.1.Tag.3.Key", "ordinary-value"); - params.putSingle("TagSpecification.1.Tag.3.Value", "present"); + params.putSingle(prefix + ".1.ResourceType", "subnet"); + params.putSingle(prefix + ".1.Tag.1.Key", "omitted-value"); + params.putSingle(prefix + ".1.Tag.2.Key", "explicit-empty-value"); + params.putSingle(prefix + ".1.Tag.2.Value", ""); + params.putSingle(prefix + ".1.Tag.3.Key", "ordinary-value"); + params.putSingle(prefix + ".1.Tag.3.Value", "present"); Response response = handler(service).handle("CreateSubnet", params, "us-east-1"); @@ -103,6 +112,78 @@ class Ec2QueryHandlerTest { assertEquals(List.of(""), tags.getValue().stream().map(Tag::getValue).toList()); } + @ParameterizedTest + @ValueSource(strings = {"TagSpecification", "TagSpecifications"}) + void validatesEveryCreateSubnetTagSpecificationBeforeMutation(String prefix) { + Ec2Service service = mock(Ec2Service.class); + MultivaluedMap params = createSubnetParams("10.39.1.0/24"); + params.putSingle(prefix + ".1.ResourceType", "subnet"); + params.putSingle(prefix + ".1.Tag.1.Key", "Name"); + params.putSingle(prefix + ".1.Tag.1.Value", "valid"); + params.putSingle(prefix + ".2.ResourceType", "vpc"); + params.putSingle(prefix + ".2.Tag.1.Key", "Name"); + + Response response = handler(service).handle("CreateSubnet", params, "us-east-1"); + + assertInvalidParameterValue(response, "resource type 'vpc'"); + verifyNoInteractions(service); + } + + @ParameterizedTest + @ValueSource(strings = {"TagSpecification", "TagSpecifications"}) + void rejectsCreateSubnetTagSpecificationWithoutResourceTypeBeforeMutation(String prefix) { + Ec2Service service = mock(Ec2Service.class); + MultivaluedMap params = createSubnetParams("10.40.1.0/24"); + params.putSingle(prefix + ".1.Tag.1.Key", "Name"); + params.putSingle(prefix + ".1.Tag.1.Value", "invalid"); + + Response response = handler(service).handle("CreateSubnet", params, "us-east-1"); + + assertInvalidParameterValue(response, "resource type ''"); + verifyNoInteractions(service); + } + + @Test + void validatesPluralSpecificationsEvenWhenSingularTagsAreValid() { + Ec2Service service = mock(Ec2Service.class); + MultivaluedMap params = createSubnetParams("10.41.1.0/24"); + params.putSingle("TagSpecification.1.ResourceType", "subnet"); + params.putSingle("TagSpecification.1.Tag.1.Key", "Name"); + params.putSingle("TagSpecifications.1.ResourceType", "vpc"); + + Response response = handler(service).handle("CreateSubnet", params, "us-east-1"); + + assertInvalidParameterValue(response, "resource type 'vpc'"); + verifyNoInteractions(service); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void deletesSubnetWhenTaggingFailsAndPreservesOriginalFailure(boolean cleanupFails) { + Ec2Service service = mock(Ec2Service.class); + Subnet subnet = new Subnet(); + subnet.setSubnetId("subnet-test"); + when(service.createSubnet("us-east-1", "vpc-test", "10.42.1.0/24", null, null, null)) + .thenReturn(subnet); + IllegalStateException taggingFailure = new IllegalStateException("tag storage failed"); + doThrow(taggingFailure).when(service).createTags(eq("us-east-1"), eq(List.of("subnet-test")), + anyList()); + IllegalStateException cleanupFailure = new IllegalStateException("subnet storage failed"); + if (cleanupFails) { + doThrow(cleanupFailure).when(service).deleteSubnet("us-east-1", "subnet-test"); + } + MultivaluedMap params = createSubnetParams("10.42.1.0/24"); + params.putSingle("TagSpecification.1.ResourceType", "subnet"); + params.putSingle("TagSpecification.1.Tag.1.Key", "Name"); + + IllegalStateException thrown = assertThrows(IllegalStateException.class, + () -> handler(service).handle("CreateSubnet", params, "us-east-1")); + + assertSame(taggingFailure, thrown); + verify(service).deleteSubnet("us-east-1", "subnet-test"); + assertEquals(cleanupFails ? List.of(cleanupFailure) : List.of(), List.of(thrown.getSuppressed())); + } + private Ec2QueryHandler handler(Ec2Service service) { return new Ec2QueryHandler( service, mock(EmulatorConfig.class), mock(FlowLogService.class), @@ -116,4 +197,11 @@ class Ec2QueryHandlerTest { params.putSingle("CidrBlock", cidrBlock); return params; } + + private void assertInvalidParameterValue(Response response, String messageFragment) { + assertEquals(400, response.getStatus()); + String body = (String) response.getEntity(); + assertTrue(body.contains("InvalidParameterValue")); + assertTrue(body.contains(messageFragment), body); + } }