-
Notifications
You must be signed in to change notification settings - Fork 134
/
block_fastly_waf_configuration_v1_active_rule.go
198 lines (162 loc) · 5.15 KB
/
block_fastly_waf_configuration_v1_active_rule.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
package fastly
import (
"log"
gofastly "github.com/fastly/go-fastly/v3/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var activeRule = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
Description: "The Web Application Firewall rule's status. Allowed values are (`log`, `block` and `score`)",
ValidateDiagFunc: validateRuleStatusType(),
},
"modsec_rule_id": {
Type: schema.TypeInt,
Required: true,
Description: "The Web Application Firewall rule's modsecurity ID",
},
"revision": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The Web Application Firewall rule's revision. The latest revision will be used if this is not provided",
},
},
},
}
func updateRules(d *schema.ResourceData, meta interface{}, wafID string, Number int) error {
conn := meta.(*FastlyClient).conn
os, ns := d.GetChange("rule")
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 := deleteByModSecID(oss.Difference(nss), add).List()
log.Print("[INFO] WAF rules update")
if len(remove) > 0 {
deleteOpts := buildBatchDeleteWAFActiveRulesInput(remove, wafID, Number)
log.Printf("[DEBUG] WAF rules delete opts: %#v", deleteOpts)
err := executeBatchWAFActiveRulesOperations(conn, &deleteOpts)
if err != nil {
return err
}
}
if len(add) > 0 {
createOpts := buildBatchCreateWAFActiveRulesInput(add, wafID, Number)
log.Printf("[DEBUG] WAF rules create opts: %#v", createOpts)
err := executeBatchWAFActiveRulesOperations(conn, &createOpts)
if err != nil {
return err
}
}
return nil
}
func readWAFRules(meta interface{}, d *schema.ResourceData, v int) error {
conn := meta.(*FastlyClient).conn
wafID := d.Get("waf_id").(string)
log.Printf("[INFO] retrieving active rules for WAF: %s", wafID)
resp, err := conn.ListAllWAFActiveRules(&gofastly.ListAllWAFActiveRulesInput{
WAFID: wafID,
WAFVersionNumber: v,
})
if err != nil {
return err
}
rules := flattenWAFActiveRules(resp.Items)
if err := d.Set("rule", rules); err != nil {
log.Printf("[WARN] Error setting WAF rules for (%s): %s", d.Id(), err)
}
return nil
}
func buildBatchCreateWAFActiveRulesInput(items []interface{}, wafID string, wafVersionNumber int) gofastly.BatchModificationWAFActiveRulesInput {
rules := make([]*gofastly.WAFActiveRule, len(items))
for i, rRaw := range items {
rf := rRaw.(map[string]interface{})
rules[i] = &gofastly.WAFActiveRule{
ModSecID: rf["modsec_rule_id"].(int),
Revision: rf["revision"].(int),
Status: rf["status"].(string),
}
}
return gofastly.BatchModificationWAFActiveRulesInput{
WAFID: wafID,
WAFVersionNumber: wafVersionNumber,
Rules: rules,
OP: gofastly.UpsertBatchOperation,
}
}
func buildBatchDeleteWAFActiveRulesInput(items []interface{}, wafID string, wafVersionNumber int) gofastly.BatchModificationWAFActiveRulesInput {
rules := make([]*gofastly.WAFActiveRule, len(items))
for i, rRaw := range items {
rf := rRaw.(map[string]interface{})
rules[i] = &gofastly.WAFActiveRule{
ModSecID: rf["modsec_rule_id"].(int),
}
}
return gofastly.BatchModificationWAFActiveRulesInput{
WAFID: wafID,
WAFVersionNumber: wafVersionNumber,
Rules: rules,
OP: gofastly.DeleteBatchOperation,
}
}
func executeBatchWAFActiveRulesOperations(conn *gofastly.Client, input *gofastly.BatchModificationWAFActiveRulesInput) error {
batchSize := gofastly.WAFBatchModifyMaximumOperations
items := input.Rules
for i := 0; i < len(items); i += batchSize {
j := i + batchSize
if j > len(items) {
j = len(items)
}
batch := items[i:j]
if _, err := conn.BatchModificationWAFActiveRules(&gofastly.BatchModificationWAFActiveRulesInput{
WAFID: input.WAFID,
WAFVersionNumber: input.WAFVersionNumber,
Rules: batch,
OP: input.OP,
}); err != nil {
return err
}
}
return nil
}
func flattenWAFActiveRules(rules []*gofastly.WAFActiveRule) []map[string]interface{} {
rl := make([]map[string]interface{}, len(rules))
for i, r := range rules {
ruleMapString := map[string]interface{}{
"modsec_rule_id": r.ModSecID,
"revision": r.Revision,
"status": r.Status,
}
rl[i] = ruleMapString
}
return rl
}
// deleteByModSecID returns a copy of the argument "remove" with all common (with the same modsec_rule_id) elements with argument "add" removed.
func deleteByModSecID(remove *schema.Set, add []interface{}) *schema.Set {
modSecIDs := make(map[int]interface{}, remove.Len())
result := schema.CopySet(remove)
for _, rv := range remove.List() {
r := rv.(map[string]interface{})
modSecIDs[r["modsec_rule_id"].(int)] = r
}
if len(modSecIDs) > 0 {
for _, av := range add {
a := av.(map[string]interface{})
if v, ok := modSecIDs[a["modsec_rule_id"].(int)]; ok {
result.Remove(v)
}
}
}
return result
}