fix(cloudformation): fail the stack when Lambda S3 code cannot be read (#2650)

* fix(cloudformation): fail the stack when Lambda S3 code cannot be read

resolveLambdaCode probed Code.S3Bucket/S3Key only to decide whether to use
it, and treated any failure as "no code available", falling through to the
built-in default handler. A fallback default is sound only when the input
is absent; applying it to an input that is present but unusable turns an
explicit user intent into a silent substitution.

With Handler: index.handler -- the stub's own handler -- the stack reached
CREATE_COMPLETE and the function served the stub's canned 200 for every
request, with nothing in the API surface saying the declared code was never
deployed. With any other handler the resource instead failed with "Handler
file '<x>' not found in deployment package", naming a handler that is
correct in the template and present in the package that was never fetched.

Throw ValidationError naming the bucket, the key and the underlying S3
error, so the resource fails and the stack rolls back as real
CloudFormation does. The default handler is kept for its legitimate case, a
template that supplies no Code block at all.

Note this changes outcomes: stacks that previously completed against
unreadable code now fail.

Closes #2648

* fix(cloudformation): allow opting back in to the Lambda stub fallback

Adds floci.services.cloudformation.allow-stub-lambda-code, defaulting to
false, so failing the stack on unreadable Lambda code stays the default
while a stack that deliberately leaves its packages unbuilt can opt back
in to the previous stub handler rather than break on upgrade. Requested
during review of the parent change.

The knob lives on EmulatorConfig.CloudFormationServiceConfig and in both
the main and test application.yml. Its javadoc records what enabling it
costs: the stack reports CREATE_COMPLETE while serving a placeholder that
returns a canned 200, so it cannot be used to verify the real function.

Reading the flag means the provisioner needs EmulatorConfig, and that
constructor parameter reaches 13 unit tests that build it positionally.
Those pass null, so the read is null safe: a provisioner built without
config gets the documented default, which is the strict behaviour.

The opt in path is covered by its own top level test class rather than a
nested one. A nested class carrying @TestProfile compiles and passes when
targeted directly, but Surefire discovers test classes by file name and
skips it in a full run.
This commit is contained in:
David authored and GitHub committed 2026-08-28 13:24:53 -04:00
1 parent 1b3417a7a3
commit 231c0972b0
19 files changed
+237 -16

No files matched your search

@@ -43,7 +43,7 @@ class ApiGatewayV2CfnProvisionerTest {
null, apiGatewayV2Service, null, null, null, null, mapper,
null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null,
new CloudFormationResourceRegistry(java.util.List.of()));
new CloudFormationResourceRegistry(java.util.List.of()), null);
Api api = new Api();
api.setApiId(API_ID);
@@ -37,7 +37,7 @@ class CloudFormationDeleteIdempotencyTest {
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
}
@Test
@@ -42,7 +42,7 @@ class CloudFormationEc2ProvisionerTest {
null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, mapper, null, null, null, null, null, null,
ec2Service, null, null, null, null, null, null, null,
null, null, new CloudFormationResourceRegistry(List.of()));
null, null, new CloudFormationResourceRegistry(List.of()), null);
}
@Test
@@ -54,7 +54,7 @@ class CloudFormationIamAttachmentProvisionerTest {
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null,
new CloudFormationResourceRegistry(List.of(new IamRoleCfnProvisioner(iamService))));
new CloudFormationResourceRegistry(List.of(new IamRoleCfnProvisioner(iamService))), null);
}
@Test
@@ -55,7 +55,7 @@ class CloudFormationLambdaLegacyNameModeTest {
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
}
@Test
@@ -0,0 +1,110 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
/**
* A stack whose Lambda names code in S3 that cannot be read must fail, the way real
* CloudFormation does (issue #2648). Floci used to substitute a built-in stub handler, so the
* stack reached CREATE_COMPLETE running code the template never referenced — and when the
* handler was not "index.handler" it failed with a handler error that pointed away from the
* real problem. Control plane only, so the test is Docker-free.
*/
@QuarkusTest
class CloudFormationLambdaMissingS3CodeIntegrationTest {
static final String CFN_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
static String template(String fnName, String handler) {
return """
{
"Resources": {
"Fn": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "%s",
"Runtime": "nodejs20.x",
"Handler": "%s",
"Role": "arn:aws:iam::000000000000:role/r",
"Code": {"S3Bucket": "cfn-missing-code-bucket", "S3Key": "does-not-exist.zip"}
}
}
}
}
""".formatted(fnName, handler);
}
static void createStack(String stackName, String template) {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "CreateStack")
.formParam("StackName", stackName)
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200);
}
private static void assertStackFailed(String stackName) {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.body(not(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>")));
}
@Test
void unreadableS3CodeFailsTheStackInsteadOfDeployingAStub() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-missing-code-" + suffix;
String fnName = "missing-code-fn-" + suffix;
// "index.handler" is the stub's own handler, so before the fix this combination was the
// silent one: the stack completed and the function served the stub's canned response.
createStack(stackName, template(fnName, "index.handler"));
assertStackFailed(stackName);
// The function must not exist at all — a stub must never stand in for the real package.
given()
.when().get("/2015-03-31/functions/" + fnName)
.then()
.statusCode(404);
}
@Test
void theFailureNamesTheUnreadableCodeNotTheHandler() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-missing-code-msg-" + suffix;
String fnName = "missing-code-msg-fn-" + suffix;
// With a non-default handler the old behaviour failed with
// "Handler file 'src/api' not found in deployment package", which is misleading: the
// handler is correct and present in the real package that was never fetched.
createStack(stackName, template(fnName, "src/api.handler"));
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStackEvents")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("does-not-exist.zip"))
.body(not(containsString("not found in deployment package")));
}
}
@@ -0,0 +1,62 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static io.github.hectorvent.floci.services.cloudformation.CloudFormationLambdaMissingS3CodeIntegrationTest.CFN_AUTH;
import static io.github.hectorvent.floci.services.cloudformation.CloudFormationLambdaMissingS3CodeIntegrationTest.createStack;
import static io.github.hectorvent.floci.services.cloudformation.CloudFormationLambdaMissingS3CodeIntegrationTest.template;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
/**
* The opt-in escape hatch for issue #2648: a stack that deliberately leaves its Lambda packages
* unbuilt can set one property and keep the old stub-handler behaviour instead of breaking on
* upgrade. Off by default, because a stack that reports CREATE_COMPLETE while serving a placeholder
* is the more dangerous of the two behaviours.
*
* <p>A separate top-level class rather than a nested one, because {@code @TestProfile} applies per
* test class and Surefire discovers test classes by file name: a nested class carrying the profile
* compiles and passes when targeted directly, but is silently skipped in a full {@code mvn test}.
*/
@QuarkusTest
@TestProfile(CloudFormationLambdaStubCodeAllowedIntegrationTest.AllowStubProfile.class)
class CloudFormationLambdaStubCodeAllowedIntegrationTest {
public static final class AllowStubProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.cloudformation.allow-stub-lambda-code", "true");
}
}
@Test
void unreadableS3CodeFallsBackToTheStubHandler() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-stub-allowed-" + suffix;
String fnName = "stub-allowed-fn-" + suffix;
createStack(stackName, template(fnName, "index.handler"));
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
// The stub stands in for the package that could not be read.
given()
.when().get("/2015-03-31/functions/" + fnName)
.then()
.statusCode(200);
}
}
@@ -48,7 +48,7 @@ class CloudFormationLogGroupProvisionerTest {
null, null, null, null, null, null, null,
null, null, logsService, null, null, null, null,
null, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
}
@Test
@@ -50,7 +50,7 @@ class CustomResourceProvisionerTest {
null, null, null, null, lambdaService, null, null, null, null, null,
null, null, null, null, null, null, mapper, store, endpoint, null, null, null, null, null, null, null, null, null, null, null, null,
null, null,
new io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry(java.util.List.of()));
new io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry(java.util.List.of()), null);
}
private CloudFormationTemplateEngine engine() {
@@ -35,7 +35,7 @@ class DynamoDbReplicaCfnProvisionerTest {
null, null, null, dynamoDbService, null, null, null, null, null, null,
null, null, null, null, null, null, mapper, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
}
@Test
@@ -53,7 +53,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
null, null, null, dynamoDbService, null, null, null, null, null, null,
null, null, null, null, null, null, mapper, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
TableDefinition created = new TableDefinition();
created.setTableName(TABLE);
@@ -46,7 +46,7 @@ class EventBusDeleteOwnershipTest {
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
}
@Test
@@ -59,7 +59,7 @@ class EventBusProvisionOwnershipTest {
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
}
/**
@@ -71,7 +71,7 @@ class RdsCfnProvisionerTest {
null, null, null, null, null, null, null,
rdsService, null, null, null, null, null, null,
null, null,
new io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry(java.util.List.of()));
new io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry(java.util.List.of()), null);
}
private CloudFormationTemplateEngine engine() {
@@ -71,7 +71,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
null, null, null, null, null, null, null,
rdsService, null, null, null, null, null, null,
docDbService, null,
new CloudFormationResourceRegistry(List.of()));
new CloudFormationResourceRegistry(List.of()), null);
}
@Test