-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.tf
164 lines (133 loc) · 4.15 KB
/
rules.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
resource "aws_networkfirewall_firewall_policy" "main" {
name = local.dashed_name
firewall_policy {
stateless_default_actions = ["aws:forward_to_sfe"]
stateless_fragment_default_actions = ["aws:forward_to_sfe"]
dynamic "stateful_rule_group_reference" {
for_each = aws_networkfirewall_rule_group.allow-ips
content {
resource_arn = stateful_rule_group_reference.value.arn
}
}
dynamic "stateful_rule_group_reference" {
for_each = aws_networkfirewall_rule_group.block-ips
content {
resource_arn = stateful_rule_group_reference.value.arn
}
}
dynamic "stateful_rule_group_reference" {
for_each = aws_networkfirewall_rule_group.block-domains
content {
resource_arn = stateful_rule_group_reference.value.arn
}
}
dynamic "stateful_rule_group_reference" {
for_each = aws_networkfirewall_rule_group.block-everything
content {
resource_arn = stateful_rule_group_reference.value.arn
}
}
}
lifecycle {
create_before_destroy = true
}
tags = var.tags
}
resource "aws_networkfirewall_rule_group" "allow-ips" {
for_each = var.allowed_ips
capacity = each.value.capacity
description = format("%s allow specific IPs for %s", local.dashed_name, each.key)
name = format("%s-allow-specific-ips-%s", local.dashed_name, each.key)
type = "STATEFUL"
rule_group {
rules_source {
dynamic "stateful_rule" {
for_each = each.value.ips
content {
action = "PASS"
header {
destination = "ANY"
destination_port = "ANY"
protocol = "IP"
direction = "ANY"
source_port = "ANY"
source = stateful_rule.value
}
rule_option {
keyword = "sid:1${each.key}${stateful_rule.key}"
}
}
}
dynamic "stateful_rule" {
for_each = each.value.ips
content {
action = "PASS"
header {
destination = stateful_rule.value
destination_port = "ANY"
protocol = "IP"
direction = "ANY"
source_port = "ANY"
source = "ANY"
}
rule_option {
keyword = "sid:0${each.key}${stateful_rule.key}"
}
}
}
}
}
tags = var.tags
}
resource "aws_networkfirewall_rule_group" "block-ips" {
for_each = var.blocked_ips
capacity = each.value.capacity
description = format("%s block specific IPs for %s", local.dashed_name, each.key)
name = format("%s-block-specific-ips-%s", local.dashed_name, each.key)
type = "STATEFUL"
rules = join("\n", [for ip in each.value.ips : "drop IP $HOME_NET ANY <> ${ip} ANY (sid:0${each.key}${index(each.value.ips, ip)};)"])
tags = var.tags
}
resource "aws_networkfirewall_rule_group" "block-domains" {
for_each = var.blocked_domains
capacity = each.value.capacity
description = format("%s block specific Domains for %s", local.dashed_name, each.key)
name = format("%s-block-specific-domains-%s", local.dashed_name, each.key)
type = "STATEFUL"
rule_group {
rules_source {
rules_source_list {
generated_rules_type = "DENYLIST"
targets = each.value.domains
target_types = ["HTTP_HOST", "TLS_SNI"]
}
}
}
tags = var.tags
}
resource "aws_networkfirewall_rule_group" "block-everything" {
count = var.enable_block_everything_by_default == true ? 1 : 0
capacity = var.block_everything_capacity
description = "Block all traffic"
name = format("%s-block-everything", local.dashed_name)
type = "STATEFUL"
rule_group {
rules_source {
stateful_rule {
action = "DROP"
header {
destination = "ANY"
destination_port = "ANY"
protocol = "IP"
direction = "ANY"
source_port = "ANY"
source = "ANY"
}
rule_option {
keyword = "sid:1"
}
}
}
}
tags = var.tags
}