Exemplo n.º 1
0
def add_files(scanid, db):
    """ Attach a file to a scan.
        The request should be performed using a POST request method.
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)

        if len(request.files) == 0:
            raise ValueError("No files uploaded")

        files = {}
        for f in request.files:
            upfile = request.files.get(f)
            filename = decode_utf8(upfile.raw_filename)
            data = upfile.file
            files[filename] = data

        scan_ctrl.add_files(scan, files, db)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 2
0
def launch(scanid, db):
    """ Launch a scan.
        The request should be performed using a POST request method.
    """
    try:
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)
        probes = None

        # handle scan parameter / cached results: "force"
        if 'force' in request.json and request.json.get('force'):
            scan.force = True
            db.commit()

        # V1 retro compatibility
        scan.mimetype_filtering = False
        scan.file_resubmit = False

        # handle scan parameter / probelist: "probes"
        if 'probes' in request.json:
            probes = request.json.get('probes').split(',')

        msg = "scanid: %s Force %s MimeF %s"
        msg += "Resub %s Probes %s"
        log.debug(msg, scanid, scan.force, scan.mimetype_filtering,
                  scan.resubmit_files, probes)
        scan_ctrl.check_probe(scan, probes, db)
        # launch_asynchronous scan via frontend task
        celery_frontend.scan_launch(scanid)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 3
0
def add_files(scanid, db):
    """ Attach a file to a scan.
        The request should be performed using a POST request method.
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)

        if len(request.files) == 0:
            raise ValueError("No files uploaded")

        files = {}
        for f in request.files:
            upfile = request.files.get(f)
            filename = decode_utf8(upfile.raw_filename)
            data = upfile.file
            files[filename] = data

        scan_ctrl.add_files(scan, files, db)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 4
0
def launch(scanid, db):
    """ Launch a scan.
        The request should be performed using a POST request method.
    """
    try:
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)
        probes = None

        # handle scan parameter / cached results: "force"
        if 'force' in request.json and request.json.get('force'):
            scan.force = True
            db.commit()

        # V1 retro compatibility
        scan.mimetype_filtering = False
        scan.file_resubmit = False

        # handle scan parameter / probelist: "probes"
        if 'probes' in request.json:
            probes = request.json.get('probes').split(',')

        msg = "scanid: %s Force %s MimeF %s"
        msg += "Resub %s Probes %s"
        log.debug(msg, scanid, scan.force, scan.mimetype_filtering,
                  scan.resubmit_files, probes)
        scan_ctrl.check_probe(scan, probes, db)
        # launch_asynchronous scan via frontend task
        celery_frontend.scan_launch(scanid)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 5
0
def get(scanid, db):
    """ Retrieve information for a specific scan
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 6
0
def get(scanid, db):
    """ Retrieve information for a specific scan
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 7
0
def get_results(scanid, db):
    """ Retrieve results for a scan. Results are the same as in the get()
        method, i.e. a summary for each scanned files.
        The request should be performed using a GET request method.
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)
        file_web_schema = FileWebSchema_v1(exclude=('probe_results',
                                                    'file_infos'))
        response.content_type = "application/json; charset=UTF-8"
        return file_web_schema.dumps(scan.files_web, many=True).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 8
0
def cancel(scanid, db):
    """ Cancel a scan.
        The request should be performed using a POST request method.
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)

        scan_ctrl.cancel(scan, db)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 9
0
def get_results(scanid, db):
    """ Retrieve results for a scan. Results are the same as in the get()
        method, i.e. a summary for each scanned files.
        The request should be performed using a GET request method.
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)
        file_web_schema = FileWebSchema_v1(exclude=('probe_results',
                                                    'file_infos'))
        response.content_type = "application/json; charset=UTF-8"
        return file_web_schema.dumps(scan.files_web, many=True).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 10
0
def cancel(scanid, db):
    """ Cancel a scan.
        The request should be performed using a POST request method.
    """
    try:
        log.debug("scanid: %s", scanid)
        validate_id(scanid)
        scan = Scan.load_from_ext_id(scanid, db)

        scan_ctrl.cancel(scan, db)

        response.content_type = "application/json; charset=UTF-8"
        return scan_schema.dumps(scan).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 11
0
def get(resultid, db):
    """ Retrieve a single fileweb result, with details.
        The request should be performed using a GET request method.
    """
    try:
        formatted = False if request.query.formatted == 'no' else True
        log.debug("resultid %s formatted %s", resultid, formatted)
        validate_id(resultid)
        fw = FileWeb.load_from_ext_id(resultid, db)

        file_web_schema = FileWebSchema_v1_1()
        file_web_schema.context = {'formatted': formatted}

        response.content_type = "application/json; charset=UTF-8"
        return file_web_schema.dumps(fw).data
    except Exception as e:
        log.exception(e)
        process_error(e)
Exemplo n.º 12
0
 def test006_validate_id_raises(self):
     with self.assertRaises(ValueError) as context:
         module.validate_id("whatever")
     self.assertEqual(str(context.exception), "Malformed id")
Exemplo n.º 13
0
 def test005_validate_id(self):
     uuid = UUID.generate()
     self.assertIsNone(module.validate_id(uuid))
Exemplo n.º 14
0
 def test006_validate_id_raises(self):
     with self.assertRaises(ValueError) as context:
         module.validate_id("whatever")
     self.assertEqual(str(context.exception), "Malformed id")
Exemplo n.º 15
0
 def test005_validate_id(self):
     uuid = UUID.generate()
     self.assertIsNone(module.validate_id(uuid))