From 7e483461f8cf0730df5ed343992b97cfb4d209d1 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 10 Mar 2020 10:41:17 +0100 Subject: [PATCH 1/4] Add details parameter to get_vt_iterator() If details is True, it adds the vt dependencies as subelement with the oid. For this an expensive query to redis is necessary. The query takes about 3 seconds, which is especially expensive when only one vt must be sent. Otherwise, the dependencies are added as subelement of custom, the query will not be perfomed, and quicker response is given for a single vt. --- ospd_openvas/daemon.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/ospd_openvas/daemon.py b/ospd_openvas/daemon.py index 985ca965..e2eb5ce1 100644 --- a/ospd_openvas/daemon.py +++ b/ospd_openvas/daemon.py @@ -467,7 +467,7 @@ def scheduler(self): """This method is called periodically to run tasks.""" self.check_feed() - def get_single_vt(self, vt_id, oids): + def get_single_vt(self, vt_id, oids=None): _vt_params = self.nvti.get_nvt_params(vt_id) _vt_refs = self.nvti.get_nvt_refs(vt_id) _custom = self.nvti.get_nvt_metadata(vt_id) @@ -476,12 +476,15 @@ def get_single_vt(self, vt_id, oids): _vt_creation_time = _custom.pop('creation_date') _vt_modification_time = _custom.pop('last_modification') - _vt_dependencies = list() - if 'dependencies' in _custom: - _deps = _custom.pop('dependencies') - _deps_list = _deps.split(', ') - for dep in _deps_list: - _vt_dependencies.append(oids.get('filename:' + dep)) + if oids: + _vt_dependencies = list() + if 'dependencies' in _custom: + _deps = _custom.pop('dependencies') + _deps_list = _deps.split(', ') + for dep in _deps_list: + _vt_dependencies.append(oids.get('filename:' + dep)) + else: + _vt_dependencies = None _summary = None _impact = None @@ -571,15 +574,15 @@ def get_single_vt(self, vt_id, oids): return vt def get_vt_iterator( - self, vt_selection: List[str] = None + self, vt_selection: List[str] = None, vt_details: bool = True ) -> Iterator[Tuple[str, Dict]]: """ Yield the vts from the Redis NVTicache. """ - oids = dict(self.nvti.get_oids()) - if vt_selection: - vt_id_list = vt_selection - else: - vt_id_list = oids.values() - for vt_id in vt_id_list: + + oids = None + if vt_details: + oids = dict(self.nvti.get_oids()) + + for vt_id in vt_selection: vt = self.get_single_vt(vt_id, oids) yield (vt_id, vt) From 50d0a2ab14c8a26632b488b666f5a1af0d5eaa67 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 10 Mar 2020 11:26:14 +0100 Subject: [PATCH 2/4] Update Pipfile.lock --- Pipfile.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Pipfile.lock b/Pipfile.lock index be4ba6a7..d8ef6d50 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -145,7 +145,7 @@ "ospd": { "editable": true, "git": "https://github.com/greenbone/ospd.git", - "ref": "9304492be3ea78cba367fdd80ca13eb9d69473c5" + "ref": "6846bcc4b165801409978752d7bbf560b55003a4" }, "packaging": { "hashes": [ @@ -304,10 +304,10 @@ }, "click": { "hashes": [ - "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", - "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7" + "sha256:8a18b4ea89d8820c5d0c7da8a64b2c324b4dabb695804dbfea19b9be9d88c0cc", + "sha256:e345d143d80bf5ee7534056164e5e112ea5e22716bbb1ce727941f4c8b471b9a" ], - "version": "==7.0" + "version": "==7.1.1" }, "isort": { "hashes": [ From b913cb06d244b9b7fa51134110858c340b4964be Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 10 Mar 2020 11:29:23 +0100 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97704d76..2060da10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). [#197](https://github.com/greenbone/ospd-openvas/pull/197) - Add support for test_alive_hosts_only feature of openvas. [#204](https://github.com/greenbone/ospd-openvas/pull/204) - Use lock file during feed update to avoid corrupted cache. [#207](https://github.com/greenbone/ospd-openvas/pull/207) +- Add details parameter to get_vt_iterator(). [#215](https://github.com/greenbone/ospd-openvas/pull/215) ### Changed - Less strict checks for the nvti cache version From 5ceee6fecee4f5c559b40fa88331b2dd2b2bd477 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 10 Mar 2020 11:34:01 +0100 Subject: [PATCH 4/4] Fix param name and makes pylint happy --- ospd_openvas/daemon.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ospd_openvas/daemon.py b/ospd_openvas/daemon.py index e2eb5ce1..70532c03 100644 --- a/ospd_openvas/daemon.py +++ b/ospd_openvas/daemon.py @@ -574,12 +574,12 @@ def get_single_vt(self, vt_id, oids=None): return vt def get_vt_iterator( - self, vt_selection: List[str] = None, vt_details: bool = True + self, vt_selection: List[str] = None, details: bool = True ) -> Iterator[Tuple[str, Dict]]: """ Yield the vts from the Redis NVTicache. """ oids = None - if vt_details: + if details: oids = dict(self.nvti.get_oids()) for vt_id in vt_selection: