Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support case insensitive usernames for Cognito user pools #12317

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions aws/resource_aws_cognito_user_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,21 @@ func resourceAwsCognitoUserPool() *schema.Resource {
ConflictsWith: []string{"alias_attributes"},
},

"username_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"case_sensitive": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
},
},
},

"user_pool_add_ons": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -644,6 +659,15 @@ func resourceAwsCognitoUserPoolCreate(d *schema.ResourceData, meta interface{})
params.UsernameAttributes = expandStringList(v.([]interface{}))
}

if v, ok := d.GetOk("username_configuration"); ok {
configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})

if ok && config != nil {
params.UsernameConfiguration = expandCognitoUserPoolUsernameConfiguration(config)
}
}

if v, ok := d.GetOk("user_pool_add_ons"); ok {
configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})
Expand Down Expand Up @@ -834,6 +858,10 @@ func resourceAwsCognitoUserPoolRead(d *schema.ResourceData, meta interface{}) er
d.Set("username_attributes", flattenStringList(resp.UserPool.UsernameAttributes))
}

if err := d.Set("username_configuration", flattenCognitoUserPoolUsernameConfiguration(resp.UserPool.UsernameConfiguration)); err != nil {
return fmt.Errorf("Failed setting username_configuration: %s", err)
}

if err := d.Set("user_pool_add_ons", flattenCognitoUserPoolUserPoolAddOns(resp.UserPool.UserPoolAddOns)); err != nil {
return fmt.Errorf("Failed setting user_pool_add_ons: %s", err)
}
Expand Down
58 changes: 58 additions & 0 deletions aws/resource_aws_cognito_user_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,40 @@ func TestAccAWSCognitoUserPool_withPasswordPolicy(t *testing.T) {
})
}

func TestAccAWSCognitoUserPool_withUsernameConfiguration(t *testing.T) {
name := acctest.RandString(5)
resourceName := "aws_cognito_user_pool.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCognitoIdentityProvider(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCognitoUserPoolDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoUserPoolConfig_withUsernameConfiguration(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoUserPoolExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "username_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "username_configuration.0.case_sensitive", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCognitoUserPoolConfig_withUsernameConfigurationUpdated(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoUserPoolExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "username_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "username_configuration.0.case_sensitive", "false"),
),
},
},
})
}

func TestAccAWSCognitoUserPool_withLambdaConfig(t *testing.T) {
name := acctest.RandString(5)
resourceName := "aws_cognito_user_pool.test"
Expand Down Expand Up @@ -1563,6 +1597,30 @@ resource "aws_cognito_user_pool" "test" {
`, name)
}

func testAccAWSCognitoUserPoolConfig_withUsernameConfiguration(name string) string {
return fmt.Sprintf(`
resource "aws_cognito_user_pool" "test" {
name = "terraform-test-pool-%s"

username_configuration {
case_sensitive = true
}
}
`, name)
}

func testAccAWSCognitoUserPoolConfig_withUsernameConfigurationUpdated(name string) string {
return fmt.Sprintf(`
resource "aws_cognito_user_pool" "test" {
name = "terraform-test-pool-%s"

username_configuration {
case_sensitive = false
}
}
`, name)
}

func testAccAWSCognitoUserPoolConfig_withLambdaConfig(name string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
Expand Down
20 changes: 20 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3479,6 +3479,26 @@ func flattenCognitoUserPoolSchema(configuredAttributes, inputs []*cognitoidentit
return values
}

func expandCognitoUserPoolUsernameConfiguration(config map[string]interface{}) *cognitoidentityprovider.UsernameConfigurationType {
usernameConfigurationType := &cognitoidentityprovider.UsernameConfigurationType{
CaseSensitive: aws.Bool(config["case_sensitive"].(bool)),
}

return usernameConfigurationType
}

func flattenCognitoUserPoolUsernameConfiguration(u *cognitoidentityprovider.UsernameConfigurationType) []map[string]interface{} {
m := map[string]interface{}{}

if u == nil {
return nil
}

m["case_sensitive"] = *u.CaseSensitive

return []map[string]interface{}{m}
}

func expandCognitoUserPoolVerificationMessageTemplate(config map[string]interface{}) *cognitoidentityprovider.VerificationMessageTemplateType {
verificationMessageTemplateType := &cognitoidentityprovider.VerificationMessageTemplateType{}

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/cognito_user_pool.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The following arguments are supported:
* `software_token_mfa_configuration` - (Optional) Configuration block for software token Mult-Factor Authentication (MFA) settings. Detailed below.
* `tags` - (Optional) A mapping of tags to assign to the User Pool.
* `username_attributes` - (Optional) Specifies whether email addresses or phone numbers can be specified as usernames when a user signs up. Conflicts with `alias_attributes`.
* `username_configuration` - (Optional) The [Username Configuration](#username-configuration).
* `user_pool_add_ons` - (Optional) Configuration block for [user pool add-ons](#user-pool-add-ons) to enable user pool advanced security mode features.
* `verification_message_template` (Optional) - The [verification message templates](#verification-message-template) configuration.

Expand Down Expand Up @@ -168,6 +169,10 @@ The following arguments are required in the `software_token_mfa_configuration` c

* `enabled` - (Required) Boolean whether to enable software token Multi-Factor (MFA) tokens, such as Time-based One-Time Password (TOTP). To disable software token MFA when `sms_configuration` is not present, the `mfa_configuration` argument must be set to `OFF` and the `software_token_mfa_configuration` configuration block must be fully removed.

#### Username Configuration

* `case_sensitive` (Required) - Specifies whether username case sensitivity will be applied for all users in the user pool through Cognito APIs.

#### User Pool Add-ons

* `advanced_security_mode` (Required) - The mode for advanced security, must be one of `OFF`, `AUDIT` or `ENFORCED`.
Expand Down