Exemple #1
0
def test_get_manager_status():
    """Check that get_manager function returns the manager status,
    for this test, the status can be stopped or failed."""
    status = utils.get_manager_status()
    for value in status.values():
        assert value == 'stopped'

    with patch('wazuh.core.cluster.utils.glob', return_value=['ossec-0.pid']):
        with patch('re.match', return_value='None'):
            status = utils.get_manager_status()
            for value in status.values():
                assert value == 'failed'
Exemple #2
0
    def _add(self, name, ip, id=None, key=None, force=None):
        """Add an agent to Wazuh.
        2 uses:
            - name and ip [force]: Add an agent like manage_agents (generate id and key).
            - name, ip, id, key [force]: Insert an agent with an existing id and key.

        Parameters
        ----------
        name : str
            Name of the new agent.
        ip : str
            IP of the new agent. It can be an IP, IP/NET or ANY.
        id : str
            ID of the new agent.
        key : str
            Key of the new agent.
        force : dict
            Remove old agents with same name or IP if conditions are met.

        Raises
        ------
        WazuhError(1706)
            If there is an agent with the same IP or the IP is invalid.
        WazuhInternalError(1725)
            If there was an error registering a new agent.
        WazuhError(1726)
            If authd is not running.

        Returns
        -------
        Agent ID.
        """
        ip = ip.lower()
        if ip != 'any':
            if ip.find('/') > 0:
                try:
                    ipaddress.ip_network(ip)
                except Exception:
                    raise WazuhError(1706, extra_message=ip)
            else:
                try:
                    ipaddress.ip_address(ip)
                except Exception:
                    raise WazuhError(1706, extra_message=ip)

        manager_status = get_manager_status()
        is_authd_running = 'wazuh-authd' in manager_status and manager_status[
            'wazuh-authd'] == 'running'

        if not is_authd_running:
            raise WazuhError(1726)

        try:
            self._add_authd(name, ip, id, key, force)
        except WazuhException as e:
            raise e
        except Exception as e:
            raise WazuhInternalError(1725, extra_message=str(e))
Exemple #3
0
    def remove(self, purge: bool = False) -> str:
        """Delete the agent.

        Parameters
        ----------
        purge : boolean
            Remove key from store.

        Raises
        ------
        WazuhError(1726)
            Authd is not running.

        WazuhInternalError(1757)
            Unhandled exception.

        Returns
        -------
        data : str
            Message generated by Wazuh.
        """

        manager_status = get_manager_status(cache=True)
        is_authd_running = 'wazuh-authd' in manager_status and manager_status[
            'wazuh-authd'] == 'running'

        if not is_authd_running:
            raise WazuhError(1726)

        try:
            data = self._remove_authd(purge)

            return data
        except WazuhException as e:
            raise e
        except Exception as e:
            raise WazuhInternalError(1757, extra_message=str(e))
Exemple #4
0
def status():
    """ Returns the Manager processes that are running. """

    return get_manager_status()