-
Notifications
You must be signed in to change notification settings - Fork 8
/
terraform-aws-waf-iplist.sh
executable file
·198 lines (158 loc) · 3.81 KB
/
terraform-aws-waf-iplist.sh
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
#!/usr/bin/env bash
# This script will generate a Terraform AWS WAF IPSet, WAF Rule, and WAF Web ACL resource blocks
# in a single Terraform file from a list of IPv4 IPs in a file called iplist.
# This can be used to easily whitelist IPs in an AWS WAF Web ACL.
# Requires wget, perl
# Set Variables
FriendlyName="Allow From IP List"
# Do not modify these variables!
TerraformResourceName=$(echo "$FriendlyName" | tr '[:upper:]' '[:lower:]' | tr -s ' ' '_' | tr -s '.' '_')
MetricName=$(echo "$FriendlyName" | sed 's/[^a-z A-Z]//g' | tr -d '[:space:]')
OutputFileName="aws_waf_$TerraformResourceName.tf"
# Debug Mode
DEBUGMODE="0"
# Functions
# Pause
function pause(){
read -n 1 -s -p "Press any key to continue..."
echo
}
# Check Command
function check_command(){
for command in "$@"
do
type -P $command &>/dev/null || fail "Unable to find $command, please install it and run this script again."
done
}
# Completed
function completed(){
echo
HorizontalRule
tput setaf 2; echo "Completed!" && tput sgr0
tput setaf 2; echo "$1" && tput sgr0
HorizontalRule
echo
}
# Fail
function fail(){
tput setaf 1; echo "Error: $*" && tput sgr0
exit 1
}
# Horizontal Rule
function HorizontalRule(){
echo "============================================================"
}
# Get Pingdom IPv4 IPs
# https://help.pingdom.com/hc/en-us/articles/203682601-How-to-get-all-Pingdom-probes-public-IP-addresses
function IPs(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function IPs"
fi
cat iplist | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 | uniq > iplisttemp
TOTALIPS=$(cat iplisttemp | wc -l | tr -d ' ')
if ! [ "$TOTALIPS" -gt "0" ]; then
fail "Unable to get list of IPs."
fi
echo
HorizontalRule
echo "Total IPs: "$TOTALIPS
HorizontalRule
echo
}
# Build the Terraform resource blocks
function buildTF(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function buildTF"
fi
echo
HorizontalRule
echo "Building Terraform Resources..."
HorizontalRule
echo
(
cat << EOP
resource "aws_waf_ipset" "$TerraformResourceName" {
name = "$FriendlyName"
ip_set_descriptors = [
EOP
) > temp1
if [[ $DEBUGMODE = "1" ]]; then
echo built temp1
fi
rm -f temp2
START=1
for (( COUNT=$START; COUNT<=$TOTALIPS; COUNT++ ))
do
iplist=$(nl iplisttemp | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
(
cat << EOP
{ type = "IPV4", value = "$iplist/32" },
EOP
) >> temp2
done
if [[ $DEBUGMODE = "1" ]]; then
echo built temp2
fi
# Remove the last comma to close JSON array
cat temp2 | sed '$ s/.$//' > temp3
if [[ $DEBUGMODE = "1" ]]; then
echo built temp3
fi
(
cat << 'EOP'
]
}
EOP
) > temp4
if [[ $DEBUGMODE = "1" ]]; then
echo built temp4
fi
rm -f temp5
(
cat << EOP
resource "aws_waf_rule" "$TerraformResourceName" {
depends_on = ["aws_waf_ipset.$TerraformResourceName"]
name = "$FriendlyName"
metric_name = "$MetricName"
predicates {
data_id = "\${aws_waf_ipset.$TerraformResourceName.id}"
negated = false
type = "IPMatch"
}
}
resource "aws_waf_web_acl" "$TerraformResourceName" {
depends_on = ["aws_waf_ipset.$TerraformResourceName", "aws_waf_rule.$TerraformResourceName"]
name = "$FriendlyName"
metric_name = "$MetricName"
default_action {
type = "BLOCK"
}
rules {
action {
type = "ALLOW"
}
priority = 1
rule_id = "\${aws_waf_rule.$TerraformResourceName.id}"
type = "REGULAR"
}
}
EOP
) >> temp5
if [[ $DEBUGMODE = "1" ]]; then
echo built temp5
fi
cat temp1 temp3 temp4 temp5 > "$OutputFileName"
if [[ $DEBUGMODE = "1" ]]; then
echo "built $OutputFileName"
fi
}
# Run the script and call functions
# Check for required applications
check_command wget perl
IPs
buildTF
# Cleanup temp files
if ! [[ $DEBUGMODE = "1" ]]; then
rm -f temp1 temp2 temp3 temp4 temp5 iplisttemp
fi
completed "Terraform WAF Resource file: $OutputFileName"