Пример #1
0
    def set_error_code(data_dict, return_obj):
        exception = None
        exception_str = None
        if 'exception' in data_dict:
            exception = data_dict['exception']
            exception_str = str(exception)

        error_code = ErrorMapper.DEFAULT_ERROR
        error_message = 'Error when converting STIX pattern to data source query'
        if exception_str:
            error_message += ': ' + exception_str

        if exception is not None:
            exception_type = type(exception).__name__
            ErrorMapper.logger.error("received exception => {}: {}".format(
                exception_type, exception))
            ErrorMapper.logger.debug(logger.exception_to_string(exception))
            if exception_type in error_mapping:
                error_code = error_mapping[exception_type][0]
                error_message = error_mapping[exception_type][1]
                exception_message = str(exception)
                if (len(exception_message) > 0):
                    if len(error_message) > 0:
                        error_message += ' : '
                    error_message += exception_message

        ErrorMapperBase.set_error_code(return_obj,
                                       error_code,
                                       message=error_message)
Пример #2
0
    def set_error_code(json_data, return_obj):
        error_code = ErrorMapper.DEFAULT_ERROR

        for error_package in error_mapping:
            search_type = error_package[0]
            search_criteria = error_package[1]
            result_error = error_package[2]

            if ErrorCaptureType.ERROR_MESSAGE == search_type:
                if 'error' in return_obj and search_criteria in return_obj[
                        'error']:
                    error_code = result_error
            elif ErrorCaptureType.STRUCT_VALUE == search_type:
                search_path = search_criteria[0]
                desired_value = search_criteria[1]
                stuct_search_result = ErrorResponder.get_struct_item(
                    json_data, search_path[:-1])
                if stuct_search_result is not None and search_path[
                        -1] in stuct_search_result and stuct_search_result[
                            search_path[-1]] == desired_value:
                    error_code = result_error

        if error_code == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.debug('failed to map: ' + str(json_data))

        if isinstance(error_code, tuple):
            ErrorMapperBase.set_error_code(return_obj, error_code[0],
                                           error_code[1])
        else:
            ErrorMapperBase.set_error_code(return_obj, error_code)
    def fill_error(return_object,
                   message_struct=None,
                   message_path=None,
                   message=None,
                   error=None,
                   connector=None):
        return_object['success'] = False
        if connector:
            return_object['connector'] = connector
        error_code = ErrorCode.TRANSMISSION_UNKNOWN

        if message is None:
            message = ''

        struct_item = ErrorResponder.get_struct_item(message_struct,
                                                     message_path)
        if struct_item is not None:
            if len(message) > 0:
                message += ';'
            if (isinstance(struct_item, list)):
                struct_item = json.dumps(struct_item)
            message += str(struct_item)
        error_msg = ''
        if error is not None:
            str_error = str(error)
            logger.error("error occurred: " + str_error)
            logger.debug(utils_logger.exception_to_string(error))
            if isinstance(error, SSLError):
                error_code = ErrorCode.TRANSMISSION_AUTH_SSL
                error_msg = 'Wrong certificate: ' + str_error
            elif isinstance(error, ConnectionError):
                error_code = ErrorCode.TRANSMISSION_CONNECT
                error_msg = 'Connection error: ' + str_error
            else:
                error_msg = str(error)

            if len(error_msg) > 0:
                if len(message) > 0:
                    message += '; '
                message += error_msg

        if message is not None and len(message) > 0:
            if error_code.value == ErrorCode.TRANSMISSION_UNKNOWN.value:
                if 'uthenticat' in message or 'uthoriz' in message or 'access denied' in message:
                    error_code = ErrorCode.TRANSMISSION_AUTH_CREDENTIALS
                elif 'query_syntax_error' in message:
                    error_code = ErrorCode.TRANSMISSION_QUERY_PARSING_ERROR
                elif 'Forbidden' in message or 'forbidden' in message:
                    error_code = ErrorCode.TRANSMISSION_FORBIDDEN
            message = '{} connector error => {}'.format(
                connector, str(message))
            return_object['error'] = str(message)
        ErrorMapperBase.set_error_code(return_object,
                                       error_code.value,
                                       connector=connector)
        if error_code == ErrorCode.TRANSMISSION_UNKNOWN:
            ErrorResponder.call_module_error_mapper(message_struct,
                                                    return_object, connector)
Пример #4
0
    def set_error_code(json_data, return_obj):
        code = None
        try:
            code = int(json_data['code'])
        except Exception:
            pass

        error_code = error_mapping.get(code, ErrorMapper.DEFAULT_ERROR)

        if error_code == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.error("failed to map: %s", json_data)

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #5
0
    def set_error_code(cls, json_data, return_obj):
        error_code = cls.DEFAULT_ERROR

        try:
            # Carbon Black Cloud stores error messages in 'translation_key'
            error_code = json_data['translation_key']
        except ValueError:
            cls.logger.debug(json_data)

        if error_code == ErrorMapper.DEFAULT_ERROR:
            cls.logger.error(f'failed to map: {str(json_data)}')

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #6
0
    def fill_error(return_object,
                   message_struct=None,
                   message_path=None,
                   message=None,
                   error=None):
        return_object['success'] = False
        error_code = ErrorCode.TRANSMISSION_UNKNOWN

        if message is None:
            message = ''

        struct_item = ErrorResponder.get_struct_item(message_struct,
                                                     message_path)
        if struct_item is not None:
            if len(message) > 0:
                message += ';'
            if (isinstance(struct_item, list)):
                struct_item = json.dumps(struct_item)
            message += str(struct_item)
        error_msg = ''
        if error is not None:
            str_error = str(error)
            #TODO replace with logger + stacktrace it to logger
            ErrorResponder.logger.error("error occurred: " + str_error)
            if isinstance(error, SSLError):
                error_code = ErrorCode.TRANSMISSION_AUTH_SSL
                error_msg = 'Wrong certificate: ' + str_error
            elif isinstance(error, ConnectionError):
                error_code = ErrorCode.TRANSMISSION_CONNECT
                error_msg = 'Connection error: ' + str_error
            else:
                error_msg = str(error)

            if len(error_msg) > 0:
                if len(message) > 0:
                    message += '; '
                message += error_msg

        if message is not None and len(message) > 0:
            if error_code.value == ErrorCode.TRANSMISSION_UNKNOWN.value:
                if 'uthenticat' in message or 'uthoriz' in message:
                    error_code = ErrorCode.TRANSMISSION_AUTH_CREDENTIALS
                elif 'query_syntax_error' in message:
                    error_code = ErrorCode.TRANSMISSION_QUERY_PARSING_ERROR
            return_object['error'] = str(message)
        ErrorMapperBase.set_error_code(return_object, error_code.value)
        if error_code == ErrorCode.TRANSMISSION_UNKNOWN:
            ErrorResponder.call_module_error_mapper(message_struct,
                                                    return_object)
Пример #7
0
    def set_error_code(json_data, return_obj):
        code = None
        try:
            code = json_data['code']
        except Exception:
            pass

        error_code = ErrorMapper.DEFAULT_ERROR
        if code in error_mapping:
            error_code = error_mapping[code]

        if error_code == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.error("failed to map: " + str(json_data))

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #8
0
    def set_error_code(json_data, return_obj):
        error_type = ''
        try:
            error_type = json_data['error']['type']
        except Exception:
            pass

        error_code = ErrorMapper.DEFAULT_ERROR

        if error_type in error_mapping:
            error_code = error_mapping[error_type]

        if error_code == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.error("failed to map: " + str(json_data))

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #9
0
    def set_error_code(json_data, return_obj):
        err_type = None
        try:
            err_type = json_data['type']
        except KeyError:
            pass

        error_type = ErrorMapper.DEFAULT_ERROR

        if err_type in error_mapping:
            error_type = error_mapping.get(err_type)

        if error_type == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.error("failed to map: %s", str(json_data))

        ErrorMapperBase.set_error_code(return_obj, error_type)
Пример #10
0
    def set_error_code(json_data, return_obj):
        """aws transmit specified error
         :param json_data: dict, error response of api_call
         :param return_obj: dict, returns error and error code"""
        error_type = ''
        try:
            error_type = json_data['__type']
        except KeyError:
            pass

        error_code = ErrorMapper.DEFAULT_ERROR

        if error_type in ERROR_MAPPING:
            error_code = ERROR_MAPPING[error_type]

        if error_code == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.debug("failed to map: " + str(json_data))

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #11
0
    def call_module_error_mapper(json_data, return_object):
        caller_path_list = traceback.extract_stack()[-3].filename.split('/')

        if 'stix_translation.py' in caller_path_list[-1]:
            module_path = 'stix_shifter_utils.stix_translation.stix_translation_error_mapper'
        else:
            caller_module_name = caller_path_list[-3:-1]
            module_path = 'stix_shifter_modules.' + caller_module_name[0] + '.'  + caller_module_name[1] + '.error_mapper'

        # path_start_position = ErrorResponder.rindex(caller_path_list, 'stix-shifter')
        # module_path = 'stix_shifter_modules.' + caller_module_name[0] + '.'  + caller_module_name[1] + '.' + caller_module_name[0] + '_error_mapper'
        # module_path = '.'.join(caller_path_list[path_start_position: -1]) + '.' + caller_module_name + '_error_mapper'
        try:
            module = importlib.import_module(module_path)
            if json_data is not None:
                module.ErrorMapper.set_error_code(json_data, return_object)
            else:
                ErrorMapperBase.set_error_code(return_object, module.ErrorMapper.DEFAULT_ERROR)
        except ModuleNotFoundError:
            pass
Пример #12
0
    def set_error_code(json_data, return_obj):
        message_text = None
        try:
            message_text = json_data['messages'][0]['text']
        except Exception as e:
            print("failed to find the message_0_text in: " + str(json_data))
            raise e

        error_code = ErrorMapper.DEFAULT_ERROR
        print('error code message: ' + message_text)

        for k, v in error_mapping.items():
            if k in message_text:
                error_code = v
                break

        if error_code == ErrorMapper.DEFAULT_ERROR:
            print("failed to map: " + str(json_data))

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #13
0
    def set_error_code(json_data, return_obj):
        """
        Parses API error response, maps native error status to STIX error, and saves to response object.

        :param json_data: native error response object
        :type json_data: map
        :param return_obj: standard error response object to return
        :type return_obj: map
        """
        code = None
        try:
            code = int(json_data['code'])
        except Exception:
            pass

        error_code = error_mapping.get(code, ErrorMapper.DEFAULT_ERROR)

        if error_code == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.error("failed to map: %s", json_data)

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #14
0
    def set_error_code(json_data, return_obj):
        """ms_atp transmit specified error
         :param json_data: dict, error response of api_call
         :param return_obj: dict, returns error and error code"""

        if isinstance(json_data, tuple):
            error_type = 'HTTPSConnectionError'
        else:
            try:
                error_type = json_data['error']['code']
            except Exception:
                error_type = json_data['error']

        error_code = ErrorMapper.DEFAULT_ERROR

        if error_type in ERROR_MAPPING:
            error_code = ERROR_MAPPING[error_type]

        if error_code == ErrorMapper.DEFAULT_ERROR:
            print("failed to map: " + str(json_data))

        ErrorMapperBase.set_error_code(return_obj, error_code)
Пример #15
0
    def set_error_code(json_data, return_obj):
        """arcsight transmit specified error
        :param json_data: dict, error response of api_call
        :param return_obj: dict, returns error and error code"""
        try:
            error_type = json_data['code']

        except Exception:
            error_type = json_data.__class__.__name__

        # logger down string error mapping
        if 'currently unavailable' in str(json_data):
            error_type = 'ConnectionError'

        error_code = ErrorMapper.DEFAULT_ERROR

        if error_type in ERROR_MAPPING:
            error_code = ERROR_MAPPING[error_type]

        if error_code == ErrorMapper.DEFAULT_ERROR:
            ErrorMapper.logger.debug("failed to map: " + str(json_data))

        ErrorMapperBase.set_error_code(return_obj, error_code)