-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgcopy.py
executable file
·64 lines (51 loc) · 1.99 KB
/
sgcopy.py
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
#!/usr/bin/env python3
import boto3
import argparse
import textwrap
import pyfiglet
banner = pyfiglet.figlet_format("SGCopy")
print(banner)
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
sgcopy copies the rules of one of your security group to another security group.
Please take care of the following points:
1. You must have your aws credentials configured
2. You cannot copy a rule if it involves another security group as it's source/description and that SG is already present in the destination security group
You can ignore the 2nd point if you do not have a SG added in your destination SG as source/destination. If it's still unclear, simply run. It'll give you a clear error so you know what you're doing wrong.
''')
)
parser.add_argument("--id", help="Set this flag if using security group ID instead of name. You can't set outbound rules without using ID", action="store_true")
parser.add_argument("source", help="Source security group name/ID")
parser.add_argument("destination", help="destination security group name/ID")
args = parser.parse_args()
ec2 = boto3.client('ec2')
if args.id:
if (args.source[0:3] != 'sg-') or (args.destination[0:3] != 'sg-'):
exit("Enter a security Group ID when using --id flag")
sg = ec2.describe_security_groups(
GroupIds = [
args.source
]
)
else:
sg = ec2.describe_security_groups(
GroupNames = [
args.source
]
)
inboundRules = sg['SecurityGroups'][0]['IpPermissions']
outboundRules = sg['SecurityGroups'][0]['IpPermissionsEgress']
if args.id:
response_ingress = ec2.authorize_security_group_ingress(
GroupId = args.destination,
IpPermissions = inboundRules
)
response_egress = ec2.authorize_security_group_egress(
GroupId = args.destination,
IpPermissions = outboundRules
)
else:
response_ingress = ec2.authorize_security_group_ingress(
GroupName = args.destination,
IpPermissions = inboundRules
)