コード例 #1
0
ファイル: scan_tasks.py プロジェクト: deloittem/irma-brain
def scan(frontend_scanid, scan_request_dict):
    try:
        with session_transaction() as session:
            log.debug("scanid: %s received %s", frontend_scanid,
                      scan_request_dict)
            user = User.get_by_rmqvhost(session)
            scan_request = IrmaScanRequest(scan_request_dict)
            scan = scan_ctrl.new(frontend_scanid, user, scan_request.nb_files,
                                 session)
            available_probelist = probe_ctrl.get_list(session)
            # Now, create one subtask per file to
            # scan per probe
            new_jobs = []
            for filehash in scan_request.filehashes():
                probelist = scan_request.get_probelist(filehash)
                scan_ctrl.check_probelist(scan, probelist, available_probelist,
                                          session)
                for probe in probelist:
                    j = Job(scan.id, filehash, probe)
                    session.add(j)
                    new_jobs.append(j)
            session.commit()
            scan_ctrl.launch(scan, new_jobs, session)
            celery_frontend.scan_launched(scan.scan_id, scan_request.to_dict())
            log.info(
                "scanid %s: %d file(s) received / %d active probe(s) / "
                "%d job(s) launched", scan.scan_id, scan.nb_files,
                len(available_probelist), len(scan.jobs))
    except Exception as e:
        log.exception(e)
        raise
コード例 #2
0
ファイル: scan_tasks.py プロジェクト: quarkslab/irma
def scan(file, probelist, frontend_scan):
    try:
        with session_transaction() as session:
            log.debug("scan_id: %s fileweb_id: %s received %s", frontend_scan,
                      file, probelist)
            user = User.get_by_rmqvhost(session)
            scan = scan_ctrl.new(frontend_scan, user, session)
            # Now, create one subtask per file to scan per probe
            new_jobs = []
            for probe in probelist:
                if probe in probe_ctrl.available_probes:
                    j = Job(scan.id, file, probe)
                    session.add(j)
                    new_jobs.append(j)
                else:
                    # send an error message to not stuck the scan
                    # One probe asked is no more present
                    res = probe_ctrl.create_error_results(probe,
                                                          "missing probe",
                                                          session)
                    celery_frontend.scan_result(file,
                                                probe,
                                                res)
            session.commit()
            scan_ctrl.launch(scan, new_jobs, session)
            log.info("scan_id %s: file %s received / %d active probe(s) / "
                     "%d job(s) launched",
                     scan.scan_id,
                     file,
                     len(probe_ctrl.available_probes),
                     len(scan.jobs))
    except Exception as e:
        log.exception(e)
        raise
コード例 #3
0
ファイル: scan_tasks.py プロジェクト: yehias/irma
def scan(file, probelist, frontend_scan):
    try:
        with session_transaction() as session:
            log.debug("scan_id: %s fileweb_id: %s received %s", frontend_scan,
                      file, probelist)
            user = User.get_by_rmqvhost(session)
            scan = scan_ctrl.new(frontend_scan, user, session)
            # Now, create one subtask per file to scan per probe
            new_jobs = []
            for probe in probelist:
                if probe in probe_ctrl.available_probes:
                    j = Job(scan.id, file, probe)
                    session.add(j)
                    new_jobs.append(j)
                else:
                    # send an error message to not stuck the scan
                    # One probe asked is no more present
                    res = probe_ctrl.create_error_results(
                        probe, "missing probe", session)
                    celery_frontend.scan_result(file, probe, res)
            session.commit()
            scan_ctrl.launch(scan, new_jobs, session)
            log.info(
                "scan_id %s: file %s received / %d active probe(s) / "
                "%d job(s) launched", scan.scan_id, file,
                len(probe_ctrl.available_probes), len(scan.jobs))
    except Exception as e:
        log.exception(type(e).__name__ + " : " + str(e))
        raise
コード例 #4
0
ファイル: scan_tasks.py プロジェクト: deloittem/irma-brain
def scan(frontend_scanid, scan_request_dict):
    try:
        with session_transaction() as session:
            log.debug("scanid: %s received %s", frontend_scanid,
                      scan_request_dict)
            user = User.get_by_rmqvhost(session)
            scan_request = IrmaScanRequest(scan_request_dict)
            scan = scan_ctrl.new(frontend_scanid, user, scan_request.nb_files,
                                 session)
            available_probelist = probe_ctrl.get_list(session)
            # Now, create one subtask per file to
            # scan per probe
            new_jobs = []
            for filehash in scan_request.filehashes():
                probelist = scan_request.get_probelist(filehash)
                scan_ctrl.check_probelist(scan, probelist,
                                          available_probelist, session)
                for probe in probelist:
                    j = Job(scan.id, filehash, probe)
                    session.add(j)
                    new_jobs.append(j)
            session.commit()
            scan_ctrl.launch(scan, new_jobs, session)
            celery_frontend.scan_launched(scan.scan_id,
                                          scan_request.to_dict())
            log.info("scanid %s: %d file(s) received / %d active probe(s) / "
                     "%d job(s) launched",
                     scan.scan_id,
                     scan.nb_files,
                     len(available_probelist),
                     len(scan.jobs))
    except Exception as e:
        log.exception(e)
        raise
コード例 #5
0
ファイル: userctrl.py プロジェクト: dewiestr/irma-brain
def get_quota(user_id):
    with session_query() as session:
        user = User.load(user_id, session)
        quota = user.quota
        remaining = None
        if quota is not None:
            remaining = user.remaining_quota(session)
        return (remaining, quota)
コード例 #6
0
 def setUp(self):
     self.user_name = "test_user"
     self.user_rmqvhost = "test_vhost"
     self.user_ftpuser = "******"
     self.user_quota = 100
     self.scanid = UUID.generate()
     try:
         self.userid = user_ctrl.get_userid(self.user_rmqvhost)
     except IrmaDatabaseResultNotFound:
         # user does not exist create one
         with session_transaction() as session:
             user = User(self.user_name,
                         self.user_rmqvhost,
                         self.user_ftpuser,
                         self.user_quota)
             user.save(session)
             session.commit()
             self.userid = user.id
コード例 #7
0
def scan_flush(frontend_scan_id):
    try:
        log.info("scan_id %s: scan flush requested", frontend_scan_id)
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(frontend_scan_id, user.id, session)
            scan_ctrl.flush(scan, session)
    except Exception as e:
        log.exception(e)
        return
コード例 #8
0
ファイル: scan_tasks.py プロジェクト: quarkslab/irma
def scan_flush(scan_id):
    try:
        log.info("scan %s: flush requested", scan_id)
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(scan_id, user.id, session)
            scan_ctrl.flush(scan, session)
    except Exception as e:
        log.exception(e)
        return
コード例 #9
0
def scan_cancel(frontend_scan_id):
    try:
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(frontend_scan_id, user.id, session)
            res = scan_ctrl.cancel(scan, session)
            return IrmaTaskReturn.success(res)
    except Exception as e:
        log.exception(e)
        return IrmaTaskReturn.error("cancel error on brain")
コード例 #10
0
ファイル: scan_tasks.py プロジェクト: yehias/irma
def scan_flush(scan_id):
    try:
        log.info("scan %s: flush requested", scan_id)
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(scan_id, user.id, session)
            scan_ctrl.flush(scan, session)
    except Exception as e:
        log.exception(type(e).__name__ + " : " + str(e))
        return
コード例 #11
0
ファイル: scan_tasks.py プロジェクト: deloittem/irma-brain
def scan_cancel(frontend_scan_id):
    try:
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(frontend_scan_id, user.id, session)
            res = scan_ctrl.cancel(scan, session)
            return IrmaTaskReturn.success(res)
    except Exception as e:
        log.exception(e)
        return IrmaTaskReturn.error("cancel error on brain")
コード例 #12
0
ファイル: scan_tasks.py プロジェクト: quarkslab/irma
def files_flush(files, scan_id):
    try:
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(scan_id, user.id, session)
            ftpuser = scan.user.ftpuser
            log.debug("Flushing files %s", files)
            ftp_ctrl.flush(ftpuser, files)
    except Exception as e:
        log.exception(e)
        return
コード例 #13
0
 def setUp(self):
     self.session = MagicMock()
     self.name = "user"
     self.rmqvhost = "vhost"
     self.ftpuser = "******"
     self.user = User(self.name, self.rmqvhost, self.ftpuser)
     self.frontend_scanid = "frontend_scanid"
     self.nb_files = randint(50, 100)
     self.scan = Scan(frontend_scanid=self.frontend_scanid,
                      user_id=self.user.id)
     self.scan.user = self.user
コード例 #14
0
ファイル: scan_tasks.py プロジェクト: yehias/irma
def files_flush(files, scan_id):
    try:
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(scan_id, user.id, session)
            ftpuser = scan.user.ftpuser
            log.debug("Flushing files %s", files)
            ftp_ctrl.flush(ftpuser, files)
    except Exception as e:
        log.exception(type(e).__name__ + " : " + str(e))
        return
コード例 #15
0
ファイル: scan_tasks.py プロジェクト: yehias/irma
def scan_cancel(scan_id):
    try:
        log.info("scan %s: cancel", scan_id)
        with session_transaction() as session:
            user = User.get_by_rmqvhost(session)
            scan = Scan.get_scan(scan_id, user.id, session)
            res = scan_ctrl.cancel(scan, session)
            return IrmaTaskReturn.success(res)
    except Exception as e:
        log.exception(type(e).__name__ + " : " + str(e))
        return IrmaTaskReturn.error("cancel error on brain")
コード例 #16
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)
コード例 #17
0
ファイル: create_user.py プロジェクト: dewiestr/irma-brain

if len(sys.argv) not in (4, 5):
    print("usage: {0} <username> <rmqvhost> <ftpuser> [quota]\n"
          "      with <username> a string\n"
          "           <rmqvhost> the rmqvhost used for the frontend\n"
          "           <ftpuser> the ftpuser used by the frontend\n"
          "           [quota] the number of file scan quota\n"
          "example: {0} test1 mqfrontend frontend"
          "".format(sys.argv[0]))
    sys.exit(1)

name = sys.argv[1]
rmqvhost = sys.argv[2]
ftpuser = sys.argv[3]
# quota is in number of files (0 means disabled)
quota = int(sys.argv[4]) if len(sys.argv) == 5 else 0
with session_transaction() as session:
    try:
        user = User.get_by_rmqvhost(rmqvhost, session)
        print("rmqvhost {0} is already assigned to user {1}. "
              "Updating with new parameters.".format(user.name, user.rmqvhost))
        user = user.load(user.id, session)
        user.name = name
        user.ftpuser = ftpuser
        user.quota = quota
        user.update(['name', 'ftpuser', 'quota'], session)
    except IrmaDatabaseResultNotFound:
        user = User(name=name, rmqvhost=rmqvhost, ftpuser=ftpuser, quota=quota)
        user.save(session)
コード例 #18
0
ファイル: create_user.py プロジェクト: vaginessa/irma
    print("SQL directory does not exist {0}" "..creating".format(dirname))
    os.makedirs(dirname)
    os.chmod(dirname, 0o777)
elif not (os.path.isdir(dirname)):
    print("Error. SQL directory is a not a dir {0}" "".format(dirname))
    raise IrmaDatabaseError("Can not create Brain database dir")

if not os.path.exists(db_name):
    # touch like method to create a rw-rw-rw- file for db
    open(db_name, 'a').close()
    os.chmod(db_name, 0o666)

# Retrieve database informations
url = config.get_sql_url()
engine = create_engine(url, echo=config.sql_debug_enabled())
# and create Database in case
Base.metadata.create_all(engine)

with session_transaction() as session:
    try:
        user = User.get_by_rmqvhost(session, rmqvhost=rmqvhost)
        print("rmqvhost {0} is already assigned to user {1}. "
              "Updating with new parameters.".format(user.name, user.rmqvhost))
        user = user.load(user.id, session)
        user.name = name
        user.ftpuser = ftpuser
        user.update(['name', 'ftpuser'], session)
    except IrmaDatabaseResultNotFound:
        user = User(name=name, rmqvhost=rmqvhost, ftpuser=ftpuser)
        user.save(session)
コード例 #19
0
ファイル: create_user.py プロジェクト: deloittem/irma-brain
    os.makedirs(dirname)
    os.chmod(dirname, 0777)
elif not (os.path.isdir(dirname)):
    print("Error. SQL directory is a not a dir {0}"
          "".format(dirname))
    raise IrmaDatabaseError("Can not create Brain database dir")

if not os.path.exists(db_name):
    # touch like method to create a rw-rw-rw- file for db
    open(db_name, 'a').close()
    os.chmod(db_name, 0666)

# Retrieve database informations
url = config.get_sql_url()
engine = create_engine(url, echo=config.sql_debug_enabled())
# and create Database in case
Base.metadata.create_all(engine)

with session_transaction() as session:
    try:
        user = User.get_by_rmqvhost(session, rmqvhost=rmqvhost)
        print("rmqvhost {0} is already assigned to user {1}. "
              "Updating with new parameters.".format(user.name, user.rmqvhost))
        user = user.load(user.id, session)
        user.name = name
        user.ftpuser = ftpuser
        user.update(['name', 'ftpuser'], session)
    except IrmaDatabaseResultNotFound:
        user = User(name=name, rmqvhost=rmqvhost, ftpuser=ftpuser)
        user.save(session)
コード例 #20
0
 def test003_get_by_rmqvhost_not_found(self):
     self.session.query.side_effect = NoResultFound
     with self.assertRaises(IrmaDatabaseResultNotFound):
         User.get_by_rmqvhost(self.session, rmqvhost="whatever")
コード例 #21
0
ファイル: create_user.py プロジェクト: quarkslab/irma
    print("SQL directory does not exist {0}"
          "..creating".format(dirname))
    os.makedirs(dirname)
    os.chmod(dirname, 0o777)
elif not (os.path.isdir(dirname)):
    print("Error. SQL directory is a not a dir {0}"
          "".format(dirname))
    raise IrmaDatabaseError("Can not create Brain database dir")

if not os.path.exists(db_name):
    # touch like method to create a rw-rw-rw- file for db
    open(db_name, 'a').close()
    os.chmod(db_name, 0o666)

# Retrieve database informations
engine = create_engine(config.sqldb.url, echo=config.sql_debug_enabled())
# and create Database in case
Base.metadata.create_all(engine)

with session_transaction() as session:
    try:
        user = User.get_by_rmqvhost(session, rmqvhost=rmqvhost)
        print("rmqvhost {0} is already assigned to user {1}. "
              "Updating with new parameters.".format(user.name, user.rmqvhost))
        session.query(User)\
            .filter_by(id=user.id)\
            .update({'name': name, 'ftpuser': ftpuser})
    except IrmaDatabaseResultNotFound:
        user = User(name=name, rmqvhost=rmqvhost, ftpuser=ftpuser)
        session.add(user)
コード例 #22
0
 def test001___init__(self):
     user = User(self.name, self.rmqvhost, self.ftpuser)
     self.assertEqual(user.name, self.name)
     self.assertEqual(user.rmqvhost, self.rmqvhost)
     self.assertEqual(user.ftpuser, self.ftpuser)
コード例 #23
0
 def test002_get_by_rmqvhost(self):
     User.get_by_rmqvhost(self.session)
     self.session.query.assert_called_once_with(User)
     m_filter = self.session.query(User).filter
     m_filter.assert_called_once()
     m_filter().one.assert_called_once()
コード例 #24
0
ファイル: userctrl.py プロジェクト: dewiestr/irma-brain
def get_ftpuser(user_id):
    with session_query() as session:
        user = User.load(user_id, session)
        return user.ftpuser
コード例 #25
0
 def test004_get_by_rmqvhost_multiple_found(self):
     self.session.query.side_effect = MultipleResultsFound
     with self.assertRaises(IrmaDatabaseError):
         User.get_by_rmqvhost(self.session, rmqvhost="whatever")
コード例 #26
0
ファイル: userctrl.py プロジェクト: dewiestr/irma-brain
def get_userid(rmqvhost):
    with session_query() as session:
        user = User.get_by_rmqvhost(rmqvhost, session)
        return user.id