1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2021 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
# pylint: disable=redefined-builtin |
20
|
|
|
|
21
|
|
|
from typing import Any, Optional, Union |
22
|
|
|
from lxml.etree import XMLSyntaxError |
23
|
|
|
|
24
|
|
|
from gvm.errors import InvalidArgument, RequiredArgument |
25
|
|
|
from gvm.protocols.gmpv208.entities.report_formats import ( |
26
|
|
|
ReportFormatType, |
27
|
|
|
) # if I use latest, I get circular import :/ |
28
|
|
|
from gvm.utils import add_filter, to_bool |
29
|
|
|
from gvm.xml import XmlCommand |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class ReportsMixin: |
33
|
|
|
def delete_report(self, report_id: str) -> Any: |
34
|
|
|
"""Deletes an existing report |
35
|
|
|
|
36
|
|
|
Arguments: |
37
|
|
|
report_id: UUID of the report to be deleted. |
38
|
|
|
""" |
39
|
|
|
if not report_id: |
40
|
|
|
raise RequiredArgument( |
41
|
|
|
function=self.delete_report.__name__, argument='report_id' |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
cmd = XmlCommand("delete_report") |
45
|
|
|
cmd.set_attribute("report_id", report_id) |
46
|
|
|
|
47
|
|
|
return self._send_xml_command(cmd) |
48
|
|
|
|
49
|
|
|
def get_report( |
50
|
|
|
self, |
51
|
|
|
report_id: str, |
52
|
|
|
*, |
53
|
|
|
filter: Optional[str] = None, |
54
|
|
|
filter_id: Optional[str] = None, |
55
|
|
|
delta_report_id: Optional[str] = None, |
56
|
|
|
report_format_id: Optional[Union[str, ReportFormatType]] = None, |
57
|
|
|
ignore_pagination: Optional[bool] = None, |
58
|
|
|
details: Optional[bool] = True, |
59
|
|
|
) -> Any: |
60
|
|
|
"""Request a single report |
61
|
|
|
|
62
|
|
|
Arguments: |
63
|
|
|
report_id: UUID of an existing report |
64
|
|
|
filter: Filter term to use to filter results in the report |
65
|
|
|
filter_id: UUID of filter to use to filter results in the report |
66
|
|
|
delta_report_id: UUID of an existing report to compare report to. |
67
|
|
|
report_format_id: UUID of report format to use |
68
|
|
|
or ReportFormatType (enum) |
69
|
|
|
ignore_pagination: Whether to ignore the filter terms "first" and |
70
|
|
|
"rows". |
71
|
|
|
details: Request additional report information details |
72
|
|
|
defaults to True |
73
|
|
|
|
74
|
|
|
Returns: |
75
|
|
|
The response. See :py:meth:`send_command` for details. |
76
|
|
|
""" |
77
|
|
|
cmd = XmlCommand("get_reports") |
78
|
|
|
|
79
|
|
|
if not report_id: |
80
|
|
|
raise RequiredArgument( |
81
|
|
|
function=self.get_report.__name__, argument='report_id' |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
cmd.set_attribute("report_id", report_id) |
85
|
|
|
|
86
|
|
|
add_filter(cmd, filter, filter_id) |
87
|
|
|
|
88
|
|
|
if delta_report_id: |
89
|
|
|
cmd.set_attribute("delta_report_id", delta_report_id) |
90
|
|
|
|
91
|
|
|
if report_format_id: |
92
|
|
|
if isinstance(report_format_id, ReportFormatType): |
93
|
|
|
report_format_id = report_format_id.value |
94
|
|
|
|
95
|
|
|
cmd.set_attribute("format_id", report_format_id) |
96
|
|
|
|
97
|
|
|
if ignore_pagination is not None: |
98
|
|
|
cmd.set_attribute("ignore_pagination", to_bool(ignore_pagination)) |
99
|
|
|
|
100
|
|
|
cmd.set_attribute("details", to_bool(details)) |
101
|
|
|
|
102
|
|
|
return self._send_xml_command(cmd) |
103
|
|
|
|
104
|
|
View Code Duplication |
def get_reports( |
|
|
|
|
105
|
|
|
self, |
106
|
|
|
*, |
107
|
|
|
filter: Optional[str] = None, |
108
|
|
|
filter_id: Optional[str] = None, |
109
|
|
|
note_details: Optional[bool] = None, |
110
|
|
|
override_details: Optional[bool] = None, |
111
|
|
|
details: Optional[bool] = None, |
112
|
|
|
) -> Any: |
113
|
|
|
"""Request a list of reports |
114
|
|
|
|
115
|
|
|
Arguments: |
116
|
|
|
filter: Filter term to use for the query |
117
|
|
|
filter_id: UUID of an existing filter to use for the query |
118
|
|
|
note_details: If notes are included, whether to include note details |
119
|
|
|
override_details: If overrides are included, whether to include |
120
|
|
|
override details |
121
|
|
|
details: Whether to exclude results |
122
|
|
|
|
123
|
|
|
Returns: |
124
|
|
|
The response. See :py:meth:`send_command` for details. |
125
|
|
|
""" |
126
|
|
|
cmd = XmlCommand("get_reports") |
127
|
|
|
|
128
|
|
|
if filter: |
129
|
|
|
cmd.set_attribute("report_filter", filter) |
130
|
|
|
|
131
|
|
|
if filter_id: |
132
|
|
|
cmd.set_attribute("report_filt_id", filter_id) |
133
|
|
|
|
134
|
|
|
if note_details is not None: |
135
|
|
|
cmd.set_attribute("note_details", to_bool(note_details)) |
136
|
|
|
|
137
|
|
|
if override_details is not None: |
138
|
|
|
cmd.set_attribute("override_details", to_bool(override_details)) |
139
|
|
|
|
140
|
|
|
if details is not None: |
141
|
|
|
cmd.set_attribute("details", to_bool(details)) |
142
|
|
|
|
143
|
|
|
cmd.set_attribute("ignore_pagination", "1") |
144
|
|
|
|
145
|
|
|
return self._send_xml_command(cmd) |
146
|
|
|
|
147
|
|
|
def import_report( |
148
|
|
|
self, |
149
|
|
|
report: str, |
150
|
|
|
*, |
151
|
|
|
task_id: Optional[str] = None, |
152
|
|
|
in_assets: Optional[bool] = None, |
153
|
|
|
) -> Any: |
154
|
|
|
"""Import a Report from XML |
155
|
|
|
|
156
|
|
|
Arguments: |
157
|
|
|
report: Report XML as string to import. This XML must contain |
158
|
|
|
a :code:`<report>` root element. |
159
|
|
|
task_id: UUID of task to import report to |
160
|
|
|
in_asset: Whether to create or update assets using the report |
161
|
|
|
|
162
|
|
|
Returns: |
163
|
|
|
The response. See :py:meth:`send_command` for details. |
164
|
|
|
""" |
165
|
|
|
if not report: |
166
|
|
|
raise RequiredArgument( |
167
|
|
|
function=self.import_report.__name__, argument='report' |
168
|
|
|
) |
169
|
|
|
|
170
|
|
|
cmd = XmlCommand("create_report") |
171
|
|
|
|
172
|
|
|
if task_id: |
173
|
|
|
cmd.add_element("task", attrs={"id": task_id}) |
174
|
|
|
else: |
175
|
|
|
raise RequiredArgument( |
176
|
|
|
function=self.import_report.__name__, argument='task_id' |
177
|
|
|
) |
178
|
|
|
|
179
|
|
|
if in_assets is not None: |
180
|
|
|
cmd.add_element("in_assets", to_bool(in_assets)) |
181
|
|
|
|
182
|
|
|
try: |
183
|
|
|
cmd.append_xml_str(report) |
184
|
|
|
except XMLSyntaxError as e: |
185
|
|
|
raise InvalidArgument( |
186
|
|
|
"Invalid xml passed as report to import_report {}".format(e) |
187
|
|
|
) from None |
188
|
|
|
|
189
|
|
|
return self._send_xml_command(cmd) |
190
|
|
|
|