Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Olamide Kosilec48e81d9561b
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 2a228abba4..adcffeaed4 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
@@ -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) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
index 8c122aa4bd..6faae904e2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
@@ -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);
}