def test_get_active_configuration_second_exceptions(agent_id, component, config, exception_): """This test checks the fourth exception.""" with patch('wazuh.core.configuration.OssecSocket.__init__', return_value=None): with patch('wazuh.core.configuration.OssecSocket.send', side_effect=None): with patch('wazuh.core.configuration.OssecSocket.receive', side_effect=ValueError): with pytest.raises(WazuhInternalError, match=f".* {exception_} .*"): configuration.get_active_configuration(agent_id, component, config)
def test_get_active_configuration(agent_id, component, config, msg): """This test checks the propper working of get_active_configuration function.""" with patch('wazuh.core.configuration.OssecSocket.__init__', return_value=None): with patch('wazuh.core.configuration.OssecSocket.send', side_effect=None): with patch('wazuh.core.configuration.OssecSocket.receive', return_value=f'ok {msg}'.encode()): with patch('wazuh.core.configuration.OssecSocket.close', side_effect=None): if json.loads(msg).get('auth', {}).get('use_password') == 'yes': result = configuration.get_active_configuration( agent_id, component, config) assert 'authd.pass' not in result with patch('builtins.open', mock_open(read_data='test_password')): result = configuration.get_active_configuration( agent_id, component, config) assert result['authd.pass'] == 'test_password' else: result = configuration.get_active_configuration( agent_id, component, config) assert 'authd.pass' not in result
def test_get_active_configuration_first_exceptions(exception_type, agent_id, component, config, exception_): """This test checks the first three exceptions.""" with patch('wazuh.core.configuration.OssecSocket.__init__', return_value=Exception): with pytest.raises(exception_type, match=f".* {exception_} .*"): configuration.get_active_configuration(agent_id, component, config)
def test_get_active_configuration_third_exceptions(agent_id, component, config, exception_): """This test checks the last exception.""" with patch('wazuh.core.configuration.OssecSocket.__init__', return_value=None): with patch('wazuh.core.configuration.OssecSocket.send', side_effect=None): with patch('wazuh.core.configuration.OssecSocket.receive', return_value=b'test 1'): with patch('wazuh.core.configuration.OssecSocket.close', side_effect=None): with pytest.raises(WazuhError, match=f".* {exception_} .*"): configuration.get_active_configuration(agent_id, component, config)
def getconfig(self, component: str = '', config: str = '', agent_version: str = '') -> dict: """Read agent's loaded configuration. Parameters ---------- component : str Selected component of the agent configuration. config : str Agent's active configuration to get. agent_version : str Agent version to compare with the required version. The format is vX.Y.Z or Wazuh vX.Y.Z. Raises ------ WazuhError(1735) The agent version is older than the minimum required version. Returns ------- dict Agent's active configuration. """ if WazuhVersion(agent_version) < WazuhVersion( common.ACTIVE_CONFIG_VERSION): raise WazuhInternalError( 1735, extra_message= f"Minimum required version is {common.ACTIVE_CONFIG_VERSION}") return configuration.get_active_configuration(self.id, component, config)
def test_get_active_configuration_fourth_exception(agent_id, component, config, exception_): with patch('wazuh.core.configuration.OssecSocket.__init__', return_value=None): with patch('wazuh.core.configuration.OssecSocket.send', side_effect=None): with patch('wazuh.core.configuration.OssecSocket.receive', return_value=b'ok {"a": "2"}'): with patch('wazuh.core.configuration.OssecSocket.close', side_effect=None): assert { "a": "2" } == configuration.get_active_configuration( agent_id, component, config)
def get_config(component=None, config=None): """ Wrapper for get_active_configuration :param component: Selected component. :param config: Configuration to get, written on disk. :return: AffectedItemsWazuhResult. """ result = AffectedItemsWazuhResult(all_msg=f"Active configuration read successfully" f"{' in specified node' if node_id != 'manager' else ''}", some_msg='Could not read active configuration in some nodes', none_msg=f"Could not read active configuration" f"{' in specified node' if node_id != 'manager' else ''}" ) try: data = configuration.get_active_configuration(agent_id='000', component=component, configuration=config) len(data.keys()) > 0 and result.affected_items.append(data) except WazuhError as e: result.add_failed_item(id_=node_id, error=e) result.total_affected_items = len(result.affected_items) return result