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

Add email sending account #8626

Closed
wants to merge 2 commits into from
Closed
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
43 changes: 35 additions & 8 deletions aws/resource_aws_cognito_user_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -128,21 +129,35 @@ func resourceAwsCognitoUserPool() *schema.Resource {
},

"email_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: suppressMissingOptionalConfigurationBlock,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"reply_to_email_address": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to get our testing working I added "" as a valid value in the validation function.

Type: schema.TypeString,
Optional: true,
ValidateFunc: validateCognitoUserPoolReplyEmailAddress,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.Any(
validation.StringInSlice([]string{""}, false),
validation.StringMatch(regexp.MustCompile(`[\p{L}\p{M}\p{S}\p{N}\p{P}]+@[\p{L}\p{M}\p{S}\p{N}\p{P}]+`),
`must satisfy regular expression pattern: [\p{L}\p{M}\p{S}\p{N}\p{P}]+@[\p{L}\p{M}\p{S}\p{N}\p{P}]+`),
),
},
"source_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"email_sending_account": {
nywilken marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Default: cognitoidentityprovider.EmailSendingAccountTypeCognitoDefault,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentityprovider.EmailSendingAccountTypeCognitoDefault,
cognitoidentityprovider.EmailSendingAccountTypeDeveloper,
}, false),
},
},
},
},
Expand Down Expand Up @@ -529,6 +544,10 @@ func resourceAwsCognitoUserPoolCreate(d *schema.ResourceData, meta interface{})
emailConfigurationType.SourceArn = aws.String(v.(string))
}

if v, ok := config["email_sending_account"]; ok && v.(string) != "" {
emailConfigurationType.EmailSendingAccount = aws.String(v.(string))
}

params.EmailConfiguration = emailConfigurationType
}
}
Expand Down Expand Up @@ -723,8 +742,10 @@ func resourceAwsCognitoUserPoolRead(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("Failed setting device_configuration: %s", err)
}

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

if resp.UserPool.Policies != nil && resp.UserPool.Policies.PasswordPolicy != nil {
Expand Down Expand Up @@ -795,10 +816,12 @@ func resourceAwsCognitoUserPoolUpdate(d *schema.ResourceData, meta interface{})
}

if v, ok := d.GetOk("email_configuration"); ok {

configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})

if ok && config != nil {
log.Printf("[DEBUG] Set Values to update from configs")
emailConfigurationType := &cognitoidentityprovider.EmailConfigurationType{}

if v, ok := config["reply_to_email_address"]; ok && v.(string) != "" {
Expand All @@ -809,6 +832,10 @@ func resourceAwsCognitoUserPoolUpdate(d *schema.ResourceData, meta interface{})
emailConfigurationType.SourceArn = aws.String(v.(string))
}

if v, ok := config["email_sending_account"]; ok && v.(string) != "" {
emailConfigurationType.EmailSendingAccount = aws.String(v.(string))
}

params.EmailConfiguration = emailConfigurationType
}
}
Expand Down
35 changes: 24 additions & 11 deletions aws/resource_aws_cognito_user_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -270,23 +271,33 @@ func TestAccAWSCognitoUserPool_withSmsVerificationMessage(t *testing.T) {

func TestAccAWSCognitoUserPool_withEmailConfiguration(t *testing.T) {
name := acctest.RandString(5)
replyTo := fmt.Sprintf("tf-acc-reply-%[email protected]", name)

sourceARN, ok := os.LookupEnv("TEST_AWS_SES_VERIFIED_EMAIL_ARN")
if !ok {
t.Skip("'TEST_AWS_SES_VERIFIED_EMAIL_ARN' not set, skipping test.")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCognitoIdentityProvider(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCognitoUserPoolDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoUserPoolConfig_basic(name),
Config: testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name, "", "", "COGNITO_DEFAULT"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoUserPoolExists("aws_cognito_user_pool.pool"),
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.#", "1"),
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.reply_to_email_address", ""),
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.email_sending_account", "COGNITO_DEFAULT"),
),
},
{
Config: testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name),
Config: testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name, replyTo, sourceARN, "DEVELOPER"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.#", "1"),
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.reply_to_email_address", "foo.bar@baz"),
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.reply_to_email_address", replyTo),
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.email_sending_account", "DEVELOPER"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks we need a slight more involved tests to handle this check as the tests require a verified SES email ARN in order to work as expected. Trying replacing the existing test TestAccAWSCognitoUserPool_withEmailConfiguration with the following updated test case.

  func TestAccAWSCognitoUserPool_EmailConfiguration(t *testing.T) {                                                                                                                                                                                                                                   
    name := acctest.RandString(5)                                                                                                                                                                                                                                                                     
    replyTo := fmt.Sprintf("tf-acc-reply-%[email protected]", name)                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                      
    sourceARN, ok := os.LookupEnv("TEST_AWS_SES_VERIFIED_EMAIL_ARN")                                                                                                                                                                                                                                      
    if !ok {                                                                                                                                                                                                                                                                                          
      t.Skip("'TEST_AWS_SES_VERIFIED_EMAIL_ARN' not set, skipping test.")                                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                      
    resource.ParallelTest(t, resource.TestCase{                                                                                                                                                                                                                                                       
      PreCheck:     func() { testAccPreCheck(t) },                                                                                                                                                                                                                                                    
      Providers:    testAccProviders,                                                                                                                                                                                                                                                                 
      CheckDestroy: testAccCheckAWSCognitoUserPoolDestroy,                                                                                                                                                                                                                                            
      Steps: []resource.TestStep{                                                                                                                                                                                                                                                                     
        {                                                                                                                                                                                                                                                                                             
          Config: testAccAWSCognitoUserPoolConfig_basic(name),                                                                                                                                                                                                                                        
          Check: resource.ComposeAggregateTestCheckFunc(                                                                                                                                                                                                                                              
            testAccCheckAWSCognitoUserPoolExists("aws_cognito_user_pool.pool"),                                                                                                                                                                                                                       
          ),                                                                                                                                                                                                                                                                                          
        },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
        {                                                                                                                                                                                                                                                                                             
          Config: testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name, replyTo, sourceARN, "DEVELOPER"),                                                                                                                                                                                      
          Check: resource.ComposeAggregateTestCheckFunc(                                                                                                                                                                                                                                              
            resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.#", "1"),                                                                                                                                                                                               
            resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.reply_to_email_address", replyTo),                                                                                                                                                                    
            resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.email_sending_account", "DEVELOPER"),                                                                                                                                                                 
          ),                                                                                                                                                                                                                                                                                          
        },                                                                                                                                                                                                                                                                                            
      },                                                                                                                                                                                                                                                                                              
    })                                                                                                                                                                                                                                                                                                
  }                                                                                                                                                                                                                                                                                                   

resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.0.source_arn", sourceARN),
),
},
},
Expand Down Expand Up @@ -857,16 +868,18 @@ resource "aws_cognito_user_pool" "pool" {
`, name)
}

func testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name string) string {
func testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name, email, arn, account string) string {
return fmt.Sprintf(`
resource "aws_cognito_user_pool" "pool" {
name = "terraform-test-pool-%s"
name = "terraform-test-pool-%[1]s"

email_configuration {
reply_to_email_address = "foo.bar@baz"
}
}
`, name)

email_configuration {
reply_to_email_address = %[2]q
source_arn = %[3]q
email_sending_account = %[4]q
}
}`, name, email, arn, account)
}

func testAccAWSCognitoUserPoolConfig_withSmsConfiguration(name string) string {
Expand Down
4 changes: 4 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,10 @@ func flattenCognitoUserPoolEmailConfiguration(s *cognitoidentityprovider.EmailCo
m["source_arn"] = *s.SourceArn
}

if s.EmailSendingAccount != nil {
m["email_sending_account"] = *s.EmailSendingAccount
}

if len(m) > 0 {
return []map[string]interface{}{m}
}
Expand Down
10 changes: 0 additions & 10 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,16 +1705,6 @@ func validateCognitoUserPoolInviteTemplateSmsMessage(v interface{}, k string) (w
return
}

func validateCognitoUserPoolReplyEmailAddress(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if !regexp.MustCompile(`[\p{L}\p{M}\p{S}\p{N}\p{P}]+@[\p{L}\p{M}\p{S}\p{N}\p{P}]+`).MatchString(value) {
errors = append(errors, fmt.Errorf(
`%q must satisfy regular expression pattern: [\p{L}\p{M}\p{S}\p{N}\p{P}]+@[\p{L}\p{M}\p{S}\p{N}\p{P}]+`, k))
}
return
}

func validateCognitoUserPoolSchemaName(v interface{}, k string) (ws []string, es []error) {
value := v.(string)
if len(value) < 1 {
Expand Down
26 changes: 0 additions & 26 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2465,32 +2465,6 @@ func TestValidateKmsKey(t *testing.T) {
}
}

func TestValidateCognitoUserPoolReplyEmailAddress(t *testing.T) {
validTypes := []string{
"[email protected]",
"foo@bar",
"foo [email protected]",
"[email protected]",
}
for _, v := range validTypes {
_, errors := validateCognitoUserPoolReplyEmailAddress(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid Cognito User Pool Reply Email Address: %q", v, errors)
}
}

invalidTypes := []string{
"foo",
"@bar.baz",
}
for _, v := range invalidTypes {
_, errors := validateCognitoUserPoolReplyEmailAddress(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid Cognito User Pool Reply Email Address", v)
}
}
}

func TestResourceAWSElastiCacheReplicationGroupAuthTokenValidation(t *testing.T) {
cases := []struct {
Value string
Expand Down
1 change: 1 addition & 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:

* `reply_to_email_address` (Optional) - The REPLY-TO email address.
* `source_arn` (Optional) - The ARN of the email source.
* `email_sending_account` (Optional) - Instruct Cognito to either use its built-in functional or Amazon SES to send out emails.

#### Lambda Configuration

Expand Down