-
Notifications
You must be signed in to change notification settings - Fork 137
/
block_fastly_waf_configuration_v1_exclusion.go
213 lines (177 loc) · 5.71 KB
/
block_fastly_waf_configuration_v1_exclusion.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
package fastly
import (
"fmt"
"log"
"strconv"
gofastly "github.com/fastly/go-fastly/v3/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
var wafRuleExclusion = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of rule exclusion",
},
"condition": {
Type: schema.TypeString,
Required: true,
Description: "A conditional expression in VCL used to determine if the condition is met",
},
"exclusion_type": {
Type: schema.TypeString,
Required: true,
Description: "The type of rule exclusion. Values are `rule` to exclude the specified rule(s), or `waf` to disable the Web Application Firewall",
ValidateDiagFunc: validateExecutionType(),
},
"modsec_rule_ids": {
Type: schema.TypeSet,
Optional: true,
Description: "Set of modsecurity IDs to be excluded. No rules should be provided when `exclusion_type` is `waf`. The rules need to be configured on the Web Application Firewall to be excluded",
Elem: &schema.Schema{Type: schema.TypeInt},
},
"number": {
Type: schema.TypeInt,
Computed: true,
Description: "The numeric ID assigned to the WAF Rule Exclusion",
},
},
},
}
func readWAFRuleExclusions(meta interface{}, d *schema.ResourceData, wafVersionNumber int) error {
conn := meta.(*FastlyClient).conn
wafID := d.Get("waf_id").(string)
resp, e := conn.ListAllWAFRuleExclusions(&gofastly.ListAllWAFRuleExclusionsInput{
WAFID: wafID,
WAFVersionNumber: wafVersionNumber,
Include: []string{"waf_rules"},
})
if e != nil {
return e
}
err := d.Set("rule_exclusion", flattenWAFRuleExclusions(resp.Items))
if err != nil {
log.Printf("[WARN] Error setting WAF rule exclusions for (%s): %s", d.Id(), err)
}
return nil
}
func flattenWAFRuleExclusions(exclusions []*gofastly.WAFRuleExclusion) []map[string]interface{} {
var result []map[string]interface{}
for _, exclusion := range exclusions {
m := make(map[string]interface{})
if exclusion.Name != nil {
m["name"] = *exclusion.Name
}
if exclusion.Number != nil {
m["number"] = *exclusion.Number
}
if exclusion.Condition != nil {
m["condition"] = *exclusion.Condition
}
if exclusion.ExclusionType != nil {
m["exclusion_type"] = *exclusion.ExclusionType
}
var rules []interface{}
for _, rule := range exclusion.Rules {
rules = append(rules, rule.ModSecID)
}
if len(rules) > 0 {
m["modsec_rule_ids"] = schema.NewSet(schema.HashInt, rules)
}
result = append(result, m)
}
return result
}
func updateWAFRuleExclusions(d *schema.ResourceData, meta interface{}, wafID string, wafVersionNumber int) error {
os, ns := d.GetChange("rule_exclusion")
if os == nil {
os = new(schema.Set)
}
if ns == nil {
ns = new(schema.Set)
}
oss := os.(*schema.Set)
nss := ns.(*schema.Set)
add := nss.Difference(oss).List()
remove := oss.Difference(nss).List()
var err error
err = deleteWAFRuleExclusion(remove, meta, wafID, wafVersionNumber)
if err != nil {
return err
}
err = createWAFRuleExclusion(add, meta, wafID, wafVersionNumber)
if err != nil {
return err
}
return nil
}
func deleteWAFRuleExclusion(remove []interface{}, meta interface{}, wafID string, wafVersionNumber int) error {
conn := meta.(*FastlyClient).conn
for _, aRaw := range remove {
a := aRaw.(map[string]interface{})
err := conn.DeleteWAFRuleExclusion(&gofastly.DeleteWAFRuleExclusionInput{
Number: a["number"].(int),
WAFID: wafID,
WAFVersionNumber: wafVersionNumber,
})
if err != nil {
return err
}
}
return nil
}
func createWAFRuleExclusion(add []interface{}, meta interface{}, wafID string, wafVersionNumber int) error {
conn := meta.(*FastlyClient).conn
for _, aRaw := range add {
a := aRaw.(map[string]interface{})
var rules []*gofastly.WAFRule
if a["exclusion_type"] == gofastly.WAFRuleExclusionTypeRule {
for _, ruleId := range a["modsec_rule_ids"].(*schema.Set).List() {
rules = append(rules, &gofastly.WAFRule{
ID: strconv.Itoa(ruleId.(int)),
})
}
} else {
rules = nil
}
_, err := conn.CreateWAFRuleExclusion(&gofastly.CreateWAFRuleExclusionInput{
WAFID: wafID,
WAFVersionNumber: wafVersionNumber,
WAFRuleExclusion: &gofastly.WAFRuleExclusion{
Name: gofastly.String(a["name"].(string)),
ExclusionType: gofastly.String(a["exclusion_type"].(string)),
Condition: gofastly.String(a["condition"].(string)),
Rules: rules,
},
})
if err != nil {
return err
}
}
return nil
}
func validateExecutionType() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(validation.StringInSlice(
[]string{
gofastly.WAFRuleExclusionTypeRule,
gofastly.WAFRuleExclusionTypeWAF,
},
false,
))
}
func validateWAFRuleExclusion(d *schema.ResourceDiff) error {
for _, i := range d.Get("rule_exclusion").(*schema.Set).List() {
wafRuleExclusion := i.(map[string]interface{})
if wafRuleExclusion["exclusion_type"] == gofastly.WAFRuleExclusionTypeWAF && len(wafRuleExclusion["modsec_rule_ids"].(*schema.Set).List()) > 0 {
return fmt.Errorf("must not set \"modsec_rule_ids\" with \"waf\" exclusion type in exclusion \"%s\"", wafRuleExclusion["name"])
}
if wafRuleExclusion["exclusion_type"] == gofastly.WAFRuleExclusionTypeRule && len(wafRuleExclusion["modsec_rule_ids"].(*schema.Set).List()) == 0 {
return fmt.Errorf("must set \"modsec_rule_ids\" with \"rule\" exclusion type in exclusion \"%s\"", wafRuleExclusion["name"])
}
}
return nil
}