-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhooks.tf
215 lines (177 loc) · 6.16 KB
/
webhooks.tf
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
resource "aws_wafv2_rule_group" "webhooks" {
for_each = length(var.webhooks) > 0 ? toset(["this"]) : toset([])
name_prefix = "${local.prefix}-webhooks-"
scope = "CLOUDFRONT"
capacity = var.webhook_rules_capacity == null ? 50 : var.webhook_rules_capacity
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "${local.prefix}-webhooks"
sampled_requests_enabled = true
}
# Define the rules to match the paths and apply a label to matching requests.
dynamic "rule" {
for_each = var.webhooks
content {
name = "${local.prefix}-webhooks-${rule.key}-label"
priority = index(keys(var.webhooks), rule.key)
rule_label {
name = "webhook:${rule.key}"
}
action {
count {}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "${local.prefix}-webhooks-${rule.key}-label"
sampled_requests_enabled = true
}
# Check if the path matches any of the webhook paths.
statement {
# OR statements require at least two child statements. If we only have
# a single path, we can use a single byte match statement.
dynamic "byte_match_statement" {
for_each = length(rule.value.paths) == 1 ? [true] : []
content {
positional_constraint = rule.value.paths[0].constraint
search_string = rule.value.paths[0].path
field_to_match {
uri_path {}
}
text_transformation {
priority = 0
type = "NONE"
}
}
}
# If there are more than one path, we'll evaluate them each in an OR
# statement.
dynamic "or_statement" {
for_each = length(rule.value.paths) > 1 ? [true] : []
content {
dynamic "statement" {
for_each = rule.value.paths
content {
byte_match_statement {
positional_constraint = statement.value.constraint
search_string = statement.value.path
field_to_match {
uri_path {}
}
text_transformation {
priority = 0
type = "NONE"
}
}
}
}
}
}
}
}
}
# Define the rules to check additional criteria before allowing the request.
dynamic "rule" {
for_each = var.webhooks
content {
name = "${local.prefix}-webhooks-${rule.key}"
priority = index(keys(var.webhooks), rule.key) + length(keys(var.webhooks))
action {
dynamic "allow" {
for_each = rule.value.action == "allow" ? [true] : []
content {}
}
dynamic "block" {
for_each = rule.value.action == "block" ? [true] : []
content {}
}
dynamic "count" {
for_each = rule.value.action == "count" ? [true] : []
content {}
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "${local.prefix}-webhooks-${rule.key}"
sampled_requests_enabled = true
}
statement {
# If there are no additional criteria, just create the label match
# statement.
dynamic "label_match_statement" {
for_each = length(rule.value.criteria) == 0 ? [true] : []
content {
scope = "LABEL"
key = "webhook:${rule.key}"
}
}
# If there are additional criteria, we'll evaluate them each in an AND
# statement, along with the label match statement.
dynamic "and_statement" {
for_each = length(rule.value.criteria) > 0 ? [true] : []
content {
statement {
label_match_statement {
scope = "LABEL"
key = "webhook:${rule.key}"
}
}
dynamic "statement" {
for_each = rule.value.criteria
content {
dynamic "byte_match_statement" {
for_each = statement.value.type == "byte" ? [true] : []
content {
positional_constraint = statement.value.constraint
search_string = statement.value.value
field_to_match {
dynamic "uri_path" {
for_each = statement.value.field == "uri" ? [true] : []
content {}
}
dynamic "single_header" {
for_each = statement.value.field == "header" ? [true] : []
content {
name = statement.value.name
}
}
}
text_transformation {
priority = 0
type = "NONE"
}
}
}
dynamic "size_constraint_statement" {
for_each = statement.value.type == "size" ? [true] : []
content {
comparison_operator = statement.value.constraint
size = tonumber(statement.value.value)
field_to_match {
dynamic "uri_path" {
for_each = statement.value.field == "uri" ? [true] : []
content {}
}
dynamic "single_header" {
for_each = statement.value.field == "header" ? [true] : []
content {
name = statement.value.name
}
}
}
text_transformation {
priority = 0
type = "NONE"
}
}
}
}
}
}
}
}
}
}
lifecycle {
create_before_destroy = true
}
}