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
46 changes: 46 additions & 0 deletions gvm/protocols/gmp/_gmpnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,49 @@ def create_agent_group_task(
)
)

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

Args:
name: Name of the new task.
oci_image_target_id: UUID of the OCI image 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_container_image_task(
name=name,
oci_image_target_id=oci_image_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_container_task(
self, name: str, *, comment: Optional[str] = None
) -> T:
Expand Down Expand Up @@ -586,6 +629,7 @@ def modify_task(
target_id: Optional[EntityID] = None,
scanner_id: Optional[EntityID] = None,
agent_group_id: Optional[EntityID] = None,
oci_image_target_id: Optional[EntityID] = None,
alterable: Optional[bool] = None,
hosts_ordering: Optional[HostsOrdering] = None,
schedule_id: Optional[EntityID] = None,
Expand All @@ -604,6 +648,7 @@ def modify_task(
target_id: UUID of target to be scanned
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.
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 @@ -622,6 +667,7 @@ def modify_task(
target_id=target_id,
scanner_id=scanner_id,
agent_group_id=agent_group_id,
oci_image_target_id=oci_image_target_id,
alterable=alterable,
hosts_ordering=hosts_ordering,
schedule_id=schedule_id,
Expand Down
109 changes: 106 additions & 3 deletions gvm/protocols/gmp/requests/next/_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,96 @@ def create_agent_group_task(

return cmd

@classmethod
def create_container_image_task(
cls,
name: str,
oci_image_target_id: EntityID,
scanner_id: EntityID,
*,
comment: Optional[str] = None,
alterable: Optional[bool] = None,
schedule_id: Optional[EntityID] = None,
alert_ids: Optional[Sequence[EntityID]] = None,
schedule_periods: Optional[int] = None,
observers: Optional[Sequence[str]] = None,
preferences: Optional[Mapping[str, SupportsStr]] = None,
) -> Request:
"""Create a new scan task using an OCI image target.

Args:
name: Name of the new task.
oci_image_target_id: UUID of the OCI Image 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.
"""
if not name:
raise RequiredArgument(
function=cls.create_container_image_task.__name__,
argument="name",
)

if not oci_image_target_id:
raise RequiredArgument(
function=cls.create_container_image_task.__name__,
argument="oci_image_target_id",
)

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

cmd = XmlCommand("create_task")
cmd.add_element("name", name)
cmd.add_element("usage_type", "scan")
cmd.add_element(
"oci_image_target", attrs={"id": str(oci_image_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_container_task(
cls, name: str, *, comment: Optional[str] = None
Expand Down Expand Up @@ -350,6 +440,7 @@ def modify_task(
target_id: Optional[EntityID] = None,
scanner_id: Optional[EntityID] = None,
agent_group_id: Optional[EntityID] = None,
oci_image_target_id: Optional[EntityID] = None,
alterable: Optional[bool] = None,
hosts_ordering: Optional[HostsOrdering] = None,
schedule_id: Optional[EntityID] = None,
Expand All @@ -368,6 +459,7 @@ def modify_task(
target_id: UUID of target to be scanned
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.
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 @@ -383,11 +475,17 @@ def modify_task(
function=cls.modify_task.__name__, argument="task_id"
)

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

cmd = XmlCommand("modify_task")
Expand All @@ -408,6 +506,11 @@ def modify_task(
if agent_group_id:
cmd.add_element("agent_group", attrs={"id": str(agent_group_id)})

if oci_image_target_id:
cmd.add_element(
"oci_image_target", attrs={"id": str(oci_image_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 @@ -5,6 +5,9 @@

from .test_clone_task import GmpCloneTaskTestMixin
from .test_create_agent_group_task import GmpCreateAgentGroupTaskTestMixin
from .test_create_container_image_task import (
GmpCreateContainerImageTaskTestMixin,
)
from .test_create_container_task import GmpCreateContainerTaskTestMixin
from .test_create_task import GmpCreateTaskTestMixin
from .test_delete_task import GmpDeleteTaskTestMixin
Expand All @@ -19,6 +22,7 @@
__all__ = (
"GmpCloneTaskTestMixin",
"GmpCreateAgentGroupTaskTestMixin",
"GmpCreateContainerImageTaskTestMixin",
"GmpCreateContainerTaskTestMixin",
"GmpCreateTaskTestMixin",
"GmpDeleteTaskTestMixin",
Expand Down
Loading