forked from swoodford/aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc-sg-import-rules-pingdom.sh
executable file
·824 lines (720 loc) · 19.7 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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#!/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.
# Pingdom currently has 114 IPv4 probe IPs. Due to AWS limits a security group can only have 60 rules,
# Therefore multiple groups will be needed to contain all IPs for Pingdom probes.
# (https://docs.aws.amazon.com/vpc/latest/userguide/amazon-vpc-limits.html#vpc-limits-security-groups)
# If security groups have already been created for Pingdom, the script will remove all the IP rules and
# add new rules to the same groups so they are not deleted and groups are still assigned to your resources.
# This script currently supports up to 180 probe IPs (three security groups).
# 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(){
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
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(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function probeIPs"
fi
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 Groups
function createGroups(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function createGroups"
echo var1: "$1"
echo var2: "$2"
fi
echo
HorizontalRule
echo "Creating Security Group: "$1
SGID=$(aws ec2 create-security-group --group-name "$1" --description "$2" --vpc-id $VPCID --profile $profile 2>&1 | jq '.GroupId' | cut -d \" -f2)
if [ ! $? -eq 0 ]; then
fail "$SGID"
fi
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="$1" --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$TAG"
fi
if echo $TAG | grep -q "error"; then
fail "$TAG"
fi
HorizontalRule
echo
}
# Builds the JSON for 61-120 rules
function buildJSON120(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function buildJSON120"
fi
(
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<=60; COUNT++ ))
do
iplist=$(nl pingdom-probe-servers.txt | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
# if [[ $DEBUGMODE = "1" ]]; then
# echo iplist:
# echo
# echo "$iplist"
# echo
# # pause
# fi
(
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=61
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 121-180 rules
function buildJSON180(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function buildJSON100"
fi
(
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<=60; 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=61
for (( COUNT=$START; COUNT<=120; 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=121
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 [[ $DEBUGMODE = "1" ]]; then
echo "function AuthorizeSecurityGroupIngress"
fi
if ! [ -f json ]; then
fail "Error building JSON."
else
json=$(cat json)
fi
HorizontalRule
echo "Adding rules to security groups..."
HorizontalRule
echo
AUTHORIZE=$(aws ec2 authorize-security-group-ingress --cli-input-json "$json" --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$AUTHORIZE"
fi
if echo $AUTHORIZE | grep -q error; then
fail "$AUTHORIZE"
fi
}
# Create AWS VPC Security Groups
# Create multiple groups for 61-120 rules
function group120(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function group120"
fi
if ! [[ "$GroupsAlreadyExist" -eq "1" ]]; then
if [[ $DEBUGMODE = "1" ]]; then
echo "Groups Do Not Already Exist..."
fi
# Set Variables for Group #1
FIRSTGROUPNAME="$GROUPNAME 1"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTGROUPNAME: "$FIRSTGROUPNAME
fi
FIRSTDESCR="$DESCRIPTION 1-60"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTDESCR: "$FIRSTDESCR
fi
createGroups "$FIRSTGROUPNAME" "$FIRSTDESCR"
SGID1="$SGID"
# Set Variables for Group #2
SECONDGROUPNAME="$GROUPNAME 2"
SECONDDESCR="$DESCRIPTION 61-$TOTALIPS"
createGroups "$SECONDGROUPNAME" "$SECONDDESCR"
SGID2="$SGID"
fi
buildJSON120
AuthorizeSecurityGroupIngress
cp json6 json
AuthorizeSecurityGroupIngress
completed
}
# Create multiple groups for 121-180 rules
function group180(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function group100"
fi
if ! [[ "$GroupsAlreadyExist" -eq "1" ]]; then
if [[ $DEBUGMODE = "1" ]]; then
echo "Groups Do Not Already Exist..."
fi
# Set Variables for Group #1
FIRSTGROUPNAME="$GROUPNAME 1"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTGROUPNAME: "$FIRSTGROUPNAME
fi
FIRSTDESCR="$DESCRIPTION 1-60"
if [[ $DEBUGMODE = "1" ]]; then
echo "FIRSTDESCR: "$FIRSTDESCR
fi
createGroups "$FIRSTGROUPNAME" "$FIRSTDESCR"
SGID1="$SGID"
# Set Variables for Group #2
SECONDGROUPNAME="$GROUPNAME 2"
SECONDDESCR="$DESCRIPTION 61-120"
createGroups "$SECONDGROUPNAME" "$SECONDDESCR"
SGID2="$SGID"
# Set Variables for Group #3
THIRDGROUPNAME="$GROUPNAME 3"
THIRDDESCR="$DESCRIPTION 121-$TOTALIPS"
createGroups "$THIRDGROUPNAME" "$THIRDDESCR"
SGID3="$SGID"
fi
buildJSON180
AuthorizeSecurityGroupIngress
cp json6 json
AuthorizeSecurityGroupIngress
cp json7 json
AuthorizeSecurityGroupIngress
completed
}
# Validate VPC ID
function validateVPCID(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function validateVPCID"
fi
if [ "$VPCID" = "YOUR-VPC-ID-HERE" ] || [ -z "$VPCID" ]; then
# Count number of VPCs
DESCRIBEVPCS=$(aws ec2 describe-vpcs --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$DESCRIBEVPCS"
fi
if echo $DESCRIBEVPCS | egrep -iq "error|not"; then
fail "$DESCRIBEVPCS"
fi
NUMVPCS=$(echo $DESCRIBEVPCS | jq '.Vpcs | length')
if [ ! $? -eq 0 ]; then
fail "$NUMVPCS"
fi
if echo $NUMVPCS | egrep -iq "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)
if [ ! $? -eq 0 ]; then
fail "$VPCID"
fi
else
FOUNDVPCS=$(echo "$DESCRIBEVPCS" | jq '.Vpcs | .[] | .VpcId' | cut -d \" -f2)
if [ ! $? -eq 0 ]; then
fail "$FOUNDVPCS"
fi
if echo $FOUNDVPCS | egrep -iq "error|not|invalid"; then
fail "$FOUNDVPCS"
fi
HorizontalRule
echo "Found VPCs:"
HorizontalRule
# Get VPC Names
for vpcid in $FOUNDVPCS; do
echo $vpcid - Name: $(aws ec2 describe-tags --filters "Name=resource-id,Values=$vpcid" "Name=key,Values=Name" --profile $profile 2>&1 | jq '.Tags | .[] | .Value' | cut -d \" -f2)
done
echo
read -r -p "Please specify VPC ID (ex. vpc-abcd1234): " 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)
if [ ! $? -eq 0 ]; then
fail "$CHECKVPC"
fi
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(){
if [[ $DEBUGMODE = "1" ]]; then
echo "function validateGroupName"
echo "GROUPNAME $GROUPNAME"
fi
validateGroupName=$(aws ec2 describe-security-groups --filters Name=vpc-id,Values="$VPCID" --output=json --profile $profile 2>&1 | jq '.SecurityGroups | .[] | .GroupName')
if [ ! $? -eq 0 ]; then
fail "$validateGroupName"
fi
if echo "$validateGroupName" | egrep -iq "\b$GROUPNAME\b|\b$GROUPNAME 1\b"; then
tput setaf 1; echo Security Group\(s\) $(echo "$validateGroupName" | egrep -i "\b$GROUPNAME\b|\b$GROUPNAME 1\b" | sort) already exist in specified VPC. && tput sgr0
# TODO: This part is too complicated to handle smoothly...
tput setaf 1; read -r -p "Do you want to remove the existing IPs and add new IPs? (y/n) " deleteIPs && tput sgr0
if [[ $deleteIPs =~ ^([yY][eE][sS]|[yY])$ ]]; then
deleteIPs
else
GroupsAlreadyExist="1"
findGroups
# echo "Exiting"
# exit 1
fi
fi
}
# Look up the security group IDs
function findGroups(){
FindGroups=$(aws ec2 describe-security-groups --filters Name=vpc-id,Values="$VPCID" --output=json --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$FindGroups"
fi
if [[ $DEBUGMODE = "1" ]]; then
echo "$FindGroups" | jq .
fi
# Assuming there are exactly 2 groups
SGID1=$(echo "$FindGroups" | jq -r --arg GROUPNAME "$GROUPNAME 1" '.SecurityGroups | .[] | select(.GroupName==$GROUPNAME) | .GroupId' | cut -d \" -f2)
SGID2=$(echo "$FindGroups" | jq -r --arg GROUPNAME "$GROUPNAME 2" '.SecurityGroups | .[] | select(.GroupName==$GROUPNAME) | .GroupId' | cut -d \" -f2)
# SGID3=$(echo "$FindGroups" | jq -r --arg GROUPNAME "$GROUPNAME 3" '.SecurityGroups | .[] | select(.GroupName==$GROUPNAME) | .GroupId' | cut -d \" -f2)
# if [[ -z $SGID1 ]] || [[ -z $SGID2 ]] || [[ -z $SGID3 ]]; then
if [[ -z $SGID1 ]] || [[ -z $SGID2 ]]; then
echo "Unable to lookup $GROUPNAME Security Group IDs."
fi
if [[ $DEBUGMODE = "1" ]]; then
echo DEBUG SGID1: "$SGID1"
echo DEBUG SGID2: "$SGID2"
# echo DEBUG SGID3: "$SGID3"
fi
}
# Remove the existing IPs and add new IPs
function deleteIPs(){
FindGroups=$(aws ec2 describe-security-groups --filters Name=vpc-id,Values="$VPCID" --output=json --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$FindGroups"
fi
if [[ $DEBUGMODE = "1" ]]; then
echo "$FindGroups" | jq .
fi
# Assuming there are exactly 2 groups
SGID1=$(echo "$FindGroups" | jq -r --arg GROUPNAME "$GROUPNAME 1" '.SecurityGroups | .[] | select(.GroupName==$GROUPNAME) | .GroupId' | cut -d \" -f2)
SGID2=$(echo "$FindGroups" | jq -r --arg GROUPNAME "$GROUPNAME 2" '.SecurityGroups | .[] | select(.GroupName==$GROUPNAME) | .GroupId' | cut -d \" -f2)
# SGID3=$(echo "$FindGroups" | jq -r --arg GROUPNAME "$GROUPNAME 3" '.SecurityGroups | .[] | select(.GroupName==$GROUPNAME) | .GroupId' | cut -d \" -f2)
# if [[ -z $SGID1 ]] || [[ -z $SGID2 ]] || [[ -z $SGID3 ]]; then
if [[ -z $SGID1 ]] || [[ -z $SGID2 ]]; then
echo "Unable to lookup $GROUPNAME Security Group IDs."
fi
if [[ $DEBUGMODE = "1" ]]; then
echo DEBUG SGID1: "$SGID1"
echo DEBUG SGID2: "$SGID2"
# echo DEBUG SGID3: "$SGID3"
fi
Group1IPs=$(aws ec2 describe-security-groups --output=json --group-id "$SGID1" --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$Group1IPs"
fi
Group2IPs=$(aws ec2 describe-security-groups --output=json --group-id "$SGID2" --profile $profile 2>&1)
if [ ! $? -eq 0 ]; then
fail "$Group2IPs"
fi
# Group3IPs=$(aws ec2 describe-security-groups --output=json --group-id "$SGID3" --profile $profile 2>&1)
# if [ ! $? -eq 0 ]; then
# fail "$Group3IPs"
# fi
# if [[ -z $Group1IPs ]] || [[ -z $Group2IPs ]] || [[ -z $Group3IPs ]]; then
if [[ -z $Group1IPs ]] || [[ -z $Group2IPs ]]; then
fail "Unable to parse $GROUPNAME Security Groups."
fi
Group1IPs=$(echo "$Group1IPs" | jq '.SecurityGroups | .[] | .IpPermissions')
Group2IPs=$(echo "$Group2IPs" | jq '.SecurityGroups | .[] | .IpPermissions')
# Group3IPs=$(echo "$Group3IPs" | jq '.SecurityGroups | .[] | .IpPermissions')
if [[ $DEBUGMODE = "1" ]]; then
echo DEBUG Group1IPs: "$Group1IPs"
echo DEBUG Group2IPs: "$Group2IPs"
# echo DEBUG Group3IPs: "$Group3IPs"
fi
echo
HorizontalRule
echo "Removing IPs from $GROUPNAME 1, Security Group ID $SGID1"
RemoveGroup1IPs=$(aws ec2 revoke-security-group-ingress --output=json --group-id "$SGID1" --profile $profile --ip-permissions "$Group1IPs" 2>&1)
if [ ! $? -eq 0 ]; then
fail "$RemoveGroup1IPs"
fi
HorizontalRule
echo
HorizontalRule
echo "Removing IPs from $GROUPNAME 2, Security Group ID $SGID2"
RemoveGroup2IPs=$(aws ec2 revoke-security-group-ingress --output=json --group-id "$SGID2" --profile $profile --ip-permissions "$Group2IPs" 2>&1)
if [ ! $? -eq 0 ]; then
fail "$RemoveGroup2IPs"
fi
HorizontalRule
# echo
# HorizontalRule
# echo "Removing IPs from $GROUPNAME 3, Security Group ID $SGID3"
# RemoveGroup3IPs=$(aws ec2 revoke-security-group-ingress --output=json --group-id "$SGID3" --profile $profile --ip-permissions "$Group3IPs" 2>&1)
# if [ ! $? -eq 0 ]; then
# fail "$RemoveGroup3IPs"
# fi
# HorizontalRule
# Set flag so there is no attempt to create the groups again
GroupsAlreadyExist="1"
}
# Run the script and call functions
# Check for required applications
check_command jq wget perl
validateVPCID
echo
HorizontalRule
echo "This script will create or update AWS VPC Security Groups with rules"
echo "to allow access to each Pingdom probe IP in the port range specified."
HorizontalRule
echo
tput setaf 1; echo "Please review 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 60 rules per group
# https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Appendix_Limits.html#vpc-limits-security-groups
# Create one group with 60 rules or less
if [ "$TOTALIPS" -gt "0" ]; then
if [ "$TOTALIPS" -lt "61" ]; then
fail "Support for 60 IPs or fewer has been depreciated."
fi
fi
# Create multiple groups for 61-120 rules
if [ "$TOTALIPS" -gt "60" ]; then
if [ "$TOTALIPS" -lt "121" ]; then
group120
fi
fi
# Create multiple groups for 121-180 rules
if [ "$TOTALIPS" -gt "120" ]; then
if [ "$TOTALIPS" -lt "181" ]; then
group180
fi
fi
# More than 180 rules not yet supported
if [ "$TOTALIPS" -gt "180" ]; then
fail "Greater than 180 IPs not yet supported."
fi
# Cleanup temp JSON files
rm -f json json1 json2 json3 json4 json5 json6 json7