Exemplo n.º 1
0
async def get_group_file_json(request,
                              group_id,
                              file_name,
                              pretty=False,
                              wait_for_complete=False):
    """Get the files placed under the group directory in json format.

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    :param group_id: Group ID.
    :param file_name: Filename
    :return: File data in JSON
    """
    f_kwargs = {
        'group_list': [group_id],
        'filename': file_name,
        'type_conf': request.query.get('type', None),
        'return_format': 'json'
    }

    dapi = DistributedAPI(
        f=agent.get_file_conf,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='local_master',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data['data']['affected_items'])

    return web.json_response(data=response,
                             status=200,
                             dumps=prettify if pretty else dumps)
Exemplo n.º 2
0
async def get_stats_weekly(request, pretty=False, wait_for_complete=False):
    """Get manager's or local_node's stats by week.

    Returns Wazuh statistical information per week. Each number in the averages field represents the average of alerts
    per hour for that specific day.

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    """
    f_kwargs = {}

    dapi = DistributedAPI(
        f=stats.weekly,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='local_any',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response,
                             status=200,
                             dumps=prettify if pretty else dumps)
Exemplo n.º 3
0
async def post_new_agent(request,
                         agent_name,
                         pretty=False,
                         wait_for_complete=False):
    """Add agent (quick method)

    Adds a new agent with name `agent_name`. This agent will use `any` as IP.'

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    :param agent_name: Agent name used when the agent was registered.
    :return: AgentIdKeyData
    """
    f_kwargs = {
        'name': agent_name,
        'use_only_authd': configuration.api_conf['use_only_authd']
    }

    dapi = DistributedAPI(
        f=agent.add_agent,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='local_master',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response,
                             status=200,
                             dumps=prettify if pretty else dumps)
Exemplo n.º 4
0
async def get_group_config(request,
                           group_id,
                           pretty=False,
                           wait_for_complete=False,
                           offset=0,
                           limit=database_limit):
    """Get group configuration defined in the `agent.conf` file.

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    :param group_id: Group ID.
    :param offset: First element to return in the collection
    :param limit: Maximum number of elements to return
    :return: GroupConfiguration
    """
    f_kwargs = {'group_list': [group_id], 'offset': offset, 'limit': limit}

    dapi = DistributedAPI(
        f=agent.get_agent_conf,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='local_master',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])
    data = raise_if_exc(await dapi.distribute_function())

    return web.json_response(data=Data(data),
                             status=200,
                             dumps=prettify if pretty else dumps)
Exemplo n.º 5
0
async def get_agent_summary_status(request,
                                   pretty=False,
                                   wait_for_complete=False):
    """Get agents status summary.

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    :return: AgentsSummaryStatus
    """
    f_kwargs = {}

    dapi = DistributedAPI(
        f=agent.get_agents_summary_status,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='local_master',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response,
                             status=200,
                             dumps=prettify if pretty else dumps)
Exemplo n.º 6
0
async def get_stats_remoted_node(request, node_id, pretty=False, wait_for_complete=False):
    """Get a specified node's remoted stats.

    :param node_id: Cluster node name.
    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    """
    f_kwargs = {'node_id': node_id,
                'filename': common.remoted_stats}

    nodes = await get_system_nodes()
    dapi = DistributedAPI(f=stats.get_daemons_stats,
                          f_kwargs=remove_nones_to_dict(f_kwargs),
                          request_type='distributed_master',
                          is_async=False,
                          wait_for_complete=wait_for_complete,
                          logger=logger,
                          rbac_permissions=request['token_info']['rbac_policies'],
                          nodes=nodes,
                          cluster_required=True
                          )
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response, status=200, dumps=prettify if pretty else dumps)
Exemplo n.º 7
0
async def get_stats_weekly_node(request, node_id, pretty=False, wait_for_complete=False):
    """Get a specified node's stats by week. 

    Returns Wazuh statistical information in node {node_id} per week. Each number in the averages field represents the
    average of alerts per hour for that specific day.

    :param node_id: Cluster node name.
    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    """
    f_kwargs = {'node_id': node_id}

    nodes = await get_system_nodes()
    dapi = DistributedAPI(f=stats.weekly,
                          f_kwargs=remove_nones_to_dict(f_kwargs),
                          request_type='distributed_master',
                          is_async=False,
                          wait_for_complete=wait_for_complete,
                          logger=logger,
                          rbac_permissions=request['token_info']['rbac_policies'],
                          nodes=nodes,
                          cluster_required=True
                          )
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response, status=200, dumps=prettify if pretty else dumps)
Exemplo n.º 8
0
async def add_agent(request, pretty=False, wait_for_complete=False):
    """Add a new Wazuh agent.

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    :return: AgentIdKey
    """
    # Get body parameters
    Body.validate_content_type(request,
                               expected_content_type='application/json')
    f_kwargs = await AgentAddedModel.get_kwargs(request)

    # Get IP if not given
    if not f_kwargs['ip']:
        if configuration.api_conf['behind_proxy_server']:
            try:
                f_kwargs['ip'] = request.headers['X-Forwarded-For']
            except KeyError:
                raise_if_exc(WazuhError(1120))
        else:
            peername = request.transport.get_extra_info('peername')
            if peername is not None:
                f_kwargs['ip'], _ = peername
    f_kwargs['use_only_authd'] = configuration.api_conf['use_only_authd']

    dapi = DistributedAPI(
        f=agent.add_agent,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='local_master',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])

    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response,
                             status=200,
                             dumps=prettify if pretty else dumps)
Exemplo n.º 9
0
async def get_stats_remoted(request, pretty=False, wait_for_complete=False):
    """Get manager's or local_node's remoted stats.

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    """
    f_kwargs = {'filename': common.remoted_stats}

    dapi = DistributedAPI(
        f=stats.get_daemons_stats,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='local_any',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response,
                             status=200,
                             dumps=prettify if pretty else dumps)
Exemplo n.º 10
0
async def get_agent_config(request,
                           pretty=False,
                           wait_for_complete=False,
                           agent_id=None,
                           component=None,
                           **kwargs):
    """Get active configuration

    Returns the active configuration the agent is currently using. This can be different from the
    configuration present in the configuration file, if it has been modified and the agent has
    not been restarted yet.

    :param pretty: Show results in human-readable format
    :param wait_for_complete: Disable timeout response
    :param agent_id: Agent ID. All posible values since 000 onwards.
    :param component: Selected agent's component.
    :return: AgentConfiguration
    """
    f_kwargs = {
        'agent_list': [agent_id],
        'component': component,
        'config': kwargs.get('configuration', None)
    }

    dapi = DistributedAPI(
        f=agent.get_agent_config,
        f_kwargs=remove_nones_to_dict(f_kwargs),
        request_type='distributed_master',
        is_async=False,
        wait_for_complete=wait_for_complete,
        logger=logger,
        rbac_permissions=request['token_info']['rbac_policies'])
    data = raise_if_exc(await dapi.distribute_function())
    response = Data(data)

    return web.json_response(data=response,
                             status=200,
                             dumps=prettify if pretty else dumps)