This repository has been archived by the owner on Sep 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 796
/
tasks.py
346 lines (284 loc) · 13.7 KB
/
tasks.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
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
# encoding=utf8
"""
.. module: security_monkey.task_scheduler.tasks
:platform: Unix
:synopsis: Sets up the Celery task scheduling for watching, auditing, and reporting changes in the environment.
.. version:: $$VERSION$$
.. moduleauthor:: Mike Grima <mgrima@netflix.com>
"""
import sys
try:
reload(sys) # Python 2
sys.setdefaultencoding('utf8')
# ^^ Fixes UTF-8 issues
except NameError:
pass # Python 3
import time
import traceback
from security_monkey import app, db, jirasync, sentry
from security_monkey.alerter import Alerter
from security_monkey.datastore import store_exception, clear_old_exceptions, Technology, Account, Item, ItemRevision
from security_monkey.monitors import get_monitors, get_monitors_and_dependencies
from security_monkey.reporter import Reporter
from security_monkey.task_scheduler.util import CELERY, setup
from sqlalchemy.exc import OperationalError, InvalidRequestError, StatementError
@CELERY.task(bind=True, max_retries=3)
def task_account_tech(self, account_name, technology_name):
setup()
app.logger.info("[ ] Executing Celery task for account: {}, technology: {}".format(account_name, technology_name))
time1 = time.time()
# Verify that the account exists (was it deleted? was it renamed?):
if not Account.query.filter(Account.name == account_name).first():
app.logger.error("[X] Account has been removed or renamed: {}. Please restart the scheduler to fix.".format(
account_name
))
return
try:
reporter_logic(account_name, technology_name)
time2 = time.time()
app.logger.info('[@] Run Account for Technology (%s/%s) took %0.1f s' % (account_name,
technology_name, (time2 - time1)))
app.logger.info(
"[+] Completed Celery task for account: {}, technology: {}".format(account_name, technology_name))
except Exception as e:
if sentry:
sentry.captureException()
app.logger.error("[X] Task Account Scheduler Exception ({}/{}): {}".format(account_name, technology_name, e))
app.logger.error(traceback.format_exc())
store_exception("scheduler-exception-on-watch", None, e)
raise self.retry(exc=e)
@CELERY.task(bind=True, max_retries=3)
def task_audit(self, account_name, technology_name):
setup()
app.logger.info("[ ] Executing Celery task to audit changes for Account: {} Technology: {}".format(account_name,
technology_name))
# Verify that the account exists (was it deleted? was it renamed?):
if not Account.query.filter(Account.name == account_name).first():
app.logger.error("[X] Account has been removed or renamed: {}. Please restart the scheduler to fix.".format(
account_name
))
return
try:
audit_changes([account_name], [technology_name], True)
app.logger.info("[+] Completed Celery task for account: {}, technology: {}".format(account_name,
technology_name))
except Exception as e:
if sentry:
sentry.captureException()
app.logger.error("[X] Task Audit Scheduler Exception ({}/{}): {}".format(account_name, technology_name, e))
app.logger.error(traceback.format_exc())
store_exception("scheduler-exception-on-audit", None, e)
self.retry(exc=e)
@CELERY.task()
def clear_expired_exceptions():
app.logger.info("[ ] Clearing out exceptions that have an expired TTL...")
clear_old_exceptions()
app.logger.info("[-] Completed clearing out exceptions that have an expired TTL.")
def fix_orphaned_deletions(account_name, technology_name):
"""
Possible issue with orphaned items. This will check if there are any, and will assume that the item
was deleted. This will create a deletion change record to it.
:param account_name:
:param technology_name:
:return:
"""
# If technology doesn't exist, then create it:
technology = Technology.query.filter(Technology.name == technology_name).first()
if not technology:
technology = Technology(name=technology_name)
db.session.add(technology)
db.session.commit()
app.logger.info("Technology: {} did not exist... created it...".format(technology_name))
account = Account.query.filter(Account.name == account_name).one()
# Query for orphaned items of the given technology/account pair:
orphaned_items = Item.query.filter(Item.account_id == account.id, Item.tech_id == technology.id,
Item.latest_revision_id == None).all() # noqa
if not orphaned_items:
app.logger.info("[@] No orphaned items have been found. (This is good)")
return
# Fix the orphaned items:
for oi in orphaned_items:
app.logger.error("[?] Found an orphaned item: {}. Creating a deletion record for it".format(oi.name))
revision = ItemRevision(active=False, config={})
oi.revisions.append(revision)
db.session.add(revision)
db.session.add(oi)
db.session.commit()
# Update the latest revision id:
db.session.refresh(revision)
oi.latest_revision_id = revision.id
db.session.add(oi)
db.session.commit()
app.logger.info("[-] Created deletion record for item: {}.".format(oi.name))
def reporter_logic(account_name, technology_name):
"""Logic for the run change reporter"""
try:
# Before doing anything... Look for orphaned items for this given technology. If they exist, then delete them:
fix_orphaned_deletions(account_name, technology_name)
# Watch and Audit:
monitors = find_changes(account_name, technology_name)
# Alert:
app.logger.info("[ ] Sending alerts (if applicable) for account: {}, technology: {}".format(account_name,
technology_name))
Alerter(monitors, account=account_name).report()
except (OperationalError, InvalidRequestError, StatementError) as e:
app.logger.exception("[X] Database error processing account %s - technology %s cleaning up session.",
account_name, technology_name)
db.session.remove()
store_exception("scheduler-task-account-tech", None, e)
raise e
def manual_run_change_reporter(accounts):
"""Manual change reporting from the command line"""
app.logger.info("[ ] Executing manual change reporter task...")
try:
for account in accounts:
time1 = time.time()
rep = Reporter(account=account)
for monitor in rep.all_monitors:
if monitor.watcher:
app.logger.info("[ ] Running change finder for "
"account: {} technology: {}".format(account, monitor.watcher.index))
reporter_logic(account, monitor.watcher.index)
time2 = time.time()
app.logger.info('[@] Run Account %s took %0.1f s' % (account, (time2 - time1)))
app.logger.info("[+] Completed manual change reporting.")
except (OperationalError, InvalidRequestError, StatementError) as e:
app.logger.exception("[X] Database error processing cleaning up session.")
db.session.remove()
store_exception("scheduler-run-change-reporter", None, e)
raise e
def manual_run_change_finder(accounts, technologies):
"""Manual change finder"""
app.logger.info("[ ] Executing manual find changes task...")
try:
for account in accounts:
time1 = time.time()
for tech in technologies:
find_changes(account, tech)
time2 = time.time()
app.logger.info('[@] Run Account %s took %0.1f s' % (account, (time2 - time1)))
app.logger.info("[+] Completed manual change finder.")
except (OperationalError, InvalidRequestError, StatementError) as e:
app.logger.exception("[X] Database error processing cleaning up session.")
db.session.remove()
store_exception("scheduler-run-change-reporter", None, e)
raise e
def find_changes(account_name, monitor_name, debug=True):
"""
Runs the watcher and stores the result, re-audits all types to account
for downstream dependencies.
"""
# Before doing anything... Look for orphaned items for this given technology. If they exist, then delete them:
fix_orphaned_deletions(account_name, monitor_name)
monitors = get_monitors(account_name, [monitor_name], debug)
for mon in monitors:
cw = mon.watcher
app.logger.info("[-->] Looking for changes in account: {}, technology: {}".format(account_name, cw.index))
if mon.batch_support:
batch_logic(mon, cw, account_name, debug)
else:
# Just fetch normally...
(items, exception_map) = cw.slurp()
cw.find_changes(current=items, exception_map=exception_map)
cw.save()
# Batched monitors have already been monitored, and they will be skipped over.
audit_changes([account_name], [monitor_name], False, debug)
db.session.close()
return monitors
def audit_changes(accounts, monitor_names, send_report, debug=True, skip_batch=True):
"""
Audits changes in the accounts
:param accounts:
:param monitor_names:
:param send_report:
:param debug:
:param skip_batch:
:return:
"""
for account in accounts:
monitors = get_monitors_and_dependencies(account, monitor_names, debug)
for monitor in monitors:
# Skip batch support monitors... They have already been monitored.
if monitor.batch_support and skip_batch:
continue
app.logger.debug("[-->] Auditing account: {}, technology: {}".format(account, monitor.watcher.index))
_audit_changes(account, monitor.auditors, send_report, debug)
def batch_logic(monitor, current_watcher, account_name, debug):
"""
Performs the batch watcher finding and auditing.
TODO: Investigate how this could, in the future, be set to parallelize the batches.
:param monitor:
:param current_watcher:
:param account_name:
:param debug:
:return:
"""
# Fetch the full list of items that we need to obtain:
_, exception_map = current_watcher.slurp_list()
if len(exception_map) > 0:
# Get the location tuple to collect the region:
location = exception_map.keys()[0]
if len(location) > 2:
region = location[2]
else:
region = "unknown"
app.logger.error("[X] Exceptions have caused nothing to be fetched for {technology}"
"/{account}/{region}..."
" CANNOT CONTINUE FOR THIS WATCHER!".format(technology=current_watcher.i_am_plural,
account=account_name,
region=region))
return
while not current_watcher.done_slurping:
app.logger.debug("[-->] Fetching a batch of {batch} items for {technology}/{account}.".format(
batch=current_watcher.batched_size, technology=current_watcher.i_am_plural, account=account_name
))
(items, exception_map) = current_watcher.slurp()
audit_items = current_watcher.find_changes(current=items, exception_map=exception_map)
_audit_specific_changes(monitor, audit_items, False, debug)
# Delete the items that no longer exist:
app.logger.debug("[-->] Deleting all items for {technology}/{account} that no longer exist.".format(
technology=current_watcher.i_am_plural, account=account_name
))
current_watcher.find_deleted_batch(account_name)
def _audit_changes(account, auditors, send_report, debug=True):
""" Runs auditors on all items """
try:
for au in auditors:
au.items = au.read_previous_items()
au.audit_objects()
au.save_issues()
if send_report:
report = au.create_report()
au.email_report(report)
if jirasync:
app.logger.info('[-->] Syncing {} issues on {} with Jira'.format(au.index, account))
jirasync.sync_issues([account], au.index)
except (OperationalError, InvalidRequestError, StatementError) as e:
app.logger.exception("[X] Database error processing accounts %s, cleaning up session.", account)
db.session.remove()
store_exception("scheduler-audit-changes", None, e)
def _audit_specific_changes(monitor, audit_items, send_report, debug=True):
"""
Runs the auditor on specific items that are passed in.
:param monitor:
:param audit_items:
:param send_report:
:param debug:
:return:
"""
try:
for au in monitor.auditors:
au.items = audit_items
au.audit_objects()
au.save_issues()
if send_report:
report = au.create_report()
au.email_report(report)
if jirasync:
app.logger.info('[-->] Syncing {} issues on {} with Jira'.format(au.index, monitor.watcher.accounts[0]))
jirasync.sync_issues(monitor.watcher.accounts, au.index)
except (OperationalError, InvalidRequestError, StatementError) as e:
app.logger.exception("[X] Database error processing accounts %s, cleaning up session.",
monitor.watcher.accounts[0])
db.session.remove()
store_exception("scheduler-audit-changes", None, e)