Esempio n. 1
0
        def wrapper(*args, **kwargs):
            total_retries = int(TenableIOClient._TOTAL_RETRIES)
            count = 0
            sleep_ms = 0
            if 'headers' not in kwargs or not kwargs['headers']:
                kwargs['headers'] = {}

            while count <= total_retries:
                # Set retry count header
                if count > 0:
                    kwargs['headers'].update(
                        {u'X-Tio-Retry-Count': str(count)})
                count += 1

                try:
                    return f(*args, **kwargs)
                except TenableIORetryableApiException as exception:
                    if count > total_retries:
                        raise TenableIOApiException(exception.response)
                    sleep_ms += count * int(
                        TenableIOClient._RETRY_SLEEP_MILLISECONDS)
                    sleep(sleep_ms / 1000.0)
                    logging.warn(u'RETRY(%d/%d)AFTER(%dms):%s' %
                                 (count, total_retries, sleep_ms,
                                  format_request(exception.response)))
    def parse(path, tag=REPORT_HOST):
        """Parse Nessus XML export from Workbench API into dicts.

        :param path: The file path.
        :param tag: The XML tag to iterate on. It should be WorkbenchParser.REPORT_HOST or WorkbenchParser.REPORT_ITEM.
        """
        assert tag in [WorkbenchParser.REPORT_HOST, WorkbenchParser.REPORT_ITEM], u'Valid tag for parsing.'

        report_host = None
        host_properties = None
        report_items = [] if tag == WorkbenchParser.REPORT_HOST else None

        try:
            for event, elem in ET.iterparse(path, events=('start', 'end')):

                if event == 'start':
                    if elem.tag == 'ReportHost':
                        report_host = WorkbenchParser._from_report_host(elem)

                if event == 'end':

                    if elem.tag == WorkbenchParser.REPORT_HOST:
                        elem.clear()
                        if tag == elem.tag:
                            yield {
                                'report_host': report_host,
                                'host_properties': host_properties,
                                'report_items': report_items,
                            }
                            report_items = []

                    if elem.tag == WorkbenchParser.HOST_PROPERTIES:
                        host_properties = WorkbenchParser._from_host_properties(elem)
                        elem.clear()

                    if elem.tag == WorkbenchParser.REPORT_ITEM:
                        report_item = WorkbenchParser._from_report_item(elem)
                        elem.clear()
                        if tag == elem.tag:
                            yield report_item
                        elif tag == WorkbenchParser.REPORT_HOST:
                            report_items.append(report_item)
        except ET.ParseError as e:
            logging.warn(u'Failed to parse Nessus XML: ' + e.msg)
        def wrapper(*args, **kwargs):
            count = 0
            retry = True
            sleep_ms = 0

            while retry:
                retry = False
                try:
                    return f(*args, **kwargs)
                except TenableIORetryableApiException as exception:
                    count += 1

                    if count <= TenableIOClient.MAX_RETRIES:
                        retry = True
                        sleep_ms += count * TenableIOClient.RETRY_SLEEP_MILLISECONDS
                        logging.warn(u'RETRY(%d/%d)AFTER(%dms):%s' %
                                     (count, TenableIOClient.MAX_RETRIES, sleep_ms, format_request(exception.response)))
                        sleep(sleep_ms / 1000.0)
                    else:
                        raise TenableIOApiException(exception.response)