mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): accept the stack ID in UpdateStack and CreateChangeSet (#4846)
* test(cloudformation): cover UpdateStack and CreateChangeSet by stack ID against AWS * fix(cloudformation): accept the stack ID in UpdateStack and CreateChangeSet StackName is "the name or the unique stack ID" for both actions, but an UPDATE change set looked the stack up by the raw value, so a stack ARN missed the name-keyed map and failed with "Stack with id ... does not exist". Resolve the canonical stack name first, as ExecuteChangeSet already does, and record it on the change set. CREATE change sets are unchanged. Closes #4842 * fix(cloudformation): refuse updating a stack that has finished deleting A delete marks the stack DELETE_COMPLETE before it leaves the live map, so an update in that window was accepted and its persist could revive the stack. An update of a DELETE_COMPLETE stack now reports that the stack does not exist. * test(cloudformation): delete each stack once after the stack ID tests * test(cloudformation): wait until a deleted stack's name is free before reusing it Floci reports DELETE_COMPLETE just before it drops the stack, so a same-name CreateStack could hit AlreadyExistsException; wait until DescribeStacks no longer finds the name. * test(cloudformation): name the stale stack ID test plainly * fix(cloudformation): refuse a deleted stack's ID with AWS's DELETE_COMPLETE message UpdateStack and UPDATE change sets given the stack ID of a deleted stack now fail with "Stack:<id> is in DELETE_COMPLETE state and can not be updated.", as measured on AWS. A stack name, or a stack ID that never existed, still gets "does not exist".
This commit is contained in:
1 parent
d8b170a144
commit
efd9512578
4 files changed
+535
-10
No files matched your search
+24
-1
@@ -19,6 +19,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -37,7 +38,8 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Covers rollback cleanup when another actor removes a resource after its create succeeded, and
|
||||
* which resources a stack delete walks after a failed operation left them in a failed status.
|
||||
* which resources a stack delete walks after a failed operation left them in a failed status, and
|
||||
* that a stack which finished deleting is no longer updatable.
|
||||
*/
|
||||
class CloudFormationServiceRollbackTest {
|
||||
|
||||
@@ -171,6 +173,27 @@ class CloudFormationServiceRollbackTest {
|
||||
assertEquals("DELETE_FAILED", bucket.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateChangeSet_onStackThatFinishedDeleting_refusesItsIdAndMissesItsName() {
|
||||
String template = "{\"Resources\":{\"Queue\":{\"Type\":\"AWS::SQS::Queue\"}}}";
|
||||
service.createChangeSet("finished-deleting", "create", "CREATE", template, null,
|
||||
Map.of(), List.of(), Map.of(), REGION, ACCOUNT);
|
||||
Stack stack = service.describeStacks("finished-deleting", REGION, ACCOUNT).getFirst();
|
||||
// A delete marks the stack DELETE_COMPLETE before it leaves the live map.
|
||||
stack.setStatus("DELETE_COMPLETE");
|
||||
|
||||
for (String nameOrId : List.of(stack.getStackName(), stack.getStackId())) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.createChangeSet(
|
||||
nameOrId, "update", "UPDATE", template, null, Map.of(), List.of(), Map.of(),
|
||||
REGION, ACCOUNT));
|
||||
assertEquals("ValidationError", error.getErrorCode());
|
||||
assertEquals(nameOrId.equals(stack.getStackId())
|
||||
? "Stack:" + nameOrId + " is in DELETE_COMPLETE state and can not be updated."
|
||||
: "Stack with id " + nameOrId + " does not exist", error.getMessage());
|
||||
}
|
||||
assertEquals(Set.of("create"), stack.getChangeSets().keySet());
|
||||
}
|
||||
|
||||
private static StackResource resource(String logicalId, String physicalId, String resourceType, String status) {
|
||||
StackResource resource = new StackResource();
|
||||
resource.setLogicalId(logicalId);
|
||||
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* UpdateStack and CreateChangeSet take "the name or the unique stack ID" as StackName. An UPDATE
|
||||
* change set given the stack ID has to land on the stack that ID names, not on a new key spelled
|
||||
* like the ARN (floci-io/floci#4842).
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationStackIdUpdateIntegrationTest {
|
||||
|
||||
private static final String TEMPLATE = """
|
||||
{
|
||||
"Parameters": {
|
||||
"ParameterName": {"Type": "String"},
|
||||
"ParameterValue": {"Type": "String"}
|
||||
},
|
||||
"Resources": {
|
||||
"Param": {
|
||||
"Type": "AWS::SSM::Parameter",
|
||||
"Properties": {
|
||||
"Name": {"Ref": "ParameterName"},
|
||||
"Type": "String",
|
||||
"Value": {"Ref": "ParameterValue"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
private final Set<String> stacksToDelete = new LinkedHashSet<>();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteStacks() {
|
||||
for (String stackName : stacksToDelete) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
stacksToDelete.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateStackAcceptsTheStackId() {
|
||||
String stackName = "stack-id-update-" + Long.toString(System.nanoTime(), 36);
|
||||
String stackId = createStack(stackName);
|
||||
|
||||
withParameters(stackName, "second")
|
||||
.formParam("Action", "UpdateStack")
|
||||
.formParam("StackName", stackId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackId>" + stackId + "</StackId>"));
|
||||
|
||||
assertThat(CfnStackWaits.awaitTerminal(stackName).status(), equalTo("UPDATE_COMPLETE"));
|
||||
assertThat(parameterValue(stackName), equalTo("second"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateChangeSetAcceptsTheStackId() {
|
||||
String stackName = "stack-id-changeset-" + Long.toString(System.nanoTime(), 36);
|
||||
String stackId = createStack(stackName);
|
||||
|
||||
XmlPath created = withParameters(stackName, "second")
|
||||
.formParam("Action", "CreateChangeSet")
|
||||
.formParam("StackName", stackId)
|
||||
.formParam("ChangeSetName", "update-by-stack-id")
|
||||
.formParam("ChangeSetType", "UPDATE")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath();
|
||||
assertThat(created.getString("CreateChangeSetResponse.CreateChangeSetResult.StackId"), equalTo(stackId));
|
||||
String changeSetId = created.getString("CreateChangeSetResponse.CreateChangeSetResult.Id");
|
||||
|
||||
XmlPath described = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet")
|
||||
.formParam("StackName", stackId)
|
||||
.formParam("ChangeSetName", changeSetId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath();
|
||||
assertThat(described.getString("DescribeChangeSetResponse.DescribeChangeSetResult.StackName"),
|
||||
equalTo(stackName));
|
||||
assertThat(described.getString("DescribeChangeSetResponse.DescribeChangeSetResult.StackId"),
|
||||
equalTo(stackId));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ExecuteChangeSet")
|
||||
.formParam("StackName", stackId)
|
||||
.formParam("ChangeSetName", changeSetId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
assertThat(CfnStackWaits.awaitTerminal(stackName).status(), equalTo("UPDATE_COMPLETE"));
|
||||
assertThat(parameterValue(stackName), equalTo("second"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateStackWithTheArnOfAnUnknownStackIsAValidationError() {
|
||||
String stackId = "arn:aws:cloudformation:us-east-1:000000000000:stack/stack-id-missing-"
|
||||
+ Long.toString(System.nanoTime(), 36) + "/00000000-0000-0000-0000-000000000000";
|
||||
|
||||
withParameters("stack-id-missing", "second")
|
||||
.formParam("Action", "UpdateStack")
|
||||
.formParam("StackName", stackId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>ValidationError</Code>"))
|
||||
.body(containsString("Stack with id " + stackId + " does not exist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedStackIdDoesNotUpdateANewStackOfTheSameName() {
|
||||
String stackName = "stack-id-stale-" + Long.toString(System.nanoTime(), 36);
|
||||
String staleStackId = createStack(stackName);
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
CfnStackWaits.awaitStackDeleted(stackName);
|
||||
String refusal = "Stack:" + staleStackId + " is in DELETE_COMPLETE state and can not be updated.";
|
||||
withParameters(stackName, "second")
|
||||
.formParam("Action", "UpdateStack")
|
||||
.formParam("StackName", staleStackId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>ValidationError</Code>"))
|
||||
.body(containsString(refusal));
|
||||
String currentStackId = createStack(stackName);
|
||||
|
||||
withParameters(stackName, "second")
|
||||
.formParam("Action", "UpdateStack")
|
||||
.formParam("StackName", staleStackId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>ValidationError</Code>"))
|
||||
.body(containsString(refusal));
|
||||
withParameters(stackName, "second")
|
||||
.formParam("Action", "CreateChangeSet")
|
||||
.formParam("StackName", staleStackId)
|
||||
.formParam("ChangeSetName", "update-by-stale-stack-id")
|
||||
.formParam("ChangeSetType", "UPDATE")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>ValidationError</Code>"))
|
||||
.body(containsString(refusal));
|
||||
|
||||
assertThat(CfnStackWaits.awaitTerminal(stackName).status(), equalTo("CREATE_COMPLETE"));
|
||||
assertThat(stackId(stackName), equalTo(currentStackId));
|
||||
assertThat(parameterValue(stackName), equalTo("first"));
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ListChangeSets")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("update-by-stale-stack-id")));
|
||||
}
|
||||
|
||||
private String createStack(String stackName) {
|
||||
stacksToDelete.add(stackName);
|
||||
String stackId = withParameters(stackName, "first")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath()
|
||||
.getString("CreateStackResponse.CreateStackResult.StackId");
|
||||
assertThat(CfnStackWaits.awaitTerminal(stackName).status(), equalTo("CREATE_COMPLETE"));
|
||||
assertThat(parameterValue(stackName), equalTo("first"));
|
||||
return stackId;
|
||||
}
|
||||
|
||||
private static RequestSpecification withParameters(String stackName, String value) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("TemplateBody", TEMPLATE)
|
||||
.formParam("Parameters.member.1.ParameterKey", "ParameterName")
|
||||
.formParam("Parameters.member.1.ParameterValue", "/" + stackName)
|
||||
.formParam("Parameters.member.2.ParameterKey", "ParameterValue")
|
||||
.formParam("Parameters.member.2.ParameterValue", value);
|
||||
}
|
||||
|
||||
private static String stackId(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath()
|
||||
.getString("DescribeStacksResponse.DescribeStacksResult.Stacks.member.StackId");
|
||||
}
|
||||
|
||||
private static String parameterValue(String stackName) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AmazonSSM.GetParameter")
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.body("{\"Name\":\"/" + stackName + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().jsonPath().getString("Parameter.Value");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user