Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(cognito): honour admin_only account recovery (#4762)

jwoa95a0be062f9f3e
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 3731c157e1..2bb8034126 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
@@ -553,7 +553,10 @@ public class CognitoService implements ResourceProvider {
if (request.containsKey("AdminCreateUserConfig")) pool.setAdminCreateUserConfig((Map<String, Object>) request.get("AdminCreateUserConfig"));
if (request.containsKey("UserPoolAddOns")) pool.setUserPoolAddOns((Map<String, Object>) request.get("UserPoolAddOns"));
if (request.containsKey("UsernameConfiguration")) pool.setUsernameConfiguration((Map<String, Object>) request.get("UsernameConfiguration"));
- if (request.containsKey("AccountRecoverySetting")) pool.setAccountRecoverySetting((Map<String, Object>) request.get("AccountRecoverySetting"));
+ if (request.containsKey("AccountRecoverySetting")) {
+ pool.setAccountRecoverySetting((Map<String, Object>) request.get("AccountRecoverySetting"));
+ validateAccountRecoverySetting(pool);
+ }
if (request.containsKey("UserAttributeUpdateSettings")) {
pool.setUserAttributeUpdateSettings(validateUserAttributeUpdateSettings(
(Map<String, Object>) request.get("UserAttributeUpdateSettings")));
@@ -561,6 +564,16 @@ public class CognitoService implements ResourceProvider {
if (request.containsKey("UserPoolTier")) pool.setUserPoolTier((String) request.get("UserPoolTier"));
}
+ private void validateAccountRecoverySetting(UserPool pool) {
+ List<String> mechanisms = accountRecoveryMechanisms(pool);
+ if (mechanisms.contains("admin_only") && mechanisms.stream().anyMatch(name -> !"admin_only".equals(name))) {
+ throw new AwsException("InvalidParameterException",
+ "Invalid account recovery setting parameter. "
+ + "Account Recovery Setting cannot use admin_only setting with any other recovery mechanisms.",
+ 400);
+ }
+ }
+
private Map<String, Object> validateUserAttributeUpdateSettings(Map<String, Object> settings) {
if (settings == null || settings.isEmpty()) {
return new HashMap<>();
@@ -2145,6 +2158,12 @@ public class CognitoService implements ResourceProvider {
}
public void adminResetUserPassword(String userPoolId, String username) {
+ UserPool pool = describeUserPool(userPoolId);
+ if (accountRecoveryMechanisms(pool).contains("admin_only")) {
+ throw new AwsException("NotAuthorizedException",
+ "This userpool does not have password recovery mechanism, the administrator must set a new password.",
+ 400);
+ }
CognitoUser resolvedUser = adminGetUser(userPoolId, username);
synchronized (userLock(userPoolId, resolvedUser.getUsername())) {
adminResetUserPasswordUnderUserLock(userPoolId, resolvedUser.getUsername());
@@ -3018,8 +3037,11 @@ public class CognitoService implements ResourceProvider {
public Map<String, Object> forgotPassword(String clientId, String username) {
UserPoolClient client = clientStore.get(clientId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found", 400));
- CognitoUser user = adminGetUser(client.getUserPoolId(), username);
UserPool pool = describeUserPool(client.getUserPoolId());
+ if (accountRecoveryMechanisms(pool).contains("admin_only")) {
+ throw new AwsException("NotAuthorizedException", "Contact administrator to reset password.", 400);
+ }
+ CognitoUser user = adminGetUser(client.getUserPoolId(), username);
ensureVerificationWiring();
DeliveryTarget deliveryTarget = resolveForgotPasswordDeliveryTarget(pool, user);
@@ -5004,7 +5026,7 @@ public class CognitoService implements ResourceProvider {
}
return recoveryMechanisms.stream().filter(Map.class::isInstance).map(Map.class::cast)
.sorted(Comparator.comparingInt(this::recoveryPriority))
- .map(m -> String.valueOf(m.get("Name"))).filter(name -> !"admin_only".equals(name))
+ .map(m -> String.valueOf(m.get("Name")))
.toList();
}
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 9336605d88..5eb03d6ba1 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
@@ -41,6 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
@QuarkusTest
@@ -908,6 +909,192 @@ class CognitoIntegrationTest {
assertTrue(delivery.path("Destination").asText().contains("*"));
}
+ @Test
+ @Order(8)
+ void forgotPasswordUnderAdminOnlyRecoveryIsRefusedWithoutSendingCode() throws Exception {
+ String pool = createAdminOnlyRecoveryPool();
+ String client = cognitoJson("CreateUserPoolClient", """
+ {
+ "UserPoolId": "%s",
+ "ClientName": "admin-only-recovery-client"
+ }
+ """.formatted(pool)).path("UserPoolClient").path("ClientId").asText();
+
+ String email = "admin-only+" + UUID.randomUUID() + "@example.com";
+ cognitoAction("AdminCreateUser", """
+ {
+ "UserPoolId": "%s",
+ "Username": "%s",
+ "UserAttributes": [
+ { "Name": "email", "Value": "%s" },
+ { "Name": "email_verified", "Value": "true" }
+ ]
+ }
+ """.formatted(pool, email, email))
+ .then()
+ .statusCode(200);
+ cognitoAction("AdminSetUserPassword", """
+ {
+ "UserPoolId": "%s",
+ "Username": "%s",
+ "Password": "OrigPass123!",
+ "Permanent": true
+ }
+ """.formatted(pool, email))
+ .then()
+ .statusCode(200);
+
+ cognitoAction("ForgotPassword", """
+ {
+ "ClientId": "%s",
+ "Username": "%s"
+ }
+ """.formatted(client, email))
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("NotAuthorizedException"))
+ .body("message", equalTo("Contact administrator to reset password."));
+
+ given()
+ .queryParam("email", email)
+ .when()
+ .get("/_aws/ses")
+ .then()
+ .statusCode(200)
+ .body("messages", hasSize(0));
+ }
+
+ @Test
+ @Order(8)
+ void forgotPasswordUnderAdminOnlyRecoveryRefusesUnknownUserTheSameWay() throws Exception {
+ String pool = createAdminOnlyRecoveryPool();
+ String client = cognitoJson("CreateUserPoolClient", """
+ {
+ "UserPoolId": "%s",
+ "ClientName": "admin-only-recovery-client"
+ }
+ """.formatted(pool)).path("UserPoolClient").path("ClientId").asText();
+
+ cognitoAction("ForgotPassword", """
+ {
+ "ClientId": "%s",
+ "Username": "nobody+%[email protected]"
+ }
+ """.formatted(client, UUID.randomUUID()))
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("NotAuthorizedException"))
+ .body("message", equalTo("Contact administrator to reset password."));
+ }
+
+ @Test
+ @Order(8)
+ void adminResetUserPasswordUnderAdminOnlyRecoveryIsRefusedAndLeavesUserConfirmed() throws Exception {
+ String pool = createAdminOnlyRecoveryPool();
+ String email = "admin-reset+" + UUID.randomUUID() + "@example.com";
+ cognitoAction("AdminCreateUser", """
+ {
+ "UserPoolId": "%s",
+ "Username": "%s",
+ "UserAttributes": [
+ { "Name": "email", "Value": "%s" },
+ { "Name": "email_verified", "Value": "true" }
+ ]
+ }
+ """.formatted(pool, email, email))
+ .then()
+ .statusCode(200);
+ cognitoAction("AdminSetUserPassword", """
+ {
+ "UserPoolId": "%s",
+ "Username": "%s",
+ "Password": "OrigPass123!",
+ "Permanent": true
+ }
+ """.formatted(pool, email))
+ .then()
+ .statusCode(200);
+
+ cognitoAction("AdminResetUserPassword", """
+ {
+ "UserPoolId": "%s",
+ "Username": "%s"
+ }
+ """.formatted(pool, email))
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("NotAuthorizedException"))
+ .body("message", equalTo(
+ "This userpool does not have password recovery mechanism, the administrator must set a new password."));
+
+ JsonNode user = cognitoJson("AdminGetUser", """
+ {
+ "UserPoolId": "%s",
+ "Username": "%s"
+ }
+ """.formatted(pool, email));
+ assertEquals("CONFIRMED", user.path("UserStatus").asText());
+ }
+
+ @Test
+ void createUserPoolRejectsAdminOnlyCombinedWithAnotherRecoveryMechanism() {
+ cognitoAction("CreateUserPool", """
+ {
+ "PoolName": "AdminOnlyCombinedPool",
+ "AccountRecoverySetting": {
+ "RecoveryMechanisms": [
+ { "Priority": 1, "Name": "admin_only" },
+ { "Priority": 2, "Name": "verified_email" }
+ ]
+ }
+ }
+ """)
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("InvalidParameterException"))
+ .body("message", equalTo("Invalid account recovery setting parameter. "
+ + "Account Recovery Setting cannot use admin_only setting with any other recovery mechanisms."));
+ }
+
+ @Test
+ void updateUserPoolRejectsAdminOnlyCombinedWithAnotherRecoveryMechanism() throws Exception {
+ String pool = cognitoJson("CreateUserPool", """
+ {
+ "PoolName": "EmailRecoveryPoolToUpdate",
+ "AccountRecoverySetting": {
+ "RecoveryMechanisms": [
+ { "Priority": 1, "Name": "verified_email" }
+ ]
+ }
+ }
+ """).path("UserPool").path("Id").asText();
+
+ cognitoAction("UpdateUserPool", """
+ {
+ "UserPoolId": "%s",
+ "AccountRecoverySetting": {
+ "RecoveryMechanisms": [
+ { "Priority": 1, "Name": "verified_phone_number" },
+ { "Priority": 2, "Name": "admin_only" }
+ ]
+ }
+ }
+ """.formatted(pool))
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("InvalidParameterException"))
+ .body("message", equalTo("Invalid account recovery setting parameter. "
+ + "Account Recovery Setting cannot use admin_only setting with any other recovery mechanisms."));
+
+ JsonNode mechanisms = cognitoJson("DescribeUserPool", """
+ {
+ "UserPoolId": "%s"
+ }
+ """.formatted(pool)).path("UserPool").path("AccountRecoverySetting").path("RecoveryMechanisms");
+ assertEquals(1, mechanisms.size());
+ assertEquals("verified_email", mechanisms.get(0).path("Name").asText());
+ }
+
// ── Groups ────────────────────────────────────────────────────────
@Test
@@ -3131,6 +3318,19 @@ class CognitoIntegrationTest {
return auth.path("AuthenticationResult").path("AccessToken").asText();
}
+ private static String createAdminOnlyRecoveryPool() throws Exception {
+ return cognitoJson("CreateUserPool", """
+ {
+ "PoolName": "AdminOnlyRecoveryPool",
+ "AccountRecoverySetting": {
+ "RecoveryMechanisms": [
+ { "Priority": 1, "Name": "admin_only" }
+ ]
+ }
+ }
+ """).path("UserPool").path("Id").asText();
+ }
+
private static String fetchLatestSesVerificationCode(String recipient) throws Exception {
String response = given()
.queryParam("email", recipient)