forked from cloudflare/terraform-provider-cloudflare
/
resource_cloudflare_access_policy.go
269 lines (230 loc) · 7.84 KB
/
resource_cloudflare_access_policy.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cloudflare
import (
"fmt"
"log"
"strings"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceCloudflareAccessPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareAccessPolicyCreate,
Read: resourceCloudflareAccessPolicyRead,
Update: resourceCloudflareAccessPolicyUpdate,
Delete: resourceCloudflareAccessPolicyDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudflareAccessPolicyImport,
},
Schema: map[string]*schema.Schema{
"application_id": {
Type: schema.TypeString,
Required: true,
},
"zone_id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"precedence": {
Type: schema.TypeInt,
Optional: true,
},
"decision": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"allow", "deny", "bypass"}, false),
},
"require": {
Type: schema.TypeList,
Optional: true,
Elem: policyOptionElement,
},
"exclude": {
Type: schema.TypeList,
Optional: true,
Elem: policyOptionElement,
},
"include": {
Type: schema.TypeList,
Required: true,
Elem: policyOptionElement,
},
},
}
}
// policyOptionElement is used by `require`, `exclude` and `include`
// attributes to build out the expected access conditions.
var policyOptionElement = &schema.Resource{
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"email_domain": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ip": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"everyone": {
Type: schema.TypeBool,
Optional: true,
},
},
}
func resourceCloudflareAccessPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
appID := d.Get("application_id").(string)
accessPolicy, err := client.AccessPolicy(zoneID, appID, d.Id())
if err != nil {
if strings.Contains(err.Error(), "HTTP status 404") {
log.Printf("[INFO] Access Policy %s no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error finding Access Policy %q: %s", d.Id(), err)
}
d.Set("name", accessPolicy.Name)
d.Set("decision", accessPolicy.Decision)
d.Set("precedence", accessPolicy.Precedence)
d.Set("require", accessPolicy.Require)
d.Set("exclude", accessPolicy.Exclude)
d.Set("include", accessPolicy.Include)
return nil
}
func resourceCloudflareAccessPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
appID := d.Get("application_id").(string)
zoneID := d.Get("zone_id").(string)
newAccessPolicy := cloudflare.AccessPolicy{
Name: d.Get("name").(string),
Precedence: d.Get("precedence").(int),
Decision: d.Get("decision").(string),
}
newAccessPolicy = appendConditionalAccessPolicyFields(newAccessPolicy, d)
log.Printf("[DEBUG] Creating Cloudflare Access Policy from struct: %+v", newAccessPolicy)
accessPolicy, err := client.CreateAccessPolicy(zoneID, appID, newAccessPolicy)
if err != nil {
return fmt.Errorf("error creating Access Policy for ID %q: %s", accessPolicy.ID, err)
}
d.SetId(accessPolicy.ID)
return nil
}
func resourceCloudflareAccessPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
appID := d.Get("application_id").(string)
updatedAccessPolicy := cloudflare.AccessPolicy{
Name: d.Get("name").(string),
Precedence: d.Get("precedence").(int),
Decision: d.Get("decision").(string),
ID: d.Id(),
}
updatedAccessPolicy = appendConditionalAccessPolicyFields(updatedAccessPolicy, d)
log.Printf("[DEBUG] Updating Cloudflare Access Policy from struct: %+v", updatedAccessPolicy)
accessPolicy, err := client.UpdateAccessPolicy(zoneID, appID, updatedAccessPolicy)
if err != nil {
return fmt.Errorf("error updating Access Policy for ID %q: %s", d.Id(), err)
}
if accessPolicy.ID == "" {
return fmt.Errorf("failed to find Access Policy ID in update response; resource was empty")
}
return resourceCloudflareAccessPolicyRead(d, meta)
}
func resourceCloudflareAccessPolicyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
appID := d.Get("application_id").(string)
log.Printf("[DEBUG] Deleting Cloudflare Access Policy using ID: %s", d.Id())
err := client.DeleteAccessPolicy(zoneID, appID, d.Id())
if err != nil {
return fmt.Errorf("error deleting Access Policy for ID %q: %s", d.Id(), err)
}
resourceCloudflareAccessPolicyRead(d, meta)
return nil
}
func resourceCloudflareAccessPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
attributes := strings.SplitN(d.Id(), "/", 3)
if len(attributes) != 3 {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"zoneID/accessApplicationID/accessPolicyID\"", d.Id())
}
zoneID, accessAppID, accessPolicyID := attributes[0], attributes[1], attributes[2]
log.Printf("[DEBUG] Importing Cloudflare Access Policy: zoneID %q, appID %q, accessPolicyID %q", zoneID, accessAppID, accessPolicyID)
d.Set("zone_id", zoneID)
d.Set("application_id", accessAppID)
d.SetId(accessPolicyID)
resourceCloudflareAccessPolicyRead(d, meta)
return []*schema.ResourceData{d}, nil
}
// appendConditionalAccessPolicyFields determines which of the
// conditional policy enforcement fields it should append to the
// AccessPolicy by iterating over the provided values and generating the
// correct structs.
func appendConditionalAccessPolicyFields(policy cloudflare.AccessPolicy, d *schema.ResourceData) cloudflare.AccessPolicy {
exclude := d.Get("exclude").([]interface{})
for _, value := range exclude {
policy.Exclude = buildAccessPolicyCondition(value.(map[string]interface{}))
}
require := d.Get("require").([]interface{})
for _, value := range require {
policy.Require = buildAccessPolicyCondition(value.(map[string]interface{}))
}
include := d.Get("include").([]interface{})
for _, value := range include {
policy.Include = buildAccessPolicyCondition(value.(map[string]interface{}))
}
return policy
}
// buildAccessPolicyCondition iterates the provided `map` of values and
// generates the required (repetitive) structs.
//
// Returns the intended combination structure of AccessPolicyEmail,
// AccessPolicyEmailDomain, AccessPolicyIP and AccessPolicyEveryone
// structs.
func buildAccessPolicyCondition(options map[string]interface{}) []interface{} {
var policy []interface{}
for accessPolicyType, values := range options {
// Since AccessPolicyEveryone is a single boolean, we don't need to
// iterate over the values like the others so we treat it a little differently.
if accessPolicyType == "everyone" {
if values == true {
log.Printf("[DEBUG] values for everyone %s", values)
policy = append(policy, cloudflare.AccessPolicyEveryone{})
}
} else {
for _, value := range values.([]interface{}) {
switch accessPolicyType {
case "email":
policy = append(policy, cloudflare.AccessPolicyEmail{Email: struct {
Email string `json:"email"`
}{Email: value.(string)}})
case "email_domain":
policy = append(policy, cloudflare.AccessPolicyEmailDomain{EmailDomain: struct {
Domain string `json:"domain"`
}{Domain: value.(string)}})
case "ip":
policy = append(policy, cloudflare.AccessPolicyIP{IP: struct {
IP string `json:"ip"`
}{IP: value.(string)}})
}
}
}
}
return policy
}