mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): refuse a Lambda function property that is not an object (#5912)
* fix(cloudformation): refuse a Lambda function property that is not an object The AWS::Lambda::Function provisioner read an object property that resolved to a scalar or a list as absent, so a malformed VpcConfig, DeadLetterConfig, TracingConfig or EphemeralStorage reset the function's setting to its default instead of failing. Measured on CloudFormation (eu-west-1, 2026-10-18): such a value fails the stack before the function is touched, on create and on update, with "Property [VpcConfig] expected type: JSONObject, found: String"; a list reads "found: JSONArray", and a number or a boolean reads "found: String" too. resolveMapOrDefault, which reads every object property of the function, now refuses such a value with that message. AWS reports it from its pre-deployment validation, with the detail in DescribeEvents, which Floci does not model; the message is on the resource's failed event. Tests: CloudFormationLambdaObjectPropertyIntegrationTest (a number VpcConfig and a list DeadLetterConfig on create, a string TracingConfig on update that keeps the setting) and, through the SDK, CloudFormationLambdaObjectPropertyTest; both fail on main. The compatibility suite gains TestFixtures.awaitStackTerminal, the same helper #5902 adds. * fix(cloudformation): leave out a Lambda property that resolves to AWS::NoValue Floci's template engine resolves Ref AWS::NoValue to an empty text node, which the new type check refused as a String, so a valid template that leaves VpcConfig, TracingConfig, DeadLetterConfig or EphemeralStorage out through AWS::NoValue, directly or chosen by Fn::If, failed. Measured on CloudFormation (eu-west-1, 2026-10-18): both stacks are created. resolveMapOrDefault now resolves with resolveNodeOmittingNoValue, as other provisioners do for optional properties, and reads what it leaves out as the default. Test: a direct Ref AWS::NoValue on VpcConfig and TracingConfig, and an Fn::If choosing it for DeadLetterConfig, each create the stack; it fails on the previous head.
This commit is contained in:
1 parent
61102a7c4e
commit
30d2bf03d9
3 files changed
+275
-2
No files matched your search
+166
@@ -0,0 +1,166 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/**
|
||||
* An object property of {@code AWS::Lambda::Function} that is not an object. Measured on AWS
|
||||
* (eu-west-1, 2026-10-18): a VpcConfig, DeadLetterConfig or LoggingConfig that is a string, a number,
|
||||
* a boolean or a list fails the stack before the function is touched, on create and on update,
|
||||
* with "Property [VpcConfig] expected type: JSONObject, found: String" ("found: JSONArray" for a
|
||||
* list). It is never read as an absent property, which would reset the function's setting.
|
||||
*/
|
||||
@QuarkusTest
|
||||
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/";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void aPropertyThatIsNotAnObjectFailsTheCreate() {
|
||||
String vpc = stackName("vpc");
|
||||
submit("CreateStack", vpc, template(",\"VpcConfig\": 5"));
|
||||
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(vpc, CFN_AUTH).status());
|
||||
assertEventReason(vpc, "Property [VpcConfig] expected type: JSONObject, found: String");
|
||||
|
||||
String deadLetter = stackName("dlq");
|
||||
submit("CreateStack", deadLetter, template(",\"DeadLetterConfig\": [\"x\"]"));
|
||||
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(deadLetter, CFN_AUTH).status());
|
||||
assertEventReason(deadLetter, "Property [DeadLetterConfig] expected type: JSONObject, found: JSONArray");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPropertyThatIsNotAnObjectFailsTheUpdateAndKeepsTheSetting() {
|
||||
String stack = stackName("update");
|
||||
submit("CreateStack", stack, template(",\"TracingConfig\": {\"Mode\": \"Active\"}"));
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
|
||||
String function = functionName(stack);
|
||||
|
||||
submit("UpdateStack", stack, template(",\"TracingConfig\": \"Active\""));
|
||||
assertNotEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
|
||||
assertEventReason(stack, "Property [TracingConfig] expected type: JSONObject, found: String");
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA + function + "/configuration")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TracingConfig.Mode", equalTo("Active"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured on AWS (eu-west-1, 2026-10-18): {@code Ref: AWS::NoValue}, directly or chosen by
|
||||
* {@code Fn::If}, leaves the property out, and the stack is created.
|
||||
*/
|
||||
@Test
|
||||
void aPropertyLeftOutWithNoValueIsNotRefused() {
|
||||
String direct = stackName("novalue");
|
||||
submit("CreateStack", direct, template(
|
||||
",\"VpcConfig\": {\"Ref\": \"AWS::NoValue\"}, \"TracingConfig\": {\"Ref\": \"AWS::NoValue\"}"));
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(direct, CFN_AUTH).status());
|
||||
|
||||
String conditional = stackName("novalue-if");
|
||||
submit("CreateStack", conditional, """
|
||||
{
|
||||
"Conditions": {"Never": {"Fn::Equals": ["a", "b"]}},
|
||||
"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'"},
|
||||
"DeadLetterConfig": {"Fn::If": ["Never",
|
||||
{"TargetArn": "arn:aws:sqs:us-east-1:000000000000:none"}, {"Ref": "AWS::NoValue"}]}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(conditional, CFN_AUTH).status());
|
||||
}
|
||||
|
||||
private String stackName(String purpose) {
|
||||
String name = "cfn-lambda-object-" + purpose + "-" + Long.toString(System.nanoTime(), 36);
|
||||
cleanup.register(() -> {
|
||||
submit("DeleteStack", name, null);
|
||||
CfnStackWaits.awaitStackDeleted(name, CFN_AUTH);
|
||||
});
|
||||
return name;
|
||||
}
|
||||
|
||||
private static String template(String property) {
|
||||
return """
|
||||
{
|
||||
"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'"}
|
||||
%s
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {"Name": {"Value": {"Ref": "Fn"}}}
|
||||
}
|
||||
""".formatted(property);
|
||||
}
|
||||
|
||||
private static void submit(String action, String stackName, String template) {
|
||||
RequestSpecification request = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stackName);
|
||||
if (template != null) {
|
||||
request.formParam("TemplateBody", template);
|
||||
}
|
||||
request
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static void assertEventReason(String stackName, String reason) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString(reason));
|
||||
}
|
||||
|
||||
private static String functionName(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("**.find { it.name() == 'OutputValue' }");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user