Skip to content

Commit

Permalink
Add: Allow to query host and os details
Browse files Browse the repository at this point in the history
Add details argument to get_host, get_hosts, get_operating_system and
get_operating_systems to allow querying the asset details.
  • Loading branch information
bjoernricks committed Apr 3, 2023
1 parent 56400a0 commit 3c38b19
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
13 changes: 11 additions & 2 deletions gvm/protocols/gmpv208/entities/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from typing import Any, Optional

from gvm.errors import InvalidArgument, RequiredArgument
from gvm.utils import add_filter
from gvm.utils import add_filter, to_bool
from gvm.xml import XmlCommand


Expand Down Expand Up @@ -101,12 +101,14 @@ def get_hosts(
*,
filter_string: Optional[str] = None,
filter_id: Optional[str] = None,
details: Optional[bool] = None,
) -> Any:
"""Request a list of hosts
Arguments:
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
details: Whether to include additional information (e.g. tags)
Returns:
The response. See :py:meth:`send_command` for details.
Expand All @@ -118,13 +120,17 @@ def get_hosts(

add_filter(cmd, filter_string, filter_id)

if details is not None:
cmd.set_attribute("details", to_bool(details))

return self._send_xml_command(cmd)

def get_host(self, host_id: str) -> Any:
def get_host(self, host_id: str, *, details: Optional[bool] = None) -> Any:
"""Request a single host
Arguments:
host_id: UUID of an existing host
details: Whether to include additional information (e.g. tags)
Returns:
The response. See :py:meth:`send_command` for details.
Expand All @@ -139,6 +145,9 @@ def get_host(self, host_id: str) -> Any:
cmd.set_attribute("asset_id", host_id)
cmd.set_attribute("type", "host")

if details is not None:
cmd.set_attribute("details", to_bool(details))

return self._send_xml_command(cmd)

def modify_host(
Expand Down
15 changes: 13 additions & 2 deletions gvm/protocols/gmpv208/entities/operating_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Any, Optional

from gvm.errors import RequiredArgument
from gvm.utils import add_filter
from gvm.utils import add_filter, to_bool
from gvm.xml import XmlCommand


Expand Down Expand Up @@ -50,12 +50,14 @@ def get_operating_systems(
*,
filter_string: Optional[str] = None,
filter_id: Optional[str] = None,
details: Optional[bool] = None,
) -> Any:
"""Request a list of operating_systems
Arguments:
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
details: Whether to include additional information (e.g. tags)
Returns:
The response. See :py:meth:`send_command` for details.
Expand All @@ -67,13 +69,19 @@ def get_operating_systems(

add_filter(cmd, filter_string, filter_id)

if details is not None:
cmd.set_attribute("details", to_bool(details))

return self._send_xml_command(cmd)

def get_operating_system(self, operating_system_id: str) -> Any:
def get_operating_system(
self, operating_system_id: str, *, details: Optional[bool] = None
) -> Any:
"""Request a single operating_system
Arguments:
operating_system_id: UUID of an existing operating_system
details: Whether to include additional information (e.g. tags)
Returns:
The response. See :py:meth:`send_command` for details.
Expand All @@ -89,6 +97,9 @@ def get_operating_system(self, operating_system_id: str) -> Any:
cmd.set_attribute("asset_id", operating_system_id)
cmd.set_attribute("type", "os")

if details is not None:
cmd.set_attribute("details", to_bool(details))

return self._send_xml_command(cmd)

def modify_operating_system(
Expand Down
13 changes: 13 additions & 0 deletions tests/protocols/gmpv208/entities/hosts/test_get_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def test_get_host(self):
'<get_assets asset_id="a1" type="host"/>'
)

def test_get_host_details(self):
self.gmp.get_host("a1", details=True)

self.connection.send.has_been_called_with(
'<get_assets asset_id="a1" type="host" details="1"/>'
)

self.gmp.get_host("a1", details=False)

self.connection.send.has_been_called_with(
'<get_assets asset_id="a1" type="host" details="0"/>'
)

def test_get_host_missing_host_id(self):
with self.assertRaises(RequiredArgument):
self.gmp.get_host(
Expand Down
13 changes: 13 additions & 0 deletions tests/protocols/gmpv208/entities/hosts/test_get_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def test_get_hosts(self):

self.connection.send.has_been_called_with('<get_assets type="host"/>')

def test_get_hosts_details(self):
self.gmp.get_hosts(details=True)

self.connection.send.has_been_called_with(
'<get_assets type="host" details="1"/>'
)

self.gmp.get_hosts(details=False)

self.connection.send.has_been_called_with(
'<get_assets type="host" details="0"/>'
)

def test_get_hosts_with_filter_string(self):
self.gmp.get_hosts(filter_string="foo=bar")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def test_get_operating_system(self):
'<get_assets asset_id="a1" type="os"/>'
)

def test_get_operating_system_details(self):
self.gmp.get_operating_system("a1", details=True)

self.connection.send.has_been_called_with(
'<get_assets asset_id="a1" type="os" details="1"/>'
)
self.gmp.get_operating_system("a1", details=False)

self.connection.send.has_been_called_with(
'<get_assets asset_id="a1" type="os" details="0"/>'
)

def test_get_asset_missing_operating_system_id(self):
with self.assertRaises(RequiredArgument):
self.gmp.get_operating_system(operating_system_id=None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def test_get_operating_systems(self):

self.connection.send.has_been_called_with('<get_assets type="os"/>')

def test_get_operating_systems_details(self):
self.gmp.get_operating_systems(details=True)

self.connection.send.has_been_called_with(
'<get_assets type="os" details="1"/>'
)

self.gmp.get_operating_systems(details=False)

self.connection.send.has_been_called_with(
'<get_assets type="os" details="0"/>'
)

def test_get_operating_systems_with_filter_string(self):
self.gmp.get_operating_systems(filter_string="foo=bar")

Expand Down

0 comments on commit 3c38b19

Please sign in to comment.