Exemple #1
0
    def __init__(self,
                 input_json: Dict,
                 logger: logging.Logger,
                 node: c_common.Handler = None,
                 debug: bool = False,
                 pretty: bool = False):
        """
        Class constructor

        :param input_json: JSON containing information/arguments about the request.
        :param logger: Logging logger to use
        :param node: Asyncio protocol object to use when sending requests to other nodes
        :param debug: Enable debug messages and raise exceptions.
        :param pretty: Return request result with pretty indent
        """
        self.logger = logger
        self.input_json = input_json
        self.node = node if node is not None else local_client
        self.cluster_items = cluster.get_cluster_items(
        ) if node is None else node.cluster_items
        self.debug = debug
        self.pretty = pretty
        self.node_info = cluster.get_node() if node is None else node.get_node(
        )
        self.request_id = str(random.randint(0, 2**10 - 1))
Exemple #2
0
def distribute_function(input_json, pretty=False, debug=False):
    """
    Distributes an API call.

    :param input_json: API call to execute.
    :param pretty: JSON pretty print.
    :param debug: whether to raise an exception or return an error.
    :return: a JSON response
    """
    try:
        node_info = cluster.get_node()
        request_type = rq.functions[input_json['function']]['type']
        is_dapi_enabled = cluster.get_cluster_items(
        )['distributed_api']['enabled']
        logger.debug("[Cluster] [D API        ] Distributed API is {}.".format(
            "enabled" if is_dapi_enabled else "disabled"))

        if 'wait_for_complete' not in input_json['arguments']:
            input_json['arguments']['wait_for_complete'] = False

        # First case: execute the request local.
        # If the distributed api is not enabled
        # If the cluster is disabled or the request type is local_any
        # if the request was made in the master node and the request type is local_master
        # if the request came forwarded from the master node and its type is distributed_master
        if not is_dapi_enabled or not cluster.check_cluster_status() or request_type == 'local_any' or\
                (request_type == 'local_master' and node_info['type'] == 'master')   or\
                (request_type == 'distributed_master' and input_json['from_cluster']):

            del input_json['arguments'][
                'wait_for_complete']  # local requests don't use this parameter
            return execute_local_request(input_json, pretty, debug)

        # Second case: forward the request
        # Only the master node will forward a request, and it will only be forwarded if its type is distributed_master
        elif request_type == 'distributed_master' and node_info[
                'type'] == 'master':
            return forward_request(input_json, node_info['node'], pretty,
                                   debug)

        # Last case: execute the request remotely.
        # A request will only be executed remotely if it was made in a worker node and its type isn't local_any
        else:
            return execute_remote_request(input_json, pretty)
    except WazuhException as e:
        return print_json(data=e.message, error=e.code, pretty=pretty)
    except Exception as e:
        if debug:
            raise
        return print_json(data=str(e), error=1000, pretty=pretty)