forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_waf_web_acl.go
237 lines (203 loc) · 5.64 KB
/
resource_aws_waf_web_acl.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsWafWebAcl() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWafWebAclCreate,
Read: resourceAwsWafWebAclRead,
Update: resourceAwsWafWebAclUpdate,
Delete: resourceAwsWafWebAclDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"default_action": &schema.Schema{
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
"metric_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"rules": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": &schema.Schema{
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
"priority": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"rule_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func resourceAwsWafWebAclCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
// ChangeToken
var ct *waf.GetChangeTokenInput
res, err := conn.GetChangeToken(ct)
if err != nil {
return fmt.Errorf("Error getting change token: %s", err)
}
params := &waf.CreateWebACLInput{
ChangeToken: res.ChangeToken,
DefaultAction: expandDefaultAction(d),
MetricName: aws.String(d.Get("metric_name").(string)),
Name: aws.String(d.Get("name").(string)),
}
resp, err := conn.CreateWebACL(params)
if err != nil {
return err
}
d.SetId(*resp.WebACL.WebACLId)
return resourceAwsWafWebAclUpdate(d, meta)
}
func resourceAwsWafWebAclRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
params := &waf.GetWebACLInput{
WebACLId: aws.String(d.Id()),
}
resp, err := conn.GetWebACL(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
log.Printf("[WARN] WAF ACL (%s) not found, error code (404)", d.Id())
d.SetId("")
return nil
}
return err
}
defaultAction := flattenDefaultAction(resp.WebACL.DefaultAction)
if defaultAction != nil {
if err := d.Set("default_action", defaultAction); err != nil {
return fmt.Errorf("error setting default_action: %s", err)
}
}
d.Set("name", resp.WebACL.Name)
d.Set("metric_name", resp.WebACL.MetricName)
return nil
}
func resourceAwsWafWebAclUpdate(d *schema.ResourceData, meta interface{}) error {
err := updateWebAclResource(d, meta, waf.ChangeActionInsert)
if err != nil {
return fmt.Errorf("Error Updating WAF ACL: %s", err)
}
return resourceAwsWafWebAclRead(d, meta)
}
func resourceAwsWafWebAclDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
err := updateWebAclResource(d, meta, waf.ChangeActionDelete)
if err != nil {
return fmt.Errorf("Error Removing WAF ACL Rules: %s", err)
}
var ct *waf.GetChangeTokenInput
resp, err := conn.GetChangeToken(ct)
req := &waf.DeleteWebACLInput{
ChangeToken: resp.ChangeToken,
WebACLId: aws.String(d.Id()),
}
log.Printf("[INFO] Deleting WAF ACL")
_, err = conn.DeleteWebACL(req)
if err != nil {
return fmt.Errorf("Error Deleting WAF ACL: %s", err)
}
return nil
}
func updateWebAclResource(d *schema.ResourceData, meta interface{}, ChangeAction string) error {
conn := meta.(*AWSClient).wafconn
// ChangeToken
var ct *waf.GetChangeTokenInput
resp, err := conn.GetChangeToken(ct)
if err != nil {
return fmt.Errorf("Error getting change token: %s", err)
}
req := &waf.UpdateWebACLInput{
ChangeToken: resp.ChangeToken,
WebACLId: aws.String(d.Id()),
}
if d.HasChange("default_action") {
req.DefaultAction = expandDefaultAction(d)
}
rules := d.Get("rules").(*schema.Set)
for _, rule := range rules.List() {
aclRule := rule.(map[string]interface{})
action := aclRule["action"].(*schema.Set).List()[0].(map[string]interface{})
aclRuleUpdate := &waf.WebACLUpdate{
Action: aws.String(ChangeAction),
ActivatedRule: &waf.ActivatedRule{
Priority: aws.Int64(int64(aclRule["priority"].(int))),
RuleId: aws.String(aclRule["rule_id"].(string)),
Action: &waf.WafAction{Type: aws.String(action["type"].(string))},
},
}
req.Updates = append(req.Updates, aclRuleUpdate)
}
_, err = conn.UpdateWebACL(req)
if err != nil {
return fmt.Errorf("Error Updating WAF ACL: %s", err)
}
return nil
}
func expandDefaultAction(d *schema.ResourceData) *waf.WafAction {
set, ok := d.GetOk("default_action")
if !ok {
return nil
}
s := set.(*schema.Set).List()
if s == nil || len(s) == 0 {
return nil
}
if s[0] == nil {
log.Printf("[ERR] First element of Default Action is set to nil")
return nil
}
dA := s[0].(map[string]interface{})
return &waf.WafAction{
Type: aws.String(dA["type"].(string)),
}
}
func flattenDefaultAction(n *waf.WafAction) []map[string]interface{} {
if n == nil {
return nil
}
m := setMap(make(map[string]interface{}))
m.SetString("type", n.Type)
return m.MapList()
}