Skip to content

Commit

Permalink
Merge pull request #39952 from maxim-anetac/Bug26868
Browse files Browse the repository at this point in the history
Bug26868: fix RAM CSR retrieval
  • Loading branch information
ewbankkit authored Dec 12, 2024
2 parents 97d1606 + 0c4de35 commit dde1c87
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/39952.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/aws_acmpca_certificate_authority: Ignore `AccessDeniedException: ... is not authorized to perform: acm-pca:GetCertificateAuthorityCsr on resource: ...` errors for RAM-shared CAs
```
13 changes: 10 additions & 3 deletions internal/service/acmpca/certificate_authority_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/acmpca"
"github.com/aws/aws-sdk-go-v2/service/acmpca/types"
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -175,13 +176,19 @@ func dataSourceCertificateAuthorityRead(ctx context.Context, d *schema.ResourceD
d.Set(names.AttrCertificateChain, outputGCACert.CertificateChain)
}

// Attempt to get the CSR (if permitted).
outputGCACsr, err := conn.GetCertificateAuthorityCsr(ctx, &acmpca.GetCertificateAuthorityCsrInput{
CertificateAuthorityArn: aws.String(certificateAuthorityARN),
})

// Returned when in PENDING_CERTIFICATE status
// InvalidStateException: The certificate authority XXXXX is not in the correct state to have a certificate signing request.
if err != nil && !errs.IsA[*types.InvalidStateException](err) {
switch {
case tfawserr.ErrCodeEquals(err, "AccessDeniedException"):
// Handle permission issues gracefully for Resource Access Manager shared CAs.
// arn:aws:ram::aws:permission/AWSRAMDefaultPermissionCertificateAuthority does not include acm-pca:GetCertificateAuthorityCsr.
case errs.IsA[*types.InvalidStateException](err):
// Returned when in PENDING_CERTIFICATE status
// InvalidStateException: The certificate authority XXXXX is not in the correct state to have a certificate signing request.
case err != nil:
return sdkdiag.AppendErrorf(diags, "reading ACM PCA Certificate Authority (%s) Certificate Signing Request: %s", d.Id(), err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestAccACMPCACertificateAuthorityDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
resourceName := "aws_acmpca_certificate_authority.test"
datasourceName := "data.aws_acmpca_certificate_authority.test"

commonName := acctest.RandomDomainName()

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -56,18 +55,13 @@ func TestAccACMPCACertificateAuthorityDataSource_s3ObjectACL(t *testing.T) {
ctx := acctest.Context(t)
resourceName := "aws_acmpca_certificate_authority.test"
datasourceName := "data.aws_acmpca_certificate_authority.test"

commonName := acctest.RandomDomainName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.ACMPCAServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCertificateAuthorityDataSourceConfig_nonExistent,
ExpectError: regexache.MustCompile(`(AccessDeniedException|ResourceNotFoundException)`),
},
{
Config: testAccCertificateAuthorityDataSourceConfig_s3ObjectACLARN(commonName),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down

0 comments on commit dde1c87

Please sign in to comment.