mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
+36
-9
@@ -392,13 +392,16 @@ public class CloudFormationService implements ResourceProvider {
|
||||
// persistStack() stays outside: it is storage I/O, and compute()'s contract is that the
|
||||
// remapping function does short, non-blocking work.
|
||||
boolean isCreateType = changeSetType == null || "CREATE".equalsIgnoreCase(changeSetType);
|
||||
// An update names its stack by name or stack ID; the map is keyed by name. One that names no
|
||||
// live stack is keyed as given, so it lands in the missing-stack branch below.
|
||||
Stack live = isCreateType ? null : resolveStack(stackName, region, accountId);
|
||||
String canonicalStackName = live == null ? stackName : live.getStackName();
|
||||
ChangeSet[] created = new ChangeSet[1];
|
||||
Stack stack = stacks.compute(stackKey(accountId, stackName, region), (k, existing) -> {
|
||||
Stack stack = stacks.compute(stackKey(accountId, canonicalStackName, region), (k, existing) -> {
|
||||
Stack target;
|
||||
if (existing == null) {
|
||||
if (!isCreateType) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Stack with id " + stackName + " does not exist", 400);
|
||||
throw noStackToUpdate(stackName, region, accountId);
|
||||
}
|
||||
target = newStack(stackName, region, accountId);
|
||||
if (tags != null) target.getTags().putAll(tags);
|
||||
@@ -412,19 +415,24 @@ public class CloudFormationService implements ResourceProvider {
|
||||
"AWS::CloudFormation::Stack", "REVIEW_IN_PROGRESS", "User Initiated");
|
||||
}
|
||||
} else {
|
||||
String status = existing.getStatus();
|
||||
boolean reusableReviewPlaceholder =
|
||||
attachToReviewInProgressStack && "REVIEW_IN_PROGRESS".equals(existing.getStatus());
|
||||
attachToReviewInProgressStack && "REVIEW_IN_PROGRESS".equals(status);
|
||||
if (isCreateType && !reusableReviewPlaceholder) {
|
||||
throw new AwsException("AlreadyExistsException",
|
||||
"Stack [" + stackName + "] already exists", 400);
|
||||
}
|
||||
// A name whose stack has finished deleting names no live stack, even before it leaves the
|
||||
// map, and neither does a stack ID that is not the live stack's.
|
||||
if (!isCreateType && !stackName.equals(existing.getStackId())
|
||||
&& ("DELETE_COMPLETE".equals(status) || AwsArnUtils.isArn(stackName))) {
|
||||
throw noStackToUpdate(stackName, region, accountId);
|
||||
}
|
||||
// The message is the one real CloudFormation emits, down to its own "can not"
|
||||
// spelling and the stack id carried as "Stack:<arn>" with no space: clients match
|
||||
// on this string.
|
||||
if (!isCreateType && refusesUpdate(existing.getStatus())) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Stack:" + existing.getStackId() + " is in " + existing.getStatus()
|
||||
+ " state and can not be updated.", 400);
|
||||
if (!isCreateType && (refusesUpdate(status) || "DELETE_COMPLETE".equals(status))) {
|
||||
throw cannotBeUpdated(existing.getStackId(), status);
|
||||
}
|
||||
if (fromPreviousTemplate && !Objects.equals(templateBody, previousTemplateOf(existing))) {
|
||||
return existing;
|
||||
@@ -435,7 +443,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
ChangeSet cs = new ChangeSet();
|
||||
cs.setChangeSetId(AwsArnUtils.Arn.of("cloudformation", region, accountId, "changeSet/" + changeSetName + "/" + UUID.randomUUID()).toString());
|
||||
cs.setChangeSetName(changeSetName);
|
||||
cs.setStackName(stackName);
|
||||
cs.setStackName(canonicalStackName);
|
||||
cs.setStackId(target.getStackId());
|
||||
cs.setChangeSetType(changeSetType != null ? changeSetType : "CREATE");
|
||||
cs.setTemplateBody(resolvedTemplate);
|
||||
@@ -461,6 +469,21 @@ public class CloudFormationService implements ResourceProvider {
|
||||
return created[0];
|
||||
}
|
||||
|
||||
private static AwsException cannotBeUpdated(String stackId, String status) {
|
||||
return new AwsException("ValidationError",
|
||||
"Stack:" + stackId + " is in " + status + " state and can not be updated.", 400);
|
||||
}
|
||||
|
||||
// AWS refuses a deleted stack's ID by its state, while a name, or an ID it never issued,
|
||||
// does not exist.
|
||||
private AwsException noStackToUpdate(String stackName, String region, String accountId) {
|
||||
Stack deleted = resolveDeletedStack(stackName, region, accountId);
|
||||
if (deleted != null) {
|
||||
return cannotBeUpdated(deleted.getStackId(), deleted.getStatus());
|
||||
}
|
||||
return new AwsException("ValidationError", "Stack with id " + stackName + " does not exist", 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the change set's {@code StatusReason} when {@code templateBody} declares the SAM
|
||||
* transform and that transform fails, {@code null} when the transform is absent or succeeds.
|
||||
@@ -2883,6 +2906,10 @@ public class CloudFormationService implements ResourceProvider {
|
||||
if (stack != null) {
|
||||
return stack;
|
||||
}
|
||||
return resolveDeletedStack(stackNameOrArn, region, accountId);
|
||||
}
|
||||
|
||||
private Stack resolveDeletedStack(String stackNameOrArn, String region, String accountId) {
|
||||
if (stackNameOrArn != null && stackNameOrArn.startsWith("arn:")) {
|
||||
DeletedStackEntry deleted = deletedStacks.get(stackNameOrArn);
|
||||
if (deleted != null) {
|
||||
|
||||
Reference in new issue
Block a user