forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_waf_xss_match_set.go
181 lines (147 loc) · 4.72 KB
/
resource_aws_waf_xss_match_set.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
package aws
import (
"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/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsWafXssMatchSet() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWafXssMatchSetCreate,
Read: resourceAwsWafXssMatchSetRead,
Update: resourceAwsWafXssMatchSetUpdate,
Delete: resourceAwsWafXssMatchSetDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"xss_match_tuples": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field_to_match": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"text_transformation": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func resourceAwsWafXssMatchSetCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
log.Printf("[INFO] Creating XssMatchSet: %s", d.Get("name").(string))
// ChangeToken
var ct *waf.GetChangeTokenInput
res, err := conn.GetChangeToken(ct)
if err != nil {
return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err)
}
params := &waf.CreateXssMatchSetInput{
ChangeToken: res.ChangeToken,
Name: aws.String(d.Get("name").(string)),
}
resp, err := conn.CreateXssMatchSet(params)
if err != nil {
return errwrap.Wrapf("[ERROR] Error creating XssMatchSet: {{err}}", err)
}
d.SetId(*resp.XssMatchSet.XssMatchSetId)
return resourceAwsWafXssMatchSetUpdate(d, meta)
}
func resourceAwsWafXssMatchSetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
log.Printf("[INFO] Reading XssMatchSet: %s", d.Get("name").(string))
params := &waf.GetXssMatchSetInput{
XssMatchSetId: aws.String(d.Id()),
}
resp, err := conn.GetXssMatchSet(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
log.Printf("[WARN] WAF IPSet (%s) not found, error code (404)", d.Id())
d.SetId("")
return nil
}
return err
}
d.Set("name", resp.XssMatchSet.Name)
return nil
}
func resourceAwsWafXssMatchSetUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Updating XssMatchSet: %s", d.Get("name").(string))
err := updateXssMatchSetResource(d, meta, waf.ChangeActionInsert)
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating XssMatchSet: {{err}}", err)
}
return resourceAwsWafXssMatchSetRead(d, meta)
}
func resourceAwsWafXssMatchSetDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
log.Printf("[INFO] Deleting XssMatchSet: %s", d.Get("name").(string))
err := updateXssMatchSetResource(d, meta, waf.ChangeActionDelete)
if err != nil {
return errwrap.Wrapf("[ERROR] Error deleting XssMatchSet: {{err}}", err)
}
var ct *waf.GetChangeTokenInput
resp, err := conn.GetChangeToken(ct)
req := &waf.DeleteXssMatchSetInput{
ChangeToken: resp.ChangeToken,
XssMatchSetId: aws.String(d.Id()),
}
_, err = conn.DeleteXssMatchSet(req)
if err != nil {
return errwrap.Wrapf("[ERROR] Error deleting XssMatchSet: {{err}}", err)
}
return nil
}
func updateXssMatchSetResource(d *schema.ResourceData, meta interface{}, ChangeAction string) error {
conn := meta.(*AWSClient).wafconn
var ct *waf.GetChangeTokenInput
resp, err := conn.GetChangeToken(ct)
if err != nil {
return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err)
}
req := &waf.UpdateXssMatchSetInput{
ChangeToken: resp.ChangeToken,
XssMatchSetId: aws.String(d.Id()),
}
xssMatchTuples := d.Get("xss_match_tuples").(*schema.Set)
for _, xssMatchTuple := range xssMatchTuples.List() {
xmt := xssMatchTuple.(map[string]interface{})
xssMatchTupleUpdate := &waf.XssMatchSetUpdate{
Action: aws.String(ChangeAction),
XssMatchTuple: &waf.XssMatchTuple{
FieldToMatch: expandFieldToMatch(xmt["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})),
TextTransformation: aws.String(xmt["text_transformation"].(string)),
},
}
req.Updates = append(req.Updates, xssMatchTupleUpdate)
}
_, err = conn.UpdateXssMatchSet(req)
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating XssMatchSet: {{err}}", err)
}
return nil
}