def test_get_list_from_file(raw): """Test basic `get_list_from_file` core functionality. `get_list_from_file` must retrieve the content of a CDB file. """ full_path = os.path.join(common.wazuh_path, PATH_FILE) if raw: with open(full_path) as f: assert get_list_from_file(full_path, raw) == f.read() else: assert get_list_from_file(full_path, raw) == CONTENT_FILE
def get_list_file(filename=None, raw=None): """Get a CDB list file content. The file is recursively searched. Parameters ---------- filename : list Full path of CDB list file to get. raw : bool, optional Respond in raw format. Returns ------- result : AffectedItemsWazuhResult CDB list content. """ result = AffectedItemsWazuhResult(all_msg='CDB list was returned', none_msg='No list was returned') try: # Recursively search for filename inside {wazuh_path}/etc/lists/ content = get_list_from_file(get_filenames_paths(filename)[0], raw) if raw: result = content else: result.affected_items.append(content) result.total_affected_items = 1 except WazuhError as e: result.add_failed_item(id_=filename[0], error=e) return result
def get_lists(path=None, offset=0, limit=common.database_limit, select=None, sort_by=None, sort_ascending=True, search_text=None, complementary_search=False, search_in_fields=None, relative_dirname=None, filename=None): """Get CDB lists :param path: Relative path of list file to get (if it is not specified, all lists will be returned) :param offset: First item to return. :param limit: Maximum number of items to return. :param select: List of selected fields to return :param sort_by: Fields to sort the items by :param sort_ascending: Sort in ascending (true) or descending (false) order :param search_text: Text to search :param complementary_search: Find items without the text to search :param search_in_fields: Fields to search in :param relative_dirname: Filters by relative dirname. :param filename: List of filenames to filter by. :return: AffectedItemsWazuhResult """ result = AffectedItemsWazuhResult(none_msg='No list was shown', some_msg='Some lists could not be shown', all_msg='All specified lists were shown') lists = list() for rel_p in path: if not any([ relative_dirname is not None and os.path.dirname(rel_p) != relative_dirname, filename is not None and os.path.split(rel_p)[1] not in filename ]): lists.append({ 'items': get_list_from_file(rel_p), 'relative_dirname': os.path.dirname(rel_p), 'filename': os.path.split(rel_p)[1] }) data = process_array(lists, search_text=search_text, search_in_fields=search_in_fields, complementary_search=complementary_search, sort_by=sort_by, sort_ascending=sort_ascending, offset=offset, limit=limit, select=select, allowed_sort_fields=SORT_FIELDS, required_fields=REQUIRED_FIELDS) result.affected_items = data['items'] result.total_affected_items = data['totalItems'] return result
def get_lists(filename=None, offset=0, limit=common.database_limit, select=None, sort_by=None, sort_ascending=True, search_text=None, complementary_search=False, search_in_fields=None, relative_dirname=None): """Get CDB lists content. Parameters ---------- filename : list Filenames to filter by. offset : int First item to return. limit : int Maximum number of items to return. select : list List of selected fields to return. sort_by : dict Fields to sort the items by. Format: {"fields":["field1","field2"],"order":"asc|desc"} sort_ascending : boolean Sort in ascending (true) or descending (false) order. search_text : str Find items with the specified string. complementary_search : bool If True, only results NOT containing `search_text` will be returned. If False, only results that contains `search_text` will be returned. search_in_fields : str Name of the field to search in for the `search_text`. relative_dirname : str Filter by relative dirname. Returns ------- result : AffectedItemsWazuhResult Lists content. """ result = AffectedItemsWazuhResult(all_msg='All specified lists were returned', some_msg='Some lists were not returned', none_msg='No list was returned') dirname = join(common.ossec_path, relative_dirname) if relative_dirname else None lists = list() for path in get_filenames_paths(filename): # Only files which exist and whose dirname is the one specified by the user (if any), will be added to response. if not any([dirname is not None and path_dirname(path) != dirname, not isfile(path)]): lists.append({'items': [{'key': key, 'value': value} for key, value in get_list_from_file(path).items()], 'relative_dirname': path_dirname(to_relative_path(path)), 'filename': split(to_relative_path(path))[1]}) data = process_array(lists, search_text=search_text, search_in_fields=search_in_fields, complementary_search=complementary_search, sort_by=sort_by, sort_ascending=sort_ascending, offset=offset, limit=limit, select=select, allowed_sort_fields=SORT_FIELDS, required_fields=REQUIRED_FIELDS) result.affected_items = data['items'] result.total_affected_items = data['totalItems'] return result
def test_get_list_from_file_with_errors(error_to_raise, wazuh_error_code): """Test `get_list_from_file` core functionality when using invalid files or paths as parameter. `get_list_from_file` must raise the proper WazuhError when facing certain scenarios like a Permission Denied error when opening a file. Parameters ---------- error_to_raise : OSError The `OSError` that `get_list_from_file` must catch when trying to open a file. wazuh_error_code : int Error code of the `WazuhError` that must be raised by `get_list_from_file` when the specified `OSError` occurrs. """ with patch("builtins.open", mock_open()) as mock: mock.side_effect = error_to_raise try: get_list_from_file("some_path") pytest.fail("No exception was raised hence failing the test") except WazuhError as e: assert e.code == wazuh_error_code except Exception as e: assert e.args == (1, "Random")
def test_get_list_from_file(): """Test basic `get_list_from_file` core functionality. `get_list_from_file` must retrieve the content of a CDB file. """ assert get_list_from_file(PATH_FILE) == CONTENT_FILE