mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): replace Cognito user pool groups like CloudFormation (#4841)
* test(cloudformation): cover AWS::Cognito::UserPoolGroup replacement against AWS * fix(cloudformation): replace Cognito user pool groups on create-only changes AWS::Cognito::UserPoolGroup left the old group behind on a rename, kept the explicit name when GroupName was dropped, and tried to update a group that did not exist when UserPoolId changed. The provisioner now creates the group whenever a create-only property changes and hands the displaced group to ReplacementCleanup, which deletes it once the update commits or deletes the replacement and restores the prior name and pool when the update rolls back. A pool move that keeps an explicit name keeps the physical id, so, as on AWS, nothing is deleted and the old-pool group stays. A generated name is kept only for an in-place update; a pool move or a dropped explicit name generates a fresh one. Each provisioned name is recorded with its pool so a displaced group is deleted from the pool it lives in. Closes #4836 * test(cloudformation): cover a failed update after moving a named Cognito group against AWS * fix(cloudformation): roll back named Cognito group pool moves and read legacy name modes A named pool move keeps the physical id, so it now records a replacement that displaces nothing. A failed update points the group back at the old pool and, as measured on AWS, leaves the new pool's group, and the stack delete reads the pool from UserPoolId. A resource persisted without FlociGroupNameMode reads its name as generated only when it has the generated shape, so dropping an explicit GroupName replaces it. * test(cloudformation): cover declaring a Cognito group's generated name explicitly against AWS Measured on AWS (eu-west-1): updating an unnamed group to declare GroupName equal to its generated name, same pool, fails the Group with UPDATE_FAILED "CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename <name> and update the stack again." before anything is created. The stack ends UPDATE_ROLLBACK_COMPLETE, Ref and the physical id stay the generated name, and the group keeps its pool, description, precedence and creation date. * fix(cloudformation): refuse declaring a Cognito group's generated name explicitly GroupName is create-only, so declaring an unnamed group's generated name explicitly in the same pool requires a replacement that keeps both the pool and the name. Measured on AWS, CloudFormation refuses it before creating anything: the Group goes UPDATE_FAILED with "CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename <name> and update the stack again.", the stack ends UPDATE_ROLLBACK_COMPLETE, and Ref and the group stay unchanged. Floci updated the group in place and reported UPDATE_COMPLETE; it now raises the same ValidationError. * fix(cloudformation): keep only owed and current Cognito groups in the pool record The group pool record added the prior and current name on every provision and never dropped one, so it grew with every name the resource ever had. It is now rebuilt after the replacement record, keeping only the names still owed a delete, with their recorded pools, plus the current name. * fix(cloudformation): refuse a declared generated Cognito group name only when the mode was recorded The refusal of a declared generated name read the prior mode from the name's shape when no mode was recorded, so a legacy group explicitly named in the generated shape had a valid in-place update refused. Only a recorded generated mode refuses now; a legacy record updates in place, as before the mode existed. * test(cloudformation): read stack outputs with XmlParser The Cognito group integration test read stack outputs by splitting the DescribeStacks XML on tag strings. It now uses XmlParser.extractPairs, as the other CloudFormation integration tests do.
This commit is contained in:
1 parent
69b4d17c67
commit
e9f2463103
8 files changed
+1224
-14
No files matched your search
+318
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.config.EncoderConfig;
|
||||
@@ -8,13 +9,19 @@ import io.restassured.parsing.Parser;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudFormationCognitoUserPoolGroupIntegrationTest {
|
||||
@@ -142,6 +149,155 @@ class CloudFormationCognitoUserPoolGroupIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void renamingAGroupReplacesItAndDeletesTheGroupUnderTheOldName() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-cognito-group-rename-" + suffix;
|
||||
|
||||
String poolId = createStack(stackName, poolAndGroupsTemplate("admin", 0));
|
||||
|
||||
updateStack(stackName, poolAndGroupsTemplate("operators", 0));
|
||||
|
||||
String describeXml = describeStack(stackName);
|
||||
assertThat(describeXml, containsString("<StackStatus>UPDATE_COMPLETE</StackStatus>"));
|
||||
assertEquals("operators", output(describeXml, "AdminGroupName"));
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + poolId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups.GroupName", containsInAnyOrder("operators", "readers"));
|
||||
cognito("GetGroup", "{\"UserPoolId\": \"" + poolId + "\", \"GroupName\": \"admin\"}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void movingAnUnnamedGroupToAnotherPoolReplacesItUnderANewGeneratedName() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-cognito-group-move-" + suffix;
|
||||
|
||||
String firstPool = createStack(stackName, twoPoolsAndUnnamedGroupTemplate("FirstPool"));
|
||||
String describeXml = describeStack(stackName);
|
||||
String secondPool = output(describeXml, "SecondPoolId");
|
||||
String original = output(describeXml, "GroupName");
|
||||
|
||||
updateStack(stackName, twoPoolsAndUnnamedGroupTemplate("SecondPool"));
|
||||
|
||||
describeXml = describeStack(stackName);
|
||||
assertThat(describeXml, containsString("<StackStatus>UPDATE_COMPLETE</StackStatus>"));
|
||||
String replacement = output(describeXml, "GroupName");
|
||||
assertNotEquals(original, replacement, "a pool move replaces the group under a fresh name");
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + secondPool + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups.GroupName", contains(replacement));
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + firstPool + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedUpdateAfterMovingANamedGroupKeepsTheOldPoolGroupAndLeavesTheNewOneBehind() {
|
||||
// Measured on AWS: the group's physical id, the bare name, does not change, so the rollback
|
||||
// points the resource at the old pool's group again and skips deleting the new pool's group.
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-cognito-group-move-rollback-" + suffix;
|
||||
String name = "movers-" + suffix;
|
||||
String firstPool = createPool(stackName + "-p1");
|
||||
String secondPool = createPool(stackName + "-p2");
|
||||
|
||||
createStackAndWait(stackName, namedGroupTemplate(firstPool, name, false));
|
||||
|
||||
// Duplicate depends on Group and claims the new pool and the same name, so it fails after
|
||||
// the group exists in the new pool and the update rolls back.
|
||||
updateStack(stackName, namedGroupTemplate(secondPool, name, true));
|
||||
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
|
||||
assertEquals("UPDATE_ROLLBACK_COMPLETE", state.status(), state.reason());
|
||||
assertEquals(name, output(describeStack(stackName), "GroupRef"));
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackResource")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("LogicalResourceId", "Group")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<PhysicalResourceId>" + name + "</PhysicalResourceId>"));
|
||||
cognito("GetGroup", "{\"UserPoolId\": \"" + firstPool + "\", \"GroupName\": \"" + name + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Group.Description", equalTo("d1"))
|
||||
.body("Group.Precedence", equalTo(1));
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + firstPool + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups.GroupName", contains(name));
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + secondPool + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups.GroupName", contains(name));
|
||||
|
||||
deleteStackAndWait(stackName);
|
||||
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + firstPool + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups", hasSize(0));
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + secondPool + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups.GroupName", contains(name));
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaringAGeneratedGroupNameExplicitlyIsRefusedAndRollsBack() {
|
||||
// Measured on AWS: GroupName is create-only, so declaring the generated name requires a
|
||||
// replacement keeping the same pool and name, which is refused before anything is created.
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-cognito-group-declare-" + suffix;
|
||||
String poolId = createPool(stackName);
|
||||
|
||||
createStackAndWait(stackName, unnamedGroupTemplate(poolId));
|
||||
String generated = output(describeStack(stackName), "GroupRef");
|
||||
Object createdAt = cognito("GetGroup",
|
||||
"{\"UserPoolId\": \"" + poolId + "\", \"GroupName\": \"" + generated + "\"}")
|
||||
.statusCode(200)
|
||||
.extract().path("Group.CreationDate");
|
||||
|
||||
updateStack(stackName, namedGroupTemplate(poolId, generated, false));
|
||||
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
|
||||
assertEquals("UPDATE_ROLLBACK_COMPLETE", state.status(), state.reason());
|
||||
List<String> failures = XmlParser.extractGroups(describeEvents(stackName), "member").stream()
|
||||
.filter(event -> "Group".equals(event.get("LogicalResourceId"))
|
||||
&& "UPDATE_FAILED".equals(event.get("ResourceStatus")))
|
||||
.map(event -> event.get("ResourceStatusReason"))
|
||||
.toList();
|
||||
assertThat(failures, contains(containsString("CloudFormation cannot update a stack when a custom-named"
|
||||
+ " resource requires replacing. Rename " + generated + " and update the stack again.")));
|
||||
assertEquals(generated, output(describeStack(stackName), "GroupRef"));
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackResource")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("LogicalResourceId", "Group")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<PhysicalResourceId>" + generated + "</PhysicalResourceId>"));
|
||||
cognito("GetGroup", "{\"UserPoolId\": \"" + poolId + "\", \"GroupName\": \"" + generated + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Group.Description", equalTo("d1"))
|
||||
.body("Group.Precedence", equalTo(1))
|
||||
.body("Group.CreationDate", equalTo(createdAt));
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + poolId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups.GroupName", contains(generated));
|
||||
|
||||
deleteStackAndWait(stackName);
|
||||
|
||||
cognito("ListGroups", "{\"UserPoolId\": \"" + poolId + "\"}")
|
||||
.statusCode(200)
|
||||
.body("Groups", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolSchemaCustomAttributesAreNamespacedAndStandardOnesAreNot() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
@@ -190,6 +346,84 @@ class CloudFormationCognitoUserPoolGroupIntegrationTest {
|
||||
.split("<OutputValue>")[1].split("</OutputValue>")[0];
|
||||
}
|
||||
|
||||
private void updateStack(String stackName, String template) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "UpdateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/** Creates the stack and waits for CREATE_COMPLETE, for templates without a PoolId output. */
|
||||
private void createStackAndWait(String stackName, String template) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
|
||||
assertEquals("CREATE_COMPLETE", state.status(), state.reason());
|
||||
}
|
||||
|
||||
private void deleteStackAndWait(String stackName) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
CfnStackWaits.awaitStackDeleted(stackName);
|
||||
}
|
||||
|
||||
private String createPool(String poolName) {
|
||||
return cognito("CreateUserPool", "{\"PoolName\": \"" + poolName + "\"}")
|
||||
.statusCode(200)
|
||||
.extract().path("UserPool.Id");
|
||||
}
|
||||
|
||||
private String describeStack(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
private String describeEvents(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
private static String output(String describeXml, String key) {
|
||||
return XmlParser.extractPairs(describeXml, "Outputs", "OutputKey", "OutputValue").get(key);
|
||||
}
|
||||
|
||||
private io.restassured.response.ValidatableResponse cognito(String target, String body) {
|
||||
return given()
|
||||
.config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig()
|
||||
@@ -241,6 +475,90 @@ class CloudFormationCognitoUserPoolGroupIntegrationTest {
|
||||
""".formatted(adminGroupName, adminPrecedence);
|
||||
}
|
||||
|
||||
/** Two pools and a group without a GroupName, placed in the pool named by {@code poolLogicalId}. */
|
||||
private static String twoPoolsAndUnnamedGroupTemplate(String poolLogicalId) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"FirstPool": {
|
||||
"Type": "AWS::Cognito::UserPool",
|
||||
"Properties": { "UserPoolName": "cfn-group-pool-first" }
|
||||
},
|
||||
"SecondPool": {
|
||||
"Type": "AWS::Cognito::UserPool",
|
||||
"Properties": { "UserPoolName": "cfn-group-pool-second" }
|
||||
},
|
||||
"Group": {
|
||||
"Type": "AWS::Cognito::UserPoolGroup",
|
||||
"Properties": {
|
||||
"UserPoolId": { "Ref": "%s" },
|
||||
"Description": "moves between pools"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"PoolId": { "Value": { "Ref": "FirstPool" } },
|
||||
"SecondPoolId": { "Value": { "Ref": "SecondPool" } },
|
||||
"GroupName": { "Value": { "Ref": "Group" } }
|
||||
}
|
||||
}
|
||||
""".formatted(poolLogicalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* A named group in a pool created outside the stack. With {@code duplicate}, a second group of
|
||||
* the same pool and name depends on it and fails with GroupExistsException once it exists.
|
||||
*/
|
||||
private static String namedGroupTemplate(String poolId, String groupName, boolean duplicate) {
|
||||
String duplicateResource = !duplicate ? "" : """
|
||||
,
|
||||
"Duplicate": {
|
||||
"Type": "AWS::Cognito::UserPoolGroup",
|
||||
"DependsOn": "Group",
|
||||
"Properties": { "UserPoolId": "%s", "GroupName": "%s" }
|
||||
}
|
||||
""".formatted(poolId, groupName);
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Group": {
|
||||
"Type": "AWS::Cognito::UserPoolGroup",
|
||||
"Properties": {
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "%s",
|
||||
"Description": "d1",
|
||||
"Precedence": 1
|
||||
}
|
||||
}%s
|
||||
},
|
||||
"Outputs": {
|
||||
"GroupRef": { "Value": { "Ref": "Group" } }
|
||||
}
|
||||
}
|
||||
""".formatted(poolId, groupName, duplicateResource);
|
||||
}
|
||||
|
||||
/** The named group's template without a GroupName, so CloudFormation generates one. */
|
||||
private static String unnamedGroupTemplate(String poolId) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Group": {
|
||||
"Type": "AWS::Cognito::UserPoolGroup",
|
||||
"Properties": {
|
||||
"UserPoolId": "%s",
|
||||
"Description": "d1",
|
||||
"Precedence": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"GroupRef": { "Value": { "Ref": "Group" } }
|
||||
}
|
||||
}
|
||||
""".formatted(poolId);
|
||||
}
|
||||
|
||||
private static String poolAndStudentGroupOnlyTemplate() {
|
||||
return """
|
||||
{
|
||||
|
||||
+217
@@ -14,6 +14,9 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -144,6 +147,220 @@ class CognitoUserPoolGroupCfnProvisionerTest {
|
||||
assertEquals("operators", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void renamingReplacesTheGroupAndTheCleanupDeletesTheOldNameInItsPool() {
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", "admin"), ctx());
|
||||
|
||||
provisioner.provision(r, props("us-east-1_a", "operators"), ctx("admin"));
|
||||
|
||||
verify(cognito).createGroup("us-east-1_a", "operators", null, null, null);
|
||||
assertEquals("operators", r.getPhysicalId());
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals("admin", provisioner.updateCleanupPhysicalId(r));
|
||||
verify(cognito, never()).deleteGroup(any(), any());
|
||||
|
||||
UpdateCleanupResult result = provisioner.completeUpdate(r);
|
||||
|
||||
assertTrue(result.complete());
|
||||
verify(cognito).deleteGroup("us-east-1_a", "admin");
|
||||
verify(cognito, never()).deleteGroup("us-east-1_a", "operators");
|
||||
}
|
||||
|
||||
@Test
|
||||
void thePoolRecordKeepsOnlyTheCurrentGroupOnceNoDeleteIsOwed() {
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", "admin"), ctx());
|
||||
provisioner.provision(r, props("us-east-1_a", "ops"), ctx("admin"));
|
||||
provisioner.completeUpdate(r);
|
||||
provisioner.provision(r, props("us-east-1_a", "staff"), ctx("ops"));
|
||||
provisioner.completeUpdate(r);
|
||||
|
||||
provisioner.provision(r, props("us-east-1_a", "staff"), ctx("staff"));
|
||||
|
||||
assertEquals("{\"staff\":\"us-east-1_a\"}", r.getAttributes().get("__FlociGroupPools"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void movingANamedGroupToAnotherPoolCreatesItThereAndOwesNoDelete() {
|
||||
// AWS replaces the group but deletes nothing: the physical id, the bare name, is unchanged.
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", "admin"), ctx());
|
||||
|
||||
provisioner.provision(r, props("us-east-1_b", "admin"), ctx("admin"));
|
||||
|
||||
verify(cognito).createGroup("us-east-1_b", "admin", null, null, null);
|
||||
verify(cognito, never()).updateGroup(any(), any(), any(), any(), any());
|
||||
assertEquals("admin", r.getPhysicalId());
|
||||
assertEquals("us-east-1_b", r.getAttributes().get("UserPoolId"));
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
assertNull(provisioner.updateCleanupPhysicalId(r));
|
||||
UpdateCleanupResult result = provisioner.completeUpdate(r);
|
||||
assertTrue(result.complete());
|
||||
assertNull(result.previousPhysicalId());
|
||||
verify(cognito, never()).deleteGroup(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollingBackANamedPoolMovePointsAtTheOldPoolGroupAndLeavesTheNewOne() {
|
||||
// Measured on AWS: the physical id did not change, so the rollback deletes nothing.
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", "admin"), ctx());
|
||||
provisioner.provision(r, props("us-east-1_b", "admin"), ctx("admin"));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
verify(cognito, never()).deleteGroup(any(), any());
|
||||
assertEquals("admin", r.getPhysicalId());
|
||||
assertEquals("us-east-1_a", r.getAttributes().get("UserPoolId"));
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
|
||||
provisioner.delete(r, "us-east-1");
|
||||
|
||||
verify(cognito).deleteGroup("us-east-1_a", "admin");
|
||||
verify(cognito, never()).deleteGroup(eq("us-east-1_b"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyResourceWithAnExplicitLookingNameIsReplacedWhenTheNameIsDropped() {
|
||||
// Persisted before the name mode was recorded: a name without the generated shape was explicit.
|
||||
StackResource r = resource("admin");
|
||||
r.setAttributes(new HashMap<>(Map.of("UserPoolId", "us-east-1_a")));
|
||||
|
||||
provisioner.provision(r, props("us-east-1_a", null), ctx("admin"));
|
||||
|
||||
String generated = r.getPhysicalId();
|
||||
assertTrue(generated.startsWith("my-stack-AdminGroup-"), generated);
|
||||
verify(cognito).createGroup("us-east-1_a", generated, null, null, null);
|
||||
verify(cognito, never()).updateGroup(any(), any(), any(), any(), any());
|
||||
assertEquals("admin", provisioner.updateCleanupPhysicalId(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyResourceWithAGeneratedNameKeepsItAndUpdatesInPlace() {
|
||||
String legacy = ctx().generatePhysicalName("AdminGroup", 128, false);
|
||||
StackResource r = resource(legacy);
|
||||
r.setAttributes(new HashMap<>(Map.of("UserPoolId", "us-east-1_a")));
|
||||
|
||||
provisioner.provision(r, props("us-east-1_a", null), ctx(legacy));
|
||||
|
||||
assertEquals(legacy, r.getPhysicalId());
|
||||
verify(cognito).updateGroup("us-east-1_a", legacy, null, null, null);
|
||||
verify(cognito, never()).createGroup(any(), any(), any(), any(), any());
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyResourceWithAGeneratedLookingExplicitNameUpdatesInPlace() {
|
||||
// Persisted before the name mode was recorded: the shape cannot tell an explicit name from
|
||||
// a generated one, so declaring the same name keeps the pre-mode in-place update.
|
||||
StackResource r = resource("my-stack-AdminGroup-0123456789ab");
|
||||
r.setAttributes(new HashMap<>(Map.of("UserPoolId", "us-east-1_a")));
|
||||
|
||||
provisioner.provision(r, props("us-east-1_a", "my-stack-AdminGroup-0123456789ab"),
|
||||
ctx("my-stack-AdminGroup-0123456789ab"));
|
||||
|
||||
verify(cognito).updateGroup("us-east-1_a", "my-stack-AdminGroup-0123456789ab", null, null, null);
|
||||
verify(cognito, never()).createGroup(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void droppingTheExplicitNameReplacesTheGroupUnderAGeneratedNameAndDeletesTheNamedOne() {
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", "admin"), ctx());
|
||||
|
||||
provisioner.provision(r, props("us-east-1_a", null), ctx("admin"));
|
||||
|
||||
String generated = r.getPhysicalId();
|
||||
assertTrue(generated.startsWith("my-stack-AdminGroup-"), generated);
|
||||
verify(cognito).createGroup("us-east-1_a", generated, null, null, null);
|
||||
assertEquals("admin", provisioner.updateCleanupPhysicalId(r));
|
||||
|
||||
provisioner.completeUpdate(r);
|
||||
|
||||
verify(cognito).deleteGroup("us-east-1_a", "admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaringTheGeneratedNameExplicitlyIsRefusedAsACustomNamedReplacement() {
|
||||
// Measured on AWS: GroupName is create-only, so declaring it replaces the group, and a
|
||||
// replacement keeping the same pool and name is refused before anything is created.
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", null), ctx());
|
||||
String generated = r.getPhysicalId();
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(r, props("us-east-1_a", generated), ctx(generated)));
|
||||
|
||||
assertEquals("ValidationError", thrown.getErrorCode());
|
||||
assertEquals("CloudFormation cannot update a stack when a custom-named resource requires replacing. "
|
||||
+ "Rename " + generated + " and update the stack again.", thrown.getMessage());
|
||||
verify(cognito, times(1)).createGroup(any(), any(), any(), any(), any());
|
||||
verify(cognito, never()).updateGroup(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void movingAGeneratedGroupToAnotherPoolGeneratesANewNameAndDeletesTheOldOneFromTheOldPool() {
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", null), ctx());
|
||||
String original = r.getPhysicalId();
|
||||
|
||||
provisioner.provision(r, props("us-east-1_b", null), ctx(original));
|
||||
|
||||
String replacement = r.getPhysicalId();
|
||||
assertNotEquals(original, replacement);
|
||||
assertTrue(replacement.startsWith("my-stack-AdminGroup-"), replacement);
|
||||
verify(cognito).createGroup("us-east-1_b", replacement, null, null, null);
|
||||
assertEquals(original, provisioner.updateCleanupPhysicalId(r));
|
||||
|
||||
provisioner.completeUpdate(r);
|
||||
|
||||
verify(cognito).deleteGroup("us-east-1_a", original);
|
||||
verify(cognito, never()).deleteGroup(eq("us-east-1_b"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInPlaceUpdateChangesTheGroupAndOwesNoCleanup() {
|
||||
StackResource named = resource(null);
|
||||
provisioner.provision(named, props("us-east-1_a", "admin"), ctx());
|
||||
StackResource unnamed = resource(null);
|
||||
provisioner.provision(unnamed, props("us-east-1_a", null), ctx());
|
||||
String generated = unnamed.getPhysicalId();
|
||||
|
||||
ObjectNode namedUpdate = props("us-east-1_a", "admin");
|
||||
namedUpdate.put("Description", "d2");
|
||||
namedUpdate.put("Precedence", 2);
|
||||
provisioner.provision(named, namedUpdate, ctx("admin"));
|
||||
provisioner.provision(unnamed, props("us-east-1_a", null), ctx(generated));
|
||||
|
||||
verify(cognito).updateGroup("us-east-1_a", "admin", "d2", 2, null);
|
||||
verify(cognito).updateGroup("us-east-1_a", generated, null, null, null);
|
||||
verify(cognito, times(2)).createGroup(any(), any(), any(), any(), any());
|
||||
assertEquals("admin", named.getPhysicalId());
|
||||
assertEquals(generated, unnamed.getPhysicalId());
|
||||
assertFalse(provisioner.hasReplacementUpdate(named));
|
||||
assertFalse(provisioner.hasReplacementUpdate(unnamed));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollingBackARenameDeletesTheReplacementAndRestoresThePriorNameAndPool() {
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("us-east-1_a", "admin"), ctx());
|
||||
provisioner.provision(r, props("us-east-1_b", "operators"), ctx("admin"));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
verify(cognito).deleteGroup("us-east-1_b", "operators");
|
||||
verify(cognito, never()).deleteGroup(any(), eq("admin"));
|
||||
assertEquals("admin", r.getPhysicalId());
|
||||
assertEquals("us-east-1_a", r.getAttributes().get("UserPoolId"));
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
|
||||
provisioner.delete(r, "us-east-1");
|
||||
|
||||
verify(cognito).deleteGroup("us-east-1_a", "admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingUserPoolIdFailsTheResourceRatherThanStubbingIt() {
|
||||
StackResource r = resource(null);
|
||||
|
||||
+21
@@ -205,6 +205,27 @@ class ProvisionContextTest {
|
||||
"a replacing update derives a different name and must still create");
|
||||
}
|
||||
|
||||
@Test
|
||||
void isGeneratedPhysicalNameRecognisesOnlyWhatGeneratePhysicalNameProducesForTheLogicalId() {
|
||||
ProvisionContext ctx = context(null);
|
||||
String generated = ctx.generatePhysicalName("AdminGroup", 128, false);
|
||||
assertTrue(ctx.isGeneratedPhysicalName(generated, "AdminGroup", 128, false), generated);
|
||||
|
||||
ProvisionContext longStack = new ProvisionContext(engine, "us-east-1", "000000000000",
|
||||
"a-stack-name-long-enough-to-force-truncation", null);
|
||||
String longLogicalId = "AVeryLongLogicalIdThatPushesTheGeneratedNamePastItsLimit";
|
||||
String truncated = longStack.generatePhysicalName(longLogicalId, 64, false);
|
||||
assertEquals(64, truncated.length(), truncated);
|
||||
assertTrue(longStack.isGeneratedPhysicalName(truncated, longLogicalId, 64, false), truncated);
|
||||
|
||||
assertFalse(ctx.isGeneratedPhysicalName("admin", "AdminGroup", 128, false), "an explicit name");
|
||||
assertFalse(ctx.isGeneratedPhysicalName(ctx.generatePhysicalName("OtherGroup", 128, false),
|
||||
"AdminGroup", 128, false), "another logical id's name");
|
||||
assertFalse(ctx.isGeneratedPhysicalName("my-stack-AdminGroup-0123456789zz", "AdminGroup", 128, false),
|
||||
"a suffix that is not hex");
|
||||
assertFalse(ctx.isGeneratedPhysicalName(null, "AdminGroup", 128, false));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The whole property node is handed to {@code engine.resolveStringList}, so a list-valued
|
||||
|
||||
+38
@@ -198,4 +198,42 @@ class ReplacementCleanupTest {
|
||||
|
||||
assertNull(previous.getAttributes().get(CfnRollback.REPLACEMENT_CLEANUP_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aReplacementThatKeptItsPhysicalIdRollsBackWithoutADeleteAndDisplacesNothing() {
|
||||
StackResource r = resource("subnet-1");
|
||||
r.getAttributes().put("VpcId", "vpc-new");
|
||||
List<String> deleted = new ArrayList<>();
|
||||
|
||||
ReplacementCleanup.record(r, update("subnet-1"), Map.of("VpcId", "vpc-old"), true);
|
||||
|
||||
assertFalse(ReplacementCleanup.hasReplacement(r));
|
||||
assertNull(ReplacementCleanup.cleanupPhysicalId(r));
|
||||
assertTrue(ReplacementCleanup.rollback(r, (type, id, region) -> deleted.add(id)));
|
||||
assertEquals("subnet-1", r.getPhysicalId());
|
||||
assertEquals("vpc-old", r.getAttributes().get("VpcId"));
|
||||
assertEquals(List.of(), deleted);
|
||||
assertNull(r.getAttributes().get(CfnRollback.REPLACEMENT_CLEANUP_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theThreeArgumentRecordStillKeysTheReplacementOnAChangedPhysicalId() {
|
||||
StackResource inPlace = resource("subnet-1");
|
||||
ReplacementCleanup.record(inPlace, update("subnet-1"), Map.of("VpcId", "vpc-old"));
|
||||
assertNull(inPlace.getAttributes().get(CfnRollback.REPLACEMENT_CLEANUP_ATTR));
|
||||
assertFalse(ReplacementCleanup.rollback(inPlace, (type, id, region) -> { }));
|
||||
|
||||
StackResource replaced = resource("subnet-2");
|
||||
List<String> deleted = new ArrayList<>();
|
||||
ReplacementCleanup.record(replaced, update("subnet-1"), Map.of("VpcId", "vpc-old"));
|
||||
assertTrue(ReplacementCleanup.hasReplacement(replaced));
|
||||
assertEquals("subnet-1", ReplacementCleanup.cleanupPhysicalId(replaced));
|
||||
assertTrue(ReplacementCleanup.rollback(replaced, (type, id, region) -> deleted.add(id)));
|
||||
assertEquals("subnet-1", replaced.getPhysicalId());
|
||||
assertEquals(List.of("subnet-2"), deleted);
|
||||
}
|
||||
|
||||
private static ProvisionContext update(String priorPhysicalId) {
|
||||
return new ProvisionContext(null, "us-east-1", "000000000000", "my-stack", priorPhysicalId);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user