mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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