From f0a1b11385eae7d1f0238aad37ded5bc2d79ad2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ribeiro?= <9574254+vinirib@users.noreply.github.com> Date: Sat, 31 Oct 2026 12:52:52 -0300 Subject: [PATCH] fix(cloudformation): keep SMS_MFA on a user pool update until MFA is turned off (#7088) * fix(cloudformation): keep SMS_MFA on a user pool update until MFA is turned off The AWS::Cognito::UserPool reference says that once SMS_MFA is enabled, only MfaConfiguration OFF turns it off. An update that stopped listing SMS_MFA in EnabledMfas cleared it. replaceUserPoolMfaConfig now keeps the pool's SMS setting when none is given and the mode is not OFF. Closes #7086 * fix(cognito): keep SMS MFA with the pool's current SMS settings Keeping SMS MFA replayed the settings it was enabled with, so an update that dropped SMS_MFA and changed SmsAuthenticationMessage or SmsConfiguration put the old values back over the ones updateUserPool had just applied. The kept SMS MFA now takes the pool's current message and SMS configuration. --- docs/services/cloudformation.md | 2 +- .../services/cognito/CognitoService.java | 14 ++++++++ .../CognitoCfnIntegrationTest.java | 34 +++++++++++++++++++ .../cognito/CognitoReplaceMfaConfigTest.java | 11 ++++++ tools/docs/cfn_resource_types.yaml | 2 +- 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index 516c013494..bd52ddcadb 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -203,7 +203,7 @@ cross-resource references. | CodePipeline | `Pipeline`, `CustomActionType`, `Webhook` | | CodeBuild | `Project` | | Batch | `ComputeEnvironment`, `JobQueue`, `JobDefinition` | -| Cognito | `UserPool` (`ProviderURL` is the local issuer of the tokens Floci mints, `/`; `EnabledMfas` is applied with `SetUserPoolMfaConfig`, as on AWS), `UserPoolClient`, `UserPoolDomain`, `UserPoolGroup`, `UserPoolResourceServer`, `UserPoolUser`, `UserPoolUserToGroupAttachment` | +| Cognito | `UserPool` (`ProviderURL` is the local issuer of the tokens Floci mints, `/`; `EnabledMfas` is applied with `SetUserPoolMfaConfig`, as on AWS, and once `SMS_MFA` is on only `MfaConfiguration: OFF` turns it off), `UserPoolClient`, `UserPoolDomain`, `UserPoolGroup`, `UserPoolResourceServer`, `UserPoolUser`, `UserPoolUserToGroupAttachment` | | ACM | `Certificate` | | EventBridge | `Rule`, `EventBus`, `EventBusPolicy`, `ApiDestination`, `Archive` (`KmsKeyIdentifier` is ignored) | | EventBridge Scheduler | `Schedule`, `ScheduleGroup` | 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 a7aa207d5e..20cc0b4686 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 @@ -850,6 +850,10 @@ public class CognitoService implements ResourceProvider { * how CloudFormation applies a user pool's EnabledMfas. The settings are built on a copy and * stored in one write, so a concurrent sign-in never reads the pool between its old and new * MFA settings, and under the pool lock, so a concurrent tag or update is kept. + * SMS is the exception: once enabled, only MfaConfiguration OFF turns it off, as the + * AWS::Cognito::UserPool reference documents, so an SMS setting that is not given is kept, + * with the pool's current message and SMS configuration rather than the ones it was enabled + * with, since an update may have just changed them. */ public UserPool replaceUserPoolMfaConfig(String id, String mfaConfiguration, Boolean softwareTokenMfaEnabled, @@ -857,6 +861,16 @@ public class CognitoService implements ResourceProvider { Map emailMfaConfiguration) { synchronized (poolLock(id)) { UserPool pool = MAPPER.convertValue(describeUserPool(id), UserPool.class); + if (smsMfaConfiguration == null && mfaConfiguration != null && !"OFF".equals(mfaConfiguration) + && pool.getSmsMfaConfiguration() != null) { + smsMfaConfiguration = new LinkedHashMap<>(pool.getSmsMfaConfiguration()); + if (pool.getSmsAuthenticationMessage() != null) { + smsMfaConfiguration.put("SmsAuthenticationMessage", pool.getSmsAuthenticationMessage()); + } + if (pool.getSmsConfiguration() != null) { + smsMfaConfiguration.put("SmsConfiguration", pool.getSmsConfiguration()); + } + } applyMfaConfig(pool, mfaConfiguration, softwareTokenMfaEnabled, smsMfaConfiguration, emailMfaConfiguration, null, true); poolStore.put(id, pool); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java index 982a7374b0..6ec9081f8c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CognitoCfnIntegrationTest.java @@ -528,6 +528,40 @@ class CognitoCfnIntegrationTest { CfnStackWaits.awaitStackDeleted(stack); } + /** Once SMS_MFA is enabled, only MfaConfiguration OFF turns it off, per the template reference. */ + @Test + void anUpdateKeepsSmsMfaUntilMfaIsTurnedOff() throws Exception { + String stack = "cognito-cfn-enabled-mfas-keep-sms-it"; + String smsConfiguration = "\"SmsConfiguration\": {\"SnsCallerArn\": \"arn:aws:iam::000000000000:role/sms\"}"; + cloudFormation(stack, "CreateStack", enabledMfasTemplate( + "\"MfaConfiguration\": \"OPTIONAL\", \"EnabledMfas\": [\"SMS_MFA\", \"SOFTWARE_TOKEN_MFA\"], " + + smsConfiguration)); + String poolId = outputValue(describeStacks(stack, "CREATE_COMPLETE"), "PoolId"); + + cloudFormation(stack, "UpdateStack", enabledMfasTemplate( + "\"MfaConfiguration\": \"OPTIONAL\", \"EnabledMfas\": [\"SOFTWARE_TOKEN_MFA\"], " + + "\"SmsAuthenticationMessage\": \"New code {####}\", " + + "\"SmsConfiguration\": {\"SnsCallerArn\": \"arn:aws:iam::000000000000:role/sms-v2\"}")); + describeStacks(stack, "UPDATE_COMPLETE"); + cognitoAction("GetUserPoolMfaConfig", "{\"UserPoolId\": \"" + poolId + "\"}") + .then().statusCode(200) + .body("MfaConfiguration", equalTo("OPTIONAL")) + .body("SoftwareTokenMfaConfiguration.Enabled", equalTo(true)) + .body("SmsMfaConfiguration.SmsAuthenticationMessage", equalTo("New code {####}")) + .body("SmsMfaConfiguration.SmsConfiguration.SnsCallerArn", equalTo("arn:aws:iam::000000000000:role/sms-v2")); + cognitoAction("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}") + .then().statusCode(200) + .body("UserPool.SmsAuthenticationMessage", equalTo("New code {####}")) + .body("UserPool.SmsConfiguration.SnsCallerArn", equalTo("arn:aws:iam::000000000000:role/sms-v2")); + + cloudFormation(stack, "UpdateStack", enabledMfasTemplate("\"MfaConfiguration\": \"OFF\"")); + describeStacks(stack, "UPDATE_COMPLETE"); + assertMfa(poolId, "OFF", false, false); + + cloudFormation(stack, "DeleteStack", null); + CfnStackWaits.awaitStackDeleted(stack); + } + /** OFF with a listed factor is refused by SetUserPoolMfaConfig after the pool exists. */ @Test void aRefusedEnabledMfasOnCreateRemovesThePool() throws Exception { diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoReplaceMfaConfigTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoReplaceMfaConfigTest.java index 6e2c71b0f5..3e30bc863f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoReplaceMfaConfigTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoReplaceMfaConfigTest.java @@ -53,6 +53,17 @@ class CognitoReplaceMfaConfigTest { assertNotNull(pool.getSmsMfaConfiguration()); } + @Test + void smsThatIsNotGivenStaysOnUntilMfaIsTurnedOff() { + service.replaceUserPoolMfaConfig(poolId, "OPTIONAL", true, Map.of("SmsAuthenticationMessage", "{####}"), null); + + service.replaceUserPoolMfaConfig(poolId, "OPTIONAL", true, null, null); + assertEquals("{####}", service.describeUserPool(poolId).getSmsMfaConfiguration().get("SmsAuthenticationMessage")); + + service.replaceUserPoolMfaConfig(poolId, "OFF", null, null, null); + assertNull(service.describeUserPool(poolId).getSmsMfaConfiguration()); + } + @Test void thePoolAReaderAlreadyHoldsKeepsItsSettings() { UserPool before = service.describeUserPool(poolId); diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml index bf635c59a0..d103549018 100644 --- a/tools/docs/cfn_resource_types.yaml +++ b/tools/docs/cfn_resource_types.yaml @@ -211,7 +211,7 @@ notes: AWS::Route53::RecordSetGroup: 'written in one change batch, so a record set that cannot be written leaves the zone unchanged; every record set must be in the group''s hosted zone' Custom::DynamoDBReplica: applied natively against DynamoDB, not via a provider Lambda AWS::DynamoDB::Table: '`TimeToLiveSpecification` applied on create and update; to rename an enabled TTL attribute, disable TTL in one update and enable the new attribute in a later one' - AWS::Cognito::UserPool: '`ProviderURL` is the local issuer of the tokens Floci mints, `/`; `EnabledMfas` is applied with `SetUserPoolMfaConfig`, as on AWS' + AWS::Cognito::UserPool: '`ProviderURL` is the local issuer of the tokens Floci mints, `/`; `EnabledMfas` is applied with `SetUserPoolMfaConfig`, as on AWS, and once `SMS_MFA` is on only `MfaConfiguration: OFF` turns it off' AWS::SES::EmailIdentity: 'BYODKIM is not supported by this resource; Easy DKIM DNS attributes use the shared emulator signing zone `dkim.amazonses.com` rather than AWS''s region- and identity-specific zone assignment' AWS::SES::ReceiptRule: 'stored in an existing SES receipt rule set; Ref and RuleName return the rule name; management plane only, with no inbound mail delivery' AWS::Events::Archive: '`KmsKeyIdentifier` is ignored'