def import_scan(self, path, include_aggregate=True):
        """Uploads and then imports scan report.

        :param path: Path of the scan report.
        :param include_aggregate: Flag indicating whether import scan results should be shown on Workbenches.
        :return: ScanRef referenced by id if exists.
        """
        uploaded_file_name = self._client.file_helper.upload(path)

        imported_scan_id = self._client.scans_api.import_scan(ScanImportRequest(uploaded_file_name), include_aggregate)

        return self.id(imported_scan_id)
    def test_export_import(self, app, client, scan_id):

        # Cannot export on a test that has never been launched, therefore launch the scan first.
        client.scans_api.launch(scan_id, ScanLaunchRequest())
        self.wait_until(
            lambda: client.scans_api.details(scan_id), lambda details: details.
            info.status in [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])

        # Stop the running scan.
        client.scans_api.stop(scan_id)
        self.wait_until(
            lambda: client.scans_api.details(scan_id),
            lambda details: details.info.status in [Scan.STATUS_CANCELED])

        file_id = client.scans_api.export_request(
            scan_id, ScanExportRequest(format=ScanExportRequest.FORMAT_NESSUS))
        assert file_id, u'The `export_request` method returns a valid file ID.'

        export_status = self.wait_until(
            lambda: client.scans_api.export_status(scan_id, file_id),
            lambda status: status == ScansApi.STATUS_EXPORT_READY)
        assert export_status == ScansApi.STATUS_EXPORT_READY, u'Scan export is ready.'

        iter_content = client.scans_api.export_download(
            scan_id, file_id, False)

        download_path = app.session_file_output('test_scan_export_import')
        assert not os.path.isfile(
            download_path), u'Scan report does not yet exist.'

        with open(download_path, 'wb') as fd:
            for chunk in iter_content:
                fd.write(chunk)
        assert os.path.isfile(download_path), u'Scan report is downloaded.'
        assert os.path.getsize(download_path) > 0, u'Scan report is not empty.'

        with open(download_path, 'rb') as fu:
            upload_file_name = client.file_api.upload(fu)
        assert upload_file_name, u'File `upload` method returns valid file name.'

        imported_scan_id = client.scans_api.import_scan(
            ScanImportRequest(upload_file_name))
        assert isinstance(imported_scan_id,
                          int), u'Import request returns scan id.'

        imported_scan_details = client.scans_api.details(imported_scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert imported_scan_details.info.name == scan_details.info.name, \
            u'Imported scan retains name of exported scan.'

        os.remove(download_path)
        client.scans_api.delete(imported_scan_id)
Beispiel #3
0
    def import_scan(self, path):
        """Uploads and then imports scan report.

        :param path: Path of the scan report.
        :return: ScanRef referenced by id if exists.
        """
        if not os.path.isfile(path):
            raise TenableIOException(u'File does not exist at path.')

        with open(path, 'rb') as upload_file:
            upload_file_name = self._client.file_api.upload(upload_file)

        imported_scan_id = self._client.scans_api.import_scan(ScanImportRequest(upload_file_name))

        return self.id(imported_scan_id)
def test_scans_export_import(client, new_scan):
    scan_id = new_scan
    # Cannot export on a test that has never been launched, therefore launch the scan first.
    client.scans_api.launch(scan_id, ScanLaunchRequest())
    wait_until(lambda: client.scans_api.details(scan_id),
               lambda details: details.info.status in [Scan.STATUS_COMPLETED])

    file_id = client.scans_api.export_request(
        scan_id, ScanExportRequest(format=ScanExportRequest.FORMAT_NESSUS))
    assert file_id, u'The `export_request` method did not return a valid file ID.'

    export_status = wait_until(
        lambda: client.scans_api.export_status(scan_id, file_id),
        lambda status: status == ScansApi.STATUS_EXPORT_READY)
    assert export_status == ScansApi.STATUS_EXPORT_READY, u'Scan export is not ready.'

    iter_content = client.scans_api.export_download(scan_id, file_id, False)

    download_path = 'test_scan_export_import'
    assert not os.path.isfile(
        download_path), u'Scan report does not yet exist.'

    with open(download_path, 'wb') as fd:
        for chunk in iter_content:
            fd.write(chunk)
    assert os.path.isfile(download_path), u'Scan report has not downloaded.'
    assert os.path.getsize(download_path) > 0, u'Scan report is not empty.'

    with open(download_path, 'rb') as fu:
        upload_file_name = client.file_api.upload(fu)
    assert upload_file_name, u'File `upload` method did not return a valid file name.'

    imported_scan_id = client.scans_api.import_scan(
        ScanImportRequest(upload_file_name))
    assert isinstance(
        imported_scan_id,
        int), u'The `import_scan` method did not return type `int`.'

    imported_scan_details = client.scans_api.details(imported_scan_id)
    scan_details = client.scans_api.details(scan_id)
    assert imported_scan_details.info.name == scan_details.info.name, \
        u'Imported scan retains name of exported scan.'

    os.remove(download_path)