Skip to content

Commit

Permalink
Implement report config commands
Browse files Browse the repository at this point in the history
Extend GMP classed to support all report config commands.
  • Loading branch information
bjoernricks committed Feb 3, 2025
1 parent 2ccae4d commit 3f3b785
Show file tree
Hide file tree
Showing 13 changed files with 872 additions and 116 deletions.
14 changes: 13 additions & 1 deletion gvm/protocols/gmp/_gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

import warnings
from types import TracebackType
from typing import Callable, Optional, Type, Union

from gvm.__version__ import __version__
from gvm.connections import GvmConnection
from gvm.errors import GvmError
from gvm.protocols.core import Response
Expand All @@ -16,6 +18,7 @@
from .requests import Version

SUPPORTED_GMP_VERSIONS = Union[GMPv224[T], GMPv225[T], GMPv226[T]]
_SUPPORTED_GMP_VERSION_STRINGS = ["22.4", "22.5", "22.6"]


class GMP(GvmProtocol[T]):
Expand Down Expand Up @@ -91,12 +94,21 @@ def determine_supported_gmp(self) -> SUPPORTED_GMP_VERSIONS:
gmp_class = GMPv224
elif major_version == 22 and minor_version == 5:
gmp_class = GMPv225
elif major_version == 22 and minor_version == 6:
elif major_version == 22 and minor_version >= 6:
gmp_class = GMPv226
if minor_version > 6:
warnings.warn(

Check warning on line 100 in gvm/protocols/gmp/_gmp.py

View check run for this annotation

Codecov / codecov/patch

gvm/protocols/gmp/_gmp.py#L100

Added line #L100 was not covered by tests
"Remote manager daemon uses a newer GMP version then "
f"supported by python-gvm {__version__}. Please update to "
"a newer release of python-gvm if possible. "
f"Remote GMP version is {major_version}.{minor_version}. "
f"Supported GMP versions are {', '.join(_SUPPORTED_GMP_VERSION_STRINGS)}."
)
else:
raise GvmError(
"Remote manager daemon uses an unsupported version of GMP. "
f"The GMP version was {major_version}.{minor_version}"
f"Supported GMP versions are {', '.join(_SUPPORTED_GMP_VERSION_STRINGS)}."
)

return gmp_class(self._connection, transform=self._transform_callable) # type: ignore[arg-type]
Expand Down
116 changes: 114 additions & 2 deletions gvm/protocols/gmp/_gmp226.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-FileCopyrightText: 2024 Greenbone AG
# SPDX-FileCopyrightText: 2025 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""
Greenbone Management Protocol (GMP) version 22.6
"""

from typing import Optional, Union
from typing import Optional, Sequence, Union

from .._protocol import T
from ._gmp225 import GMPv225
Expand All @@ -15,6 +15,8 @@
EntityID,
Filters,
FilterType,
ReportConfigParameter,
ReportConfigs,
ReportFormatType,
Reports,
ResourceNames,
Expand Down Expand Up @@ -334,3 +336,113 @@ def modify_filter(
filter_type=filter_type,
)
)

def clone_report_config(self, report_config_id: EntityID) -> T:
"""Clone a report config from an existing one

Args:
report_config_id: UUID of the existing report config
"""
return self._send_and_transform_command(
ReportConfigs.clone_report_config(report_config_id)
)

def delete_report_config(
self,
report_config_id: EntityID,
*,
ultimate: Optional[bool] = False,
) -> T:
"""Deletes an existing report config

Args:
report_config_id: UUID of the report config to be deleted.
ultimate: Whether to remove entirely, or to the trashcan.
"""
return self._send_and_transform_command(
ReportConfigs.delete_report_config(
report_config_id, ultimate=ultimate
)
)

def get_report_configs(
self,
*,
filter_string: Optional[str] = None,
filter_id: Optional[EntityID] = None,
trash: Optional[bool] = None,
details: Optional[bool] = None,
) -> T:
"""Request a list of report configs

Args:
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
trash: Whether to get the trashcan report configs instead
details: Include report config details
"""
return self._send_and_transform_command(
ReportConfigs.get_report_configs(
filter_string=filter_string,
filter_id=filter_id,
trash=trash,
details=details,
)
)

def get_report_config(
self,
report_config_id: EntityID,
) -> T:
"""Request a single report config

Args:
report_config_id: UUID of an existing report config
"""
return self._send_and_transform_command(
ReportConfigs.get_report_config(report_config_id)
)

def create_report_config(
self,
name: str,
report_format_id: Union[EntityID, ReportFormatType],
*,
comment: Optional[str] = None,
params: Optional[Sequence[ReportConfigParameter]] = None,
) -> T:
"""Create a report config

Args:
name: Name of the new report config
report_format_id: UUID of the report format to be used or ReportFormatType.
comment: An optional comment for the report config.
params: A list of report config parameters.
"""
return self._send_and_transform_command(
ReportConfigs.create_report_config(
name, report_format_id, comment=comment, params=params
)
)

def modify_report_config(
self,
report_config_id: EntityID,
*,
name: Optional[str] = None,
comment: Optional[str] = None,
params: Optional[Sequence[ReportConfigParameter]] = None,
) -> T:
"""Create a report config

Args:
name: Name of the report config
report_config_id: UUID of the report config to be modified.
comment: An optional comment for the report config.
params: A list of report config parameters.
"""
return self._send_and_transform_command(
ReportConfigs.modify_report_config(
report_config_id, name=name, comment=comment, params=params
)
)
3 changes: 3 additions & 0 deletions gvm/protocols/gmp/requests/v226/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
)
from ._audit_reports import AuditReports
from ._filters import Filters, FilterType
from ._report_configs import ReportConfigParameter, ReportConfigs
from ._reports import Reports
from ._resource_names import ResourceNames, ResourceType

Expand Down Expand Up @@ -113,6 +114,8 @@
"Policies",
"PortLists",
"PortRangeType",
"ReportConfigs",
"ReportConfigParameter",
"ReportFormatType",
"ReportFormats",
"Reports",
Expand Down
Loading

0 comments on commit 3f3b785

Please sign in to comment.