forked from swoodford/aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc-sg-import-rules-pingdom.sh
executable file
·730 lines (648 loc) · 16.4 KB
/
vpc-sg-import-rules-pingdom.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#!/usr/bin/env bash
# This script will save a list of current Pingdom IPv4 probe server IPs in the file pingdom-probe-servers.txt
# Then create an AWS VPC Security Group with rules to allow access to each IP at the port specified.
# Due to AWS limits a group can only have 50 rules. This script will create multiple groups if greater than 50 rules.
# Supports up to 150 rules
# Requires the AWS CLI, jq, wget, perl
# Set Variables
GROUPNAME="Pingdom"
DESCRIPTION="Pingdom Probe Servers"
VPCID="YOUR-VPC-ID-HERE"
PROTOCOL="tcp"
# If allowing only one port use the same port number for both from and to vars below
FROMPORT="80"
TOPORT="443"
# Debug Mode
DEBUGMODE="0"
# Functions
# Pause
function pause(){
read -n 1 -s -p "Press any key to continue..."
echo
}
# Check Command
function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}
# Completed
function completed(){
echo
HorizontalRule
tput setaf 2; echo "Completed!" && tput sgr0
HorizontalRule
echo
}
# Fail
function fail(){
tput setaf 1; echo "Error: $*" && tput sgr0
exit 1
}
# Horizontal Rule
function HorizontalRule(){
echo "============================================================"
}
# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/credentials; then
if ! grep -q aws_access_key_id ~/.aws/config; then
fail "AWS config not found or CLI not installed. Please run \"aws configure\"."
fi
fi
# Check for AWS CLI profile argument passed into the script
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-multiple-profiles
if [ $# -eq 0 ]; then
scriptname=`basename "$0"`
echo "Usage: ./$scriptname profile"
echo "Where profile is the AWS CLI profile name"
echo "Using default profile"
echo
profile=default
else
profile=$1
fi
# Get Pingdom IPv4 IPs
# https://help.pingdom.com/hc/en-us/articles/203682601-How-to-get-all-Pingdom-probes-public-IP-addresses
function probeIPs(){
wget --quiet -O- https://www.pingdom.com/rss/probe_servers.xml | \
perl -nle 'print $1 if /IP: (([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]));/' | \
sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 | \
uniq > pingdom-probe-servers.txt
TOTALIPS=$(cat pingdom-probe-servers.txt | wc -l | tr -d ' ')
if ! [ "$TOTALIPS" -gt "0" ]; then
fail "Unable to lookup Pingdom IPs."
fi
echo
HorizontalRule
echo "Total Pingdom IPs: "$TOTALIPS
HorizontalRule
echo
}
# Create Security Group
function createGroup(){
echo
HorizontalRule
echo "Creating Security Group: "$GROUPNAME
SGID=$(aws ec2 create-security-group --group-name "$GROUPNAME" --description "$DESCRIPTION" --vpc-id $VPCID --profile $profile 2>&1 | jq '.GroupId' | cut -d \" -f2)
if echo $SGID | grep -q "error"; then
fail "$SGID"
fi
echo "Security Group ID:" $SGID
TAG=$(aws ec2 create-tags --resources $SGID --tags Key=Name,Value="$GROUPNAME" --profile $profile 2>&1)
if echo $TAG | grep -q "error"; then
fail "$TAG"
fi
HorizontalRule
echo
}
# Builds the JSON for 50 rules or less
function buildJSON0(){
(
cat << EOP
{
"GroupId": "$SGID",
"IpPermissions": [
{
"IpProtocol": "$PROTOCOL",
"FromPort": $FROMPORT,
"ToPort": $TOPORT,
"IpRanges": [
EOP
) > json1
if [[ $DEBUGMODE = "1" ]]; then
echo built json1
fi
rm -f json2
START=1
for (( COUNT=$START; COUNT<=$TOTALIPS; COUNT++ ))
do
iplist=$(nl pingdom-probe-servers.txt | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
(
cat << EOP
{
"CidrIp": "$iplist/32"
},
EOP
) >> json2
done
if [[ $DEBUGMODE = "1" ]]; then
echo built json2
fi
# Remove the last comma to close JSON array
cat json2 | sed '$ s/.$//' > json3
if [[ $DEBUGMODE = "1" ]]; then
echo built json3
fi
(
cat << 'EOP'
]
}
]
}
EOP
) > json4
if [[ $DEBUGMODE = "1" ]]; then
echo built json4
fi
cat json1 json3 json4 > json
if [[ $DEBUGMODE = "1" ]]; then
echo built json
fi
}
# Builds the JSON for 51-100 rules
function buildJSON50(){
(
cat << EOP
{
"GroupId": "$SGID1",
"IpPermissions": [
{
"IpProtocol": "$PROTOCOL",
"FromPort": $FROMPORT,
"ToPort": $TOPORT,
"IpRanges": [
EOP
) > json1
if [[ $DEBUGMODE = "1" ]]; then
echo built json1
fi
rm -f json2
START=1
for (( COUNT=$START; COUNT<=50; COUNT++ ))
do
iplist=$(nl pingdom-probe-servers.txt | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
(
cat << EOP
{
"CidrIp": "$iplist/32"
},
EOP
) >> json2
done
if [[ $DEBUGMODE = "1" ]]; then
echo built json2
fi
# Remove the last comma to close JSON array
cat json2 | sed '$ s/.$//' > json3
if [[ $DEBUGMODE = "1" ]]; then
echo built json3
fi
(
cat << 'EOP'
]
}
]
}
EOP
) > json4
if [[ $DEBUGMODE = "1" ]]; then
echo built json4
fi
cat json1 json3 json4 > json
if [[ $DEBUGMODE = "1" ]]; then
echo built json
fi
# GROUP 2
(
cat << EOP
{
"GroupId": "$SGID2",
"IpPermissions": [
{
"IpProtocol": "$PROTOCOL",
"FromPort": $FROMPORT,
"ToPort": $TOPORT,
"IpRanges": [
EOP
) > json1
if [[ $DEBUGMODE = "1" ]]; then
echo built json1
fi
rm -f json2
START=51
for (( COUNT=$START; COUNT<=$TOTALIPS; COUNT++ ))
do
iplist=$(nl pingdom-probe-servers.txt | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
(
cat << EOP
{
"CidrIp": "$iplist/32"
},
EOP
) >> json2
done
if [[ $DEBUGMODE = "1" ]]; then
echo built json2
fi
# Remove the last comma to close JSON array
cat json2 | sed '$ s/.$//' > json3
if [[ $DEBUGMODE = "1" ]]; then
echo built json3
fi
(
cat << 'EOP'
]
}
]
}
EOP
) > json4
if [[ $DEBUGMODE = "1" ]]; then
echo built json4
fi
cat json1 json3 json4 > json6
if [[ $DEBUGMODE = "1" ]]; then
echo built json6
fi
}
# Builds the JSON for 101-150 rules
function buildJSON100(){
(
cat << EOP
{
"GroupId": "$SGID1",
"IpPermissions": [
{
"IpProtocol": "$PROTOCOL",
"FromPort": $FROMPORT,
"ToPort": $TOPORT,
"IpRanges": [
EOP
) > json1
if [[ $DEBUGMODE = "1" ]]; then
echo built json1
fi
rm -f json2
START=1
for (( COUNT=$START; COUNT<=50; COUNT++ ))
do
iplist=$(nl pingdom-probe-servers.txt | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
(
cat << EOP
{
"CidrIp": "$iplist/32"
},
EOP
) >> json2
done
if [[ $DEBUGMODE = "1" ]]; then
echo built json2
fi
# Remove the last comma to close JSON array
cat json2 | sed '$ s/.$//' > json3
if [[ $DEBUGMODE = "1" ]]; then
echo built json3
fi
(
cat << 'EOP'
]
}
]
}
EOP
) > json4
if [[ $DEBUGMODE = "1" ]]; then
echo built json4
fi
cat json1 json3 json4 > json
if [[ $DEBUGMODE = "1" ]]; then
echo built json
fi
# GROUP 2
(
cat << EOP
{
"GroupId": "$SGID2",
"IpPermissions": [
{
"IpProtocol": "$PROTOCOL",
"FromPort": $FROMPORT,
"ToPort": $TOPORT,
"IpRanges": [
EOP
) > json1
if [[ $DEBUGMODE = "1" ]]; then
echo built json1
fi
rm -f json2
START=51
for (( COUNT=$START; COUNT<=100; COUNT++ ))
do
iplist=$(nl pingdom-probe-servers.txt | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
(
cat << EOP
{
"CidrIp": "$iplist/32"
},
EOP
) >> json2
done
if [[ $DEBUGMODE = "1" ]]; then
echo built json2
fi
# Remove the last comma to close JSON array
cat json2 | sed '$ s/.$//' > json3
if [[ $DEBUGMODE = "1" ]]; then
echo built json3
fi
(
cat << 'EOP'
]
}
]
}
EOP
) > json4
if [[ $DEBUGMODE = "1" ]]; then
echo built json4
fi
cat json1 json3 json4 > json6
if [[ $DEBUGMODE = "1" ]]; then
echo built json6
fi
# GROUP 3
(
cat << EOP
{
"GroupId": "$SGID3",
"IpPermissions": [
{
"IpProtocol": "$PROTOCOL",
"FromPort": $FROMPORT,
"ToPort": $TOPORT,
"IpRanges": [
EOP
) > json1
if [[ $DEBUGMODE = "1" ]]; then
echo built json1
fi
rm -f json2
START=101
for (( COUNT=$START; COUNT<=$TOTALIPS; COUNT++ ))
do
iplist=$(nl pingdom-probe-servers.txt | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
(
cat << EOP
{
"CidrIp": "$iplist/32"
},
EOP
) >> json2
done
if [[ $DEBUGMODE = "1" ]]; then
echo built json2
fi
# Remove the last comma to close JSON array
cat json2 | sed '$ s/.$//' > json3
if [[ $DEBUGMODE = "1" ]]; then
echo built json3
fi
(
cat << 'EOP'
]
}
]
}
EOP
) > json4
if [[ $DEBUGMODE = "1" ]]; then
echo built json4
fi
cat json1 json3 json4 > json7
if [[ $DEBUGMODE = "1" ]]; then
echo built json7
fi
}
# Request ingress authorization to a security group from JSON file
function AuthorizeSecurityGroupIngress(){
if ! [ -f json ]; then
fail "Error building JSON."
else
json=$(cat json)
fi
AUTHORIZE=$(aws ec2 authorize-security-group-ingress --cli-input-json "$json" --profile $profile 2>&1)
if echo $AUTHORIZE | grep -q error; then
fail "$AUTHORIZE"
fi
}
# Create AWS VPC Security Groups
# Create one group with 50 rules or less
function group0(){
createGroup
echo
buildJSON0
AuthorizeSecurityGroupIngress
completed
}
# Create multiple groups for 51-100 rules
function group50(){
# Set Variables for Group #1
FIRSTGROUPNAME="$GROUPNAME 1"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTGROUPNAME: "$FIRSTGROUPNAME
fi
FIRSTDESCR="$DESCRIPTION 1-50"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTDESCR: "$FIRSTDESCR
fi
echo
HorizontalRule
echo "Creating Security Group: "$FIRSTGROUPNAME
SGID1=$(aws ec2 create-security-group --group-name "$FIRSTGROUPNAME" --description "$FIRSTDESCR" --vpc-id $VPCID --profile $profile 2>&1 | jq '.GroupId' | cut -d \" -f2)
if echo $SGID1 | grep -q "error"; then
fail "$SGID1"
fi
echo "Security Group ID:" $SGID1
TAG=$(aws ec2 create-tags --resources $SGID1 --tags Key=Name,Value="$FIRSTGROUPNAME" --profile $profile 2>&1)
if echo $TAG | grep -q "error"; then
fail "$TAG"
fi
HorizontalRule
echo
# Set Variables for Group #2
SECONDGROUPNAME="$GROUPNAME 2"
SECONDDESCR="$DESCRIPTION 51-$TOTALIPS"
echo
HorizontalRule
echo "Creating Security Group: "$SECONDGROUPNAME
SGID2=$(aws ec2 create-security-group --group-name "$SECONDGROUPNAME" --description "$SECONDDESCR" --vpc-id $VPCID --profile $profile 2>&1 | jq '.GroupId' | cut -d \" -f2)
if echo $SGID2 | grep -q "error"; then
fail "$SGID2"
fi
echo "Security Group ID:" $SGID2
TAG=$(aws ec2 create-tags --resources $SGID2 --tags Key=Name,Value="$SECONDGROUPNAME" --profile $profile 2>&1)
if echo $TAG | grep -q "error"; then
fail "$TAG"
fi
HorizontalRule
echo
buildJSON50
AuthorizeSecurityGroupIngress
cp json6 json
AuthorizeSecurityGroupIngress
completed
}
# Create multiple groups for 101-150 rules
function group100(){
# Set Variables for Group #1
FIRSTGROUPNAME="$GROUPNAME 1"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTGROUPNAME: "$FIRSTGROUPNAME
fi
FIRSTDESCR="$DESCRIPTION 1-50"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTDESCR: "$FIRSTDESCR
fi
echo
HorizontalRule
echo "Creating Security Group: "$FIRSTGROUPNAME
SGID1=$(aws ec2 create-security-group --group-name "$FIRSTGROUPNAME" --description "$FIRSTDESCR" --vpc-id $VPCID --profile $profile 2>&1 | jq '.GroupId' | cut -d \" -f2)
if echo $SGID1 | grep -q "error"; then
fail "$SGID1"
fi
echo "Security Group ID:" $SGID1
TAG=$(aws ec2 create-tags --resources $SGID1 --tags Key=Name,Value="$FIRSTGROUPNAME" --profile $profile 2>&1)
if echo $TAG | grep -q "error"; then
fail "$TAG"
fi
HorizontalRule
echo
# Set Variables for Group #2
SECONDGROUPNAME="$GROUPNAME 2"
SECONDDESCR="$DESCRIPTION 51-100"
echo
HorizontalRule
echo "Creating Security Group: "$SECONDGROUPNAME
SGID2=$(aws ec2 create-security-group --group-name "$SECONDGROUPNAME" --description "$SECONDDESCR" --vpc-id $VPCID --profile $profile 2>&1 | jq '.GroupId' | cut -d \" -f2)
if echo $SGID2 | grep -q "error"; then
fail "$SGID2"
fi
echo "Security Group ID:" $SGID2
TAG=$(aws ec2 create-tags --resources $SGID2 --tags Key=Name,Value="$SECONDGROUPNAME" --profile $profile 2>&1)
if echo $TAG | grep -q "error"; then
fail "$TAG"
fi
HorizontalRule
echo
# Set Variables for Group #3
THIRDGROUPNAME="$GROUPNAME 3"
THIRDDESCR="$DESCRIPTION 101-$TOTALIPS"
echo
HorizontalRule
echo "Creating Security Group: "$THIRDGROUPNAME
SGID3=$(aws ec2 create-security-group --group-name "$THIRDGROUPNAME" --description "$THIRDDESCR" --vpc-id $VPCID --profile $profile 2>&1 | jq '.GroupId' | cut -d \" -f2)
if echo $SGID3 | grep -q "error"; then
fail "$SGID3"
fi
echo "Security Group ID:" $SGID3
TAG=$(aws ec2 create-tags --resources $SGID3 --tags Key=Name,Value="$THIRDGROUPNAME" --profile $profile 2>&1)
if echo $TAG | grep -q "error"; then
fail "$TAG"
fi
HorizontalRule
echo
buildJSON100
AuthorizeSecurityGroupIngress
cp json6 json
AuthorizeSecurityGroupIngress
cp json7 json
AuthorizeSecurityGroupIngress
completed
}
# Validate VPC ID
function validateVPCID(){
if [ "$VPCID" = "YOUR-VPC-ID-HERE" ] || [ -z "$VPCID" ]; then
# Count number of VPCs
DESCRIBEVPCS=$(aws ec2 describe-vpcs --profile $profile 2>&1)
if echo $DESCRIBEVPCS | egrep -q "Error|error|not"; then
fail "$DESCRIBEVPCS"
fi
NUMVPCS=$(echo $DESCRIBEVPCS | jq '.Vpcs | length')
if echo $NUMVPCS | egrep -q "Error|error|not|invalid"; then
fail "$NUMVPCS"
fi
# If only one VPC, use that ID
if [ "$NUMVPCS" -eq "1" ]; then
VPCID=$(echo "$DESCRIBEVPCS" | jq '.Vpcs | .[] | .VpcId' | cut -d \" -f2)
else
FOUNDVPCS=$(aws ec2 describe-vpcs --profile $profile 2>&1 | jq '.Vpcs | .[] | .VpcId')
if echo $FOUNDVPCS | egrep -q "Error|error|not|invalid"; then
fail "$FOUNDVPCS"
fi
echo "Found VPCs:" $FOUNDVPCS
echo
read -r -p "Please specify VPC ID (ex. vpc-12345678): " VPCID
if [ -z "$VPCID" ]; then
fail "Must specify a valid VPC ID."
fi
fi
fi
CHECKVPC=$(aws ec2 describe-vpcs --vpc-ids "$VPCID" --profile $profile 2>&1)
# Test for error
if ! echo "$CHECKVPC" | grep -q "available"; then
fail $CHECKVPC
else
tput setaf 2; echo "VPC ID Validated" && tput sgr0
fi
}
# Confirm the group with this name does not already exist in the VPC
function validateGroupName(){
validateGroupName=$(aws ec2 describe-security-groups --filters Name=vpc-id,Values="$VPCID" --output=json --profile $profile 2>&1 | jq '.SecurityGroups | .[] | .GroupName')
if echo "$validateGroupName" | egrep -q "\b$GROUPNAME\b|\b$GROUPNAME 1\b"; then
echo Warning: Security Group $(echo "$validateGroupName" | egrep "\b$GROUPNAME\b|\b$GROUPNAME 1\b") already exists in specified VPC.
fi
}
# Run the script and call functions
# Check for required applications
check_command "jq"
check_command "wget"
check_command "perl"
validateVPCID
echo
HorizontalRule
echo "This script will save a list of current Pingdom probe server IPs in the file pingdom-probe-servers.txt"
echo "then create one or more AWS VPC Security Groups with rules to allow access to each IP in the port range specified."
HorizontalRule
echo
tput setaf 1; echo "Please verify all settings before continuing..." && tput sgr0
echo
echo "AWS CLI Profile Name: "$profile
echo "Group Name: "$GROUPNAME
echo "Group Description: "$DESCRIPTION
echo "VPC ID: "$VPCID
echo "Protocol: "$PROTOCOL
if [ "$FROMPORT" -eq "$TOPORT" ]; then
echo "Port: "$FROMPORT
else
echo "Port Range: "$FROMPORT-$TOPORT
fi
echo
pause
echo
validateGroupName
probeIPs
# Determine number of security groups needed since default AWS limit is 50 rules per group
# https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Appendix_Limits.html#vpc-limits-security-groups
# Create one group with 50 rules or less
if [ "$TOTALIPS" -gt "0" ]; then
if [ "$TOTALIPS" -lt "51" ]; then
group0
fi
fi
# Create multiple groups for 51-100 rules
if [ "$TOTALIPS" -gt "50" ]; then
if [ "$TOTALIPS" -lt "101" ]; then
group50
fi
fi
# Create multiple groups for 101-150 rules
if [ "$TOTALIPS" -gt "100" ]; then
if [ "$TOTALIPS" -lt "151" ]; then
group100
fi
fi
# More than 150 rules not yet supported
if [ "$TOTALIPS" -gt "150" ]; then
fail "Greater than 150 IPs not yet supported."
fi
# Cleanup temp JSON files
rm -f json json1 json2 json3 json4 json5 json6 json7