mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(autoscaling): reject malformed tag propagation flags (#8079)
This commit is contained in:
1 parent
589e1e58b4
commit
f8e5a883d8
6 files changed
+299
-30
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class AwsQueryBooleansTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"true,true", "1,true", "false,false", "0,false"})
|
||||
void acceptsXsdBooleanLexicalValues(String value, boolean expected) {
|
||||
assertEquals(expected, AwsQueryBooleans.parse(value));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"TRUE", "False", " true", "false ", " ", "yes", "2", "invalid\u0000flag"})
|
||||
void rejectsOtherValuesWithoutEchoingInput(String value) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> AwsQueryBooleans.parse(value));
|
||||
assertEquals("MalformedInput", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals("boolean must follow xsd1.1 definition", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyValueRetainsMissingValueError() {
|
||||
AwsException error = assertThrows(AwsException.class, () -> AwsQueryBooleans.parse(""));
|
||||
assertEquals("MalformedInput", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals("missing value for boolean type", error.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,16 @@ import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.floci.aws.services.autoscaling.AutoScalingXmlAssertions.XML_NAMESPACE;
|
||||
import static io.floci.aws.services.autoscaling.AutoScalingXmlAssertions.assertModeledNamespace;
|
||||
@@ -28,6 +31,95 @@ class AutoScalingQueryHandlerTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("malformedTagPropagationValues")
|
||||
void malformedTagPropagationIsRejectedBeforeServiceCalls(String action, String flag) {
|
||||
AutoScalingService service = mock(AutoScalingService.class);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "missing");
|
||||
params.add("Tags.member.1.Key", "valid");
|
||||
params.add("Tags.member.1.ResourceId", "missing");
|
||||
params.add("Tags.member.1.PropagateAtLaunch", "true");
|
||||
params.add("Tags.member.2.Key", "invalid");
|
||||
params.add("Tags.member.2.ResourceId", "missing");
|
||||
params.add("Tags.member.2.PropagateAtLaunch", flag);
|
||||
|
||||
Response response = new AutoScalingQueryHandler(service).handle(action, params, REGION);
|
||||
|
||||
assertEquals(400, response.getStatus(), action);
|
||||
assertTrue(response.getEntity().toString().contains("<Code>MalformedInput</Code>"), action);
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateAutoScalingGroup", "CreateOrUpdateTags", "DeleteTags"})
|
||||
void sparseMalformedTagPropagationIsRejectedBeforeServiceCalls(String action) {
|
||||
AutoScalingService service = mock(AutoScalingService.class);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "missing");
|
||||
params.add("Tags.member.3.Key", "invalid");
|
||||
params.add("Tags.member.3.PropagateAtLaunch", "yes");
|
||||
|
||||
Response response = new AutoScalingQueryHandler(service).handle(action, params, REGION);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(response.getEntity().toString().contains("<Code>MalformedInput</Code>"));
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateAutoScalingGroup", "CreateOrUpdateTags", "DeleteTags"})
|
||||
void controlCharacterFlagReturnsParseableXml(String action) throws Exception {
|
||||
AutoScalingService service = mock(AutoScalingService.class);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "missing");
|
||||
params.add("Tags.member.1.Key", "invalid");
|
||||
params.add("Tags.member.1.PropagateAtLaunch", "invalid" + (char) 0 + (char) 1);
|
||||
|
||||
Response response = new AutoScalingQueryHandler(service).handle(action, params, REGION);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
Element root = assertModeledNamespace(response.getEntity().toString());
|
||||
assertEquals("MalformedInput", root.getElementsByTagNameNS(XML_NAMESPACE, "Code").item(0).getTextContent());
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sparseRequiredFlagErrorUsesOriginalMemberIndex() {
|
||||
AutoScalingService service = mock(AutoScalingService.class);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("Tags.member.7.Key", "missing-flag");
|
||||
|
||||
Response response = new AutoScalingQueryHandler(service).handle("CreateOrUpdateTags", params, REGION);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(response.getEntity().toString().contains("Tags.member.7.PropagateAtLaunch is required"));
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"CreateAutoScalingGroup", "CreateOrUpdateTags", "DeleteTags"})
|
||||
void memberWithoutKeyIsRejectedBeforeMutation(String action) {
|
||||
AutoScalingService service = mock(AutoScalingService.class);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "missing");
|
||||
params.add("Tags.member.1.Key", "valid");
|
||||
params.add("Tags.member.1.PropagateAtLaunch", "true");
|
||||
params.add("Tags.member.3.PropagateAtLaunch", "false");
|
||||
|
||||
Response response = new AutoScalingQueryHandler(service).handle(action, params, REGION);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(response.getEntity().toString().contains("Tags.member.3.Key is required"));
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> malformedTagPropagationValues() {
|
||||
return Stream.of("CreateAutoScalingGroup", "CreateOrUpdateTags", "DeleteTags")
|
||||
.flatMap(action -> Stream.of("", " ", "yes", "TRUE", "true ", " false")
|
||||
.map(flag -> Arguments.of(action, flag)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void repeatedDetachMembersCountEachDetachedInstanceOnce(boolean decrementDesiredCapacity) {
|
||||
|
||||
+128
@@ -4,6 +4,7 @@ import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -29,6 +30,7 @@ import java.util.function.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.floci.aws.testing.QueryRequests.request;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -98,6 +100,132 @@ class AutoScalingTagPropagationIntegrationTest {
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", " ", "yes", "TRUE", "true ", " false"})
|
||||
void malformedPropagationLeavesExistingTagsUnchanged(String flag) {
|
||||
withAutoScalingGroup(List.of(Tag.builder().key("existing").value("before")
|
||||
.propagateAtLaunch(true).build()), (client, name) -> {
|
||||
Map<String, TagDescription> before = describeGroupTags(client, name);
|
||||
for (String action : List.of("CreateOrUpdateTags", "DeleteTags")) {
|
||||
request(ACCOUNT, "autoscaling", action)
|
||||
.formParam("Tags.member.1.ResourceId", name)
|
||||
.formParam("Tags.member.1.ResourceType", "auto-scaling-group")
|
||||
.formParam("Tags.member.1.Key", "existing")
|
||||
.formParam("Tags.member.1.Value", "changed")
|
||||
.formParam("Tags.member.1.PropagateAtLaunch", "false")
|
||||
.formParam("Tags.member.2.ResourceId", "missing-group")
|
||||
.formParam("Tags.member.2.ResourceType", "auto-scaling-group")
|
||||
.formParam("Tags.member.2.Key", "invalid")
|
||||
.formParam("Tags.member.2.PropagateAtLaunch", flag)
|
||||
.post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("MalformedInput"));
|
||||
assertEquals(before, describeGroupTags(client, name));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", " ", "yes", "TRUE", "true ", " false"})
|
||||
void malformedPropagationCannotCreateGroup(String flag) {
|
||||
String name = "tag-malformed-" + UUID.randomUUID();
|
||||
try (AutoScalingClient client = AwsTestClients.create(AutoScalingClient.builder(), ACCOUNT, Region.of(REGION))) {
|
||||
try {
|
||||
Response response = request(ACCOUNT, "autoscaling", "CreateAutoScalingGroup")
|
||||
.formParam("AutoScalingGroupName", name)
|
||||
.formParam("LaunchConfigurationName", "missing-configuration")
|
||||
.formParam("MinSize", 0).formParam("MaxSize", 1).formParam("DesiredCapacity", 0)
|
||||
.formParam("AvailabilityZones.member.1", REGION + "a")
|
||||
.formParam("Tags.member.1.Key", "invalid")
|
||||
.formParam("Tags.member.1.PropagateAtLaunch", flag)
|
||||
.post("/");
|
||||
assertTrue(client.describeAutoScalingGroups(r -> r.autoScalingGroupNames(name))
|
||||
.autoScalingGroups().isEmpty());
|
||||
response.then().statusCode(400).body("ErrorResponse.Error.Code", equalTo("MalformedInput"));
|
||||
} finally {
|
||||
if (!client.describeAutoScalingGroups(r -> r.autoScalingGroupNames(name)).autoScalingGroups().isEmpty()) {
|
||||
client.deleteAutoScalingGroup(r -> r.autoScalingGroupName(name).forceDelete(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"yes", "invalid\u0000flag"})
|
||||
void sparseInvalidFlagLeavesExistingTagsUnchanged(String flag) {
|
||||
withAutoScalingGroup(List.of(Tag.builder().key("existing").value("before")
|
||||
.propagateAtLaunch(true).build()), (client, name) -> {
|
||||
Map<String, TagDescription> before = describeGroupTags(client, name);
|
||||
for (String action : List.of("CreateOrUpdateTags", "DeleteTags")) {
|
||||
request(ACCOUNT, "autoscaling", action)
|
||||
.formParam("Tags.member.1.ResourceId", name)
|
||||
.formParam("Tags.member.1.ResourceType", "auto-scaling-group")
|
||||
.formParam("Tags.member.1.Key", "existing")
|
||||
.formParam("Tags.member.1.Value", "changed")
|
||||
.formParam("Tags.member.1.PropagateAtLaunch", "false")
|
||||
.formParam("Tags.member.3.ResourceId", name)
|
||||
.formParam("Tags.member.3.ResourceType", "auto-scaling-group")
|
||||
.formParam("Tags.member.3.Key", "later")
|
||||
.formParam("Tags.member.3.PropagateAtLaunch", flag)
|
||||
.post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("MalformedInput"));
|
||||
assertEquals(before, describeGroupTags(client, name));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"1,true", "0,false"})
|
||||
void numericPropagationFlagsAreAccepted(String flag, boolean expected) {
|
||||
withAutoScalingGroup(List.of(Tag.builder().key("existing").value("before")
|
||||
.propagateAtLaunch(!expected).build()), (client, name) -> {
|
||||
request(ACCOUNT, "autoscaling", "CreateOrUpdateTags")
|
||||
.formParam("Tags.member.1.ResourceId", name)
|
||||
.formParam("Tags.member.1.ResourceType", "auto-scaling-group")
|
||||
.formParam("Tags.member.1.Key", "existing")
|
||||
.formParam("Tags.member.1.PropagateAtLaunch", flag)
|
||||
.post("/").then().statusCode(200);
|
||||
assertEquals(expected, describeGroupTags(client, name).get("existing").propagateAtLaunch());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void sparseTagsRetainCreateDefaultsUpdateAndDeleteSemantics() {
|
||||
String name = "sparse-tags-" + UUID.randomUUID();
|
||||
try (AutoScalingClient client = AwsTestClients.create(AutoScalingClient.builder(), ACCOUNT, Region.of(REGION))) {
|
||||
try {
|
||||
request(ACCOUNT, "autoscaling", "CreateAutoScalingGroup")
|
||||
.formParam("AutoScalingGroupName", name)
|
||||
.formParam("LaunchConfigurationName", "sparse-configuration")
|
||||
.formParam("MinSize", 0).formParam("MaxSize", 1).formParam("DesiredCapacity", 0)
|
||||
.formParam("AvailabilityZones.member.1", REGION + "a")
|
||||
.formParam("Tags.member.3.Key", "existing")
|
||||
.formParam("Tags.member.3.Value", "before")
|
||||
.post("/").then().statusCode(200);
|
||||
assertEquals("before", describeGroupTags(client, name).get("existing").value());
|
||||
assertTrue(describeGroupTags(client, name).get("existing").propagateAtLaunch());
|
||||
request(ACCOUNT, "autoscaling", "CreateOrUpdateTags")
|
||||
.formParam("Tags.member.7.ResourceId", name)
|
||||
.formParam("Tags.member.7.ResourceType", "auto-scaling-group")
|
||||
.formParam("Tags.member.7.Key", "existing")
|
||||
.formParam("Tags.member.7.Value", "after")
|
||||
.formParam("Tags.member.7.PropagateAtLaunch", "false")
|
||||
.post("/").then().statusCode(200);
|
||||
assertEquals("after", describeGroupTags(client, name).get("existing").value());
|
||||
assertEquals(false, describeGroupTags(client, name).get("existing").propagateAtLaunch());
|
||||
request(ACCOUNT, "autoscaling", "DeleteTags")
|
||||
.formParam("Tags.member.9.ResourceId", name)
|
||||
.formParam("Tags.member.9.ResourceType", "auto-scaling-group")
|
||||
.formParam("Tags.member.9.Key", "existing")
|
||||
.post("/").then().statusCode(200);
|
||||
assertTrue(describeGroupTags(client, name).isEmpty());
|
||||
} finally {
|
||||
if (!client.describeAutoScalingGroups(r -> r.autoScalingGroupNames(name)).autoScalingGroups().isEmpty()) {
|
||||
client.deleteAutoScalingGroup(r -> r.autoScalingGroupName(name).forceDelete(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTagsAllowsOmittedPropagationFlag() {
|
||||
List<Tag> tags = List.of(
|
||||
|
||||
Reference in new issue
Block a user