diff --git a/CHANGELOG.md b/CHANGELOG.md index 6750fd2c07e..ac78a9f4755 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Change Log All notable changes to this project will be documented in this file. +## [TBD] + +### Fixed + +- Ignore uppercase and lowercase sorting an array in framework. ([#814](https://github.com/wazuh/wazuh/pull/814)) + + ## [v3.5.0] ### Added diff --git a/framework/wazuh/utils.py b/framework/wazuh/utils.py index a5a74fea884..5855bf4ebb7 100755 --- a/framework/wazuh/utils.py +++ b/framework/wazuh/utils.py @@ -17,6 +17,10 @@ from itertools import groupby, chain from xml.etree.ElementTree import fromstring from operator import itemgetter +import sys +# Python 2/3 compability +if sys.version_info[0] == 3: + unicode = str try: from subprocess import check_output @@ -98,7 +102,7 @@ def cut_array(array, offset, limit): elif limit == 0: raise WazuhException(1406) - if not array or limit == None: + if not array or limit is None: return array offset = int(offset) @@ -125,8 +129,8 @@ def sort_array(array, sort_by=None, order='asc', allowed_sort_fields=None): def check_sort_fields(allowed_sort_fields, sort_by): # Check if every element in sort['fields'] is in allowed_sort_fields if not sort_by.issubset(allowed_sort_fields): - uncorrect_fields = map(lambda x: str(x), sort_by - allowed_sort_fields) - raise WazuhException(1403, 'Allowed sort fields: {0}. Fields: {1}'.format(list(allowed_sort_fields), uncorrect_fields)) + incorrect_fields = ', '.join(sort_by - allowed_sort_fields) + raise WazuhException(1403, 'Allowed sort fields: {0}. Fields: {1}'.format(', '.join(allowed_sort_fields), incorrect_fields)) if not array: return array @@ -145,9 +149,13 @@ def check_sort_fields(allowed_sort_fields, sort_by): if type(array[0]) is dict: check_sort_fields(set(array[0].keys()), set(sort_by)) - return sorted(array, key=lambda o: tuple(o.get(a) for a in sort_by), reverse=order_desc) + return sorted(array, + key=lambda o: tuple(o.get(a).lower() if type(o.get(a)) in (str,unicode) else o.get(a) for a in sort_by), + reverse=order_desc) else: - return sorted(array, key=lambda o: tuple(getattr(o, a) for a in sort_by), reverse=order_desc) + return sorted(array, + key=lambda o: tuple(getattr(o, a).lower() if type(getattr(o, a)) in (str,unicode) else getattr(o, a) for a in sort_by), + reverse=order_desc) else: if type(array) is set or (type(array[0]) is not dict and 'class \'wazuh' not in str(type(array[0]))): return sorted(array, reverse=order_desc) @@ -217,6 +225,7 @@ def search_array(array, text, negation=False, fields=None): return found + _filemode_table = ( ((stat.S_IFLNK, "l"), (stat.S_IFREG, "-"), @@ -367,6 +376,7 @@ def md5(fname): hash_md5.update(chunk) return hash_md5.hexdigest() + def get_fields_to_nest(fields, force_fields=[], split_character="_"): nest = {k:set(filter(lambda x: x != k, chain.from_iterable(g))) for k,g in groupby(map(lambda x: x.split(split_character), sorted(fields)),