mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): copy a SAM function's VersionDescription to its version (#6770)
With AutoPublishAlias, SAM passes a function's VersionDescription to the Description of the AWS::Lambda::Version it generates. The transform dropped it, so a version published from a SAM template never had the description the template gave it. The version now takes it as written, leaving an intrinsic function for CloudFormation to resolve. As in SAM, it stays out of the hash that names the version, and Globals cannot set it: the transform refuses it there, as it already refuses AutoPublishCodeSha256. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
1a5decbb4a
commit
a6cf29ea86
5 files changed
+343
-10
No files matched your search
+141
@@ -20,6 +20,7 @@ import java.util.zip.ZipOutputStream;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.emptyOrNullString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
@@ -527,6 +528,146 @@ class SamTransformIntegrationTest {
|
||||
.body("__type", equalTo("ProvisionedConcurrencyConfigNotFoundException"));
|
||||
}
|
||||
|
||||
/**
|
||||
* VersionDescription describes the version the alias publishes, resolved as CloudFormation
|
||||
* resolves it, and not the function; each version keeps the description it was published with.
|
||||
*/
|
||||
@Test
|
||||
void samVersionDescriptionDescribesEachPublishedVersion() {
|
||||
String stackName = "sam-version-description-stack";
|
||||
String function = "sam-version-description-func";
|
||||
stacksToDelete.add(stackName);
|
||||
String template = """
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Parameters:
|
||||
Release:
|
||||
Type: String
|
||||
Resources:
|
||||
Function:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
FunctionName: sam-version-description-func
|
||||
Handler: index.handler
|
||||
Runtime: nodejs20.x
|
||||
AutoPublishAlias: live
|
||||
VersionDescription:
|
||||
Fn::Sub: release ${Release}
|
||||
InlineCode: "exports.handler = async () => ({ release: '%s' });"
|
||||
""";
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("first"))
|
||||
.formParam("Parameters.member.1.ParameterKey", "Release")
|
||||
.formParam("Parameters.member.1.ParameterValue", "1")
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "CREATE_COMPLETE");
|
||||
given().queryParam("Qualifier", "live")
|
||||
.when().get("/2015-03-31/functions/" + function + "/configuration")
|
||||
.then().statusCode(200).body("Version", equalTo("1")).body("Description", equalTo("release 1"));
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("second"))
|
||||
.formParam("Parameters.member.1.ParameterKey", "Release")
|
||||
.formParam("Parameters.member.1.ParameterValue", "2")
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "UPDATE_COMPLETE");
|
||||
|
||||
given().queryParam("Qualifier", "live")
|
||||
.when().get("/2015-03-31/functions/" + function + "/configuration")
|
||||
.then().statusCode(200).body("Version", equalTo("2")).body("Description", equalTo("release 2"));
|
||||
given().when().get("/2015-03-31/functions/" + function + "/versions")
|
||||
.then().statusCode(200)
|
||||
.body("Versions.Version", equalTo(List.of("$LATEST", "1", "2")))
|
||||
.body("Versions.find { it.Version == '1' }.Description", equalTo("release 1"))
|
||||
.body("Versions.find { it.Version == '$LATEST' }.Description", emptyOrNullString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Under AutoPublishAliasAllProperties a Tags change names a new id but publishes nothing, since
|
||||
* the function has not changed. A new VersionDescription alone keeps the id: the change set
|
||||
* reports the version replaced, but floci keeps it with its first description, where AWS fails
|
||||
* the update. The next new id, from another Tags change, publishes with the new description,
|
||||
* which no longer matches the last version's.
|
||||
*/
|
||||
@Test
|
||||
void samVersionDescriptionAloneKeepsThePublishedVersion() {
|
||||
String stackName = "sam-version-description-only-stack";
|
||||
String function = "sam-version-description-only-func";
|
||||
stacksToDelete.add(stackName);
|
||||
String template = """
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Resources:
|
||||
Function:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
FunctionName: sam-version-description-only-func
|
||||
Handler: index.handler
|
||||
Runtime: nodejs20.x
|
||||
AutoPublishAlias: live
|
||||
AutoPublishAliasAllProperties: true
|
||||
VersionDescription: %s
|
||||
Tags:
|
||||
build: %s
|
||||
InlineCode: "exports.handler = async () => ({});"
|
||||
""";
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("release 1", "a"))
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "CREATE_COMPLETE");
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("release 1", "b"))
|
||||
.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("1"));
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateChangeSet").formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "described").formParam("ChangeSetType", "UPDATE")
|
||||
.formParam("TemplateBody", template.formatted("release 2", "b"))
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
String changeSet = given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet").formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "described")
|
||||
.when().post("/").then().statusCode(200).extract().asString();
|
||||
List<String> versionChanges = XmlParser.extractGroups(changeSet, "ResourceChange").stream()
|
||||
.filter(change -> change.get("LogicalResourceId").startsWith("FunctionVersion"))
|
||||
.map(change -> change.get("Action") + " " + change.get("Replacement"))
|
||||
.toList();
|
||||
assertThat(versionChanges, equalTo(List.of("Modify True")));
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ExecuteChangeSet").formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", "described")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "UPDATE_COMPLETE");
|
||||
given().when().get("/2015-03-31/functions/" + function + "/versions")
|
||||
.then().statusCode(200)
|
||||
.body("Versions.Version", equalTo(List.of("$LATEST", "1")))
|
||||
.body("Versions.find { it.Version == '1' }.Description", equalTo("release 1"));
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("release 2", "c"))
|
||||
.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("2"));
|
||||
given().when().get("/2015-03-31/functions/" + function + "/versions")
|
||||
.then().statusCode(200)
|
||||
.body("Versions.find { it.Version == '1' }.Description", equalTo("release 1"))
|
||||
.body("Versions.find { it.Version == '2' }.Description", equalTo("release 2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void samFunction_withoutAutoPublishAlias_createsNoAlias() {
|
||||
String stackName = "sam-no-alias-stack";
|
||||
|
||||
+130
@@ -578,6 +578,136 @@ class SamTransformProcessorTest {
|
||||
+ "'Function'.", refused.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* SAM gives the version the function's VersionDescription as its Description, as written, so
|
||||
* CloudFormation resolves an intrinsic function in it. The function does not take it, and
|
||||
* without AutoPublishAlias there is no version to take it.
|
||||
*/
|
||||
@Test
|
||||
void expandSamTemplate_versionDescriptionDescribesTheVersion() throws Exception {
|
||||
String template = """
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Parameters": {"Release": {"Type": "String", "Default": "1"}},
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Handler": "index.handler", "Runtime": "nodejs20.x",%s
|
||||
"InlineCode": "exports.handler = async () => ({});", "VersionDescription": %s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
String alias = " \"AutoPublishAlias\": \"live\",";
|
||||
|
||||
JsonNode resources = processor.expandSamTemplate(objectMapper.readTree(
|
||||
template.formatted(alias, "\"first release\""))).path("Resources");
|
||||
assertEquals("first release",
|
||||
resources.path("MyFuncVersion102360dfc3").path("Properties").path("Description").asText());
|
||||
assertFalse(resources.path("MyFunc").path("Properties").has("Description"), "the function takes no description");
|
||||
assertEquals("{\"Fn::Sub\":\"release ${Release}\"}", versionProperties(template.formatted(alias,
|
||||
"{\"Fn::Sub\": \"release ${Release}\"}")).path("Description").toString(),
|
||||
"an intrinsic function is copied unresolved, as SAM copies it");
|
||||
assertEquals("\"\"", versionProperties(template.formatted(alias, "\"\"")).path("Description").toString(),
|
||||
"SAM keeps an empty value");
|
||||
assertFalse(versionProperties(template.formatted(alias, "null")).has("Description"),
|
||||
"SAM drops a null value");
|
||||
|
||||
JsonNode unpublished = processor.expandSamTemplate(objectMapper.readTree(
|
||||
template.formatted("", "\"first release\""))).path("Resources");
|
||||
assertTrue(unpublished.has("MyFuncRole"));
|
||||
assertEquals(2, unpublished.size(), "only the function and its role: " + unpublished);
|
||||
assertFalse(unpublished.path("MyFunc").path("Properties").has("Description"));
|
||||
}
|
||||
|
||||
/** The version resource SAM's translator gives this function, id and Description included. */
|
||||
@Test
|
||||
void expandSamTemplate_versionDescriptionGivesSamsVersionResource() throws Exception {
|
||||
JsonNode resources = processor.expandSamTemplate(objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Resources": {
|
||||
"MinimalFunction": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"CodeUri": "s3://sam-demo-bucket/hello.zip", "Handler": "hello.handler",
|
||||
"Runtime": "python2.7", "AutoPublishAlias": "live", "VersionDescription": "sam-testing"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""")).path("Resources");
|
||||
|
||||
assertEquals(objectMapper.readTree("""
|
||||
{"Type": "AWS::Lambda::Version", "DeletionPolicy": "Retain",
|
||||
"Properties": {"Description": "sam-testing", "FunctionName": {"Ref": "MinimalFunction"}}}
|
||||
"""), resources.path("MinimalFunctionVersion640128d35d"), resources.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* SAM leaves VersionDescription out of the version's id, by default, under
|
||||
* AutoPublishAliasAllProperties and beside a given AutoPublishCodeSha256, so a new one alone
|
||||
* names no new version. The version takes it without the AutoPublishCodeSha256.
|
||||
*/
|
||||
@Test
|
||||
void expandSamTemplate_versionDescriptionIsNotPartOfTheVersionId() throws Exception {
|
||||
String template = """
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Handler": "index.handler", "Runtime": "nodejs20.x", "AutoPublishAlias": "live",
|
||||
"InlineCode": "exports.handler = async () => ({});"%s%s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
String described = ", \"VersionDescription\": \"first release\"";
|
||||
String codeSha256 = ", \"AutoPublishCodeSha256\": \"abc123\"";
|
||||
|
||||
for (String versionProperty : List.of("", ", \"AutoPublishAliasAllProperties\": true", codeSha256)) {
|
||||
assertEquals(publishedVersionId(template.formatted(versionProperty, "")),
|
||||
publishedVersionId(template.formatted(versionProperty, described)), versionProperty);
|
||||
}
|
||||
assertEquals("MyFuncVersion102360dfc3", publishedVersionId(template.formatted("", described)));
|
||||
assertEquals("first release", processor.expandSamTemplate(objectMapper.readTree(
|
||||
template.formatted(codeSha256, described))).path("Resources").path("MyFuncVersionabc123")
|
||||
.path("Properties").path("Description").asText());
|
||||
}
|
||||
|
||||
/** SAM's Globals do not take VersionDescription either, which would describe every function's version. */
|
||||
@Test
|
||||
void expandSamTemplate_globalsRefuseVersionDescription() throws Exception {
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Globals": {"Function": {"AutoPublishAlias": "live", "VersionDescription": "every release"}},
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {"Handler": "index.handler", "Runtime": "nodejs20.x",
|
||||
"InlineCode": "exports.handler = async () => ({});"}
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class, () -> processor.expandSamTemplate(template));
|
||||
assertEquals("ValidationError", refused.getErrorCode());
|
||||
assertEquals("'Globals' section is invalid. 'VersionDescription' is not a supported property of "
|
||||
+ "'Function'.", refused.getMessage());
|
||||
}
|
||||
|
||||
private JsonNode versionProperties(String template) throws Exception {
|
||||
return processor.expandSamTemplate(objectMapper.readTree(template)).path("Resources")
|
||||
.path("MyFuncVersion102360dfc3").path("Properties");
|
||||
}
|
||||
|
||||
/**
|
||||
* SAM retains each version it publishes unless the function sets a {@code DeletionPolicy}, which
|
||||
* the version and alias both take, or a {@code VersionDeletionPolicy}; a function on a capacity
|
||||
|
||||
Reference in new issue
Block a user