mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cognito): keep attribute updates after sender failures (#6493)
* fix(cognito): keep attribute updates after sender failures * fix(cognito): withdraw stale codes after sender failure Invalidate prior verification codes when a failed sender handover retains a different active or pending contact destination. Preserve codes for unchanged contacts the send loop never reached.
This commit is contained in:
1 parent
b15bc708fa
commit
da9f7e93b1
3 files changed
+170
-28
No files matched your search
+124
@@ -21,6 +21,9 @@ import io.github.hectorvent.floci.services.ses.SesService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
@@ -47,6 +50,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -57,6 +61,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -336,6 +341,125 @@ class CognitoCustomEmailSenderTest {
|
||||
verify(lambda, never()).invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
}
|
||||
|
||||
/** The disabled-key capture on #6207 keeps the whole update, in either attribute order. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void aKeyThatCannotEncryptKeepsTheWholeAttributeUpdate(boolean emailFirst) {
|
||||
UserPool pool = pool(Map.of("KMSKeyID", keyArn, "CustomEmailSender", sender()), List.of("email"));
|
||||
UserPoolClient client = client(pool);
|
||||
service.adminCreateUser(pool.getId(), "alice",
|
||||
Map.of("email", "alice@example.com", "email_verified", "true", "name", "Original"), null);
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "Passw0rd!", true);
|
||||
String token = signIn(client, "alice", "Passw0rd!");
|
||||
kms.disableKey(keyArn, REGION);
|
||||
Map<String, String> attributes = new LinkedHashMap<>();
|
||||
if (emailFirst) {
|
||||
attributes.put("email", "alice@example.org");
|
||||
}
|
||||
attributes.put("name", "Renamed");
|
||||
if (!emailFirst) {
|
||||
attributes.put("email", "alice@example.org");
|
||||
}
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> service.updateUserAttributes(token, attributes));
|
||||
|
||||
Map<String, String> stored = service.adminGetUser(pool.getId(), "alice").getAttributes();
|
||||
assertAll(
|
||||
() -> assertEquals("CodeDeliveryFailureException", failure.getErrorCode()),
|
||||
() -> assertEquals("Caller needs to enable the configured KMS Key", failure.getMessage()),
|
||||
() -> assertEquals("alice@example.org", stored.get("email")),
|
||||
() -> assertEquals("false", stored.get("email_verified")),
|
||||
() -> assertEquals("Renamed", stored.get("name")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUndeliveredAttributeUpdateCodeCanBeRequestedAgain() throws Exception {
|
||||
UserPool pool = pool(Map.of("KMSKeyID", keyArn, "CustomEmailSender", sender()), List.of("email"));
|
||||
UserPoolClient client = client(pool);
|
||||
service.adminCreateUser(pool.getId(), "alice",
|
||||
Map.of("email", "alice@example.com", "email_verified", "true"), null);
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "Passw0rd!", true);
|
||||
String token = signIn(client, "alice", "Passw0rd!");
|
||||
kms.disableKey(keyArn, REGION);
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.updateUserAttributes(token, Map.of("email", "alice@example.org")));
|
||||
kms.enableKey(keyArn, REGION);
|
||||
|
||||
service.getUserAttributeVerificationCode(token, "email");
|
||||
|
||||
Map<String, Object> verification = senderEvent();
|
||||
assertEquals("CustomEmailSender_VerifyUserAttribute", verification.get("triggerSource"));
|
||||
assertEquals("alice@example.org",
|
||||
((Map<?, ?>) request(verification).get("userAttributes")).get("email"));
|
||||
service.verifyUserAttribute(token, "email", decrypt(request(verification)).plaintext());
|
||||
assertEquals("true", service.adminGetUser(pool.getId(), "alice").getAttributes().get("email_verified"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"OMIT, false", "SAME, false", "REPLACE, false", "REPLACE, true"})
|
||||
void anEmailSenderFailureOnlyWithdrawsCodesForChangedDestinations(String phoneUpdate, boolean requiresVerification) {
|
||||
UserPool pool = pool(Map.of("KMSKeyID", keyArn, "CustomEmailSender", sender()),
|
||||
List.of("email", "phone_number"));
|
||||
if (requiresVerification) {
|
||||
service.updateUserPool(Map.of("UserPoolId", pool.getId(), "UserAttributeUpdateSettings",
|
||||
Map.of("AttributesRequireVerificationBeforeUpdate", List.of("phone_number"))), REGION);
|
||||
}
|
||||
UserPoolClient client = client(pool);
|
||||
service.adminCreateUser(pool.getId(), "alice", Map.of(
|
||||
"email", "alice@example.com", "email_verified", "true",
|
||||
"phone_number", "+15555550100", "phone_number_verified", "false"), null);
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "Passw0rd!", true);
|
||||
String token = signIn(client, "alice", "Passw0rd!");
|
||||
clearInvocations(sns);
|
||||
service.getUserAttributeVerificationCode(token, "phone_number");
|
||||
ArgumentCaptor<String> oldMessage = ArgumentCaptor.forClass(String.class);
|
||||
verify(sns).publish(any(), any(), eq("+15555550100"), oldMessage.capture(), any(), any(), eq(REGION));
|
||||
String oldCode = oldMessage.getValue().replaceAll("\\D", "");
|
||||
assertTrue(oldCode.matches("[0-9]{6}"));
|
||||
kms.disableKey(keyArn, REGION);
|
||||
Map<String, String> attributes = new LinkedHashMap<>();
|
||||
attributes.put("email", "alice@example.org");
|
||||
attributes.put("name", "Renamed");
|
||||
boolean replacePhone = "REPLACE".equals(phoneUpdate);
|
||||
if (replacePhone) {
|
||||
attributes.put("phone_number", "+15555550200");
|
||||
} else if ("SAME".equals(phoneUpdate)) {
|
||||
attributes.put("phone_number", "+15555550100");
|
||||
}
|
||||
|
||||
AwsException delivery = assertThrows(AwsException.class,
|
||||
() -> service.updateUserAttributes(token, attributes));
|
||||
|
||||
assertEquals("CodeDeliveryFailureException", delivery.getErrorCode());
|
||||
if (!replacePhone) {
|
||||
assertEquals("+15555550100", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number"));
|
||||
service.verifyUserAttribute(token, "phone_number", oldCode);
|
||||
assertEquals("true", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number_verified"));
|
||||
return;
|
||||
}
|
||||
assertEquals(requiresVerification ? "+15555550100" : "+15555550200",
|
||||
service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number"));
|
||||
if (requiresVerification) {
|
||||
assertEquals("+15555550200",
|
||||
service.adminGetUser(pool.getId(), "alice").getPendingAttributes().get("phone_number"));
|
||||
}
|
||||
AwsException staleCode = assertThrows(AwsException.class,
|
||||
() -> service.verifyUserAttribute(token, "phone_number", oldCode));
|
||||
assertEquals("CodeMismatchException", staleCode.getErrorCode());
|
||||
assertEquals("false", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number_verified"));
|
||||
|
||||
service.getUserAttributeVerificationCode(token, "phone_number");
|
||||
ArgumentCaptor<String> newMessage = ArgumentCaptor.forClass(String.class);
|
||||
verify(sns).publish(any(), any(), eq("+15555550200"), newMessage.capture(), any(), any(), eq(REGION));
|
||||
String newCode = newMessage.getValue().replaceAll("\\D", "");
|
||||
assertTrue(newCode.matches("[0-9]{6}"));
|
||||
service.verifyUserAttribute(token, "phone_number", newCode);
|
||||
assertEquals("+15555550200", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number"));
|
||||
assertEquals("true", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number_verified"));
|
||||
assertFalse(service.adminGetUser(pool.getId(), "alice").getPendingAttributes().containsKey("phone_number"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured (eu-west-1, 2026-10-20): Cognito hands out a code again at once, on a healthy pool
|
||||
* and once a key that could not encrypt is enabled again. The code a failed delivery issued is
|
||||
|
||||
Reference in new issue
Block a user