Skip to content

Commit 36dbf56

Browse files
committed
add: add web application task support to python-gvm
1 parent dabe887 commit 36dbf56

6 files changed

Lines changed: 439 additions & 0 deletions

File tree

gvm/protocols/gmp/_gmpnext.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,49 @@ def create_container_task(
709709
Tasks.create_container_task(name=name, comment=comment)
710710
)
711711

712+
def create_web_application_task(
713+
self,
714+
name: str,
715+
web_application_target_id: EntityID,
716+
scanner_id: EntityID,
717+
*,
718+
comment: str | None = None,
719+
alterable: bool | None = None,
720+
schedule_id: EntityID | None = None,
721+
alert_ids: Sequence[EntityID] | None = None,
722+
schedule_periods: int | None = None,
723+
observers: Sequence[str] | None = None,
724+
preferences: Mapping[str, SupportsStr] | None = None,
725+
) -> T:
726+
"""Create a new scan task using an OCI image target.
727+
728+
Args:
729+
name: Name of the new task.
730+
web_application_target_id: UUID of the web application target to be scanned.
731+
scanner_id: UUID of scanner to use for scanning the agents.
732+
comment: Optional comment for the task.
733+
alterable: Whether the task should be alterable.
734+
alert_ids: List of UUIDs for alerts to be applied to the task.
735+
schedule_id: UUID of a schedule when the task should be run.
736+
schedule_periods: Limit to number of scheduled runs, 0 for unlimited.
737+
observers: List of usernames or IDs allowed to observe the task.
738+
preferences: Scanner preferences as name/value pairs.
739+
"""
740+
return self._send_request_and_transform_response(
741+
Tasks.create_web_application_task(
742+
name=name,
743+
web_application_target_id=web_application_target_id,
744+
scanner_id=scanner_id,
745+
comment=comment,
746+
alterable=alterable,
747+
schedule_id=schedule_id,
748+
alert_ids=alert_ids,
749+
schedule_periods=schedule_periods,
750+
observers=observers,
751+
preferences=preferences,
752+
)
753+
)
754+
712755
def create_task(
713756
self,
714757
name: str,
@@ -826,6 +869,7 @@ def modify_task(
826869
scanner_id: EntityID | None = None,
827870
agent_group_id: EntityID | None = None,
828871
oci_image_target_id: EntityID | None = None,
872+
web_application_target_id: EntityID | None = None,
829873
alterable: bool | None = None,
830874
hosts_ordering: HostsOrdering | None = None,
831875
schedule_id: EntityID | None = None,
@@ -845,6 +889,7 @@ def modify_task(
845889
scanner_id: UUID of scanner to use for scanning the target
846890
agent_group_id: UUID of agent group to use for scanning
847891
oci_image_target_id: UUID of the OCI Image target to be scanned.
892+
web_application_target_id: UUID of the web application target to be scanned.
848893
comment: The comment on the task.
849894
alert_ids: List of UUIDs for alerts to be applied to the task
850895
hosts_ordering: The order hosts are scanned in
@@ -864,6 +909,7 @@ def modify_task(
864909
scanner_id=scanner_id,
865910
agent_group_id=agent_group_id,
866911
oci_image_target_id=oci_image_target_id,
912+
web_application_target_id=web_application_target_id,
867913
alterable=alterable,
868914
hosts_ordering=hosts_ordering,
869915
schedule_id=schedule_id,

gvm/protocols/gmp/requests/next/_tasks.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,97 @@ def create_container_task(
248248
"""
249249
return cls.create_import_task(name=name, comment=comment)
250250

251+
@classmethod
252+
def create_web_application_task(
253+
cls,
254+
name: str,
255+
web_application_target_id: EntityID,
256+
scanner_id: EntityID,
257+
*,
258+
comment: str | None = None,
259+
alterable: bool | None = None,
260+
schedule_id: EntityID | None = None,
261+
alert_ids: Sequence[EntityID] | None = None,
262+
schedule_periods: int | None = None,
263+
observers: Sequence[str] | None = None,
264+
preferences: Mapping[str, SupportsStr] | None = None,
265+
) -> Request:
266+
"""Create a new scan task using a web application target.
267+
268+
Args:
269+
name: Name of the new task.
270+
web_application_target_id: UUID of the web application target to be scanned.
271+
scanner_id: UUID of scanner to use for scanning the web application.
272+
comment: Optional comment for the task.
273+
alterable: Whether the task should be alterable.
274+
alert_ids: List of UUIDs for alerts to be applied to the task.
275+
schedule_id: UUID of a schedule when the task should be run.
276+
schedule_periods: Limit to number of scheduled runs, 0 for unlimited.
277+
observers: List of usernames or IDs allowed to observe the task.
278+
preferences: Scanner preferences as name/value pairs.
279+
"""
280+
if not name:
281+
raise RequiredArgument(
282+
function=cls.create_web_application_task.__name__,
283+
argument="name",
284+
)
285+
286+
if not web_application_target_id:
287+
raise RequiredArgument(
288+
function=cls.create_web_application_task.__name__,
289+
argument="web_application_target_id",
290+
)
291+
292+
if not scanner_id:
293+
raise RequiredArgument(
294+
function=cls.create_web_application_task.__name__,
295+
argument="scanner_id",
296+
)
297+
298+
cmd = XmlCommand("create_task")
299+
cmd.add_element("name", name)
300+
cmd.add_element("usage_type", "scan")
301+
cmd.add_element(
302+
"web_application_target",
303+
attrs={"id": str(web_application_target_id)},
304+
)
305+
cmd.add_element("scanner", attrs={"id": str(scanner_id)})
306+
307+
if comment:
308+
cmd.add_element("comment", comment)
309+
310+
if alterable is not None:
311+
cmd.add_element("alterable", to_bool(alterable))
312+
313+
if alert_ids:
314+
for alert in alert_ids:
315+
cmd.add_element("alert", attrs={"id": str(alert)})
316+
317+
if schedule_id:
318+
cmd.add_element("schedule", attrs={"id": str(schedule_id)})
319+
320+
if schedule_periods is not None:
321+
if (
322+
not isinstance(schedule_periods, Integral)
323+
or schedule_periods < 0
324+
):
325+
raise InvalidArgument(
326+
"schedule_periods must be an integer greater or equal than 0"
327+
)
328+
cmd.add_element("schedule_periods", str(schedule_periods))
329+
330+
if observers:
331+
cmd.add_element("observers", to_comma_list(observers))
332+
333+
if preferences is not None:
334+
xml_prefs = cmd.add_element("preferences")
335+
for pref_name, pref_value in preferences.items():
336+
xml_pref = xml_prefs.add_element("preference")
337+
xml_pref.add_element("scanner_name", pref_name)
338+
xml_pref.add_element("value", str(pref_value))
339+
340+
return cmd
341+
251342
@classmethod
252343
def create_task(
253344
cls,
@@ -453,6 +544,7 @@ def modify_task(
453544
scanner_id: EntityID | None = None,
454545
agent_group_id: EntityID | None = None,
455546
oci_image_target_id: EntityID | None = None,
547+
web_application_target_id: EntityID | None = None,
456548
alterable: bool | None = None,
457549
hosts_ordering: HostsOrdering | None = None,
458550
schedule_id: EntityID | None = None,
@@ -472,6 +564,7 @@ def modify_task(
472564
scanner_id: UUID of scanner to use for scanning the target
473565
agent_group_id: UUID of agent group to use for scanning
474566
oci_image_target_id: UUID of the OCI Image target to be scanned.
567+
web_application_target_id: UUID of the web application target to be scanned.
475568
comment: The comment on the task.
476569
alert_ids: List of UUIDs for alerts to be applied to the task
477570
hosts_ordering: The order hosts are scanned in
@@ -507,6 +600,30 @@ def modify_task(
507600
cmd = XmlCommand("modify_task")
508601
cmd.set_attribute("task_id", str(task_id))
509602

603+
if (
604+
sum(
605+
entity_id is not None
606+
for entity_id in (
607+
target_id,
608+
agent_group_id,
609+
oci_image_target_id,
610+
web_application_target_id,
611+
)
612+
)
613+
> 1
614+
):
615+
raise InvalidArgument(
616+
function=cls.modify_task.__name__,
617+
argument=(
618+
"target_id/agent_group_id/oci_image_target_id/"
619+
"web_application_target_id"
620+
),
621+
message=(
622+
"Only one of target_id, agent_group_id, oci_image_target_id "
623+
"or web_application_target_id can be modified at a time"
624+
),
625+
)
626+
510627
if name:
511628
cmd.add_element("name", name)
512629

@@ -526,6 +643,11 @@ def modify_task(
526643
cmd.add_element(
527644
"oci_image_target", attrs={"id": str(oci_image_target_id)}
528645
)
646+
if web_application_target_id:
647+
cmd.add_element(
648+
"web_application_target",
649+
attrs={"id": str(web_application_target_id)},
650+
)
529651

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

tests/protocols/gmpnext/entities/tasks/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from .test_create_container_task import GmpCreateContainerTaskTestMixin
1212
from .test_create_import_task import GmpCreateImportTaskTestMixin
1313
from .test_create_task import GmpCreateTaskTestMixin
14+
from .test_create_web_application_task import (
15+
GmpCreateWebApplicationTaskTestMixin,
16+
)
1417
from .test_delete_task import GmpDeleteTaskTestMixin
1518
from .test_get_task import GmpGetTaskTestMixin
1619
from .test_get_tasks import GmpGetTasksTestMixin
@@ -27,6 +30,7 @@
2730
"GmpCreateContainerTaskTestMixin",
2831
"GmpCreateImportTaskTestMixin",
2932
"GmpCreateTaskTestMixin",
33+
"GmpCreateWebApplicationTaskTestMixin",
3034
"GmpDeleteTaskTestMixin",
3135
"GmpGetTaskTestMixin",
3236
"GmpGetTasksTestMixin",

0 commit comments

Comments
 (0)