Example #1
0
def test_get_wpk_file(grp_mock, pwd_mock, versions_mock, get_req_mock, open_mock, sha1_mock, test_data, agent_id):
    """
    Test _get_wpk_file method
    """
    def get_manager_info(available_versions):
        """
        Return hash from manager version in available_versions list
        """
        for version in available_versions:
            if WazuhVersion(version[0]) == WazuhVersion(get_manager_version()):
                return version[0], version[1]
        raise Exception  # raise an exception if there is not hash for manager version

    def get_package_version(package_name):
        """
        Return package version from package_name
        """
        return re.search(r'^wazuh_agent_(v\d+\.\d+\.\d+)\w+\.wpk$', package_name).group(1)

    # mock _get_versions method with a list of available versions
    versions_mock.return_value = wpk_versions

    with patch('sqlite3.connect') as mock_db:
        mock_db.return_value = test_data.global_db
        agent = Agent(agent_id)
        agent._load_info_from_DB()
        # mock return value of hexdigest function
        manager_version, hash_manager_version = get_manager_info(wpk_versions)
        sha1_mock.return_value.hexdigest.return_value = hash_manager_version

        result = agent._get_wpk_file()

        assert get_package_version(result[0]) == manager_version
        assert result[1] == hash_manager_version
Example #2
0
def get_info() -> Dict:
    """
    Returns manager configuration with cluster details

    :return: Dictionary with information about manager and cluster
    """
    # get name from agent 000
    manager = Agent(id=0)
    manager._load_info_from_DB()

    # read cluster configuration
    cluster_config = read_cluster_config()

    # get manager status
    cluster_info = get_cluster_status()
    # add 'name', 'node_name' and 'node_type' to cluster_info
    for name in ('name', 'node_name', 'node_type'):
        cluster_info[name] = cluster_config[name]

    # merge manager information into an unique dictionary
    manager_info = {
        **Wazuh(common.ossec_path).get_ossec_init(),
        **{
            'name': manager.name,
            'cluster': cluster_info
        }
    }

    return manager_info
Example #3
0
def get_manager_version():
    """
    Get manager version
    """
    manager = Agent(id=0)
    manager._load_info_from_DB()

    return manager.version
Example #4
0
def test_get_available_versions(requests_mock, test_data, agent_id):
    """
    Test _get_versions method
    """
    # regex for checking SHA-1 hash
    regex_sha1 = re.compile(r'^[0-9a-f]{40}$')

    with patch('sqlite3.connect') as mock_db:
        mock_db.return_value = test_data.global_db
        manager_version = get_manager_version()
        agent = Agent(agent_id)
        agent._load_info_from_DB()
        # mock request with available versions from server
        requests_mock.return_value.get.return_value = wpk_versions
        available_versions = agent._get_versions()

        for version in available_versions:
            assert WazuhVersion(version[0]) <= WazuhVersion(manager_version)
            assert re.search(regex_sha1, version[1])
Example #5
0
def main():
    # Check arguments
    if args.list_outdated:
        list_outdated()
        exit(0)

    if not args.agent:
        arg_parser.print_help()
        exit(0)

    if args.silent:
        args.debug = False

    # Capture Ctrl + C
    signal(SIGINT, signal_handler)

    # Initialize framework
    myWazuh = Wazuh(get_init=True)

    agent = Agent(id=args.agent)
    agent._load_info_from_DB()

    agent_info = "{0}/queue/agent-info/{1}-{2}".format(common.ossec_path,
                                                       agent.name, agent.ip)
    if not os.path.isfile(agent_info):
        raise WazuhException(1720)

    # Custom WPK file
    if args.file:
        if args.execute:
            upgrade_command_result = agent.upgrade_custom(
                file_path=args.file,
                installer=args.execute,
                debug=args.debug,
                show_progress=print_progress if not args.silent else None,
                chunk_size=args.chunk_size,
                rl_timeout=args.timeout)
            if not args.silent:
                if not args.debug:
                    print(
                        "\n{0}... Please wait.".format(upgrade_command_result))
                else:
                    print(upgrade_command_result)

            counter = 0
            agent_info_stat = os.stat(agent_info).st_mtime

            sleep(10)
            while agent_info_stat == os.stat(
                    agent_info
            ).st_mtime and counter < common.agent_info_retries:
                sleep(common.agent_info_sleep)
                counter = counter + 1

            if agent_info_stat == os.stat(agent_info).st_mtime:
                raise WazuhException(
                    1716, "Timeout waiting for agent reconnection.")

            upgrade_result = agent.upgrade_result(debug=args.debug)
            if not args.silent:
                print(upgrade_result)
        else:
            print("Error: Need executable filename.")

    # WPK upgrade file
    else:
        prev_ver = agent.version
        upgrade_command_result = agent.upgrade(
            wpk_repo=args.repository,
            debug=args.debug,
            version=args.version,
            force=args.force,
            show_progress=print_progress if not args.silent else None,
            chunk_size=args.chunk_size,
            rl_timeout=args.timeout)
        if not args.silent:
            if not args.debug:
                print("\n{0}... Please wait.".format(upgrade_command_result))
            else:
                print(upgrade_command_result)

        counter = 0
        agent_info_stat = os.stat(agent_info).st_mtime

        while agent_info_stat == os.stat(
                agent_info).st_mtime and counter < common.agent_info_retries:
            sleep(common.agent_info_sleep)
            counter = counter + 1

        if agent_info_stat == os.stat(agent_info).st_mtime:
            raise WazuhException(1716,
                                 "Timeout waiting for agent reconnection.")

        sleep(10)
        upgrade_result = agent.upgrade_result(debug=args.debug)
        if not args.silent:
            if not args.debug:
                agent._load_info_from_DB()
                print("Agent upgraded: {0} -> {1}".format(
                    prev_ver, agent.version))
            else:
                print(upgrade_result)
Example #6
0
def main():
    # Capture Ctrl + C
    signal(SIGINT, signal_handler)

    # Check arguments
    if args.list_outdated:
        list_outdated()
        exit(0)

    if not args.agent:
        arg_parser.print_help()
        exit(0)

    if args.silent:
        args.debug = False

    use_http = False
    if args.http:
        use_http = True

    agent = Agent(id=args.agent)
    agent._load_info_from_DB()

    agent_info = "{0}/queue/agent-info/{1}-{2}".format(common.ossec_path, agent.name, agent.registerIP)
    if not os.path.isfile(agent_info):
        raise WazuhException(1720)

    # Evaluate if the version is correct
    if args.version is not None:
        pattern = re.compile("v[0-9]+\.[0-9]+\.[0-9]+")
        if not pattern.match(args.version):
            raise WazuhException(1733, "Version received: {0}".format(args.version))

    if args.chunk_size is not None:
        if args.chunk_size < 1 or args.chunk_size > 64000:
            raise WazuhException(1744, "Chunk defined: {0}".format(args.chunk_size))

    # Custom WPK file
    if args.file:
        upgrade_command_result = agent.upgrade_custom(file_path=args.file,
                                                      installer=args.execute if args.execute else "upgrade.sh",
                                                      debug=args.debug,
                                                      show_progress=print_progress if not args.silent else None,
                                                      chunk_size=args.chunk_size,
                                                      rl_timeout=-1 if args.timeout == None else args.timeout)
        if not args.silent:
            if not args.debug:
                print("\n{0}... Please wait.".format(upgrade_command_result))
            else:
                print(upgrade_command_result)

        counter = 0
        agent_info_stat = os.stat(agent_info).st_mtime

        sleep(10)
        while agent_info_stat == os.stat(agent_info).st_mtime and counter < common.agent_info_retries:
            sleep(common.agent_info_sleep)
            counter = counter + 1

        if agent_info_stat == os.stat(agent_info).st_mtime:
            raise WazuhException(1716, "Timeout waiting for agent reconnection.")

        upgrade_result = agent.upgrade_result(debug=args.debug)
        if not args.silent:
            print(upgrade_result)

    # WPK upgrade file
    else:
        prev_ver = agent.version
        upgrade_command_result = agent.upgrade(wpk_repo=args.repository, debug=args.debug, version=args.version,
                                               force=args.force,
                                               show_progress=print_progress if not args.silent else None,
                                               chunk_size=args.chunk_size,
                                               rl_timeout=-1 if args.timeout == None else args.timeout, use_http=use_http)
        if not args.silent:
            if not args.debug:
                print("\n{0}... Please wait.".format(upgrade_command_result))
            else:
                print(upgrade_command_result)

        counter = 0
        agent_info_stat = os.stat(agent_info).st_mtime

        while agent_info_stat == os.stat(agent_info).st_mtime and counter < common.agent_info_retries:
            sleep(common.agent_info_sleep)
            counter = counter + 1

        if agent_info_stat == os.stat(agent_info).st_mtime:
            raise WazuhException(1716, "Timeout waiting for agent reconnection.")

        sleep(10)
        upgrade_result = agent.upgrade_result(debug=args.debug)
        if not args.silent:
            if not args.debug:
                agent._load_info_from_DB()
                print("Agent upgraded: {0} -> {1}".format(prev_ver, agent.version))
            else:
                print(upgrade_result)