fix(cognito): delete a pool's users and resource servers on DeleteUserPool (#3419)

Fixes #2864. DeleteUserPool cascaded to groups and (since #2858) identity
providers, but left userStore and resourceServerStore untouched. A pool id
can be pinned with the floci:override-id tag, so unlike real AWS (where a
pool id is never reused) a pool recreated on the same id inherited the
deleted pool's users, password hashes included, and resource servers.

Adds the same prefix-scan cascade already used for groups/identity
providers to users and resource servers, and adds regression tests for
both cases mirroring the identity-provider orphan-reuse test from #2858.
Verified by disabling the new cascade and confirming both new tests fail
with the exact expected count mismatch before restoring it. Full cognito
package regression (368 tests) passes clean.
This commit is contained in:
Olamide Kosile authored and GitHub committed 2026-09-11 13:59:49 -04:00
1 parent 70176d6287
commit c48e81d956
2 files changed
+132

No files matched your search

@@ -543,6 +543,15 @@ public class CognitoService implements ResourceProvider {
String prefix = id + "::";
groupStore.scan(k -> k.startsWith(prefix))
.forEach(g -> groupStore.delete(groupKey(id, g.getGroupName())));
// github.com/floci-io/floci/issues/2864: a pool id can be pinned with the
// floci:override-id tag, so it can be reused after delete - unlike real AWS, where a
// pool id is never reused and this situation can't arise. Without this, a pool
// recreated on the same id inherited the deleted pool's users (password hashes and
// all) and resource servers.
userStore.scan(k -> k.startsWith(prefix))
.forEach(u -> userStore.delete(userKey(id, u.getUsername())));
resourceServerStore.scan(k -> k.startsWith(prefix))
.forEach(r -> resourceServerStore.delete(resourceServerKey(id, r.getIdentifier())));
// Same lock as the provider mutations: a create or update that interleaves with
// this cascade would otherwise reinstate a provider for a pool that is going away.
synchronized (identityProviderLock) {
@@ -2473,6 +2473,129 @@ class CognitoIntegrationTest {
.body("__type", org.hamcrest.Matchers.equalTo("NotAuthorizedException"));
}
/**
* github.com/floci-io/floci/issues/2864: found while addressing review feedback on the
* identity-provider version of this bug (#2858) - deleteUserPool cascaded to groups and
* identity providers but not users or resource servers, so a pool id pinned with the
* floci:override-id tag and recreated after delete inherited the deleted pool's users,
* password hashes included. This is the more serious of the two orphan cases.
*/
@Test
@Order(103)
void deletedUserPoolDoesNotLeaveOrphanedUsersForAReusedPoolId() throws Exception {
String pinnedId = "us-east-1_userorph1";
cognitoAction("CreateUserPool", """
{
"PoolName": "UserOrphanPool",
"UserPoolTags": {"floci:override-id": "%s"}
}
""".formatted(pinnedId))
.then()
.statusCode(200);
cognitoAction("AdminCreateUser", """
{
"UserPoolId": "%s",
"Username": "orphanuser",
"MessageAction": "SUPPRESS"
}
""".formatted(pinnedId))
.then()
.statusCode(200);
cognitoAction("DeleteUserPool", """
{
"UserPoolId": "%s"
}
""".formatted(pinnedId))
.then()
.statusCode(200);
cognitoAction("CreateUserPool", """
{
"PoolName": "UserOrphanPoolAgain",
"UserPoolTags": {"floci:override-id": "%s"}
}
""".formatted(pinnedId))
.then()
.statusCode(200);
assertEquals(0, cognitoJson("ListUsers", """
{
"UserPoolId": "%s"
}
""".formatted(pinnedId)).path("Users").size(),
"a recreated pool must not inherit the deleted pool's users");
cognitoAction("DeleteUserPool", """
{
"UserPoolId": "%s"
}
""".formatted(pinnedId))
.then()
.statusCode(200);
}
/** Same as above, for resource servers (issue #2864's second orphan case). */
@Test
@Order(104)
void deletedUserPoolDoesNotLeaveOrphanedResourceServersForAReusedPoolId() throws Exception {
String pinnedId = "us-east-1_rsorph1";
cognitoAction("CreateUserPool", """
{
"PoolName": "ResourceServerOrphanPool",
"UserPoolTags": {"floci:override-id": "%s"}
}
""".formatted(pinnedId))
.then()
.statusCode(200);
cognitoAction("CreateResourceServer", """
{
"UserPoolId": "%s",
"Identifier": "https://api.example.com",
"Name": "API",
"Scopes": [{"ScopeName": "read", "ScopeDescription": "r"}]
}
""".formatted(pinnedId))
.then()
.statusCode(200);
cognitoAction("DeleteUserPool", """
{
"UserPoolId": "%s"
}
""".formatted(pinnedId))
.then()
.statusCode(200);
cognitoAction("CreateUserPool", """
{
"PoolName": "ResourceServerOrphanPoolAgain",
"UserPoolTags": {"floci:override-id": "%s"}
}
""".formatted(pinnedId))
.then()
.statusCode(200);
assertEquals(0, cognitoJson("ListResourceServers", """
{
"UserPoolId": "%s"
}
""".formatted(pinnedId)).path("ResourceServers").size(),
"a recreated pool must not inherit the deleted pool's resource servers");
cognitoAction("DeleteUserPool", """
{
"UserPoolId": "%s"
}
""".formatted(pinnedId))
.then()
.statusCode(200);
}
private static JsonNode decodeJwtPayload(String token) throws Exception {
return decodeJwtPart(token, 1);
}