From a1cd64f7fbfdaee747698768c0815179c7523521 Mon Sep 17 00:00:00 2001 From: Ahmed Abdelsalam Date: Thu, 19 Nov 2026 14:00:53 +0100 Subject: [PATCH] Add: Add target source support. --- docs/api/gmpnext.md | 7 ++ gvm/protocols/gmp/_gmpnext.py | 7 ++ gvm/protocols/gmp/requests/next/__init__.py | 7 +- gvm/protocols/gmp/requests/next/_targets.py | 41 +++++++++ .../entities/targets/test_create_target.py | 80 ++++++++++++++++- .../entities/targets/test_modify_target.py | 89 ++++++++++++++++++- 6 files changed, 228 insertions(+), 3 deletions(-) diff --git a/docs/api/gmpnext.md b/docs/api/gmpnext.md index d8c0e7925..8f48372d6 100644 --- a/docs/api/gmpnext.md +++ b/docs/api/gmpnext.md @@ -176,3 +176,10 @@ :undoc-members: :no-index: ``` + +```{eval-rst} +.. autoclass:: gvm.protocols.gmp.requests.next.TargetSource + :members: + :undoc-members: + :no-index: +``` diff --git a/gvm/protocols/gmp/_gmpnext.py b/gvm/protocols/gmp/_gmpnext.py index d7cc70ee4..ebc994aca 100644 --- a/gvm/protocols/gmp/_gmpnext.py +++ b/gvm/protocols/gmp/_gmpnext.py @@ -39,6 +39,7 @@ ReportVulnerabilities, ScanReports, Targets, + TargetSource, Tasks, Timezones, WebApplicationTargets, @@ -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, @@ -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 @@ -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, @@ -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, @@ -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 @@ -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, diff --git a/gvm/protocols/gmp/requests/next/__init__.py b/gvm/protocols/gmp/requests/next/__init__.py index 611bb9a2e..1c427c6d6 100644 --- a/gvm/protocols/gmp/requests/next/__init__.py +++ b/gvm/protocols/gmp/requests/next/__init__.py @@ -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, @@ -220,6 +224,7 @@ "SystemReports", "TLSCertificates", "Tags", + "TargetSource", "Targets", "Tasks", "TicketStatus", diff --git a/gvm/protocols/gmp/requests/next/_targets.py b/gvm/protocols/gmp/requests/next/_targets.py index 16f843f21..f236c6958 100644 --- a/gvm/protocols/gmp/requests/next/_targets.py +++ b/gvm/protocols/gmp/requests/next/_targets.py @@ -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( @@ -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, @@ -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 @@ -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) @@ -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, @@ -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 @@ -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)) diff --git a/tests/protocols/gmpnext/entities/targets/test_create_target.py b/tests/protocols/gmpnext/entities/targets/test_create_target.py index 3988a0be8..1f4c3b11f 100644 --- a/tests/protocols/gmpnext/entities/targets/test_create_target.py +++ b/tests/protocols/gmpnext/entities/targets/test_create_target.py @@ -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: @@ -253,3 +253,81 @@ def test_create_target_with_host_discovery_ipv6_alive_test(self): b"Host Discovery IPv6" b"" ) + + 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"" + b"foo" + b"foo" + b"manual" + b"" + ) + + 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"" + b"foo" + b"foo" + b"active_directory" + b"" + ) + + 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"" + b"foo" + b"foo" + b"manual" + b"" + ) + + 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"" + b"foo" + b"foo" + b"active_directory" + b"" + ) + + 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"foofoo" + ) + + 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"" + b"foo" + b"foo" + b"active_directory" + b"" + ) diff --git a/tests/protocols/gmpnext/entities/targets/test_modify_target.py b/tests/protocols/gmpnext/entities/targets/test_modify_target.py index aae550fe3..c218b076f 100644 --- a/tests/protocols/gmpnext/entities/targets/test_modify_target.py +++ b/tests/protocols/gmpnext/entities/targets/test_modify_target.py @@ -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: @@ -231,3 +231,90 @@ def test_modify_target_with_host_discovery_ipv6_alive_test_as_string(self): b"Host Discovery IPv6" b"" ) + + 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'' + b"manual" + b"" + ) + + 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'' + b"active_directory" + b"" + ) + + 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'' + b"manual" + b"" + ) + + 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'' + b"active_directory" + b"" + ) + + 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'' + ) + + 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'' + b"active_directory" + b"" + ) + + 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'' + b"bar" + b"foo" + b"foo" + b"active_directory" + b"" + b"" + )