fix(cloudformation): evaluate conditions with intrinsics, require parameter values (#5737)

* fix(cloudformation): require a value for every declared parameter

CreateStack, UpdateStack and CreateChangeSet accepted a template that
declares a parameter with no Default when the request gave it no value.
The stack deployed with the parameter unset: a condition compared it as
an empty string, and a Ref resolved to the parameter's own name.

Such a request now fails with ValidationError "Parameters: [Env] must
have values", naming every such parameter in declaration order, before
the SAM transform and the dependency preflight run and before any stack
state changes. An empty value counts as a value. An update does not
carry values over, so it needs a value or UsePreviousValue, which keeps
the deployed one, for each parameter without a Default.
UsePreviousValue on a parameter with no deployed value passes none,
rather than an empty string. A request its stack refuses outright, an
update of a stack that is missing or cannot be updated or a create over
a name in use, still gets that refusal. A parameter that an AWS::Include
defines, which only execution merges, is not checked up front.

The same check refuses CreateStackSet and UpdateStackSet, before the
stack set changes or an operation is recorded. UpdateStackSet resolves
UsePreviousValue against the previous template's Defaults too, since a
stack set keeps only the values it was given. A nested stack resource
whose Parameters leave a child parameter without a value fails, before
the child is created or changed. Those Parameters now resolve as a
property value: an Fn::If may choose the whole map, and an AWS::NoValue
value passes nothing.

A CreateStackInstances whose targets cannot be resolved, or whose
change set is refused, now ends its operation FAILED. Left RUNNING, it
refused every later operation on the stack set.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(cloudformation): evaluate conditions through the template engine

Conditions resolved only a bare Ref, and only to a parameter, the
account or the region. Every function other than that Ref compared as
an empty string inside Fn::Equals: Fn::FindInMap, a Ref to
AWS::StackName or AWS::StackId, Fn::Join. A condition on a mapped value
or on the stack's own name was decided wrongly, so the wrong resources,
or none, were created.

The operands now resolve through the template engine, with the
template's Mappings and the stack's name and id. The change-set preview
and the dependency preflight evaluate against the stack too: the live
one an update targets, or the name a create takes, whose id does not
exist yet. An operand the engine cannot resolve compares as an empty
string, as every function did before, so a template that evaluated then
still does. A declared parameter that a stack stored by an older Floci
lacks still compares as an empty string, and AWS::NotificationARNs is
the empty list, now in properties and outputs too.

CloudFormation does not allow Fn::ImportValue in a condition, so a
template being deployed with one there is refused with "Template error:
Cannot use Fn::ImportValue in Conditions.", also in an operand that
evaluation never reaches. A SAM template's change set, or its stack,
fails with that reason instead. A stored template compares the import
as an empty string, so it never blocks an update that supplies a new
template; UsePreviousTemplate checks the stored one as it would a new
one.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(cloudformation): check a create's dependencies with its stack id

A create's dependency preflight evaluated AWS::StackId as an empty
string while the execution used the stack's id, so a dependency on a
resource whose condition reads the stack id could pass the preflight
and then fail the deploy, or the other way round. A create over a name
in use was checked against the stack holding the name instead, and
could get a dependency error rather than AlreadyExistsException.

createChangeSet now allocates the new stack's id before the preflight
and creates the stack with it, so the preflight, the change-set preview
and the execution evaluate the same id. An update, or a create that
attaches to a REVIEW_IN_PROGRESS placeholder, is checked against that
stack as before, and a create over a name in use is checked as the new
stack it asks for.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* docs(cloudformation): place the parameter check before dependency checks

The parameter section said template errors such as a dependency cycle
come first, but createChangeSet checks the parameter values before the
dependency checks, which evaluate conditions that read those values.
Say so, and keep the stack's own refusal ahead of both.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* docs(cloudformation): order the stack's refusal after dependency errors

The previous commit said the stack's own refusal comes ahead of both
the parameter check and the dependency checks. It comes ahead of the
parameter check only: a request its stack refuses still gets a
template's dependency error first, as on main. Say so, and re-wrap the
conditions paragraph.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* docs(cloudformation): say parameter values are checked before dependency errors

The parameter section still read as if a template's dependency errors
came first. createChangeSet checks the parameter values before the
dependency checks, which evaluate conditions that read the values, so
a template with a cycle and a missing value gets the parameter error.
Say that, and that a stack's own refusal replaces the parameter error
but still follows a dependency error.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(cloudformation): pin a missing value ahead of a dependency cycle

A template with both a DependsOn cycle and a parameter without a value
gets "Parameters: [Env] must have values" and creates no stack.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* refactor(cloudformation): name the tolerated parse failures ignored

requireParameterValues and parametersWithDefaults return when the
template does not parse, as intended: there is nothing to check or to
add, and the preflight and the execution report the parse error. Name
the caught exception ignored and say why, as AGENTS.md asks.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* refactor(cloudformation): say only execution reports a parse failure

The comment on the ignored parse failure in requireParameterValues
said the preflight reports it too. The dependency preflight skips a
template that does not parse; only executing the change set reports
the error.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-15 16:56:00 -04:00
1 parent 8eddae4ed6
commit ad8fba7f45
12 files changed
+1880 -92

No files matched your search

@@ -230,7 +230,7 @@ class CloudFormationChangeSetDiffIntegrationTest {
}
@Test
void omittedParameterWithNoDefault_isReportedAsAChange() {
void omittedParameterWithNoDefault_isRefused() {
String stackName = "cs-diff-omitted-param-no-default-stack";
stacksToDelete.add(stackName);
@@ -262,9 +262,9 @@ class CloudFormationChangeSetDiffIntegrationTest {
.then()
.statusCode(200);
// Same template, but the update omits Suffix entirely and the template has no Default -
// resolveDefaultParameters drops the parameter, and ExecuteChangeSet would apply the
// resulting (missing) value. The preview must flag the resource as changed too.
// Same template, but the update omits Suffix entirely and the template has no Default. An
// update does not carry the deployed value over, so CloudFormation refuses the change set
// rather than preview, or apply, the parameter without a value.
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateChangeSet")
@@ -275,19 +275,9 @@ class CloudFormationChangeSetDiffIntegrationTest {
.when()
.post("/")
.then()
.statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeChangeSet")
.formParam("StackName", stackName)
.formParam("ChangeSetName", "omitted-param-no-default-cs")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<LogicalResourceId>Q</LogicalResourceId>"))
.body(containsString("<Action>Modify</Action>"));
.statusCode(400)
.body(containsString("<Code>ValidationError</Code>"))
.body(containsString("<Message>Parameters: [Suffix] must have values</Message>"));
}
@Test
@@ -0,0 +1,663 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* A declared parameter that the request gives no value and the template no {@code Default}
* fails CreateStack, UpdateStack and CreateChangeSet with {@code Parameters: [X] must have values},
* before any stack state changes. {@code UsePreviousValue} supplies the deployed value on an update.
*/
@QuarkusTest
class CloudFormationParameterValuesIntegrationTest {
private static final String BLANK_ENV_TEMPLATE = """
{
"Parameters": {"Env": {"Type": "String"}},
"Conditions": {"IsBlank": {"Fn::Equals": [{"Ref": "Env"}, ""]}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Condition": "IsBlank"}}
}
""";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void createStackRefusesAParameterWithNoValueAndNoDefault() {
String stackName = "cfn-param-missing-" + Long.toString(System.nanoTime(), 36);
request("CreateStack", stackName)
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
describeStacks(stackName).then().statusCode(400);
}
/** Every such parameter is named, in the order the template declares them. */
@Test
void everyParameterWithoutAValueIsNamed() {
String stackName = "cfn-param-several-" + Long.toString(System.nanoTime(), 36);
request("CreateStack", stackName)
.formParam("TemplateBody", """
{
"Parameters": {
"Zeta": {"Type": "String"},
"Alpha": {"Type": "String", "Default": "a"},
"Beta": {"Type": "Number"},
"Gamma": {"Type": "String"}
},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue"}}
}
""")
.formParam("Parameters.member.1.ParameterKey", "Gamma")
.formParam("Parameters.member.1.ParameterValue", "g")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Zeta, Beta] must have values"));
describeStacks(stackName).then().statusCode(400);
}
/**
* The values are checked before the template's dependency checks, which evaluate conditions
* that read them, so a template with both a cycle and a missing value gets the parameter error.
*/
@Test
void aMissingValueIsReportedBeforeADependencyCycle() {
String stackName = "cfn-param-cycle-" + Long.toString(System.nanoTime(), 36);
request("CreateStack", stackName)
.formParam("TemplateBody", """
{
"Parameters": {"Env": {"Type": "String"}},
"Resources": {
"A": {"Type": "AWS::SQS::Queue", "DependsOn": "B"},
"B": {"Type": "AWS::SQS::Queue", "DependsOn": "A"}
}
}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
describeStacks(stackName).then().statusCode(400);
}
/** An empty value is a value, and a Default stands in for an omitted one. */
@Test
void anEmptyValueAndADefaultAreValues() {
String stackName = "cfn-param-empty-" + Long.toString(System.nanoTime(), 36);
String queueName = "param-empty-" + Long.toString(System.nanoTime(), 36);
request("CreateStack", stackName)
.formParam("TemplateBody", """
{
"Parameters": {
"Env": {"Type": "String"},
"Timeout": {"Type": "Number", "Default": "45"}
},
"Conditions": {"IsBlank": {"Fn::Equals": [{"Ref": "Env"}, ""]}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Condition": "IsBlank",
"Properties": {"QueueName": "%s", "VisibilityTimeout": {"Ref": "Timeout"}}}}
}
""".formatted(queueName))
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.ParameterValue", "")
.when()
.post("/")
.then()
.statusCode(200);
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
getQueueUrl(queueName).then().statusCode(200);
} finally {
deleteStack(stackName);
}
}
/** A CREATE change set is refused too, and leaves no REVIEW_IN_PROGRESS stack behind. */
@Test
void createChangeSetRefusesAParameterWithNoValue() {
String stackName = "cfn-param-changeset-" + Long.toString(System.nanoTime(), 36);
request("CreateChangeSet", stackName)
.formParam("ChangeSetName", "deploy")
.formParam("ChangeSetType", "CREATE")
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
describeStacks(stackName).then().statusCode(400);
}
/**
* The check runs before the SAM transform, so a SAM template is refused with the same 400
* rather than given a change set that fails later.
*/
@Test
void samTemplateWithoutAValueIsRefusedBeforeTheTransform() {
String stackName = "cfn-param-sam-" + Long.toString(System.nanoTime(), 36);
request("CreateChangeSet", stackName)
.formParam("ChangeSetName", "deploy")
.formParam("ChangeSetType", "CREATE")
.formParam("TemplateBody", """
{
"Transform": "AWS::Serverless-2016-10-31",
"Parameters": {"Env": {"Type": "String"}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue",
"Properties": {"QueueName": {"Fn::Sub": "sam-${Env}"}}}}
}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
describeStacks(stackName).then().statusCode(400);
}
/**
* An update does not carry a parameter over: one it omits is refused whether the update sends
* a template or reuses the previous one, and the stack keeps its status and value.
* {@code UsePreviousValue} keeps the deployed value, on UpdateStack and on an UPDATE change set.
*/
@Test
void anUpdateNeedsAValueOrUsePreviousValue() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-param-update-" + suffix;
String queueName = "param-update-" + suffix;
String template = """
{
"Parameters": {"Env": {"Type": "String"}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue",
"Properties": {"QueueName": {"Fn::Sub": "%s-${Env}"}, "VisibilityTimeout": %d}}}
}
""";
request("CreateStack", stackName)
.formParam("TemplateBody", template.formatted(queueName, 30))
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.ParameterValue", "dev")
.when()
.post("/")
.then()
.statusCode(200);
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
request("UpdateStack", stackName)
.formParam("TemplateBody", template.formatted(queueName, 60))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
request("UpdateStack", stackName)
.formParam("UsePreviousTemplate", "true")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
request("CreateChangeSet", stackName)
.formParam("ChangeSetName", "omits-env")
.formParam("ChangeSetType", "UPDATE")
.formParam("TemplateBody", template.formatted(queueName, 60))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
describeStacks(stackName).then()
.statusCode(200)
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
.body(containsString("<ParameterValue>dev</ParameterValue>"));
request("CreateChangeSet", stackName)
.formParam("ChangeSetName", "keeps-env")
.formParam("ChangeSetType", "UPDATE")
.formParam("TemplateBody", template.formatted(queueName, 45))
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.when()
.post("/")
.then()
.statusCode(200);
request("UpdateStack", stackName)
.formParam("TemplateBody", template.formatted(queueName, 60))
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.when()
.post("/")
.then()
.statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals("60", visibilityTimeout(queueName + "-dev"));
describeStacks(stackName).then()
.statusCode(200)
.body(containsString("<ParameterValue>dev</ParameterValue>"));
} finally {
deleteStack(stackName);
}
}
/**
* {@code UsePreviousValue} keeps a deployed value; on a parameter that has none, on a create or
* for a parameter the update adds, it passes nothing, so the template's Default applies, or the
* request is refused.
*/
@Test
void usePreviousValueWithNothingDeployedPassesNoValue() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-param-previous-" + suffix;
String queueName = "param-previous-" + suffix;
request("CreateStack", stackName)
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
request("CreateStack", stackName)
.formParam("TemplateBody", """
{"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": "%s"}}}}
""".formatted(queueName))
.when()
.post("/")
.then()
.statusCode(200);
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
String added = """
{
"Parameters": {"Added": {"Type": "String"%s}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue",
"Properties": {"QueueName": "%s", "Tags": [{"Key": "added", "Value": {"Ref": "Added"}}]}}}
}
""";
request("UpdateStack", stackName)
.formParam("TemplateBody", added.formatted("", queueName))
.formParam("Parameters.member.1.ParameterKey", "Added")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Added] must have values"));
request("UpdateStack", stackName)
.formParam("TemplateBody", added.formatted(", \"Default\": \"fallback\"", queueName))
.formParam("Parameters.member.1.ParameterKey", "Added")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.when()
.post("/")
.then()
.statusCode(200);
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
describeStacks(stackName).then()
.statusCode(200)
.body(containsString("<ParameterValue>fallback</ParameterValue>"));
} finally {
deleteStack(stackName);
}
}
/**
* A request its stack refuses outright gets that refusal, whatever its parameters: an update of
* a stack that does not exist, whose UsePreviousValue has nothing to keep, and a create over a
* name in use.
*/
@Test
void aRequestItsStackRefusesGetsThatRefusal() {
String suffix = Long.toString(System.nanoTime(), 36);
String missing = "cfn-param-missing-stack-" + suffix;
request("UpdateStack", missing)
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Stack with id " + missing + " does not exist"));
String taken = "cfn-param-taken-" + suffix;
request("CreateStack", taken)
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.ParameterValue", "")
.when()
.post("/")
.then()
.statusCode(200);
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(taken).status());
request("CreateStack", taken)
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("AlreadyExistsException"));
} finally {
deleteStack(taken);
}
}
/**
* A REVIEW_IN_PROGRESS placeholder refuses an update, so an update without a value gets that
* refusal; a CREATE change set may attach to it, so one without a value gets the parameter error.
*/
@Test
void aPlaceholderStackRefusesTheUpdateButNotTheAttachingCreate() {
String stackName = "cfn-param-placeholder-" + Long.toString(System.nanoTime(), 36);
request("CreateChangeSet", stackName)
.formParam("ChangeSetName", "first")
.formParam("ChangeSetType", "CREATE")
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.formParam("Parameters.member.1.ParameterKey", "Env")
.formParam("Parameters.member.1.ParameterValue", "")
.when()
.post("/")
.then()
.statusCode(200);
try {
request("UpdateStack", stackName)
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message",
containsString("is in REVIEW_IN_PROGRESS state and can not be updated."));
request("CreateChangeSet", stackName)
.formParam("ChangeSetName", "second")
.formParam("ChangeSetType", "CREATE")
.formParam("TemplateBody", BLANK_ENV_TEMPLATE)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Env] must have values"));
} finally {
deleteStack(stackName);
}
}
/**
* A nested stack is created with the values its resource passes, so a child parameter left
* without one fails that resource, as the child's CreateStack would, and the parent rolls
* back. The child stack is never created.
*/
@Test
void aNestedStackWithoutAValueFailsItsResource() {
String suffix = Long.toString(System.nanoTime(), 36);
String bucket = "cfn-param-nested-" + suffix;
String stackName = "cfn-param-nested-" + suffix;
String templateUrl = putChildTemplate(bucket);
try {
request("CreateStack", stackName)
.formParam("TemplateBody", parentTemplate(templateUrl, "{}"))
.when()
.post("/")
.then()
.statusCode(200);
CfnStackWaits.StackState state = CfnStackWaits.awaitStatus(stackName, "ROLLBACK_COMPLETE");
assertEquals("Parameters: [Env] must have values", state.reason());
request("DescribeStackEvents", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<ResourceStatusReason>Parameters: [Env] must have values</ResourceStatusReason>"));
describeStacks(stackName + "-Child").then().statusCode(400);
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
deleteChildTemplate(bucket);
}
}
/**
* A nested stack update that leaves a child parameter without a value fails the resource
* before the child changes, and the parent rolls back with the child as it was.
*/
@Test
void aNestedStackUpdateWithoutAValueLeavesTheChild() {
String suffix = Long.toString(System.nanoTime(), 36);
String bucket = "cfn-param-nested-update-" + suffix;
String stackName = "cfn-param-nested-update-" + suffix;
String templateUrl = putChildTemplate(bucket);
try {
request("CreateStack", stackName)
.formParam("TemplateBody", parentTemplate(templateUrl, "{\"Env\": \"dev\"}"))
.when()
.post("/")
.then()
.statusCode(200);
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
request("UpdateStack", stackName)
.formParam("TemplateBody", parentTemplate(templateUrl, "{\"Size\": \"2\"}"))
.when()
.post("/")
.then()
.statusCode(200);
CfnStackWaits.awaitStatus(stackName, "UPDATE_ROLLBACK_COMPLETE");
request("DescribeStackEvents", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<ResourceStatusReason>Parameters: [Env] must have values</ResourceStatusReason>"));
describeStacks(stackName + "-Child").then()
.statusCode(200)
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
.body(containsString("<ParameterValue>dev</ParameterValue>"));
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
deleteChildTemplate(bucket);
}
}
/**
* The resource's Parameters resolve as a property value: an Fn::If choosing the whole map
* passes the chosen one, and a value that is AWS::NoValue passes nothing, so the child's
* Default applies.
*/
@Test
void aNestedStacksParametersResolveAsAPropertyValue() {
String suffix = Long.toString(System.nanoTime(), 36);
String bucket = "cfn-param-nested-if-" + suffix;
String stackName = "cfn-param-nested-if-" + suffix;
String templateUrl = putChildTemplate(bucket);
try {
request("CreateStack", stackName)
.formParam("TemplateBody", """
{
"Conditions": {"Always": {"Fn::Equals": ["a", "a"]}},
"Resources": {
"Child": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {"TemplateURL": "%s", "Parameters": {"Fn::If": ["Always",
{"Env": "dev", "Size": {"Ref": "AWS::NoValue"}}, {"Env": "prod"}]}}
}
}
}
""".formatted(templateUrl))
.when()
.post("/")
.then()
.statusCode(200);
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
describeStacks(stackName + "-Child").then()
.statusCode(200)
.body("**.find { it.ParameterKey == 'Env' }.ParameterValue", equalTo("dev"))
.body("**.find { it.ParameterKey == 'Size' }.ParameterValue", equalTo("1"));
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
deleteChildTemplate(bucket);
}
}
/**
* A parameter definition an AWS::Include supplies is only known once execution merges it, so it
* is not checked up front: a Default the snippet carries still applies. The section's other
* parameters are checked as usual.
*/
@Test
void aParameterAnIncludeDefinesIsNotCheckedUpFront() {
String suffix = Long.toString(System.nanoTime(), 36);
String bucket = "cfn-param-include-" + suffix;
String stackName = "cfn-param-include-" + suffix;
String queueName = "param-include-" + suffix;
given().when().put("/" + bucket).then().statusCode(200);
given().body("{\"Type\": \"String\", \"Default\": \"dev\"}")
.when().put("/" + bucket + "/env.json").then().statusCode(200);
try {
request("CreateStack", stackName)
.formParam("TemplateBody", """
{
"Parameters": {"Env": {"Fn::Transform": {"Name": "AWS::Include",
"Parameters": {"Location": "s3://%s/env.json"}}}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue",
"Properties": {"QueueName": {"Fn::Sub": "%s-${Env}"}}}}
}
""".formatted(bucket, queueName))
.when()
.post("/")
.then()
.statusCode(200);
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
getQueueUrl(queueName + "-dev").then().statusCode(200);
request("CreateStack", stackName + "-other")
.formParam("TemplateBody", """
{
"Parameters": {"Other": {"Type": "String"}, "Env": {"Fn::Transform": {"Name": "AWS::Include",
"Parameters": {"Location": "s3://%s/env.json"}}}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue"}}
}
""".formatted(bucket))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", equalTo("Parameters: [Other] must have values"));
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
given().when().delete("/" + bucket + "/env.json");
given().when().delete("/" + bucket);
}
}
/** Puts a child template declaring Env (no Default) and Size (Default 1) in a new bucket. */
private static String putChildTemplate(String bucket) {
given().when().put("/" + bucket).then().statusCode(200);
given().contentType("application/json")
.body("""
{
"Parameters": {"Env": {"Type": "String"}, "Size": {"Type": "Number", "Default": "1"}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue"}}
}
""")
.when()
.put("/" + bucket + "/child.json")
.then()
.statusCode(200);
return "http://localhost/" + bucket + "/child.json";
}
private static void deleteChildTemplate(String bucket) {
given().when().delete("/" + bucket + "/child.json");
given().when().delete("/" + bucket);
}
private static String parentTemplate(String templateUrl, String parameters) {
return """
{
"Resources": {
"Child": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {"TemplateURL": "%s", "Parameters": %s}
}
}
}
""".formatted(templateUrl, parameters);
}
private static RequestSpecification request(String action, String stackName) {
return given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", action)
.formParam("StackName", stackName);
}
private static Response describeStacks(String stackName) {
return request("DescribeStacks", stackName).when().post("/");
}
private static void deleteStack(String stackName) {
request("DeleteStack", stackName).when().post("/").then().statusCode(200);
}
private static Response getQueueUrl(String queueName) {
return given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "GetQueueUrl")
.formParam("QueueName", queueName)
.when()
.post("/");
}
private static String visibilityTimeout(String queueName) {
String queueUrl = getQueueUrl(queueName).then().statusCode(200).extract().xmlPath()
.getString("GetQueueUrlResponse.GetQueueUrlResult.QueueUrl");
return given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "GetQueueAttributes")
.formParam("QueueUrl", queueUrl)
.formParam("AttributeName.1", "VisibilityTimeout")
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath()
.getString("**.find { it.Name == 'VisibilityTimeout' }.Value");
}
}
@@ -4,11 +4,14 @@ import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@QuarkusTest
@@ -62,6 +65,90 @@ class CloudFormationResourceConditionIntegrationTest {
.statusCode(400);
}
/**
* Fn::FindInMap inside Fn::Equals resolves through the template's mappings. Read as an empty
* string, a mapped "false" made UseCode true, and the bucket was named after the mapped value.
*/
@ParameterizedTest
@CsvSource({"false,us-east-1", "iad,iad"})
void mappedConditionSelectsTheBucketName(String mappedValue, String expectedLocation) {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-mapped-condition-" + suffix;
String prefix = "mapped-condition-" + suffix + "-";
createStack(stackName, """
{
"Mappings": {"Locations": {"us-east-1": {"Code": "%s"}}},
"Conditions": {
"UseCode": {"Fn::Not": [{"Fn::Equals": [
{"Fn::FindInMap": ["Locations", {"Ref": "AWS::Region"}, "Code"]}, "false"
]}]}
},
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {"BucketName": {"Fn::Join": ["", ["%s",
{"Fn::If": ["UseCode",
{"Fn::FindInMap": ["Locations", {"Ref": "AWS::Region"}, "Code"]},
{"Ref": "AWS::Region"}]}
]]}}
}
}
}
""".formatted(mappedValue, prefix));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
given().when().head("/" + prefix + expectedLocation).then().statusCode(200);
given().when().head("/" + prefix + "false").then().statusCode(404);
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
}
given().when().head("/" + prefix + expectedLocation).then().statusCode(404);
}
/**
* The update's change-set preview and its execution evaluate the mapped condition as the
* create does, so a mapping edit that flips it reaches the property chosen by its Fn::If.
*/
@Test
void mappedConditionIsEvaluatedAgainOnUpdate() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-mapped-update-" + suffix;
String queueName = "mapped-update-" + suffix;
String template = """
{
"Mappings": {"Locations": {"us-east-1": {"Code": "%s"}}},
"Conditions": {
"UseCode": {"Fn::Not": [{"Fn::Equals": [
{"Fn::FindInMap": ["Locations", {"Ref": "AWS::Region"}, "Code"]}, "false"
]}]}
},
"Resources": {
"Queue": {
"Type": "AWS::SQS::Queue",
"Properties": {"QueueName": "%s", "VisibilityTimeout": {"Fn::If": ["UseCode", 60, 30]}}
}
}
}
""";
createStack(stackName, template.formatted("false", queueName));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals("30", visibilityTimeout(queueName));
updateStack(stackName, template.formatted("iad", queueName));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals("60", visibilityTimeout(queueName));
updateStack(stackName, template.formatted("false", queueName));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals("30", visibilityTimeout(queueName));
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void createStack_skipsResourceWhenResourceLevelConditionIsFalse() {
String suffix = Long.toString(System.nanoTime(), 36);
@@ -581,6 +668,340 @@ class CloudFormationResourceConditionIntegrationTest {
.post("/");
}
/**
* A create's dependency preflight evaluates {@code AWS::StackId} as the id the stack is given,
* so it agrees with the execution: a dependency on a resource the stack id excludes is refused
* up front, and one on a resource it includes deploys.
*/
@Test
void aCreatesPreflightSeesTheStackIdItWillGet() {
String suffix = Long.toString(System.nanoTime(), 36);
String template = """
{
"Conditions": {
"NoId": {"Fn::Equals": [{"Ref": "AWS::StackId"}, ""]},
"HasId": {"Fn::Not": [{"Condition": "NoId"}]}
},
"Resources": {
"Dependency": {"Type": "AWS::SQS::Queue", "Condition": "%s",
"Properties": {"QueueName": "%s-dependency"}},
"Dependent": {"Type": "AWS::SQS::Queue", "DependsOn": "Dependency",
"Properties": {"QueueName": "%s-dependent"}}
}
}
""";
String excluded = "cfn-stackid-excluded-" + suffix;
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", excluded)
.formParam("TemplateBody", template.formatted("NoId", excluded, excluded))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", containsString("Unresolved resource dependencies"));
String included = "cfn-stackid-included-" + suffix;
createStack(included, template.formatted("HasId", included, included));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(included).status());
getQueueUrl(included + "-dependency").then().statusCode(200);
getQueueUrl(included + "-dependent").then().statusCode(200);
} finally {
deleteStack(included);
CfnStackWaits.awaitStackDeleted(included);
}
}
/**
* A create over a name in use is checked as the new stack it asks for, not as the stack that
* holds the name, so a template that is valid for a new stack gets AlreadyExistsException.
*/
@Test
void aCreateOverATakenNameIsCheckedAsTheNewStack() {
String stackName = "cfn-stackid-taken-" + Long.toString(System.nanoTime(), 36);
createStack(stackName, """
{"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": "%s"}}}}
""".formatted(stackName));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
String takenId = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath().getString("**.find { it.name() == 'StackId' }");
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", stackName)
.formParam("TemplateBody", """
{
"Conditions": {"NotTheTakenStack": {"Fn::Not": [{"Fn::Equals": [
{"Ref": "AWS::StackId"}, "%s"]}]}},
"Resources": {
"Dependency": {"Type": "AWS::SQS::Queue", "Condition": "NotTheTakenStack"},
"Dependent": {"Type": "AWS::SQS::Queue", "DependsOn": "Dependency"}
}
}
""".formatted(takenId))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("AlreadyExistsException"));
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
/**
* The change-set preview, the preflight and the execution all evaluate {@code AWS::StackName}
* against the stack. Read as an empty string, the condition was false and the stack held no
* resources; with only the preview reading it so, the update would skip the queue's edit.
*/
@Test
void stackNameConditionHoldsAcrossAnUpdateThatEditsItsResource() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-stackname-condition-" + suffix;
String queueName = "stackname-condition-" + suffix;
String template = """
{
"Conditions": {"IsThisStack": {"Fn::Equals": [{"Ref": "AWS::StackName"}, "%s"]}},
"Resources": {
"Queue": {
"Type": "AWS::SQS::Queue", "Condition": "IsThisStack",
"Properties": {"QueueName": "%s", "VisibilityTimeout": %d}
}
}
}
""";
createStack(stackName, template.formatted(stackName, queueName, 30));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals("30", visibilityTimeout(queueName));
updateStack(stackName, template.formatted(stackName, queueName, 60));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals("60", visibilityTimeout(queueName));
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
/**
* CloudFormation allows only Ref, Fn::FindInMap and the condition functions inside a
* condition, so a template that uses Fn::ImportValue there is refused, even though the
* exporting stack has the export.
*/
@Test
void importValueInAConditionIsRejectedAsUnsupported() {
String suffix = Long.toString(System.nanoTime(), 36);
String exporter = "cfn-cond-exporter-" + suffix;
String exportName = "cfn-cond-export-" + suffix;
String importer = "cfn-cond-importer-" + suffix;
createStack(exporter, """
{"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": "%s"}}},
"Outputs": {"Flag": {"Value": "on", "Export": {"Name": "%s"}}}}
""".formatted("cond-export-queue-" + suffix, exportName));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(exporter).status());
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", importer)
.formParam("TemplateBody", """
{"Conditions": {"IsOn": {"Fn::Equals": [{"Fn::ImportValue": "%s"}, "on"]}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Condition": "IsOn"}}}
""".formatted(exportName))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message",
equalTo("Template error: Cannot use Fn::ImportValue in Conditions."));
} finally {
deleteStack(importer);
deleteStack(exporter);
CfnStackWaits.awaitStackDeleted(exporter);
}
}
/** An import in an Fn::Or operand that evaluation never reaches is still a template error. */
@Test
void importValueInAConditionOperandEvaluationSkipsIsStillRejected() {
String stackName = "cfn-cond-or-import-" + Long.toString(System.nanoTime(), 36);
try {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", stackName)
.formParam("TemplateBody", """
{"Conditions": {"IsOn": {"Fn::Or": [{"Fn::Equals": ["a", "a"]},
{"Fn::Equals": [{"Fn::ImportValue": "never-resolved"}, "on"]}]}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Condition": "IsOn"}}}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message",
equalTo("Template error: Cannot use Fn::ImportValue in Conditions."));
} finally {
deleteStack(stackName);
}
}
/**
* A SAM template's conditions are checked after the transform, so an import there fails the
* change set with the template error, as a non-string condition reference does.
*/
@Test
void importValueInASamTemplateConditionFailsTheChangeSet() {
String stackName = "cfn-cond-sam-import-" + Long.toString(System.nanoTime(), 36);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateChangeSet")
.formParam("StackName", stackName)
.formParam("ChangeSetName", "deploy")
.formParam("ChangeSetType", "CREATE")
.formParam("TemplateBody", """
{"Transform": "AWS::Serverless-2016-10-31",
"Conditions": {"IsOn": {"Fn::Equals": [{"Fn::ImportValue": "never-resolved"}, "on"]}},
"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Condition": "IsOn"}}}
""")
.when()
.post("/")
.then()
.statusCode(200);
try {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeChangeSet")
.formParam("StackName", stackName)
.formParam("ChangeSetName", "deploy")
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeChangeSetResponse.DescribeChangeSetResult.Status", equalTo("FAILED"))
.body("DescribeChangeSetResponse.DescribeChangeSetResult.StatusReason",
equalTo("Template error: Cannot use Fn::ImportValue in Conditions."));
} finally {
deleteStack(stackName);
}
}
/**
* {@code AWS::NotificationARNs} is the empty list, in a condition and in an output: Floci keeps
* no notification ARNs, and the template engine would otherwise answer the Ref with its name.
*/
@Test
void notificationArnsAreAnEmptyList() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-notification-arns-" + suffix;
String queueName = "notification-arns-" + suffix;
createStack(stackName, """
{
"Conditions": {
"HasNotifications": {"Fn::Not": [{"Fn::Equals": [
{"Fn::Join": [",", {"Ref": "AWS::NotificationARNs"}]}, ""]}]}
},
"Resources": {
"Notified": {"Type": "AWS::SQS::Queue", "Condition": "HasNotifications",
"Properties": {"QueueName": "%s-notified"}},
"Always": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": "%s-always"}}
},
"Outputs": {"Arns": {"Value": {"Fn::Join": [",", ["arns", {"Fn::Join": [",",
{"Ref": "AWS::NotificationARNs"}]}]]}}}
}
""".formatted(queueName, queueName));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
getQueueUrl(queueName + "-always").then().statusCode(200);
getQueueUrl(queueName + "-notified").then().statusCode(400);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.body("**.find { it.OutputKey == 'Arns' }.OutputValue", equalTo("arns,"));
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
/**
* A condition operand the template engine cannot resolve, here an Fn::Sub whose template is not
* a string, compares as an empty string, as it did before conditions went through the engine:
* the stack deploys, and the dependency checks still run, rather than the request failing with
* a 500 or the stack with the engine's exception.
*/
@Test
void aConditionOperandTheEngineCannotResolveComparesAsEmpty() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-cond-malformed-sub-" + suffix;
String queueName = "cond-malformed-sub-" + suffix;
String condition = """
"Conditions": {"IsBlank": {"Fn::Equals": [{"Fn::Sub": [{"Ref": "AWS::Region"}, {}]}, ""]}}""";
createStack(stackName, """
{%s,
"Resources": {"Queue": {"Type": "AWS::SQS::Queue", "Condition": "IsBlank",
"Properties": {"QueueName": "%s"}}}}
""".formatted(condition, queueName));
try {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
getQueueUrl(queueName).then().statusCode(200);
} finally {
deleteStack(stackName);
CfnStackWaits.awaitStackDeleted(stackName);
}
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", stackName + "-cycle")
.formParam("TemplateBody", """
{%s,
"Resources": {
"A": {"Type": "AWS::SQS::Queue", "DependsOn": "B"},
"B": {"Type": "AWS::SQS::Queue", "DependsOn": "A"}}}
""".formatted(condition))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Message", containsString("Circular dependency between resources"));
}
private static String visibilityTimeout(String queueName) {
String queueUrl = getQueueUrl(queueName).then().statusCode(200).extract().xmlPath()
.getString("GetQueueUrlResponse.GetQueueUrlResult.QueueUrl");
return given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "GetQueueAttributes")
.formParam("QueueUrl", queueUrl)
.formParam("AttributeName.1", "VisibilityTimeout")
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath()
.getString("**.find { it.Name == 'VisibilityTimeout' }.Value");
}
@Test
void createStack_failsWhenResourceReferencesConditionFalseResourceViaGetAtt() {
// Verified against real AWS: an unguarded Fn::GetAtt to a condition-false resource is
@@ -381,6 +381,46 @@ class CloudFormationServiceManagedStackSetsIntegrationTest {
assertQueueAbsent(account, queue);
}
@Test
void anOrganizationalUnitThatCannotBeResolvedFailsTheOperation() {
// Resolving the targets is part of the operation: a target that fails ends it FAILED rather
// than leave it RUNNING, which would refuse every later operation on the stack set.
String management = "646464646464";
organizations(management, "CreateOrganization", "{\"FeatureSet\":\"ALL\"}")
.post("/").then().statusCode(200);
String rootId = organizations(management, "ListRoots", "{}")
.post("/").then().statusCode(200)
.extract().jsonPath().getString("Roots[0].Id");
cloudFormation(management, "ActivateOrganizationsAccess")
.post("/").then().statusCode(200);
String suffix = UUID.randomUUID().toString().substring(0, 8);
String stackSet = "missing-ou-" + suffix;
cloudFormation(management, "CreateStackSet")
.formParam("StackSetName", stackSet)
.formParam("TemplateBody", queueTemplate("missing-ou-q-" + suffix))
.formParam("PermissionModel", "SERVICE_MANAGED")
.post("/").then().statusCode(200);
cloudFormation(management, "CreateStackInstances")
.formParam("StackSetName", stackSet)
.formParam("DeploymentTargets.OrganizationalUnitIds.member.1", "ou-zzzz-zzzzzzzz")
.formParam("Regions.member.1", REGION)
.post("/").then().statusCode(400)
.body(containsString("OrganizationalUnitNotFoundException"));
cloudFormation(management, "ListStackSetOperations")
.formParam("StackSetName", stackSet)
.post("/").then().statusCode(200)
.body(containsString("<Status>FAILED</Status>"))
.body(not(containsString("<Status>RUNNING</Status>")));
cloudFormation(management, "CreateStackInstances")
.formParam("StackSetName", stackSet)
.formParam("DeploymentTargets.OrganizationalUnitIds.member.1", rootId)
.formParam("Regions.member.1", REGION)
.post("/").then().statusCode(200);
}
@Test
void activateOrganizationsAccessRequiresAllFeatures() {
String management = "939393939393";
@@ -751,6 +751,249 @@ class CloudFormationStackSetsIntegrationTest {
assertQueueAbsent(ACCOUNT_C, dependent);
}
@Test
void createStackSetWithoutAParameterValueIsRefused() {
// Every instance is deployed with the stack set's parameters, so a declared one the stack
// set has no value for is refused up front, as a stack's CreateStack refuses it.
String setName = "noval-" + UUID.randomUUID().toString().substring(0, 8);
String queue = "noval-q-" + UUID.randomUUID().toString().substring(0, 8);
String template = """
{"Parameters":{"Suffix":{"Type":"String"}},
"Resources":{"Q":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":{"Fn::Sub":"%s-${Suffix}"}}}}}
""".formatted(queue);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", template)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(400)
.body(containsString("<Code>ValidationError</Code>"))
.body(containsString("<Message>Parameters: [Suffix] must have values</Message>"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStackSet")
.formParam("StackSetName", setName)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(404)
.body(containsString("StackSetNotFoundException"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", template)
.formParam("Parameters.member.1.ParameterKey", "Suffix")
.formParam("Parameters.member.1.ParameterValue", "b")
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackInstances")
.formParam("StackSetName", setName)
.formParam("Accounts.member.1", ACCOUNT_B)
.formParam("Regions.member.1", REGION)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
assertQueueVisible(ACCOUNT_B, queue + "-b");
}
@Test
void updateStackSetWithoutAParameterValueChangesNothing() {
// Checked against the template and parameters the update would leave the stack set with,
// before either changes or an operation is recorded.
String setName = "noval-update-" + UUID.randomUUID().toString().substring(0, 8);
String queue = "noval-update-q-" + UUID.randomUUID().toString().substring(0, 8);
String template = """
{"Parameters":{"Suffix":{"Type":"String"}},
"Resources":{"Q":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":{"Fn::Sub":"%s-${Suffix}"}}}}}
""".formatted(queue);
String withExtra = """
{"Parameters":{"Suffix":{"Type":"String"},"Extra":{"Type":"String"}},
"Resources":{"Q":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":{"Fn::Sub":"%s-${Suffix}"},
"Tags":[{"Key":"extra","Value":{"Ref":"Extra"}}]}}}}
""".formatted(queue);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", template)
.formParam("Parameters.member.1.ParameterKey", "Suffix")
.formParam("Parameters.member.1.ParameterValue", "a")
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackInstances")
.formParam("StackSetName", setName)
.formParam("Accounts.member.1", ACCOUNT_B)
.formParam("Regions.member.1", REGION)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
assertQueueVisible(ACCOUNT_B, queue + "-a");
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "UpdateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", withExtra)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(400)
.body(containsString("<Message>Parameters: [Extra] must have values</Message>"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStackSet")
.formParam("StackSetName", setName)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200)
.body(not(containsString("Extra")));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ListStackSetOperations")
.formParam("StackSetName", setName)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200)
.body(not(containsString("<Action>UPDATE</Action>")));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "UpdateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", withExtra)
.formParam("Parameters.member.1.ParameterKey", "Suffix")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.formParam("Parameters.member.2.ParameterKey", "Extra")
.formParam("Parameters.member.2.ParameterValue", "x")
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ListStackSetOperations")
.formParam("StackSetName", setName)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200)
.body(containsString("<Action>UPDATE</Action>"))
.body(not(containsString("<Status>FAILED</Status>")));
}
@Test
void updateStackSetUsePreviousValueKeepsAValueTheStackSetTookFromItsDefault() {
// A stack set keeps only the values it was given, so UsePreviousValue also reads the
// previous template's Default: the instances deployed with it, and an update that drops
// that Default keeps them on it.
String setName = "upv-default-" + UUID.randomUUID().toString().substring(0, 8);
String queue = "upv-default-q-" + UUID.randomUUID().toString().substring(0, 8);
String template = """
{"Parameters":{"Suffix":{"Type":"String"%s}},
"Resources":{"Q":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":{"Fn::Sub":"%s-${Suffix}"}}}}}
""";
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", template.formatted(",\"Default\":\"d1\"", queue))
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackInstances")
.formParam("StackSetName", setName)
.formParam("Accounts.member.1", ACCOUNT_B)
.formParam("Regions.member.1", REGION)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
assertQueueVisible(ACCOUNT_B, queue + "-d1");
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "UpdateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", template.formatted("", queue))
.formParam("Parameters.member.1.ParameterKey", "Suffix")
.formParam("Parameters.member.1.UsePreviousValue", "true")
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStackSet")
.formParam("StackSetName", setName)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200)
.body(containsString("<ParameterValue>d1</ParameterValue>"));
assertQueueVisible(ACCOUNT_B, queue + "-d1");
}
@Test
void createStackInstancesThatIsRefusedFailsItsOperation() {
// An instance whose change set is refused fails the operation instead of leaving it
// RUNNING, which would refuse every later operation on the stack set.
String setName = "refused-op-" + UUID.randomUUID().toString().substring(0, 8);
String conditional = "refused-c-" + UUID.randomUUID().toString().substring(0, 8);
String dependent = "refused-d-" + UUID.randomUUID().toString().substring(0, 8);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackSet")
.formParam("StackSetName", setName)
.formParam("TemplateBody", conditionalDependentTemplate(ACCOUNT_B, conditional, dependent))
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackInstances")
.formParam("StackSetName", setName)
.formParam("Accounts.member.1", ACCOUNT_C)
.formParam("Regions.member.1", REGION)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(400)
.body(containsString("Unresolved resource dependencies"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ListStackSetOperations")
.formParam("StackSetName", setName)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200)
.body(containsString("<Status>FAILED</Status>"))
.body(not(containsString("<Status>RUNNING</Status>")));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStackInstances")
.formParam("StackSetName", setName)
.formParam("Accounts.member.1", ACCOUNT_B)
.formParam("Regions.member.1", REGION)
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200);
assertQueueVisible(ACCOUNT_B, conditional);
assertQueueVisible(ACCOUNT_B, dependent);
}
@Test
void createStackInstancesRejectsInvalidAccountBeforeDeployment() {
String setName = "invalid-account-" + UUID.randomUUID().toString().substring(0, 8);