Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): append AutoPublishCodeSha256 to a SAM function's Description (#6364)

Jackson Price9c6d6a0ce8ea
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index 6ae57a016a..3b57c33806 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -449,7 +449,14 @@ hash covers every property floci gives the function, its code's parameter refere
above, plus its `SnapStart`; those properties differ from the ones SAM generates, so these ids are
floci's own rather than SAM's. An `AutoPublishCodeSha256` that is a string, or a `Ref` to a
parameter holding one, replaces the hash with its first ten characters, under
-`AutoPublishAliasAllProperties` too; an empty value leaves the hash in place. `Globals` can set
+`AutoPublishAliasAllProperties` too; an empty value leaves the hash in place. As in SAM, a given
+`AutoPublishCodeSha256` is also appended to the function's `Description`, after a space, so the
+version a new value names publishes even when nothing else changed. The description's parameter
+references are resolved first, one that is another intrinsic function is joined with the value by
+`Fn::Join`, and a function without a description gets the value alone. An empty
+`AutoPublishCodeSha256` appends nothing, but SAM checks the property as written, so a `Ref` to a
+parameter holding an empty value still appends it: a description then ends in a space, and a
+function without one gets an empty `Description`. `Globals` can set
`AutoPublishAliasAllProperties` and `VersionDeletionPolicy`, but as in SAM not
`AutoPublishCodeSha256`, which fails the transform there. floci does not honour a resource's
`IgnoreGlobals`, so no function can opt out of a `VersionDeletionPolicy` in `Globals` with it: one
@@ -462,8 +469,6 @@ configuration changed since its last version. Otherwise the new version resource
version and the alias stays where it is, as when an update changes only one of these:
- A change to `SnapStart`: floci does not give the function `SnapStart`.
-- A new `AutoPublishCodeSha256` alone: on AWS this publishes, because SAM appends it to the
- function's `Description`, but floci's transform sets no `Description` on the function.
- Under `AutoPublishAliasAllProperties`, a change to `Tags` or `ReservedConcurrentExecutions`
only: AWS fails that update with
`A version for this Lambda function exists ( N ). Modify the function to create a new version.`,
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java
index e72327a18f..bafdaf56fe 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java
@@ -960,7 +960,7 @@ class SamTransformProcessor {
resources.set(logicalId, lambdaResource);
String aliasLogicalId = expandAutoPublishAlias(logicalId, properties,
- lambdaResource.path("Properties"), passthrough, template, parameterValues, resources);
+ (ObjectNode) lambdaResource.path("Properties"), passthrough, template, parameterValues, resources);
List<ObjectNode> eventInvokePolicies = expandEventInvokeConfig(
logicalId, properties, aliasLogicalId, passthrough, template, resources, parameterValues);
@@ -999,7 +999,7 @@ class SamTransformProcessor {
* which publishes, and the alias follows it; a given {@code AutoPublishCodeSha256} fixes the id
* instead.
*/
- private String expandAutoPublishAlias(String functionLogicalId, JsonNode properties, JsonNode functionProperties,
+ private String expandAutoPublishAlias(String functionLogicalId, JsonNode properties, ObjectNode functionProperties,
ObjectNode passthrough, JsonNode template,
Map<String, String> parameterValues, ObjectNode resources) {
validateVersionProperties(functionLogicalId, properties);
@@ -1009,6 +1009,7 @@ class SamTransformProcessor {
return null;
}
+ appendCodeSha256ToDescription(functionLogicalId, properties, functionProperties, template, parameterValues);
String versionId = uniqueId(
versionLogicalId(functionLogicalId, properties, functionProperties, template, parameterValues),
resources);
@@ -1042,6 +1043,35 @@ class SamTransformProcessor {
return aliasId;
}
+ /**
+ * Appends a given {@code AutoPublishCodeSha256} to the function's {@code Description}, after a
+ * space, as SAM does. Lambda publishes no version of a function that has not changed since its
+ * last one, so without this a new value for the same code names a new version resource that
+ * publishes nothing, and the alias stays where it was. Like SAM, it resolves the description's
+ * template parameters first and joins any other intrinsic function with the value through
+ * {@code Fn::Join}. SAM tests the property as written, not the value it resolves to, so a
+ * {@code Ref} to an empty value appends an empty value.
+ */
+ private void appendCodeSha256ToDescription(String functionLogicalId, JsonNode properties,
+ ObjectNode functionProperties, JsonNode template,
+ Map<String, String> parameterValues) {
+ if (!isTruthy(properties.path("AutoPublishCodeSha256"))) {
+ return;
+ }
+ String codeSha256 = autoPublishCodeSha256(functionLogicalId, properties, template, parameterValues);
+ JsonNode description = resolveParameterRefs(properties.path("Description"), template, parameterValues);
+ if (!isTruthy(description)) {
+ functionProperties.put("Description", codeSha256);
+ } else if (description.isTextual()) {
+ functionProperties.put("Description", description.asText() + " " + codeSha256);
+ } else {
+ ObjectNode joined = objectMapper.createObjectNode();
+ joined.set("Fn::Join", objectMapper.createArrayNode().add(" ")
+ .add(objectMapper.createArrayNode().add(description.deepCopy()).add(codeSha256)));
+ functionProperties.set("Description", joined);
+ }
+ }
+
/**
* SAM's {@code DeletionPolicy} for the version when the function declares none: the function's
* {@code VersionDeletionPolicy}, else {@code Delete} for a function on a capacity provider and
@@ -1629,6 +1659,7 @@ class SamTransformProcessor {
copyIfPresent(properties, "PackageType", lambdaProps);
copyIfPresent(properties, "Handler", lambdaProps);
copyIfPresent(properties, "Runtime", lambdaProps);
+ copyIfPresent(properties, "Description", lambdaProps);
copyIfPresent(properties, "ImageConfig", lambdaProps);
lambdaProps.set("Code", buildLambdaCode(properties));
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformIntegrationTest.java
index 52f1478761..913d46f550 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformIntegrationTest.java
@@ -279,6 +279,7 @@ class SamTransformIntegrationTest {
FunctionName: sam-code-sha-func
Handler: index.handler
Runtime: nodejs20.x
+ Description: Release
AutoPublishAlias: live
AutoPublishCodeSha256: "%s"
InlineCode: "exports.handler = async () => ({ release: '%s' });"
@@ -310,6 +311,20 @@ class SamTransformIntegrationTest {
.then().statusCode(200).body("FunctionVersion", equalTo("2"));
given().when().get("/2015-03-31/functions/" + function + "/versions")
.then().statusCode(200).body("Versions.Version", equalTo(List.of("$LATEST", "1", "2")));
+
+ // A new hash alone publishes too: SAM appends it to the function's Description, so the
+ // function changes even though its code does not.
+ given().contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateStack").formParam("StackName", stackName)
+ .formParam("TemplateBody", template.formatted("cccccccccc", "second"))
+ .formParam("Capabilities.member.1", "CAPABILITY_IAM")
+ .when().post("/").then().statusCode(200);
+ waitForStackStatus(stackName, "UPDATE_COMPLETE");
+ given().when().get("/2015-03-31/functions/" + function + "/aliases/live")
+ .then().statusCode(200).body("FunctionVersion", equalTo("3"));
+ given().queryParam("Qualifier", "live")
+ .when().get("/2015-03-31/functions/" + function + "/configuration")
+ .then().statusCode(200).body("Version", equalTo("3")).body("Description", equalTo("Release cccccccccc"));
}
/** Under AutoPublishAliasAllProperties a property outside the default hash, here Timeout, publishes. */
@@ -636,6 +651,7 @@ class SamTransformIntegrationTest {
Handler: app.handler
Runtime: python3.12
InlineCode: "def handler(event, context): return {'statusCode': 200}"
+ Description: Configured by SAM
Timeout: 30
MemorySize: 256
Environment:
@@ -674,6 +690,7 @@ class SamTransformIntegrationTest {
.body("Configuration.FunctionName", equalTo("sam-configured-func"))
.body("Configuration.Handler", equalTo("app.handler"))
.body("Configuration.Runtime", equalTo("python3.12"))
+ .body("Configuration.Description", equalTo("Configured by SAM"))
.body("Configuration.Timeout", equalTo(30))
.body("Configuration.MemorySize", equalTo(256))
.body("Configuration.Environment.Variables.TABLE_NAME", equalTo("my-table"))
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java
index 55d388c22f..5309fa7429 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java
@@ -357,6 +357,111 @@ class SamTransformProcessorTest {
"only the first ten characters name the version");
}
+ /** As in SAM, the function's Description reaches the Lambda function as written. */
+ @Test
+ void expandSamTemplate_functionDescriptionIsCopiedToTheFunction() throws Exception {
+ String template = """
+ {
+ "Transform": "AWS::Serverless-2016-10-31",
+ "Parameters": {"Purpose": {"Type": "String", "Default": "orders"}},
+ "Resources": {
+ "MyFunc": {
+ "Type": "AWS::Serverless::Function",
+ "Properties": {
+ "Handler": "index.handler", "Runtime": "nodejs20.x",
+ "InlineCode": "exports.handler = async () => ({});"%s
+ }
+ }
+ }
+ }
+ """;
+
+ assertEquals("\"Orders API\"", functionDescription(template.formatted(", \"Description\": \"Orders API\"")));
+ assertEquals("{\"Ref\":\"Purpose\"}", functionDescription(template.formatted(
+ ", \"Description\": {\"Ref\": \"Purpose\"}")), "an intrinsic function is left to CloudFormation");
+ assertEquals("\"Orders API\"", functionDescription(template.formatted(
+ ", \"Description\": \"Orders API\", \"AutoPublishAlias\": \"live\"")),
+ "AutoPublishAlias without AutoPublishCodeSha256 leaves it as written");
+ assertEquals("\"Orders API\"", functionDescription(template.formatted(
+ ", \"Description\": \"Orders API\", \"AutoPublishCodeSha256\": \"abc\"")),
+ "AutoPublishCodeSha256 without AutoPublishAlias leaves it as written");
+ assertTrue(processor.expandSamTemplate(objectMapper.readTree(template.formatted(""))).path("Resources")
+ .path("MyFunc").path("Properties").path("Description").isMissingNode());
+ String allProperties = ", \"AutoPublishAlias\": \"live\", \"AutoPublishAliasAllProperties\": true";
+ assertNotEquals(publishedVersionId(template.formatted(allProperties + ", \"Description\": \"first\"")),
+ publishedVersionId(template.formatted(allProperties + ", \"Description\": \"second\"")),
+ "under AutoPublishAliasAllProperties a new Description names a new version");
+ }
+
+ /**
+ * With AutoPublishAlias, SAM appends a given AutoPublishCodeSha256 to the function's Description, so
+ * that a new value alone changes the function and a version publishes for it. A function without a
+ * description gets the bare hash, a plain string gets the hash after a space, and an intrinsic
+ * function is wrapped in an Fn::Join with the hash.
+ */
+ @Test
+ void expandSamTemplate_autoPublishCodeSha256IsAppendedToTheDescription() throws Exception {
+ String template = """
+ {
+ "Transform": "AWS::Serverless-2016-10-31",
+ "Parameters": {
+ "AutoPublishCodeSha256": {"Type": "String",
+ "Default": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"},
+ "Purpose": {"Type": "String", "Default": "orders"}
+ },
+ "Resources": {
+ "MyFunc": {
+ "Type": "AWS::Serverless::Function",
+ "Properties": {
+ "Handler": "index.handler", "Runtime": "nodejs20.x", "AutoPublishAlias": "live",
+ "InlineCode": "exports.handler = async () => ({});", "AutoPublishCodeSha256": %s%s
+ }
+ }
+ }
+ }
+ """;
+ String sha = "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b";
+ String ref = "{\"Ref\": \"AutoPublishCodeSha256\"}";
+
+ assertEquals("\"" + sha + "\"", functionDescription(template.formatted(ref, "")));
+ assertEquals("\"My testing description " + sha + "\"",
+ functionDescription(template.formatted(ref, ", \"Description\": \"My testing description\"")));
+ assertEquals("{\"Fn::Join\":[\" \",[{\"Fn::Join\":[\":\",[\"a\",\"b\",\"c\"]]},\"" + sha + "\"]]}",
+ functionDescription(template.formatted(ref,
+ ", \"Description\": {\"Fn::Join\": [\":\", [\"a\", \"b\", \"c\"]]}")));
+
+ assertEquals("\"payments abc\"", functionDescription(template.formatted("\"abc\"",
+ ", \"Description\": {\"Ref\": \"Purpose\"}"), Map.of("Purpose", "payments")),
+ "a parameter is resolved first, as SAM resolves it");
+ assertEquals("{\"Fn::Join\":[\" \",[{\"Fn::Sub\":\"payments handler\"},\"abc\"]]}",
+ functionDescription(template.formatted("\"abc\"", ", \"Description\": {\"Fn::Sub\": "
+ + "\"${Purpose} handler\"}"), Map.of("Purpose", "payments")),
+ "any other intrinsic function stays, with its parameters resolved");
+ assertEquals("\"abc\"", functionDescription(template.formatted("\"abc\"", ", \"Description\": \"\"")));
+ assertEquals("\"orders\"", functionDescription(template.formatted("\"\"", ", \"Description\": \"orders\"")),
+ "an empty AutoPublishCodeSha256 appends nothing");
+ assertEquals("{\"Ref\":\"Purpose\"}", functionDescription(template.formatted("\"\"",
+ ", \"Description\": {\"Ref\": \"Purpose\"}"), Map.of("Purpose", "payments")),
+ "with nothing appended the Description stays as written");
+ assertEquals("\"Shared abc\"", functionDescription(template.formatted("\"abc\"", "").replace("\"Resources\"",
+ "\"Globals\": {\"Function\": {\"Description\": \"Shared\"}}, \"Resources\"")),
+ "a Description from Globals takes the value too");
+ assertEquals("\"orders \"", functionDescription(template.formatted(ref, ", \"Description\": \"orders\""),
+ Map.of("AutoPublishCodeSha256", "")),
+ "SAM tests the property as written, so a Ref to an empty value still appends it");
+ assertEquals("\"\"", functionDescription(template.formatted(ref, ""), Map.of("AutoPublishCodeSha256", "")),
+ "and a function without a Description gets an empty one");
+ }
+
+ private String functionDescription(String template) throws Exception {
+ return functionDescription(template, Map.of());
+ }
+
+ private String functionDescription(String template, Map<String, String> parameterValues) throws Exception {
+ return processor.expandSamTemplate(objectMapper.readTree(template), parameterValues).path("Resources")
+ .path("MyFunc").path("Properties").path("Description").toString();
+ }
+
/** SAM's types for the other version properties, and VersionDeletionPolicy needs AutoPublishAlias. */
@Test
void expandSamTemplate_versionPropertiesHaveSamsTypes() throws Exception {