mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): preserve numeric GetAtt attributes (#6564)
This commit is contained in:
1 parent
47226f76d1
commit
d903074d75
8 files changed
+401
-20
No files matched your search
+110
@@ -0,0 +1,110 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.testing.CfnStackWaits;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudFormationNumericAttributeIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261020/us-east-1/cloudformation/aws4_request";
|
||||
private static final String EC2_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261020/us-east-1/ec2/aws4_request";
|
||||
|
||||
@Test
|
||||
void aNumericGetAttCanConfigureAnotherResourcesNumericProperty() {
|
||||
String stack = "numeric-attribute-" + UUID.randomUUID();
|
||||
String template = """
|
||||
{"Parameters":{"Number":{"Type":"Number","Default":1}},
|
||||
"Resources":{"First":{"Type":"AWS::EC2::PrefixList","Properties":{
|
||||
"PrefixListName":"%1$s-first","AddressFamily":"IPv4","MaxEntries":1}},
|
||||
"Second":{"Type":"AWS::EC2::PrefixList","Properties":{
|
||||
"PrefixListName":"%1$s-second","AddressFamily":"IPv4",
|
||||
"MaxEntries":{"Fn::GetAtt":["First","Version"]}}}},
|
||||
"Outputs":{"Id":{"Value":{"Fn::GetAtt":["Second","PrefixListId"]}},
|
||||
"Arn":{"Value":{"Fn::Sub":"${Second.Arn}"}},
|
||||
"Literal":{"Value":1},"Parameter":{"Value":{"Ref":"Number"}},
|
||||
"Join":{"Value":{"Fn::Join":["",[1]]}},
|
||||
"SubMap":{"Value":{"Fn::Sub":["${Value}",{"Value":1}]}}}}
|
||||
""".formatted(stack);
|
||||
try {
|
||||
cloudFormation(stack, "CreateStack", template);
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
|
||||
String prefixes = describePrefixLists();
|
||||
assertTrue(prefixes.contains(stack + "-second") && prefixes.contains("<maxEntries>1</maxEntries>"),
|
||||
prefixes);
|
||||
String described = given().contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH).formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stack).post("/").then().statusCode(200).extract().asString();
|
||||
Map<String, String> outputs = XmlParser.extractPairs(
|
||||
described, "Outputs", "OutputKey", "OutputValue");
|
||||
for (String key : new String[]{"Literal", "Parameter", "Join", "SubMap"}) {
|
||||
assertEquals("1", outputs.get(key), key);
|
||||
}
|
||||
} finally {
|
||||
deleteStack(stack);
|
||||
}
|
||||
assertFalse(describePrefixLists().contains(stack));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"Fn::GetAtt\":[\"First\",\"Version\"]}",
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::GetAtt\":[\"First\",\"Version\"]}]]}",
|
||||
"{\"Fn::Sub\":\"${First.Version}\"}",
|
||||
"{\"Fn::Sub\":[\"${Value}\",{\"Value\":{\"Fn::GetAtt\":[\"First\",\"Version\"]}}]}",
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::Select\":[0,[{\"Fn::GetAtt\":[\"First\",\"Version\"]}]]}]]}",
|
||||
"{\"Fn::Select\":[0,{\"Fn::Split\":[\",\",{\"Fn::GetAtt\":[\"First\",\"Version\"]}]}]}",
|
||||
"{\"Fn::Base64\":{\"Fn::GetAtt\":[\"First\",\"Version\"]}}"
|
||||
})
|
||||
void numericStringOutputsFailAndRollBackOwnedResources(String value) {
|
||||
String stack = "numeric-output-" + UUID.randomUUID();
|
||||
String template = """
|
||||
{"Resources":{"First":{"Type":"AWS::EC2::PrefixList","Properties":{
|
||||
"PrefixListName":"%s","AddressFamily":"IPv4","MaxEntries":1}}},
|
||||
"Outputs":{"Version":{"Value":%s}}}
|
||||
""".formatted(stack, value);
|
||||
try {
|
||||
cloudFormation(stack, "CreateStack", template);
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stack, CFN_AUTH);
|
||||
assertEquals("ROLLBACK_COMPLETE", state.status(), state.reason());
|
||||
assertFalse(describePrefixLists().contains(stack));
|
||||
} finally {
|
||||
deleteStack(stack);
|
||||
}
|
||||
}
|
||||
|
||||
private static void cloudFormation(String stack, String action, String template) {
|
||||
RequestSpecification request = given().contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH).formParam("Action", action).formParam("StackName", stack);
|
||||
if (template != null) {
|
||||
request.formParam("TemplateBody", template);
|
||||
}
|
||||
request.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void deleteStack(String stack) {
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack, CFN_AUTH);
|
||||
}
|
||||
|
||||
private static String describePrefixLists() {
|
||||
return given().contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH).formParam("Action", "DescribeManagedPrefixLists")
|
||||
.formParam("Version", "2016-11-15")
|
||||
.when().post("/").then().statusCode(200).extract().asString();
|
||||
}
|
||||
}
|
||||
+120
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2NetworkCfnProvisioner;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
@@ -57,6 +58,27 @@ class CloudFormationTemplateEngineTest {
|
||||
assertEquals("Thing.Missing", e.resolve(json("{\"Fn::GetAtt\": [\"Thing\", \"Missing\"]}")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"Fn::Sub\":\"${Bucket.Missing}/*\"}",
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::GetAtt\":[\"Bucket\",\"Missing\"]},\"/*\"]]}",
|
||||
"{\"Fn::Sub\":[\"${Value}/*\",{\"Value\":{\"Fn::GetAtt\":\"Bucket.Missing\"}}]}",
|
||||
"{\"Fn::Base64\":{\"Fn::GetAtt\":\"Bucket.Missing\"}}",
|
||||
"{\"Fn::Split\":[\",\",{\"Fn::GetAtt\":\"Bucket.Missing\"}]}",
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::If\":[\"Absent\",\"unused\",{\"Fn::GetAtt\":\"Bucket.Missing\"}]}]]}",
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::Select\":[0,[{\"Fn::GetAtt\":\"Bucket.Missing\"}]]}]]}"
|
||||
})
|
||||
void stringTypeChecksRecordMissingAttributesOnce(String intrinsic) {
|
||||
CloudFormationTemplateEngine e = engineWithAttributes(Map.of("Bucket", Map.of("Arn", "bucket-arn")));
|
||||
JsonNode policy = mapper.createObjectNode().set("Resource", json(intrinsic));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> e.resolveJsonAttributeStrict(policy));
|
||||
|
||||
assertEquals("ValidationError", failure.getErrorCode());
|
||||
assertEquals("Policy document contains unresolved CloudFormation intrinsics: Fn::GetAtt Bucket.Missing. "
|
||||
+ "Storing them would silently void the statements that use them.", failure.getMessage());
|
||||
}
|
||||
|
||||
private JsonNode json(String s) {
|
||||
try {
|
||||
return mapper.readTree(s);
|
||||
@@ -65,6 +87,72 @@ class CloudFormationTemplateEngineTest {
|
||||
}
|
||||
}
|
||||
|
||||
private CloudFormationTemplateEngine engineWithNumericAttribute() {
|
||||
Ec2NetworkCfnProvisioner provisioner = new Ec2NetworkCfnProvisioner(null);
|
||||
return new CloudFormationTemplateEngine("000000000000", "us-east-1", "my-stack", "stack/id",
|
||||
Map.of(), Map.of(), Map.of("Prefix", Map.of("Version", "1", "PrefixListId", "pl-example")),
|
||||
Map.of("Include", true), Map.of(), mapper, name -> null, null, null, Map.of(),
|
||||
(id, attribute, value) -> provisioner.attributeValue("AWS::EC2::PrefixList", attribute, value));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]}",
|
||||
"{\"Fn::GetAtt\":\"Prefix.Version\"}",
|
||||
"{\"Fn::If\":[\"Include\",{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]},0]}",
|
||||
"{\"Fn::Select\":[0,[{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]}]]}"
|
||||
})
|
||||
void getAttPreservesItsNumericType(String intrinsic) {
|
||||
JsonNode result = engineWithNumericAttribute().resolveNode(json(intrinsic));
|
||||
assertTrue(result.isIntegralNumber());
|
||||
assertEquals(1, result.intValue());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]}]]}",
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::If\":[\"Include\",{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]},\"ok\"]}]]}",
|
||||
"{\"Fn::Sub\":\"${Prefix.Version}\"}",
|
||||
"{\"Fn::Sub\":[\"${Version}\",{\"Version\":{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]}}]}",
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::Select\":[0,[{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]}]]}]]}",
|
||||
"{\"Fn::Select\":[0,{\"Fn::Split\":[\",\",{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]}]}]}",
|
||||
"{\"Fn::Base64\":{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]}}"
|
||||
})
|
||||
void numericAttributesCannotBeUsedAsStringFragments(String intrinsic) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> engineWithNumericAttribute().resolve(json(intrinsic)));
|
||||
assertEquals("ValidationError", error.getErrorCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"Fn::Join\":[\"\",[{\"Fn::GetAtt\":[\"Prefix\",\"PrefixListId\"]}]]}",
|
||||
"{\"Fn::Sub\":\"${Prefix.PrefixListId}\"}",
|
||||
"{\"Fn::Sub\":[\"${Id}\",{\"Id\":{\"Fn::GetAtt\":[\"Prefix\",\"PrefixListId\"]}}]}",
|
||||
"{\"Fn::Select\":[0,{\"Fn::Split\":[\",\",{\"Fn::GetAtt\":[\"Prefix\",\"PrefixListId\"]}]}]}",
|
||||
"{\"Fn::If\":[\"Missing\",{\"Fn::GetAtt\":[\"Prefix\",\"Version\"]},\"pl-example\"]}"
|
||||
})
|
||||
void stringAttributesAndUnselectedNumericBranchesRemainValid(String intrinsic) {
|
||||
assertEquals("pl-example", engineWithNumericAttribute().resolve(json(intrinsic)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void literalsNumberParametersAndUnselectedNumericAttributesKeepStringCoercion() {
|
||||
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
|
||||
"us-east-1", "stack", "stack/id", Map.of("Number", "1"), Map.of(), Map.of(),
|
||||
Map.of(), Map.of(), mapper, name -> null, null, null, Map.of("Number", "Number"));
|
||||
assertEquals("1", e.resolveString(json("1"), "must be a string"));
|
||||
assertEquals("1", e.resolveString(json("{\"Ref\":\"Number\"}"), "must be a string"));
|
||||
assertEquals("1", e.resolve(json("{\"Fn::Join\":[\"\",[1]]}")));
|
||||
assertEquals("1", e.resolve(json("{\"Fn::Join\":[\"\",[{\"Ref\":\"Number\"}]]}")));
|
||||
assertEquals("1", e.resolve(json("{\"Fn::Select\":[0,{\"Fn::Split\":[\",\",1]}]}")));
|
||||
assertEquals("1", e.resolve(json("{\"Fn::Select\":[0,{\"Fn::Split\":[\",\",{\"Ref\":\"Number\"}]}]}")));
|
||||
assertEquals("1", e.resolve(json("{\"Fn::Sub\":[\"${Value}\",{\"Value\":1}]}")));
|
||||
assertEquals("pl-example", engineWithNumericAttribute().resolve(json("""
|
||||
{"Fn::Join":["",[{"Fn::Select":[1,[{"Fn::GetAtt":["Prefix","Version"]},"pl-example"]]}]]}
|
||||
""")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void selectedNoValueOmitsTheOptionalPropertyWhileKeepingLiteralEmptyText() {
|
||||
JsonNode source = json("""
|
||||
@@ -541,6 +629,38 @@ class CloudFormationTemplateEngineTest {
|
||||
e.resolveNode(json("\"{{resolve:ssm:/demo/url}}\"")).asText());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"Fn::GetAtt\":[\"Custom\",\"Url\"]}",
|
||||
"{\"Fn::GetAtt\":\"Custom.Url\"}",
|
||||
"{\"Fn::If\":[\"Include\",{\"Fn::GetAtt\":[\"Custom\",\"Url\"]},\"unused\"]}",
|
||||
"{\"Fn::Select\":[0,[{\"Fn::GetAtt\":[\"Custom\",\"Url\"]}]]}"
|
||||
})
|
||||
void getAttResolvesTextualDynamicReferencesOnce(String intrinsic) {
|
||||
List<String> calls = new ArrayList<>();
|
||||
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
|
||||
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(),
|
||||
Map.of("Custom", Map.of("Url", "{{resolve:ssm:/demo/url}}")),
|
||||
Map.of("Include", true), Map.of(), mapper, name -> null, value -> {
|
||||
calls.add(value);
|
||||
return "{{resolve:ssm:/literal-result}}";
|
||||
});
|
||||
|
||||
assertEquals("{{resolve:ssm:/literal-result}}", e.resolveNode(json(intrinsic)).textValue());
|
||||
assertEquals(List.of("{{resolve:ssm:/demo/url}}"), calls);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"{\"Fn::GetAtt\":[\"Custom\",\"Url\"]}", "{\"Fn::GetAtt\":\"Custom.Url\"}"})
|
||||
void getAttWithoutDynamicReferencesLeavesTextUnchanged(String intrinsic) {
|
||||
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
|
||||
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(),
|
||||
Map.of("Custom", Map.of("Url", "{{resolve:ssm:/demo/url}}")), Map.of(), Map.of(), mapper,
|
||||
name -> null, value -> fail("dynamic reference must not be resolved here: " + value));
|
||||
|
||||
assertEquals("{{resolve:ssm:/demo/url}}", e.resolveWithoutDynamicReferences(json(intrinsic)));
|
||||
}
|
||||
|
||||
/**
|
||||
* CDK emits an RDS master credential as an {@code Fn::Join} whose fragments split one dynamic
|
||||
* reference: {@code ["{{resolve:secretsmanager:", {"Ref": "Secret"}, ":SecretString:password::}}"]}.
|
||||
|
||||
Reference in new issue
Block a user