-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.tf
277 lines (238 loc) · 9.52 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
resource "aws_networkfirewall_firewall" "this" {
name = "${var.prefix}-nfw-${var.firewall_name}"
description = coalesce(var.description, var.firewall_name)
firewall_policy_arn = aws_networkfirewall_firewall_policy.this.arn
vpc_id = var.vpc_id
firewall_policy_change_protection = var.firewall_policy_change_protection
subnet_change_protection = var.subnet_change_protection
dynamic "subnet_mapping" {
for_each = toset(var.subnet_mapping)
content {
subnet_id = subnet_mapping.value
}
}
tags = var.tags
}
resource "aws_networkfirewall_rule_group" "suricata_stateful_group" {
count = length(var.suricata_stateful_rule_group) > 0 ? length(var.suricata_stateful_rule_group) : 0
type = "STATEFUL"
name = var.suricata_stateful_rule_group[count.index]["name"]
description = var.suricata_stateful_rule_group[count.index]["description"]
capacity = var.suricata_stateful_rule_group[count.index]["capacity"]
rule_group {
rules_source {
rules_string = file(var.suricata_stateful_rule_group[count.index]["rules_file"])
}
dynamic "rule_variables" {
for_each = [
for b in lookup(var.suricata_stateful_rule_group[count.index], "rule_variables", {}) : b
if length(b) > 1
]
content {
dynamic "ip_sets" {
for_each = lookup(lookup(var.suricata_stateful_rule_group[count.index], "rule_variables", {}), "ip_sets", [])
content {
key = ip_sets.value["key"]
ip_set {
definition = ip_sets.value["ip_set"]
}
}
}
dynamic "port_sets" {
for_each = lookup(lookup(var.suricata_stateful_rule_group[count.index], "rule_variables", {}), "port_sets", [])
content {
key = port_sets.value["key"]
port_set {
definition = port_sets.value["port_sets"]
}
}
}
}
}
}
tags = merge(var.tags)
}
resource "aws_networkfirewall_rule_group" "domain_stateful_group" {
count = length(var.domain_stateful_rule_group) > 0 ? length(var.domain_stateful_rule_group) : 0
type = "STATEFUL"
name = var.domain_stateful_rule_group[count.index]["name"]
description = var.domain_stateful_rule_group[count.index]["description"]
capacity = var.domain_stateful_rule_group[count.index]["capacity"]
rule_group {
dynamic "rule_variables" {
for_each = [
for b in lookup(var.domain_stateful_rule_group[count.index], "rule_variables", {}) : b
if length(b) > 1
]
content {
dynamic "ip_sets" {
for_each = lookup(lookup(var.domain_stateful_rule_group[count.index], "rule_variables", {}), "ip_sets", [])
content {
key = ip_sets.value["key"]
ip_set {
definition = ip_sets.value["ip_set"]
}
}
}
dynamic "port_sets" {
for_each = lookup(lookup(var.domain_stateful_rule_group[count.index], "rule_variables", {}), "port_sets", [])
content {
key = port_sets.value["key"]
port_set {
definition = port_sets.value["port_sets"]
}
}
}
}
}
rules_source {
rules_source_list {
generated_rules_type = var.domain_stateful_rule_group[count.index]["actions"]
target_types = var.domain_stateful_rule_group[count.index]["protocols"]
targets = var.domain_stateful_rule_group[count.index]["domain_list"]
}
}
}
tags = merge(var.tags)
}
resource "aws_networkfirewall_rule_group" "fivetuple_stateful_group" {
count = length(var.fivetuple_stateful_rule_group) > 0 ? length(var.fivetuple_stateful_rule_group) : 0
type = "STATEFUL"
name = var.fivetuple_stateful_rule_group[count.index]["name"]
description = var.fivetuple_stateful_rule_group[count.index]["description"]
capacity = var.fivetuple_stateful_rule_group[count.index]["capacity"]
rule_group {
rules_source {
dynamic "stateful_rule" {
for_each = var.fivetuple_stateful_rule_group[count.index].rule_config
content {
action = upper(stateful_rule.value.actions["type"])
header {
destination = stateful_rule.value.destination_ipaddress
destination_port = stateful_rule.value.destination_port
direction = upper(stateful_rule.value.direction)
protocol = upper(stateful_rule.value.protocol)
source = stateful_rule.value.source_ipaddress
source_port = stateful_rule.value.source_port
}
rule_option {
keyword = "sid"
settings = ["${stateful_rule.value.sid}; msg:\"${try(stateful_rule.value.description, "")}\""]
}
}
}
}
}
tags = merge(var.tags)
}
resource "aws_networkfirewall_rule_group" "stateless_group" {
count = length(var.stateless_rule_group) > 0 ? length(var.stateless_rule_group) : 0
type = "STATELESS"
name = var.stateless_rule_group[count.index]["name"]
description = var.stateless_rule_group[count.index]["description"]
capacity = var.stateless_rule_group[count.index]["capacity"]
rule_group {
rules_source {
stateless_rules_and_custom_actions {
dynamic "stateless_rule" {
for_each = var.stateless_rule_group[count.index].rule_config
content {
priority = stateless_rule.value.priority
rule_definition {
actions = ["aws:${stateless_rule.value.actions["type"]}"]
match_attributes {
source {
address_definition = stateless_rule.value.source_ipaddress
}
# If protocol is TCP : 6 or UDP :17 get source ports from variables and set in source_port block
dynamic "source_port" {
for_each = contains(stateless_rule.value.protocols_number, 6) || contains(stateless_rule.value.protocols_number, 17) ? try(toset([{
from = stateless_rule.value.source_from_port,
to = stateless_rule.value.source_to_port
}]), []) : []
content {
from_port = source_port.value.from
to_port = source_port.value.to
}
}
destination {
address_definition = stateless_rule.value.destination_ipaddress
}
# If protocol is TCP : 6 or UDP :17 get destination ports from variables and set in destination_port block
dynamic "destination_port" {
for_each = contains(stateless_rule.value.protocols_number, 6) || contains(stateless_rule.value.protocols_number, 17) ? try(toset([{
from = stateless_rule.value.destination_from_port,
to = stateless_rule.value.destination_to_port
}]), []) : []
content {
from_port = destination_port.value.from
to_port = destination_port.value.to
}
}
protocols = stateless_rule.value.protocols_number
dynamic "tcp_flag" {
for_each = contains(stateless_rule.value.protocols_number, 6) || contains(stateless_rule.value.protocols_number, 17) ? try(toset([{
flag = stateless_rule.value.tcp_flag["flags"],
masks = stateless_rule.value.tcp_flag["masks"]
}]), []) : []
content {
flags = tcp_flag.value.flag
masks = tcp_flag.value.masks
}
}
}
}
}
}
}
}
}
tags = merge(var.tags)
}
resource "aws_networkfirewall_firewall_policy" "this" {
name = "${var.prefix}-nfw-policy-${var.firewall_name}"
firewall_policy {
stateless_default_actions = ["aws:${var.stateless_default_actions}"]
stateless_fragment_default_actions = ["aws:${var.stateless_fragment_default_actions}"]
#Stateless Rule Group Reference
dynamic "stateless_rule_group_reference" {
for_each = local.this_stateless_group_arn
content {
# Priority is sequentially as per index in stateless_rule_group list
priority = index(local.this_stateless_group_arn, stateless_rule_group_reference.value) + 1
resource_arn = stateless_rule_group_reference.value
}
}
#StateFul Rule Group Reference
dynamic "stateful_rule_group_reference" {
for_each = local.this_stateful_group_arn
content {
resource_arn = stateful_rule_group_reference.value
}
}
}
tags = merge(var.tags)
}
###################### Logging Config ######################
resource "aws_cloudwatch_log_group" "nfw" {
for_each = try(var.logging_config, {})
name = "/aws/network-firewall/${each.key}"
tags = merge(var.tags)
retention_in_days = each.value.retention_in_days
}
resource "aws_networkfirewall_logging_configuration" "this" {
count = try(length(var.logging_config), 0) > 0 ? 1 : 0
firewall_arn = aws_networkfirewall_firewall.this.arn
logging_configuration {
dynamic "log_destination_config" {
for_each = var.logging_config
content {
log_destination = {
logGroup = aws_cloudwatch_log_group.nfw[log_destination_config.key].name
}
log_destination_type = "CloudWatchLogs"
log_type = upper(log_destination_config.key)
}
}
}
}