mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(cloudformation): pass Lambda hot-reload code through to the Lambda service (#4093)
* fix(cloudformation): pass Lambda hot-reload code through to the Lambda service
AWS::Lambda::Function code of {S3Bucket: hot-reload, S3Key: /host/path} is
Lambda's bind-mount hot-reload convention: the bucket is a marker and the key
is a directory on the Docker host. The provisioner probed S3 for the pair
before calling CreateFunction, so no such object existed and the stack failed
with a ValidationError (or, with allow-stub-lambda-code on, quietly deployed
the stub handler).
Skip the S3 probe for the hot-reload bucket and hand the Code map to
LambdaService unchanged, which keeps enforcing hot-reload enablement, the
absolute-path rule and the allow-list, and reports its own error as the
resource status reason. Record a hot-reload code identity so a redeploy with
the same path is a no-op, and compare adopted functions by host path since a
hot-reload function carries no S3 bucket or key.
* fix(cloudformation): canonicalise the hot-reload code identity
LambdaService stores a hot-reload host path normalised (#4053), while the
provisioner recorded the raw template key as the code identity and compared
adopted functions against it. A template that spelled the same directory with
a trailing slash or a ".." segment, or a function adopted from a direct
CreateFunction, therefore looked like a code change and triggered an
UpdateFunctionCode that mounted the same directory again.
Build the identity from, and compare against, the normalised path. The raw key
still goes to Lambda so its validation and error messages are unchanged. Also
cover the documented SAM CodeUri form, s3://hot-reload//abs/path, through the
transform.
This commit is contained in:
1 parent
67f06c3ac9
commit
34f4f72e82
7 files changed
+372
-2
No files matched your search
+30
@@ -19,6 +19,8 @@ import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
@@ -48,6 +50,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
private static final String LAYER_VERSION = "AWS::Lambda::LayerVersion";
|
||||
|
||||
private static final String LAMBDA_CODE_IDENTITY_ATTR = "FlociLambdaCodeIdentity";
|
||||
private static final String HOT_RELOAD_BUCKET = "hot-reload";
|
||||
private static final String LAMBDA_NAME_MODE_ATTR = "FlociLambdaFunctionNameMode";
|
||||
private static final String LAMBDA_PACKAGE_TYPE_ATTR = "FlociLambdaPackageType";
|
||||
private static final String NAME_MODE_EXPLICIT = "explicit";
|
||||
@@ -265,6 +268,14 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
String s3Bucket = codeNode.path("S3Bucket").asText(null);
|
||||
String s3Key = codeNode.path("S3Key").asText(null);
|
||||
if (HOT_RELOAD_BUCKET.equals(s3Bucket) && s3Key != null) {
|
||||
// Lambda's bind-mount hot-reload convention: the bucket is a marker and the key
|
||||
// is a directory on the Docker host, so there is no S3 object to probe. Hand the
|
||||
// pair to LambdaService unchanged; it enforces the hot-reload enablement and
|
||||
// path allow-list and reports its own errors, which fail the resource.
|
||||
return new LambdaCodeSpec(Map.of("S3Bucket", s3Bucket, "S3Key", s3Key),
|
||||
"hot-reload:" + canonicalHotReloadPath(s3Key));
|
||||
}
|
||||
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
|
||||
@@ -394,6 +405,21 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The form LambdaService stores a hot-reload host path in: normalized, so {@code /a/../b/}
|
||||
* and {@code /b} are the same mount. Identities and change detection use it so a redeploy
|
||||
* that only rewrites the path spelling, or a function adopted from a direct CreateFunction,
|
||||
* is not mistaken for a code change. The raw key still goes to Lambda, which validates it and
|
||||
* reports its own error for a path it cannot use.
|
||||
*/
|
||||
private static String canonicalHotReloadPath(String s3Key) {
|
||||
try {
|
||||
return Path.of(s3Key).normalize().toString();
|
||||
} catch (InvalidPathException e) {
|
||||
return s3Key;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean lambdaCodeChanged(LambdaFunction fn,
|
||||
LambdaCodeSpec code, String previousIdentity) {
|
||||
if (previousIdentity != null) {
|
||||
@@ -403,6 +429,10 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (request.containsKey("ImageUri")) {
|
||||
return !Objects.equals(fn.getImageUri(), request.get("ImageUri"));
|
||||
}
|
||||
if (HOT_RELOAD_BUCKET.equals(request.get("S3Bucket"))) {
|
||||
return !Objects.equals(fn.getHotReloadHostPath(),
|
||||
canonicalHotReloadPath((String) request.get("S3Key")));
|
||||
}
|
||||
if (request.containsKey("S3Bucket") && request.containsKey("S3Key")) {
|
||||
return !Objects.equals(fn.getS3Bucket(), request.get("S3Bucket"))
|
||||
|| !Objects.equals(fn.getS3Key(), request.get("S3Key"));
|
||||
|
||||
Reference in new issue
Block a user