mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
+19
-3
@@ -1572,6 +1572,19 @@ public class CloudFormationService implements ResourceProvider {
|
||||
return buildTemplateSummary(template);
|
||||
}
|
||||
|
||||
/** Each parameter the template declares, with its {@link #parameterType}. */
|
||||
private static Map<String, String> parameterTypes(JsonNode template) {
|
||||
Map<String, String> types = new HashMap<>();
|
||||
template.path("Parameters").properties()
|
||||
.forEach(entry -> types.put(entry.getKey(), parameterType(entry.getValue())));
|
||||
return types;
|
||||
}
|
||||
|
||||
/** A parameter declaration's {@code Type}, {@code String} when the template leaves it out. */
|
||||
private static String parameterType(JsonNode declaration) {
|
||||
return declaration.hasNonNull("Type") ? declaration.get("Type").asText() : "String";
|
||||
}
|
||||
|
||||
private TemplateSummary buildTemplateSummary(JsonNode template) {
|
||||
String description = template.hasNonNull("Description") ? template.get("Description").asText() : null;
|
||||
String version = template.hasNonNull("AWSTemplateFormatVersion")
|
||||
@@ -1588,7 +1601,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
p.hasNonNull("Default") ? p.get("Default").asText() : null,
|
||||
p.path("NoEcho").asBoolean(false),
|
||||
p.hasNonNull("Description") ? p.get("Description").asText() : null,
|
||||
p.hasNonNull("Type") ? p.get("Type").asText() : "String"));
|
||||
parameterType(p)));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1956,6 +1969,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
Map<String, String> givenParams = resolveDefaultParameters(template, params);
|
||||
stack.replaceParameters(givenParams);
|
||||
Map<String, String> resolvedParams = resolveSsmParameters(template, givenParams, region);
|
||||
Map<String, String> parameterTypes = parameterTypes(template);
|
||||
stack.setResolvedParameters(new LinkedHashMap<>(resolvedParams));
|
||||
|
||||
// Resolve conditions first
|
||||
@@ -2006,7 +2020,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
name -> exports.get(accountExportKey(accountId, exportKey(region, name))),
|
||||
value -> dynamicReferences.resolveDynamicReferences(value, region, false),
|
||||
(name, output, outputRegion, roleArn) ->
|
||||
resolveStackOutput(accountId, name, output, outputRegion, roleArn));
|
||||
resolveStackOutput(accountId, name, output, outputRegion, roleArn),
|
||||
parameterTypes);
|
||||
|
||||
StackResource resource = stack.getResources().get(logicalId);
|
||||
StackResource previousResource = resource;
|
||||
@@ -2174,7 +2189,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
name -> exports.get(accountExportKey(accountId, exportKey(region, name))),
|
||||
value -> dynamicReferences.resolveDynamicReferences(value, region, false),
|
||||
(name, output, outputRegion, roleArn) ->
|
||||
resolveStackOutput(accountId, name, output, outputRegion, roleArn));
|
||||
resolveStackOutput(accountId, name, output, outputRegion, roleArn),
|
||||
parameterTypes);
|
||||
|
||||
// Resolve outputs before mutating stack/global export state, so failed updates do not
|
||||
// leave stale or partially registered exports behind.
|
||||
|
||||
+62
@@ -64,6 +64,7 @@ public class CloudFormationTemplateEngine {
|
||||
private final Function<String, String> importValueResolver;
|
||||
private final UnaryOperator<String> dynamicReferenceResolver;
|
||||
private final StackOutputResolver stackOutputResolver;
|
||||
private final Map<String, String> parameterTypes;
|
||||
private final List<String> unresolvedIntrinsics = new ArrayList<>();
|
||||
/**
|
||||
* Above zero while {@code Fn::GetStackOutput}'s arguments resolve: CloudFormation hands them on
|
||||
@@ -134,6 +135,27 @@ public class CloudFormationTemplateEngine {
|
||||
Function<String, String> importValueResolver,
|
||||
UnaryOperator<String> dynamicReferenceResolver,
|
||||
StackOutputResolver stackOutputResolver) {
|
||||
this(accountId, region, stackName, stackId, parameters, physicalIds, resourceAttributes,
|
||||
conditions, mappings, objectMapper, importValueResolver, dynamicReferenceResolver,
|
||||
stackOutputResolver, Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param parameterTypes each declared parameter's {@code Type}, which tells a {@code Ref} to a
|
||||
* list parameter, resolved to the comma-joined text, from one to a string
|
||||
* (see {@link #yieldsString})
|
||||
*/
|
||||
CloudFormationTemplateEngine(String accountId, String region, String stackName, String stackId,
|
||||
Map<String, String> parameters,
|
||||
Map<String, String> physicalIds,
|
||||
Map<String, Map<String, String>> resourceAttributes,
|
||||
Map<String, Boolean> conditions,
|
||||
Map<String, JsonNode> mappings,
|
||||
ObjectMapper objectMapper,
|
||||
Function<String, String> importValueResolver,
|
||||
UnaryOperator<String> dynamicReferenceResolver,
|
||||
StackOutputResolver stackOutputResolver,
|
||||
Map<String, String> parameterTypes) {
|
||||
this.accountId = accountId;
|
||||
this.region = region;
|
||||
this.stackName = stackName;
|
||||
@@ -147,6 +169,7 @@ public class CloudFormationTemplateEngine {
|
||||
this.importValueResolver = importValueResolver;
|
||||
this.dynamicReferenceResolver = dynamicReferenceResolver;
|
||||
this.stackOutputResolver = stackOutputResolver;
|
||||
this.parameterTypes = parameterTypes;
|
||||
}
|
||||
|
||||
/** The id of the stack this engine resolves for, as {@code AWS::StackId} returns it. */
|
||||
@@ -578,6 +601,45 @@ public class CloudFormationTemplateEngine {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code node} yields a string, which CloudFormation's template validation refuses where
|
||||
* a list is expected, "expected type: JSONArray, found: String" (measured, eu-west-1,
|
||||
* 2026-10-18): a literal scalar, a {@code Ref} to a parameter whose type is not a list
|
||||
* ({@code String}, {@code Number}, ...) or to a pseudo-parameter but
|
||||
* {@code AWS::NotificationARNs}, {@code Fn::Select}, {@code Fn::Join}, {@code Fn::Sub},
|
||||
* {@code Fn::Base64}, or an {@code Fn::If} choosing one of these. A {@code Ref} to a {@code CommaDelimitedList},
|
||||
* {@code List<...>} or SSM list parameter is a list, though it resolves to the comma-joined
|
||||
* text. What only provisioning can tell (a resource's {@code Ref}, {@code Fn::GetAtt},
|
||||
* {@code Fn::ImportValue}, {@code Fn::FindInMap}) is not reported here.
|
||||
*/
|
||||
public boolean yieldsString(JsonNode node) {
|
||||
if (node == null || node.isNull() || node.isMissingNode() || node.isArray()) {
|
||||
return false;
|
||||
}
|
||||
if (node.isValueNode()) {
|
||||
return true;
|
||||
}
|
||||
if (node.has("Ref")) {
|
||||
String name = node.get("Ref").asText();
|
||||
if (name.startsWith("AWS::")) {
|
||||
return !"AWS::NotificationARNs".equals(name) && !"AWS::NoValue".equals(name);
|
||||
}
|
||||
String type = parameterTypes.get(name);
|
||||
return type != null && !isListParameterType(type);
|
||||
}
|
||||
if (node.has("Fn::If")) {
|
||||
return yieldsString(selectIfBranch(node.get("Fn::If")));
|
||||
}
|
||||
return node.has("Fn::Select") || node.has("Fn::Join") || node.has("Fn::Sub") || node.has("Fn::Base64");
|
||||
}
|
||||
|
||||
/** The parameter types whose value is a list: CommaDelimitedList, List<...> and their SSM forms. */
|
||||
private static boolean isListParameterType(String type) {
|
||||
return "CommaDelimitedList".equals(type) || type.startsWith("List<")
|
||||
|| "AWS::SSM::Parameter::Value<CommaDelimitedList>".equals(type)
|
||||
|| type.startsWith("AWS::SSM::Parameter::Value<List<");
|
||||
}
|
||||
|
||||
private boolean isListValuedIntrinsic(JsonNode node) {
|
||||
if (node == null) {
|
||||
return false;
|
||||
|
||||
+3
-2
@@ -795,7 +795,8 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
/**
|
||||
* The list-of-strings property {@code source}, or null when the template leaves it out,
|
||||
* {@code Ref: AWS::NoValue} directly or through {@code Fn::If} included. A literal of another
|
||||
* type is refused (see {@link #requireType}). An intrinsic is read through
|
||||
* type is refused (see {@link #requireType}), and so is an intrinsic that yields a string (see
|
||||
* {@link CloudFormationTemplateEngine#yieldsString}). Any other intrinsic is read through
|
||||
* {@link CloudFormationTemplateEngine#resolveStringList}, as CloudFormation reads a {@code Ref}
|
||||
* to a {@code CommaDelimitedList} parameter, which the engine resolves to the comma-joined text,
|
||||
* and {@code Fn::Split} (measured, eu-west-1, 2026-10-18).
|
||||
@@ -814,7 +815,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (resolved == null || resolved.isNull() || resolved.isMissingNode()) {
|
||||
return null;
|
||||
}
|
||||
if (resolved.isObject()) {
|
||||
if (resolved.isObject() || (!resolved.isArray() && engine.yieldsString(raw))) {
|
||||
requireType(source, resolved, JsonNodeType.ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user