Skip to content

Commit

Permalink
Adding support for password_policy.temporary_password_validity_days
Browse files Browse the repository at this point in the history
  • Loading branch information
michalschott committed Jan 11, 2020
1 parent a0a24c3 commit f8e23ac
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
16 changes: 16 additions & 0 deletions aws/resource_aws_cognito_user_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func resourceAwsCognitoUserPool() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Default: 7,
Deprecated: "Use password_policy.temporary_password_validity_days instead",
ValidateFunc: validation.IntBetween(0, 90),
},
},
Expand Down Expand Up @@ -295,6 +296,11 @@ func resourceAwsCognitoUserPool() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"temporary_password_validity_days": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 365),
},
},
},
},
Expand Down Expand Up @@ -672,6 +678,11 @@ func resourceAwsCognitoUserPoolCreate(d *schema.ResourceData, meta interface{})
log.Printf("[DEBUG] Received %s, retrying CreateUserPool", err)
return resource.RetryableError(err)
}
if isAWSErr(err, cognitoidentityprovider.ErrCodeInvalidParameterException, "Please use TemporaryPasswordValidityDays in PasswordPolicy instead of UnusedAccountValidityDays") {
log.Printf("[DEBUG] Received %s, retrying UpdateUserPool without UnusedAccountValidityDays", err)
params.AdminCreateUserConfig.UnusedAccountValidityDays = nil
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
})
Expand Down Expand Up @@ -948,6 +959,11 @@ func resourceAwsCognitoUserPoolUpdate(d *schema.ResourceData, meta interface{})
log.Printf("[DEBUG] Received %s, retrying UpdateUserPool", err)
return resource.RetryableError(err)
}
if isAWSErr(err, cognitoidentityprovider.ErrCodeInvalidParameterException, "Please use TemporaryPasswordValidityDays in PasswordPolicy instead of UnusedAccountValidityDays") {
log.Printf("[DEBUG] Received %s, retrying UpdateUserPool without UnusedAccountValidityDays", err)
params.AdminCreateUserConfig.UnusedAccountValidityDays = nil
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
})
Expand Down
34 changes: 24 additions & 10 deletions aws/resource_aws_cognito_user_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ func TestAccAWSCognitoUserPool_withPasswordPolicy(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "password_policy.0.require_numbers", "false"),
resource.TestCheckResourceAttr(resourceName, "password_policy.0.require_symbols", "true"),
resource.TestCheckResourceAttr(resourceName, "password_policy.0.require_uppercase", "false"),
resource.TestCheckResourceAttr(resourceName, "password_policy.0.temporary_password_validity_days", "7"),
),
},
{
Expand All @@ -500,6 +501,7 @@ func TestAccAWSCognitoUserPool_withPasswordPolicy(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "password_policy.0.require_numbers", "true"),
resource.TestCheckResourceAttr(resourceName, "password_policy.0.require_symbols", "false"),
resource.TestCheckResourceAttr(resourceName, "password_policy.0.require_uppercase", "true"),
resource.TestCheckResourceAttr(resourceName, "password_policy.0.temporary_password_validity_days", "14"),
),
},
},
Expand Down Expand Up @@ -872,6 +874,11 @@ resource "aws_cognito_user_pool" "test" {
sms_message = "Your username is {username} and temporary password is {####}."
}
}
password_policy {
minimum_length = 6
temporary_password_validity_days = 6
}
}
`, name)
}
Expand All @@ -891,6 +898,11 @@ resource "aws_cognito_user_pool" "test" {
sms_message = "Your username is {username} and constant password is {####}."
}
}
password_policy {
minimum_length = 6
temporary_password_validity_days = 7
}
}
`, name)
}
Expand Down Expand Up @@ -1086,11 +1098,12 @@ resource "aws_cognito_user_pool" "test" {
name = "terraform-test-pool-%s"
password_policy {
minimum_length = 7
require_lowercase = true
require_numbers = false
require_symbols = true
require_uppercase = false
minimum_length = 7
require_lowercase = true
require_numbers = false
require_symbols = true
require_uppercase = false
temporary_password_validity_days = 7
}
}
`, name)
Expand All @@ -1102,11 +1115,12 @@ resource "aws_cognito_user_pool" "test" {
name = "terraform-test-pool-%s"
password_policy {
minimum_length = 9
require_lowercase = false
require_numbers = true
require_symbols = false
require_uppercase = true
minimum_length = 9
require_lowercase = false
require_numbers = true
require_symbols = false
require_uppercase = true
temporary_password_validity_days = 14
}
}
`, name)
Expand Down
8 changes: 8 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2721,6 +2721,10 @@ func expandCognitoUserPoolPasswordPolicy(config map[string]interface{}) *cognito
configs.RequireUppercase = aws.Bool(v.(bool))
}

if v, ok := config["temporary_password_validity_days"]; ok {
configs.TemporaryPasswordValidityDays = aws.Int64(int64(v.(int)))
}

return configs
}

Expand Down Expand Up @@ -2993,6 +2997,10 @@ func flattenCognitoUserPoolPasswordPolicy(s *cognitoidentityprovider.PasswordPol
m["require_uppercase"] = *s.RequireUppercase
}

if s.TemporaryPasswordValidityDays != nil {
m["temporary_password_validity_days"] = *s.TemporaryPasswordValidityDays
}

if len(m) > 0 {
return []map[string]interface{}{m}
}
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/cognito_user_pool.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The following arguments are supported:

* `allow_admin_create_user_only` (Optional) - Set to True if only the administrator is allowed to create user profiles. Set to False if users can sign themselves up via an app.
* `invite_message_template` (Optional) - The [invite message template structure](#invite-message-template).
* `unused_account_validity_days` (Optional) - The user account expiration limit, in days, after which the account is no longer usable.
* `unused_account_validity_days` (Optional) - **DEPRECATED** Use password_policy.temporary_password_validity_days instead - The user account expiration limit, in days, after which the account is no longer usable.

##### Invite Message template

Expand Down Expand Up @@ -87,6 +87,7 @@ The following arguments are supported:
* `require_numbers` (Optional) - Whether you have required users to use at least one number in their password.
* `require_symbols` (Optional) - Whether you have required users to use at least one symbol in their password.
* `require_uppercase` (Optional) - Whether you have required users to use at least one uppercase letter in their password.
* `temporary_password_validity_days` (Optional) - In the password policy you have set, refers to the number of days a temporary password is valid. If the user does not sign-in during this time, their password will need to be reset by an administrator.

#### Schema Attributes

Expand Down

0 comments on commit f8e23ac

Please sign in to comment.