Skip to content
Open
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
46 changes: 46 additions & 0 deletions gvm/protocols/gmp/_gmpnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,49 @@ def create_container_task(
Tasks.create_container_task(name=name, comment=comment)
)

def create_web_application_task(
self,
name: str,
web_application_target_id: EntityID,
scanner_id: EntityID,
*,
comment: str | None = None,
alterable: bool | None = None,
schedule_id: EntityID | None = None,
alert_ids: Sequence[EntityID] | None = None,
schedule_periods: int | None = None,
observers: Sequence[str] | None = None,
preferences: Mapping[str, SupportsStr] | None = None,
) -> T:
"""Create a new scan task using an OCI image target.

Args:
name: Name of the new task.
web_application_target_id: UUID of the web application target to be scanned.
scanner_id: UUID of scanner to use for scanning the agents.
comment: Optional comment for the task.
alterable: Whether the task should be alterable.
alert_ids: List of UUIDs for alerts to be applied to the task.
schedule_id: UUID of a schedule when the task should be run.
schedule_periods: Limit to number of scheduled runs, 0 for unlimited.
observers: List of usernames or IDs allowed to observe the task.
preferences: Scanner preferences as name/value pairs.
"""
return self._send_request_and_transform_response(
Tasks.create_web_application_task(
name=name,
web_application_target_id=web_application_target_id,
scanner_id=scanner_id,
comment=comment,
alterable=alterable,
schedule_id=schedule_id,
alert_ids=alert_ids,
schedule_periods=schedule_periods,
observers=observers,
preferences=preferences,
)
)

def create_task(
self,
name: str,
Expand Down Expand Up @@ -826,6 +869,7 @@ def modify_task(
scanner_id: EntityID | None = None,
agent_group_id: EntityID | None = None,
oci_image_target_id: EntityID | None = None,
web_application_target_id: EntityID | None = None,
alterable: bool | None = None,
hosts_ordering: HostsOrdering | None = None,
schedule_id: EntityID | None = None,
Expand All @@ -845,6 +889,7 @@ def modify_task(
scanner_id: UUID of scanner to use for scanning the target
agent_group_id: UUID of agent group to use for scanning
oci_image_target_id: UUID of the OCI Image target to be scanned.
web_application_target_id: UUID of the web application target to be scanned.
comment: The comment on the task.
alert_ids: List of UUIDs for alerts to be applied to the task
hosts_ordering: The order hosts are scanned in
Expand All @@ -864,6 +909,7 @@ def modify_task(
scanner_id=scanner_id,
agent_group_id=agent_group_id,
oci_image_target_id=oci_image_target_id,
web_application_target_id=web_application_target_id,
alterable=alterable,
hosts_ordering=hosts_ordering,
schedule_id=schedule_id,
Expand Down
122 changes: 122 additions & 0 deletions gvm/protocols/gmp/requests/next/_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,97 @@ def create_container_task(
"""
return cls.create_import_task(name=name, comment=comment)

@classmethod
def create_web_application_task(
cls,
name: str,
web_application_target_id: EntityID,
scanner_id: EntityID,
*,
comment: str | None = None,
alterable: bool | None = None,
schedule_id: EntityID | None = None,
alert_ids: Sequence[EntityID] | None = None,
schedule_periods: int | None = None,
observers: Sequence[str] | None = None,
preferences: Mapping[str, SupportsStr] | None = None,
) -> Request:
"""Create a new scan task using a web application target.

Args:
name: Name of the new task.
web_application_target_id: UUID of the web application target to be scanned.
scanner_id: UUID of scanner to use for scanning the web application.
comment: Optional comment for the task.
alterable: Whether the task should be alterable.
alert_ids: List of UUIDs for alerts to be applied to the task.
schedule_id: UUID of a schedule when the task should be run.
schedule_periods: Limit to number of scheduled runs, 0 for unlimited.
observers: List of usernames or IDs allowed to observe the task.
preferences: Scanner preferences as name/value pairs.
"""
if not name:
raise RequiredArgument(
function=cls.create_web_application_task.__name__,
argument="name",
)

if not web_application_target_id:
raise RequiredArgument(
function=cls.create_web_application_task.__name__,
argument="web_application_target_id",
)

if not scanner_id:
raise RequiredArgument(
function=cls.create_web_application_task.__name__,
argument="scanner_id",
)

cmd = XmlCommand("create_task")
cmd.add_element("name", name)
cmd.add_element("usage_type", "scan")
cmd.add_element(
"web_application_target",
attrs={"id": str(web_application_target_id)},
)
cmd.add_element("scanner", attrs={"id": str(scanner_id)})

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

if alterable is not None:
cmd.add_element("alterable", to_bool(alterable))

if alert_ids:
for alert in alert_ids:
cmd.add_element("alert", attrs={"id": str(alert)})

if schedule_id:
cmd.add_element("schedule", attrs={"id": str(schedule_id)})

if schedule_periods is not None:
if (
not isinstance(schedule_periods, Integral)
or schedule_periods < 0
):
raise InvalidArgument(
"schedule_periods must be an integer greater or equal than 0"
)
cmd.add_element("schedule_periods", str(schedule_periods))

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

if preferences is not None:
xml_prefs = cmd.add_element("preferences")
for pref_name, pref_value in preferences.items():
xml_pref = xml_prefs.add_element("preference")
xml_pref.add_element("scanner_name", pref_name)
xml_pref.add_element("value", str(pref_value))

return cmd

@classmethod
def create_task(
cls,
Expand Down Expand Up @@ -453,6 +544,7 @@ def modify_task(
scanner_id: EntityID | None = None,
agent_group_id: EntityID | None = None,
oci_image_target_id: EntityID | None = None,
web_application_target_id: EntityID | None = None,
alterable: bool | None = None,
hosts_ordering: HostsOrdering | None = None,
schedule_id: EntityID | None = None,
Expand All @@ -472,6 +564,7 @@ def modify_task(
scanner_id: UUID of scanner to use for scanning the target
agent_group_id: UUID of agent group to use for scanning
oci_image_target_id: UUID of the OCI Image target to be scanned.
web_application_target_id: UUID of the web application target to be scanned.
comment: The comment on the task.
alert_ids: List of UUIDs for alerts to be applied to the task
hosts_ordering: The order hosts are scanned in
Expand Down Expand Up @@ -507,6 +600,30 @@ def modify_task(
cmd = XmlCommand("modify_task")
cmd.set_attribute("task_id", str(task_id))

if (
sum(
entity_id is not None
for entity_id in (
target_id,
agent_group_id,
oci_image_target_id,
web_application_target_id,
)
)
> 1
):
raise InvalidArgument(
function=cls.modify_task.__name__,
argument=(
"target_id/agent_group_id/oci_image_target_id/"
"web_application_target_id"
),
message=(
"Only one of target_id, agent_group_id, oci_image_target_id "
"or web_application_target_id can be modified at a time"
),
)

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

Expand All @@ -526,6 +643,11 @@ def modify_task(
cmd.add_element(
"oci_image_target", attrs={"id": str(oci_image_target_id)}
)
if web_application_target_id:
cmd.add_element(
"web_application_target",
attrs={"id": str(web_application_target_id)},
)

if alterable is not None:
cmd.add_element("alterable", to_bool(alterable))
Expand Down
4 changes: 4 additions & 0 deletions tests/protocols/gmpnext/entities/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from .test_create_container_task import GmpCreateContainerTaskTestMixin
from .test_create_import_task import GmpCreateImportTaskTestMixin
from .test_create_task import GmpCreateTaskTestMixin
from .test_create_web_application_task import (
GmpCreateWebApplicationTaskTestMixin,
)
from .test_delete_task import GmpDeleteTaskTestMixin
from .test_get_task import GmpGetTaskTestMixin
from .test_get_tasks import GmpGetTasksTestMixin
Expand All @@ -27,6 +30,7 @@
"GmpCreateContainerTaskTestMixin",
"GmpCreateImportTaskTestMixin",
"GmpCreateTaskTestMixin",
"GmpCreateWebApplicationTaskTestMixin",
"GmpDeleteTaskTestMixin",
"GmpGetTaskTestMixin",
"GmpGetTasksTestMixin",
Expand Down
Loading
Loading