def status(request, id): """ Status of a probe instance. """ probe = Probe.get_by_id(id) if probe.subtype: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.subtype) else: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.type) probe = my_class.get_by_id(id) if probe is None: return HttpResponseNotFound else: try: response_status = probe.status() if response_status: messages.add_message( request, messages.SUCCESS, "OK probe " + str(probe.name) + " get status successfully") else: messages.add_message(request, messages.ERROR, 'Error during the status') except Exception as e: messages.add_message(request, messages.ERROR, 'Error during the status : ' + str(e)) return render(request, probe.type.lower() + '/index.html', {'probe': probe})
def deploy_rules(request, id): """ Deploy the rules of a probe instance. """ probe = Probe.get_by_id(id) if probe.subtype: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.subtype) else: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.type) probe = my_class.get_by_id(id) if probe is None: return HttpResponseNotFound else: deploy_rules_probe.delay(probe.name) messages.add_message( request, messages.SUCCESS, mark_safe( "Deployed rules launched with succeed. <a href='/admin/home/job/'>View Job</a>" )) return render(request, probe.type.lower() + '/index.html', {'probe': probe})
def update(request, id): """ Update a probe instance. """ probe = Probe.get_by_id(id) if probe.subtype: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.subtype) else: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.type) probe = my_class.get_by_id(id) if probe is None: return HttpResponseNotFound else: try: update_probe.delay(probe.name) except Exception as e: messages.add_message(request, messages.ERROR, 'Error during the update : ' + str(e)) messages.add_message( request, messages.SUCCESS, mark_safe( "Update probe launched with succeed. <a href='/admin/home/job/'>View Job</a>" )) return render(request, probe.type.lower() + '/index.html', {'probe': probe})
def test_create_deploy_rules_task(self): probe = Probe.get_by_id(1) create_deploy_rules_task(probe) periodic_task = PeriodicTask.objects.get( name=probe.name + '_deploy_rules_' + str(probe.scheduled_rules_deployment_crontab)) self.assertEqual(periodic_task.task, 'home.tasks.deploy_rules') self.assertEqual(periodic_task.args, str([probe.name, ]).replace("'", '"'))
def test_create_deploy_rules_task_with_schedule(self): probe = Probe.get_by_id(1) schedule = CrontabSchedule.objects.get(id=1) source = Source.objects.get(id=1) create_deploy_rules_task(probe, schedule, source) periodic_task = PeriodicTask.objects.get(name=probe.name + '_' + source.uri + '_deploy_rules_' + str(schedule)) self.assertEqual(periodic_task.task, 'home.tasks.deploy_rules') self.assertEqual(periodic_task.args, str([probe.name, ]).replace("'", '"'))
def deploy_conf(request, id): """ Deploy the configuration of a probe instance. """ probe = Probe.get_by_id(id) if probe.subtype: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.subtype) else: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.type) probe = my_class.get_by_id(id) if probe is None: return HttpResponseNotFound else: response_test = probe.configuration.test() logger.error(str(response_test)) if probe.secure_deployment: if not response_test['status']: messages.add_message(request, messages.ERROR, 'Error during the test configuration') return render(request, probe.type.lower() + '/index.html', {'probe': probe}) if response_test['status']: messages.add_message(request, messages.SUCCESS, "Test configuration OK") else: messages.add_message( request, messages.ERROR, "Test configuration failed ! " + str(response_test['errors'])) try: response_deploy_conf = probe.deploy_conf() response_restart = probe.restart() if response_deploy_conf['status'] and response_restart['status']: messages.add_message(request, messages.SUCCESS, 'Deployed configuration successfully') elif not response_deploy_conf['status']: messages.add_message( request, messages.ERROR, 'Error during the configuration deployed: ' + str(response_deploy_conf['errors'])) elif not response_restart['status']: messages.add_message( request, messages.ERROR, 'Error during the configuration deployed: ' + str(response_restart['errors'])) except Exception as e: messages.add_message( request, messages.ERROR, 'Error during the configuration deployed : ' + str(e)) return render(request, probe.type.lower() + '/index.html', {'probe': probe})
def test_probe(self): all_probe = Probe.get_all() probe = Probe.get_by_id(1) self.assertEqual(Probe.get_by_name("probe1"), Probe.get_by_id(1)) self.assertEqual(len(all_probe), 1) self.assertEqual(probe.name, "probe1") self.assertEqual(str(probe), "probe1") self.assertEqual(probe.description, "test") probe = Probe.get_by_id(99) self.assertEqual(probe, None) with self.assertRaises(AttributeError): probe.name probe = Probe.get_by_name("probe99") self.assertEqual(probe, None) with self.assertRaises(AttributeError): probe.name with self.assertLogs('home.models', level='DEBUG'): Probe.get_by_id(99) with self.assertLogs('home.models', level='DEBUG'): Probe.get_by_name('probe99') with self.assertRaises(IntegrityError): Probe.objects.create(name="suricata1")
def status(probe_id): probe = Probe.get_by_id(probe_id) if probe is None: # pragma: no cover return { "message": "Error - probe is None - param id not set : " + str(probe_id) } if probe.subtype: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.subtype) else: my_class = getattr( importlib.import_module(probe.type.lower() + ".models"), probe.type) probe = my_class.get_by_id(probe_id) response = probe.status() if 'active (running)' in response: return 'success' else: return 'danger'
def test_reload_probe(self): with self.assertRaises(TypeError): reload_probe(Probe.get_by_id(1).name) self.assertEqual( reload_probe('bad'), {"message": "Error - probe is None - param id not set : " + 'bad'})
def test_deploy_rules(self): with self.assertRaises(TypeError): deploy_rules(Probe.get_by_id(1).name) self.assertEqual( deploy_rules('bad'), {"message": "Error - probe is None - param id not set : " + 'bad'})
def test_create_reload_task(self): create_reload_task(Probe.get_by_id(1)) periodic_task = PeriodicTask.objects.get(name=Probe.get_by_id(1).name + '_reload_task') self.assertEqual(periodic_task.task, 'home.tasks.reload_probe') self.assertEqual(periodic_task.args, str([Probe.get_by_id(1).name, ]).replace("'", '"'))