|
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
|
|
|
from typing import Any, List, Optional, Tuple |
|
20
|
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType |
|
22
|
|
|
from gvm.utils import add_filter, to_base64, to_bool, is_list_like |
|
23
|
|
|
from gvm.xml import XmlCommand |
|
24
|
|
|
|
|
25
|
|
|
_EMPTY_POLICY_ID = '085569ce-73ed-11df-83c3-002264764cea' |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class PoliciesMixin: |
|
29
|
|
|
def clone_policy(self, policy_id: str) -> Any: |
|
30
|
|
|
"""Clone a policy from an existing one |
|
31
|
|
|
|
|
32
|
|
|
Arguments: |
|
33
|
|
|
policy_id: UUID of the existing policy |
|
34
|
|
|
|
|
35
|
|
|
Returns: |
|
36
|
|
|
The response. See :py:meth:`send_command` for details. |
|
37
|
|
|
""" |
|
38
|
|
|
if not policy_id: |
|
39
|
|
|
raise RequiredArgument( |
|
40
|
|
|
function=self.clone_policy.__name__, argument='policy_id' |
|
41
|
|
|
) |
|
42
|
|
|
|
|
43
|
|
|
cmd = XmlCommand("create_config") |
|
44
|
|
|
cmd.add_element("copy", policy_id) |
|
45
|
|
|
return self._send_xml_command(cmd) |
|
46
|
|
|
|
|
47
|
|
|
def create_policy( |
|
48
|
|
|
self, name: str, *, policy_id: str = None, comment: Optional[str] = None |
|
49
|
|
|
) -> Any: |
|
50
|
|
|
"""Create a new policy |
|
51
|
|
|
|
|
52
|
|
|
Arguments: |
|
53
|
|
|
name: Name of the new policy |
|
54
|
|
|
policy_id: UUID of an existing policy as base. By default the empty |
|
55
|
|
|
policy is used. |
|
56
|
|
|
comment: A comment on the policy |
|
57
|
|
|
|
|
58
|
|
|
Returns: |
|
59
|
|
|
The response. See :py:meth:`send_command` for details. |
|
60
|
|
|
""" |
|
61
|
|
|
cmd = XmlCommand("create_config") |
|
62
|
|
|
|
|
63
|
|
|
if policy_id is None: |
|
64
|
|
|
policy_id = _EMPTY_POLICY_ID |
|
65
|
|
|
if not name: |
|
66
|
|
|
raise RequiredArgument( |
|
67
|
|
|
function=self.create_policy.__name__, argument='name' |
|
68
|
|
|
) |
|
69
|
|
|
|
|
70
|
|
|
if comment is not None: |
|
71
|
|
|
cmd.add_element("comment", comment) |
|
72
|
|
|
cmd.add_element("copy", policy_id) |
|
73
|
|
|
cmd.add_element("name", name) |
|
74
|
|
|
cmd.add_element("usage_type", "policy") |
|
75
|
|
|
return self._send_xml_command(cmd) |
|
76
|
|
|
|
|
77
|
|
|
def delete_policy( |
|
78
|
|
|
self, policy_id: str, *, ultimate: Optional[bool] = False |
|
79
|
|
|
) -> Any: |
|
80
|
|
|
"""Deletes an existing policy |
|
81
|
|
|
Arguments: |
|
82
|
|
|
policy_id: UUID of the policy to be deleted. |
|
83
|
|
|
ultimate: Whether to remove entirely, or to the trashcan. |
|
84
|
|
|
""" |
|
85
|
|
|
if not policy_id: |
|
86
|
|
|
raise RequiredArgument( |
|
87
|
|
|
function=self.delete_policy.__name__, argument='policy_id' |
|
88
|
|
|
) |
|
89
|
|
|
|
|
90
|
|
|
cmd = XmlCommand("delete_config") |
|
91
|
|
|
cmd.set_attribute("config_id", policy_id) |
|
92
|
|
|
cmd.set_attribute("ultimate", to_bool(ultimate)) |
|
93
|
|
|
|
|
94
|
|
|
return self._send_xml_command(cmd) |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
def get_policies( |
|
|
|
|
|
|
97
|
|
|
self, |
|
98
|
|
|
*, |
|
99
|
|
|
audits: Optional[bool] = None, |
|
100
|
|
|
filter_string: Optional[str] = None, |
|
101
|
|
|
filter_id: Optional[str] = None, |
|
102
|
|
|
details: Optional[bool] = None, |
|
103
|
|
|
families: Optional[bool] = None, |
|
104
|
|
|
preferences: Optional[bool] = None, |
|
105
|
|
|
trash: Optional[bool] = None, |
|
106
|
|
|
) -> Any: |
|
107
|
|
|
"""Request a list of policies |
|
108
|
|
|
|
|
109
|
|
|
Arguments: |
|
110
|
|
|
audits: Whether to get audits using the policy |
|
111
|
|
|
filter_string: Filter term to use for the query |
|
112
|
|
|
filter_id: UUID of an existing filter to use for the query |
|
113
|
|
|
details: Whether to get families, preferences, nvt selectors |
|
114
|
|
|
and tasks. |
|
115
|
|
|
families: Whether to include the families if no details are |
|
116
|
|
|
requested |
|
117
|
|
|
preferences: Whether to include the preferences if no details are |
|
118
|
|
|
requested |
|
119
|
|
|
trash: Whether to get the trashcan audits instead |
|
120
|
|
|
|
|
121
|
|
|
Returns: |
|
122
|
|
|
The response. See :py:meth:`send_command` for details. |
|
123
|
|
|
""" |
|
124
|
|
|
cmd = XmlCommand("get_configs") |
|
125
|
|
|
cmd.set_attribute("usage_type", "policy") |
|
126
|
|
|
|
|
127
|
|
|
add_filter(cmd, filter_string, filter_id) |
|
128
|
|
|
|
|
129
|
|
|
if trash is not None: |
|
130
|
|
|
cmd.set_attribute("trash", to_bool(trash)) |
|
131
|
|
|
|
|
132
|
|
|
if details is not None: |
|
133
|
|
|
cmd.set_attribute("details", to_bool(details)) |
|
134
|
|
|
|
|
135
|
|
|
if families is not None: |
|
136
|
|
|
cmd.set_attribute("families", to_bool(families)) |
|
137
|
|
|
|
|
138
|
|
|
if preferences is not None: |
|
139
|
|
|
cmd.set_attribute("preferences", to_bool(preferences)) |
|
140
|
|
|
|
|
141
|
|
|
if audits is not None: |
|
142
|
|
|
cmd.set_attribute("tasks", to_bool(audits)) |
|
143
|
|
|
|
|
144
|
|
|
return self._send_xml_command(cmd) |
|
145
|
|
|
|
|
146
|
|
|
def get_policy( |
|
147
|
|
|
self, policy_id: str, *, audits: Optional[bool] = None |
|
148
|
|
|
) -> Any: |
|
149
|
|
|
"""Request a single policy |
|
150
|
|
|
|
|
151
|
|
|
Arguments: |
|
152
|
|
|
policy_id: UUID of an existing policy |
|
153
|
|
|
audits: Whether to get audits using this policy |
|
154
|
|
|
|
|
155
|
|
|
Returns: |
|
156
|
|
|
The response. See :py:meth:`send_command` for details. |
|
157
|
|
|
""" |
|
158
|
|
|
if not policy_id: |
|
159
|
|
|
raise RequiredArgument( |
|
160
|
|
|
function=self.get_policy.__name__, argument='policy_id' |
|
161
|
|
|
) |
|
162
|
|
|
|
|
163
|
|
|
cmd = XmlCommand("get_configs") |
|
164
|
|
|
cmd.set_attribute("config_id", policy_id) |
|
165
|
|
|
|
|
166
|
|
|
cmd.set_attribute("usage_type", "policy") |
|
167
|
|
|
|
|
168
|
|
|
if audits is not None: |
|
169
|
|
|
cmd.set_attribute("tasks", to_bool(audits)) |
|
170
|
|
|
|
|
171
|
|
|
# for single entity always request all details |
|
172
|
|
|
cmd.set_attribute("details", "1") |
|
173
|
|
|
|
|
174
|
|
|
return self._send_xml_command(cmd) |
|
175
|
|
|
|
|
176
|
|
View Code Duplication |
def modify_policy_set_nvt_preference( |
|
|
|
|
|
|
177
|
|
|
self, |
|
178
|
|
|
policy_id: str, |
|
179
|
|
|
name: str, |
|
180
|
|
|
nvt_oid: str, |
|
181
|
|
|
*, |
|
182
|
|
|
value: Optional[str] = None, |
|
183
|
|
|
) -> Any: |
|
184
|
|
|
"""Modifies the nvt preferences of an existing policy. |
|
185
|
|
|
|
|
186
|
|
|
Arguments: |
|
187
|
|
|
policy_id: UUID of policy to modify. |
|
188
|
|
|
name: Name for preference to change. |
|
189
|
|
|
nvt_oid: OID of the NVT associated with preference to modify |
|
190
|
|
|
value: New value for the preference. None to delete the preference |
|
191
|
|
|
and to use the default instead. |
|
192
|
|
|
""" |
|
193
|
|
|
if not policy_id: |
|
194
|
|
|
raise RequiredArgument( |
|
195
|
|
|
function=self.modify_policy_set_nvt_preference.__name__, |
|
196
|
|
|
argument='policy_id', |
|
197
|
|
|
) |
|
198
|
|
|
|
|
199
|
|
|
if not nvt_oid: |
|
200
|
|
|
raise RequiredArgument( |
|
201
|
|
|
function=self.modify_policy_set_nvt_preference.__name__, |
|
202
|
|
|
argument='nvt_oid', |
|
203
|
|
|
) |
|
204
|
|
|
|
|
205
|
|
|
if not name: |
|
206
|
|
|
raise RequiredArgument( |
|
207
|
|
|
function=self.modify_policy_set_nvt_preference.__name__, |
|
208
|
|
|
argument='name', |
|
209
|
|
|
) |
|
210
|
|
|
|
|
211
|
|
|
cmd = XmlCommand("modify_config") |
|
212
|
|
|
cmd.set_attribute("config_id", str(policy_id)) |
|
213
|
|
|
|
|
214
|
|
|
_xmlpref = cmd.add_element("preference") |
|
215
|
|
|
|
|
216
|
|
|
_xmlpref.add_element("nvt", attrs={"oid": nvt_oid}) |
|
217
|
|
|
_xmlpref.add_element("name", name) |
|
218
|
|
|
|
|
219
|
|
|
if value: |
|
220
|
|
|
_xmlpref.add_element("value", to_base64(value)) |
|
221
|
|
|
|
|
222
|
|
|
return self._send_xml_command(cmd) |
|
223
|
|
|
|
|
224
|
|
|
def modify_policy_set_name(self, policy_id: str, name: str) -> Any: |
|
225
|
|
|
"""Modifies the name of an existing policy |
|
226
|
|
|
|
|
227
|
|
|
Arguments: |
|
228
|
|
|
policy_id: UUID of policy to modify. |
|
229
|
|
|
name: New name for the policy. |
|
230
|
|
|
""" |
|
231
|
|
|
if not policy_id: |
|
232
|
|
|
raise RequiredArgument( |
|
233
|
|
|
function=self.modify_policy_set_name.__name__, |
|
234
|
|
|
argument='policy_id', |
|
235
|
|
|
) |
|
236
|
|
|
|
|
237
|
|
|
if not name: |
|
238
|
|
|
raise RequiredArgument( |
|
239
|
|
|
function=self.modify_policy_set_name.__name__, |
|
240
|
|
|
argument='name', |
|
241
|
|
|
) |
|
242
|
|
|
|
|
243
|
|
|
cmd = XmlCommand("modify_config") |
|
244
|
|
|
cmd.set_attribute("config_id", str(policy_id)) |
|
245
|
|
|
|
|
246
|
|
|
cmd.add_element("name", name) |
|
247
|
|
|
|
|
248
|
|
|
return self._send_xml_command(cmd) |
|
249
|
|
|
|
|
250
|
|
|
def modify_policy_set_comment( |
|
251
|
|
|
self, policy_id: str, comment: Optional[str] = None |
|
252
|
|
|
) -> Any: |
|
253
|
|
|
"""Modifies the comment of an existing policy |
|
254
|
|
|
|
|
255
|
|
|
Arguments: |
|
256
|
|
|
policy_id: UUID of policy to modify. |
|
257
|
|
|
comment: Comment to set on a policy. Default is an |
|
258
|
|
|
empty comment and the previous comment will be |
|
259
|
|
|
removed. |
|
260
|
|
|
""" |
|
261
|
|
|
if not policy_id: |
|
262
|
|
|
raise RequiredArgument( |
|
263
|
|
|
function=self.modify_policy_set_comment.__name__, |
|
264
|
|
|
argument='policy_id', |
|
265
|
|
|
) |
|
266
|
|
|
|
|
267
|
|
|
cmd = XmlCommand("modify_config") |
|
268
|
|
|
cmd.set_attribute("config_id", str(policy_id)) |
|
269
|
|
|
if not comment: |
|
270
|
|
|
comment = "" |
|
271
|
|
|
cmd.add_element("comment", comment) |
|
272
|
|
|
|
|
273
|
|
|
return self._send_xml_command(cmd) |
|
274
|
|
|
|
|
275
|
|
|
def modify_policy_set_scanner_preference( |
|
276
|
|
|
self, policy_id: str, name: str, *, value: Optional[str] = None |
|
277
|
|
|
) -> Any: |
|
278
|
|
|
"""Modifies the scanner preferences of an existing policy |
|
279
|
|
|
|
|
280
|
|
|
Arguments: |
|
281
|
|
|
policy_id: UUID of policy to modify. |
|
282
|
|
|
name: Name of the scanner preference to change |
|
283
|
|
|
value: New value for the preference. None to delete the preference |
|
284
|
|
|
and to use the default instead. |
|
285
|
|
|
|
|
286
|
|
|
""" |
|
287
|
|
|
if not policy_id: |
|
288
|
|
|
raise RequiredArgument( |
|
289
|
|
|
function=(self.modify_policy_set_scanner_preference.__name__), |
|
290
|
|
|
argument='policy_id', |
|
291
|
|
|
) |
|
292
|
|
|
|
|
293
|
|
|
if not name: |
|
294
|
|
|
raise RequiredArgument( |
|
295
|
|
|
function=(self.modify_policy_set_scanner_preference.__name__), |
|
296
|
|
|
argument='name argument', |
|
297
|
|
|
) |
|
298
|
|
|
|
|
299
|
|
|
cmd = XmlCommand("modify_config") |
|
300
|
|
|
cmd.set_attribute("config_id", str(policy_id)) |
|
301
|
|
|
|
|
302
|
|
|
_xmlpref = cmd.add_element("preference") |
|
303
|
|
|
|
|
304
|
|
|
_xmlpref.add_element("name", name) |
|
305
|
|
|
|
|
306
|
|
|
if value: |
|
307
|
|
|
_xmlpref.add_element("value", to_base64(value)) |
|
308
|
|
|
|
|
309
|
|
|
return self._send_xml_command(cmd) |
|
310
|
|
|
|
|
311
|
|
View Code Duplication |
def modify_policy_set_nvt_selection( |
|
|
|
|
|
|
312
|
|
|
self, policy_id: str, family: str, nvt_oids: List[str] |
|
313
|
|
|
) -> Any: |
|
314
|
|
|
"""Modifies the selected nvts of an existing policy |
|
315
|
|
|
|
|
316
|
|
|
The manager updates the given family in the policy to include only the |
|
317
|
|
|
given NVTs. |
|
318
|
|
|
|
|
319
|
|
|
Arguments: |
|
320
|
|
|
policy_id: UUID of policy to modify. |
|
321
|
|
|
family: Name of the NVT family to include NVTs from |
|
322
|
|
|
nvt_oids: List of NVTs to select for the family. |
|
323
|
|
|
""" |
|
324
|
|
|
if not policy_id: |
|
325
|
|
|
raise RequiredArgument( |
|
326
|
|
|
function=self.modify_policy_set_nvt_selection.__name__, |
|
327
|
|
|
argument='policy_id', |
|
328
|
|
|
) |
|
329
|
|
|
|
|
330
|
|
|
if not family: |
|
331
|
|
|
raise RequiredArgument( |
|
332
|
|
|
function=self.modify_policy_set_nvt_selection.__name__, |
|
333
|
|
|
argument='family argument', |
|
334
|
|
|
) |
|
335
|
|
|
|
|
336
|
|
|
if not is_list_like(nvt_oids): |
|
337
|
|
|
raise InvalidArgumentType( |
|
338
|
|
|
function=self.modify_policy_set_nvt_selection.__name__, |
|
339
|
|
|
argument='nvt_oids', |
|
340
|
|
|
arg_type='list', |
|
341
|
|
|
) |
|
342
|
|
|
|
|
343
|
|
|
cmd = XmlCommand("modify_config") |
|
344
|
|
|
cmd.set_attribute("config_id", str(policy_id)) |
|
345
|
|
|
|
|
346
|
|
|
_xmlnvtsel = cmd.add_element("nvt_selection") |
|
347
|
|
|
_xmlnvtsel.add_element("family", family) |
|
348
|
|
|
|
|
349
|
|
|
for nvt in nvt_oids: |
|
350
|
|
|
_xmlnvtsel.add_element("nvt", attrs={"oid": nvt}) |
|
351
|
|
|
|
|
352
|
|
|
return self._send_xml_command(cmd) |
|
353
|
|
|
|
|
354
|
|
View Code Duplication |
def modify_policy_set_family_selection( |
|
|
|
|
|
|
355
|
|
|
self, |
|
356
|
|
|
policy_id: str, |
|
357
|
|
|
families: List[Tuple[str, bool, bool]], |
|
358
|
|
|
*, |
|
359
|
|
|
auto_add_new_families: Optional[bool] = True, |
|
360
|
|
|
) -> Any: |
|
361
|
|
|
""" |
|
362
|
|
|
Selected the NVTs of a policy at a family level. |
|
363
|
|
|
|
|
364
|
|
|
Arguments: |
|
365
|
|
|
policy_id: UUID of policy to modify. |
|
366
|
|
|
families: A list of tuples with the first entry being the name |
|
367
|
|
|
of the NVT family selected, second entry a boolean indicating |
|
368
|
|
|
whether new NVTs should be added to the family automatically, |
|
369
|
|
|
and third entry a boolean indicating whether all nvts from |
|
370
|
|
|
the family should be included. |
|
371
|
|
|
auto_add_new_families: Whether new families should be added to the |
|
372
|
|
|
policy automatically. Default: True. |
|
373
|
|
|
""" |
|
374
|
|
|
if not policy_id: |
|
375
|
|
|
raise RequiredArgument( |
|
376
|
|
|
function=self.modify_policy_set_family_selection.__name__, |
|
377
|
|
|
argument='policy_id', |
|
378
|
|
|
) |
|
379
|
|
|
|
|
380
|
|
|
if not is_list_like(families): |
|
381
|
|
|
raise InvalidArgumentType( |
|
382
|
|
|
function=self.modify_policy_set_family_selection.__name__, |
|
383
|
|
|
argument='families', |
|
384
|
|
|
arg_type='list', |
|
385
|
|
|
) |
|
386
|
|
|
|
|
387
|
|
|
cmd = XmlCommand("modify_config") |
|
388
|
|
|
cmd.set_attribute("config_id", str(policy_id)) |
|
389
|
|
|
|
|
390
|
|
|
_xmlfamsel = cmd.add_element("family_selection") |
|
391
|
|
|
_xmlfamsel.add_element("growing", to_bool(auto_add_new_families)) |
|
392
|
|
|
|
|
393
|
|
|
for family in families: |
|
394
|
|
|
_xmlfamily = _xmlfamsel.add_element("family") |
|
395
|
|
|
_xmlfamily.add_element("name", family[0]) |
|
396
|
|
|
|
|
397
|
|
|
if len(family) != 3: |
|
398
|
|
|
raise InvalidArgument( |
|
399
|
|
|
"Family must be a tuple of 3. (str, bool, bool)" |
|
400
|
|
|
) |
|
401
|
|
|
|
|
402
|
|
|
if not isinstance(family[1], bool) or not isinstance( |
|
403
|
|
|
family[2], bool |
|
404
|
|
|
): |
|
405
|
|
|
raise InvalidArgumentType( |
|
406
|
|
|
function=(self.modify_policy_set_family_selection.__name__), |
|
407
|
|
|
argument='families', |
|
408
|
|
|
arg_type='[tuple(str, bool, bool)]', |
|
409
|
|
|
) |
|
410
|
|
|
|
|
411
|
|
|
_xmlfamily.add_element("all", to_bool(family[2])) |
|
412
|
|
|
_xmlfamily.add_element("growing", to_bool(family[1])) |
|
413
|
|
|
|
|
414
|
|
|
return self._send_xml_command(cmd) |
|
415
|
|
|
|