mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): refuse a list property that a parameter or an intrinsic makes a string (#5949)
* fix(cloudformation): refuse a list property that a parameter or an intrinsic makes a string The follow-up asked on #5923. The engine kept parameter values but not their types, so the Lambda provisioner's list readers could not tell a Ref to a CommaDelimitedList parameter, which the engine resolves to the comma-joined text, from a Ref to a String parameter: both read as lists, where CloudFormation refuses the second. Measured on CloudFormation (eu-west-1, 2026-10-18), Layers and Architectures of AWS::Lambda::Function: - a Ref to a CommaDelimitedList, AWS::SSM::Parameter::Value<List<String>> or AWS::SSM::Parameter::Value<CommaDelimitedList> parameter, Fn::Split, an Fn::If choosing a Ref to a list, and Ref AWS::NotificationARNs are lists; - a Ref to a String or Number parameter, Ref AWS::Region, Fn::Select, Fn::Join, Fn::Sub and an Fn::If choosing a literal string fail the template's validation with "Property [Architectures] expected type: JSONArray, found: String"; - a resource's Ref and Fn::GetAtt fail only when the resource is provisioned, with "Properties validation failed for resource Fn with message: #/Architectures: expected type: JSONArray, found: String". CloudFormationTemplateEngine now takes the declared parameter types, which executeTemplate reads from the template (stacks, change sets, stack sets and nested stacks all deploy through it), and yieldsString tells the forms of the second group. The list readers refuse such an intrinsic with the validation message, as they refuse a literal of the wrong type. The third group is left as it was: the engine cannot tell a list-valued attribute from a string one before provisioning, and a resource's own message format is not modelled. parameterType gives a declaration's Type, String when it is left out, for the new reader and for GetTemplateSummary, which had its own copy. Tests: CloudFormationTemplateEngineTest for yieldsString over every measured form; CloudFormationLambdaObjectPropertyIntegrationTest for each refused form, an Fn::If choosing a list, and a nested stack whose own parameter types decide; CloudFormationLambdaObjectPropertyTest in sdk-test-java for a String parameter through the SDK. The integration and SDK tests fail on main. * fix(cloudformation): take Fn::Base64 as a string where a list is expected Review of #5949 (Qodo). Measured on CloudFormation (eu-west-1, 2026-10-18): Architectures: {"Fn::Base64": "x86_64"} fails validation with "Property [Architectures] expected type: JSONArray, found: String", like Fn::Sub. yieldsString left it out, so the Lambda reader split the encoded text into a list and the function got an invalid architecture instead. Tests: CloudFormationTemplateEngineTest and CloudFormationLambdaObjectPropertyIntegrationTest gain the case; both fail on the previous head.
This commit is contained in:
1 parent
7ce4b6bd23
commit
1f4a46804c
7 files changed
+245
-7
No files matched your search
+91
-1
@@ -11,6 +11,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
@@ -35,6 +36,15 @@ class CloudFormationLambdaObjectPropertyIntegrationTest {
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
private static final String LAMBDA = "/2015-03-31/functions/";
|
||||
/** A String, a Number and a CommaDelimitedList parameter, and a condition that holds. */
|
||||
private static final String LIST_SECTIONS = """
|
||||
"Parameters": {
|
||||
"Str": {"Type": "String", "Default": "x86_64"},
|
||||
"Num": {"Type": "Number", "Default": "1"},
|
||||
"Csv": {"Type": "CommaDelimitedList", "Default": "x86_64"}
|
||||
},
|
||||
"Conditions": {"Yes": {"Fn::Equals": ["a", "a"]}},
|
||||
""";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
@@ -239,6 +249,80 @@ class CloudFormationLambdaObjectPropertyIntegrationTest {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured on CloudFormation (eu-west-1, 2026-10-18): where a list is expected, a Ref to a String
|
||||
* or Number parameter, Ref AWS::Region, Fn::Select, Fn::Join, Fn::Sub, Fn::Base64 and an Fn::If
|
||||
* choosing a literal string fail validation with "found: String", while an Fn::If choosing a Ref to a
|
||||
* CommaDelimitedList is a list. A nested stack's own parameter types decide for its resources.
|
||||
*/
|
||||
@Test
|
||||
void aListPropertyThatYieldsAStringFailsTheCreate() {
|
||||
for (String architectures : List.of("{\"Ref\": \"Str\"}", "{\"Ref\": \"Num\"}", "{\"Ref\": \"AWS::Region\"}",
|
||||
"{\"Fn::Select\": [0, {\"Ref\": \"Csv\"}]}", "{\"Fn::Join\": [\",\", {\"Ref\": \"Csv\"}]}",
|
||||
"{\"Fn::Sub\": \"x86_64\"}", "{\"Fn::Base64\": \"x86_64\"}",
|
||||
"{\"Fn::If\": [\"Yes\", \"x86_64\", {\"Ref\": \"AWS::NoValue\"}]}")) {
|
||||
String stack = stackName("yields-string");
|
||||
submit("CreateStack", stack, template(LIST_SECTIONS, ",\"Architectures\": " + architectures));
|
||||
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status(), architectures);
|
||||
assertEventReason(stack, "Property [Architectures] expected type: JSONArray, found: String");
|
||||
}
|
||||
|
||||
String listChosen = stackName("if-list");
|
||||
submit("CreateStack", listChosen, template(LIST_SECTIONS,
|
||||
",\"Architectures\": {\"Fn::If\": [\"Yes\", {\"Ref\": \"Csv\"}, [\"arm64\"]]}"));
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(listChosen, CFN_AUTH).status());
|
||||
given().when().get(LAMBDA + functionName(listChosen) + "/configuration")
|
||||
.then().statusCode(200)
|
||||
.body("Architectures", contains("x86_64"));
|
||||
|
||||
String bucket = "cfn-lambda-nested-" + Long.toString(System.nanoTime(), 36);
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
cleanup.register(() -> {
|
||||
given().when().delete("/" + bucket + "/string.json");
|
||||
given().when().delete("/" + bucket + "/list.json");
|
||||
given().when().delete("/" + bucket);
|
||||
});
|
||||
for (String type : List.of("String", "CommaDelimitedList")) {
|
||||
String child = """
|
||||
{"Parameters": {"Archs": {"Type": "%s"}},
|
||||
"Resources": {"Fn": {"Type": "AWS::Lambda::Function", "Properties": {
|
||||
"Runtime": "python3.14", "Handler": "index.handler",
|
||||
"Role": "arn:aws:iam::000000000000:role/lambda-role",
|
||||
"Code": {"ZipFile": "def handler(e, c): return 'ok'"},
|
||||
"Architectures": {"Ref": "Archs"}}}},
|
||||
"Outputs": {"Name": {"Value": {"Ref": "Fn"}}}}
|
||||
""".formatted(type);
|
||||
String key = "String".equals(type) ? "string.json" : "list.json";
|
||||
given().contentType("application/json").body(child)
|
||||
.when().put("/" + bucket + "/" + key).then().statusCode(200);
|
||||
String parent = stackName("nested-" + key.replace(".json", ""));
|
||||
submit("CreateStack", parent, """
|
||||
{"Resources": {"Child": {"Type": "AWS::CloudFormation::Stack", "Properties": {
|
||||
"TemplateURL": "http://localhost/%s/%s", "Parameters": {"Archs": "x86_64"}}}}}
|
||||
""".formatted(bucket, key));
|
||||
assertEquals("String".equals(type) ? "ROLLBACK_COMPLETE" : "CREATE_COMPLETE",
|
||||
CfnStackWaits.awaitTerminal(parent, CFN_AUTH).status(), type);
|
||||
String childStack = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackResource")
|
||||
.formParam("StackName", parent)
|
||||
.formParam("LogicalResourceId", "Child")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }");
|
||||
if ("String".equals(type)) {
|
||||
assertEventReason(childStack, "Property [Architectures] expected type: JSONArray, found: String");
|
||||
} else {
|
||||
given().when().get(LAMBDA + functionName(childStack) + "/configuration")
|
||||
.then().statusCode(200)
|
||||
.body("Architectures", contains("x86_64"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String publishLayer() throws IOException {
|
||||
String layerName = "cfn-lambda-list-" + Long.toString(System.nanoTime(), 36);
|
||||
ByteArrayOutputStream zip = new ByteArrayOutputStream();
|
||||
@@ -269,8 +353,14 @@ class CloudFormationLambdaObjectPropertyIntegrationTest {
|
||||
}
|
||||
|
||||
private static String template(String property) {
|
||||
return template("", property);
|
||||
}
|
||||
|
||||
/** {@link #template(String)} after the template sections {@code sections}, each followed by a comma. */
|
||||
private static String template(String sections, String property) {
|
||||
return """
|
||||
{
|
||||
%s
|
||||
"Resources": {
|
||||
"Fn": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
@@ -285,7 +375,7 @@ class CloudFormationLambdaObjectPropertyIntegrationTest {
|
||||
},
|
||||
"Outputs": {"Name": {"Value": {"Ref": "Fn"}}}
|
||||
}
|
||||
""".formatted(property);
|
||||
""".formatted(sections, property);
|
||||
}
|
||||
|
||||
private static void submit(String action, String stackName, String template) {
|
||||
|
||||
+31
@@ -14,6 +14,7 @@ import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -242,6 +243,36 @@ class CloudFormationTemplateEngineTest {
|
||||
assertNull(engine().resolveJsonAttribute(mapper.createArrayNode().path("nope")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured on CloudFormation (eu-west-1, 2026-10-18), for a Lambda function's Architectures and
|
||||
* Layers: the first forms are refused as a string where a list is expected, the others are
|
||||
* lists. Fn::GetAtt and Fn::ImportValue are only known when the resource is provisioned.
|
||||
*/
|
||||
@Test
|
||||
void yieldsStringTellsTheFormsCloudFormationRefusesWhereAListIsExpected() {
|
||||
Map<String, String> types = Map.of("Str", "String", "Num", "Number", "Csv", "CommaDelimitedList",
|
||||
"Subnets", "List<AWS::EC2::Subnet::Id>", "SsmList", "AWS::SSM::Parameter::Value<List<String>>",
|
||||
"SsmCsv", "AWS::SSM::Parameter::Value<CommaDelimitedList>");
|
||||
Map<String, String> values = Map.of("Str", "x86_64", "Num", "1", "Csv", "x86_64", "Subnets", "subnet-a",
|
||||
"SsmList", "/p", "SsmCsv", "/p");
|
||||
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000", "us-east-1",
|
||||
"my-stack", "stack/id", values, Map.of(), Map.of(), Map.of("Yes", true), Map.of(), mapper,
|
||||
(Function<String, String>) name -> null, null, null, types);
|
||||
|
||||
for (String string : List.of("\"x86_64\"", "5", "true", "{\"Ref\": \"Str\"}", "{\"Ref\": \"Num\"}",
|
||||
"{\"Ref\": \"AWS::Region\"}", "{\"Fn::Select\": [0, {\"Ref\": \"Csv\"}]}",
|
||||
"{\"Fn::Join\": [\",\", {\"Ref\": \"Csv\"}]}", "{\"Fn::Sub\": \"x86_64\"}", "{\"Fn::Base64\": \"x86_64\"}",
|
||||
"{\"Fn::If\": [\"Yes\", \"x86_64\", {\"Ref\": \"AWS::NoValue\"}]}")) {
|
||||
assertTrue(e.yieldsString(json(string)), string);
|
||||
}
|
||||
for (String list : List.of("[\"x86_64\"]", "{\"Ref\": \"Csv\"}", "{\"Ref\": \"Subnets\"}",
|
||||
"{\"Ref\": \"SsmList\"}", "{\"Ref\": \"SsmCsv\"}", "{\"Ref\": \"AWS::NotificationARNs\"}",
|
||||
"{\"Fn::Split\": [\",\", {\"Ref\": \"Str\"}]}", "{\"Fn::If\": [\"Yes\", {\"Ref\": \"Csv\"}, [\"arm64\"]]}",
|
||||
"{\"Fn::GetAtt\": [\"Queue\", \"Arn\"]}", "{\"Fn::ImportValue\": \"Archs\"}")) {
|
||||
assertFalse(e.yieldsString(json(list)), list);
|
||||
}
|
||||
}
|
||||
|
||||
private CloudFormationTemplateEngine engineWithCondition(String name, boolean value) {
|
||||
return new CloudFormationTemplateEngine("000000000000", "us-east-1", "my-stack",
|
||||
"stack/id", Map.of(), Map.of(), Map.of(), Map.of(name, value), Map.of(), mapper,
|
||||
|
||||
Reference in new issue
Block a user