Skip to content

Commit

Permalink
Add: Readd get_nvts and get_nvt original calls (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself authored Apr 25, 2023
1 parent 3edad27 commit 6e602c4
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 2 deletions.
112 changes: 111 additions & 1 deletion gvm/protocols/gmpv208/entities/secinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
*,
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions tests/protocols/gmpv208/entities/secinfo/test_get_nvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(
'<get_nvts nvt_oid="nvt_oid" details="1" '
'preferences="1" preference_count="1"/>'
)
)

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)
76 changes: 75 additions & 1 deletion tests/protocols/gmpv208/entities/secinfo/test_get_nvts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<get_info type="NVT"/>')
Expand Down Expand Up @@ -56,3 +56,77 @@ def test_get_nvts_with_details(self):
self.connection.send.has_been_called_with(
'<get_info type="NVT" details="0"/>'
)

def test_get_extended_nvts(self):
self.gmp.get_nvts(extended=True)

self.connection.send.has_been_called_with("<get_nvts/>")

def test_get_extended_nvts_with_details(self):
self.gmp.get_nvts(extended=True, details=True)

self.connection.send.has_been_called_with('<get_nvts details="1"/>')

self.gmp.get_nvts(extended=True, details=False)

self.connection.send.has_been_called_with('<get_nvts details="0"/>')

def test_get_extended_nvts_with_preferences(self):
self.gmp.get_nvts(extended=True, preferences=True)

self.connection.send.has_been_called_with('<get_nvts preferences="1"/>')

self.gmp.get_nvts(extended=True, preferences=False)

self.connection.send.has_been_called_with('<get_nvts preferences="0"/>')

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(
'<get_nvts preference_count="1"/>'
)

def test_get_extended_nvts_with_timeout(self):
self.gmp.get_nvts(extended=True, timeout=True)

self.connection.send.has_been_called_with('<get_nvts timeout="1"/>')

self.gmp.get_nvts(extended=True, timeout=False)

self.connection.send.has_been_called_with('<get_nvts timeout="0"/>')

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(
'<get_nvts config_id="config_id"/>'
)

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(
'<get_nvts preferences_config_id="preferences_config_id"/>'
)

def test_get_extended_nvts_with_family(self):
self.gmp.get_nvts(extended=True, family="family")

self.connection.send.has_been_called_with('<get_nvts family="family"/>')

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(
'<get_nvts sort_order="sort_order"/>'
)

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(
'<get_nvts sort_field="sort_field"/>'
)

0 comments on commit 6e602c4

Please sign in to comment.