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 4203b8654f..b8c7f0d868 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 @@ -86,6 +86,8 @@ public class CognitoService implements ResourceProvider { private static final int DEFAULT_REFRESH_TOKEN_VALIDITY_DAYS = 30; private static final String COGNITO_PASSWORD_SYMBOLS = "^$*.[]{}()?\"!@#%&/\\,><':;|_~`=+-"; + // JVM-local stripes bound lock memory without retaining one lock for every user key. + private static final int USER_LOCK_STRIPES = 512; private static final Logger LOG = Logger.getLogger(CognitoService.class); private static final ObjectMapper MAPPER = new ObjectMapper(); @@ -132,10 +134,24 @@ public class CognitoService implements ResourceProvider { private final VerificationCodeService verificationCodeService; private final CognitoMessageDispatcher messageDispatcher; private final TlsCertificateManager certificateManager; + private final Object[] userLocks = newUserLockStripes(); // Keyed by session token; contains SRP ephemeral state (bPrivate, B, A, secretBlock) private final CognitoAuthFlowHandler authFlowHandler; + private static Object[] newUserLockStripes() { + Object[] stripes = new Object[USER_LOCK_STRIPES]; + for (int i = 0; i < stripes.length; i++) { + stripes[i] = new Object(); + } + return stripes; + } + + private Object userLock(String poolId, String username) { + String key = userKey(poolId, username); + return userLocks[Math.floorMod(key.hashCode(), USER_LOCK_STRIPES)]; + } + @Inject public CognitoService(StorageFactory storageFactory, EmulatorConfig emulatorConfig, RegionResolver regionResolver, LambdaService lambdaService, AcmService acmService, @@ -579,7 +595,11 @@ public class CognitoService implements ResourceProvider { groupStore.scan(k -> k.startsWith(prefix)) .forEach(g -> groupStore.delete(groupKey(id, g.getGroupName()))); userStore.scan(k -> k.startsWith(prefix)) - .forEach(u -> userStore.delete(userKey(id, u.getUsername()))); + .forEach(u -> { + synchronized (userLock(id, u.getUsername())) { + userStore.delete(userKey(id, u.getUsername())); + } + }); resourceServerStore.scan(k -> k.startsWith(prefix)) .forEach(r -> resourceServerStore.delete(resourceServerKey(id, r.getIdentifier()))); // Clients are keyed by client id alone, so they are found by their userPoolId field. @@ -1577,6 +1597,21 @@ public class CognitoService implements ResourceProvider { String temporaryPassword, String messageAction, boolean forceAliasCreation) { + // Locked on the requested username/alias (not yet resolved to a canonical id, since + // an alias pool doesn't have one until creation), so two concurrent requests for the + // same identifier can't both pass the existence/alias check and create duplicates. + synchronized (userLock(userPoolId, username)) { + return adminCreateUserUnderUserLock(userPoolId, username, attributes, + temporaryPassword, messageAction, forceAliasCreation); + } + } + + private CognitoUser adminCreateUserUnderUserLock(String userPoolId, + String username, + Map attributes, + String temporaryPassword, + String messageAction, + boolean forceAliasCreation) { UserPool pool = describeUserPool(userPoolId); boolean resend = "RESEND".equalsIgnoreCase(messageAction); boolean aliasPool = usesAliasUsernames(pool); @@ -1654,6 +1689,13 @@ public class CognitoService implements ResourceProvider { void adminCreateMigratedUser(String userPoolId, String username, String password, Map attributes, String finalUserStatus) { + synchronized (userLock(userPoolId, username)) { + adminCreateMigratedUserUnderUserLock(userPoolId, username, password, attributes, finalUserStatus); + } + } + + private void adminCreateMigratedUserUnderUserLock(String userPoolId, String username, String password, + Map attributes, String finalUserStatus) { UserPool pool = describeUserPool(userPoolId); boolean aliasPool = usesAliasUsernames(pool); @@ -1754,6 +1796,13 @@ public class CognitoService implements ResourceProvider { } public void adminDeleteUser(String userPoolId, String username) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminDeleteUserUnderUserLock(userPoolId, resolvedUser.getUsername()); + } + } + + private void adminDeleteUserUnderUserLock(String userPoolId, String username) { CognitoUser user = adminGetUser(userPoolId, username); for (String groupName : new ArrayList<>(user.getGroupNames())) { groupStore.get(groupKey(userPoolId, groupName)).ifPresent(group -> { @@ -1766,6 +1815,14 @@ public class CognitoService implements ResourceProvider { } public void adminSetUserPassword(String userPoolId, String username, String password, boolean permanent) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminSetUserPasswordUnderUserLock(userPoolId, resolvedUser.getUsername(), password, permanent); + } + } + + private void adminSetUserPasswordUnderUserLock(String userPoolId, String username, String password, + boolean permanent) { CognitoUser user = adminGetUser(userPoolId, username); updateUserPassword(user, password); user.setTemporaryPassword(!permanent); @@ -1776,6 +1833,15 @@ public class CognitoService implements ResourceProvider { } public void adminUpdateUserAttributes(String userPoolId, String username, Map attributes) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminUpdateUserAttributesUnderUserLock( + userPoolId, resolvedUser.getUsername(), attributes); + } + } + + private void adminUpdateUserAttributesUnderUserLock( + String userPoolId, String username, Map attributes) { CognitoUser user = adminGetUser(userPoolId, username); UserPool pool = describeUserPool(userPoolId); if (usesAliasUsernames(pool)) { @@ -1799,6 +1865,15 @@ public class CognitoService implements ResourceProvider { } public void adminDeleteUserAttributes(String userPoolId, String username, List attributeNames) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminDeleteUserAttributesUnderUserLock( + userPoolId, resolvedUser.getUsername(), attributeNames); + } + } + + private void adminDeleteUserAttributesUnderUserLock( + String userPoolId, String username, List attributeNames) { CognitoUser user = adminGetUser(userPoolId, username); for (String attrName : attributeNames) { user.getAttributes().remove(attrName); @@ -1810,6 +1885,13 @@ public class CognitoService implements ResourceProvider { } public void adminEnableUser(String userPoolId, String username) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminEnableUserUnderUserLock(userPoolId, resolvedUser.getUsername()); + } + } + + private void adminEnableUserUnderUserLock(String userPoolId, String username) { CognitoUser user = adminGetUser(userPoolId, username); user.setEnabled(true); user.setLastModifiedDate(System.currentTimeMillis() / 1000L); @@ -1818,6 +1900,13 @@ public class CognitoService implements ResourceProvider { } public void adminDisableUser(String userPoolId, String username) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminDisableUserUnderUserLock(userPoolId, resolvedUser.getUsername()); + } + } + + private void adminDisableUserUnderUserLock(String userPoolId, String username) { CognitoUser user = adminGetUser(userPoolId, username); user.setEnabled(false); user.setLastModifiedDate(System.currentTimeMillis() / 1000L); @@ -1826,6 +1915,13 @@ public class CognitoService implements ResourceProvider { } public void adminResetUserPassword(String userPoolId, String username) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminResetUserPasswordUnderUserLock(userPoolId, resolvedUser.getUsername()); + } + } + + private void adminResetUserPasswordUnderUserLock(String userPoolId, String username) { CognitoUser user = adminGetUser(userPoolId, username); UserPool pool = describeUserPool(userPoolId); String outgoingPasswordHash = user.getPasswordHash(); @@ -1870,10 +1966,20 @@ public class CognitoService implements ResourceProvider { "SourceUser.ProviderAttributeValue is required.", 400); } + CognitoUser resolvedUser = adminGetUser(userPoolId, destinationUsername); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminLinkProviderForUserUnderUserLock(userPoolId, resolvedUser.getUsername(), + sourceProviderName, sourceUserId); + } + } + + private void adminLinkProviderForUserUnderUserLock(String userPoolId, String destinationUsername, + String sourceProviderName, String sourceUserId) { // The uniqueness check and the write must not interleave with another // link of the same source identity. The UserPool object cannot serve as // the monitor — updateUserPool replaces the stored instance — so links - // serialize on a dedicated lock. + // serialize on a dedicated lock in addition to the per-user lock above, + // since the uniqueness check spans every user in the pool, not just this one. synchronized (identityLinkLock) { CognitoUser user = adminGetUser(userPoolId, destinationUsername); String prefix = userPoolId + "::"; @@ -2191,13 +2297,19 @@ public class CognitoService implements ResourceProvider { public void deleteGroup(String userPoolId, String groupName) { CognitoGroup group = getGroup(userPoolId, groupName); long now = System.currentTimeMillis() / 1000L; + // Each member is locked individually, one at a time, rather than holding every + // member's lock for the duration of the loop: this method never needs more than one + // user's invariant held at once, and locking them one at a time avoids having to + // reason about lock ordering across members. for (String username : new ArrayList<>(group.getUserNames())) { - userStore.get(userKey(userPoolId, username)).ifPresent(user -> { - if (user.getGroupNames().remove(groupName)) { - user.setLastModifiedDate(now); - userStore.put(userKey(userPoolId, user.getUsername()), user); - } - }); + synchronized (userLock(userPoolId, username)) { + userStore.get(userKey(userPoolId, username)).ifPresent(user -> { + if (user.getGroupNames().remove(groupName)) { + user.setLastModifiedDate(now); + userStore.put(userKey(userPoolId, user.getUsername()), user); + } + }); + } } groupStore.delete(groupKey(userPoolId, groupName)); LOG.infov("Deleted Cognito group: {0} from pool {1}", groupName, userPoolId); @@ -2223,6 +2335,13 @@ public class CognitoService implements ResourceProvider { } public void adminAddUserToGroup(String userPoolId, String groupName, String username) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminAddUserToGroupUnderUserLock(userPoolId, groupName, resolvedUser.getUsername()); + } + } + + private void adminAddUserToGroupUnderUserLock(String userPoolId, String groupName, String username) { CognitoGroup group = getGroup(userPoolId, groupName); CognitoUser user = adminGetUser(userPoolId, username); long now = System.currentTimeMillis() / 1000L; @@ -2238,6 +2357,13 @@ public class CognitoService implements ResourceProvider { } public void adminRemoveUserFromGroup(String userPoolId, String groupName, String username) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminRemoveUserFromGroupUnderUserLock(userPoolId, groupName, resolvedUser.getUsername()); + } + } + + private void adminRemoveUserFromGroupUnderUserLock(String userPoolId, String groupName, String username) { CognitoGroup group = getGroup(userPoolId, groupName); CognitoUser user = adminGetUser(userPoolId, username); long now = System.currentTimeMillis() / 1000L; @@ -2266,6 +2392,15 @@ public class CognitoService implements ResourceProvider { .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found", 400)); String userPoolId = client.getUserPoolId(); + // Locked on the requested username/alias, same reasoning as adminCreateUser: an alias + // pool has no canonical id to lock on until creation succeeds. + synchronized (userLock(userPoolId, username)) { + return signUpUnderUserLock(client, userPoolId, username, password, attributes); + } + } + + private CognitoUser signUpUnderUserLock(UserPoolClient client, String userPoolId, + String username, String password, Map attributes) { UserPool pool = describeUserPool(userPoolId); boolean aliasPool = usesAliasUsernames(pool); @@ -2363,6 +2498,13 @@ public class CognitoService implements ResourceProvider { .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found", 400)); String userPoolId = client.getUserPoolId(); + synchronized (userLock(userPoolId, username)) { + confirmSignUpUnderUserLock(client, userPoolId, username, confirmationCode); + } + } + + private void confirmSignUpUnderUserLock(UserPoolClient client, String userPoolId, String username, + String confirmationCode) { UserPool pool = poolStore.get(userPoolId) .orElseThrow(() -> userPoolNotFound(userPoolId)); CognitoUser user = adminGetUser(client.getUserPoolId(), username); @@ -2375,7 +2517,7 @@ public class CognitoService implements ResourceProvider { throw mapVerificationCodeException(e); } - var signupDeliveryTarget = resolveSignUpDeliveryTarget(pool, user); + DeliveryTarget signupDeliveryTarget = resolveSignUpDeliveryTarget(pool, user); if (signupDeliveryTarget != null) { if ("email".equals(signupDeliveryTarget.attributeName())) { @@ -2452,6 +2594,13 @@ public class CognitoService implements ResourceProvider { } public void adminConfirmSignUp(String userPoolId, String username) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminConfirmSignUpUnderUserLock(userPoolId, resolvedUser.getUsername()); + } + } + + private void adminConfirmSignUpUnderUserLock(String userPoolId, String username) { CognitoUser user = adminGetUser(userPoolId, username); user.setUserStatus("CONFIRMED"); user.setLastModifiedDate(System.currentTimeMillis() / 1000L); @@ -2510,6 +2659,13 @@ public class CognitoService implements ResourceProvider { String username = token.username(); String poolId = token.poolId(); + synchronized (userLock(poolId, username)) { + changePasswordUnderUserLock(poolId, username, previousPassword, proposedPassword); + } + } + + private void changePasswordUnderUserLock(String poolId, String username, String previousPassword, + String proposedPassword) { CognitoUser user = adminGetUser(poolId, username); if (user.getPasswordHash() != null && !user.getPasswordHash().equals(hashPassword(previousPassword))) { throw new AwsException("NotAuthorizedException", "Incorrect username or password", 400); @@ -2590,6 +2746,13 @@ public class CognitoService implements ResourceProvider { String username = token.username(); String poolId = token.poolId(); + synchronized (userLock(poolId, username)) { + return getUserAttributeVerificationCodeUnderUserLock(poolId, username, attributeName); + } + } + + private Map getUserAttributeVerificationCodeUnderUserLock( + String poolId, String username, String attributeName) { if (!"email".equals(attributeName) && !"phone_number".equals(attributeName)) { throw new AwsException("InvalidParameterException", "Invalid attribute name. Only phone_number and email can be verified.", 400); @@ -2642,6 +2805,13 @@ public class CognitoService implements ResourceProvider { String username = token.username(); String poolId = token.poolId(); + synchronized (userLock(poolId, username)) { + verifyUserAttributeUnderUserLock(poolId, username, attributeName, code); + } + } + + private void verifyUserAttributeUnderUserLock( + String poolId, String username, String attributeName, String code) { if (!"email".equals(attributeName) && !"phone_number".equals(attributeName)) { throw new AwsException("InvalidParameterException", "Invalid attribute name. Only phone_number and email can be verified.", 400); @@ -2691,6 +2861,13 @@ public class CognitoService implements ResourceProvider { 400); } + synchronized (userLock(poolId, username)) { + return updateUserAttributesUnderUserLock(poolId, username, attributes); + } + } + + private List> updateUserAttributesUnderUserLock( + String poolId, String username, Map attributes) { UserPool pool = describeUserPool(poolId); CognitoUser currentUser = adminGetUser(poolId, username); @@ -4324,6 +4501,19 @@ public class CognitoService implements ResourceProvider { Boolean emailEnabled, Boolean emailPreferred) { + CognitoUser resolvedUser = adminGetUser(userPoolId, username); + synchronized (userLock(userPoolId, resolvedUser.getUsername())) { + adminSetUserMFAPreferenceUnderUserLock( + userPoolId, resolvedUser.getUsername(), emailEnabled, emailPreferred); + } + } + + private void adminSetUserMFAPreferenceUnderUserLock( + String userPoolId, + String username, + Boolean emailEnabled, + Boolean emailPreferred) { + CognitoUser user = adminGetUser(userPoolId, username); updateEmailMfaPreference(user, emailEnabled, emailPreferred); @@ -4339,13 +4529,25 @@ public class CognitoService implements ResourceProvider { Boolean emailPreferred) { VerifiedAccessToken token = verifyAccessToken(accessToken); - CognitoUser user = adminGetUser(token.poolId(), token.username()); + synchronized (userLock(token.poolId(), token.username())) { + setUserMFAPreferenceUnderUserLock( + token.poolId(), token.username(), emailEnabled, emailPreferred); + } + } + + private void setUserMFAPreferenceUnderUserLock( + String poolId, + String username, + Boolean emailEnabled, + Boolean emailPreferred) { + + CognitoUser user = adminGetUser(poolId, username); updateEmailMfaPreference(user, emailEnabled, emailPreferred); user.setLastModifiedDate(System.currentTimeMillis() / 1000L); - userStore.put(userKey(token.poolId(), user.getUsername()), user); + userStore.put(userKey(poolId, user.getUsername()), user); } private void updateEmailMfaPreference( diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAttributeVerificationConcurrencyTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAttributeVerificationConcurrencyTest.java new file mode 100644 index 0000000000..09fd7196cc --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAttributeVerificationConcurrencyTest.java @@ -0,0 +1,486 @@ +package io.github.hectorvent.floci.services.cognito; + +import io.github.hectorvent.floci.config.TlsCertificateManager; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.acm.AcmService; +import io.github.hectorvent.floci.services.cognito.model.CognitoUser; +import io.github.hectorvent.floci.services.cognito.model.UserPool; +import io.github.hectorvent.floci.services.cognito.model.UserPoolClient; +import io.github.hectorvent.floci.services.cognito.verification.CognitoMessageDispatcher; +import io.github.hectorvent.floci.services.cognito.verification.VerificationCode; +import io.github.hectorvent.floci.services.cognito.verification.VerificationCodeException; +import io.github.hectorvent.floci.services.cognito.verification.VerificationCodeService; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class CognitoAttributeVerificationConcurrencyTest { + + private static final String OLD_EMAIL = "old@example.com"; + private static final String FIRST_EMAIL = "alpha@example.com"; + private static final String SECOND_EMAIL = "beta@example.com"; + private static final String ADMIN_EMAIL = "admin@example.com"; + private static final String FIRST_CODE = "111111"; + private static final String SECOND_CODE = "222222"; + + @Test + void concurrentUpdatesAreSerializedPerUser() throws Exception { + VerificationCodeService codes = mock(VerificationCodeService.class); + CognitoMessageDispatcher dispatcher = mock(CognitoMessageDispatcher.class); + Harness harness = harness(codes, dispatcher); + AtomicInteger issues = new AtomicInteger(); + AtomicReference latestCode = new AtomicReference<>(); + Map destinationByCode = new java.util.concurrent.ConcurrentHashMap<>(); + CountDownLatch firstIssueEntered = new CountDownLatch(1); + CountDownLatch releaseFirstIssue = new CountDownLatch(1); + CountDownLatch secondStarted = new CountDownLatch(1); + + when(codes.issue(anyString(), anyString(), any(), any(Duration.class))) + .thenAnswer(invocation -> { + int issue = issues.incrementAndGet(); + if (issue == 1) { + firstIssueEntered.countDown(); + await(releaseFirstIssue); + } + String code = issue == 1 ? FIRST_CODE : SECOND_CODE; + latestCode.set(code); + return code; + }); + doAnswer(invocation -> { + CognitoUser deliveryUser = invocation.getArgument(1); + String code = invocation.getArgument(3); + destinationByCode.put(code, deliveryUser.getAttributes().get("email")); + return null; + }).when(dispatcher).dispatch(any(), any(), any(), anyString(), any()); + acceptOnlyLatestCode(codes, latestCode); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future first = executor.submit(() -> harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", FIRST_EMAIL))); + assertTrue(firstIssueEntered.await(5, TimeUnit.SECONDS)); + + Future second = executor.submit(() -> { + secondStarted.countDown(); + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", SECOND_EMAIL)); + }); + assertTrue(secondStarted.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, () -> second.get(100, TimeUnit.MILLISECONDS)); + releaseFirstIssue.countDown(); + first.get(5, TimeUnit.SECONDS); + second.get(5, TimeUnit.SECONDS); + + String validCode = latestCode.get(); + harness.service().verifyUserAttribute(harness.accessToken(), "email", validCode); + + assertEquals(SECOND_CODE, validCode); + assertEquals(SECOND_EMAIL, harness.user().getAttributes().get("email")); + assertEquals(destinationByCode.get(validCode), harness.user().getAttributes().get("email"), + "the valid code must only promote the destination it was sent to"); + } finally { + releaseFirstIssue.countDown(); + executor.shutdownNow(); + } + } + + @Test + void resendAndUpdateAreSerializedPerUser() throws Exception { + VerificationCodeService codes = mock(VerificationCodeService.class); + CognitoMessageDispatcher dispatcher = mock(CognitoMessageDispatcher.class); + Harness harness = harness(codes, dispatcher); + AtomicInteger issues = new AtomicInteger(); + AtomicReference latestCode = new AtomicReference<>(); + CountDownLatch resendIssueEntered = new CountDownLatch(1); + CountDownLatch releaseResendIssue = new CountDownLatch(1); + CountDownLatch updateStarted = new CountDownLatch(1); + + when(codes.issue(anyString(), anyString(), any(), any(Duration.class))) + .thenAnswer(invocation -> { + int issue = issues.incrementAndGet(); + if (issue == 2) { + resendIssueEntered.countDown(); + await(releaseResendIssue); + } + String code = issue == 1 ? FIRST_CODE : issue == 2 ? SECOND_CODE : "333333"; + latestCode.set(code); + return code; + }); + acceptOnlyLatestCode(codes, latestCode); + + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", FIRST_EMAIL)); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future> resend = executor.submit(() -> harness.service() + .getUserAttributeVerificationCode(harness.accessToken(), "email")); + assertTrue(resendIssueEntered.await(5, TimeUnit.SECONDS)); + + Future update = executor.submit(() -> { + updateStarted.countDown(); + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", SECOND_EMAIL)); + }); + assertTrue(updateStarted.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, () -> update.get(100, TimeUnit.MILLISECONDS)); + releaseResendIssue.countDown(); + Map resendResult = resend.get(5, TimeUnit.SECONDS); + update.get(5, TimeUnit.SECONDS); + + harness.service().verifyUserAttribute( + harness.accessToken(), "email", latestCode.get()); + + assertEquals("a***@e***", resendResult.get("Destination"), + "the resend must complete against its serialized pending email"); + assertEquals(SECOND_EMAIL, harness.user().getAttributes().get("email")); + } finally { + releaseResendIssue.countDown(); + executor.shutdownNow(); + } + } + + @Test + void verifyAndUpdateAreSerializedPerUser() throws Exception { + VerificationCodeService codes = mock(VerificationCodeService.class); + CognitoMessageDispatcher dispatcher = mock(CognitoMessageDispatcher.class); + Harness harness = harness(codes, dispatcher); + AtomicInteger issues = new AtomicInteger(); + CountDownLatch consumeEntered = new CountDownLatch(1); + CountDownLatch releaseConsume = new CountDownLatch(1); + CountDownLatch updateStarted = new CountDownLatch(1); + + when(codes.issue(anyString(), anyString(), any(), any(Duration.class))) + .thenAnswer(invocation -> issues.incrementAndGet() == 1 ? FIRST_CODE : SECOND_CODE); + doAnswer(invocation -> { + assertEquals(FIRST_CODE, invocation.getArgument(3)); + consumeEntered.countDown(); + await(releaseConsume); + return null; + }).when(codes).consume(anyString(), anyString(), any(), anyString()); + + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", FIRST_EMAIL)); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future verify = executor.submit(() -> harness.service().verifyUserAttribute( + harness.accessToken(), "email", FIRST_CODE)); + assertTrue(consumeEntered.await(5, TimeUnit.SECONDS)); + + Future update = executor.submit(() -> { + updateStarted.countDown(); + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", SECOND_EMAIL)); + }); + assertTrue(updateStarted.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, () -> update.get(100, TimeUnit.MILLISECONDS)); + releaseConsume.countDown(); + verify.get(5, TimeUnit.SECONDS); + update.get(5, TimeUnit.SECONDS); + + assertEquals(FIRST_EMAIL, harness.user().getAttributes().get("email")); + assertEquals(SECOND_EMAIL, harness.user().getPendingAttributes().get("email"), + "a successful concurrent update must not be lost when verification completes"); + } finally { + releaseConsume.countDown(); + executor.shutdownNow(); + } + } + + @Test + void verificationFinishingAfterAdminUpdateDoesNotOverwriteAdminEmail() throws Exception { + VerificationCodeService codes = mock(VerificationCodeService.class); + CognitoMessageDispatcher dispatcher = mock(CognitoMessageDispatcher.class); + Harness harness = harness(codes, dispatcher); + CountDownLatch consumeEntered = new CountDownLatch(1); + CountDownLatch releaseConsume = new CountDownLatch(1); + CountDownLatch adminStarted = new CountDownLatch(1); + + when(codes.issue(anyString(), anyString(), any(), any(Duration.class))) + .thenReturn(FIRST_CODE); + doAnswer(invocation -> { + assertEquals(FIRST_CODE, invocation.getArgument(3)); + consumeEntered.countDown(); + await(releaseConsume); + return null; + }).when(codes).consume(anyString(), anyString(), any(), anyString()); + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", FIRST_EMAIL)); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future verify = executor.submit(() -> harness.service().verifyUserAttribute( + harness.accessToken(), "email", FIRST_CODE)); + assertTrue(consumeEntered.await(5, TimeUnit.SECONDS)); + + Future adminUpdate = executor.submit(() -> { + adminStarted.countDown(); + harness.service().adminUpdateUserAttributes( + harness.poolId(), "alice", Map.of("email", ADMIN_EMAIL)); + }); + assertTrue(adminStarted.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, + () -> adminUpdate.get(100, TimeUnit.MILLISECONDS)); + + releaseConsume.countDown(); + verify.get(5, TimeUnit.SECONDS); + adminUpdate.get(5, TimeUnit.SECONDS); + + assertEquals(ADMIN_EMAIL, harness.user().getAttributes().get("email"), + "verification finishing later must not overwrite a completed admin update"); + assertNull(harness.user().getPendingAttributes().get("email")); + } finally { + releaseConsume.countDown(); + executor.shutdownNow(); + } + } + + @Test + void verificationFinishingAfterAdminDeleteDoesNotRestoreDeletedEmail() throws Exception { + VerificationCodeService codes = mock(VerificationCodeService.class); + CognitoMessageDispatcher dispatcher = mock(CognitoMessageDispatcher.class); + Harness harness = harness(codes, dispatcher); + CountDownLatch consumeEntered = new CountDownLatch(1); + CountDownLatch releaseConsume = new CountDownLatch(1); + CountDownLatch adminStarted = new CountDownLatch(1); + + when(codes.issue(anyString(), anyString(), any(), any(Duration.class))) + .thenReturn(FIRST_CODE); + doAnswer(invocation -> { + assertEquals(FIRST_CODE, invocation.getArgument(3)); + consumeEntered.countDown(); + await(releaseConsume); + return null; + }).when(codes).consume(anyString(), anyString(), any(), anyString()); + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", FIRST_EMAIL)); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future verify = executor.submit(() -> harness.service().verifyUserAttribute( + harness.accessToken(), "email", FIRST_CODE)); + assertTrue(consumeEntered.await(5, TimeUnit.SECONDS)); + + Future adminDelete = executor.submit(() -> { + adminStarted.countDown(); + harness.service().adminDeleteUserAttributes( + harness.poolId(), "alice", List.of("email")); + }); + assertTrue(adminStarted.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, + () -> adminDelete.get(100, TimeUnit.MILLISECONDS)); + + releaseConsume.countDown(); + verify.get(5, TimeUnit.SECONDS); + adminDelete.get(5, TimeUnit.SECONDS); + + assertNull(harness.user().getAttributes().get("email"), + "verification finishing later must not restore a completed admin deletion"); + assertNull(harness.user().getPendingAttributes().get("email")); + } finally { + releaseConsume.countDown(); + executor.shutdownNow(); + } + } + + @Test + void verificationFinishingAfterAdminUserDeleteDoesNotResurrectTheUser() throws Exception { + VerificationCodeService codes = mock(VerificationCodeService.class); + CognitoMessageDispatcher dispatcher = mock(CognitoMessageDispatcher.class); + Harness harness = harness(codes, dispatcher); + CountDownLatch consumeEntered = new CountDownLatch(1); + CountDownLatch releaseConsume = new CountDownLatch(1); + CountDownLatch adminStarted = new CountDownLatch(1); + + when(codes.issue(anyString(), anyString(), any(), any(Duration.class))) + .thenReturn(FIRST_CODE); + doAnswer(invocation -> { + assertEquals(FIRST_CODE, invocation.getArgument(3)); + consumeEntered.countDown(); + await(releaseConsume); + return null; + }).when(codes).consume(anyString(), anyString(), any(), anyString()); + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", FIRST_EMAIL)); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future verify = executor.submit(() -> harness.service().verifyUserAttribute( + harness.accessToken(), "email", FIRST_CODE)); + assertTrue(consumeEntered.await(5, TimeUnit.SECONDS)); + + Future adminDelete = executor.submit(() -> { + adminStarted.countDown(); + harness.service().adminDeleteUser(harness.poolId(), "alice"); + }); + assertTrue(adminStarted.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, + () -> adminDelete.get(100, TimeUnit.MILLISECONDS)); + + releaseConsume.countDown(); + verify.get(5, TimeUnit.SECONDS); + adminDelete.get(5, TimeUnit.SECONDS); + + AwsException notFound = assertThrows(AwsException.class, + () -> harness.service().adminGetUser(harness.poolId(), "alice"), + "verification finishing later must not resurrect a completed admin deletion"); + assertEquals("UserNotFoundException", notFound.getErrorCode()); + } finally { + releaseConsume.countDown(); + executor.shutdownNow(); + } + } + + @Test + void changePasswordFinishingDuringVerificationDoesNotLoseEitherChange() throws Exception { + // Demonstrates the per-user lock also serializes an operation outside the original + // five: ChangePassword never touched pending-attribute state before this fix, so + // nothing stopped it from racing VerifyUserAttribute's read-modify-write on the same + // whole-object user record. + VerificationCodeService codes = mock(VerificationCodeService.class); + CognitoMessageDispatcher dispatcher = mock(CognitoMessageDispatcher.class); + Harness harness = harness(codes, dispatcher); + CountDownLatch consumeEntered = new CountDownLatch(1); + CountDownLatch releaseConsume = new CountDownLatch(1); + CountDownLatch changeStarted = new CountDownLatch(1); + + when(codes.issue(anyString(), anyString(), any(), any(Duration.class))) + .thenReturn(FIRST_CODE); + doAnswer(invocation -> { + assertEquals(FIRST_CODE, invocation.getArgument(3)); + consumeEntered.countDown(); + await(releaseConsume); + return null; + }).when(codes).consume(anyString(), anyString(), any(), anyString()); + harness.service().updateUserAttributes( + harness.accessToken(), Map.of("email", FIRST_EMAIL)); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future verify = executor.submit(() -> harness.service().verifyUserAttribute( + harness.accessToken(), "email", FIRST_CODE)); + assertTrue(consumeEntered.await(5, TimeUnit.SECONDS)); + + Future changePassword = executor.submit(() -> { + changeStarted.countDown(); + harness.service().changePassword(harness.accessToken(), "Permanent1!", "Permanent2!"); + }); + assertTrue(changeStarted.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, + () -> changePassword.get(100, TimeUnit.MILLISECONDS)); + + releaseConsume.countDown(); + verify.get(5, TimeUnit.SECONDS); + changePassword.get(5, TimeUnit.SECONDS); + + assertEquals(FIRST_EMAIL, harness.user().getAttributes().get("email"), + "the password change finishing after verification must not lose the promoted email"); + assertEquals("true", harness.user().getAttributes().get("email_verified")); + + assertThrows(AwsException.class, () -> harness.service().initiateAuth( + harness.clientId(), "USER_PASSWORD_AUTH", + Map.of("USERNAME", "alice", "PASSWORD", "Permanent1!")), + "verification finishing must not lose the password change back to the old password"); + @SuppressWarnings("unchecked") + Map reAuth = (Map) harness.service().initiateAuth( + harness.clientId(), "USER_PASSWORD_AUTH", + Map.of("USERNAME", "alice", "PASSWORD", "Permanent2!")) + .get("AuthenticationResult"); + assertTrue(reAuth.get("AccessToken") instanceof String); + } finally { + releaseConsume.countDown(); + executor.shutdownNow(); + } + } + + private static Harness harness(VerificationCodeService codes, + CognitoMessageDispatcher dispatcher) { + CognitoService service = new CognitoService( + new InMemoryStorage<>(), + new InMemoryStorage<>(), + new InMemoryStorage<>(), + new InMemoryStorage<>(), + new InMemoryStorage<>(), + new InMemoryStorage<>(), + new InMemoryStorage<>(), + new InMemoryStorage<>(), + "http://localhost:4566", + new RegionResolver("us-east-1", "000000000000"), + null, + mock(AcmService.class), + codes, + dispatcher, + mock(TlsCertificateManager.class)); + UserPool pool = service.createUserPool(Map.of( + "PoolName", "ConcurrencyPool", + "AutoVerifiedAttributes", List.of("email"), + "UserAttributeUpdateSettings", Map.of( + "AttributesRequireVerificationBeforeUpdate", List.of("email"))), + "us-east-1"); + UserPoolClient client = service.createUserPoolClient( + pool.getId(), "concurrency-client", false, false, List.of(), List.of()); + service.adminCreateUser(pool.getId(), "alice", Map.of( + "email", OLD_EMAIL, + "email_verified", "true"), "TempPass1!"); + service.adminSetUserPassword(pool.getId(), "alice", "Permanent1!", true); + + @SuppressWarnings("unchecked") + Map authentication = (Map) service.initiateAuth( + client.getClientId(), "USER_PASSWORD_AUTH", + Map.of("USERNAME", "alice", "PASSWORD", "Permanent1!")) + .get("AuthenticationResult"); + return new Harness(service, pool.getId(), client.getClientId(), + (String) authentication.get("AccessToken")); + } + + private static void acceptOnlyLatestCode(VerificationCodeService codes, + AtomicReference latestCode) { + doAnswer(invocation -> { + String supplied = invocation.getArgument(3); + if (!supplied.equals(latestCode.get())) { + throw new VerificationCodeException( + VerificationCodeException.Kind.MISMATCH, + "Invalid verification code provided, please try again"); + } + return null; + }).when(codes).consume(anyString(), anyString(), any(), anyString()); + } + + private static void await(CountDownLatch latch) { + try { + assertTrue(latch.await(5, TimeUnit.SECONDS)); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new AssertionError("Interrupted while coordinating concurrency test", e); + } + } + + private record Harness(CognitoService service, String poolId, String clientId, String accessToken) { + CognitoUser user() { + return service.adminGetUser(poolId, "alice"); + } + } +}