def register(name, display_name, category, mimetype_regexp, session): try: probe = Probe.get_by_name(name, session) log.info( "probe %s already registred " "updating parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) probe.display_name = display_name probe.category = category probe.mimetype_regexp = mimetype_regexp probe.online = True probe.update(['category', 'mimetype_regexp', 'online'], session) except IrmaDatabaseResultNotFound: log.info( "register probe %s" " with parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) probe = Probe(name=name, display_name=display_name, category=category, mimetype_regexp=mimetype_regexp, online=True) probe.save(session) return
def register(name, display_name, category, mimetype_regexp, session): try: probe = Probe.get_by_name(name, session) log.info( "probe %s already registred " "updating parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) session.query(Probe)\ .filter_by(id=probe.id)\ .update({'category': category, 'mimetype_regexp': mimetype_regexp, 'online': True, 'display_name': display_name}) except IrmaDatabaseResultNotFound: log.info( "register probe %s" " with parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) probe = Probe(name=name, display_name=display_name, category=category, mimetype_regexp=mimetype_regexp, online=True) session.add(probe) return
def test001___init__(self): probe = Probe(self.name, self.display_name, self.category, self.mimetype_regexp, self.online) self.assertEqual(probe.name, self.name) self.assertEqual(probe.display_name, self.display_name) self.assertEqual(probe.category, self.category) self.assertEqual(probe.mimetype_regexp, self.mimetype_regexp) self.assertEqual(probe.online, self.online)
def create_error_results(probename, error, session): try: probe = Probe.get_by_name(probename, session) display_name = probe.display_name category = probe.category except IrmaDatabaseError: display_name = probename category = "unknown" result = PluginResult(name=display_name, type=category, error=error) return result
def mimetype_probelist(mimetype, session): log.debug("asking what probes handle %s", mimetype) probe_list = [] for p in Probe.all(session): regexp = p.mimetype_regexp if regexp is None or \ re.search(regexp, mimetype, re.IGNORECASE) is not None: probe_list.append(p.name) log.debug("probes: %s", "-".join(probe_list)) return probe_list
def refresh_probes(session): """ Put all probes offline and send them a register request Infos/Online state will be updated """ probes = Probe.all(session) for probe in probes: probe.online = False for active_probe in active_probes(): celery_probe.get_info(active_probe) return
def register(name, display_name, category, mimetype_regexp, session): try: probe = Probe.get_by_name(name, session) log.info("probe %s already registred " "updating parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) probe.display_name = display_name probe.category = category probe.mimetype_regexp = mimetype_regexp probe.online = True probe.update(['category', 'mimetype_regexp', 'online'], session) except IrmaDatabaseResultNotFound: log.info("register probe %s" " with parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) probe = Probe(name=name, display_name=display_name, category=category, mimetype_regexp=mimetype_regexp, online=True) probe.save(session) return
def get_list(session): """ Return a list of probe name (queues name) that a scan could use """ active_probes_list = active_probes() probes = Probe.all(session) # Update Status for probe in probes: if probe.name not in active_probes_list: log.debug("probe list set %s offline", probe.name) probe.online = False elif probe.online is False: log.debug("probe list set %s online", probe.name) probe.online = True probes_list = [p.name for p in probes if p.online] log.info("probe list %s", "-".join(probes_list)) return probes_list
def job_error(parent_taskid, frontend_scan_id, filename, probename): try: log.info("scanid %s: filename:%s probe %s", frontend_scan_id, filename, probename) with session_query() as session: probe = Probe.get_by_name(probename, session) result = {} result['status'] = -1 result['name'] = probe.display_name result['type'] = probe.category result['error'] = "job error" result['duration'] = None celery_frontend.scan_result(frontend_scan_id, filename, probe, result) except Exception as e: log.exception(e) raise job_error.retry(countdown=5, max_retries=3, exc=e)
def refresh_probelist(session): global available_probes dbprobes = Probe.all(session) result_queue_name = config.brain_config['broker_probe'].queue queues = probe_app.control.inspect().active_queues() or {} queues = [q['name'] for ql in queues.values() for q in ql if q['name'] != result_queue_name] for probe in dbprobes: if probe.online and probe.name not in queues: log.debug("probelist set %s offline", probe.name) probe.online = False try: available_probes.remove(probe.name) except ValueError: pass elif (not probe.online or probe.name not in available_probes) \ and probe.name in queues: log.debug("probelist set %s online", probe.name) probe.online = True available_probes.append(probe.name)
def refresh_probelist(session): global available_probes dbprobes = Probe.all(session) result_queue_name = config.brain_config['broker_probe'].queue queues = probe_app.control.inspect().active_queues() or {} queues = [ q['name'] for ql in queues.values() for q in ql if q['name'] != result_queue_name ] for probe in dbprobes: if probe.online and probe.name not in queues: log.debug("probelist set %s offline", probe.name) probe.online = False try: available_probes.remove(probe.name) except ValueError: pass elif (not probe.online or probe.name not in available_probes) \ and probe.name in queues: log.debug("probelist set %s online", probe.name) probe.online = True available_probes.append(probe.name)
def register(name, display_name, category, mimetype_regexp, session): try: probe = Probe.get_by_name(name, session) log.info("probe %s already registred " "updating parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) session.query(Probe)\ .filter_by(id=probe.id)\ .update({'category': category, 'mimetype_regexp': mimetype_regexp, 'online': True, 'display_name': display_name}) except IrmaDatabaseResultNotFound: log.info("register probe %s" " with parameters: " "[display_name:%s cat:%s regexp:%s]", name, display_name, category, mimetype_regexp) probe = Probe(name=name, display_name=display_name, category=category, mimetype_regexp=mimetype_regexp, online=True) session.add(probe) return
def test003_get_by_name_not_found(self): self.session.query.side_effect = NoResultFound with self.assertRaises(IrmaDatabaseResultNotFound): Probe.get_by_name("whatever", self.session)
def test002_get_by_name(self): Probe.get_by_name("whatever", self.session) self.session.query.assert_called_once_with(Probe) m_filter = self.session.query().filter m_filter.assert_called_once() m_filter().one.assert_called_once()
def test004_get_by_name_multiple_found(self): self.session.query.side_effect = MultipleResultsFound with self.assertRaises(IrmaDatabaseError): Probe.get_by_name("whatever", self.session)
def test005_all(self): Probe.all(self.session) self.session.query.assert_called_once_with(Probe) self.session.query().all.assert_called_once()