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

@@ -468,6 +468,27 @@ class CognitoFeaturesTest {
cognito.adminDeleteUser(b -> b.userPoolId(poolId).username(tempUser));
}
@Test
@Order(61)
void deletionProtectionRefusesDeleteUntilDeactivated() {
String protectedPoolId = cognito.createUserPool(b -> b
.poolName("compat-protected-pool")
.deletionProtection(DeletionProtectionType.ACTIVE))
.userPool().id();
try {
assertThatThrownBy(() -> cognito.deleteUserPool(b -> b.userPoolId(protectedPoolId)))
.isInstanceOf(InvalidParameterException.class)
.hasMessageContaining("deletion protection is activated");
assertThat(cognito.describeUserPool(b -> b.userPoolId(protectedPoolId)).userPool().deletionProtection())
.isEqualTo(DeletionProtectionType.ACTIVE);
} finally {
cognito.updateUserPool(b -> b.userPoolId(protectedPoolId).deletionProtection(DeletionProtectionType.INACTIVE));
cognito.deleteUserPool(b -> b.userPoolId(protectedPoolId));
}
assertThatThrownBy(() -> cognito.describeUserPool(b -> b.userPoolId(protectedPoolId)))
.isInstanceOf(ResourceNotFoundException.class);
}
// ── Issue #234 note ───────────────────────────────────────────────────────
// GetTokensFromRefreshToken is tested in sdk-test-node/tests/cognito-features.test.ts
// because GetTokensFromRefreshTokenCommand is not available in Java SDK 2.31.8.
+1 -1
View File
@@ -29,7 +29,7 @@ An action given a user pool ID that does not resolve returns `ResourceNotFoundEx
| DescribeUserPool | Returns the stored user pool configuration. |
| ListUserPools | Lists local user pools visible in the request region. |
| UpdateUserPool | Updates mutable user pool settings and persisted user-pool tags. |
| DeleteUserPool | Deletes a local user pool and everything it owns: users, groups, app clients, resource servers, identity providers, revoked tokens and verification codes. Refused with `InvalidParameterException` while a domain is still configured. |
| DeleteUserPool | Deletes a local user pool and everything it owns: users, groups, app clients, resource servers, identity providers, revoked tokens and verification codes. Refused with `InvalidParameterException` while `DeletionProtection` is `ACTIVE` (switch it to `INACTIVE` with `UpdateUserPool` first, as on AWS) or while a domain is still configured. |
| GetUserPoolMfaConfig | Returns the pool's MFA mode and, once configured, its software-token setting. |
| SetUserPoolMfaConfig | Sets `MfaConfiguration` (`OFF`/`ON`/`OPTIONAL`) and `SoftwareTokenMfaConfiguration`. An absent `MfaConfiguration` means `OFF`, and turning MFA off drops the factor configuration with it. Validation follows the live service: `OFF` alongside a software-token, email or SMS factor is rejected, and `ON`/`OPTIONAL` with none of those three is rejected, in both cases on the member being present, not on its `Enabled` value. `WebAuthnConfiguration` sits outside both rules, as it does in AWS. SMS, email and WebAuthn configurations are validated and not stored: Floci cannot deliver those factors, so keeping the config would imply a capability it does not have. |
@@ -578,6 +578,15 @@ public class CognitoService implements ResourceProvider {
}
public void deleteUserPool(String id) {
// Deletion protection is the pool's own guard against this call: with it ACTIVE, AWS
// refuses until an UpdateUserPool switches it to INACTIVE (developer guide, "User pool
// deletion protection"), and a CloudFormation delete of the pool reports DELETE_FAILED.
String deletionProtection = poolStore.get(id).map(UserPool::getDeletionProtection).orElse(null);
if ("ACTIVE".equalsIgnoreCase(deletionProtection)) {
throw new AwsException("InvalidParameterException",
"The user pool cannot be deleted because deletion protection is activated. "
+ "Deletion protection must be inactivated first.", 400);
}
// AWS refuses to delete a pool that still has a hosted UI / custom domain; the
// DeleteUserPool API reference documents this exact InvalidParameterException.
boolean hasDomain = domainStore.scan(k -> true).stream()
@@ -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);
}
}