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_lb_listener_certificate: Prevent resource ID parsing error with IAM Server Certificate names containing underscores #17645

Merged
merged 2 commits into from
Feb 17, 2021
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
3 changes: 3 additions & 0 deletions .changelog/17645.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lb_listener_certificate: Prevent resource ID parsing error with IAM Server Certificate names containing underscores
```
2 changes: 1 addition & 1 deletion aws/internal/service/elbv2/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const listenerCertificateIDSeparator = "_"

func ListenerCertificateParseID(id string) (string, string, error) {
parts := strings.Split(id, listenerCertificateIDSeparator)
parts := strings.SplitN(id, listenerCertificateIDSeparator, 2)
if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
return parts[0], parts[1], nil
}
Expand Down
100 changes: 100 additions & 0 deletions aws/resource_aws_lb_listener_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@ func TestAccAwsLbListenerCertificate_basic(t *testing.T) {
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/17639
func TestAccAwsLbListenerCertificate_CertificateArn_Underscores(t *testing.T) {
key := tlsRsaPrivateKeyPem(2048)
certificate := tlsRsaX509SelfSignedCertificatePem(key, "example.com")
iamServerCertificateResourceName := "aws_iam_server_certificate.test"
lbListenerResourceName := "aws_lb_listener.test"
resourceName := "aws_lb_listener_certificate.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsLbListenerCertificateDestroy,
Steps: []resource.TestStep{
{
Config: testAccLbListenerCertificateConfigCertificateArnUnderscores(rName, key, certificate),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLbListenerCertificateExists(resourceName),
resource.TestCheckResourceAttrPair(resourceName, "certificate_arn", iamServerCertificateResourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "listener_arn", lbListenerResourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAwsLbListenerCertificate_multiple(t *testing.T) {
keys := make([]string, 4)
certificates := make([]string, 4)
Expand Down Expand Up @@ -267,6 +298,75 @@ resource "aws_lb_listener_certificate" "test" {
`
}

func testAccLbListenerCertificateConfigCertificateArnUnderscores(rName, key, certificate string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"

filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "terraform-testacc-lb-listener-certificate"
}
}

resource "aws_subnet" "test" {
count = 2

availability_zone = data.aws_availability_zones.available.names[count.index]
cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, count.index)
vpc_id = aws_vpc.test.id

tags = {
Name = "tf-acc-lb-listener-certificate-${count.index}"
}
}

resource "aws_lb_target_group" "test" {
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.test.id
}

resource "aws_lb" "test" {
internal = true
name = %[1]q
subnets = aws_subnet.test[*].id
}

resource "aws_iam_server_certificate" "test" {
name = replace("%[1]s", "-", "_")
certificate_body = "%[2]s"
private_key = "%[3]s"
}

resource "aws_lb_listener" "test" {
load_balancer_arn = aws_lb.test.arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = aws_iam_server_certificate.test.arn

default_action {
target_group_arn = aws_lb_target_group.test.arn
type = "forward"
}
}

resource "aws_lb_listener_certificate" "test" {
certificate_arn = aws_iam_server_certificate.test.arn
listener_arn = aws_lb_listener.test.arn
}
`, rName, tlsPemEscapeNewlines(certificate), tlsPemEscapeNewlines(key))
}

func testAccLbListenerCertificateConfigMultiple(rName string, keys, certificates []string) string {
return testAccLbListenerCertificateConfigLbListenerBase(rName, keys[0], certificates[0]) + fmt.Sprintf(`
resource "aws_lb_listener_certificate" "default" {
Expand Down