diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java index 5e5ce93107..7ac7c30feb 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java @@ -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. diff --git a/docs/services/cognito.md b/docs/services/cognito.md index b64ad94a8b..22ea28bef4 100644 --- a/docs/services/cognito.md +++ b/docs/services/cognito.md @@ -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. | diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java index cff7653b5b..4ad7e3fff4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java @@ -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() diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java index 462f5884ea..50c2475f96 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java @@ -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("DELETE_FAILED"), 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("" + expectedStatus + "")) { + 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") diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoDeletionProtectionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoDeletionProtectionIntegrationTest.java new file mode 100644 index 0000000000..143b2661d7 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoDeletionProtectionIntegrationTest.java @@ -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); + } +}