This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
forked from cloudflare/terraform-provider-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_cloudflare_access_identity_provider_test.go
214 lines (197 loc) · 7.09 KB
/
resource_cloudflare_access_identity_provider_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
202
203
204
205
206
207
208
209
210
211
212
213
214
package cloudflare
import (
"fmt"
"log"
"os"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)
func init() {
resource.AddTestSweepers("cloudflare_access_identity_provider", &resource.Sweeper{
Name: "cloudflare_access_identity_provider",
F: testSweepCloudflareAccessIdentityProviders,
})
}
func testSweepCloudflareAccessIdentityProviders(r string) error {
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
client, clientErr := sharedClient()
if clientErr != nil {
log.Printf("[ERROR] Failed to create Cloudflare client: %s", clientErr)
}
accessIDPs, accessIDPsErr := client.AccessIdentityProviders(accountID)
if accessIDPsErr != nil {
log.Printf("[ERROR] Failed to fetch Access Identity Providers: %s", accessIDPsErr)
}
if len(accessIDPs) == 0 {
log.Print("[DEBUG] No Access Identity Providers to sweep")
return nil
}
for _, idp := range accessIDPs {
log.Printf("[INFO] Deleting Access Identity Provider ID: %s", idp.ID)
_, err := client.DeleteAccessIdentityProvider(accountID, idp.ID)
if err != nil {
log.Printf("[ERROR] Failed to delete Access Identity Provider (%s): %s", idp.ID, err)
}
}
return nil
}
func TestAccCloudflareAccessIdentityProviderOneTimePin(t *testing.T) {
t.Parallel()
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
rnd := generateRandomResourceName()
resourceName := "cloudflare_access_identity_provider." + rnd
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareAccessIdentityProviderOneTimePin(accountID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_id", accountID),
resource.TestCheckResourceAttr(resourceName, "name", rnd),
resource.TestCheckResourceAttr(resourceName, "type", "onetimepin"),
),
},
},
})
}
func TestAccCloudflareAccessIdentityProviderOAuth(t *testing.T) {
t.Parallel()
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
rnd := generateRandomResourceName()
resourceName := "cloudflare_access_identity_provider." + rnd
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareAccessIdentityProviderOAuth(accountID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_id", accountID),
resource.TestCheckResourceAttr(resourceName, "name", rnd),
resource.TestCheckResourceAttr(resourceName, "type", "github"),
resource.TestCheckResourceAttr(resourceName, "config.0.client_id", "test"),
resource.TestCheckResourceAttrSet(resourceName, "config.0.client_secret"),
),
},
},
})
}
func TestAccCloudflareAccessIdentityProviderOAuthWithUpdate(t *testing.T) {
t.Parallel()
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
rnd := generateRandomResourceName()
resourceName := "cloudflare_access_identity_provider." + rnd
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareAccessIdentityProviderOAuth(accountID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_id", accountID),
resource.TestCheckResourceAttr(resourceName, "name", rnd),
resource.TestCheckResourceAttr(resourceName, "type", "github"),
resource.TestCheckResourceAttr(resourceName, "config.0.client_id", "test"),
resource.TestCheckResourceAttrSet(resourceName, "config.0.client_secret"),
),
},
{
Config: testAccCheckCloudflareAccessIdentityProviderOAuthUpdatedName(accountID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_id", accountID),
resource.TestCheckResourceAttr(resourceName, "name", rnd+"-updated"),
resource.TestCheckResourceAttr(resourceName, "type", "github"),
resource.TestCheckResourceAttr(resourceName, "config.0.client_id", "test"),
resource.TestCheckResourceAttrSet(resourceName, "config.0.client_secret"),
),
},
},
})
}
func TestAccCloudflareAccessIdentityProviderSAML(t *testing.T) {
t.Parallel()
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
rnd := generateRandomResourceName()
resourceName := "cloudflare_access_identity_provider." + rnd
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareAccessIdentityProviderSAML(accountID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_id", accountID),
resource.TestCheckResourceAttr(resourceName, "name", rnd),
resource.TestCheckResourceAttr(resourceName, "type", "saml"),
resource.TestCheckResourceAttr(resourceName, "config.0.issuer_url", "jumpcloud"),
resource.TestCheckResourceAttr(resourceName, "config.0.sso_target_url", "https://sso.myexample.jumpcloud.com/saml2/cloudflareaccess"),
resource.TestCheckResourceAttr(resourceName, "config.0.sign_request", "false"),
resource.TestCheckResourceAttr(resourceName, "config.0.attributes.#", "2"),
resource.TestCheckResourceAttr(resourceName, "config.0.attributes.0", "email"),
resource.TestCheckResourceAttr(resourceName, "config.0.attributes.1", "username"),
resource.TestCheckResourceAttrSet(resourceName, "config.0.idp_public_cert"),
),
},
},
})
}
func testAccCheckCloudflareAccessIdentityProviderOneTimePin(accountID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_access_identity_provider" "%[2]s" {
account_id = "%[1]s"
name = "%[2]s"
type = "onetimepin"
}`, accountID, name)
}
func testAccCheckCloudflareAccessIdentityProviderOAuth(accountID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_access_identity_provider" "%[2]s" {
account_id = "%[1]s"
name = "%[2]s"
type = "github"
config {
client_id = "test"
client_secret = "secret"
}
}`, accountID, name)
}
func testAccCheckCloudflareAccessIdentityProviderOAuthUpdatedName(accountID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_access_identity_provider" "%[2]s" {
account_id = "%[1]s"
name = "%[2]s-updated"
type = "github"
config {
client_id = "test"
client_secret = "secret"
}
}`, accountID, name)
}
func testAccCheckCloudflareAccessIdentityProviderSAML(accountID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_access_identity_provider" "%[2]s" {
account_id = "%[1]s"
name = "%[2]s"
type = "saml"
config {
issuer_url = "jumpcloud"
sso_target_url = "https://sso.myexample.jumpcloud.com/saml2/cloudflareaccess"
attributes = [ "email", "username" ]
sign_request = false
idp_public_cert = "MIIDpDCCAoygAwIBAgIGAV2ka+55MA0GCSqGSIb3DQEBCwUAMIGSMQswCQYDVQQGEwJVUzETMBEG\nA1UEC…..GF/Q2/MHadws97cZg\nuTnQyuOqPuHbnN83d/2l1NSYKCbHt24o"
}
}`, accountID, name)
}