fix(cloudformation): send the stack's StackId to custom resources (#4861)

AWS documents StackId in every Create, Update and Delete request as the
ARN of the stack that contains the custom resource, and handlers rely on
it. #4973 now passes the engine's stack id and stashes it for Delete, but
an engine with no stack ARN still leaks a non-ARN: Cloud Control's
standalone engine uses the stack name as its id, so its custom resources
got StackId "cloudcontrol".

A stack id is now minted when there is no stack ARN to send. A resource
created through Cloud Control has its minted id stashed at Create, so its
Delete repeats it. A resource stashed by an earlier release with no
__FlociStackId, or with a non-ARN one, gets a freshly minted ARN on
Delete, because the delete path does not reach the stack.

An integration test drives Create, Update and Delete through a mocked
handler and checks the event's StackId against Ref AWS::StackId and
DescribeStacks, including a nested stack's own id, and the docs say so.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-08 14:43:01 -04:00
1 parent ebd364e23e
commit f248eb51af
4 files changed
+276 -5

No files matched your search

+3
View File
@@ -404,6 +404,9 @@ Lambda. floci supports two shapes:
event and waits for it to `PUT` its `SUCCESS`/`FAILED` result to the response URL. Inline `ZipFile`
handlers get the `cfn-response` / `cfnresponse` module bundled in (see the Lambda row in
[Supported Resource Types](#supported-resource-types)), so Solutions-style handlers work unmodified.
The event's `StackId` is the id of the stack that contains the resource, the value
`Ref AWS::StackId` returns and `DescribeStacks` reports, on `Create`, `Update` and `Delete` alike.
In a nested stack it is the nested stack's own id.
- **CDK Provider framework** — when the `ServiceToken` points at a CDK `framework.onEvent` function,
floci drives the asynchronous provider protocol: `onEvent` starts the work and `framework.isComplete`
is polled (via Step Functions [`Retry`](step-functions.md)) until it reports done, at which point the
@@ -116,9 +116,10 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
return;
}
String stackId = stackIdOrMinted(engine.stackId(), region, ctx.accountId(), ctx.stackName());
JsonNode response = invokeCustomResourceHandler(serviceToken, requestType, r.getLogicalId(),
r.getResourceType(), priorPhysicalId, resourceProperties, oldResourceProperties,
region, engine.stackId());
region, stackId);
String status = response.path("Status").asText("FAILED");
if (!"SUCCESS".equals(status)) {
@@ -146,7 +147,7 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
// Stash what a later Delete invocation needs (delete() only gets the StackResource).
r.getAttributes().put(CR_SERVICE_TOKEN_ATTR, serviceToken);
r.getAttributes().put(CR_PROPERTIES_ATTR, resourceProperties.toString());
r.getAttributes().put(CR_STACK_ID_ATTR, engine.stackId());
r.getAttributes().put(CR_STACK_ID_ATTR, stackId);
}
@Override
@@ -158,10 +159,10 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
}
ObjectNode stashed = readStashedProperties(r);
ObjectNode resourceProperties = stashed != null ? stashed : objectMapper.createObjectNode();
// A resource stashed before the stack id was recorded has none, and delete() has no stack.
String stackId = stackIdOrMinted(r.getAttributes().get(CR_STACK_ID_ATTR), region,
accountFromArn(serviceToken), "");
try {
// Resources stashed before the stack id was recorded fall back to a synthetic id.
String stackId = r.getAttributes().getOrDefault(CR_STACK_ID_ATTR, AwsArnUtils.Arn.of(
"cloudformation", region, accountFromArn(serviceToken), "stack//" + UUID.randomUUID()).toString());
JsonNode response = invokeCustomResourceHandler(serviceToken, "Delete", r.getLogicalId(),
r.getResourceType(), r.getPhysicalId(), resourceProperties, null, region, stackId);
if (!"SUCCESS".equals(response.path("Status").asText("FAILED"))) {
@@ -265,6 +266,19 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
return objectMapper.getNodeFactory().textNode(node.asText());
}
/**
* The {@code StackId} a request carries: the id of the stack that contains the resource, the
* value {@code Ref AWS::StackId} returns. A resource with no stack ARN, one provisioned through
* Cloud Control or one stashed before the id was recorded, gets one minted in its place.
*/
private static String stackIdOrMinted(String stackId, String region, String accountId, String stackName) {
if (AwsArnUtils.isArn(stackId)) {
return stackId;
}
return AwsArnUtils.Arn.of("cloudformation", region, accountId, "stack/"
+ (stackName == null ? "" : stackName) + "/" + UUID.randomUUID()).toString();
}
private static String accountFromArn(String arn) {
String account = AwsArnUtils.accountOrDefault(arn, "000000000000");
return account.matches("\\d{12}") ? account : "000000000000";
@@ -0,0 +1,206 @@
package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
/**
* Every request CloudFormation sends a custom resource carries the {@code StackId} of the stack that
* contains it, the value {@code Ref AWS::StackId} returns. Handlers use it to tie what they create to
* the owning stack, so a made-up id breaks them.
*
* <p>The ServiceToken Lambda is mocked, as {@code SecretsManagerRotationIntegrationTest} mocks its
* rotation Lambda, so no Docker is needed. The mock answers like a real handler: it PUTs its result to
* the event's {@code ResponseURL}, returning the event's {@code StackId} as {@code Data}.
*/
@QuarkusTest
class CloudFormationCustomResourceStackIdIntegrationTest {
private static final String SERVICE_TOKEN =
"arn:aws:lambda:us-east-1:000000000000:function:stack-id-probe";
private final ObjectMapper mapper = new ObjectMapper();
private final List<JsonNode> events = new CopyOnWriteArrayList<>();
@InjectMock
LambdaService lambdaService;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@BeforeEach
void stubHandler() {
when(lambdaService.invoke(any(), eq(SERVICE_TOKEN), any(), eq(InvocationType.RequestResponse)))
.thenAnswer(inv -> {
JsonNode event = mapper.readTree((byte[]) inv.getArgument(2));
events.add(event);
putResponse(event);
return new InvokeResult(200, null, "null".getBytes(StandardCharsets.UTF_8), null, "req-1");
});
}
@Test
void customResourceRequestsCarryTheStacksOwnStackId() throws InterruptedException {
String stackName = "cr-stack-id-" + Long.toString(System.nanoTime(), 36);
String stackId = createStack(stackName, probeTemplate("1"));
String created = awaitStackStatus(stackId, "CREATE_COMPLETE");
assertEquals(stackId, XmlParser.extractFirst(created, "StackId", null));
assertEquals(stackId, outputValue(created, "RefStackId"));
assertEquals(stackId, outputValue(created, "EventStackId"));
updateStack(stackName, probeTemplate("2"));
String updated = awaitStackStatus(stackId, "UPDATE_COMPLETE");
assertEquals(stackId, outputValue(updated, "EventStackId"));
deleteStack(stackName);
awaitStackStatus(stackId, "DELETE_COMPLETE");
assertEquals(List.of("Create", "Update", "Delete"),
events.stream().map(e -> e.get("RequestType").asText()).toList());
for (JsonNode event : events) {
assertEquals(stackId, event.get("StackId").asText(), event.toString());
}
// What a handler compares: the property built from Ref AWS::StackId against the event.
assertEquals(events.get(0).get("StackId").asText(),
events.get(0).get("ResourceProperties").get("OwningStackArn").asText());
}
@Test
void customResourceInANestedStackGetsTheNestedStacksStackId() throws InterruptedException {
String suffix = Long.toString(System.nanoTime(), 36);
String bucket = "cr-stack-id-templates-" + suffix;
String childKey = "child-" + suffix + ".json";
given().when().put("/" + bucket).then().statusCode(200);
given().contentType("application/json").body(probeTemplate("1"))
.when().put("/" + bucket + "/" + childKey).then().statusCode(200);
String parentTemplate = """
{
"Resources": {
"Child": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {"TemplateURL": "http://localhost/%s/%s"}
}
},
"Outputs": {
"ChildStackId": {"Value": {"Ref": "Child"}},
"ChildEventStackId": {"Value": {"Fn::GetAtt": ["Child", "Outputs.EventStackId"]}}
}
}
""".formatted(bucket, childKey);
String parentStackId = createStack("cr-nested-stack-id-" + suffix, parentTemplate);
String parent = awaitStackStatus(parentStackId, "CREATE_COMPLETE");
String childStackId = outputValue(parent, "ChildStackId");
assertNotEquals(parentStackId, childStackId);
assertEquals(childStackId, outputValue(parent, "ChildEventStackId"));
String child = awaitStackStatus(childStackId, "CREATE_COMPLETE");
assertEquals(childStackId, outputValue(child, "RefStackId"));
assertEquals(1, events.size());
assertEquals(childStackId, events.get(0).get("StackId").asText());
}
private static String probeTemplate(String revision) {
return """
{
"Resources": {
"Probe": {
"Type": "Custom::StackIdProbe",
"Properties": {
"ServiceToken": "%s",
"OwningStackArn": {"Ref": "AWS::StackId"},
"Revision": "%s"
}
}
},
"Outputs": {
"EventStackId": {"Value": {"Fn::GetAtt": ["Probe", "EventStackId"]}},
"RefStackId": {"Value": {"Ref": "AWS::StackId"}}
}
}
""".formatted(SERVICE_TOKEN, revision);
}
/** Answers the request the way a handler does: a PUT of its result document to ResponseURL. */
private void putResponse(JsonNode event) {
ObjectNode response = mapper.createObjectNode();
response.put("Status", "SUCCESS");
response.put("RequestId", event.get("RequestId").asText());
response.put("StackId", event.get("StackId").asText());
response.put("LogicalResourceId", event.get("LogicalResourceId").asText());
response.put("PhysicalResourceId", "stack-id-probe");
response.putObject("Data").put("EventStackId", event.get("StackId").asText());
String path = URI.create(event.get("ResponseURL").asText()).getPath();
given().body(response.toString()).when().put(path).then().statusCode(200);
}
private static String createStack(String stackName, String template) {
String xml = cfn(Map.of("Action", "CreateStack", "StackName", stackName, "TemplateBody", template));
return XmlParser.extractFirst(xml, "StackId", null);
}
private static void updateStack(String stackName, String template) {
cfn(Map.of("Action", "UpdateStack", "StackName", stackName, "TemplateBody", template));
}
private static void deleteStack(String stackName) {
cfn(Map.of("Action", "DeleteStack", "StackName", stackName));
}
/** Polls DescribeStacks by stack id, which also answers for a deleted stack, and returns its XML. */
private static String awaitStackStatus(String stackId, String status) throws InterruptedException {
String xml = "";
long deadline = System.currentTimeMillis() + 10_000;
while (System.currentTimeMillis() < deadline) {
xml = cfn(Map.of("Action", "DescribeStacks", "StackName", stackId));
if (xml.contains("<StackStatus>" + status + "</StackStatus>")) {
return xml;
}
Thread.sleep(50);
}
fail("stack " + stackId + " never reached " + status + ": " + xml);
return xml;
}
private static String cfn(Map<String, String> params) {
return given()
.contentType("application/x-www-form-urlencoded")
.formParams(params)
.when()
.post("/")
.then()
.statusCode(200)
.extract().asString();
}
private static String outputValue(String xml, String key) {
return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key);
}
}
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnResourceDispatcher;
@@ -13,6 +14,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@@ -229,6 +232,51 @@ class CustomResourceProvisionerTest {
assertEquals("hi", deleteEvent.get("ResourceProperties").get("Message").asText());
}
@Test
void resourceOutsideAStackGetsOneStackIdForCreateAndDelete() {
stubHandler("phys-123", Map.of());
// Cloud Control's engine has no stack, so its stack id is not an ARN.
CloudFormationTemplateEngine standalone = CloudFormationTemplateEngine.standalone(
"000000000000", "us-east-1", "cloudcontrol", mapper, null);
StackResource r = provisioner.provision("resource", "Custom::Test", props(),
standalone, "us-east-1", "000000000000", "cloudcontrol");
provisioner.delete(r, "us-east-1");
List<JsonNode> events = capturedEvents(2);
String createStackId = events.get(0).get("StackId").asText();
assertTrue(AwsArnUtils.isArnFor(createStackId, "cloudformation"), createStackId);
assertEquals(createStackId, events.get(1).get("StackId").asText());
}
@Test
void deleteOfAResourceWithNoRecordedStackIdStillSendsAStackArn() {
stubHandler("phys-123", Map.of());
// Stashed by a release that did not record the stack id.
StackResource legacy = new StackResource();
legacy.setLogicalId("MyCr");
legacy.setResourceType("Custom::Test");
legacy.setPhysicalId("phys-123");
legacy.getAttributes().put("__FlociServiceToken", SERVICE_TOKEN);
legacy.getAttributes().put("__FlociResourceProperties", props().toString());
provisioner.delete(legacy, "us-east-1");
String stackId = capturedEvents(1).get(0).get("StackId").asText();
assertTrue(AwsArnUtils.isArnFor(stackId, "cloudformation"), stackId);
}
private List<JsonNode> capturedEvents(int count) {
ArgumentCaptor<byte[]> payloads = ArgumentCaptor.forClass(byte[].class);
verify(lambdaService, times(count)).invoke(any(), eq(SERVICE_TOKEN), payloads.capture(),
eq(InvocationType.RequestResponse));
List<JsonNode> events = new ArrayList<>();
for (byte[] payload : payloads.getAllValues()) {
events.add(readEvent(() -> payload));
}
return events;
}
private JsonNode readEvent(ArgumentCaptor<byte[]> captor) {
return readEvent(captor::getValue);
}