def mimetype_filter_scan_request(scan_request_dict): try: with session_transaction() as session: scan_request = IrmaScanRequest(scan_request_dict) for file in scan_request.files(): filtered_probelist = [] mimetype = scan_request.get_mimetype(file) mimetype_probelist = probe_ctrl.mimetype_probelist( mimetype, session) probe_list = scan_request.get_probelist(file) log.debug("file: %s probe_list: %s", file, "-".join(probe_list)) # first check probe_list for unknown probe for probe in probe_list: # check if probe exists if probe not in probe_ctrl.available_probes: log.warning("probe %s not available", probe) if probe in mimetype_probelist: # probe is asked and supported by mimetype log.debug( "file %s probe %s asked" + " and supported for %s append to request", file, probe, mimetype) filtered_probelist.append(probe) # update probe list in scan request scan_request.set_probelist(file, filtered_probelist) return IrmaTaskReturn.success(scan_request.to_dict()) except Exception as e: log.exception(type(e).__name__ + " : " + str(e)) return IrmaTaskReturn.error("Brain error")
def _create_scan_request(file_ext_list, probelist, mimetype_filtering): # Create scan request # dict of filename : probe_list # force parameter taken into account log.debug("probelist: %s mimetype_filtering: %s", probelist, mimetype_filtering) scan_request = IrmaScanRequest() for file_ext in file_ext_list: scan_request.add_file(file_ext.external_id, probelist, file_ext.file.mimetype) if mimetype_filtering is True: srdict = scan_request.to_dict() filtered_srdict = celery_brain.mimetype_filter_scan_request(srdict) scan_request = IrmaScanRequest(filtered_srdict) return scan_request
def mimetype_filter_scan_request(scan_request_dict): try: with session_transaction() as session: scan_request = IrmaScanRequest(scan_request_dict) for file in scan_request.files(): filtered_probelist = [] mimetype = scan_request.get_mimetype(file) mimetype_probelist = probe_ctrl.mimetype_probelist(mimetype, session) probe_list = scan_request.get_probelist(file) log.debug("file: %s probe_list: %s", file, "-".join(probe_list)) # first check probe_list for unknown probe for probe in probe_list: # check if probe exists if probe not in probe_ctrl.available_probes: log.warning("probe %s not available", probe) if probe in mimetype_probelist: # probe is asked and supported by mimetype log.debug("file %s probe %s asked" + " and supported for %s append to request", file, probe, mimetype) filtered_probelist.append(probe) # update probe list in scan request scan_request.set_probelist(file, filtered_probelist) return IrmaTaskReturn.success(scan_request.to_dict()) except Exception as e: log.exception(e) return IrmaTaskReturn.error("Brain error")
def _add_empty_results(file_ext_list, scan_request, scan, session): log.debug("scan %s: scan_request: %s", scan.external_id, scan_request.to_dict()) new_scan_request = IrmaScanRequest() for file_ext in file_ext_list: probelist = scan_request.get_probelist(file_ext.external_id) updated_probe_list = _add_empty_result(file_ext, probelist, scan, session) # Update scan_request according to results already known linked # in _add_empty_result if len(updated_probe_list) > 0: mimetype = scan_request.get_mimetype(file_ext.external_id) log.debug("scan %s: update scan_request for file %s" "previously asked %s now %s", scan.external_id, file_ext.external_id, scan_request.get_probelist(file_ext.external_id), updated_probe_list) new_scan_request.add_file(file_ext.external_id, updated_probe_list, mimetype) log.debug("scan %s: new scan_request %s", scan.external_id, new_scan_request.to_dict()) return new_scan_request
def _add_empty_results(file_ext_list, scan_request, scan, session): log.debug("scan %s: scan_request: %s", scan.external_id, scan_request.to_dict()) new_scan_request = IrmaScanRequest() for file_ext in file_ext_list: probelist = scan_request.get_probelist(file_ext.external_id) updated_probe_list = _add_empty_result(file_ext, probelist, scan, session) # Update scan_request according to results already known linked # in _add_empty_result if len(updated_probe_list) > 0: mimetype = scan_request.get_mimetype(file_ext.external_id) log.debug( "scan %s: update scan_request for file %s" "previously asked %s now %s", scan.external_id, file_ext.external_id, scan_request.get_probelist(file_ext.external_id), updated_probe_list) new_scan_request.add_file(file_ext.external_id, updated_probe_list, mimetype) log.debug("scan %s: new scan_request %s", scan.external_id, new_scan_request.to_dict()) return new_scan_request
def test_init(self): isr = IrmaScanRequest({"foo": Mock(), "bar": Mock()}) self.assertEqual(isr.nb_files, 2)
def setUp(self): self.isr = IrmaScanRequest()
class TestIrmaScanRequest(unittest.TestCase): def setUp(self): self.isr = IrmaScanRequest() def test_init(self): isr = IrmaScanRequest({"foo": Mock(), "bar": Mock()}) self.assertEqual(isr.nb_files, 2) def test_add_file(self): self.isr.add_file("foo", "probelist", "mimetype") self.assertDictEqual(self.isr.request["foo"], { "probe_list": "probelist", "mimetype": "mimetype" }) self.assertEqual(self.isr.nb_files, 1) def test_del_file0(self): self.isr.add_file("foo", "probelist", "mimetype") self.isr.del_file("foo") self.assertNotIn("foo", self.isr.request) self.assertEqual(self.isr.nb_files, 0) def test_del_file1(self): self.isr.del_file("foo") self.assertNotIn("foo", self.isr.request) self.assertEqual(self.isr.nb_files, 0) def test_get_probelist(self): self.isr.add_file("foo", "bar", "mimetype") result = self.isr.get_probelist("foo") self.assertEqual(result, "bar") self.assertEqual(self.isr.nb_files, 1) def test_set_probelist(self): self.isr.add_file("foo", "bar", "mimetype") self.isr.set_probelist("foo", "baz") self.assertEqual(self.isr.get_probelist("foo"), "baz") self.assertEqual(self.isr.nb_files, 1) def test_get_mimetype(self): self.isr.add_file("foo", "probelist", "bar") result = self.isr.get_mimetype("foo") self.assertEqual(result, "bar") self.assertEqual(self.isr.nb_files, 1) def test_to_dict(self): self.assertIs(self.isr.request, self.isr.to_dict()) def test_files(self): self.assertEqual(self.isr.request.keys(), self.isr.files())