Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Improve get_filtered_vts_list().
Browse files Browse the repository at this point in the history
Don't do a copy. Instead use a generator.
This reduce considerably the amount of memory used during the filtered vts list.
  • Loading branch information
jjnicola committed Feb 10, 2020
1 parent 52da0ec commit b89c503
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions ospd/vtfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def format_filter_value(self, element: str, value: Dict):
format_func = self.allowed_filter.get(element)
return format_func(value)

def get_filtered_vts_list(self, vts: Dict, vt_filter: str) -> Optional[Dict]:
def get_filtered_vts_list(
self, vts: Dict, vt_filter: str
) -> Optional[Dict]:
""" Gets a collection of vulnerability test from the vts dictionary,
which match the filter.
Expand All @@ -107,7 +109,8 @@ def get_filtered_vts_list(self, vts: Dict, vt_filter: str) -> Optional[Dict]:
vts (dictionary): The complete vts collection.
Returns:
Dictionary with filtered vulnerability tests.
List with filtered vulnerability tests. The list can be empty.
None in case of filter parse failure.
"""
if not vt_filter:
raise OspdCommandError('vt_filter: A valid filter is required.')
Expand All @@ -116,17 +119,21 @@ def get_filtered_vts_list(self, vts: Dict, vt_filter: str) -> Optional[Dict]:
if not filters:
return None

_vts_aux = vts.copy()
vt_oid_list = list(vts.keys())

for _element, _oper, _filter_val in filters:
for vt_id in _vts_aux.copy():
if not _vts_aux[vt_id].get(_element):
_vts_aux.pop(vt_id)
vts_generator = (vt for vt in vts)
for vt_oid in vts_generator:
if vt_oid not in vt_oid_list:
continue
if not vts[vt_oid].get(_element):
vt_oid_list.remove(vt_oid)
continue
_elem_val = _vts_aux[vt_id].get(_element)
_elem_val = vts[vt_oid].get(_element)
_val = self.format_filter_value(_element, _elem_val)
if self.filter_operator[_oper](_val, _filter_val):
continue
else:
_vts_aux.pop(vt_id)
vt_oid_list.remove(vt_oid)

return _vts_aux
return vt_oid_list

0 comments on commit b89c503

Please sign in to comment.