forked from hjacobs/connection-tracker
/
app.py
executable file
·166 lines (135 loc) · 4.87 KB
/
app.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
#!/usr/bin/env python3
import botocore
import collections
import connexion
import flask
import logging
import os
import requests
import scan
import time
import tokens
import uwsgi_metrics
tokens.manage('tok', ['uid'])
# HTTP connection pooling
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
_session = requests.Session()
_session.mount('http://', adapter)
_session.mount('https://', adapter)
requests = _session
def get_health():
return 'OK'
def get_addresses():
scan.update_addresses()
q = flask.request.args.get('q')
return {k: v for k, v in scan.NAMES.items() if not q or (v and q in v)}
def get_endpoints(date=None):
scan.update_accounts()
if not date:
date = flask.request.args.get('date')
res = {}
for account_id, data in scan.ACCOUNTS.items():
for region in data['regions']:
res['/'.join((account_id, region))] = get_endpoints_by_account(account_id, region, date)
return res
def get_endpoints_by_account(account_id, region, date=None):
if not date:
date = flask.request.args.get('date')
endpoints = set()
for row in get_connections_by_account(account_id, region, date):
endpoints.add((row['dest'], row['dest_port']))
return list([{'dest': e[0], 'dest_port': e[1]} for e in endpoints])
def get_time(v):
if v:
return v.isoformat('T') + 'Z'
def get_accounts():
scan.update_accounts()
return {k: {'name': v['name'], 'last_update': get_time(scan.get_last_update(k, v['regions'][0]))}
for k, v in scan.ACCOUNTS.items()}
def get_account_connections(date=None):
if not date:
date = flask.request.args.get('date')
include = set(flask.request.args.get('include', '').split(','))
res = {}
for key, connections in get_connections(date).items():
counter = collections.Counter()
for conn in connections:
if include and conn['source'] in include:
counter[conn['source']] += conn['score']
else:
parts = conn['source'].split('/')
if len(parts) >= 3:
counter['/'.join(parts[:2])] += conn['score']
res[key] = [{'source': k, 'score': v} for k, v in counter.items()]
return res
def get_connections(date=None):
scan.update_accounts()
if not date:
date = flask.request.args.get('date')
res = {}
for account_id, data in scan.ACCOUNTS.items():
for region in data['regions']:
res['/'.join((account_id, region))] = get_connections_by_account(account_id, region, date)
return res
def get_connections_by_account(account_id, region, date=None):
res = []
if not date:
date = flask.request.args.get('date')
for conn, score in scan.get_stored_connections(account_id, region, date):
src, dst = conn.split('->', 1)
dst, dst_port = dst.rsplit(':', 1)
dst_port = int(dst_port)
res.append({'source': src, 'dest': dst, 'dest_port': dst_port, 'score': score})
return res
# run this amount of background jobs in parallel
PARALLEL = 8
def run_update(signum):
if uwsgi.is_locked(signum):
return
uwsgi.lock(signum)
try:
while True:
try:
logging.info('Updating accounts..')
scan.update_accounts()
logging.info('Updating addresses..')
scan.update_addresses()
break
except:
logging.exception('Failed to update accounts/addresses, retrying')
time.sleep(30)
for i in range(100):
try:
for acc in scan.ACCOUNTS:
if hash(acc) % PARALLEL == signum-1:
for region in os.getenv('REGIONS').split(','):
for i in range(3):
try:
scan.update_connections(acc, region)
break
except botocore.exceptions.ClientError:
logging.exception('Client error')
# throttling
time.sleep(60)
except:
logging.exception('Failed to update')
time.sleep(60)
finally:
uwsgi.unlock(signum)
logging.basicConfig(level=logging.INFO)
logging.getLogger('botocore.vendored.requests.packages.urllib3.connectionpool').setLevel(logging.WARNING)
app = connexion.App(__name__)
app.add_api('swagger.yaml')
application = app.app
try:
import uwsgi
for i in range(1, 1 + PARALLEL):
signum = i
uwsgi.register_signal(signum, "", run_update)
uwsgi.add_timer(signum, 10)
# initialization for /metrics endpoint (ZMON support)
uwsgi_metrics.initialize()
except Exception as e:
print(e)
if __name__ == '__main__':
app.run(port=8080)