-
Notifications
You must be signed in to change notification settings - Fork 134
/
block_fastly_waf_configuration_v1_active_rule.go
202 lines (168 loc) · 5.64 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
199
200
201
202
package fastly
import (
"fmt"
"log"
gofastly "github.com/fastly/go-fastly/v5/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)
}
oldSet := os.(*schema.Set)
newSet := ns.(*schema.Set)
setDiff := NewSetDiff(func(resource interface{}) (interface{}, error) {
t, ok := resource.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("resource failed to be type asserted: %+v", resource)
}
// NOTE: WAF rule block doesn't have a "name" attribute.
return t["modsec_rule_id"], nil
})
diffResult, err := setDiff.Diff(oldSet, newSet)
if err != nil {
return err
}
// NOTE: Fastly WAF (WAF 2020) API doesn't have a proper batch update endpoint.
// go-fastly uses the below endpoint for UpsertBatchOperation:
// "POST /waf/firewalls/:firewall_id/versions/:version_id/active-rules"
// but this endpoint only updates "status" when it comes to upsert operation and "revision" field is ignored.
// Therefore, when one of the rule attributes is changed we must delete it first and create it as a new rule.
log.Print("[INFO] WAF rules update")
// DELETE removed rules
if len(diffResult.Deleted) > 0 || len(diffResult.Modified) > 0 {
var items []interface{}
items = append(items, diffResult.Deleted...)
items = append(items, diffResult.Modified...)
deleteOpts := buildBatchDeleteWAFActiveRulesInput(items, wafID, Number)
log.Printf("[DEBUG] WAF rules delete opts: %#v", deleteOpts)
err := executeBatchWAFActiveRulesOperations(conn, &deleteOpts)
if err != nil {
return err
}
}
// CREATE new rules
if len(diffResult.Added) > 0 || len(diffResult.Modified) > 0 {
var items []interface{}
items = append(items, diffResult.Added...)
items = append(items, diffResult.Modified...)
createOpts := buildBatchCreateWAFActiveRulesInput(items, 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
}