/
data_source_aws_acmpca_certificate_authority.go
176 lines (152 loc) · 5.4 KB
/
data_source_aws_acmpca_certificate_authority.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/acmpca"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsAcmpcaCertificateAuthority() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsAcmpcaCertificateAuthorityRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
},
"certificate": {
Type: schema.TypeString,
Computed: true,
},
"certificate_chain": {
Type: schema.TypeString,
Computed: true,
},
"certificate_signing_request": {
Type: schema.TypeString,
Computed: true,
},
"not_after": {
Type: schema.TypeString,
Computed: true,
},
"not_before": {
Type: schema.TypeString,
Computed: true,
},
// https://docs.aws.amazon.com/acm-pca/latest/APIReference/API_RevocationConfiguration.html
"revocation_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// https://docs.aws.amazon.com/acm-pca/latest/APIReference/API_CrlConfiguration.html
"crl_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"custom_cname": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"expiration_in_days": {
Type: schema.TypeInt,
Computed: true,
},
"s3_bucket_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"serial": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
"type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsAcmpcaCertificateAuthorityRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).acmpcaconn
certificateAuthorityArn := d.Get("arn").(string)
describeCertificateAuthorityInput := &acmpca.DescribeCertificateAuthorityInput{
CertificateAuthorityArn: aws.String(certificateAuthorityArn),
}
log.Printf("[DEBUG] Reading ACMPCA Certificate Authority: %s", describeCertificateAuthorityInput)
describeCertificateAuthorityOutput, err := conn.DescribeCertificateAuthority(describeCertificateAuthorityInput)
if err != nil {
return fmt.Errorf("error reading ACMPCA Certificate Authority: %s", err)
}
if describeCertificateAuthorityOutput.CertificateAuthority == nil {
return fmt.Errorf("error reading ACMPCA Certificate Authority: not found")
}
certificateAuthority := describeCertificateAuthorityOutput.CertificateAuthority
d.Set("arn", certificateAuthority.Arn)
d.Set("not_after", certificateAuthority.NotAfter)
d.Set("not_before", certificateAuthority.NotBefore)
if err := d.Set("revocation_configuration", flattenAcmpcaRevocationConfiguration(certificateAuthority.RevocationConfiguration)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
d.Set("serial", certificateAuthority.Serial)
d.Set("status", certificateAuthority.Status)
d.Set("type", certificateAuthority.Type)
getCertificateAuthorityCertificateInput := &acmpca.GetCertificateAuthorityCertificateInput{
CertificateAuthorityArn: aws.String(certificateAuthorityArn),
}
log.Printf("[DEBUG] Reading ACMPCA Certificate Authority Certificate: %s", getCertificateAuthorityCertificateInput)
getCertificateAuthorityCertificateOutput, err := conn.GetCertificateAuthorityCertificate(getCertificateAuthorityCertificateInput)
if err != nil {
// Returned when in PENDING_CERTIFICATE status
// InvalidStateException: The certificate authority XXXXX is not in the correct state to have a certificate signing request.
if !isAWSErr(err, acmpca.ErrCodeInvalidStateException, "") {
return fmt.Errorf("error reading ACMPCA Certificate Authority Certificate: %s", err)
}
}
d.Set("certificate", "")
d.Set("certificate_chain", "")
if getCertificateAuthorityCertificateOutput != nil {
d.Set("certificate", getCertificateAuthorityCertificateOutput.Certificate)
d.Set("certificate_chain", getCertificateAuthorityCertificateOutput.CertificateChain)
}
getCertificateAuthorityCsrInput := &acmpca.GetCertificateAuthorityCsrInput{
CertificateAuthorityArn: aws.String(certificateAuthorityArn),
}
log.Printf("[DEBUG] Reading ACMPCA Certificate Authority Certificate Signing Request: %s", getCertificateAuthorityCsrInput)
getCertificateAuthorityCsrOutput, err := conn.GetCertificateAuthorityCsr(getCertificateAuthorityCsrInput)
if err != nil {
return fmt.Errorf("error reading ACMPCA Certificate Authority Certificate Signing Request: %s", err)
}
d.Set("certificate_signing_request", "")
if getCertificateAuthorityCsrOutput != nil {
d.Set("certificate_signing_request", getCertificateAuthorityCsrOutput.Csr)
}
tags, err := listAcmpcaTags(conn, certificateAuthorityArn)
if err != nil {
return fmt.Errorf("error reading ACMPCA Certificate Authority %q tags: %s", certificateAuthorityArn, err)
}
if err := d.Set("tags", tagsToMapACMPCA(tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
d.SetId(certificateAuthorityArn)
return nil
}