Skip to content

Commit

Permalink
Merge pull request #36338 from mattburgess/acmpca-awssdkv2-migration
Browse files Browse the repository at this point in the history
acmpca: Migrate to AWS SDK v2
  • Loading branch information
ewbankkit authored Mar 28, 2024
2 parents c5dcef9 + 9b8d173 commit 6b3949f
Show file tree
Hide file tree
Showing 37 changed files with 801 additions and 806 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ require (

require github.com/aws/aws-sdk-go-v2/service/costandusagereportservice v1.23.3

require github.com/aws/aws-sdk-go-v2/service/acmpca v1.29.3

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/aws/aws-sdk-go-v2/service/account v1.16.3 h1:naZ+3ZZa/j5c7N25vCII8ZHW
github.com/aws/aws-sdk-go-v2/service/account v1.16.3/go.mod h1:QBT5/WHp07EA3HgT/Wg3qVpL9baYpqaLl6XWSH18ntk=
github.com/aws/aws-sdk-go-v2/service/acm v1.25.3 h1:AH94I88C4CPMp6YOTncdshON5hsyBDWUAM/FBAHHkco=
github.com/aws/aws-sdk-go-v2/service/acm v1.25.3/go.mod h1:hFOyylMVlIkhN7YLhv64oBZzVTJoi8bqhJZfkDVlZww=
github.com/aws/aws-sdk-go-v2/service/acmpca v1.29.3 h1:KkkUQXWi8ddsVrmX04FcAOBz/R8dmt+7MNsUoB7XImU=
github.com/aws/aws-sdk-go-v2/service/acmpca v1.29.3/go.mod h1:EfOq2JRfHuaQY21aVXTGYQ7pjF0zW9+xD+u36e1nqTU=
github.com/aws/aws-sdk-go-v2/service/amp v1.25.3 h1:GkOnnt0ItVXnvo7xt1/+XJkzB6q1NAa80cLn7KkQd50=
github.com/aws/aws-sdk-go-v2/service/amp v1.25.3/go.mod h1:FUrdgK1jyv01Q1DjpW6MWjA/ZEuTRphMLdkqtLTBXq0=
github.com/aws/aws-sdk-go-v2/service/appconfig v1.29.1 h1:AtkGG+t4U+Mb7sR2lQj/uvDlbdA7GVAIv8o/AAnS+vk=
Expand Down
69 changes: 39 additions & 30 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

"github.com/YakDriver/regexache"
accounttypes "github.com/aws/aws-sdk-go-v2/service/account/types"
"github.com/aws/aws-sdk-go-v2/service/acmpca"
acmpcatypes "github.com/aws/aws-sdk-go-v2/service/acmpca/types"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/iam"
awstypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
Expand All @@ -30,7 +32,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/acmpca"
"github.com/aws/aws-sdk-go/service/directoryservice"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/outposts"
Expand Down Expand Up @@ -83,6 +84,8 @@ const (
ProviderNameThird = "awsthird"

ResourcePrefix = "tf-acc-test"

CertificateIssueTimeout = 5 * time.Minute
)

const RFC3339RegexPattern = `^[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?([Zz]|([+-]([01][0-9]|2[0-3]):[0-5][0-9]))$`
Expand Down Expand Up @@ -1933,32 +1936,32 @@ func ACMCertificateRandomSubDomain(rootDomain string) string {
rootDomain)
}

func CheckACMPCACertificateAuthorityActivateRootCA(ctx context.Context, certificateAuthority *acmpca.CertificateAuthority) resource.TestCheckFunc {
func CheckACMPCACertificateAuthorityActivateRootCA(ctx context.Context, certificateAuthority *acmpcatypes.CertificateAuthority) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := Provider.Meta().(*conns.AWSClient).ACMPCAConn(ctx)
conn := Provider.Meta().(*conns.AWSClient).ACMPCAClient(ctx)

if v := aws.StringValue(certificateAuthority.Type); v != acmpca.CertificateAuthorityTypeRoot {
if v := string(certificateAuthority.Type); v != string(acmpcatypes.CertificateAuthorityTypeRoot) {
return fmt.Errorf("attempting to activate ACM PCA %s Certificate Authority", v)
}

arn := aws.StringValue(certificateAuthority.Arn)

getCsrOutput, err := conn.GetCertificateAuthorityCsrWithContext(ctx, &acmpca.GetCertificateAuthorityCsrInput{
getCsrOutput, err := conn.GetCertificateAuthorityCsr(ctx, &acmpca.GetCertificateAuthorityCsrInput{
CertificateAuthorityArn: aws.String(arn),
})

if err != nil {
return fmt.Errorf("getting ACM PCA Certificate Authority (%s) CSR: %w", arn, err)
}

issueCertOutput, err := conn.IssueCertificateWithContext(ctx, &acmpca.IssueCertificateInput{
issueCertOutput, err := conn.IssueCertificate(ctx, &acmpca.IssueCertificateInput{
CertificateAuthorityArn: aws.String(arn),
Csr: []byte(aws.StringValue(getCsrOutput.Csr)),
IdempotencyToken: aws.String(id.UniqueId()),
SigningAlgorithm: certificateAuthority.CertificateAuthorityConfiguration.SigningAlgorithm,
TemplateArn: aws.String(fmt.Sprintf("arn:%s:acm-pca:::template/RootCACertificate/V1", Partition())),
Validity: &acmpca.Validity{
Type: aws.String(acmpca.ValidityPeriodTypeYears),
Validity: &acmpcatypes.Validity{
Type: acmpcatypes.ValidityPeriodTypeYears,
Value: aws.Int64(10),
},
})
Expand All @@ -1968,16 +1971,19 @@ func CheckACMPCACertificateAuthorityActivateRootCA(ctx context.Context, certific
}

// Wait for certificate status to become ISSUED.
err = conn.WaitUntilCertificateIssuedWithContext(ctx, &acmpca.GetCertificateInput{
waiter := acmpca.NewCertificateIssuedWaiter(conn)
params := &acmpca.GetCertificateInput{
CertificateAuthorityArn: aws.String(arn),
CertificateArn: issueCertOutput.CertificateArn,
})
}

err = waiter.Wait(ctx, params, CertificateIssueTimeout)

if err != nil {
return fmt.Errorf("waiting for ACM PCA Certificate Authority (%s) Root CA certificate to become ISSUED: %w", arn, err)
}

getCertOutput, err := conn.GetCertificateWithContext(ctx, &acmpca.GetCertificateInput{
getCertOutput, err := conn.GetCertificate(ctx, &acmpca.GetCertificateInput{
CertificateAuthorityArn: aws.String(arn),
CertificateArn: issueCertOutput.CertificateArn,
})
Expand All @@ -1986,7 +1992,7 @@ func CheckACMPCACertificateAuthorityActivateRootCA(ctx context.Context, certific
return fmt.Errorf("getting ACM PCA Certificate Authority (%s) issued Root CA certificate: %w", arn, err)
}

_, err = conn.ImportCertificateAuthorityCertificateWithContext(ctx, &acmpca.ImportCertificateAuthorityCertificateInput{
_, err = conn.ImportCertificateAuthorityCertificate(ctx, &acmpca.ImportCertificateAuthorityCertificateInput{
CertificateAuthorityArn: aws.String(arn),
Certificate: []byte(aws.StringValue(getCertOutput.Certificate)),
})
Expand All @@ -1999,17 +2005,17 @@ func CheckACMPCACertificateAuthorityActivateRootCA(ctx context.Context, certific
}
}

func CheckACMPCACertificateAuthorityActivateSubordinateCA(ctx context.Context, rootCertificateAuthority, certificateAuthority *acmpca.CertificateAuthority) resource.TestCheckFunc {
func CheckACMPCACertificateAuthorityActivateSubordinateCA(ctx context.Context, rootCertificateAuthority, certificateAuthority *acmpcatypes.CertificateAuthority) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := Provider.Meta().(*conns.AWSClient).ACMPCAConn(ctx)
conn := Provider.Meta().(*conns.AWSClient).ACMPCAClient(ctx)

if v := aws.StringValue(certificateAuthority.Type); v != acmpca.CertificateAuthorityTypeSubordinate {
if v := string(certificateAuthority.Type); v != string(acmpcatypes.CertificateAuthorityTypeSubordinate) {
return fmt.Errorf("attempting to activate ACM PCA %s Certificate Authority", v)
}

arn := aws.StringValue(certificateAuthority.Arn)

getCsrOutput, err := conn.GetCertificateAuthorityCsrWithContext(ctx, &acmpca.GetCertificateAuthorityCsrInput{
getCsrOutput, err := conn.GetCertificateAuthorityCsr(ctx, &acmpca.GetCertificateAuthorityCsrInput{
CertificateAuthorityArn: aws.String(arn),
})

Expand All @@ -2019,14 +2025,14 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(ctx context.Context, r

rootCertificateAuthorityArn := aws.StringValue(rootCertificateAuthority.Arn)

issueCertOutput, err := conn.IssueCertificateWithContext(ctx, &acmpca.IssueCertificateInput{
issueCertOutput, err := conn.IssueCertificate(ctx, &acmpca.IssueCertificateInput{
CertificateAuthorityArn: aws.String(rootCertificateAuthorityArn),
Csr: []byte(aws.StringValue(getCsrOutput.Csr)),
IdempotencyToken: aws.String(id.UniqueId()),
SigningAlgorithm: certificateAuthority.CertificateAuthorityConfiguration.SigningAlgorithm,
TemplateArn: aws.String(fmt.Sprintf("arn:%s:acm-pca:::template/SubordinateCACertificate_PathLen0/V1", Partition())),
Validity: &acmpca.Validity{
Type: aws.String(acmpca.ValidityPeriodTypeYears),
Validity: &acmpcatypes.Validity{
Type: acmpcatypes.ValidityPeriodTypeYears,
Value: aws.Int64(3),
},
})
Expand All @@ -2036,16 +2042,19 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(ctx context.Context, r
}

// Wait for certificate status to become ISSUED.
err = conn.WaitUntilCertificateIssuedWithContext(ctx, &acmpca.GetCertificateInput{
CertificateAuthorityArn: aws.String(rootCertificateAuthorityArn),
waiter := acmpca.NewCertificateIssuedWaiter(conn)
params := &acmpca.GetCertificateInput{
CertificateAuthorityArn: aws.String(arn),
CertificateArn: issueCertOutput.CertificateArn,
})
}

err = waiter.Wait(ctx, params, CertificateIssueTimeout)

if err != nil {
return fmt.Errorf("waiting for ACM PCA Certificate Authority (%s) Subordinate CA certificate to become ISSUED: %w", arn, err)
}

getCertOutput, err := conn.GetCertificateWithContext(ctx, &acmpca.GetCertificateInput{
getCertOutput, err := conn.GetCertificate(ctx, &acmpca.GetCertificateInput{
CertificateAuthorityArn: aws.String(rootCertificateAuthorityArn),
CertificateArn: issueCertOutput.CertificateArn,
})
Expand All @@ -2054,7 +2063,7 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(ctx context.Context, r
return fmt.Errorf("getting ACM PCA Certificate Authority (%s) issued Subordinate CA certificate: %w", arn, err)
}

_, err = conn.ImportCertificateAuthorityCertificateWithContext(ctx, &acmpca.ImportCertificateAuthorityCertificateInput{
_, err = conn.ImportCertificateAuthorityCertificate(ctx, &acmpca.ImportCertificateAuthorityCertificateInput{
CertificateAuthorityArn: aws.String(arn),
Certificate: []byte(aws.StringValue(getCertOutput.Certificate)),
CertificateChain: []byte(aws.StringValue(getCertOutput.CertificateChain)),
Expand All @@ -2068,20 +2077,20 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(ctx context.Context, r
}
}

func CheckACMPCACertificateAuthorityDisableCA(ctx context.Context, certificateAuthority *acmpca.CertificateAuthority) resource.TestCheckFunc {
func CheckACMPCACertificateAuthorityDisableCA(ctx context.Context, certificateAuthority *acmpcatypes.CertificateAuthority) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := Provider.Meta().(*conns.AWSClient).ACMPCAConn(ctx)
conn := Provider.Meta().(*conns.AWSClient).ACMPCAClient(ctx)

_, err := conn.UpdateCertificateAuthorityWithContext(ctx, &acmpca.UpdateCertificateAuthorityInput{
_, err := conn.UpdateCertificateAuthority(ctx, &acmpca.UpdateCertificateAuthorityInput{
CertificateAuthorityArn: certificateAuthority.Arn,
Status: aws.String(acmpca.CertificateAuthorityStatusDisabled),
Status: acmpcatypes.CertificateAuthorityStatusDisabled,
})

return err
}
}

func CheckACMPCACertificateAuthorityExists(ctx context.Context, n string, certificateAuthority *acmpca.CertificateAuthority) resource.TestCheckFunc {
func CheckACMPCACertificateAuthorityExists(ctx context.Context, n string, certificateAuthority *acmpcatypes.CertificateAuthority) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -2092,7 +2101,7 @@ func CheckACMPCACertificateAuthorityExists(ctx context.Context, n string, certif
return fmt.Errorf("no ACM PCA Certificate Authority ID is set")
}

conn := Provider.Meta().(*conns.AWSClient).ACMPCAConn(ctx)
conn := Provider.Meta().(*conns.AWSClient).ACMPCAClient(ctx)

output, err := tfacmpca.FindCertificateAuthorityByARN(ctx, conn, rs.Primary.ID)

Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/generate/tags/templates/v2/list_tags_body.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func {{ .ListTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifier

pages := {{ .TagPackage }}.New{{ .ListTagsOp }}Paginator(conn, input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
page, err := pages.NextPage(ctx, optFns...)

{{ if and ( .ParentNotFoundErrCode ) ( .ParentNotFoundErrMsg ) }}
if tfawserr.ErrMessageContains(err, "{{ .ParentNotFoundErrCode }}", "{{ .ParentNotFoundErrMsg }}") {
Expand Down
Loading

0 comments on commit 6b3949f

Please sign in to comment.