-
Notifications
You must be signed in to change notification settings - Fork 73
/
aws_sg_summary.py
executable file
·197 lines (177 loc) · 6.43 KB
/
aws_sg_summary.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
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
#!/usr/bin/env python
"""
Python script to print a summary of all SGs in the current account/region,
their rules, and what's in them. Output in markdown.
Should work with python 2.7-3.6. Requires ``boto3``from pypi.
The latest version of this script can be found at:
https://github.com/jantman/misc-scripts/blob/master/aws_sg_summary.py
Copyright 2018 Jason Antman <[email protected]> <http://www.jasonantman.com>
Free for any use provided that patches are submitted back to me.
CHANGELOG:
2018-03-26 Jason Antman <[email protected]>:
- initial version of script
"""
import sys
import argparse
import logging
import re
try:
import boto3
except ImportError:
raise SystemExit("This script requires boto3. Please 'pip install boto3'")
from botocore.exceptions import ClientError
FORMAT = "[%(levelname)s %(filename)s:%(lineno)s - " \
"%(funcName)20s() ] %(message)s"
logging.basicConfig(level=logging.WARNING, format=FORMAT)
logger = logging.getLogger(__name__)
DEFAULT_EGRESS = [
{
'IpProtocol': '-1',
'IpRanges': [{'CidrIp': '0.0.0.0/0'}],
'Ipv6Ranges': [],
'PrefixListIds': [],
'UserIdGroupPairs': []
}
]
class AWSSgSummary:
"""AWS Security Group Summary"""
def __init__(self):
"""connect to AWS API"""
logger.debug("Connecting to AWS API")
self.ec2 = boto3.client('ec2')
self.ec2_res = boto3.resource('ec2')
logger.info("Connected to AWS API")
self.interfaces = {}
self.acct_id = None
self._acct_info()
def _acct_info(self):
try:
iam = boto3.client('iam')
alias = iam.list_account_aliases()['AccountAliases'][0]
except Exception:
alias = ''
sts = boto3.client('sts')
self.acct_id = sts.get_caller_identity()['Account']
print('## %s (%s) %s\n' % (
self.acct_id, alias, sts._client_config.region_name
))
def run(self):
sgs = {}
for sg in self.ec2_res.security_groups.all():
sgs[sg.id] = {
'id': sg.id,
'name': sg.group_name,
'description': sg.description,
'ip_permissions': sg.ip_permissions,
'ip_permissions_egress': sg.ip_permissions_egress,
'vpc_id': sg.vpc_id,
'tags': sg.tags,
'interfaces': []
}
for ni in self.ec2_res.network_interfaces.all():
self.interfaces[ni.id] = {
'id': ni.id,
'description': ni.description,
'vpc_id': ni.vpc_id,
'attachment': ni.attachment,
'interface_type': ni.interface_type,
'groups': ni.groups
}
for sg in ni.groups:
if sg['GroupId'] in sgs:
sgs[sg['GroupId']]['interfaces'].append(ni.id)
else:
logger.warning(
'%s has unknown SG %s', ni.id, sg
)
for sg_id, sg in sgs.items():
self.sg_markdown(sg)
def sg_markdown(self, sg):
print('### %s - %s ("%s") %s\n' % (
sg['id'], sg['name'], sg['description'], sg['vpc_id']
))
if sg['tags'] is not None:
print('Tags:\n')
for t in sorted(sg['tags'], key=lambda x: x['Key']):
print('* "%s": "%s"' % (t['Key'], t['Value']))
print('')
print('#### Ingress\n')
for r in sg['ip_permissions']:
self.sg_rule_markdown(r, 'from')
print('')
print('#### Egress\n')
if sg['ip_permissions_egress'] == DEFAULT_EGRESS:
print('* DEFAULT (allow all egress)')
else:
for r in sg['ip_permissions_egress']:
self.sg_rule_markdown(r, 'to')
print('')
print('#### Network Interfaces\n')
for i in sg['interfaces']:
if self.interfaces[i].get('attachment', None) is None:
ownerid = ''
else:
ownerid = self.interfaces[i].get(
'attachment', {}
).get('InstanceOwnerId', '')
print('* %s - %s (%s)' % (
self.interfaces[i]['id'],
self.interfaces[i].get('description', ''),
ownerid
))
print('')
def sg_rule_markdown(self, rule, direction):
if 'FromPort' not in rule:
rule['FromPort'] = 'ALL'
if 'ToPort' not in rule:
rule['ToPort'] = 'ALL'
to = []
for i in rule.get('IpRanges', []):
if 'Description' in i:
to.append('%s (%s)' % (i['CidrIp'], i['Description']))
else:
to.append(i['CidrIp'])
for i in rule.get('Ipv6Ranges', []):
if 'Description' in i:
to.append('%s (%s)' % (i['CidrIpv6'], i['Description']))
else:
to.append(i['CidrIpv6'])
for i in rule.get('PrefixListIds', []):
if 'Description' in i:
to.append('%s (%s)' % (i['PrefixListId'], i['Description']))
else:
to.append(i['PrefixListId'])
for i in rule.get('UserIdGroupPairs', []):
to.append(self.sg_useridgroup_str(i))
s = '* %s port %s to port %s ' % (
rule['IpProtocol'] if rule['IpProtocol'] != '-1' else 'ALL',
rule['FromPort'] if rule['FromPort'] != '-1' else 'ALL',
rule['ToPort'] if rule['ToPort'] != '-1' else 'ALL'
)
if len(to) == 1:
print(s + direction + ' ' + to[0])
return
s += direction + ':\n'
s += '\n'.join(
[' * %s' % x for x in to]
)
print(s)
def sg_useridgroup_str(self, g):
suffix = ''
if 'Description' in g:
suffix = ' (%s)' % g['Description']
if g['UserId'] == self.acct_id:
return g['GroupId'] + suffix
return '%s/%s%s' % (g['UserId'], g['UserId'], suffix)
def parse_args(argv):
"""parse arguments/options"""
p = argparse.ArgumentParser(description='AWS Security Group Summary')
p.add_argument('-v', '--verbose', dest='verbose', action='store_true',
default=False, help='verbose output.')
args = p.parse_args(argv)
return args
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
if args.verbose:
logger.setLevel(logging.DEBUG)
AWSSgSummary().run()