-
Notifications
You must be signed in to change notification settings - Fork 630
/
resource_cloudflare_access_mutual_tls_certificate_test.go
201 lines (174 loc) · 7.37 KB
/
resource_cloudflare_access_mutual_tls_certificate_test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package sdkv2provider
import (
"context"
"fmt"
"os"
"testing"
"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func init() {
resource.AddTestSweepers("cloudflare_zero_trust_access_mtls_certificate", &resource.Sweeper{
Name: "cloudflare_zero_trust_access_mtls_certificate",
F: testSweepCloudflareAccessMutualTLSCertificate,
})
}
func testSweepCloudflareAccessMutualTLSCertificate(r string) error {
ctx := context.Background()
client, clientErr := sharedClient()
if clientErr != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to create Cloudflare client: %s", clientErr))
}
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
accountCerts, _, err := client.ListAccessMutualTLSCertificates(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.ListAccessMutualTLSCertificatesParams{})
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to fetch Cloudflare Access Mutual TLS certificates: %s", err))
}
for _, cert := range accountCerts {
err := client.DeleteAccessMutualTLSCertificate(context.Background(), cloudflare.AccountIdentifier(accountID), cert.ID)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to delete Cloudflare Access Mutual TLS certificate (%s) in account ID: %s", cert.ID, accountID))
}
}
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
zoneCerts, _, err := client.ListAccessMutualTLSCertificates(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListAccessMutualTLSCertificatesParams{})
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to fetch Cloudflare Access Mutual TLS certificates: %s", err))
}
for _, cert := range zoneCerts {
err := client.DeleteAccessMutualTLSCertificate(context.Background(), cloudflare.ZoneIdentifier(zoneID), cert.ID)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to delete Cloudflare Access Mutual TLS certificate (%s) in zone ID: %s", cert.ID, zoneID))
}
}
return nil
}
func TestAccCloudflareAccessMutualTLSBasic(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
t.Setenv("CLOUDFLARE_API_TOKEN", "")
}
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_zero_trust_access_mtls_certificate.%s", rnd)
cert := os.Getenv("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE")
domain := os.Getenv("CLOUDFLARE_DOMAIN")
if v := os.Getenv("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE"); v == "" {
t.Fatal("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE must be set for this acceptance test")
}
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflareAccessMutualTLSCertificateDestroy,
Steps: []resource.TestStep{
{
Config: testAccessMutualTLSCertificateConfigBasic(rnd, cloudflare.AccountIdentifier(accountID), cert, domain),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttrSet(name, "certificate"),
resource.TestCheckResourceAttr(name, "associated_hostnames.0", domain),
),
},
{
Config: testAccessMutualTLSCertificateUpdated(rnd, cloudflare.AccountIdentifier(accountID), cert),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttrSet(name, "certificate"),
resource.TestCheckResourceAttr(name, "associated_hostnames.#", "0"),
),
},
},
})
}
func TestAccCloudflareAccessMutualTLSBasicWithZoneID(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
t.Setenv("CLOUDFLARE_API_TOKEN", "")
}
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_zero_trust_access_mtls_certificate.%s", rnd)
cert := os.Getenv("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE")
domain := os.Getenv("CLOUDFLARE_DOMAIN")
if v := os.Getenv("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE"); v == "" {
t.Fatal("CLOUDFLARE_MUTUAL_TLS_CERTIFICATE must be set for this acceptance test")
}
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflareAccessMutualTLSCertificateDestroy,
Steps: []resource.TestStep{
{
Config: testAccessMutualTLSCertificateConfigBasic(rnd, cloudflare.ZoneIdentifier(zoneID), cert, domain),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, consts.ZoneIDSchemaKey, zoneID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttrSet(name, "certificate"),
resource.TestCheckResourceAttr(name, "associated_hostnames.0", domain),
),
},
{
Config: testAccessMutualTLSCertificateUpdated(rnd, cloudflare.ZoneIdentifier(zoneID), cert),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, consts.ZoneIDSchemaKey, zoneID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttrSet(name, "certificate"),
resource.TestCheckResourceAttr(name, "associated_hostnames.#", "0"),
),
},
},
})
}
func testAccCheckCloudflareAccessMutualTLSCertificateDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudflare.API)
for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudflare_zero_trust_access_mtls_certificate" {
continue
}
if rs.Primary.Attributes[consts.ZoneIDSchemaKey] != "" {
_, err := client.GetAccessMutualTLSCertificate(context.Background(), cloudflare.ZoneIdentifier(rs.Primary.Attributes[consts.ZoneIDSchemaKey]), rs.Primary.ID)
if err == nil {
return fmt.Errorf("AccessMutualTLSCertificate still exists")
}
}
if rs.Primary.Attributes[consts.AccountIDSchemaKey] != "" {
_, err := client.GetAccessMutualTLSCertificate(context.Background(), cloudflare.AccountIdentifier(rs.Primary.Attributes[consts.AccountIDSchemaKey]), rs.Primary.ID)
if err == nil {
return fmt.Errorf("AccessMutualTLSCertificate still exists")
}
}
}
return nil
}
func testAccessMutualTLSCertificateConfigBasic(rnd string, identifier *cloudflare.ResourceContainer, cert, domain string) string {
return fmt.Sprintf(`
resource "cloudflare_zero_trust_access_mtls_certificate" "%[1]s" {
name = "%[1]s"
%[2]s_id = "%[3]s"
associated_hostnames = ["%[5]s"]
certificate = "%[4]s"
}
`, rnd, identifier.Type, identifier.Identifier, cert, domain)
}
func testAccessMutualTLSCertificateUpdated(rnd string, identifier *cloudflare.ResourceContainer, cert string) string {
return fmt.Sprintf(`
resource "cloudflare_zero_trust_access_mtls_certificate" "%[1]s" {
name = "%[1]s"
%[2]s_id = "%[3]s"
associated_hostnames = []
certificate = "%[4]s"
}
`, rnd, identifier.Type, identifier.Identifier, cert)
}