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'