Example #1
0
 def test_user_quota_update(self):
     # test update quota
     (before, _) = user_ctrl.get_quota(self.userid)
     scanid = scan_ctrl.new(self.scanid, self.userid, 2)
     for i in xrange(0, 10):
         for probe in ['probe1', 'probe2']:
             job_ctrl.new(scanid, "file-{0}".format(i), probe, 1)
     scan_ctrl.launched(scanid)
     (after, _) = user_ctrl.get_quota(self.userid)
     self.assertEqual(before - after, 20)
     with session_query() as session:
         user = User.load(self.userid, session)
         scan_ids = [scan.id for scan in user.scans]
         self.assertNotEqual(scan_ids, [])
         self.assertTrue(scanid in scan_ids)
Example #2
0
 def test_user_quota(self):
     # test we have a correct quota
     (remaining, quota) = user_ctrl.get_quota(self.userid)
     self.assertIsNotNone(quota)
     self.assertEqual(type(quota), int)
     self.assertIsNotNone(remaining)
     self.assertEqual(type(remaining), int)
Example #3
0
def scan(frontend_scanid, scan_request):
    try:
        log.info("%s", frontend_scanid)
        # TODO user should be identified by RMQ vhost
        # read vhost from config as workaround
        rmqvhost = config.get_frontend_rmqvhost()
        user_id = user_ctrl.get_userid(rmqvhost)
        ftpuser = user_ctrl.get_ftpuser(user_id)
        # create an initial entry to track future errors
        # tell frontend that frontend_scanid is now known to brain
        # progress available
        scan_id = scan_ctrl.new(frontend_scanid, user_id, len(scan_request))
        # send a scan launched event to frontend
        (remaining, quota) = user_ctrl.get_quota(user_id)
        if remaining is not None:
            log.info("%s quota remaining {0}/{1}",
                     frontend_scanid,
                     remaining,
                     quota)
        else:
            log.info("%s unlimited quota",
                     frontend_scanid)
        if remaining <= 0:
            scan_ctrl.error(scan_id, IrmaScanStatus.error)

        available_probelist = celery_probe.get_probelist()
        for (filename, probe_list) in scan_request.items():
            if probe_list is None:
                scan_ctrl.error(scan_id, IrmaScanStatus.error_probe_missing)
                log.info("%s Empty probe list", frontend_scanid)
                return IrmaTaskReturn.error("empty probe list on brain")
            # first check probe_list
            for p in probe_list:
                # check if probe exists
                if p not in available_probelist:
                    scan_ctrl.error(scan_id,
                                    IrmaScanStatus.error_probe_missing)
                    msg = "Unknown probe {0}".format(p)
                    log.info("%s Unknown probe %s",
                             frontend_scanid,
                             p)
                    return IrmaTaskReturn.error(msg)

            # Now, create one subtask per file to
            # scan per probe according to quota
            for probe in probe_list:
                if remaining is not None:
                    if remaining <= 0:
                        break
                    else:
                        remaining -= 1
                task_id = UUID.generate()
                job_id = job_ctrl.new(scan_id, filename, probe, task_id)
                celery_probe.job_launch(ftpuser,
                                        frontend_scanid,
                                        filename,
                                        probe,
                                        job_id,
                                        task_id)
        scan_ctrl.launched(scan_id)
        celery_frontend.scan_launched(frontend_scanid)
        log.info(
            "%s %d files receives / %d active probe /" +
            " %d probe used / %d jobs launched",
            frontend_scanid,
            len(scan_request),
            len(available_probelist),
            len(probe_list),
            scan_ctrl.get_nbjobs(scan_id))
    except:
        log.info("exception", exc_info=True)
        raise