def test_workbenches_export(client):
    file_id = client.workbenches_api.export_request(
        WorkbenchesApi.FORMAT_NESSUS,
        WorkbenchesApi.REPORT_VULNERABILITIES,
        WorkbenchesApi.CHAPTER_VULN_BY_ASSET,
    )
    assert file_id, u'The `export_request` method did not return a valid file ID.'

    export_status = wait_until(
        lambda: client.workbenches_api.export_status(file_id),
        lambda status: status == WorkbenchesApi.STATUS_EXPORT_READY)

    assert export_status == WorkbenchesApi.STATUS_EXPORT_READY, u'Workbench export is not ready.'

    iter_content = client.workbenches_api.export_download(file_id, False)
    download_path = 'test_workbench_export'

    assert not os.path.isfile(
        download_path), u'Workbench report does not exist.'

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

    os.remove(download_path)
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)
Exemplo n.º 3
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.'
def test_export_assets(client):
    # Create export request
    export_uuid = client.exports_api.assets_request_export(
        ExportsAssetsRequest(chunk_size=100))
    assert export_uuid, u'The `assets_request_export` method returns a valid export UUID'
    # Check status
    export_status = client.exports_api.assets_export_status(export_uuid)
    assert isinstance(
        export_status, ExportsAssetsStatus
    ), u'The `assets_export_status` method return did not return type `ExportsAssetsStatus`.'

    wait_until(
        lambda: client.exports_api.assets_export_status(export_uuid),
        lambda status: status.status == ExportsAssetsStatus.STATUS_FINISHED)

    # Export results
    for chunk_id in export_status.chunks_available[:1]:
        asset_list = client.exports_api.assets_chunk(export_uuid, chunk_id)
        for asset in asset_list:
            assert isinstance(
                asset, AssetsExport
            ), u'The list should only countain `AssetsExport` object type.'
def test_export_vulns(client):
    # Create export request
    export_uuid = client.exports_api.vulns_request_export(
        ExportsVulnsRequest(filters={'since': 1451606400}))
    assert export_uuid, u'The `vulns_request_export` method returns a valid export UUID'
    # Check status
    export_status = client.exports_api.vulns_export_status(export_uuid)
    assert isinstance(
        export_status, ExportsVulnsStatus
    ), u'The `vulns_export_status` method return did not return type `ExportsVulnsStatus`.'

    wait_until(
        lambda: client.exports_api.vulns_export_status(export_uuid),
        lambda status: status.status == ExportsVulnsStatus.STATUS_FINISHED)
    export_status = client.exports_api.vulns_export_status(export_uuid)

    # Export results
    vuln_list = client.exports_api.vulns_chunk(
        export_uuid, export_status.chunks_available[0])
    for vuln in vuln_list:
        assert isinstance(
            vuln, VulnsExport
        ), u'The list should only countain `VulnsExport` object type.'
Exemplo n.º 6
0
def test_sc_policy_compliance(client):
    image = upload_image('test_sc_policy_compliance', 'test_sc_policy_compliance')
    policy = wait_until(lambda: client.sc_policy_api.compliance(image['id']),
                        lambda response: response[u'status'] != u'error')
    assert policy[u'status'] in [u'pass', u'fail'], \
        u'Status should be either pass or fail based on the policy'
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`.'