From 6e602c46aa0a4f515e709a7eaf0a8b909bb750a9 Mon Sep 17 00:00:00 2001 From: Jaspar S Date: Tue, 25 Apr 2023 15:15:57 +0200 Subject: [PATCH] Add: Readd get_nvts and get_nvt original calls (#1008) --- gvm/protocols/gmpv208/entities/secinfo.py | 112 +++++++++++++++++- .../gmpv208/entities/secinfo/test_get_nvt.py | 20 ++++ .../gmpv208/entities/secinfo/test_get_nvts.py | 76 +++++++++++- 3 files changed, 206 insertions(+), 2 deletions(-) diff --git a/gvm/protocols/gmpv208/entities/secinfo.py b/gvm/protocols/gmpv208/entities/secinfo.py index 65b57d544..d9cb99670 100644 --- a/gvm/protocols/gmpv208/entities/secinfo.py +++ b/gvm/protocols/gmpv208/entities/secinfo.py @@ -218,6 +218,15 @@ def get_nvts( filter_id: Optional[str] = None, name: Optional[str] = None, details: Optional[bool] = None, + extended: Optional[bool] = None, + preferences: Optional[bool] = None, + preference_count: Optional[bool] = None, + timeout: Optional[bool] = None, + config_id: Optional[str] = None, + preferences_config_id: Optional[str] = None, + family: Optional[str] = None, + sort_order: Optional[str] = None, + sort_field: Optional[str] = None, ) -> Any: """Request a list of NVTs @@ -227,11 +236,35 @@ def get_nvts( name: Name or identifier of the requested information details: Whether to include information about references to this information + extended: Whether to receive extended NVT information + (calls get_nvts, instead of get_info) + preferences: Whether to include NVT preferences + preference_count: Whether to include preference count + timeout: Whether to include the special timeout preference + config_id: UUID of scan config to which to limit the NVT listing + preferences_config_id: UUID of scan config to use for preference + values + family: Family to which to limit NVT listing + sort_order: Sort order + sort_field: Sort field Returns: The response. See :py:meth:`send_command` for details. """ + if extended: + return self._get_nvt_details( + details=details, + preferences=preferences, + preference_count=preference_count, + timeout=timeout, + config_id=config_id, + preferences_config_id=preferences_config_id, + family=family, + sort_order=sort_order, + sort_field=sort_field, + ) + return self.get_info_list( info_type=InfoType.NVT, filter_string=filter_string, @@ -240,6 +273,65 @@ def get_nvts( details=details, ) + def _get_nvt_details( + self, + *, + details: Optional[bool] = None, + preferences: Optional[bool] = None, + preference_count: Optional[bool] = None, + timeout: Optional[bool] = None, + config_id: Optional[str] = None, + preferences_config_id: Optional[str] = None, + family: Optional[str] = None, + sort_order: Optional[str] = None, + sort_field: Optional[str] = None, + ): + """Request a list of NVTs with extended details + Arguments: + details: Whether to include full details + preferences: Whether to include nvt preferences + preference_count: Whether to include preference count + timeout: Whether to include the special timeout preference + config_id: UUID of scan config to which to limit the NVT listing + preferences_config_id: UUID of scan config to use for preference + values + family: Family to which to limit NVT listing + sort_order: Sort order + sort_field: Sort field + Returns: + The response. See :py:meth:`send_command` for details. + """ + cmd = XmlCommand("get_nvts") + + if details is not None: + cmd.set_attribute("details", to_bool(details)) + + if preferences is not None: + cmd.set_attribute("preferences", to_bool(preferences)) + + if preference_count is not None: + cmd.set_attribute("preference_count", to_bool(preference_count)) + + if timeout is not None: + cmd.set_attribute("timeout", to_bool(timeout)) + + if config_id: + cmd.set_attribute("config_id", config_id) + + if preferences_config_id: + cmd.set_attribute("preferences_config_id", preferences_config_id) + + if family: + cmd.set_attribute("family", family) + + if sort_order: + cmd.set_attribute("sort_order", sort_order) + + if sort_field: + cmd.set_attribute("sort_field", sort_field) + + return self._send_xml_command(cmd) + def get_dfn_cert_advisories( self, *, @@ -400,16 +492,34 @@ def get_cpe(self, cpe_id: str) -> Any: return self.get_info(cpe_id, InfoType.CPE) - def get_nvt(self, nvt_id: str) -> Any: + def get_nvt(self, nvt_id: str, *, extended: Optional[bool] = None) -> Any: """Request a single NVT Arguments: nvt_id: ID of an existing NVT + extended: Whether to receive extended NVT information + (calls get_nvts, instead of get_info) Returns: The response. See :py:meth:`send_command` for details. """ + if extended: + cmd = XmlCommand("get_nvts") + + if not nvt_id: + raise RequiredArgument( + function=self.get_nvt.__name__, argument="nvt_oid" + ) + + cmd.set_attribute("nvt_oid", nvt_id) + + # for single entity always request all details + cmd.set_attribute("details", "1") + cmd.set_attribute("preferences", "1") + cmd.set_attribute("preference_count", "1") + return self._send_xml_command(cmd) + return self.get_info(nvt_id, InfoType.NVT) def get_dfn_cert_advisory(self, cert_id: str) -> Any: diff --git a/tests/protocols/gmpv208/entities/secinfo/test_get_nvt.py b/tests/protocols/gmpv208/entities/secinfo/test_get_nvt.py index d63a84224..8b95f927b 100644 --- a/tests/protocols/gmpv208/entities/secinfo/test_get_nvt.py +++ b/tests/protocols/gmpv208/entities/secinfo/test_get_nvt.py @@ -42,3 +42,23 @@ def test_get_nvt_missing_nvt_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_nvt(nvt_id=None) + + def test_get_extended_nvt_with_nvt_oid(self): + self.gmp.get_nvt(extended=True, nvt_id="nvt_oid") + + self.connection.send.has_been_called_with( + ( + '' + ) + ) + + def test_get_extended_nvt_missing_nvt_oid(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_nvt(extended=True, nvt_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_nvt(extended=True, nvt_id="") + + with self.assertRaises(RequiredArgument): + self.gmp.get_nvt("", extended=True) diff --git a/tests/protocols/gmpv208/entities/secinfo/test_get_nvts.py b/tests/protocols/gmpv208/entities/secinfo/test_get_nvts.py index cd6079424..c4ee3dcdf 100644 --- a/tests/protocols/gmpv208/entities/secinfo/test_get_nvts.py +++ b/tests/protocols/gmpv208/entities/secinfo/test_get_nvts.py @@ -18,7 +18,7 @@ class GmpGetNvtListTestMixin: - def test_get_cpes(self): + def test_get_nvts(self): self.gmp.get_nvts() self.connection.send.has_been_called_with('') @@ -56,3 +56,77 @@ def test_get_nvts_with_details(self): self.connection.send.has_been_called_with( '' ) + + def test_get_extended_nvts(self): + self.gmp.get_nvts(extended=True) + + self.connection.send.has_been_called_with("") + + def test_get_extended_nvts_with_details(self): + self.gmp.get_nvts(extended=True, details=True) + + self.connection.send.has_been_called_with('') + + self.gmp.get_nvts(extended=True, details=False) + + self.connection.send.has_been_called_with('') + + def test_get_extended_nvts_with_preferences(self): + self.gmp.get_nvts(extended=True, preferences=True) + + self.connection.send.has_been_called_with('') + + self.gmp.get_nvts(extended=True, preferences=False) + + self.connection.send.has_been_called_with('') + + def test_get_extended_nvts_with_preference_count(self): + self.gmp.get_nvts(extended=True, preference_count=True) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_extended_nvts_with_timeout(self): + self.gmp.get_nvts(extended=True, timeout=True) + + self.connection.send.has_been_called_with('') + + self.gmp.get_nvts(extended=True, timeout=False) + + self.connection.send.has_been_called_with('') + + def test_get_extended_nvts_with_config_id(self): + self.gmp.get_nvts(extended=True, config_id="config_id") + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_extended_nvts_with_preferences_config_id(self): + self.gmp.get_nvts( + extended=True, preferences_config_id="preferences_config_id" + ) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_extended_nvts_with_family(self): + self.gmp.get_nvts(extended=True, family="family") + + self.connection.send.has_been_called_with('') + + def test_get_extended_nvts_with_sort_order(self): + self.gmp.get_nvts(extended=True, sort_order="sort_order") + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_extended_nvts_with_sort_field(self): + self.gmp.get_nvts(extended=True, sort_field="sort_field") + + self.connection.send.has_been_called_with( + '' + )