From 0d74eb5eeb1fe587c09d10fa6baf7a9fa7284730 Mon Sep 17 00:00:00 2001 From: Oleksandr Nikolaichuk <57410473+Sekator778@users.noreply.github.com> Date: Tue, 27 Oct 2026 17:31:02 +0300 Subject: [PATCH] fix(cloudformation): treat an AWS::NoValue property as omitted before provisioning (#6726) --- .../cloudformation/CloudFormationService.java | 4 +- ...rmationNoValueOmissionIntegrationTest.java | 259 ++++++++++++++++++ 2 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationNoValueOmissionIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java index 0868281dcc..d5fac7c417 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java @@ -2012,7 +2012,6 @@ public class CloudFormationService implements ResourceProvider { JsonNode resDef = resources.get(logicalId); String type = resDef.path("Type").asText(); String deletionPolicy = resDef.path("DeletionPolicy").asText(null); - JsonNode props = resDef.path("Properties"); CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine( accountId, region, stack.getStackName(), @@ -2024,6 +2023,9 @@ public class CloudFormationService implements ResourceProvider { parameterTypes, (id, attribute, value) -> dispatcher.attributeValue( resources.path(id).path("Type").asText(), attribute, value)); + // AWS::NoValue removes the property, wherever the template puts it, so no + // provisioner sees it: not a member, a list item or a whole property. + JsonNode props = engine.omitNoValue(resDef.path("Properties")); StackResource resource = stack.getResources().get(logicalId); StackResource previousResource = resource; diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationNoValueOmissionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationNoValueOmissionIntegrationTest.java new file mode 100644 index 0000000000..19d32b6c19 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationNoValueOmissionIntegrationTest.java @@ -0,0 +1,259 @@ +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.CfnStackWaits; +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.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +/** + * {@code AWS::NoValue} is an omission wherever a template property can hold it: the properties a + * provisioner receives have had every {@code Fn::If} that chose it, and every list item or member + * that is it, taken out. CloudFormation documents it as removing the property, so a stack that + * writes {@code Fn::If [Off, x, AWS::NoValue]} must end as the stack that leaves the property out, + * on create and on update, for a resource whose provisioner never looked for NoValue. + */ +@QuarkusTest +class CloudFormationNoValueOmissionIntegrationTest { + + private static final String IAM_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/iam/aws4_request"; + private static final String SERVICE_TOKEN = + "arn:aws:lambda:us-east-1:000000000000:function:no-value-probe"; + private static final String NO_VALUE_IF = "{\"Fn::If\": [\"Off\", \"unused\", {\"Ref\": \"AWS::NoValue\"}]}"; + + private final ObjectMapper mapper = new ObjectMapper(); + private final List 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 accessKeyStatusChosenAsNoValueIsLeftOutOnCreate() { + String stack = unique("nv-key-create"); + createStack(stack, accessKeyTemplate("Status", NO_VALUE_IF)); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack).status()); + assertEquals("Active", accessKeyStatus(stack)); + } + + @Test + void accessKeyStatusChosenAsNoValueActsAsOmissionOnUpdate() { + String dropped = unique("nv-key-dropped"); + String novalue = unique("nv-key-novalue"); + createStack(dropped, accessKeyTemplate("Status", "\"Inactive\"")); + createStack(novalue, accessKeyTemplate("Status", "\"Inactive\"")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(dropped).status()); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(novalue).status()); + + updateStack(dropped, accessKeyTemplate(null, null)); + updateStack(novalue, accessKeyTemplate("Status", NO_VALUE_IF)); + + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(dropped).status()); + CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(novalue); + assertEquals("UPDATE_COMPLETE", state.status(), state.reason()); + assertEquals(accessKeyStatus(dropped), accessKeyStatus(novalue)); + } + + @Test + void secretDescriptionChosenAsNoValueIsLeftOutOnCreate() { + String stack = unique("nv-secret-create"); + createStack(stack, secretTemplate("Description", NO_VALUE_IF, stack)); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack).status()); + assertNull(secretDescription(stack)); + } + + @Test + void secretDescriptionChosenAsNoValueActsAsOmissionOnUpdate() { + String dropped = unique("nv-secret-dropped"); + String novalue = unique("nv-secret-novalue"); + createStack(dropped, secretTemplate("Description", "\"first\"", dropped)); + createStack(novalue, secretTemplate("Description", "\"first\"", novalue)); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(dropped).status()); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(novalue).status()); + + updateStack(dropped, secretTemplate(null, null, dropped)); + updateStack(novalue, secretTemplate("Description", NO_VALUE_IF, novalue)); + + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(dropped).status()); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(novalue).status()); + assertEquals(secretDescription(dropped), secretDescription(novalue)); + } + + @Test + void customResourcePropertiesOmitNoValueAtEveryDepthOnCreateAndUpdate() { + String stack = unique("nv-custom"); + createStack(stack, probeTemplate("one")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack).status()); + updateStack(stack, probeTemplate("two")); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack).status()); + + assertEquals(List.of("Create", "Update"), + events.stream().map(e -> e.get("RequestType").asText()).toList()); + assertEquals(expected("one"), events.get(0).get("ResourceProperties")); + assertEquals(expected("two"), events.get(1).get("ResourceProperties")); + assertEquals(expected("one"), events.get(1).get("OldResourceProperties")); + } + + private JsonNode expected(String revision) { + ObjectNode properties = mapper.createObjectNode(); + properties.put("ServiceToken", SERVICE_TOKEN); + properties.put("Revision", revision); + properties.putObject("Nested").put("Kept", "yes"); + properties.putArray("Items").add("a").add("c"); + return properties; + } + + private static String probeTemplate(String revision) { + return """ + { + "Conditions": {"Off": {"Fn::Equals": ["a", "b"]}}, + "Resources": { + "Probe": { + "Type": "Custom::NoValueProbe", + "Properties": { + "ServiceToken": "%s", + "Revision": "%s", + "Dropped": %s, + "Nested": {"Kept": "yes", "Dropped": %s}, + "Items": ["a", %s, "c"] + } + } + } + } + """.formatted(SERVICE_TOKEN, revision, NO_VALUE_IF, NO_VALUE_IF, NO_VALUE_IF); + } + + /** A key on a user; {@code property} is left out when null. */ + private static String accessKeyTemplate(String property, String value) { + return """ + { + "Conditions": {"Off": {"Fn::Equals": ["a", "b"]}}, + "Resources": { + "User": {"Type": "AWS::IAM::User"}, + "Key": { + "Type": "AWS::IAM::AccessKey", + "Properties": {"UserName": {"Ref": "User"}%s} + } + }, + "Outputs": {"UserName": {"Value": {"Ref": "User"}}} + } + """.formatted(property == null ? "" : ", \"" + property + "\": " + value); + } + + /** A secret named after its stack; {@code property} is left out when null. */ + private static String secretTemplate(String property, String value, String name) { + return """ + { + "Conditions": {"Off": {"Fn::Equals": ["a", "b"]}}, + "Resources": { + "Secret": { + "Type": "AWS::SecretsManager::Secret", + "Properties": {"Name": "%s", "SecretString": "s3cret"%s} + } + } + } + """.formatted(name, property == null ? "" : ", \"" + property + "\": " + value); + } + + private static String accessKeyStatus(String stack) { + String user = outputValue(describe(stack), "UserName"); + String xml = given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", IAM_AUTH) + .formParam("Action", "ListAccessKeys") + .formParam("UserName", user) + .when().post("/").then().statusCode(200).extract().asString(); + return XmlParser.extractFirst(xml, "Status", null); + } + + private static String secretDescription(String name) { + return given() + .header("X-Amz-Target", "secretsmanager.DescribeSecret") + .contentType("application/x-amz-json-1.1") + .body("{\"SecretId\": \"" + name + "\"}") + .when().post("/").then().statusCode(200) + .extract().jsonPath().getString("Description"); + } + + /** 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", "no-value-probe"); + response.putObject("Data"); + String path = URI.create(event.get("ResponseURL").asText()).getPath(); + given().body(response.toString()).when().put(path).then().statusCode(200); + } + + private static String unique(String prefix) { + return prefix + "-" + Long.toString(System.nanoTime(), 36); + } + + private static void createStack(String stackName, String template) { + cfn(Map.of("Action", "CreateStack", "StackName", stackName, "TemplateBody", template)); + } + + private static void updateStack(String stackName, String template) { + cfn(Map.of("Action", "UpdateStack", "StackName", stackName, "TemplateBody", template)); + } + + private static String describe(String stackName) { + return cfn(Map.of("Action", "DescribeStacks", "StackName", stackName)); + } + + private static String cfn(Map 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); + } +}