mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
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:
1 parent
1b3417a7a3
commit
231c0972b0
19 files changed
+237
-16
No files matched your search
@@ -1341,6 +1341,19 @@ public interface EmulatorConfig {
|
||||
|
||||
@WithDefault("30")
|
||||
long deletedStackRetentionSeconds();
|
||||
|
||||
/**
|
||||
* Whether an {@code AWS::Lambda::Function} whose template names code in S3 that cannot be
|
||||
* read should fall back to the built-in stub handler instead of failing the resource.
|
||||
*
|
||||
* <p>Defaults to {@code false}, matching real CloudFormation, which fails the resource and
|
||||
* rolls the stack back. Set to {@code true} to restore the older behaviour for a stack that
|
||||
* deliberately leaves Lambda packages unbuilt — note that such a stack reports
|
||||
* {@code CREATE_COMPLETE} while serving a placeholder that returns
|
||||
* {@code {"statusCode":200}}, so it cannot be used to verify the real function.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean allowStubLambdaCode();
|
||||
}
|
||||
|
||||
interface AcmServiceConfig {
|
||||
|
||||
+33
-3
@@ -100,6 +100,7 @@ import io.github.hectorvent.floci.services.apigatewayv2.model.*;
|
||||
import io.github.hectorvent.floci.services.cognito.CognitoService;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPool;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -220,6 +221,7 @@ public class CloudFormationResourceProvisioner {
|
||||
// below. As types migrate, their switch cases and provisionXxx methods are removed here; the
|
||||
// now-dead service deps above are cleared in the final cleanup once the switch is empty.
|
||||
private final CloudFormationResourceRegistry resourceRegistry;
|
||||
private final EmulatorConfig config;
|
||||
|
||||
@Inject
|
||||
public CloudFormationResourceProvisioner(S3Service s3Service, SqsService sqsService,
|
||||
@@ -251,7 +253,9 @@ public class CloudFormationResourceProvisioner {
|
||||
FirehoseService firehoseService,
|
||||
DocDbService docDbService,
|
||||
CloudFrontService cloudFrontService,
|
||||
CloudFormationResourceRegistry resourceRegistry) {
|
||||
CloudFormationResourceRegistry resourceRegistry,
|
||||
EmulatorConfig config) {
|
||||
this.config = config;
|
||||
this.s3Service = s3Service;
|
||||
this.sqsService = sqsService;
|
||||
this.snsService = snsService;
|
||||
@@ -2412,6 +2416,15 @@ public class CloudFormationResourceProvisioner {
|
||||
reservedConcurrentExecutions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an unreadable explicit {@code Code} reference may fall back to the stub handler.
|
||||
* The provisioners hand-built in unit tests carry no config; absent configuration means the
|
||||
* documented default, which is the strict behaviour.
|
||||
*/
|
||||
private boolean stubLambdaCodeAllowed() {
|
||||
return config != null && config.services().cloudformation().allowStubLambdaCode();
|
||||
}
|
||||
|
||||
private LambdaCodeSpec resolveLambdaCode(JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String handler, String runtime) {
|
||||
if (props != null && props.has("Code")) {
|
||||
@@ -2420,13 +2433,30 @@ public class CloudFormationResourceProvisioner {
|
||||
String s3Bucket = codeNode.path("S3Bucket").asText(null);
|
||||
String s3Key = codeNode.path("S3Key").asText(null);
|
||||
if (s3Bucket != null && s3Key != null) {
|
||||
// A template that names its code explicitly must fail if that code cannot be
|
||||
// read, the way real CloudFormation does. Substituting the stub handler here
|
||||
// let a stack reach CREATE_COMPLETE running code the template never referenced
|
||||
// — or, when the handler was not "index.handler", fail with a handler error
|
||||
// that pointed away from the real problem (issue #2648). The stub below is for
|
||||
// a template that supplies no Code at all, which is a different case.
|
||||
//
|
||||
// allow-stub-lambda-code opts back in to the old fallback, for a stack that
|
||||
// deliberately leaves its Lambda packages unbuilt and only cares about the
|
||||
// other resources. Off by default: silently serving a placeholder is the more
|
||||
// dangerous of the two behaviours.
|
||||
try {
|
||||
s3Service.getObject(s3Bucket, s3Key);
|
||||
return new LambdaCodeSpec(Map.of("S3Bucket", s3Bucket, "S3Key", s3Key),
|
||||
"s3:" + s3Bucket + "\n" + s3Key);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("S3 code not found for Lambda ({0}/{1}), using default handler: {2}",
|
||||
s3Bucket, s3Key, e.getMessage());
|
||||
if (!stubLambdaCodeAllowed()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Error occurred while GetObject. S3 Error Message: " + e.getMessage()
|
||||
+ " (bucket: " + s3Bucket + ", key: " + s3Key + ")", 400);
|
||||
}
|
||||
LOG.warnv("S3 code not found for Lambda ({0}/{1}), using default handler because "
|
||||
+ "floci.services.cloudformation.allow-stub-lambda-code is enabled: {2}",
|
||||
s3Bucket, s3Key, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -371,6 +371,9 @@ floci:
|
||||
timeout-sweep-interval-seconds: 1
|
||||
cloudformation:
|
||||
enabled: true
|
||||
# Fail an AWS::Lambda::Function whose S3 code cannot be read, as real CloudFormation
|
||||
# does. Set true to fall back to the built-in stub handler instead (issue #2648).
|
||||
allow-stub-lambda-code: false
|
||||
acm:
|
||||
enabled: true
|
||||
validation-wait-seconds: 0
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+110
@@ -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")));
|
||||
}
|
||||
}
|
||||
+62
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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() {
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -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() {
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -213,6 +213,9 @@ floci:
|
||||
timeout-sweep-interval-seconds: 1
|
||||
cloudformation:
|
||||
enabled: true
|
||||
# Fail an AWS::Lambda::Function whose S3 code cannot be read, as real CloudFormation
|
||||
# does. Set true to fall back to the built-in stub handler instead (issue #2648).
|
||||
allow-stub-lambda-code: false
|
||||
acm:
|
||||
enabled: true
|
||||
validation-wait-seconds: 0
|
||||
|
||||
Reference in new issue
Block a user