def test_scans_agent_scan(client, new_agent_group):
    template_list = client.editor_api.list('scan')
    assert len(
        template_list.templates) > 0, u'Expected at least one scan template.'

    agent_group_uuid = client.agent_groups_api.details(new_agent_group).uuid

    test_templates = [
        t for t in template_list.templates if t.name == 'agent_basic'
    ]
    scan_id = client.scans_api.create(
        ScanCreateRequest(
            test_templates[0].uuid,
            ScanSettings(
                'test_basic_agent_scan',
                agent_group_id=[agent_group_uuid],
            )))
    # we need to launch the scan in order for some fields to be fully populated
    client.scans_api.launch(scan_id, ScanLaunchRequest())

    scan_details = client.scans_api.details(scan_id)
    assert isinstance(
        scan_details, ScanDetails
    ), u'The `details` method did not return type `ScanDetails`.'
    assert scan_details.info.agent_targets is not None, u'Expected agent_targets attribute to be present.'
    assert len(
        scan_details.info.agent_targets
    ) == 1, u'Expected a single agent group to be included in agent_targets.'
    assert scan_details.info.agent_targets[0].uuid == agent_group_uuid, \
        u'Expected agent group uuid to match configured value.'
    def test_create_launch_pause_resume_history_stop_delete(
            self, client, scan_id):
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [Scan.STATUS_CANCELED, Scan.STATUS_COMPLETED, Scan.STATUS_EMPTY], \
            u'Scan is in idling state.'

        # Launch the scan.
        client.scans_api.launch(scan_id, ScanLaunchRequest())
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_PENDING, Scan.STATUS_RUNNING
        ], u'Scan is in launched state.'

        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id), lambda details: details.
            info.status in [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])
        assert scan_details.info.status == Scan.STATUS_RUNNING, u'Scan should be running to test pause.'

        # Pause the running scan.
        client.scans_api.pause(scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_PAUSED, Scan.STATUS_PAUSING
        ], u'Scan is pausing.'
        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id),
            lambda details: details.info.status in [Scan.STATUS_PAUSED])
        assert scan_details.info.status == Scan.STATUS_PAUSED, u'Scan is paused.'

        # Resume the paused scan.
        client.scans_api.resume(scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_RESUMING, Scan.STATUS_RUNNING
        ], u'Scan is resuming.'

        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id), lambda details: details.
            info.status in [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])
        assert scan_details.info.status == Scan.STATUS_RUNNING, u'Scan is running.'

        # Stop the running scan.
        client.scans_api.stop(scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_CANCELED, Scan.STATUS_STOPPING
        ], u'Scan is resuming.'

        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id),
            lambda details: details.info.status in [Scan.STATUS_CANCELED])
        assert scan_details.info.status == Scan.STATUS_CANCELED, u'Scan is canceled.'

        # Test the history API.
        assert len(scan_details.history) > 0, u'Scan has at least one history.'
        history_id = scan_details.history[0].history_id
        history = client.scans_api.history(scan_id, history_id)
        assert history.status == scan_details.info.status, u'Scan history reports same status as scan details.'
    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)
    def test_create_launch_pause_resume_stop_delete(self, client, scan_id):
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [Scan.STATUS_CANCELED, Scan.STATUS_COMPLETED, Scan.STATUS_EMPTY], \
            u'Scan is in idling state.'

        # Launch the scan.
        client.scans_api.launch(scan_id, ScanLaunchRequest())
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_PENDING, Scan.STATUS_RUNNING
        ], u'Scan is in launched state.'

        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id), lambda details: details.
            info.status in [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])
        assert scan_details.info.status == Scan.STATUS_RUNNING, u'Scan should be running to test pause.'

        # Pause the running scan.
        client.scans_api.pause(scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_PAUSED, Scan.STATUS_PAUSING
        ], u'Scan is pausing.'
        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id),
            lambda details: details.info.status in [Scan.STATUS_PAUSED])
        assert scan_details.info.status == Scan.STATUS_PAUSED, u'Scan is paused.'

        # Resume the paused scan.
        client.scans_api.resume(scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_RESUMING, Scan.STATUS_RUNNING
        ], u'Scan is resuming.'

        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id), lambda details: details.
            info.status in [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])
        assert scan_details.info.status == Scan.STATUS_RUNNING, u'Scan is running.'

        # Stop the running scan.
        client.scans_api.stop(scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_CANCELED, Scan.STATUS_STOPPING
        ], u'Scan is resuming.'

        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id),
            lambda details: details.info.status in [Scan.STATUS_CANCELED])
        assert scan_details.info.status == Scan.STATUS_CANCELED, u'Scan is canceled.'
    def scan(self, app, client, scanner, template):
        """
        Create scan for testing. Ensure there is one active (running or paused) scan in given scanner.
        """
        scan_id = client.scans_api.create(
            ScanCreateRequest(
                template.uuid,
                ScanSettings(app.session_name('test_scanners'),
                             TenableIOTestConfig.get('scan_text_targets'),
                             scanner_id=scanner.id)))

        client.scans_api.launch(scan_id, ScanLaunchRequest())
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_PENDING, Scan.STATUS_RUNNING
        ], u'Scan is in launched state.'

        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id), lambda details: details.
            info.status in [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])
        assert scan_details.info.status == Scan.STATUS_RUNNING, u'Scan should be running to test pause.'

        client.scans_api.pause(scan_id)
        scan_details = client.scans_api.details(scan_id)
        assert scan_details.info.status in [
            Scan.STATUS_PAUSED, Scan.STATUS_PAUSING
        ], u'Scan is pausing.'
        scan_details = self.wait_until(
            lambda: client.scans_api.details(scan_id),
            lambda details: details.info.status in [Scan.STATUS_PAUSED])
        assert scan_details.info.status == Scan.STATUS_PAUSED, u'Scan is paused.'

        yield scan_details

        try:
            client.scans_api.delete(scan_id)
        except TenableIOApiException:
            # This happens when the scan is not idling.
            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, Scan.STATUS_COMPLETED, Scan.
                    STATUS_EMPTY
                ])
            client.scans_api.delete(scan_id)
예제 #6
0
    def launch(self, wait=True, alt_targets=None):
        """Launch the scan.

        :param wait: If True, the method blocks until the scan's status is not \
        :class:`tenable_io.api.models.Scan`.STATUS_PENDING. Default is False.

        :param alt_targets: String of comma separated alternative targets or list of alternative target strings.
        :return: The same ScanRef instance.
        """
        if isinstance(alt_targets, six.string_types):
            alt_targets = [alt_targets]

        self._client.scans_api.launch(
            self.id, ScanLaunchRequest(alt_targets=alt_targets))
        if wait:
            util.wait_until(
                lambda: self.status() not in ScanHelper.STATUSES_PENDING)
        return self
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)
예제 #8
0
def test_scanner_control_scan(client, fetch_scanner):
    template_list = client.editor_api.list('scan')
    assert len(
        template_list.templates) > 0, u'Expected at least one scan template.'

    test_templates = [t for t in template_list.templates if t.name == 'basic']
    scan_id = client.scans_api.create(
        ScanCreateRequest(
            test_templates[0].uuid,
            ScanSettings('test_scanners_scan',
                         TenableIOTestConfig.get('scan_text_targets'),
                         scanner_id=fetch_scanner.id)))
    client.scans_api.launch(scan_id, ScanLaunchRequest())

    scan = client.scans_api.details(scan_id)

    wait_until(
        lambda: client.scans_api.details(scan_id),
        lambda details: details.info.status in
        [Scan.STATUS_PENDING, Scan.STATUS_RUNNING, Scan.STATUS_INITIALIZING])
    assert scan.info.status in [Scan.STATUS_PENDING, Scan.STATUS_INITIALIZING, Scan.STATUS_RUNNING], \
        u'Scan is in launched state.'

    scan_details = wait_until(
        lambda: client.scans_api.details(scan_id),
        lambda details: details.info.status in [Scan.STATUS_RUNNING])
    assert scan_details.info.status == Scan.STATUS_RUNNING, u'Scan should be running to test pause.'

    scan_list = client.scanners_api.get_scans(fetch_scanner.id)
    assert isinstance(scan_list, ScannerScanList), u'Get request returns type.'
    assert len(scan_list.scans) > 0, u'At least one test scan.'

    scan_uuid = scan.info.uuid
    test_scans = [s for s in scan_list.scans if s.id == scan_uuid]
    assert len(test_scans) == 1, u'Must contain test scan.'

    client.scanners_api.control_scans(fetch_scanner.id, scan_uuid,
                                      ScannerControlRequest(u'pause'))
    scan_details = wait_until(
        lambda: client.scans_api.details(scan_id),
        lambda details: details.info.status in [Scan.STATUS_PAUSED])
    assert scan_details.info.status == Scan.STATUS_PAUSED, u'Scan is paused.'
예제 #9
0
        pass
    for item in ipList:
        if item.startswith(('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')):
            pass
        else:
            ipList.remove(item)
        finalList = [i.split(' ')[0] for i in ipList]

ipFile.close()

#This block imports the API keys from an ini file.
parser = configparser.ConfigParser()
parser.read('tenable_io.ini')
accessKey = parser.get('tenable_io', 'access_key')
secretKey = parser.get('tenable_io', 'secret_key')

#This block launches the Tenable scan.
client = TenableIOClient(access_key=accessKey, secret_key=secretKey)
scanners = {
    scanner.name: scanner.id
    for scanner in client.scanners_api.list().scanners
}
scans = {scan.name: scan.id for scan in client.scans_api.list().scans}
scan_id = client.scans_api.launch(
    #SCAN ID GOES HERE,
    ScanLaunchRequest(alt_targets=finalList))

#This block downloads the scan report with the date and time the scan was started as the name.
scan = client.scan_helper.id(scans['#NAME OF SCAN GOES HERE'])
scan.download(reportName + '.pdf')
def test_scans_control_endpoints_using_schedule_uuid(client, new_scan):
    scan_id = new_scan
    scan_details = client.scans_api.details(schedule_uuid=new_scan)
    assert scan_details.info.status in [Scan.STATUS_CANCELED, Scan.STATUS_COMPLETED, Scan.STATUS_EMPTY], \
        u'Scan is not in an idling state.'

    # Launch the scan.
    client.scans_api.launch(schedule_uuid=new_scan,
                            scan_launch_request=ScanLaunchRequest())
    scan_details = client.scans_api.details(schedule_uuid=new_scan)
    assert scan_details.info.status in [Scan.STATUS_PENDING, Scan.STATUS_INITIALIZING, Scan.STATUS_RUNNING], \
        u'The scan is not launching.'

    scan_details = wait_until(
        lambda: client.scans_api.details(schedule_uuid=new_scan),
        lambda details: details.info.status in
        [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])
    assert scan_details.info.status == Scan.STATUS_RUNNING, u'The scan is not running.'

    # Pause the running scan.
    client.scans_api.pause(schedule_uuid=new_scan)
    scan_details = client.scans_api.details(schedule_uuid=new_scan)
    assert scan_details.info.status in [
        Scan.STATUS_PAUSED, Scan.STATUS_PAUSING
    ], u'The scan is not pausing.'
    scan_details = wait_until(
        lambda: client.scans_api.details(schedule_uuid=new_scan),
        lambda details: details.info.status in [Scan.STATUS_PAUSED])
    assert scan_details.info.status == Scan.STATUS_PAUSED, u'The scan is not paused.'

    # Resume the paused scan.
    client.scans_api.resume(schedule_uuid=new_scan)
    scan_details = client.scans_api.details(schedule_uuid=new_scan)
    assert scan_details.info.status in [
        Scan.STATUS_RESUMING, Scan.STATUS_RUNNING
    ], u'The scan is not resuming.'

    scan_details = wait_until(
        lambda: client.scans_api.details(schedule_uuid=new_scan),
        lambda details: details.info.status in
        [Scan.STATUS_COMPLETED, Scan.STATUS_RUNNING])
    assert scan_details.info.status == Scan.STATUS_RUNNING, u'The scan is not running.'

    # Stop the running scan.
    client.scans_api.stop(schedule_uuid=new_scan)
    scan_details = client.scans_api.details(schedule_uuid=new_scan)
    assert scan_details.info.status in [
        Scan.STATUS_CANCELED, Scan.STATUS_STOPPING
    ], u'The scan is not resuming.'

    scan_details = wait_until(
        lambda: client.scans_api.details(schedule_uuid=new_scan),
        lambda details: details.info.status in [Scan.STATUS_CANCELED])
    assert scan_details.info.status == Scan.STATUS_CANCELED, u'The scan was not canceled.'

    # Test the history API.
    assert len(scan_details.history
               ) > 0, u'The scan should have at least one history.'
    history_id = scan_details.history[0].history_id
    history = client.scans_api.history(schedule_uuid=new_scan,
                                       history_id=history_id)
    assert history.status == scan_details.info.status, u'Scan history should report same status as scan details.'

    # Test the scan host details API.
    assert len(scan_details.hosts
               ) > 0, u'Expected the scan to have at least one host.'
    host_details = client.scans_api.host_details(
        schedule_uuid=new_scan, host_id=scan_details.hosts[0].host_id)
    assert isinstance(host_details, ScanHostDetails), \
        u'The `host_details` method did not return type `ScanHostDetails`.'