fix(cloudformation): resolve SAM AutoPublishAlias references (#7048)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-30 23:04:51 -04:00
1 parent d3543aca7e
commit 5f327da1c8
3 files changed
+167 -2

No files matched your search

@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
@@ -28,6 +30,7 @@ import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
class SamTransformIntegrationTest {
@@ -109,6 +112,73 @@ class SamTransformIntegrationTest {
assertThat(resourcesXml, not(containsString("AWS::Serverless::Function")));
}
@Test
void samAutoPublishAliasReferenceCreatesLambdaPermission() throws Exception {
String stackName = "sam-ref-alias-" + Long.toString(System.nanoTime(), 36);
String functionName = stackName + "-fn";
stacksToDelete.add(stackName);
String template = """
Transform: AWS::Serverless-2016-10-31
Resources:
Fn:
Type: AWS::Serverless::Function
Properties:
FunctionName: %s
Runtime: nodejs22.x
Handler: index.handler
InlineCode: "exports.handler = async () => ({ ok: true });"
AutoPublishAlias: live
FnPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref Fn.Alias
Principal: events.amazonaws.com
Outputs:
AliasArn:
Value: !Ref Fn.Alias
""".formatted(functionName);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", stackName)
.formParam("TemplateBody", template)
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
.when()
.post("/")
.then()
.statusCode(200);
waitForStackStatus(stackName, "CREATE_COMPLETE");
String stackXml = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.extract().asString();
assertThat(stackXml, containsString(":function:" + functionName + ":live"));
String functionArn = given().when().get("/2015-03-31/functions/" + functionName)
.then().statusCode(200).extract().path("Configuration.FunctionArn");
String policyJson = given()
.when()
.get("/2015-03-31/functions/" + functionName + "/policy?Qualifier=live")
.then()
.statusCode(200)
.extract().path("Policy");
JsonNode statements = new ObjectMapper().readTree(policyJson).path("Statement");
assertEquals(1, statements.size());
JsonNode statement = statements.get(0);
assertEquals("lambda:InvokeFunction", statement.path("Action").asText());
assertEquals("events.amazonaws.com", statement.path("Principal").path("Service").asText());
assertEquals(functionArn + ":live", statement.path("Resource").asText());
}
@Test
@Tag("docker")
void samFunction_withAutoPublishAlias_createsVersionAndAlias() throws Exception {
@@ -154,6 +154,77 @@ class SamTransformProcessorTest {
aliasProps.path("FunctionVersion").path("Fn::GetAtt"));
}
@Test
void expandSamTemplate_autoPublishAliasResolvesReferenceableProperty() throws Exception {
JsonNode template = objectMapper.readTree("""
{
"Transform": "AWS::Serverless-2016-10-31",
"Resources": {
"FnPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {"Ref": "Fn.Alias"},
"Principal": "events.amazonaws.com"
}
},
"Fn": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"Runtime": "nodejs22.x",
"AutoPublishAlias": "live",
"InlineCode": "exports.handler = async () => ({});"
}
}
},
"Outputs": {
"AliasArn": {"Value": {"Ref": "Fn.Alias"}},
"FunctionName": {"Value": {"Ref": "Fn"}}
}
}
""");
JsonNode expanded = processor.expandSamTemplate(template);
assertEquals("FnAliasLive", expanded.path("Resources").path("FnPermission")
.path("Properties").path("FunctionName").path("Ref").asText());
assertEquals("FnAliasLive", expanded.path("Outputs").path("AliasArn")
.path("Value").path("Ref").asText());
assertEquals("Fn", expanded.path("Outputs").path("FunctionName")
.path("Value").path("Ref").asText());
}
@Test
void expandSamTemplate_autoPublishAliasReferenceUsesGeneratedIdAfterCollision() throws Exception {
JsonNode template = objectMapper.readTree("""
{
"Transform": "AWS::Serverless-2016-10-31",
"Resources": {
"FnAliasLive": {"Type": "AWS::S3::Bucket"},
"Fn": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"Runtime": "nodejs22.x",
"AutoPublishAlias": "live",
"InlineCode": "exports.handler = async () => ({});"
}
},
"FnPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {"FunctionName": {"Ref": "Fn.Alias"}}
}
}
}
""");
JsonNode resources = processor.expandSamTemplate(template).path("Resources");
assertEquals("AWS::S3::Bucket", resources.path("FnAliasLive").path("Type").asText());
assertEquals("AWS::Lambda::Alias", resources.path("FnAliasLive2").path("Type").asText());
assertEquals("FnAliasLive2", resources.path("FnPermission").path("Properties")
.path("FunctionName").path("Ref").asText());
}
/** The ids are the ones SAM gives the same function. */
@Test
void expandSamTemplate_autoPublishAliasVersionIdFollowsSamsHashInput() throws Exception {