This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscheduler.py
359 lines (319 loc) · 15 KB
/
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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
"""
(Description)
Created on Apr 16, 2015
"""
from ce1sus.helpers.common.config import Configuration
from datetime import datetime
from os.path import dirname, abspath
from ce1sus.common.checks import is_event_owner, is_event_viewable_user, is_event_viewable_group
from ce1sus.controllers.admin.mails import MailController
from ce1sus.controllers.admin.syncserver import SyncServerController
from ce1sus.controllers.admin.user import UserController
from ce1sus.controllers.base import ControllerNothingFoundException, ControllerException
from ce1sus.controllers.common.process import ProcessController
from ce1sus.controllers.events.event import EventController
from ce1sus.db.brokers.permissions.group import GroupBroker
from ce1sus.db.classes.processitem import ProcessType
from ce1sus.db.classes.report import Report, Reference
from ce1sus.db.common.broker import BrokerException
from ce1sus.db.common.session import SessionManager
from ce1sus.mappers.misp.mispce1sus import MispConverter, MispConverterException
from ce1sus.views.web.adapters.ce1susadapter import Ce1susAdapterException, Ce1susAdapter, Ce1susAdapterNothingFoundException
from ce1sus.views.web.adapters.misp.misp import MISPAdapter, MISPAdapterException
__author__ = 'Weber Jean-Paul'
__email__ = '[email protected]'
__copyright__ = 'Copyright 2013-2014, GOVCERT Luxembourg'
__license__ = 'GPL v3+'
class SchedulerException(Exception):
pass
class Scheduler(object):
def __init__(self, config):
self.config = config
self.session_manager = SessionManager(config)
directconnection = self.session_manager.connector.get_direct_session()
self.process_controller = ProcessController(config, directconnection)
self.server_controller = SyncServerController(config, directconnection)
self.misp_converter = MispConverter(config, None, None, None, directconnection)
dump = config.get('MISPAdapter', 'dump', False)
file_loc = config.get('MISPAdapter', 'file', None)
self.misp_converter.dump = dump
self.misp_converter.file_location = file_loc
self.misp_ctrl = MISPAdapter(config, directconnection)
user_uuid = config.get('ce1sus', 'maintenaceuseruuid', None)
self.user_controller = UserController(config, directconnection)
self.event_controller = EventController(config, directconnection)
self.group_broker = self.user_controller.broker_factory(GroupBroker)
self.mail_controller = MailController(config, directconnection)
self.ce1sus_adapter = Ce1susAdapter(config, directconnection)
if None:
raise SchedulerException('maintenaceuseruuid was not defined in config')
try:
self.user = self.user_controller.get_user_by_uuid(user_uuid)
except ControllerNothingFoundException:
raise SchedulerException('Cannot find maintenance user with uuid {0}'.format(user_uuid))
except ControllerException as error:
raise SchedulerException(error)
def __publish_event(self, item, event):
# server publishing
if item.server_details:
server_details = item.server_details
else:
raise SchedulerException('Server could not be defined')
if server_details.type == 'MISP':
self.__push_misp(item, event)
elif server_details.type == 'Ce1sus':
self.__push_ce1sus(item, event)
else:
raise SchedulerException('Server type {0} is unkown'.format(server_details.type))
def __push_ce1sus(self, item, event, dologin=True):
try:
self.ce1sus_adapter.server_details = item.server_details
if dologin:
self.ce1sus_adapter.login()
rem_event = self.ce1sus_adapter.get_event_by_uuid(event.uuid, False, False)
# check if server has the event already
if rem_event:
# update the event
self.ce1sus_adapter.update_event(event, True, True)
else:
# insert the event
self.ce1sus_adapter.insert_event(event, True, True)
except Ce1susAdapterException as error:
raise SchedulerException(error)
try:
if dologin:
self.ce1sus_adapter.logout()
except Ce1susAdapterException as error:
raise SchedulerException(error)
def __push_misp(self, item, event):
# set the parameters for misp
self.misp_converter.api_key = item.server_details.user.api_key
self.misp_converter.api_url = item.server_details.baseurl
self.misp_converter.tag = item.server_details.name
# cehck if event can be seen else ignore, normally should not happen
if not self.misp_ctrl.is_event_viewable(event, item.server_details.user):
# Ignore push
return True
# use the other function as it filters unwanted stuff out
event_xml = self.misp_ctrl.make_misp_xml(event, item.server_details.user)
try:
self.misp_converter.push_event(event_xml)
self.process_controller.process_finished_success(item, self.user)
except MispConverterException as error:
# TODO Log
self.process_controller.process_finished_in_error(item, self.user)
raise SchedulerException('Error during push of event {0} on server with url {1} with error {2}'.format(event.uuid, item.server_details.baseurl, error))
def __publish(self, event_uuid, item_array):
event = self.event_controller.get_event_by_uuid(event_uuid)
item_publish = False
for item in item_array:
try:
if item.server_details:
# do the sync only for this server
self.__publish_event(item, event)
else:
# it is to send emails
self.__send_mails(event, item.type_)
# set event as published only if it is the furst publication
if item.type_ in [ProcessType.PUBLISH, ProcessType.PUBLISH_UPDATE]:
item_publish = True
# remove item from queue
self.process_controller.process_finished_success(item, self.user)
except (ControllerException, BrokerException) as error:
self.process_controller.process_finished_in_error(item, self.user)
# only update the publish date once
if item_publish:
try:
event.last_publish_date = datetime.utcnow()
self.event_controller.update_event(self.user, event, True, True)
except (ControllerException, BrokerException) as error:
raise SchedulerException(error)
def __send_mails(self, event, type_):
# send mails only if plugin is enabled
if self.mail_controller.mail_handler:
try:
seen_mails = list()
# send mails for the users who wants them
users = self.user_controller.get_all_notifiable_users()
for user in users:
# prevent double emails
if user.email in seen_mails:
continue
else:
# only send mails to users who can see the event!!!
if is_event_viewable_user(event, user):
if user.gpg_key:
seen_mails.append(user.email)
self.__send_mail(type_, event, user, None, True)
else:
# only send email when white
if event.tlp_level_id >= 3:
seen_mails.append(user.email)
self.__send_mail(type_, event, user, None, False)
# send the mails for the groups which want them
groups = self.group_broker.get_all_notifiable_groups()
for group in groups:
if group.email in seen_mails:
continue
else:
if is_event_viewable_group(event, group):
# only send mails to users who can see the event!!!
seen_mails.append(group.email)
if group.gpg_key:
self.__send_mail(type_, event, None, group, True)
else:
# only send email when white
if event.tlp_level_id >= 3:
self.__send_mail(type_, event, None, group, False)
except (ControllerException, BrokerException) as error:
raise SchedulerException(error)
def __get_mail(self, event, type_, user, group):
if type_ in [ProcessType.PUBLISH, ProcessType.REPUBLISH]:
return self.mail_controller.get_publication_mail(event, user, group)
elif type_ in [ProcessType.PUBLISH_UPDATE, ProcessType.REPUBLISH_UPDATE]:
return self.mail_controller.get_publication_update_mail(event, user, group)
elif type_ == ProcessType.PROPOSAL:
return self.mail_controller.get_proposal_mail(event, user, group)
else:
raise SchedulerException(u'{0} is undefined'.format(type_))
def __send_mail(self, type_, event, user, group, encrypt):
# determine if it is an update
mail = self.__get_mail(event, type_, user, group)
mail.encrypt = encrypt
# send mail
self.mail_controller.send_mail(mail)
def __pull(self, item):
if item.server_details:
# do the sync only for this server
if item.server_details.type == 'MISP':
# set the parameters for misp
self.misp_converter.api_key = item.server_details.user.api_key
self.misp_converter.api_url = item.server_details.baseurl
self.misp_converter.tag = item.server_details.name
# fetch one event from misp
misp_event_xml = self.misp_converter.get_xml_event(item.event_uuid)
# make insert/merge
try:
self.misp_ctrl.ins_merg_event(item.server_details, misp_event_xml, item.server_details.user, self.user)
self.process_controller.process_finished_success(item, self.user)
except MISPAdapterException as error:
raise SchedulerException(error)
# TODO dump xml or log it in browser
elif item.server_details.type.lower() == 'ce1sus':
self.ce1sus_adapter.server_details = item.server_details
self.ce1sus_adapter.login()
rem_json = self.ce1sus_adapter.get_event_by_uuid(item.event_uuid, True, True, True, True)
# check if event exists
try:
event = self.event_controller.get_event_by_uuid(item.event_uuid)
# merge the event fetched
owner = is_event_owner(event, item.server_details.user)
event = self.ce1sus_adapter.assembler.update_event(event, rem_json, item.server_details.user, owner, True)
self.__insert_provenance_report(event, item.server_details)
self.event_controller.update_event(self.user, event, True, True)
except ControllerNothingFoundException:
event = self.ce1sus_adapter.assembler.assemble_event(rem_json, item.server_details.user, True, True, False)
event.properties.is_validated = False
self.__insert_provenance_report(event, item.server_details)
self.event_controller.insert_event(self.user, event, True, True)
except ControllerException as error:
self.ce1sus_adapter.logout()
raise SchedulerException(error)
self.ce1sus_adapter.logout()
self.process_controller.process_finished_success(item, self.user)
pass
else:
raise SchedulerException('Server type {0} is unkown'.format(item.server_details.type))
else:
# do the sync for all servers which are pull servers
pass
def __insert_provenance_report(self, event, server_details):
report = Report()
report.event = event
self.ce1sus_adapter.set_extended_logging(report, server_details.user, server_details.user.group, True)
reference = Reference()
definition = self.misp_converter.reference_definitions_broker.get_by_uuid('dee2aa50-874e-4a92-9fd0-441171e76585')
reference.definition = definition
reference.value = '{0}/#/events/event/{1}'.format(server_details.baseurl, event.uuid)
report.references.append(reference)
event.reports.append(report)
self.ce1sus_adapter.set_extended_logging(reference, server_details.user, server_details.user.group, True)
def __push(self, item):
if item.server_details:
# do the sync only for this server
if item.server_details.type == 'MISP':
event = self.event_controller.get_event_by_uuid(item.event_uuid)
self.__push_misp(item, event)
elif item.server_details.type == 'Ce1sus':
self.ce1sus_adapter.server_details = item.server_details
self.ce1sus_adapter.login()
event = self.event_controller.get_event_by_uuid(item.event_uuid)
try:
try:
rem_event = self.ce1sus_adapter.get_event_by_uuid(item.event_uuid, False, False, False, False)
self.__insert_provenance_report(event, item.server_details)
owner = is_event_owner(event, item.server_details.user)
event = self.ce1sus_adapter.assembler.update_event(event, rem_event, item.server_details.user, owner, True)
self.__insert_provenance_report(event, item.server_details)
self.ce1sus_adapter.update_event(event, True, True)
except Ce1susAdapterNothingFoundException:
event.properties.validated = False
self.__insert_provenance_report(event, item.server_details)
self.ce1sus_adapter.insert_event(event, True, True)
self.process_controller.process_finished_success(item, item.server_details.user)
except (BrokerException, ControllerException, Ce1susAdapterException) as error:
raise SchedulerException(error)
finally:
self.ce1sus_adapter.logout()
else:
raise SchedulerException('Server type {0} is unkown'.format(item.server_details.type))
else:
# do the sync for all servers which are push servers
pass
def process(self):
# get all items
items = self.process_controller.get_scheduled_process_items(self.user)
# sort out publications
publications = list()
remaining = list()
for item in items:
self.process_controller.process_task(item, self.user)
if item.type_ in [ProcessType.PUBLISH, ProcessType.PUBLISH_UPDATE, ProcessType.REPUBLISH, ProcessType.REPUBLISH_UPDATE]:
publications.append(item)
else:
remaining.append(item)
for item in remaining:
try:
if item.type_ == ProcessType.PULL:
self.__pull(item)
elif item.type_ == ProcessType.PUSH:
self.__push(item)
elif item.type_ == ProcessType.RELATIONS:
# this is only possible if it is a ce1sus internal server, hence server details = none
if item.server_details:
raise SchedulerException('For relations the server details have to be none')
else:
# TODO: make relations
pass
elif item.type_ == ProcessType.PROPOSAL:
self.__proposal(item)
except SchedulerException:
self.process_controller.process_finished_in_error(item, self.user)
grouped_publications = dict()
for item in publications:
items = grouped_publications.get(item.event_uuid, None)
if items is None:
grouped_publications[item.event_uuid] = list()
grouped_publications[item.event_uuid].append(item)
for key, item_array in grouped_publications.iteritems():
try:
self.__publish(key, item_array)
except SchedulerException:
self.process_controller.process_finished_in_error(item, self.user)
if __name__ == '__main__':
basePath = dirname(abspath(__file__))
ce1susConfigFile = basePath + '/config/ce1sus.conf'
config = Configuration(ce1susConfigFile)
s = Scheduler(config)
s.process()