Beispiel #1
0
def test_run_command(cmd_patch, agent_patch, queue_patch, expected_exception,
                     agent_id, command, arguments, custom):
    """
    Tests run_command function
    """
    agent_patch.return_value.get_basic_information.return_value = {
        'status': 'disconnected' if expected_exception else 'active'
    }
    queue_patch.return_value.send_msg_to_agent.return_value = "success"
    queue_patch.AR_TYPE = "AR"

    if expected_exception is not None:
        with pytest.raises(WazuhException,
                           match=f'.* {expected_exception} .*'):
            active_response.run_command(agent_id, command, arguments, custom)
    else:
        ret = active_response.run_command(agent_id, command, arguments, custom)
        assert ret == "success"
        handle = queue_patch()
        msg = f'{"!" if custom else ""}{command} {"- -" if not arguments else " ".join(arguments)}'
        if agent_id != 'all':
            handle.send_msg_to_agent.assert_called_with(agent_id=agent_id,
                                                        msg=msg,
                                                        msg_type='AR')
        else:
            handle.send_msg_to_agent.assert_called_with(agent_id=None,
                                                        msg=msg,
                                                        msg_type='AR')
Beispiel #2
0
def test_run_command(mock_get_agents_info, mock_close, mock_send, mock_conn, message_exception,
                     send_exception, agent_id, command, arguments, custom, alert, version):
    """Verify the proper operation of active_response module.

    Parameters
    ----------
    message_exception : int
        Exception code expected when calling create_message.
    send_exception : int
        Exception code expected when calling send_command.
    agent_id : list
        Agents on which to execute the Active response command.
    command : string
        Command to be executed on the agent.
    arguments : list
        Arguments of the command.
    custom : boolean
        True if command is a script.
    version : list
        List with the agent version to test whether the message sent was the correct one or not.
    """
    with patch('wazuh.core.agent.Agent.get_basic_information',
               return_value=agent_info_exception_and_version(send_exception, version)):
        with patch('wazuh.core.agent.Agent.getconfig', return_value=agent_config(send_exception)):
            if message_exception:
                ret = run_command(agent_list=agent_id, command=command, arguments=arguments, custom=custom, alert=alert)
                assert ret.render()['data']['failed_items'][0]['error']['code'] == message_exception
            else:
                ret = run_command(agent_list=agent_id, command=command, arguments=arguments, custom=custom, alert=alert)
                if send_exception:
                    assert ret.render()['message'] == 'AR command was not sent to any agent'
                    assert ret.render()['data']['failed_items'][0]['error']['code'] == send_exception
                else:
                    assert ret.render()['message'] == 'AR command was sent to all agents'
def test_run_command(mock_close,  mock_send, mock_conn, message_exception, send_exception, agent_id, command,
                     arguments, custom):
    """Verify the proper operation of active_response module.

    Parameters
    ----------
    message_exception : int
        Exception code expected when calling create_message.
    send_exception : int
        Exception code expected when calling send_command.
    agent_id : list
        Agents on which to execute the Active response command.
    command : string
        Command to be executed on the agent.
    arguments : list, optional
        Arguments of the command.
    custom : boolean
        True if command is a script.
    """
    with patch('wazuh.core.agent.Agent.get_basic_information', return_value=agent_info(send_exception)):
        with patch('wazuh.core.agent.Agent.getconfig', return_value=agent_config(send_exception)):
            if message_exception:
                with pytest.raises(WazuhError, match=f'.* {message_exception} .*'):
                    run_command(agent_list=agent_id, command=command, arguments=arguments, custom=custom)
            else:
                ret = run_command(agent_list=agent_id, command=command, arguments=arguments, custom=custom)
                if send_exception:
                    assert ret.render()['message'] == 'Could not send command to any agent'
                else:
                    assert ret.render()['message'] == 'Command sent to all agents'