mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): resolve change sets by ARN without stack name (#4885)
* fix(cloudformation): resolve change sets by ARN without stack name * test(cloudformation): verify change set ARN execution with SDK * test(cloudformation): wait for change set execution status * fix(cloudformation): tighten change set ARN validation tests
This commit is contained in:
1 parent
27434be88a
commit
ba944df922
3 files changed
+304
-23
No files matched your search
+122
@@ -0,0 +1,122 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
||||
import software.amazon.awssdk.services.cloudformation.model.ChangeSetType;
|
||||
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Stack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
class CloudFormationChangeSetArnTest {
|
||||
|
||||
private static final String TEMPLATE = "Resources:\n Queue:\n Type: AWS::SQS::Queue\n";
|
||||
private static CloudFormationClient cloudFormation;
|
||||
|
||||
private final List<String> stacksToDelete = new ArrayList<>();
|
||||
|
||||
@BeforeAll
|
||||
static void createClient() {
|
||||
cloudFormation = TestFixtures.cloudFormationClient();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void closeClient() {
|
||||
cloudFormation.close();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteStacks() {
|
||||
for (String stackName : stacksToDelete) {
|
||||
try {
|
||||
cloudFormation.deleteStack(request -> request.stackName(stackName));
|
||||
} catch (CloudFormationException ignored) {
|
||||
// A failed assertion may leave a change set without a stack to delete.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkResolvesChangeSetArnWithoutStackNameAndExecutesItsStack() throws InterruptedException {
|
||||
String firstStack = newStackName();
|
||||
String secondStack = newStackName();
|
||||
String firstArn = createChangeSet(firstStack);
|
||||
String secondArn = createChangeSet(secondStack);
|
||||
|
||||
assertThat(firstArn).isNotEqualTo(secondArn);
|
||||
assertThat(cloudFormation.describeChangeSet(request -> request.changeSetName(secondArn)).stackName())
|
||||
.isEqualTo(secondStack);
|
||||
assertThat(cloudFormation.describeChangeSet(request -> request.changeSetName(firstArn)).stackName())
|
||||
.isEqualTo(firstStack);
|
||||
|
||||
cloudFormation.executeChangeSet(request -> request.changeSetName(secondArn));
|
||||
awaitStackCreated(secondStack);
|
||||
assertThat(cloudFormation.describeStackResource(request -> request.stackName(secondStack)
|
||||
.logicalResourceId("Queue")).stackResourceDetail().resourceStatusAsString())
|
||||
.isEqualTo("CREATE_COMPLETE");
|
||||
assertThat(awaitChangeSetExecuted(secondArn)).isEqualTo("EXECUTE_COMPLETE");
|
||||
assertThat(cloudFormation.describeStacks(request -> request.stackName(firstStack)).stacks().get(0)
|
||||
.stackStatusAsString()).isEqualTo("REVIEW_IN_PROGRESS");
|
||||
assertThat(cloudFormation.describeChangeSet(request -> request.changeSetName(firstArn))
|
||||
.executionStatusAsString()).isEqualTo("AVAILABLE");
|
||||
|
||||
cloudFormation.deleteChangeSet(request -> request.changeSetName(firstArn));
|
||||
assertThatThrownBy(() -> cloudFormation.describeChangeSet(request -> request.changeSetName(firstArn)))
|
||||
.isInstanceOf(CloudFormationException.class)
|
||||
.satisfies(error -> assertThat(((CloudFormationException) error).awsErrorDetails().errorCode())
|
||||
.isEqualTo("ChangeSetNotFoundException"));
|
||||
}
|
||||
|
||||
private String newStackName() {
|
||||
String stackName = TestFixtures.uniqueName("cfn-arn-sdk");
|
||||
stacksToDelete.add(stackName);
|
||||
return stackName;
|
||||
}
|
||||
|
||||
private static String createChangeSet(String stackName) {
|
||||
return cloudFormation.createChangeSet(request -> request.stackName(stackName)
|
||||
.changeSetName("shared-name").changeSetType(ChangeSetType.CREATE)
|
||||
.templateBody(TEMPLATE)).id();
|
||||
}
|
||||
|
||||
private static void awaitStackCreated(String stackName) throws InterruptedException {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
|
||||
while (System.nanoTime() < deadline) {
|
||||
Stack stack = cloudFormation.describeStacks(request -> request.stackName(stackName)).stacks().get(0);
|
||||
String status = stack.stackStatusAsString();
|
||||
if ("CREATE_COMPLETE".equals(status)) {
|
||||
return;
|
||||
}
|
||||
if (status.endsWith("_FAILED") || status.contains("ROLLBACK")) {
|
||||
throw new AssertionError(stackName + " reached " + status + ": " + stack.stackStatusReason());
|
||||
}
|
||||
Thread.sleep(100);
|
||||
}
|
||||
throw new AssertionError(stackName + " did not reach CREATE_COMPLETE within 60 seconds");
|
||||
}
|
||||
|
||||
private static String awaitChangeSetExecuted(String changeSetArn) throws InterruptedException {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
|
||||
String lastStatus = null;
|
||||
while (System.nanoTime() < deadline) {
|
||||
lastStatus = cloudFormation.describeChangeSet(request -> request.changeSetName(changeSetArn))
|
||||
.executionStatusAsString();
|
||||
if ("EXECUTE_COMPLETE".equals(lastStatus)) {
|
||||
return lastStatus;
|
||||
}
|
||||
if ("EXECUTE_FAILED".equals(lastStatus)) {
|
||||
throw new AssertionError(changeSetArn + " reached EXECUTE_FAILED");
|
||||
}
|
||||
Thread.sleep(100);
|
||||
}
|
||||
throw new AssertionError(changeSetArn + " did not execute within 60 seconds; last status: " + lastStatus);
|
||||
}
|
||||
}
|
||||
+45
-23
@@ -452,14 +452,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
// ── DescribeChangeSet ─────────────────────────────────────────────────────
|
||||
|
||||
public ChangeSet describeChangeSet(String stackName, String changeSetName, String region) {
|
||||
Stack stack = getStackOrThrow(stackName, region);
|
||||
ChangeSet cs = stack.getChangeSets().get(
|
||||
resolveChangeSetName(changeSetName, region, currentAccount()));
|
||||
if (cs == null) {
|
||||
throw new AwsException("ChangeSetNotFoundException",
|
||||
"ChangeSet [" + changeSetName + "] does not exist", 400);
|
||||
}
|
||||
return cs;
|
||||
Stack stack = getStackForChangeSet(stackName, changeSetName, region, currentAccount());
|
||||
return getChangeSetOrThrow(stack, changeSetName, region, currentAccount());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -827,8 +821,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
// compute() holds the stack's per-key lock for the whole claim, so only one racing execution can win.
|
||||
private Future<?> claimAndSubmitExecution(String stackNameOrArn, String changeSetName, String region,
|
||||
String accountId, boolean requireAvailable) {
|
||||
String canonicalStackName = getStackOrThrow(stackNameOrArn, region, accountId).getStackName();
|
||||
String resolvedChangeSetName = resolveChangeSetName(changeSetName, region, accountId);
|
||||
String canonicalStackName = getStackForChangeSet(stackNameOrArn, changeSetName, region,
|
||||
accountId).getStackName();
|
||||
|
||||
ClaimedExecution[] claimed = new ClaimedExecution[1];
|
||||
StackMutationSnapshot[] snapshot = new StackMutationSnapshot[1];
|
||||
@@ -837,11 +831,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
throw new AwsException("ValidationError",
|
||||
"Stack with id " + stackNameOrArn + " does not exist", 400);
|
||||
}
|
||||
ChangeSet cs = existing.getChangeSets().get(resolvedChangeSetName);
|
||||
if (cs == null) {
|
||||
throw new AwsException("ChangeSetNotFoundException",
|
||||
"ChangeSet [" + changeSetName + "] does not exist", 400);
|
||||
}
|
||||
ChangeSet cs = getChangeSetOrThrow(existing, changeSetName, region, accountId);
|
||||
String executionStatus = cs.getExecutionStatus();
|
||||
boolean eligible = requireAvailable
|
||||
? "AVAILABLE".equals(executionStatus)
|
||||
@@ -947,14 +937,10 @@ public class CloudFormationService implements ResourceProvider {
|
||||
// ── DeleteChangeSet ───────────────────────────────────────────────────────
|
||||
|
||||
public void deleteChangeSet(String stackName, String changeSetName, String region) {
|
||||
Stack stack = getStackOrThrow(stackName, region);
|
||||
String name = resolveChangeSetName(changeSetName, region, currentAccount());
|
||||
ChangeSet cs = stack.getChangeSets().get(name);
|
||||
if (cs == null) {
|
||||
throw new AwsException("ChangeSetNotFoundException",
|
||||
"ChangeSet [" + changeSetName + "] does not exist", 400);
|
||||
}
|
||||
stack.getChangeSets().remove(name);
|
||||
String accountId = currentAccount();
|
||||
Stack stack = getStackForChangeSet(stackName, changeSetName, region, accountId);
|
||||
ChangeSet cs = getChangeSetOrThrow(stack, changeSetName, region, accountId);
|
||||
stack.getChangeSets().remove(cs.getChangeSetName());
|
||||
persistStack(stack);
|
||||
}
|
||||
|
||||
@@ -2937,6 +2923,42 @@ public class CloudFormationService implements ResourceProvider {
|
||||
"Invalid change set ARN: " + changeSetNameOrArn, 400);
|
||||
}
|
||||
|
||||
private Stack getStackForChangeSet(String stackNameOrArn, String changeSetNameOrArn,
|
||||
String region, String accountId) {
|
||||
if (stackNameOrArn != null && !stackNameOrArn.isBlank()) {
|
||||
return getStackOrThrow(stackNameOrArn, region, accountId);
|
||||
}
|
||||
if (changeSetNameOrArn == null || !changeSetNameOrArn.startsWith("arn:")) {
|
||||
throw new AwsException("ValidationError", "StackName is required when ChangeSetName is not an ARN", 400);
|
||||
}
|
||||
// A change-set ARN contains its name and UUID, but not its stack name. Search only
|
||||
// stacks in the caller's account and region, and match the complete ARN so another
|
||||
// stack's change set with the same short name cannot be selected accidentally.
|
||||
String changeSetName = resolveChangeSetName(changeSetNameOrArn, region, accountId);
|
||||
for (Stack stack : stacks.values()) {
|
||||
if (accountId.equals(ownerAccount(stack)) && region.equals(stack.getRegion())) {
|
||||
ChangeSet changeSet = stack.getChangeSets().get(changeSetName);
|
||||
if (changeSet != null && changeSetNameOrArn.equals(changeSet.getChangeSetId())) {
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new AwsException("ChangeSetNotFoundException",
|
||||
"ChangeSet [" + changeSetNameOrArn + "] does not exist", 400);
|
||||
}
|
||||
|
||||
private ChangeSet getChangeSetOrThrow(Stack stack, String changeSetNameOrArn,
|
||||
String region, String accountId) {
|
||||
ChangeSet changeSet = stack.getChangeSets().get(
|
||||
resolveChangeSetName(changeSetNameOrArn, region, accountId));
|
||||
if (changeSet == null || (changeSetNameOrArn != null && changeSetNameOrArn.startsWith("arn:")
|
||||
&& !changeSetNameOrArn.equals(changeSet.getChangeSetId()))) {
|
||||
throw new AwsException("ChangeSetNotFoundException",
|
||||
"ChangeSet [" + changeSetNameOrArn + "] does not exist", 400);
|
||||
}
|
||||
return changeSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a live stack by name or ARN. An ARN is a stack id and resolves only to the stack
|
||||
* carrying that exact id, never to a newer stack that reused the name.
|
||||
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudFormationChangeSetArnIntegrationTest {
|
||||
|
||||
private static final String TEMPLATE = "Resources:\n Queue:\n Type: AWS::SQS::Queue\n";
|
||||
|
||||
private final List<String> stacksToDelete = new ArrayList<>();
|
||||
|
||||
@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("/");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeSetArnWorksWithoutStackNameForDescribeExecuteAndDelete() {
|
||||
String firstStack = newStackName("first");
|
||||
String secondStack = newStackName("second");
|
||||
String firstArn = createChangeSet(firstStack, "shared-name");
|
||||
String secondArn = createChangeSet(secondStack, "shared-name");
|
||||
|
||||
request("DescribeChangeSet", secondArn).then().statusCode(200)
|
||||
.body(containsString("<StackName>" + secondStack + "</StackName>"))
|
||||
.body(containsString("<ChangeSetId>" + secondArn + "</ChangeSetId>"));
|
||||
|
||||
// An ARN's UUID must be checked too; a matching short name on another stack is not enough.
|
||||
request("DescribeChangeSet", firstArn + "-wrong").then().statusCode(400)
|
||||
.body(containsString("<Code>ChangeSetNotFoundException</Code>"));
|
||||
request("DescribeChangeSet", firstArn.replace(":000000000000:", ":000000000001:"))
|
||||
.then().statusCode(400)
|
||||
.body(containsString("<Code>ValidationError</Code>"));
|
||||
request("DescribeChangeSet", firstArn.replace(":us-east-1:", ":us-west-2:"))
|
||||
.then().statusCode(400)
|
||||
.body(containsString("<Code>ValidationError</Code>"));
|
||||
|
||||
request("ExecuteChangeSet", secondArn).then().statusCode(200);
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(secondStack).status());
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackResources")
|
||||
.formParam("StackName", secondStack)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<LogicalResourceId>Queue</LogicalResourceId>"))
|
||||
.body(containsString("<ResourceStatus>CREATE_COMPLETE</ResourceStatus>"));
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", firstStack)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<StackStatus>REVIEW_IN_PROGRESS</StackStatus>"));
|
||||
request("DeleteChangeSet", firstArn).then().statusCode(200);
|
||||
|
||||
request("DescribeChangeSet", firstArn).then().statusCode(400)
|
||||
.body(containsString("<Code>ChangeSetNotFoundException</Code>"));
|
||||
request("DescribeChangeSet", secondArn).then().statusCode(200)
|
||||
.body(containsString("<StackName>" + secondStack + "</StackName>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shortChangeSetNameStillRequiresStackNameAndExplicitStackNameStillWorks() {
|
||||
String stackName = newStackName("named");
|
||||
String arn = createChangeSet(stackName, "named-change-set");
|
||||
|
||||
request("DescribeChangeSet", "named-change-set").then().statusCode(400)
|
||||
.body(containsString("<Code>ValidationError</Code>"));
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", arn)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<ChangeSetId>" + arn + "</ChangeSetId>"));
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", arn + "-wrong")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body(containsString("<Code>ChangeSetNotFoundException</Code>"));
|
||||
}
|
||||
|
||||
private String newStackName(String suffix) {
|
||||
String name = "cfn-cs-arn-" + suffix + "-" + Long.toString(System.nanoTime(), 36);
|
||||
stacksToDelete.add(name);
|
||||
return name;
|
||||
}
|
||||
|
||||
private static String createChangeSet(String stackName, String changeSetName) {
|
||||
String xml = given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateChangeSet")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("ChangeSetName", changeSetName)
|
||||
.formParam("ChangeSetType", "CREATE")
|
||||
.formParam("TemplateBody", TEMPLATE)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().asString();
|
||||
String changeSetArn = XmlParser.extractFirst(xml, "Id", null);
|
||||
assertNotNull(changeSetArn, xml);
|
||||
return changeSetArn;
|
||||
}
|
||||
|
||||
private static Response request(String action, String changeSetArn) {
|
||||
return given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", action)
|
||||
.formParam("ChangeSetName", changeSetArn)
|
||||
.when().post("/");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user