fix(cognito): refuse DeleteUserPool while deletion protection is active (#3961)

DeletionProtection was stored and round-tripped but never read on the
delete path, so a protected pool was deleted like any other and a
CloudFormation DeleteStack owning one succeeded. AWS refuses with
InvalidParameterException until UpdateUserPool switches the flag to
INACTIVE, and the stack reports DELETE_FAILED with that reason.

The check reads the pool store directly: describeUserPool would turn a
delete of a missing id into ResourceNotFoundException (a separate
divergence) and generate signing keys as a side effect.

Closes #3948
This commit is contained in:
David authored and GitHub committed 2026-09-19 19:53:48 -04:00
1 parent 7d9f499f2d
commit 1e8485830c
5 files changed
+174 -1

No files matched your search

@@ -402,6 +402,75 @@ class CognitoCfnIntegrationTest {
.body("__type", equalTo("ResourceNotFoundException"));
}
/**
* A pool with deletion protection refuses its own delete, and the stack has to say so: on AWS
* the resource and the stack report DELETE_FAILED with Cognito's reason, the pool stays, and
* the stack can only go once an update has switched the protection off.
*/
@Test
void deletingAStackWhosePoolHasDeletionProtectionFailsAndLeavesThePoolUntilItIsDeactivated() throws Exception {
String stack = "cognito-cfn-deletion-protection-it";
cloudFormation(stack, "CreateStack", deletionProtectionTemplate("ACTIVE"));
String stacks = describeStacks(stack, "CREATE_COMPLETE");
String poolId = outputValue(stacks, "PoolId");
cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then().statusCode(200)
.body("UserPool.DeletionProtection", equalTo("ACTIVE"));
cloudFormation(stack, "DeleteStack", null);
awaitStackStatus(stack, "DELETE_FAILED");
String events = describeStackEvents(stack);
assertTrue(events.contains("<ResourceStatus>DELETE_FAILED</ResourceStatus>"), events);
assertTrue(events.contains("deletion protection is activated"), events);
cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then().statusCode(200)
.body("UserPool.Id", equalTo(poolId));
cloudFormation(stack, "UpdateStack", deletionProtectionTemplate("INACTIVE"));
describeStacks(stack, "UPDATE_COMPLETE");
cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then().statusCode(200)
.body("UserPool.DeletionProtection", equalTo("INACTIVE"));
cloudFormation(stack, "DeleteStack", null);
awaitStackDeleted(stack);
cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then()
.body("__type", equalTo("ResourceNotFoundException"));
}
private static String deletionProtectionTemplate(String deletionProtection) {
return """
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"Pool": {
"Type": "AWS::Cognito::UserPool",
"Properties": {"UserPoolName": "cognito-cfn-protected-pool", "DeletionProtection": "%s"}
}
},
"Outputs": {"PoolId": {"Value": {"Ref": "Pool"}}}
}
""".formatted(deletionProtection);
}
private static void awaitStackStatus(String stack, String expectedStatus) throws InterruptedException {
String body = null;
for (int i = 0; i < 100; i++) {
body = given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStacks")
.formParam("StackName", stack)
.when().post("/").then().extract().asString();
if (body.contains("<StackStatus>" + expectedStatus + "</StackStatus>")) {
return;
}
Thread.sleep(50);
}
fail("stack " + stack + " did not reach " + expectedStatus + " within the timeout: " + body);
}
private static void cloudFormation(String stack, String action, String templateBody) {
RequestSpecification request = given()
.contentType("application/x-www-form-urlencoded")
@@ -0,0 +1,74 @@
package io.github.hectorvent.floci.services.cognito;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
import static org.hamcrest.Matchers.equalTo;
/**
* Deletion protection is the pool's guard against DeleteUserPool. The developer guide ("User
* pool deletion protection") states the contract: with it ACTIVE the delete is refused with
* InvalidParameterException until an UpdateUserPool switches it to INACTIVE.
*/
@QuarkusTest
class CognitoDeletionProtectionIntegrationTest {
private static final String PROTECTED_MESSAGE =
"The user pool cannot be deleted because deletion protection is activated. "
+ "Deletion protection must be inactivated first.";
@Test
void deleteUserPool_deletionProtectionActive_isRefusedUntilDeactivated() throws Exception {
String poolId = cognitoJson("CreateUserPool", """
{"PoolName": "cognito-deletion-protection-it", "DeletionProtection": "ACTIVE"}
""").path("UserPool").path("Id").asText();
cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then()
.statusCode(200)
.body("UserPool.DeletionProtection", equalTo("ACTIVE"));
cognitoAction("DeleteUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterException"))
.body("message", equalTo(PROTECTED_MESSAGE));
// The refusal left the pool untouched.
cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then()
.statusCode(200)
.body("UserPool.Id", equalTo(poolId));
cognitoAction("UpdateUserPool", "{\"UserPoolId\": \"" + poolId + "\", \"DeletionProtection\": \"INACTIVE\"}")
.then()
.statusCode(200);
cognitoAction("DeleteUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then()
.statusCode(200);
cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
.then()
.body("__type", equalTo("ResourceNotFoundException"));
}
@Test
void deleteUserPool_deletionProtectionInactiveOrUnset_deletes() throws Exception {
String unset = cognitoJson("CreateUserPool", """
{"PoolName": "cognito-deletion-protection-unset-it"}
""").path("UserPool").path("Id").asText();
cognitoAction("DeleteUserPool", "{\"UserPoolId\": \"" + unset + "\"}")
.then()
.statusCode(200);
String inactive = cognitoJson("CreateUserPool", """
{"PoolName": "cognito-deletion-protection-inactive-it", "DeletionProtection": "INACTIVE"}
""").path("UserPool").path("Id").asText();
cognitoAction("DeleteUserPool", "{\"UserPoolId\": \"" + inactive + "\"}")
.then()
.statusCode(200);
}
}