mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): resolve Fn::Sub in AWS::Include Location (#8748)
Resolve Fn::Sub (both the string and the [template, variableMap] forms) and Ref in an AWS::Include Location against the stack's pseudo parameters, template parameters, mappings and exports before parsing the S3 URI. An unreadable AWS::SSM::Parameter::Value<String> parameter is left out of what the include sees, so only a Location that uses it is rejected, and DescribeChangeSet still succeeds otherwise. Closes #8667
This commit is contained in:
1 parent
2250972578
commit
711b216bad
5 files changed
+395
-34
No files matched your search
@@ -674,6 +674,123 @@ class AwsIncludeIntegrationTest {
|
||||
.body(containsString("<LogicalResourceId>DependentParam</LogicalResourceId>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fnSubLocationWithPseudoParameterMergesSnippetOnCreateStack() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String bucketPrefix = "aws-include-sub-" + suffix;
|
||||
String bucket = bucketPrefix + "-us-east-1";
|
||||
String key = "names.yaml";
|
||||
String stackName = "aws-include-sub-stack-" + suffix;
|
||||
String expectedParamName = "/aws-include-it/sub-" + suffix;
|
||||
stacksToDelete.add(stackName);
|
||||
|
||||
putSnippet(bucket, key, "Params:\n Name: " + expectedParamName + "\n");
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Mappings": {
|
||||
"Names": {
|
||||
"Fn::Transform": {
|
||||
"Name": "AWS::Include",
|
||||
"Parameters": {
|
||||
"Location": {
|
||||
"Fn::Sub": "s3://%s-${AWS::Region}/%s"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Resources": {
|
||||
"IncludedParam": {
|
||||
"Type": "AWS::SSM::Parameter",
|
||||
"Properties": {
|
||||
"Name": { "Fn::FindInMap": ["Names", "Params", "Name"] },
|
||||
"Type": "String",
|
||||
"Value": "from-fn-sub-include"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(bucketPrefix, key);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackId>"));
|
||||
|
||||
CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE");
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackResources")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<PhysicalResourceId>" + expectedParamName + "</PhysicalResourceId>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeChangeSetWithIncludeToleratesUnrelatedMissingSsmParameter() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String bucket = "aws-include-cs-ssm-" + suffix;
|
||||
String key = "meta.yaml";
|
||||
String stackName = "aws-include-cs-ssm-stack-" + suffix;
|
||||
String location = putSnippet(bucket, key, "mergedFlag: merged-value\n");
|
||||
stacksToDelete.add(stackName);
|
||||
|
||||
String template = """
|
||||
Parameters:
|
||||
IncludeLocation:
|
||||
Type: String
|
||||
Default: %s
|
||||
MissingSsm:
|
||||
Type: AWS::SSM::Parameter::Value<String>
|
||||
Default: /aws-include-it/does-not-exist-%s
|
||||
Resources:
|
||||
IncludedParam:
|
||||
Type: AWS::SSM::Parameter
|
||||
Metadata:
|
||||
Fn::Transform:
|
||||
Name: AWS::Include
|
||||
Parameters:
|
||||
Location: !Ref IncludeLocation
|
||||
Properties:
|
||||
Name: !Ref MissingSsm
|
||||
Type: String
|
||||
Value: preview-value
|
||||
""".formatted(location, suffix);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "preview-cs")
|
||||
.formParam("ChangeSetType", "CREATE")
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "preview-cs")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<LogicalResourceId>IncludedParam</LogicalResourceId>"));
|
||||
}
|
||||
|
||||
private static String stateMachineTemplate(String stateMachineName, String location) {
|
||||
return """
|
||||
Resources:
|
||||
|
||||
@@ -12,6 +12,8 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -87,8 +89,16 @@ class AwsIncludeProcessorTest {
|
||||
}
|
||||
|
||||
private JsonNode mergedSubstitutions(String templateYaml) throws Exception {
|
||||
return mergedSubstitutions(templateYaml, Map.of(), "us-east-1", "000000000000", "", "", name -> null);
|
||||
}
|
||||
|
||||
private JsonNode mergedSubstitutions(String templateYaml, Map<String, String> parameters,
|
||||
String region, String accountId, String stackName, String stackId,
|
||||
Function<String, String> importValueResolver) throws Exception {
|
||||
JsonNode template = new CloudFormationYamlParser(mapper).parse(templateYaml);
|
||||
JsonNode merged = processor.mergeIncludes(template);
|
||||
JsonNode merged = processor.mergeIncludes(template,
|
||||
CloudFormationService.resolveDefaultParameters(template, parameters),
|
||||
region, accountId, stackName, stackId, importValueResolver);
|
||||
return merged.at("/Resources/OrdersStateMachine/Properties/DefinitionSubstitutions");
|
||||
}
|
||||
|
||||
@@ -214,10 +224,136 @@ class AwsIncludeProcessorTest {
|
||||
"message does not name the rejected location: " + bare.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fnSubLocationResolvesPseudoParametersAndTemplateParameters() throws Exception {
|
||||
stubSnippet("snippets-eu-west-1", "prod/names.yaml", "orderTableName: orders-eu\n");
|
||||
String template = """
|
||||
Parameters:
|
||||
Env:
|
||||
Type: String
|
||||
Default: dev
|
||||
Resources:
|
||||
OrdersStateMachine:
|
||||
Type: AWS::StepFunctions::StateMachine
|
||||
Properties:
|
||||
DefinitionSubstitutions:
|
||||
Fn::Transform:
|
||||
Name: AWS::Include
|
||||
Parameters:
|
||||
Location: !Sub s3://snippets-${AWS::Region}/${Env}/names.yaml
|
||||
""";
|
||||
|
||||
JsonNode substitutions = mergedSubstitutions(template, Map.of("Env", "prod"),
|
||||
"eu-west-1", "123456789012", "orders-stack",
|
||||
"arn:aws:cloudformation:eu-west-1:123456789012:stack/orders-stack/1", name -> null);
|
||||
|
||||
assertEquals("orders-eu", substitutions.get("orderTableName").asText());
|
||||
assertFalse(substitutions.has("Fn::Transform"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fnSubArrayLocationResolvesVariableMapAndDefaultParameters() throws Exception {
|
||||
stubSnippet("snippets-us-east-1", "custom/names.yaml", "orderTableName: orders-custom\n");
|
||||
String template = """
|
||||
Parameters:
|
||||
BucketPrefix:
|
||||
Type: String
|
||||
Default: snippets
|
||||
Resources:
|
||||
OrdersStateMachine:
|
||||
Type: AWS::StepFunctions::StateMachine
|
||||
Properties:
|
||||
DefinitionSubstitutions:
|
||||
Fn::Transform:
|
||||
Name: AWS::Include
|
||||
Parameters:
|
||||
Location:
|
||||
Fn::Sub:
|
||||
- s3://${BucketPrefix}-${AWS::Region}/${Folder}/names.yaml
|
||||
- Folder: custom
|
||||
""";
|
||||
|
||||
JsonNode substitutions = mergedSubstitutions(template);
|
||||
|
||||
assertEquals("orders-custom", substitutions.get("orderTableName").asText());
|
||||
assertFalse(substitutions.has("Fn::Transform"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fnSubLocationWithUnresolvedVariableIsRejected() {
|
||||
String template = """
|
||||
Resources:
|
||||
OrdersStateMachine:
|
||||
Type: AWS::StepFunctions::StateMachine
|
||||
Properties:
|
||||
DefinitionSubstitutions:
|
||||
Fn::Transform:
|
||||
Name: AWS::Include
|
||||
Parameters:
|
||||
Location: !Sub s3://snippets-${Missing}/names.yaml
|
||||
""";
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> mergedSubstitutions(template));
|
||||
assertTrue(e.getMessage().contains("\"Fn::Sub\":\"s3://snippets-${Missing}/names.yaml\""),
|
||||
"unresolved Fn::Sub variable must reject the Location node rather than substituting the variable name: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fnSubLocationWithImportValueResolvesExport() throws Exception {
|
||||
stubSnippet("exported-snippets", "names.yaml", "orderTableName: orders-exported\n");
|
||||
String template = """
|
||||
Resources:
|
||||
OrdersStateMachine:
|
||||
Type: AWS::StepFunctions::StateMachine
|
||||
Properties:
|
||||
DefinitionSubstitutions:
|
||||
Fn::Transform:
|
||||
Name: AWS::Include
|
||||
Parameters:
|
||||
Location:
|
||||
Fn::Sub:
|
||||
- s3://${Bucket}/names.yaml
|
||||
- Bucket: { Fn::ImportValue: SharedSnippetBucket }
|
||||
""";
|
||||
|
||||
JsonNode substitutions = mergedSubstitutions(template, Map.of(), "us-east-1", "000000000000", "", "",
|
||||
exportName -> "SharedSnippetBucket".equals(exportName) ? "exported-snippets" : null);
|
||||
|
||||
assertEquals("orders-exported", substitutions.get("orderTableName").asText());
|
||||
assertFalse(substitutions.has("Fn::Transform"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void refLocationResolvesTemplateParameter() throws Exception {
|
||||
stubMeasuredFragment();
|
||||
String template = """
|
||||
Parameters:
|
||||
InputValue:
|
||||
Type: String
|
||||
Default: %s
|
||||
Resources:
|
||||
OrdersStateMachine:
|
||||
Type: AWS::StepFunctions::StateMachine
|
||||
Properties:
|
||||
DefinitionSubstitutions:
|
||||
Fn::Transform:
|
||||
Name: AWS::Include
|
||||
Parameters:
|
||||
Location: { Ref: InputValue }
|
||||
""".formatted(LOCATION);
|
||||
|
||||
JsonNode substitutions = mergedSubstitutions(template);
|
||||
|
||||
assertEquals(EXPAND_ITEMS, substitutions.path("expandItems").asText());
|
||||
assertEquals("orders", substitutions.get("orderTableName").asText());
|
||||
assertFalse(substitutions.has("Fn::Transform"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void locationAsAMappingNamesTheNodeInTheRejectionMessage() {
|
||||
// Location: {Ref: InputValue} is the form AWS's own Fn::Transform documentation uses in its
|
||||
// only AWS::Include example. Ref resolution is not implemented; the node must still be named.
|
||||
// AWS::Include example. When InputValue is not declared, the unresolved node is named.
|
||||
String template = """
|
||||
Resources:
|
||||
OrdersStateMachine:
|
||||
|
||||
Reference in new issue
Block a user