fix(cloudformation): preserve list intrinsics and conditional tags (#2986)

This commit is contained in:
BingEdward authored and GitHub committed 2026-09-07 19:23:33 -05:00
1 parent b9039fef1d
commit 9f442543e3
14 files changed
+207 -54

No files matched your search

@@ -0,0 +1,108 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
class CloudFormationListIntrinsicsIntegrationTest {
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void conditionalTagsAndJoinedListsSurviveStackCreationAndUpdates() {
String name = "cfn-lists-" + Long.toString(System.nanoTime(), 36);
String template = """
{
"Parameters": {"UseTags": {"Type": "String"}},
"Conditions": {"Tagged": {"Fn::Equals": [{"Ref": "UseTags"}, "true"]}},
"Resources": {
"Stream": {
"Type": "AWS::Kinesis::Stream",
"Properties": {"Name": "%1$s", "ShardCount": 1,
"Tags": {"Fn::If": ["Tagged", [{"Key": "region", "Value": {"Ref": "AWS::Region"}}], []]}}
},
"Table": {
"Type": "AWS::DynamoDB::Table",
"Properties": {"TableName": "%1$s", "BillingMode": "PAY_PER_REQUEST",
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"Tags": {"Fn::If": ["Tagged", [{"Key": "region", "Value": {"Ref": "AWS::Region"}}], []]}}
},
"Group": {
"Type": "AWS::Scheduler::ScheduleGroup",
"Properties": {"Name": "%1$s",
"Tags": {"Fn::If": ["Tagged", [{"Key": "region", "Value": {"Ref": "AWS::Region"}}], []]}}
}
},
"Outputs": {
"JoinedIf": {"Value": {"Fn::Join": [",", {"Fn::If": ["Tagged", ["a", "b"], ["c"]]}]}},
"JoinedSplit": {"Value": {"Fn::Join": ["|", {"Fn::Split": [",", "x,,y,"]}]}}
}
}
""".formatted(name);
try {
for (boolean tagged : List.of(true, false)) {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", tagged ? "CreateStack" : "UpdateStack")
.formParam("StackName", name)
.formParam("TemplateBody", template)
.formParam("Parameters.member.1.ParameterKey", "UseTags")
.formParam("Parameters.member.1.ParameterValue", Boolean.toString(tagged))
.when().post("/")
.then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", name)
.when().post("/")
.then().statusCode(200)
.body(containsString(tagged ? "CREATE_COMPLETE" : "UPDATE_COMPLETE"))
.body(containsString(tagged ? "<OutputValue>a,b</OutputValue>" : "<OutputValue>c</OutputValue>"))
.body(containsString("<OutputValue>x||y|</OutputValue>"));
List<Map<String, String>> expected = tagged
? List.of(Map.of("Key", "region", "Value", "us-east-1")) : List.of();
given()
.contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "Kinesis_20131202.ListTagsForStream")
.body(Map.of("StreamName", name))
.when().post("/")
.then().statusCode(200).body("Tags", equalTo(expected));
given()
.contentType("application/x-amz-json-1.0")
.header("X-Amz-Target", "DynamoDB_20120810.ListTagsOfResource")
.body(Map.of("ResourceArn", "arn:aws:dynamodb:us-east-1:000000000000:table/" + name))
.when().post("/")
.then().statusCode(200).body("Tags", equalTo(expected));
given()
.queryParam("resourceArn", "arn:aws:scheduler:us-east-1:000000000000:schedule-group/" + name)
.when().get("/tags")
.then().statusCode(200).body("Tags", equalTo(expected));
}
} finally {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteStack")
.formParam("StackName", name)
.when().post("/")
.then().statusCode(200);
}
}
}
@@ -3,6 +3,9 @@ package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
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 java.util.List;
import java.util.Map;
@@ -30,6 +33,49 @@ class CloudFormationTemplateEngineTest {
}
}
@Test
void joinAcceptsSplitAsItsListOfValues() {
assertEquals("x|y|z", engine().resolve(json("""
{"Fn::Join": ["|", {"Fn::Split": [",", "x,y,z"]}]}
""")));
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void resolveNodePreservesConditionalTagLists(boolean useTags) {
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(), Map.of(),
Map.of("UseTags", useTags), Map.of(), mapper, name -> null);
JsonNode tags = json("""
{"Fn::If": ["UseTags", [{"Key": "region", "Value": {"Ref": "AWS::Region"}}], []]}
""");
assertEquals(json(useTags ? "[{\"Key\":\"region\",\"Value\":\"us-east-1\"}]" : "[]"),
e.resolveNode(tags));
}
@ParameterizedTest
@CsvSource(textBlock = """
'{"Fn::Split": [".", "a..b."]}', '["a", "", "b", ""]'
'{"Fn::GetAZs": ""}', '["us-east-1a", "us-east-1b", "us-east-1c"]'
'{"Fn::Cidr": ["10.0.0.0/16", 2, 8]}', '["10.0.0.0/24", "10.0.1.0/24"]'
""")
void resolveNodePreservesListValuedIntrinsics(String expression, String expected) {
assertEquals(json(expected), engine().resolveNode(json(expression)));
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void joinAcceptsConditionalLists(boolean useFirst) {
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(), Map.of(),
Map.of("UseFirst", useFirst), Map.of(), mapper, name -> null);
assertEquals(useFirst ? "a,b" : "x,,y,", e.resolve(json("""
{"Fn::Join": [",", {"Fn::If": ["UseFirst", ["a", "b"],
{"Fn::Split": ["|", "x||y|"]}]}]}
""")));
}
@Test
void getAzsReturnsStackRegionZones() {
assertEquals("us-east-1a,us-east-1b,us-east-1c",
@@ -46,6 +46,7 @@ class SchedulerScheduleGroupCfnProvisionerTest {
/** An update-path context carries the physical id the previous provision assigned. */
private ProvisionContext ctx(String priorPhysicalId) {
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
when(engine.resolve(any())).thenAnswer(inv -> {
JsonNode node = inv.getArgument(0);
return node == null ? null : node.asText();
@@ -205,6 +206,7 @@ class SchedulerScheduleGroupCfnProvisionerTest {
props.set("Tags", mapper.createArrayNode().add(tag));
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
when(engine.resolve(any())).thenAnswer(inv -> {
JsonNode node = inv.getArgument(0);
return node == null ? null : node.asText();
@@ -255,19 +257,12 @@ class SchedulerScheduleGroupCfnProvisionerTest {
@Test
void sameStackRetryWithUnresolvableTagsDoesNotUntagExistingTags() {
// Greptile review on PR #2796: the engine's Fn::If support is scalar-only, so a Tags
// property wrapped in Fn::If (choosing between two tag lists) does not resolve to an array
// here - props.get("Tags").isArray() is false, same as no Tags at all. Before this test, that
// made the retry path's tag-diff see an empty desired set and untag every live key, actively
// destroying tags a resolvable Tags property never asked to remove.
// A malformed Tags value must not be mistaken for an empty desired tag set.
when(schedulerService.getScheduleGroup("my-group", "us-east-1"))
.thenReturn(group("my-group", Map.of("Old", "value")));
StackResource r = resource("MyGroup");
ObjectNode props = mapper.createObjectNode().put("Name", "my-group");
ObjectNode condition = mapper.createObjectNode();
condition.set("Fn::If", mapper.createArrayNode().add("UseProdTags")
.add(mapper.createArrayNode()).add(mapper.createArrayNode()));
props.set("Tags", condition);
props.put("Tags", "not-a-list");
provisioner.provision(r, props, ctx("my-group"));