Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cognito): name missing user pools in errors (#2936)
Aniket Shuklac30ca8fb5fc5
diff --git a/docs/services/cognito.md b/docs/services/cognito.mdindex 5c10479a16..80090fc840 100644--- a/docs/services/cognito.md+++ b/docs/services/cognito.md@@ -17,6 +17,8 @@ Floci strips reserved `floci:*` tags from stored and returned `UserPoolTags` onStandalone `TagResource` rejects reserved `floci:*` keys. `ListTagsForResource` and `UntagResource` operate on the persisted user-pool tag map.+An action given a user pool ID that does not resolve returns `ResourceNotFoundException` with the live service's wording, `User pool <poolId> does not exist.`, so tooling that matches Cognito error text behaves the same way locally.+## Supported Actions### User Poolsdiff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.javaindex d632795b0e..fc0f8d3190 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@@ -359,7 +359,7 @@ public class CognitoService implements ResourceProvider {public UserPool describeUserPool(String id) {UserPool pool = poolStore.get(id)- .orElseThrow(() -> new AwsException("ResourceNotFoundException", "User pool not found", 400));+ .orElseThrow(() -> userPoolNotFound(id));boolean generatedKeys = ensureJwtSigningKeys(pool);boolean generatedSecret = ensureRefreshTokenSecret(pool);if (generatedKeys || generatedSecret) {@@ -1416,7 +1416,7 @@ public class CognitoService implements ResourceProvider {public CognitoUser adminGetUser(String userPoolId, String username) {UserPool pool = poolStore.get(userPoolId).orElseThrow(- () -> new AwsException("ResourceNotFoundException", "User pool not found", 400));+ () -> userPoolNotFound(userPoolId));LinkedHashMap<String, CognitoUser> matches = new LinkedHashMap<>();userStore.get(userKey(userPoolId, username)).ifPresent(u -> matches.put(u.getUsername(), u));@@ -2034,9 +2034,9 @@ public class CognitoService implements ResourceProvider {UserPoolClient client = clientStore.get(clientId).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found",400));- UserPool pool = poolStore.get(client.getUserPoolId())- .orElseThrow(() -> new AwsException("ResourceNotFoundException",- "User pool not found", 400));+ String userPoolId = client.getUserPoolId();+ UserPool pool = poolStore.get(userPoolId)+ .orElseThrow(() -> userPoolNotFound(userPoolId));CognitoUser user = adminGetUser(client.getUserPoolId(), username);if (verificationCodeService != null && isSignUpConfirmationEnabled(pool)) {try {@@ -2398,6 +2398,11 @@ public class CognitoService implements ResourceProvider {// ──────────────────────────── Private helpers ────────────────────────────+ private static AwsException userPoolNotFound(String userPoolId) {+ return new AwsException("ResourceNotFoundException",+ "User pool " + userPoolId + " does not exist.", 400);+ }+UserPoolClient findClientById(String clientId) {return clientStore.get(clientId).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found", 400));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.javaindex 90c4cb3265..8c122aa4bd 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@@ -230,6 +230,20 @@ class CognitoIntegrationTest {assertFalse(sub.path("Mutable").asBoolean(), "sub must not be Mutable");}+ @Test+ @Order(5)+ void describeUnknownUserPoolNamesPoolInResourceNotFoundMessage() {+ String missingPoolId = "us-east-1_000000000";++ cognitoAction("DescribeUserPool", """+ { "UserPoolId": "%s" }+ """.formatted(missingPoolId))+ .then()+ .statusCode(400)+ .body("__type", equalTo("ResourceNotFoundException"))+ .body("message", equalTo("User pool " + missingPoolId + " does not exist."));+ }+@Test@Order(6)void confirmForgotPasswordRejectsWrongConfirmationCode() throws Exception {diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.javaindex 64ca113f08..7ff98418fc 100644--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java@@ -1468,6 +1468,20 @@ class CognitoServiceTest {assertEquals(400, ex.getHttpStatus());}+ @Test+ void confirmSignUpNamesDeletedUserPoolInResourceNotFoundMessage() {+ UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");+ UserPoolClient client = service.createUserPoolClient(+ pool.getId(), "test-client", false, false, List.of(), List.of());+ service.deleteUserPool(pool.getId());++ AwsException ex = assertThrows(AwsException.class, () ->+ service.confirmSignUp(client.getClientId(), "carol", "123456"));+ assertEquals("ResourceNotFoundException", ex.getErrorCode());+ assertEquals("User pool " + pool.getId() + " does not exist.", ex.getMessage());+ assertEquals(400, ex.getHttpStatus());+ }+@ParameterizedTest@CsvSource({"email_verified", "phone_number_verified"})@SuppressWarnings("unchecked")@@ -1854,9 +1868,12 @@ class CognitoServiceTest {@Testvoid adminGetUserRejectsUnknownPoolWithResourceNotFound() {+ String missingPoolId = "us-east-1_missing";+AwsException ex = assertThrows(AwsException.class,- () -> service.adminGetUser("us-east-1_missing", "bob"));+ () -> service.adminGetUser(missingPoolId, "bob"));assertEquals("ResourceNotFoundException", ex.getErrorCode());+ assertEquals("User pool " + missingPoolId + " does not exist.", ex.getMessage());assertEquals(400, ex.getHttpStatus());}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.javaindex 3bf7f20231..5ca28753a5 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.java@@ -88,7 +88,7 @@ class S3WellKnownKeyIntegrationTest {.then().extract().asString();- if (body.contains("ResourceNotFoundException") || body.contains("User pool not found")) {+ if (body.contains("ResourceNotFoundException") || body.contains("User pool ")) {throw new AssertionError("GET /" + BUCKET + "/" + JWKS_KEY+ " was routed to Cognito instead of S3. Response: " + body);