-
Notifications
You must be signed in to change notification settings - Fork 6
/
api_handlers.py
294 lines (238 loc) · 9.58 KB
/
api_handlers.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
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
import json
import os
import boto3
import ipaddress
import logging
import math
from boto3.dynamodb.conditions import Key, Attr
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def check_conflict(event, context):
"""Take in a CIDR block and check for conflicts against all known blocks
to cidr-house-rules
"""
dynamodb = boto3.resource('dynamodb')
cidr_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_CIDRS'])
input_cidr = event['queryStringParameters']['cidr']
cidrs = cidr_table.scan()['Items']
try:
ipaddress.ip_network(input_cidr)
except ValueError:
return _return_422('Invalid CIDR input')
for cidr in cidrs:
compare_input_cidr = ipaddress.ip_network(input_cidr)
known_cidr = ipaddress.ip_network(cidr['cidr'])
if compare_input_cidr.overlaps(known_cidr):
return _return_200(
f'''*** Warning, CIDR overlaps with another AWS acct ***
Account: {cidr['AccountID']}
Region: {cidr['Region']}
VpcId: {cidr['VpcId']}
CIDR: {cidr['cidr']}
''')
else:
return _return_200('OK, no CIDR conflicts')
def add_account(event, context):
dynamodb = boto3.resource('dynamodb')
accounts_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_ACCOUNTS'])
input_account = event['queryStringParameters']['account']
input_team = event['queryStringParameters']['team']
try:
response = accounts_table.put_item(
Item={
'id': input_account,
'team': input_team
})
return _return_200("OK")
except ValueError:
_return_422('Invalid input')
def get_number_of_nat_gateway_pages(event, context):
"""Get the number of pages of NAT gateways with given number of results per
page with ?results_per_page parameter"""
logger.info(f'DEBUG: {event}')
dynamodb = boto3.resource('dynamodb')
nat_gateways_table = dynamodb.Table(
os.environ['DYNAMODB_TABLE_NAT_GATEWAYS'])
try:
response = []
nat_gateways = nat_gateways_table.scan()
for n in nat_gateways['Items']:
response.append(n['PublicIp'] + '/32')
results_per_page = _check_results_per_page(event)
logger.info(f'response: {response}')
pages = math.ceil(len(response) / results_per_page)
logger.info(f'pages: {pages}')
return _return_200(pages)
except ValueError:
_return_422('Invalid input')
def get_nat_gateways_for_all(event, context):
"""Return NAT Gateways for all teams. Optional, pagination with ?page and
?results_per_page URI query parameters"""
logger.info(f'DEBUG: {event}')
dynamodb = boto3.resource('dynamodb')
nat_gateways_table = dynamodb.Table(
os.environ['DYNAMODB_TABLE_NAT_GATEWAYS'])
try:
response = []
nat_gateways = nat_gateways_table.scan()
for n in nat_gateways['Items']:
response.append(n['PublicIp'] + '/32')
results_per_page = _check_results_per_page(event)
if event['queryStringParameters']:
if event['queryStringParameters']['page']:
page = int(event['queryStringParameters']['page'])
logger.info(f'response: {response}')
paged_response = _ip_list_pagination(
response, results_per_page)
logger.info(f'paged_response: {paged_response}')
# formatted_response should be the page requested
formatted_response = _ip_list_formatter(paged_response[0 + page])
logger.info(f'formatted_response: {formatted_response}')
else:
formatted_response = _ip_list_formatter(response)
if not formatted_response:
_no_items_found("NAT Gateway", "All accounts")
return _return_200(formatted_response)
except ValueError:
return _return_422('Invalid input')
def get_nat_gateways_for_team(event, context):
dynamodb = boto3.resource('dynamodb')
accounts_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_ACCOUNTS'])
nat_gateways_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_NAT_GATEWAYS'])
if 'team' in event['queryStringParameters']:
input_teams = [event['queryStringParameters']['team']]
else:
input_teams = event['queryStringParameters']
response = []
logger.info(f'Event: {event}')
if not input_teams:
return _return_422(
'Invalid input. Provide atleast 1 team as query parameter')
try:
for team in input_teams:
accounts = accounts_table.scan()
for a in accounts['Items']:
if a['team'] == team:
account_id = a['id']
nat_gateways = nat_gateways_table.scan()
for n in nat_gateways['Items']:
if n['AccountID'] == account_id:
response.append(n['PublicIp'] + '/32')
formatted_response = _ip_list_formatter(response)
if not formatted_response:
_no_items_found("NAT Gateway", account_id)
logger.info(f'NAT gatways: {formatted_response}')
return _return_200(formatted_response)
except ValueError:
return _return_422('Invalid input')
def get_elbs_for_all(event, context):
dynamodb = boto3.resource('dynamodb')
elbs_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_ELB'])
try:
response = []
elbs = elbs_table.scan()
for elb in elbs['Items']:
response.append(elb['id'])
return _return_200(str(json.dumps(response)))
except ValueError:
return _return_404("Unable to scan elbs table")
def get_eips_for_team(event, context):
dynamodb = boto3.resource('dynamodb')
accounts_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_ACCOUNTS'])
eips_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_EIP'])
input_team = event['queryStringParameters']['team']
try:
accounts = accounts_table.scan()
for a in accounts['Items']:
if a['team'] == input_team:
account_id = a['id']
response = []
eips = eips_table.scan()
for e in eips['Items']:
if e['AccountID'] == account_id:
response.append(e['PublicIp'] + '/32')
return _return_200(str(json.dumps(response)))
except ValueError:
_return_422('Invalid input')
def get_service_endpoints_for_all(event, context):
dynamodb = boto3.resource('dynamodb')
endpoint_services_table = dynamodb.Table(
os.environ['DYNAMODB_TABLE_ENDPOINT_SERVICES'])
try:
response = []
endpoint_services = endpoint_services_table.scan()
for endpoint in endpoint_services['Items']:
response.append(endpoint['ServiceName'])
return _return_200(str(json.dumps(response)))
except ValueError:
_return_404('Unable to scan endpoint_services table')
def get_service_endpoint_for_nlb(event, context):
dynamodb = boto3.resource('dynamodb')
endpoint_services_table = dynamodb.Table(
os.environ['DYNAMODB_TABLE_ENDPOINT_SERVICES'])
nlb_name = event['queryStringParameters']['nlb']
# Expects the NLB to be tagged with a 'Name' key
# Find NLBs that have a "Name" tag and compare with event input nlb_name
try:
response = ""
endpoint_services = endpoint_services_table.scan()
for endpoint in endpoint_services['Items']:
for arn in endpoint['NetworkLoadBalancerArns']:
nlb_tags = endpoint.get('NLBTags', None)
if nlb_tags:
for tags in nlb_tags[arn][0]:
if (tags.get('Key')) == 'Name':
if tags.get('Value') == nlb_name:
response = endpoint['ServiceName']
return _return_200(str(json.dumps(response)))
except ValueError:
_return_404(f'Unable to find NLB with Name tag {nlb_name}')
def _ip_list_formatter(ip_list):
"""Create a repsponse that looks like this:
50.112.204.31/32,50.112.53.175/32,52.34.22.83/32,52.38.146.43/32
"""
formatted_response = (str(ip_list)
.strip("[")
.strip("]")
.replace('\'','')
.replace(" ",""))
return formatted_response
def _ip_list_pagination(ip_list, results_per_page):
"""Return paginated results for list of ips."""
paged_response = [ip_list[i:i+results_per_page]
for i in range(0, len(ip_list), results_per_page)]
return paged_response
def _no_items_found(service, account_id):
"""Return 422 response code when items not found in DynamoDB"""
logger.info(f'No {service} for account: {account_id}')
return {
"statusCode": 422,
"body": f'No {service} found for account: {account_id}'
}
def _check_results_per_page(event):
if event['queryStringParameters']:
if event['queryStringParameters']['results_per_page']:
results_per_page = (
int(event['queryStringParameters']['results_per_page']))
else:
# Default to 50 results per page if parameter not given
results_per_page = 50
return results_per_page
def _return_200(response_body):
"""Return 200 response with provided body message"""
return {
"statusCode": 200,
"body": response_body
}
def _return_404(response_body):
"""Return 404 response with provided body message"""
return {
"statusCode": 404,
"body": response_body
}
def _return_422(response_body):
"""Return 422 response with provided body message"""
return {
"statusCode": 422,
"body": response_body
}