mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
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())
|
||||
|
||||
@@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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("<Code>InvalidParameterValue</Code>"));
|
||||
assertTrue(body.contains(messageFragment), body);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user