-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalert_scheduler.py
86 lines (69 loc) · 3.1 KB
/
alert_scheduler.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
import datetime
import json
import os
from security_monkey import app
from security_monkey.task_scheduler.util import CELERY
from security_monkey.common.sts_connect import connect
from security_monkey.export.krampus_alerters import *
from boto.s3.key import Key
def s3connect(account, bucket):
""" s3connect will attempt to connect to an s3 bucket resource.
If the resource does not exist it will attempt to create it
:param account: string the aws account you are connecting to
:param bucket: string the name of the bucket you wish to connect to
:returns: Boolean of connection Status
"""
conn = connect(
account,
's3'
)
if conn.lookup(bucket) is None:
bucket = conn.create_bucket(bucket)
else:
bucket = conn.get_bucket(bucket)
key = Key(bucket)
return conn, bucket, key
def get_s3_key(conn, bucket, key, filename):
""" Return the key contents for a specific s3 object
:param bucket: the bucket to connect to
:param key: the key of the bucket object
:param filename: the file name of the s3 object
:returns: data in the form of a string or Dict.
"""
if bucket.lookup(filename) is None:
newkey = self.bucket.new_key(filename)
newkey.set_contents_from_string(json.dumps(json.loads('{}')))
key.key = filename
tmp = key.get_contents_as_string()
return json.loads(tmp)
@CELERY.task(bind=True, max_retries=3)
def schedule_krampus_alerts(self, actioned_time):
""" Alert accounts about the actions that will be
made to their resources based on the Justice Engine
:param actioned_time: the time that these accounts were found by the Justice Engine
:return: String of the workers completed
"""
conn, bucket, key = s3connect(os.getenv('AWS_ACCOUNT_NAME'), os.getenv('KRAMPUS_BUCKET'))
filename = "{0}.json".format(datetime.datetime.now().strftime('%Y-%m-%d'))
items = get_s3_key(conn, bucket, key, filename)
if items == {}:
app.logger.info("No items to alert on. Closing.")
return "Unable to notify with no items passed to the alert_scheduler"
recent_jobs = filter(lambda item: item['audited_time'] > actioned_time, items)
if recent_jobs == []:
app.logger.info("No new items to alert on. Closing.")
return "No new jobs passed to alerters"
account_mapping = get_s3_key(conn, bucket, key, os.getenv('MAPPING_FILE_NAME'))
if items == {}:
app.logger.info("No information in Justice Engine mapping file. All alerts going to default rooms.")
# The following array contains the class names that you intend to alert with.
enabled_workers = os.getenv('ENABLED_ALERT_HANLDERS').split(',')
actioned = []
for worker in enabled_workers:
if worker in globals().keys():
invoked_worker = globals()[worker]()
invoked_worker.alert(recent_jobs, account_mapping)
actioned.append(worker)
else:
app.logger.error('Can\'t call {0} as method does not exist'.format(worker))
return "worked: {0}".format(", ".join(x for x in actioned))