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

resource/aws_acm_certificate: Automatically trim trailing period from domain_name and subject_alternative_names #6844

Merged
merged 1 commit into from
Dec 13, 2018
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
27 changes: 21 additions & 6 deletions aws/resource_aws_acm_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -26,12 +27,24 @@ func resourceAwsAcmCertificate() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
// AWS Provider 1.42.0+ aws_route53_zone references may contain a
// trailing period, which generates an ACM API error
return strings.TrimSuffix(v.(string), ".")
},
},
"subject_alternative_names": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
StateFunc: func(v interface{}) string {
// AWS Provider 1.42.0+ aws_route53_zone references may contain a
// trailing period, which generates an ACM API error
return strings.TrimSuffix(v.(string), ".")
},
},
},
"validation_method": {
Type: schema.TypeString,
Expand Down Expand Up @@ -79,14 +92,16 @@ func resourceAwsAcmCertificate() *schema.Resource {
func resourceAwsAcmCertificateCreate(d *schema.ResourceData, meta interface{}) error {
acmconn := meta.(*AWSClient).acmconn
params := &acm.RequestCertificateInput{
DomainName: aws.String(d.Get("domain_name").(string)),
DomainName: aws.String(strings.TrimSuffix(d.Get("domain_name").(string), ".")),
ValidationMethod: aws.String(d.Get("validation_method").(string)),
}

sans, ok := d.GetOk("subject_alternative_names")
if ok {
sanStrings := sans.([]interface{})
params.SubjectAlternativeNames = expandStringList(sanStrings)
if sans, ok := d.GetOk("subject_alternative_names"); ok {
subjectAlternativeNames := make([]*string, len(sans.([]interface{})))
for i, sanRaw := range sans.([]interface{}) {
subjectAlternativeNames[i] = aws.String(strings.TrimSuffix(sanRaw.(string), "."))
}
params.SubjectAlternativeNames = subjectAlternativeNames
}

log.Printf("[DEBUG] ACM Certificate Request: %#v", params)
Expand Down
78 changes: 78 additions & 0 deletions aws/resource_aws_acm_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"strconv"
"strings"
"testing"

"os"
Expand Down Expand Up @@ -132,6 +133,40 @@ func TestAccAWSAcmCertificate_root(t *testing.T) {
})
}

func TestAccAWSAcmCertificate_root_TrailingPeriod(t *testing.T) {
rootDomain := testAccAwsAcmCertificateDomainFromEnv(t)
domain := fmt.Sprintf("%s.", rootDomain)
resourceName := "aws_acm_certificate.cert"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAcmCertificateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAcmCertificateConfig(domain, acm.ValidationMethodDns),
Check: resource.ComposeTestCheckFunc(
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "acm", regexp.MustCompile(`certificate/.+`)),
resource.TestCheckResourceAttr(resourceName, "domain_name", strings.TrimSuffix(domain, ".")),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.0.domain_name", strings.TrimSuffix(domain, ".")),
resource.TestCheckResourceAttrSet(resourceName, "domain_validation_options.0.resource_record_name"),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.0.resource_record_type", "CNAME"),
resource.TestCheckResourceAttrSet(resourceName, "domain_validation_options.0.resource_record_value"),
resource.TestCheckResourceAttr(resourceName, "subject_alternative_names.#", "0"),
resource.TestCheckResourceAttr(resourceName, "validation_emails.#", "0"),
resource.TestCheckResourceAttr(resourceName, "validation_method", acm.ValidationMethodDns),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSAcmCertificate_rootAndWildcardSan(t *testing.T) {
rootDomain := testAccAwsAcmCertificateDomainFromEnv(t)
wildcardDomain := fmt.Sprintf("*.%s", rootDomain)
Expand Down Expand Up @@ -260,6 +295,49 @@ func TestAccAWSAcmCertificate_san_multiple(t *testing.T) {
})
}

func TestAccAWSAcmCertificate_san_TrailingPeriod(t *testing.T) {
rootDomain := testAccAwsAcmCertificateDomainFromEnv(t)

rInt1 := acctest.RandInt()

domain := fmt.Sprintf("tf-acc-%d.%s", rInt1, rootDomain)
sanDomain := fmt.Sprintf("tf-acc-%d-san.%s.", rInt1, rootDomain)
resourceName := "aws_acm_certificate.cert"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAcmCertificateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAcmCertificateConfig_subjectAlternativeNames(domain, strconv.Quote(sanDomain), acm.ValidationMethodDns),
Check: resource.ComposeTestCheckFunc(
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "acm", regexp.MustCompile(`certificate/.+`)),
resource.TestCheckResourceAttr(resourceName, "domain_name", domain),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.#", "2"),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.0.domain_name", domain),
resource.TestCheckResourceAttrSet(resourceName, "domain_validation_options.0.resource_record_name"),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.0.resource_record_type", "CNAME"),
resource.TestCheckResourceAttrSet(resourceName, "domain_validation_options.0.resource_record_value"),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.1.domain_name", strings.TrimSuffix(sanDomain, ".")),
resource.TestCheckResourceAttrSet(resourceName, "domain_validation_options.1.resource_record_name"),
resource.TestCheckResourceAttr(resourceName, "domain_validation_options.1.resource_record_type", "CNAME"),
resource.TestCheckResourceAttrSet(resourceName, "domain_validation_options.1.resource_record_value"),
resource.TestCheckResourceAttr(resourceName, "subject_alternative_names.#", "1"),
resource.TestCheckResourceAttr(resourceName, "subject_alternative_names.0", strings.TrimSuffix(sanDomain, ".")),
resource.TestCheckResourceAttr(resourceName, "validation_emails.#", "0"),
resource.TestCheckResourceAttr(resourceName, "validation_method", acm.ValidationMethodDns),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSAcmCertificate_wildcard(t *testing.T) {
rootDomain := testAccAwsAcmCertificateDomainFromEnv(t)
wildcardDomain := fmt.Sprintf("*.%s", rootDomain)
Expand Down