Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/api/gmpnext.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,10 @@
:undoc-members:
:no-index:
```

```{eval-rst}
.. autoclass:: gvm.protocols.gmp.requests.next.TargetSource
:members:
:undoc-members:
:no-index:
```
7 changes: 7 additions & 0 deletions gvm/protocols/gmp/_gmpnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ReportVulnerabilities,
ScanReports,
Targets,
TargetSource,
Tasks,
Timezones,
WebApplicationTargets,
Expand Down Expand Up @@ -1720,6 +1721,7 @@ def create_target(
*,
asset_hosts_filter: str | None = None,
hosts: list[str] | None = None,
source: TargetSource | str | None = None,
comment: str | None = None,
exclude_hosts: list[str] | None = None,
ssh_credential_id: EntityID | None = None,
Expand All @@ -1740,6 +1742,7 @@ def create_target(
name: Name of the target
asset_hosts_filter: Filter to select target host from assets hosts
hosts: List of hosts addresses to scan
source: Target source. Omit to default to manual.
exclude_hosts: List of hosts addresses to exclude from scan
comment: Comment for the target
ssh_credential_id: UUID of a ssh credential to use on target
Expand All @@ -1761,6 +1764,7 @@ def create_target(
name,
asset_hosts_filter=asset_hosts_filter,
hosts=hosts,
source=source,
comment=comment,
exclude_hosts=exclude_hosts,
ssh_credential_id=ssh_credential_id,
Expand All @@ -1784,6 +1788,7 @@ def modify_target(
name: str | None = None,
comment: str | None = None,
hosts: list[str] | None = None,
source: TargetSource | str | None = None,
exclude_hosts: list[str] | None = None,
ssh_credential_id: EntityID | None = None,
ssh_credential_port: str | int | None = None,
Expand All @@ -1803,6 +1808,7 @@ def modify_target(
comment: Comment on target.
name: Name of target.
hosts: List of target hosts.
source: Target source. Omit to keep the current source.
exclude_hosts: A list of hosts to exclude.
ssh_credential_id: UUID of SSH credential to use on target.
ssh_credential_port: The port to use for ssh credential
Expand All @@ -1823,6 +1829,7 @@ def modify_target(
name=name,
comment=comment,
hosts=hosts,
source=source,
exclude_hosts=exclude_hosts,
ssh_credential_id=ssh_credential_id,
ssh_credential_port=ssh_credential_port,
Expand Down
7 changes: 6 additions & 1 deletion gvm/protocols/gmp/requests/next/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@
ReportVulnerabilities,
)
from gvm.protocols.gmp.requests.next._scan_report import ScanReports
from gvm.protocols.gmp.requests.next._targets import AliveTest, Targets
from gvm.protocols.gmp.requests.next._targets import (
AliveTest,
Targets,
TargetSource,
)
from gvm.protocols.gmp.requests.next._tasks import Tasks
from gvm.protocols.gmp.requests.next._timezones import (
Timezones,
Expand Down Expand Up @@ -220,6 +224,7 @@
"SystemReports",
"TLSCertificates",
"Tags",
"TargetSource",
"Targets",
"Tasks",
"TicketStatus",
Expand Down
41 changes: 41 additions & 0 deletions gvm/protocols/gmp/requests/next/_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def from_string(
) from None


class TargetSource(Enum):
"""Enum for choosing a target source"""

MANUAL = "manual"
ACTIVE_DIRECTORY = "active_directory"


class Targets:
@classmethod
def create_target(
Expand All @@ -66,6 +73,7 @@ def create_target(
*,
asset_hosts_filter: str | None = None,
hosts: list[str] | None = None,
source: TargetSource | str | None = None,
comment: str | None = None,
exclude_hosts: list[str] | None = None,
ssh_credential_id: EntityID | None = None,
Expand All @@ -86,6 +94,7 @@ def create_target(
name: Name of the target
asset_hosts_filter: Filter to select target host from assets hosts
hosts: List of hosts addresses to scan
source: Target source. Omit to default to manual.
exclude_hosts: List of hosts addresses to exclude from scan
comment: Comment for the target
ssh_credential_id: UUID of a ssh credential to use on target
Expand Down Expand Up @@ -122,6 +131,21 @@ def create_target(
argument="hosts or asset_hosts_filter",
)

if source is not None:
if not source:
raise InvalidArgument(
function=cls.create_target.__name__, argument="source"
)
try:
source = TargetSource(source)
except InvalidArgument as error:
raise InvalidArgument(
str(error),
function=cls.create_target.__name__,
argument="source",
) from None
cmd.add_element("source", source.value)

if comment:
cmd.add_element("comment", comment)

Expand Down Expand Up @@ -184,6 +208,7 @@ def modify_target(
name: str | None = None,
comment: str | None = None,
hosts: list[str] | None = None,
source: TargetSource | str | None = None,
exclude_hosts: list[str] | None = None,
ssh_credential_id: EntityID | None = None,
ssh_credential_port: str | int | None = None,
Expand All @@ -203,6 +228,7 @@ def modify_target(
comment: Comment on target.
name: Name of target.
hosts: List of target hosts.
source: Target source. Omit to keep the current source.
exclude_hosts: A list of hosts to exclude.
ssh_credential_id: UUID of SSH credential to use on target.
ssh_credential_port: The port to use for ssh credential
Expand Down Expand Up @@ -236,6 +262,21 @@ def modify_target(
if exclude_hosts is None:
exclude_hosts = [""]

if source is not None:
if not source:
raise InvalidArgument(
function=cls.modify_target.__name__, argument="source"
)
try:
source = TargetSource(source)
except InvalidArgument as error:
raise InvalidArgument(
str(error),
function=cls.modify_target.__name__,
argument="source",
) from None
cmd.add_element("source", source.value)

if exclude_hosts:
cmd.add_element("exclude_hosts", to_comma_list(exclude_hosts))

Expand Down
80 changes: 79 additions & 1 deletion tests/protocols/gmpnext/entities/targets/test_create_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

from gvm.errors import InvalidArgument, RequiredArgument
from gvm.protocols.gmp.requests.next import AliveTest
from gvm.protocols.gmp.requests.next import AliveTest, TargetSource


class GmpCreateTargetTestMixin:
Expand Down Expand Up @@ -253,3 +253,81 @@ def test_create_target_with_host_discovery_ipv6_alive_test(self):
b"<alive_tests>Host Discovery IPv6</alive_tests>"
b"</create_target>"
)

def test_create_target_with_manual_source_as_string(self):
self.gmp.create_target("foo", hosts=["foo"], source="manual")

self.connection.send.has_been_called_with(
b"<create_target>"
b"<name>foo</name>"
b"<hosts>foo</hosts>"
b"<source>manual</source>"
b"</create_target>"
)

def test_create_target_with_active_directory_source_as_string(self):
self.gmp.create_target("foo", hosts=["foo"], source="active_directory")

self.connection.send.has_been_called_with(
b"<create_target>"
b"<name>foo</name>"
b"<hosts>foo</hosts>"
b"<source>active_directory</source>"
b"</create_target>"
)

def test_create_target_with_manual_source(self):
self.gmp.create_target("foo", hosts=["foo"], source=TargetSource.MANUAL)

self.connection.send.has_been_called_with(
b"<create_target>"
b"<name>foo</name>"
b"<hosts>foo</hosts>"
b"<source>manual</source>"
b"</create_target>"
)

def test_create_target_with_active_directory_source(self):
self.gmp.create_target(
"foo", hosts=["foo"], source=TargetSource.ACTIVE_DIRECTORY
)

self.connection.send.has_been_called_with(
b"<create_target>"
b"<name>foo</name>"
b"<hosts>foo</hosts>"
b"<source>active_directory</source>"
b"</create_target>"
)

def test_create_target_with_source_none(self):
self.gmp.create_target("foo", hosts=["foo"], source=None)

self.connection.send.has_been_called_with(
b"<create_target><name>foo</name><hosts>foo</hosts></create_target>"
)

def test_create_target_with_invalid_source(self):
with self.assertRaises(InvalidArgument) as context:
self.gmp.create_target("foo", hosts=["foo"], source="invalid")

self.assertEqual(context.exception.argument, "source")
self.assertEqual(context.exception.function, "create_target")

def test_create_target_with_empty_source(self):
with self.assertRaises(InvalidArgument) as context:
self.gmp.create_target("foo", hosts=["foo"], source="")

self.assertEqual(context.exception.argument, "source")
self.assertEqual(context.exception.function, "create_target")

def test_create_target_with_mixed_case_source(self):
self.gmp.create_target("foo", hosts=["foo"], source="Active Directory")

self.connection.send.has_been_called_with(
b"<create_target>"
b"<name>foo</name>"
b"<hosts>foo</hosts>"
b"<source>active_directory</source>"
b"</create_target>"
)
89 changes: 88 additions & 1 deletion tests/protocols/gmpnext/entities/targets/test_modify_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

from gvm.errors import InvalidArgument, RequiredArgument
from gvm.protocols.gmp.requests.next import AliveTest
from gvm.protocols.gmp.requests.next import AliveTest, TargetSource


class GmpModifyTargetTestMixin:
Expand Down Expand Up @@ -231,3 +231,90 @@ def test_modify_target_with_host_discovery_ipv6_alive_test_as_string(self):
b"<alive_tests>Host Discovery IPv6</alive_tests>"
b"</modify_target>"
)

def test_modify_target_with_manual_source_as_string(self):
self.gmp.modify_target(target_id="t1", source="manual")

self.connection.send.has_been_called_with(
b'<modify_target target_id="t1">'
b"<source>manual</source>"
b"</modify_target>"
)

def test_modify_target_with_active_directory_source_as_string(self):
self.gmp.modify_target(target_id="t1", source="active_directory")

self.connection.send.has_been_called_with(
b'<modify_target target_id="t1">'
b"<source>active_directory</source>"
b"</modify_target>"
)

def test_modify_target_with_manual_source(self):
self.gmp.modify_target(target_id="t1", source=TargetSource.MANUAL)

self.connection.send.has_been_called_with(
b'<modify_target target_id="t1">'
b"<source>manual</source>"
b"</modify_target>"
)

def test_modify_target_with_active_directory_source(self):
self.gmp.modify_target(
target_id="t1", source=TargetSource.ACTIVE_DIRECTORY
)

self.connection.send.has_been_called_with(
b'<modify_target target_id="t1">'
b"<source>active_directory</source>"
b"</modify_target>"
)

def test_modify_target_with_source_none(self):
self.gmp.modify_target(target_id="t1", source=None)

self.connection.send.has_been_called_with(
b'<modify_target target_id="t1"/>'
)

def test_modify_target_with_invalid_source(self):
with self.assertRaises(InvalidArgument) as context:
self.gmp.modify_target(target_id="t1", source="invalid")

self.assertEqual(context.exception.argument, "source")
self.assertEqual(context.exception.function, "modify_target")

def test_modify_target_with_empty_source(self):
with self.assertRaises(InvalidArgument) as context:
self.gmp.modify_target(target_id="t1", source="")

self.assertEqual(context.exception.argument, "source")
self.assertEqual(context.exception.function, "modify_target")

def test_modify_target_with_mixed_case_source(self):
self.gmp.modify_target(target_id="t1", source="Active Directory")

self.connection.send.has_been_called_with(
b'<modify_target target_id="t1">'
b"<source>active_directory</source>"
b"</modify_target>"
)

def test_modify_target_with_source_and_hosts(self):
self.gmp.modify_target(
target_id="t1",
name="foo",
comment="bar",
hosts=["foo"],
source=TargetSource.ACTIVE_DIRECTORY,
)

self.connection.send.has_been_called_with(
b'<modify_target target_id="t1">'
b"<comment>bar</comment>"
b"<name>foo</name>"
b"<hosts>foo</hosts>"
b"<source>active_directory</source>"
b"<exclude_hosts></exclude_hosts>"
b"</modify_target>"
)