Total Complexity | 544 |
Total Lines | 4624 |
Duplicated Lines | 15.2 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like gvm.protocols.gmpv208.gmpv208 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
2 | # Copyright (C) 2018-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=arguments-differ, redefined-builtin, too-many-lines |
||
20 | |||
21 | """ |
||
22 | Module for communication with gvmd in |
||
23 | `Greenbone Management Protocol version 20.08`_ |
||
24 | |||
25 | .. _Greenbone Management Protocol version 20.08: |
||
26 | https://docs.greenbone.net/API/GMP/gmp-20.08.html |
||
27 | """ |
||
28 | import collections |
||
29 | import logging |
||
30 | from numbers import Integral |
||
31 | |||
32 | from typing import Any, List, Optional, Callable, Union, Tuple |
||
33 | from lxml import etree |
||
34 | |||
35 | from gvm.connections import GvmConnection |
||
36 | from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument |
||
37 | from gvm.protocols.base import GvmProtocol |
||
38 | from gvm.protocols.gmpv208.entities.report_formats import ( |
||
39 | ReportFormatType, |
||
40 | ) |
||
41 | from gvm.utils import ( |
||
42 | deprecation, |
||
43 | check_command_status, |
||
44 | is_list_like, |
||
45 | to_base64, |
||
46 | to_bool, |
||
47 | to_comma_list, |
||
48 | add_filter, |
||
49 | ) |
||
50 | from gvm.xml import XmlCommand |
||
51 | |||
52 | from . import types |
||
53 | from .types import * # pylint: disable=unused-wildcard-import, wildcard-import |
||
54 | from .types import _UsageType as UsageType |
||
55 | |||
56 | _EMPTY_POLICY_ID = '085569ce-73ed-11df-83c3-002264764cea' |
||
57 | |||
58 | PROTOCOL_VERSION = (20, 8) |
||
59 | |||
60 | |||
61 | logger = logging.getLogger(__name__) |
||
62 | |||
63 | |||
64 | class GmpV208Mixin(GvmProtocol): |
||
65 | """Python interface for Greenbone Management Protocol |
||
66 | |||
67 | This class implements the `Greenbone Management Protocol version 20.08`_ |
||
68 | |||
69 | Arguments: |
||
70 | connection: Connection to use to talk with the gvmd daemon. See |
||
71 | :mod:`gvm.connections` for possible connection types. |
||
72 | transform: Optional transform `callable`_ to convert response data. |
||
73 | After each request the callable gets passed the plain response data |
||
74 | which can be used to check the data and/or conversion into different |
||
75 | representations like a xml dom. |
||
76 | |||
77 | See :mod:`gvm.transforms` for existing transforms. |
||
78 | |||
79 | .. _Greenbone Management Protocol version 20.08: |
||
80 | https://docs.greenbone.net/API/GMP/gmp-20.08.html |
||
81 | .. _callable: |
||
82 | https://docs.python.org/3/library/functions.html#callable |
||
83 | """ |
||
84 | |||
85 | types = types |
||
|
|||
86 | |||
87 | def __init__( |
||
88 | self, |
||
89 | connection: GvmConnection, |
||
90 | *, |
||
91 | transform: Optional[Callable[[str], Any]] = None, |
||
92 | ): |
||
93 | super().__init__(connection, transform=transform) |
||
94 | |||
95 | # Is authenticated on gvmd |
||
96 | self._authenticated = False |
||
97 | |||
98 | def is_authenticated(self) -> bool: |
||
99 | """Checks if the user is authenticated |
||
100 | |||
101 | If the user is authenticated privileged GMP commands like get_tasks |
||
102 | may be send to gvmd. |
||
103 | |||
104 | Returns: |
||
105 | bool: True if an authenticated connection to gvmd has been |
||
106 | established. |
||
107 | """ |
||
108 | return self._authenticated |
||
109 | |||
110 | def authenticate(self, username: str, password: str) -> Any: |
||
111 | """Authenticate to gvmd. |
||
112 | |||
113 | The generated authenticate command will be send to server. |
||
114 | Afterwards the response is read, transformed and returned. |
||
115 | |||
116 | Arguments: |
||
117 | username: Username |
||
118 | password: Password |
||
119 | |||
120 | Returns: |
||
121 | Transformed response from server. |
||
122 | """ |
||
123 | cmd = XmlCommand("authenticate") |
||
124 | |||
125 | if not username: |
||
126 | raise RequiredArgument( |
||
127 | function=self.authenticate.__name__, argument='username' |
||
128 | ) |
||
129 | |||
130 | if not password: |
||
131 | raise RequiredArgument( |
||
132 | function=self.authenticate.__name__, argument='password' |
||
133 | ) |
||
134 | |||
135 | credentials = cmd.add_element("credentials") |
||
136 | credentials.add_element("username", username) |
||
137 | credentials.add_element("password", password) |
||
138 | |||
139 | self._send(cmd.to_string()) |
||
140 | response = self._read() |
||
141 | |||
142 | if check_command_status(response): |
||
143 | self._authenticated = True |
||
144 | |||
145 | return self._transform(response) |
||
146 | |||
147 | def create_audit( |
||
148 | self, |
||
149 | name: str, |
||
150 | policy_id: str, |
||
151 | target_id: str, |
||
152 | scanner_id: str, |
||
153 | *, |
||
154 | alterable: Optional[bool] = None, |
||
155 | hosts_ordering: Optional[HostsOrdering] = None, |
||
156 | schedule_id: Optional[str] = None, |
||
157 | alert_ids: Optional[List[str]] = None, |
||
158 | comment: Optional[str] = None, |
||
159 | schedule_periods: Optional[int] = None, |
||
160 | observers: Optional[List[str]] = None, |
||
161 | preferences: Optional[dict] = None, |
||
162 | ) -> Any: |
||
163 | """Create a new audit task |
||
164 | |||
165 | Arguments: |
||
166 | name: Name of the new audit |
||
167 | policy_id: UUID of policy to use by the audit |
||
168 | target_id: UUID of target to be scanned |
||
169 | scanner_id: UUID of scanner to use for scanning the target |
||
170 | comment: Comment for the audit |
||
171 | alterable: Whether the task should be alterable |
||
172 | alert_ids: List of UUIDs for alerts to be applied to the audit |
||
173 | hosts_ordering: The order hosts are scanned in |
||
174 | schedule_id: UUID of a schedule when the audit should be run. |
||
175 | schedule_periods: A limit to the number of times the audit will be |
||
176 | scheduled, or 0 for no limit |
||
177 | observers: List of names or ids of users which should be allowed to |
||
178 | observe this audit |
||
179 | preferences: Name/Value pairs of scanner preferences. |
||
180 | |||
181 | Returns: |
||
182 | The response. See :py:meth:`send_command` for details. |
||
183 | """ |
||
184 | |||
185 | return self.__create_task( |
||
186 | name=name, |
||
187 | config_id=policy_id, |
||
188 | target_id=target_id, |
||
189 | scanner_id=scanner_id, |
||
190 | usage_type=UsageType.AUDIT, |
||
191 | function=self.create_audit.__name__, |
||
192 | alterable=alterable, |
||
193 | hosts_ordering=hosts_ordering, |
||
194 | schedule_id=schedule_id, |
||
195 | alert_ids=alert_ids, |
||
196 | comment=comment, |
||
197 | schedule_periods=schedule_periods, |
||
198 | observers=observers, |
||
199 | preferences=preferences, |
||
200 | ) |
||
201 | |||
202 | def create_config( |
||
203 | self, config_id: str, name: str, *, comment: Optional[str] = None |
||
204 | ) -> Any: |
||
205 | """Create a new scan config |
||
206 | |||
207 | Arguments: |
||
208 | config_id: UUID of the existing scan config |
||
209 | name: Name of the new scan config |
||
210 | comment: A comment on the config |
||
211 | |||
212 | Returns: |
||
213 | The response. See :py:meth:`send_command` for details. |
||
214 | """ |
||
215 | return self.__create_config( |
||
216 | config_id=config_id, |
||
217 | name=name, |
||
218 | comment=comment, |
||
219 | usage_type=UsageType.SCAN, |
||
220 | function=self.create_config.__name__, |
||
221 | ) |
||
222 | |||
223 | def create_config_from_osp_scanner( |
||
224 | self, scanner_id: str, name: str, *, comment: Optional[str] = None |
||
225 | ) -> Any: |
||
226 | """Create a new scan config from an ospd scanner. |
||
227 | |||
228 | Create config by retrieving the expected preferences from the given |
||
229 | scanner via OSP. |
||
230 | |||
231 | Arguments: |
||
232 | scanner_id: UUID of an OSP scanner to get config data from |
||
233 | name: Name of the new scan config |
||
234 | comment: A comment on the config |
||
235 | |||
236 | Returns: |
||
237 | The response. See :py:meth:`send_command` for details. |
||
238 | """ |
||
239 | return self.__create_config_from_osp_scanner( |
||
240 | scanner_id=scanner_id, |
||
241 | name=name, |
||
242 | comment=comment, |
||
243 | usage_type=UsageType.SCAN, |
||
244 | function=self.create_config.__name__, |
||
245 | ) |
||
246 | |||
247 | def create_permission( |
||
248 | self, |
||
249 | name: str, |
||
250 | subject_id: str, |
||
251 | subject_type: PermissionSubjectType, |
||
252 | *, |
||
253 | resource_id: Optional[str] = None, |
||
254 | resource_type: Optional[EntityType] = None, |
||
255 | comment: Optional[str] = None, |
||
256 | ) -> Any: |
||
257 | """Create a new permission |
||
258 | |||
259 | Arguments: |
||
260 | name: Name of the new permission |
||
261 | subject_id: UUID of subject to whom the permission is granted |
||
262 | subject_type: Type of the subject user, group or role |
||
263 | comment: Comment for the permission |
||
264 | resource_id: UUID of entity to which the permission applies |
||
265 | resource_type: Type of the resource. For Super permissions user, |
||
266 | group or role |
||
267 | |||
268 | Returns: |
||
269 | The response. See :py:meth:`send_command` for details. |
||
270 | """ |
||
271 | if not name: |
||
272 | raise RequiredArgument( |
||
273 | function=self.create_permission.__name__, argument='name' |
||
274 | ) |
||
275 | |||
276 | if not subject_id: |
||
277 | raise RequiredArgument( |
||
278 | function=self.create_permission.__name__, argument='subject_id' |
||
279 | ) |
||
280 | |||
281 | if not isinstance(subject_type, PermissionSubjectType): |
||
282 | raise InvalidArgumentType( |
||
283 | function=self.create_permission.__name__, |
||
284 | argument='subject_type', |
||
285 | arg_type=PermissionSubjectType.__name__, |
||
286 | ) |
||
287 | |||
288 | cmd = XmlCommand("create_permission") |
||
289 | cmd.add_element("name", name) |
||
290 | |||
291 | _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
||
292 | _xmlsubject.add_element("type", subject_type.value) |
||
293 | |||
294 | if comment: |
||
295 | cmd.add_element("comment", comment) |
||
296 | |||
297 | View Code Duplication | if resource_id or resource_type: |
|
298 | if not resource_id: |
||
299 | raise RequiredArgument( |
||
300 | function=self.create_permission.__name__, |
||
301 | argument='resource_id', |
||
302 | ) |
||
303 | |||
304 | if not resource_type: |
||
305 | raise RequiredArgument( |
||
306 | function=self.create_permission.__name__, |
||
307 | argument='resource_type', |
||
308 | ) |
||
309 | |||
310 | if not isinstance(resource_type, self.types.EntityType): |
||
311 | raise InvalidArgumentType( |
||
312 | function=self.create_permission.__name__, |
||
313 | argument='resource_type', |
||
314 | arg_type=self.types.EntityType.__name__, |
||
315 | ) |
||
316 | |||
317 | _xmlresource = cmd.add_element( |
||
318 | "resource", attrs={"id": resource_id} |
||
319 | ) |
||
320 | |||
321 | _actual_resource_type = resource_type |
||
322 | if resource_type.value == EntityType.AUDIT.value: |
||
323 | _actual_resource_type = EntityType.TASK |
||
324 | elif resource_type.value == EntityType.POLICY.value: |
||
325 | _actual_resource_type = EntityType.SCAN_CONFIG |
||
326 | |||
327 | _xmlresource.add_element("type", _actual_resource_type.value) |
||
328 | |||
329 | return self._send_xml_command(cmd) |
||
330 | |||
331 | def create_policy( |
||
332 | self, name: str, *, policy_id: str = None, comment: Optional[str] = None |
||
333 | ) -> Any: |
||
334 | """Create a new policy config |
||
335 | |||
336 | Arguments: |
||
337 | name: Name of the new policy |
||
338 | policy_id: UUID of an existing policy as base. By default the empty |
||
339 | policy is used. |
||
340 | comment: A comment on the policy |
||
341 | |||
342 | Returns: |
||
343 | The response. See :py:meth:`send_command` for details. |
||
344 | """ |
||
345 | if policy_id is None: |
||
346 | policy_id = _EMPTY_POLICY_ID |
||
347 | return self.__create_config( |
||
348 | config_id=policy_id, |
||
349 | name=name, |
||
350 | comment=comment, |
||
351 | usage_type=UsageType.POLICY, |
||
352 | function=self.create_policy.__name__, |
||
353 | ) |
||
354 | |||
355 | def create_tag( |
||
356 | self, |
||
357 | name: str, |
||
358 | resource_type: EntityType, |
||
359 | *, |
||
360 | resource_filter: Optional[str] = None, |
||
361 | resource_ids: Optional[List[str]] = None, |
||
362 | value: Optional[str] = None, |
||
363 | comment: Optional[str] = None, |
||
364 | active: Optional[bool] = None, |
||
365 | ) -> Any: |
||
366 | """Create a tag. |
||
367 | |||
368 | Arguments: |
||
369 | name: Name of the tag. A full tag name consisting of namespace and |
||
370 | predicate e.g. `foo:bar`. |
||
371 | resource_type: Entity type the tag is to be attached to. |
||
372 | resource_filter: Filter term to select resources the tag is to be |
||
373 | attached to. Only one of resource_filter or resource_ids can be |
||
374 | provided. |
||
375 | resource_ids: IDs of the resources the tag is to be attached to. |
||
376 | Only one of resource_filter or resource_ids can be provided. |
||
377 | value: Value associated with the tag. |
||
378 | comment: Comment for the tag. |
||
379 | active: Whether the tag should be active. |
||
380 | |||
381 | Returns: |
||
382 | The response. See :py:meth:`send_command` for details. |
||
383 | """ |
||
384 | if not name: |
||
385 | raise RequiredArgument( |
||
386 | function=self.create_tag.__name__, argument='name' |
||
387 | ) |
||
388 | |||
389 | if resource_filter and resource_ids: |
||
390 | raise InvalidArgument( |
||
391 | "create_tag accepts either resource_filter or resource_ids " |
||
392 | "argument", |
||
393 | function=self.create_tag.__name__, |
||
394 | ) |
||
395 | |||
396 | if not resource_type: |
||
397 | raise RequiredArgument( |
||
398 | function=self.create_tag.__name__, argument='resource_type' |
||
399 | ) |
||
400 | |||
401 | if not isinstance(resource_type, self.types.EntityType): |
||
402 | raise InvalidArgumentType( |
||
403 | function=self.create_tag.__name__, |
||
404 | argument='resource_type', |
||
405 | arg_type=EntityType.__name__, |
||
406 | ) |
||
407 | |||
408 | cmd = XmlCommand('create_tag') |
||
409 | cmd.add_element('name', name) |
||
410 | |||
411 | _xmlresources = cmd.add_element("resources") |
||
412 | if resource_filter is not None: |
||
413 | _xmlresources.set_attribute("filter", resource_filter) |
||
414 | |||
415 | for resource_id in resource_ids or []: |
||
416 | _xmlresources.add_element( |
||
417 | "resource", attrs={"id": str(resource_id)} |
||
418 | ) |
||
419 | |||
420 | _actual_resource_type = resource_type |
||
421 | if resource_type.value == EntityType.AUDIT.value: |
||
422 | _actual_resource_type = EntityType.TASK |
||
423 | elif resource_type.value == EntityType.POLICY.value: |
||
424 | _actual_resource_type = EntityType.SCAN_CONFIG |
||
425 | _xmlresources.add_element("type", _actual_resource_type.value) |
||
426 | |||
427 | if comment: |
||
428 | cmd.add_element("comment", comment) |
||
429 | |||
430 | if value: |
||
431 | cmd.add_element("value", value) |
||
432 | |||
433 | if active is not None: |
||
434 | if active: |
||
435 | cmd.add_element("active", "1") |
||
436 | else: |
||
437 | cmd.add_element("active", "0") |
||
438 | |||
439 | return self._send_xml_command(cmd) |
||
440 | |||
441 | def get_aggregates( |
||
442 | self, |
||
443 | resource_type: EntityType, |
||
444 | *, |
||
445 | filter: Optional[str] = None, |
||
446 | filter_id: Optional[str] = None, |
||
447 | sort_criteria: Optional[list] = None, |
||
448 | data_columns: Optional[list] = None, |
||
449 | group_column: Optional[str] = None, |
||
450 | subgroup_column: Optional[str] = None, |
||
451 | text_columns: Optional[list] = None, |
||
452 | first_group: Optional[int] = None, |
||
453 | max_groups: Optional[int] = None, |
||
454 | mode: Optional[int] = None, |
||
455 | **kwargs, |
||
456 | ) -> Any: |
||
457 | """Request aggregated information on a resource / entity type |
||
458 | |||
459 | Additional arguments can be set via the kwargs parameter for backward |
||
460 | compatibility with older versions of python-gvm, but are not validated. |
||
461 | |||
462 | Arguments: |
||
463 | resource_type: The entity type to gather data from |
||
464 | filter: Filter term to use for the query |
||
465 | filter_id: UUID of an existing filter to use for the query |
||
466 | sort_criteria: List of sort criteria (dicts that can contain |
||
467 | a field, stat and order) |
||
468 | data_columns: List of fields to aggregate data from |
||
469 | group_column: The field to group the entities by |
||
470 | subgroup_column: The field to further group the entities |
||
471 | inside groups by |
||
472 | text_columns: List of simple text columns which no statistics |
||
473 | are calculated for |
||
474 | first_group: The index of the first aggregate group to return |
||
475 | max_groups: The maximum number of aggregate groups to return, |
||
476 | -1 for all |
||
477 | mode: Special mode for aggregation |
||
478 | |||
479 | Returns: |
||
480 | The response. See :py:meth:`send_command` for details. |
||
481 | """ |
||
482 | if not resource_type: |
||
483 | raise RequiredArgument( |
||
484 | function=self.get_aggregates.__name__, argument='resource_type' |
||
485 | ) |
||
486 | |||
487 | if not isinstance(resource_type, self.types.EntityType): |
||
488 | raise InvalidArgumentType( |
||
489 | function=self.get_aggregates.__name__, |
||
490 | argument='resource_type', |
||
491 | arg_type=self.types.EntityType.__name__, |
||
492 | ) |
||
493 | |||
494 | cmd = XmlCommand('get_aggregates') |
||
495 | |||
496 | _actual_resource_type = resource_type |
||
497 | if resource_type.value == EntityType.AUDIT.value: |
||
498 | _actual_resource_type = EntityType.TASK |
||
499 | cmd.set_attribute('usage_type', 'audit') |
||
500 | elif resource_type.value == EntityType.POLICY.value: |
||
501 | _actual_resource_type = EntityType.SCAN_CONFIG |
||
502 | cmd.set_attribute('usage_type', 'policy') |
||
503 | elif resource_type.value == EntityType.SCAN_CONFIG.value: |
||
504 | cmd.set_attribute('usage_type', 'scan') |
||
505 | elif resource_type.value == EntityType.TASK.value: |
||
506 | cmd.set_attribute('usage_type', 'scan') |
||
507 | cmd.set_attribute('type', _actual_resource_type.value) |
||
508 | |||
509 | add_filter(cmd, filter, filter_id) |
||
510 | |||
511 | if first_group is not None: |
||
512 | if not isinstance(first_group, int): |
||
513 | raise InvalidArgumentType( |
||
514 | function=self.get_aggregates.__name__, |
||
515 | argument='first_group', |
||
516 | arg_type=int.__name__, |
||
517 | ) |
||
518 | cmd.set_attribute('first_group', str(first_group)) |
||
519 | |||
520 | if max_groups is not None: |
||
521 | if not isinstance(max_groups, int): |
||
522 | raise InvalidArgumentType( |
||
523 | function=self.get_aggregates.__name__, |
||
524 | argument='max_groups', |
||
525 | arg_type=int.__name__, |
||
526 | ) |
||
527 | cmd.set_attribute('max_groups', str(max_groups)) |
||
528 | |||
529 | if sort_criteria is not None: |
||
530 | if not isinstance(sort_criteria, list): |
||
531 | raise InvalidArgumentType( |
||
532 | function=self.get_aggregates.__name__, |
||
533 | argument='sort_criteria', |
||
534 | arg_type=list.__name__, |
||
535 | ) |
||
536 | for sort in sort_criteria: |
||
537 | if not isinstance(sort, dict): |
||
538 | raise InvalidArgumentType( |
||
539 | function=self.get_aggregates.__name__, |
||
540 | argument='sort_criteria', |
||
541 | ) |
||
542 | |||
543 | sort_elem = cmd.add_element('sort') |
||
544 | if sort.get('field'): |
||
545 | sort_elem.set_attribute('field', sort.get('field')) |
||
546 | |||
547 | if sort.get('stat'): |
||
548 | if isinstance(sort['stat'], AggregateStatistic): |
||
549 | sort_elem.set_attribute('stat', sort['stat'].value) |
||
550 | else: |
||
551 | stat = get_aggregate_statistic_from_string(sort['stat']) |
||
552 | sort_elem.set_attribute('stat', stat.value) |
||
553 | |||
554 | if sort.get('order'): |
||
555 | if isinstance(sort['order'], SortOrder): |
||
556 | sort_elem.set_attribute('order', sort['order'].value) |
||
557 | else: |
||
558 | so = get_sort_order_from_string(sort['order']) |
||
559 | sort_elem.set_attribute('order', so.value) |
||
560 | |||
561 | if data_columns is not None: |
||
562 | if not isinstance(data_columns, list): |
||
563 | raise InvalidArgumentType( |
||
564 | function=self.get_aggregates.__name__, |
||
565 | argument='data_columns', |
||
566 | arg_type=list.__name__, |
||
567 | ) |
||
568 | for column in data_columns: |
||
569 | cmd.add_element('data_column', column) |
||
570 | |||
571 | if group_column is not None: |
||
572 | cmd.set_attribute('group_column', group_column) |
||
573 | |||
574 | if subgroup_column is not None: |
||
575 | if not group_column: |
||
576 | raise RequiredArgument( |
||
577 | '{} requires a group_column argument' |
||
578 | ' if subgroup_column is given'.format( |
||
579 | self.get_aggregates.__name__ |
||
580 | ), |
||
581 | function=self.get_aggregates.__name__, |
||
582 | argument='subgroup_column', |
||
583 | ) |
||
584 | cmd.set_attribute('subgroup_column', subgroup_column) |
||
585 | |||
586 | if text_columns is not None: |
||
587 | if not isinstance(text_columns, list): |
||
588 | raise InvalidArgumentType( |
||
589 | function=self.get_aggregates.__name__, |
||
590 | argument='text_columns', |
||
591 | arg_type=list.__name__, |
||
592 | ) |
||
593 | for column in text_columns: |
||
594 | cmd.add_element('text_column', column) |
||
595 | |||
596 | if mode is not None: |
||
597 | cmd.set_attribute('mode', mode) |
||
598 | |||
599 | # Add additional keyword args as attributes for backward compatibility. |
||
600 | cmd.set_attributes(kwargs) |
||
601 | |||
602 | return self._send_xml_command(cmd) |
||
603 | |||
604 | def modify_audit( |
||
605 | self, |
||
606 | audit_id: str, |
||
607 | *, |
||
608 | name: Optional[str] = None, |
||
609 | policy_id: Optional[str] = None, |
||
610 | target_id: Optional[str] = None, |
||
611 | scanner_id: Optional[str] = None, |
||
612 | alterable: Optional[bool] = None, |
||
613 | hosts_ordering: Optional[HostsOrdering] = None, |
||
614 | schedule_id: Optional[str] = None, |
||
615 | schedule_periods: Optional[int] = None, |
||
616 | comment: Optional[str] = None, |
||
617 | alert_ids: Optional[List[str]] = None, |
||
618 | observers: Optional[List[str]] = None, |
||
619 | preferences: Optional[dict] = None, |
||
620 | ) -> Any: |
||
621 | """Modifies an existing task. |
||
622 | |||
623 | Arguments: |
||
624 | audit_id: UUID of audit to modify. |
||
625 | name: The name of the audit. |
||
626 | policy_id: UUID of policy to use by the audit |
||
627 | target_id: UUID of target to be scanned |
||
628 | scanner_id: UUID of scanner to use for scanning the target |
||
629 | comment: The comment on the audit. |
||
630 | alert_ids: List of UUIDs for alerts to be applied to the audit |
||
631 | hosts_ordering: The order hosts are scanned in |
||
632 | schedule_id: UUID of a schedule when the audit should be run. |
||
633 | schedule_periods: A limit to the number of times the audit will be |
||
634 | scheduled, or 0 for no limit. |
||
635 | observers: List of names or ids of users which should be allowed to |
||
636 | observe this audit |
||
637 | preferences: Name/Value pairs of scanner preferences. |
||
638 | |||
639 | Returns: |
||
640 | The response. See :py:meth:`send_command` for details. |
||
641 | """ |
||
642 | self.modify_task( |
||
643 | task_id=audit_id, |
||
644 | name=name, |
||
645 | config_id=policy_id, |
||
646 | target_id=target_id, |
||
647 | scanner_id=scanner_id, |
||
648 | alterable=alterable, |
||
649 | hosts_ordering=hosts_ordering, |
||
650 | schedule_id=schedule_id, |
||
651 | schedule_periods=schedule_periods, |
||
652 | comment=comment, |
||
653 | alert_ids=alert_ids, |
||
654 | observers=observers, |
||
655 | preferences=preferences, |
||
656 | ) |
||
657 | |||
658 | def modify_permission( |
||
659 | self, |
||
660 | permission_id: str, |
||
661 | *, |
||
662 | comment: Optional[str] = None, |
||
663 | name: Optional[str] = None, |
||
664 | resource_id: Optional[str] = None, |
||
665 | resource_type: Optional[EntityType] = None, |
||
666 | subject_id: Optional[str] = None, |
||
667 | subject_type: Optional[PermissionSubjectType] = None, |
||
668 | ) -> Any: |
||
669 | """Modifies an existing permission. |
||
670 | |||
671 | Arguments: |
||
672 | permission_id: UUID of permission to be modified. |
||
673 | comment: The comment on the permission. |
||
674 | name: Permission name, currently the name of a command. |
||
675 | subject_id: UUID of subject to whom the permission is granted |
||
676 | subject_type: Type of the subject user, group or role |
||
677 | resource_id: UUID of entity to which the permission applies |
||
678 | resource_type: Type of the resource. For Super permissions user, |
||
679 | group or role |
||
680 | |||
681 | Returns: |
||
682 | The response. See :py:meth:`send_command` for details. |
||
683 | """ |
||
684 | if not permission_id: |
||
685 | raise RequiredArgument( |
||
686 | function=self.modify_permission.__name__, |
||
687 | argument='permission_id', |
||
688 | ) |
||
689 | |||
690 | cmd = XmlCommand("modify_permission") |
||
691 | cmd.set_attribute("permission_id", permission_id) |
||
692 | |||
693 | if comment: |
||
694 | cmd.add_element("comment", comment) |
||
695 | |||
696 | if name: |
||
697 | cmd.add_element("name", name) |
||
698 | |||
699 | View Code Duplication | if resource_id or resource_type: |
|
700 | if not resource_id: |
||
701 | raise RequiredArgument( |
||
702 | function=self.modify_permission.__name__, |
||
703 | argument='resource_id', |
||
704 | ) |
||
705 | |||
706 | if not resource_type: |
||
707 | raise RequiredArgument( |
||
708 | function=self.modify_permission.__name__, |
||
709 | argument='resource_type', |
||
710 | ) |
||
711 | |||
712 | if not isinstance(resource_type, self.types.EntityType): |
||
713 | raise InvalidArgumentType( |
||
714 | function=self.modify_permission.__name__, |
||
715 | argument='resource_type', |
||
716 | arg_type=self.types.EntityType.__name__, |
||
717 | ) |
||
718 | |||
719 | _xmlresource = cmd.add_element( |
||
720 | "resource", attrs={"id": resource_id} |
||
721 | ) |
||
722 | _actual_resource_type = resource_type |
||
723 | if resource_type.value == EntityType.AUDIT.value: |
||
724 | _actual_resource_type = EntityType.TASK |
||
725 | elif resource_type.value == EntityType.POLICY.value: |
||
726 | _actual_resource_type = EntityType.SCAN_CONFIG |
||
727 | _xmlresource.add_element("type", _actual_resource_type.value) |
||
728 | |||
729 | if subject_id or subject_type: |
||
730 | if not subject_id: |
||
731 | raise RequiredArgument( |
||
732 | function=self.modify_permission.__name__, |
||
733 | argument='subject_id', |
||
734 | ) |
||
735 | |||
736 | if not isinstance(subject_type, PermissionSubjectType): |
||
737 | raise InvalidArgumentType( |
||
738 | function=self.modify_permission.__name__, |
||
739 | argument='subject_type', |
||
740 | arg_type=PermissionSubjectType.__name__, |
||
741 | ) |
||
742 | |||
743 | _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
||
744 | _xmlsubject.add_element("type", subject_type.value) |
||
745 | |||
746 | return self._send_xml_command(cmd) |
||
747 | |||
748 | def modify_policy_set_nvt_preference( |
||
749 | self, |
||
750 | policy_id: str, |
||
751 | name: str, |
||
752 | nvt_oid: str, |
||
753 | *, |
||
754 | value: Optional[str] = None, |
||
755 | ) -> Any: |
||
756 | """Modifies the nvt preferences of an existing policy. |
||
757 | |||
758 | Arguments: |
||
759 | policy_id: UUID of policy to modify. |
||
760 | name: Name for preference to change. |
||
761 | nvt_oid: OID of the NVT associated with preference to modify |
||
762 | value: New value for the preference. None to delete the preference |
||
763 | and to use the default instead. |
||
764 | """ |
||
765 | self.modify_config_set_nvt_preference( |
||
766 | config_id=policy_id, name=name, nvt_oid=nvt_oid, value=value |
||
767 | ) |
||
768 | |||
769 | def modify_policy_set_name(self, policy_id: str, name: str) -> Any: |
||
770 | """Modifies the name of an existing policy |
||
771 | |||
772 | Arguments: |
||
773 | config_id: UUID of policy to modify. |
||
774 | name: New name for the config. |
||
775 | """ |
||
776 | self.modify_config_set_name(config_id=policy_id, name=name) |
||
777 | |||
778 | def modify_policy_set_comment( |
||
779 | self, policy_id: str, comment: Optional[str] = "" |
||
780 | ) -> Any: |
||
781 | """Modifies the comment of an existing policy |
||
782 | |||
783 | Arguments: |
||
784 | policy_id: UUID of policy to modify. |
||
785 | comment: Comment to set on a config. Default: '' |
||
786 | """ |
||
787 | self.modify_config_set_comment(config_id=policy_id, comment=comment) |
||
788 | |||
789 | def modify_policy_set_scanner_preference( |
||
790 | self, policy_id: str, name: str, *, value: Optional[str] = None |
||
791 | ) -> Any: |
||
792 | """Modifies the scanner preferences of an existing policy |
||
793 | |||
794 | Arguments: |
||
795 | policy_id: UUID of policy to modify. |
||
796 | name: Name of the scanner preference to change |
||
797 | value: New value for the preference. None to delete the preference |
||
798 | and to use the default instead. |
||
799 | |||
800 | """ |
||
801 | self.modify_config_set_scanner_preference( |
||
802 | config_id=policy_id, name=name, value=value |
||
803 | ) |
||
804 | |||
805 | def modify_policy_set_nvt_selection( |
||
806 | self, policy_id: str, family: str, nvt_oids: List[str] |
||
807 | ) -> Any: |
||
808 | """Modifies the selected nvts of an existing policy |
||
809 | |||
810 | The manager updates the given family in the config to include only the |
||
811 | given NVTs. |
||
812 | |||
813 | Arguments: |
||
814 | policy_id: UUID of policy to modify. |
||
815 | family: Name of the NVT family to include NVTs from |
||
816 | nvt_oids: List of NVTs to select for the family. |
||
817 | """ |
||
818 | self.modify_config_set_nvt_selection( |
||
819 | config_id=policy_id, family=family, nvt_oids=nvt_oids |
||
820 | ) |
||
821 | |||
822 | def modify_policy_set_family_selection( |
||
823 | self, |
||
824 | policy_id: str, |
||
825 | families: List[Tuple[str, bool, bool]], |
||
826 | *, |
||
827 | auto_add_new_families: Optional[bool] = True, |
||
828 | ) -> Any: |
||
829 | """ |
||
830 | Selected the NVTs of a policy at a family level. |
||
831 | |||
832 | Arguments: |
||
833 | policy_id: UUID of policy to modify. |
||
834 | families: A list of tuples with the first entry being the name |
||
835 | of the NVT family selected, second entry a boolean indicating |
||
836 | whether new NVTs should be added to the family automatically, |
||
837 | and third entry a boolean indicating whether all nvts from |
||
838 | the family should be included. |
||
839 | auto_add_new_families: Whether new families should be added to the |
||
840 | policy automatically. Default: True. |
||
841 | """ |
||
842 | self.modify_config_set_family_selection( |
||
843 | config_id=policy_id, |
||
844 | families=families, |
||
845 | auto_add_new_families=auto_add_new_families, |
||
846 | ) |
||
847 | |||
848 | def modify_tag( |
||
849 | self, |
||
850 | tag_id: str, |
||
851 | *, |
||
852 | comment: Optional[str] = None, |
||
853 | name: Optional[str] = None, |
||
854 | value=None, |
||
855 | active=None, |
||
856 | resource_action: Optional[str] = None, |
||
857 | resource_type: Optional[EntityType] = None, |
||
858 | resource_filter: Optional[str] = None, |
||
859 | resource_ids: Optional[List[str]] = None, |
||
860 | ) -> Any: |
||
861 | """Modifies an existing tag. |
||
862 | |||
863 | Arguments: |
||
864 | tag_id: UUID of the tag. |
||
865 | comment: Comment to add to the tag. |
||
866 | name: Name of the tag. |
||
867 | value: Value of the tag. |
||
868 | active: Whether the tag is active. |
||
869 | resource_action: Whether to add or remove resources instead of |
||
870 | overwriting. One of '', 'add', 'set' or 'remove'. |
||
871 | resource_type: Type of the resources to which to attach the tag. |
||
872 | Required if resource_filter is set. |
||
873 | resource_filter: Filter term to select resources the tag is to be |
||
874 | attached to. |
||
875 | resource_ids: IDs of the resources to which to attach the tag. |
||
876 | |||
877 | Returns: |
||
878 | The response. See :py:meth:`send_command` for details. |
||
879 | """ |
||
880 | if not tag_id: |
||
881 | raise RequiredArgument( |
||
882 | function=self.modify_tag.__name__, argument='tag_id' |
||
883 | ) |
||
884 | |||
885 | cmd = XmlCommand("modify_tag") |
||
886 | cmd.set_attribute("tag_id", str(tag_id)) |
||
887 | |||
888 | if comment: |
||
889 | cmd.add_element("comment", comment) |
||
890 | |||
891 | if name: |
||
892 | cmd.add_element("name", name) |
||
893 | |||
894 | if value: |
||
895 | cmd.add_element("value", value) |
||
896 | |||
897 | if active is not None: |
||
898 | cmd.add_element("active", to_bool(active)) |
||
899 | |||
900 | if resource_action or resource_filter or resource_ids or resource_type: |
||
901 | if resource_filter and not resource_type: |
||
902 | raise RequiredArgument( |
||
903 | function=self.modify_tag.__name__, argument='resource_type' |
||
904 | ) |
||
905 | |||
906 | _xmlresources = cmd.add_element("resources") |
||
907 | if resource_action is not None: |
||
908 | _xmlresources.set_attribute("action", resource_action) |
||
909 | |||
910 | if resource_filter is not None: |
||
911 | _xmlresources.set_attribute("filter", resource_filter) |
||
912 | |||
913 | for resource_id in resource_ids or []: |
||
914 | _xmlresources.add_element( |
||
915 | "resource", attrs={"id": str(resource_id)} |
||
916 | ) |
||
917 | |||
918 | if resource_type is not None: |
||
919 | if not isinstance(resource_type, self.types.EntityType): |
||
920 | raise InvalidArgumentType( |
||
921 | function=self.modify_tag.__name__, |
||
922 | argument="resource_type", |
||
923 | arg_type=EntityType.__name__, |
||
924 | ) |
||
925 | _actual_resource_type = resource_type |
||
926 | if resource_type.value == EntityType.AUDIT.value: |
||
927 | _actual_resource_type = EntityType.TASK |
||
928 | elif resource_type.value == EntityType.POLICY.value: |
||
929 | _actual_resource_type = EntityType.SCAN_CONFIG |
||
930 | _xmlresources.add_element("type", _actual_resource_type.value) |
||
931 | |||
932 | return self._send_xml_command(cmd) |
||
933 | |||
934 | def clone_ticket(self, ticket_id: str) -> Any: |
||
935 | """Clone an existing ticket |
||
936 | |||
937 | Arguments: |
||
938 | ticket_id: UUID of an existing ticket to clone from |
||
939 | |||
940 | Returns: |
||
941 | The response. See :py:meth:`send_command` for details. |
||
942 | """ |
||
943 | if not ticket_id: |
||
944 | raise RequiredArgument( |
||
945 | function=self.clone_ticket.__name__, argument='ticket_id' |
||
946 | ) |
||
947 | |||
948 | cmd = XmlCommand("create_ticket") |
||
949 | |||
950 | _copy = cmd.add_element("copy", ticket_id) |
||
951 | |||
952 | return self._send_xml_command(cmd) |
||
953 | |||
954 | def get_configs( |
||
955 | self, |
||
956 | *, |
||
957 | filter: Optional[str] = None, |
||
958 | filter_id: Optional[str] = None, |
||
959 | trash: Optional[bool] = None, |
||
960 | details: Optional[bool] = None, |
||
961 | families: Optional[bool] = None, |
||
962 | preferences: Optional[bool] = None, |
||
963 | tasks: Optional[bool] = None, |
||
964 | ) -> Any: |
||
965 | """Request a list of scan configs |
||
966 | |||
967 | Arguments: |
||
968 | filter: Filter term to use for the query |
||
969 | filter_id: UUID of an existing filter to use for the query |
||
970 | trash: Whether to get the trashcan scan configs instead |
||
971 | details: Whether to get config families, preferences, nvt selectors |
||
972 | and tasks. |
||
973 | families: Whether to include the families if no details are |
||
974 | requested |
||
975 | preferences: Whether to include the preferences if no details are |
||
976 | requested |
||
977 | tasks: Whether to get tasks using this config |
||
978 | |||
979 | Returns: |
||
980 | The response. See :py:meth:`send_command` for details. |
||
981 | """ |
||
982 | return self.__get_configs( |
||
983 | UsageType.SCAN, |
||
984 | filter=filter, |
||
985 | filter_id=filter_id, |
||
986 | trash=trash, |
||
987 | details=details, |
||
988 | families=families, |
||
989 | preferences=preferences, |
||
990 | tasks=tasks, |
||
991 | ) |
||
992 | |||
993 | def get_policies( |
||
994 | self, |
||
995 | *, |
||
996 | audits: Optional[bool] = None, |
||
997 | filter: Optional[str] = None, |
||
998 | filter_id: Optional[str] = None, |
||
999 | details: Optional[bool] = None, |
||
1000 | families: Optional[bool] = None, |
||
1001 | preferences: Optional[bool] = None, |
||
1002 | trash: Optional[bool] = None, |
||
1003 | ) -> Any: |
||
1004 | """Request a list of policies |
||
1005 | |||
1006 | Arguments: |
||
1007 | audits: Whether to get audits using the policy |
||
1008 | filter: Filter term to use for the query |
||
1009 | filter_id: UUID of an existing filter to use for the query |
||
1010 | details: Whether to get families, preferences, nvt selectors |
||
1011 | and tasks. |
||
1012 | families: Whether to include the families if no details are |
||
1013 | requested |
||
1014 | preferences: Whether to include the preferences if no details are |
||
1015 | requested |
||
1016 | trash: Whether to get the trashcan audits instead |
||
1017 | |||
1018 | Returns: |
||
1019 | The response. See :py:meth:`send_command` for details. |
||
1020 | """ |
||
1021 | return self.__get_configs( |
||
1022 | UsageType.POLICY, |
||
1023 | filter=filter, |
||
1024 | filter_id=filter_id, |
||
1025 | details=details, |
||
1026 | families=families, |
||
1027 | preferences=preferences, |
||
1028 | tasks=audits, |
||
1029 | trash=trash, |
||
1030 | ) |
||
1031 | |||
1032 | def get_config( |
||
1033 | self, config_id: str, *, tasks: Optional[bool] = None |
||
1034 | ) -> Any: |
||
1035 | """Request a single scan config |
||
1036 | |||
1037 | Arguments: |
||
1038 | config_id: UUID of an existing scan config |
||
1039 | tasks: Whether to get tasks using this config |
||
1040 | |||
1041 | Returns: |
||
1042 | The response. See :py:meth:`send_command` for details. |
||
1043 | """ |
||
1044 | return self.__get_config( |
||
1045 | config_id=config_id, usage_type=UsageType.SCAN, tasks=tasks |
||
1046 | ) |
||
1047 | |||
1048 | def get_policy( |
||
1049 | self, policy_id: str, *, audits: Optional[bool] = None |
||
1050 | ) -> Any: |
||
1051 | """Request a single policy |
||
1052 | |||
1053 | Arguments: |
||
1054 | policy_id: UUID of an existing policy |
||
1055 | audits: Whether to get audits using this config |
||
1056 | |||
1057 | Returns: |
||
1058 | The response. See :py:meth:`send_command` for details. |
||
1059 | """ |
||
1060 | return self.__get_config(policy_id, UsageType.POLICY, tasks=audits) |
||
1061 | |||
1062 | def get_tasks( |
||
1063 | self, |
||
1064 | *, |
||
1065 | filter: Optional[str] = None, |
||
1066 | filter_id: Optional[str] = None, |
||
1067 | trash: Optional[bool] = None, |
||
1068 | details: Optional[bool] = None, |
||
1069 | schedules_only: Optional[bool] = None, |
||
1070 | ) -> Any: |
||
1071 | """Request a list of tasks |
||
1072 | |||
1073 | Arguments: |
||
1074 | filter: Filter term to use for the query |
||
1075 | filter_id: UUID of an existing filter to use for the query |
||
1076 | trash: Whether to get the trashcan tasks instead |
||
1077 | details: Whether to include full task details |
||
1078 | schedules_only: Whether to only include id, name and schedule |
||
1079 | details |
||
1080 | |||
1081 | Returns: |
||
1082 | The response. See :py:meth:`send_command` for details. |
||
1083 | """ |
||
1084 | return self.__get_tasks( |
||
1085 | UsageType.SCAN, |
||
1086 | filter=filter, |
||
1087 | filter_id=filter_id, |
||
1088 | trash=trash, |
||
1089 | details=details, |
||
1090 | schedules_only=schedules_only, |
||
1091 | ) |
||
1092 | |||
1093 | def get_audits( |
||
1094 | self, |
||
1095 | *, |
||
1096 | filter: Optional[str] = None, |
||
1097 | filter_id: Optional[str] = None, |
||
1098 | trash: Optional[bool] = None, |
||
1099 | details: Optional[bool] = None, |
||
1100 | schedules_only: Optional[bool] = None, |
||
1101 | ) -> Any: |
||
1102 | """Request a list of audits |
||
1103 | |||
1104 | Arguments: |
||
1105 | filter: Filter term to use for the query |
||
1106 | filter_id: UUID of an existing filter to use for the query |
||
1107 | trash: Whether to get the trashcan audits instead |
||
1108 | details: Whether to include full audit details |
||
1109 | schedules_only: Whether to only include id, name and schedule |
||
1110 | details |
||
1111 | |||
1112 | Returns: |
||
1113 | The response. See :py:meth:`send_command` for details. |
||
1114 | """ |
||
1115 | return self.__get_tasks( |
||
1116 | UsageType.AUDIT, |
||
1117 | filter=filter, |
||
1118 | filter_id=filter_id, |
||
1119 | trash=trash, |
||
1120 | details=details, |
||
1121 | schedules_only=schedules_only, |
||
1122 | ) |
||
1123 | |||
1124 | def get_task(self, task_id: str) -> Any: |
||
1125 | """Request a single task |
||
1126 | |||
1127 | Arguments: |
||
1128 | task_id: UUID of an existing task |
||
1129 | |||
1130 | Returns: |
||
1131 | The response. See :py:meth:`send_command` for details. |
||
1132 | """ |
||
1133 | return self.__get_task(task_id, UsageType.SCAN) |
||
1134 | |||
1135 | def get_audit(self, audit_id: str) -> Any: |
||
1136 | """Request a single audit |
||
1137 | |||
1138 | Arguments: |
||
1139 | audit_id: UUID of an existing audit |
||
1140 | |||
1141 | Returns: |
||
1142 | The response. See :py:meth:`send_command` for details. |
||
1143 | """ |
||
1144 | return self.__get_task(audit_id, UsageType.AUDIT) |
||
1145 | |||
1146 | def clone_audit(self, audit_id: str) -> Any: |
||
1147 | """Clone an existing audit |
||
1148 | |||
1149 | Arguments: |
||
1150 | audit_id: UUID of existing audit to clone from |
||
1151 | |||
1152 | Returns: |
||
1153 | The response. See :py:meth:`send_command` for details. |
||
1154 | """ |
||
1155 | if not audit_id: |
||
1156 | raise RequiredArgument( |
||
1157 | function=self.clone_audit.__name__, argument='audit_id' |
||
1158 | ) |
||
1159 | |||
1160 | cmd = XmlCommand("create_task") |
||
1161 | cmd.add_element("copy", audit_id) |
||
1162 | return self._send_xml_command(cmd) |
||
1163 | |||
1164 | def clone_policy(self, policy_id: str) -> Any: |
||
1165 | """Clone a policy from an existing one |
||
1166 | |||
1167 | Arguments: |
||
1168 | policy_id: UUID of the existing policy |
||
1169 | |||
1170 | Returns: |
||
1171 | The response. See :py:meth:`send_command` for details. |
||
1172 | """ |
||
1173 | if not policy_id: |
||
1174 | raise RequiredArgument( |
||
1175 | function=self.clone_policy.__name__, argument='policy_id' |
||
1176 | ) |
||
1177 | |||
1178 | cmd = XmlCommand("create_config") |
||
1179 | cmd.add_element("copy", policy_id) |
||
1180 | return self._send_xml_command(cmd) |
||
1181 | |||
1182 | def delete_audit( |
||
1183 | self, audit_id: str, *, ultimate: Optional[bool] = False |
||
1184 | ) -> Any: |
||
1185 | """Deletes an existing audit |
||
1186 | |||
1187 | Arguments: |
||
1188 | audit_id: UUID of the audit to be deleted. |
||
1189 | ultimate: Whether to remove entirely, or to the trashcan. |
||
1190 | """ |
||
1191 | if not audit_id: |
||
1192 | raise RequiredArgument( |
||
1193 | function=self.delete_audit.__name__, argument='audit_id' |
||
1194 | ) |
||
1195 | |||
1196 | cmd = XmlCommand("delete_task") |
||
1197 | cmd.set_attribute("task_id", audit_id) |
||
1198 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
1199 | |||
1200 | return self._send_xml_command(cmd) |
||
1201 | |||
1202 | def delete_policy( |
||
1203 | self, policy_id: str, *, ultimate: Optional[bool] = False |
||
1204 | ) -> Any: |
||
1205 | """Deletes an existing policy |
||
1206 | |||
1207 | Arguments: |
||
1208 | policy_id: UUID of the policy to be deleted. |
||
1209 | ultimate: Whether to remove entirely, or to the trashcan. |
||
1210 | """ |
||
1211 | if not policy_id: |
||
1212 | raise RequiredArgument( |
||
1213 | function=self.delete_policy.__name__, argument='policy_id' |
||
1214 | ) |
||
1215 | |||
1216 | cmd = XmlCommand("delete_config") |
||
1217 | cmd.set_attribute("config_id", policy_id) |
||
1218 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
1219 | |||
1220 | return self._send_xml_command(cmd) |
||
1221 | |||
1222 | def __create_task( |
||
1223 | self, |
||
1224 | name: str, |
||
1225 | config_id: str, |
||
1226 | target_id: str, |
||
1227 | scanner_id: str, |
||
1228 | usage_type: UsageType, |
||
1229 | function: str, |
||
1230 | *, |
||
1231 | alterable: Optional[bool] = None, |
||
1232 | hosts_ordering: Optional[HostsOrdering] = None, |
||
1233 | schedule_id: Optional[str] = None, |
||
1234 | alert_ids: Optional[List[str]] = None, |
||
1235 | comment: Optional[str] = None, |
||
1236 | schedule_periods: Optional[int] = None, |
||
1237 | observers: Optional[List[str]] = None, |
||
1238 | preferences: Optional[dict] = None, |
||
1239 | ) -> Any: |
||
1240 | if not name: |
||
1241 | raise RequiredArgument(function=function, argument='name') |
||
1242 | |||
1243 | if not config_id: |
||
1244 | raise RequiredArgument(function=function, argument='config_id') |
||
1245 | |||
1246 | if not target_id: |
||
1247 | raise RequiredArgument(function=function, argument='target_id') |
||
1248 | |||
1249 | if not scanner_id: |
||
1250 | raise RequiredArgument(function=function, argument='scanner_id') |
||
1251 | |||
1252 | # don't allow to create a container task with create_task |
||
1253 | if target_id == '0': |
||
1254 | raise InvalidArgument(function=function, argument='target_id') |
||
1255 | |||
1256 | cmd = XmlCommand("create_task") |
||
1257 | cmd.add_element("name", name) |
||
1258 | cmd.add_element("usage_type", usage_type.value) |
||
1259 | cmd.add_element("config", attrs={"id": config_id}) |
||
1260 | cmd.add_element("target", attrs={"id": target_id}) |
||
1261 | cmd.add_element("scanner", attrs={"id": scanner_id}) |
||
1262 | |||
1263 | if comment: |
||
1264 | cmd.add_element("comment", comment) |
||
1265 | |||
1266 | if alterable is not None: |
||
1267 | cmd.add_element("alterable", to_bool(alterable)) |
||
1268 | |||
1269 | if hosts_ordering: |
||
1270 | if not isinstance(hosts_ordering, self.types.HostsOrdering): |
||
1271 | raise InvalidArgumentType( |
||
1272 | function=function, |
||
1273 | argument='hosts_ordering', |
||
1274 | arg_type=HostsOrdering.__name__, |
||
1275 | ) |
||
1276 | cmd.add_element("hosts_ordering", hosts_ordering.value) |
||
1277 | |||
1278 | if alert_ids: |
||
1279 | if isinstance(alert_ids, str): |
||
1280 | deprecation( |
||
1281 | "Please pass a list as alert_ids parameter to {}. " |
||
1282 | "Passing a string is deprecated and will be removed in " |
||
1283 | "future.".format(function) |
||
1284 | ) |
||
1285 | |||
1286 | # if a single id is given as a string wrap it into a list |
||
1287 | alert_ids = [alert_ids] |
||
1288 | if is_list_like(alert_ids): |
||
1289 | # parse all given alert id's |
||
1290 | for alert in alert_ids: |
||
1291 | cmd.add_element("alert", attrs={"id": str(alert)}) |
||
1292 | |||
1293 | View Code Duplication | if schedule_id: |
|
1294 | cmd.add_element("schedule", attrs={"id": schedule_id}) |
||
1295 | |||
1296 | if schedule_periods is not None: |
||
1297 | if ( |
||
1298 | not isinstance(schedule_periods, Integral) |
||
1299 | or schedule_periods < 0 |
||
1300 | ): |
||
1301 | raise InvalidArgument( |
||
1302 | "schedule_periods must be an integer greater or equal " |
||
1303 | "than 0" |
||
1304 | ) |
||
1305 | cmd.add_element("schedule_periods", str(schedule_periods)) |
||
1306 | |||
1307 | if observers is not None: |
||
1308 | if not is_list_like(observers): |
||
1309 | raise InvalidArgumentType( |
||
1310 | function=function, argument='observers', arg_type='list' |
||
1311 | ) |
||
1312 | |||
1313 | # gvmd splits by comma and space |
||
1314 | # gvmd tries to lookup each value as user name and afterwards as |
||
1315 | # user id. So both user name and user id are possible |
||
1316 | cmd.add_element("observers", to_comma_list(observers)) |
||
1317 | |||
1318 | View Code Duplication | if preferences is not None: |
|
1319 | if not isinstance(preferences, collections.abc.Mapping): |
||
1320 | raise InvalidArgumentType( |
||
1321 | function=function, |
||
1322 | argument='preferences', |
||
1323 | arg_type=collections.abc.Mapping.__name__, |
||
1324 | ) |
||
1325 | |||
1326 | _xmlprefs = cmd.add_element("preferences") |
||
1327 | for pref_name, pref_value in preferences.items(): |
||
1328 | _xmlpref = _xmlprefs.add_element("preference") |
||
1329 | _xmlpref.add_element("scanner_name", pref_name) |
||
1330 | _xmlpref.add_element("value", str(pref_value)) |
||
1331 | |||
1332 | return self._send_xml_command(cmd) |
||
1333 | |||
1334 | def __create_config( |
||
1335 | self, |
||
1336 | config_id: str, |
||
1337 | name: str, |
||
1338 | usage_type: UsageType, |
||
1339 | function: str, |
||
1340 | *, |
||
1341 | comment: Optional[str] = None, |
||
1342 | ) -> Any: |
||
1343 | if not name: |
||
1344 | raise RequiredArgument(function=function, argument='name') |
||
1345 | |||
1346 | if not config_id: |
||
1347 | raise RequiredArgument(function=function, argument='config_id') |
||
1348 | |||
1349 | cmd = XmlCommand("create_config") |
||
1350 | if comment is not None: |
||
1351 | cmd.add_element("comment", comment) |
||
1352 | cmd.add_element("copy", config_id) |
||
1353 | cmd.add_element("name", name) |
||
1354 | cmd.add_element("usage_type", usage_type.value) |
||
1355 | return self._send_xml_command(cmd) |
||
1356 | |||
1357 | def __create_config_from_osp_scanner( |
||
1358 | self, |
||
1359 | scanner_id: str, |
||
1360 | name: str, |
||
1361 | usage_type: UsageType, |
||
1362 | function: str, |
||
1363 | *, |
||
1364 | comment: Optional[str] = None, |
||
1365 | ) -> Any: |
||
1366 | if not name: |
||
1367 | raise RequiredArgument(function=function, argument='name') |
||
1368 | |||
1369 | if not scanner_id: |
||
1370 | raise RequiredArgument(function=function, argument='scanner_id') |
||
1371 | |||
1372 | cmd = XmlCommand("create_config") |
||
1373 | if comment is not None: |
||
1374 | cmd.add_element("comment", comment) |
||
1375 | cmd.add_element("scanner", scanner_id) |
||
1376 | cmd.add_element("name", name) |
||
1377 | cmd.add_element("usage_type", usage_type.value) |
||
1378 | return self._send_xml_command(cmd) |
||
1379 | |||
1380 | def __get_configs( |
||
1381 | self, |
||
1382 | usage_type: UsageType, |
||
1383 | *, |
||
1384 | filter: Optional[str] = None, |
||
1385 | filter_id: Optional[str] = None, |
||
1386 | trash: Optional[bool] = None, |
||
1387 | details: Optional[bool] = None, |
||
1388 | families: Optional[bool] = None, |
||
1389 | preferences: Optional[bool] = None, |
||
1390 | tasks: Optional[bool] = None, |
||
1391 | ) -> Any: |
||
1392 | cmd = XmlCommand("get_configs") |
||
1393 | cmd.set_attribute("usage_type", usage_type.value) |
||
1394 | |||
1395 | add_filter(cmd, filter, filter_id) |
||
1396 | |||
1397 | if trash is not None: |
||
1398 | cmd.set_attribute("trash", to_bool(trash)) |
||
1399 | |||
1400 | if details is not None: |
||
1401 | cmd.set_attribute("details", to_bool(details)) |
||
1402 | |||
1403 | if families is not None: |
||
1404 | cmd.set_attribute("families", to_bool(families)) |
||
1405 | |||
1406 | if preferences is not None: |
||
1407 | cmd.set_attribute("preferences", to_bool(preferences)) |
||
1408 | |||
1409 | if tasks is not None: |
||
1410 | cmd.set_attribute("tasks", to_bool(tasks)) |
||
1411 | |||
1412 | return self._send_xml_command(cmd) |
||
1413 | |||
1414 | def __get_config( |
||
1415 | self, |
||
1416 | config_id: str, |
||
1417 | usage_type: UsageType, |
||
1418 | *, |
||
1419 | tasks: Optional[bool] = None, |
||
1420 | ) -> Any: |
||
1421 | if not config_id: |
||
1422 | raise RequiredArgument( |
||
1423 | function=self.get_config.__name__, argument='config_id' |
||
1424 | ) |
||
1425 | |||
1426 | cmd = XmlCommand("get_configs") |
||
1427 | cmd.set_attribute("config_id", config_id) |
||
1428 | |||
1429 | cmd.set_attribute("usage_type", usage_type.value) |
||
1430 | |||
1431 | if tasks is not None: |
||
1432 | cmd.set_attribute("tasks", to_bool(tasks)) |
||
1433 | |||
1434 | # for single entity always request all details |
||
1435 | cmd.set_attribute("details", "1") |
||
1436 | |||
1437 | return self._send_xml_command(cmd) |
||
1438 | |||
1439 | def __get_tasks( |
||
1440 | self, |
||
1441 | usage_type: UsageType, |
||
1442 | *, |
||
1443 | filter: Optional[str] = None, |
||
1444 | filter_id: Optional[str] = None, |
||
1445 | trash: Optional[bool] = None, |
||
1446 | details: Optional[bool] = None, |
||
1447 | schedules_only: Optional[bool] = None, |
||
1448 | ) -> Any: |
||
1449 | cmd = XmlCommand("get_tasks") |
||
1450 | cmd.set_attribute("usage_type", usage_type.value) |
||
1451 | |||
1452 | add_filter(cmd, filter, filter_id) |
||
1453 | |||
1454 | if trash is not None: |
||
1455 | cmd.set_attribute("trash", to_bool(trash)) |
||
1456 | |||
1457 | if details is not None: |
||
1458 | cmd.set_attribute("details", to_bool(details)) |
||
1459 | |||
1460 | if schedules_only is not None: |
||
1461 | cmd.set_attribute("schedules_only", to_bool(schedules_only)) |
||
1462 | |||
1463 | return self._send_xml_command(cmd) |
||
1464 | |||
1465 | def __get_task(self, task_id: str, usage_type: UsageType) -> Any: |
||
1466 | if not task_id: |
||
1467 | raise RequiredArgument( |
||
1468 | function=self.get_task.__name__, argument='task_id' |
||
1469 | ) |
||
1470 | |||
1471 | cmd = XmlCommand("get_tasks") |
||
1472 | cmd.set_attribute("task_id", task_id) |
||
1473 | cmd.set_attribute("usage_type", usage_type.value) |
||
1474 | |||
1475 | # for single entity always request all details |
||
1476 | cmd.set_attribute("details", "1") |
||
1477 | return self._send_xml_command(cmd) |
||
1478 | |||
1479 | def resume_audit(self, audit_id: str) -> Any: |
||
1480 | """Resume an existing stopped audit |
||
1481 | |||
1482 | Arguments: |
||
1483 | audit_id: UUID of the audit to be resumed |
||
1484 | |||
1485 | Returns: |
||
1486 | The response. See :py:meth:`send_command` for details. |
||
1487 | """ |
||
1488 | if not audit_id: |
||
1489 | raise RequiredArgument( |
||
1490 | function=self.resume_audit.__name__, argument='audit_id' |
||
1491 | ) |
||
1492 | |||
1493 | cmd = XmlCommand("resume_task") |
||
1494 | cmd.set_attribute("task_id", audit_id) |
||
1495 | |||
1496 | return self._send_xml_command(cmd) |
||
1497 | |||
1498 | def start_audit(self, audit_id: str) -> Any: |
||
1499 | """Start an existing audit |
||
1500 | |||
1501 | Arguments: |
||
1502 | audit_id: UUID of the audit to be started |
||
1503 | |||
1504 | Returns: |
||
1505 | The response. See :py:meth:`send_command` for details. |
||
1506 | """ |
||
1507 | if not audit_id: |
||
1508 | raise RequiredArgument( |
||
1509 | function=self.start_audit.__name__, argument='audit_id' |
||
1510 | ) |
||
1511 | |||
1512 | cmd = XmlCommand("start_task") |
||
1513 | cmd.set_attribute("task_id", audit_id) |
||
1514 | |||
1515 | return self._send_xml_command(cmd) |
||
1516 | |||
1517 | def stop_audit(self, audit_id: str) -> Any: |
||
1518 | """Stop an existing running audit |
||
1519 | |||
1520 | Arguments: |
||
1521 | audit_id: UUID of the audit to be stopped |
||
1522 | |||
1523 | Returns: |
||
1524 | The response. See :py:meth:`send_command` for details. |
||
1525 | """ |
||
1526 | if not audit_id: |
||
1527 | raise RequiredArgument( |
||
1528 | function=self.stop_audit.__name__, argument='audit_id' |
||
1529 | ) |
||
1530 | |||
1531 | cmd = XmlCommand("stop_task") |
||
1532 | cmd.set_attribute("task_id", audit_id) |
||
1533 | |||
1534 | return self._send_xml_command(cmd) |
||
1535 | |||
1536 | def get_feed(self, feed_type: Optional[FeedType]) -> Any: |
||
1537 | """Request a single feed |
||
1538 | |||
1539 | Arguments: |
||
1540 | feed_type: Type of single feed to get: NVT, CERT or SCAP |
||
1541 | |||
1542 | Returns: |
||
1543 | The response. See :py:meth:`send_command` for details. |
||
1544 | """ |
||
1545 | if not feed_type: |
||
1546 | raise RequiredArgument( |
||
1547 | function=self.get_feed.__name__, argument='feed_type' |
||
1548 | ) |
||
1549 | |||
1550 | if not isinstance(feed_type, FeedType): |
||
1551 | raise InvalidArgumentType( |
||
1552 | function=self.get_feed.__name__, |
||
1553 | argument='feed_type', |
||
1554 | arg_type=FeedType.__name__, |
||
1555 | ) |
||
1556 | |||
1557 | cmd = XmlCommand("get_feeds") |
||
1558 | cmd.set_attribute("type", feed_type.value) |
||
1559 | |||
1560 | return self._send_xml_command(cmd) |
||
1561 | |||
1562 | def create_credential( |
||
1563 | self, |
||
1564 | name: str, |
||
1565 | credential_type: CredentialType, |
||
1566 | *, |
||
1567 | comment: Optional[str] = None, |
||
1568 | allow_insecure: Optional[bool] = None, |
||
1569 | certificate: Optional[str] = None, |
||
1570 | key_phrase: Optional[str] = None, |
||
1571 | private_key: Optional[str] = None, |
||
1572 | login: Optional[str] = None, |
||
1573 | password: Optional[str] = None, |
||
1574 | auth_algorithm: Optional[SnmpAuthAlgorithm] = None, |
||
1575 | community: Optional[str] = None, |
||
1576 | privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None, |
||
1577 | privacy_password: Optional[str] = None, |
||
1578 | public_key: Optional[str] = None, |
||
1579 | ) -> Any: |
||
1580 | """Create a new credential |
||
1581 | |||
1582 | Create a new credential e.g. to be used in the method of an alert. |
||
1583 | |||
1584 | Currently the following credential types are supported: |
||
1585 | |||
1586 | - Username + Password |
||
1587 | - Username + SSH-Key |
||
1588 | - Client Certificates |
||
1589 | - SNMPv1 or SNMPv2c protocol |
||
1590 | - S/MIME Certificate |
||
1591 | - OpenPGP Key |
||
1592 | - Password only |
||
1593 | |||
1594 | Arguments: |
||
1595 | name: Name of the new credential |
||
1596 | credential_type: The credential type. |
||
1597 | comment: Comment for the credential |
||
1598 | allow_insecure: Whether to allow insecure use of the credential |
||
1599 | certificate: Certificate for the credential. |
||
1600 | Required for client-certificate and smime credential types. |
||
1601 | key_phrase: Key passphrase for the private key. |
||
1602 | Used for the username+ssh-key credential type. |
||
1603 | private_key: Private key to use for login. Required |
||
1604 | for usk credential type. Also used for the cc credential type. |
||
1605 | The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM, |
||
1606 | PKC#12, OpenSSL, ...) depend on your installed GnuTLS version. |
||
1607 | login: Username for the credential. Required for username+password, |
||
1608 | username+ssh-key and snmp credential type. |
||
1609 | password: Password for the credential. Used for username+password |
||
1610 | and snmp credential types. |
||
1611 | community: The SNMP community |
||
1612 | auth_algorithm: The SNMP authentication algorithm. Required for snmp |
||
1613 | credential type. |
||
1614 | privacy_algorithm: The SNMP privacy algorithm |
||
1615 | privacy_password: The SNMP privacy password |
||
1616 | public_key: PGP public key in *armor* plain text format. Required |
||
1617 | for pgp credential type. |
||
1618 | |||
1619 | Examples: |
||
1620 | Creating a Username + Password credential |
||
1621 | |||
1622 | .. code-block:: python |
||
1623 | |||
1624 | gmp.create_credential( |
||
1625 | name='UP Credential', |
||
1626 | credential_type=CredentialType.USERNAME_PASSWORD, |
||
1627 | login='foo', |
||
1628 | password='bar', |
||
1629 | ) |
||
1630 | |||
1631 | Creating a Username + SSH Key credential |
||
1632 | |||
1633 | .. code-block:: python |
||
1634 | |||
1635 | with open('path/to/private-ssh-key') as f: |
||
1636 | key = f.read() |
||
1637 | |||
1638 | gmp.create_credential( |
||
1639 | name='USK Credential', |
||
1640 | credential_type=CredentialType.USERNAME_SSH_KEY, |
||
1641 | login='foo', |
||
1642 | key_phrase='foobar', |
||
1643 | private_key=key, |
||
1644 | ) |
||
1645 | |||
1646 | Creating a PGP credential |
||
1647 | |||
1648 | .. note:: |
||
1649 | |||
1650 | A compatible public pgp key file can be exported with GnuPG via |
||
1651 | :: |
||
1652 | |||
1653 | $ gpg --armor --export [email protected] > alice.asc |
||
1654 | |||
1655 | .. code-block:: python |
||
1656 | |||
1657 | with open('path/to/pgp.key.asc') as f: |
||
1658 | key = f.read() |
||
1659 | |||
1660 | gmp.create_credential( |
||
1661 | name='PGP Credential', |
||
1662 | credential_type=CredentialType.PGP_ENCRYPTION_KEY, |
||
1663 | public_key=key, |
||
1664 | ) |
||
1665 | |||
1666 | Creating a S/MIME credential |
||
1667 | |||
1668 | .. code-block:: python |
||
1669 | |||
1670 | with open('path/to/smime-cert') as f: |
||
1671 | cert = f.read() |
||
1672 | |||
1673 | gmp.create_credential( |
||
1674 | name='SMIME Credential', |
||
1675 | credential_type=CredentialType.SMIME_CERTIFICATE, |
||
1676 | certificate=cert, |
||
1677 | ) |
||
1678 | |||
1679 | Creating a Password-Only credential |
||
1680 | |||
1681 | .. code-block:: python |
||
1682 | |||
1683 | gmp.create_credential( |
||
1684 | name='Password-Only Credential', |
||
1685 | credential_type=CredentialType.PASSWORD_ONLY, |
||
1686 | password='foo', |
||
1687 | ) |
||
1688 | Returns: |
||
1689 | The response. See :py:meth:`send_command` for details. |
||
1690 | """ |
||
1691 | if not name: |
||
1692 | raise RequiredArgument( |
||
1693 | function=self.create_credential.__name__, argument='name' |
||
1694 | ) |
||
1695 | |||
1696 | if not isinstance(credential_type, self.types.CredentialType): |
||
1697 | raise InvalidArgumentType( |
||
1698 | function=self.create_credential.__name__, |
||
1699 | argument='credential_type', |
||
1700 | arg_type=CredentialType.__name__, |
||
1701 | ) |
||
1702 | |||
1703 | cmd = XmlCommand("create_credential") |
||
1704 | cmd.add_element("name", name) |
||
1705 | |||
1706 | cmd.add_element("type", credential_type.value) |
||
1707 | |||
1708 | if comment: |
||
1709 | cmd.add_element("comment", comment) |
||
1710 | |||
1711 | if allow_insecure is not None: |
||
1712 | cmd.add_element("allow_insecure", to_bool(allow_insecure)) |
||
1713 | |||
1714 | if ( |
||
1715 | credential_type == CredentialType.CLIENT_CERTIFICATE |
||
1716 | or credential_type == CredentialType.SMIME_CERTIFICATE |
||
1717 | ): |
||
1718 | if not certificate: |
||
1719 | raise RequiredArgument( |
||
1720 | function=self.create_credential.__name__, |
||
1721 | argument='certificate', |
||
1722 | ) |
||
1723 | |||
1724 | cmd.add_element("certificate", certificate) |
||
1725 | |||
1726 | if ( |
||
1727 | credential_type == CredentialType.USERNAME_PASSWORD |
||
1728 | or credential_type == CredentialType.USERNAME_SSH_KEY |
||
1729 | or credential_type == CredentialType.SNMP |
||
1730 | ): |
||
1731 | if not login: |
||
1732 | raise RequiredArgument( |
||
1733 | function=self.create_credential.__name__, argument='login' |
||
1734 | ) |
||
1735 | |||
1736 | cmd.add_element("login", login) |
||
1737 | |||
1738 | if credential_type == CredentialType.PASSWORD_ONLY and not password: |
||
1739 | raise RequiredArgument( |
||
1740 | function=self.create_credential.__name__, argument='password' |
||
1741 | ) |
||
1742 | |||
1743 | if ( |
||
1744 | credential_type == CredentialType.USERNAME_PASSWORD |
||
1745 | or credential_type == CredentialType.SNMP |
||
1746 | or credential_type == CredentialType.PASSWORD_ONLY |
||
1747 | ) and password: |
||
1748 | cmd.add_element("password", password) |
||
1749 | |||
1750 | if credential_type == CredentialType.USERNAME_SSH_KEY: |
||
1751 | if not private_key: |
||
1752 | raise RequiredArgument( |
||
1753 | function=self.create_credential.__name__, |
||
1754 | argument='private_key', |
||
1755 | ) |
||
1756 | |||
1757 | _xmlkey = cmd.add_element("key") |
||
1758 | _xmlkey.add_element("private", private_key) |
||
1759 | |||
1760 | if key_phrase: |
||
1761 | _xmlkey.add_element("phrase", key_phrase) |
||
1762 | |||
1763 | if credential_type == CredentialType.CLIENT_CERTIFICATE and private_key: |
||
1764 | _xmlkey = cmd.add_element("key") |
||
1765 | _xmlkey.add_element("private", private_key) |
||
1766 | |||
1767 | if credential_type == CredentialType.SNMP: |
||
1768 | if not isinstance(auth_algorithm, self.types.SnmpAuthAlgorithm): |
||
1769 | raise InvalidArgumentType( |
||
1770 | function=self.create_credential.__name__, |
||
1771 | argument='auth_algorithm', |
||
1772 | arg_type=SnmpAuthAlgorithm.__name__, |
||
1773 | ) |
||
1774 | |||
1775 | cmd.add_element("auth_algorithm", auth_algorithm.value) |
||
1776 | |||
1777 | if community: |
||
1778 | cmd.add_element("community", community) |
||
1779 | |||
1780 | View Code Duplication | if privacy_algorithm is not None or privacy_password: |
|
1781 | _xmlprivacy = cmd.add_element("privacy") |
||
1782 | |||
1783 | if privacy_algorithm is not None: |
||
1784 | if not isinstance( |
||
1785 | privacy_algorithm, self.types.SnmpPrivacyAlgorithm |
||
1786 | ): |
||
1787 | raise InvalidArgumentType( |
||
1788 | function=self.create_credential.__name__, |
||
1789 | argument='privacy_algorithm', |
||
1790 | arg_type=SnmpPrivacyAlgorithm.__name__, |
||
1791 | ) |
||
1792 | |||
1793 | _xmlprivacy.add_element( |
||
1794 | "algorithm", privacy_algorithm.value |
||
1795 | ) |
||
1796 | |||
1797 | if privacy_password: |
||
1798 | _xmlprivacy.add_element("password", privacy_password) |
||
1799 | |||
1800 | if credential_type == CredentialType.PGP_ENCRYPTION_KEY: |
||
1801 | if not public_key: |
||
1802 | raise RequiredArgument( |
||
1803 | function=self.create_credential.__name__, |
||
1804 | argument='public_key', |
||
1805 | ) |
||
1806 | |||
1807 | _xmlkey = cmd.add_element("key") |
||
1808 | _xmlkey.add_element("public", public_key) |
||
1809 | |||
1810 | return self._send_xml_command(cmd) |
||
1811 | |||
1812 | def modify_credential( |
||
1813 | self, |
||
1814 | credential_id: str, |
||
1815 | *, |
||
1816 | name: Optional[str] = None, |
||
1817 | comment: Optional[str] = None, |
||
1818 | allow_insecure: Optional[bool] = None, |
||
1819 | certificate: Optional[str] = None, |
||
1820 | key_phrase: Optional[str] = None, |
||
1821 | private_key: Optional[str] = None, |
||
1822 | login: Optional[str] = None, |
||
1823 | password: Optional[str] = None, |
||
1824 | auth_algorithm: Optional[SnmpAuthAlgorithm] = None, |
||
1825 | community: Optional[str] = None, |
||
1826 | privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None, |
||
1827 | privacy_password: Optional[str] = None, |
||
1828 | public_key: Optional[str] = None, |
||
1829 | ) -> Any: |
||
1830 | """Modifies an existing credential. |
||
1831 | |||
1832 | Arguments: |
||
1833 | credential_id: UUID of the credential |
||
1834 | name: Name of the credential |
||
1835 | comment: Comment for the credential |
||
1836 | allow_insecure: Whether to allow insecure use of the credential |
||
1837 | certificate: Certificate for the credential |
||
1838 | key_phrase: Key passphrase for the private key |
||
1839 | private_key: Private key to use for login |
||
1840 | login: Username for the credential |
||
1841 | password: Password for the credential |
||
1842 | auth_algorithm: The authentication algorithm for SNMP |
||
1843 | community: The SNMP community |
||
1844 | privacy_algorithm: The privacy algorithm for SNMP |
||
1845 | privacy_password: The SNMP privacy password |
||
1846 | public_key: PGP public key in *armor* plain text format |
||
1847 | |||
1848 | Returns: |
||
1849 | The response. See :py:meth:`send_command` for details. |
||
1850 | """ |
||
1851 | if not credential_id: |
||
1852 | raise RequiredArgument( |
||
1853 | function=self.modify_credential.__name__, |
||
1854 | argument='credential_id', |
||
1855 | ) |
||
1856 | |||
1857 | cmd = XmlCommand("modify_credential") |
||
1858 | cmd.set_attribute("credential_id", credential_id) |
||
1859 | |||
1860 | if comment: |
||
1861 | cmd.add_element("comment", comment) |
||
1862 | |||
1863 | if name: |
||
1864 | cmd.add_element("name", name) |
||
1865 | |||
1866 | if allow_insecure is not None: |
||
1867 | cmd.add_element("allow_insecure", to_bool(allow_insecure)) |
||
1868 | |||
1869 | if certificate: |
||
1870 | cmd.add_element("certificate", certificate) |
||
1871 | |||
1872 | if key_phrase and private_key: |
||
1873 | _xmlkey = cmd.add_element("key") |
||
1874 | _xmlkey.add_element("phrase", key_phrase) |
||
1875 | _xmlkey.add_element("private", private_key) |
||
1876 | elif (not key_phrase and private_key) or ( |
||
1877 | key_phrase and not private_key |
||
1878 | ): |
||
1879 | raise RequiredArgument( |
||
1880 | function=self.modify_credential.__name__, |
||
1881 | argument='key_phrase and private_key', |
||
1882 | ) |
||
1883 | |||
1884 | if login: |
||
1885 | cmd.add_element("login", login) |
||
1886 | |||
1887 | if password: |
||
1888 | cmd.add_element("password", password) |
||
1889 | |||
1890 | if auth_algorithm: |
||
1891 | if not isinstance(auth_algorithm, self.types.SnmpAuthAlgorithm): |
||
1892 | raise InvalidArgumentType( |
||
1893 | function=self.modify_credential.__name__, |
||
1894 | argument='auth_algorithm', |
||
1895 | arg_type=SnmpAuthAlgorithm.__name__, |
||
1896 | ) |
||
1897 | cmd.add_element("auth_algorithm", auth_algorithm.value) |
||
1898 | |||
1899 | if community: |
||
1900 | cmd.add_element("community", community) |
||
1901 | |||
1902 | View Code Duplication | if privacy_algorithm is not None or privacy_password is not None: |
|
1903 | _xmlprivacy = cmd.add_element("privacy") |
||
1904 | |||
1905 | if privacy_algorithm is not None: |
||
1906 | if not isinstance( |
||
1907 | privacy_algorithm, self.types.SnmpPrivacyAlgorithm |
||
1908 | ): |
||
1909 | raise InvalidArgumentType( |
||
1910 | function=self.modify_credential.__name__, |
||
1911 | argument='privacy_algorithm', |
||
1912 | arg_type=SnmpPrivacyAlgorithm.__name__, |
||
1913 | ) |
||
1914 | |||
1915 | _xmlprivacy.add_element("algorithm", privacy_algorithm.value) |
||
1916 | |||
1917 | if privacy_password is not None: |
||
1918 | _xmlprivacy.add_element("password", privacy_password) |
||
1919 | |||
1920 | if public_key: |
||
1921 | _xmlkey = cmd.add_element("key") |
||
1922 | _xmlkey.add_element("public", public_key) |
||
1923 | |||
1924 | return self._send_xml_command(cmd) |
||
1925 | |||
1926 | def create_ticket( |
||
1927 | self, |
||
1928 | *, |
||
1929 | result_id: str, |
||
1930 | assigned_to_user_id: str, |
||
1931 | note: str, |
||
1932 | comment: Optional[str] = None, |
||
1933 | ) -> Any: |
||
1934 | """Create a new ticket |
||
1935 | |||
1936 | Arguments: |
||
1937 | result_id: UUID of the result the ticket applies to |
||
1938 | assigned_to_user_id: UUID of a user the ticket should be assigned to |
||
1939 | note: A note about opening the ticket |
||
1940 | comment: Comment for the ticket |
||
1941 | |||
1942 | Returns: |
||
1943 | The response. See :py:meth:`send_command` for details. |
||
1944 | """ |
||
1945 | if not result_id: |
||
1946 | raise RequiredArgument( |
||
1947 | function=self.create_ticket.__name__, argument='result_id' |
||
1948 | ) |
||
1949 | |||
1950 | if not assigned_to_user_id: |
||
1951 | raise RequiredArgument( |
||
1952 | function=self.create_ticket.__name__, |
||
1953 | argument='assigned_to_user_id', |
||
1954 | ) |
||
1955 | |||
1956 | if not note: |
||
1957 | raise RequiredArgument( |
||
1958 | function=self.create_ticket.__name__, argument='note' |
||
1959 | ) |
||
1960 | |||
1961 | cmd = XmlCommand("create_ticket") |
||
1962 | |||
1963 | _result = cmd.add_element("result") |
||
1964 | _result.set_attribute("id", result_id) |
||
1965 | |||
1966 | _assigned = cmd.add_element("assigned_to") |
||
1967 | _user = _assigned.add_element("user") |
||
1968 | _user.set_attribute("id", assigned_to_user_id) |
||
1969 | |||
1970 | _note = cmd.add_element("open_note", note) |
||
1971 | |||
1972 | if comment: |
||
1973 | cmd.add_element("comment", comment) |
||
1974 | |||
1975 | return self._send_xml_command(cmd) |
||
1976 | |||
1977 | def delete_ticket( |
||
1978 | self, ticket_id: str, *, ultimate: Optional[bool] = False |
||
1979 | ): |
||
1980 | """Deletes an existing ticket |
||
1981 | |||
1982 | Arguments: |
||
1983 | ticket_id: UUID of the ticket to be deleted. |
||
1984 | ultimate: Whether to remove entirely, or to the trashcan. |
||
1985 | """ |
||
1986 | if not ticket_id: |
||
1987 | raise RequiredArgument( |
||
1988 | function=self.delete_ticket.__name__, argument='ticket_id' |
||
1989 | ) |
||
1990 | |||
1991 | cmd = XmlCommand("delete_ticket") |
||
1992 | cmd.set_attribute("ticket_id", ticket_id) |
||
1993 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
1994 | |||
1995 | return self._send_xml_command(cmd) |
||
1996 | |||
1997 | View Code Duplication | def get_tickets( |
|
1998 | self, |
||
1999 | *, |
||
2000 | trash: Optional[bool] = None, |
||
2001 | filter: Optional[str] = None, |
||
2002 | filter_id: Optional[str] = None, |
||
2003 | ) -> Any: |
||
2004 | """Request a list of tickets |
||
2005 | |||
2006 | Arguments: |
||
2007 | filter: Filter term to use for the query |
||
2008 | filter_id: UUID of an existing filter to use for the query |
||
2009 | trash: True to request the tickets in the trashcan |
||
2010 | |||
2011 | Returns: |
||
2012 | The response. See :py:meth:`send_command` for details. |
||
2013 | """ |
||
2014 | cmd = XmlCommand("get_tickets") |
||
2015 | |||
2016 | add_filter(cmd, filter, filter_id) |
||
2017 | |||
2018 | if trash is not None: |
||
2019 | cmd.set_attribute("trash", to_bool(trash)) |
||
2020 | |||
2021 | return self._send_xml_command(cmd) |
||
2022 | |||
2023 | def get_ticket(self, ticket_id: str) -> Any: |
||
2024 | """Request a single ticket |
||
2025 | |||
2026 | Arguments: |
||
2027 | ticket_id: UUID of an existing ticket |
||
2028 | |||
2029 | Returns: |
||
2030 | The response. See :py:meth:`send_command` for details. |
||
2031 | """ |
||
2032 | if not ticket_id: |
||
2033 | raise RequiredArgument( |
||
2034 | function=self.get_ticket.__name__, argument='ticket_id' |
||
2035 | ) |
||
2036 | |||
2037 | cmd = XmlCommand("get_tickets") |
||
2038 | cmd.set_attribute("ticket_id", ticket_id) |
||
2039 | return self._send_xml_command(cmd) |
||
2040 | |||
2041 | def get_vulnerabilities( |
||
2042 | self, *, filter: Optional[str] = None, filter_id: Optional[str] = None |
||
2043 | ) -> Any: |
||
2044 | """Request a list of vulnerabilities |
||
2045 | |||
2046 | Arguments: |
||
2047 | filter: Filter term to use for the query |
||
2048 | filter_id: UUID of an existing filter to use for the query |
||
2049 | Returns: |
||
2050 | The response. See :py:meth:`send_command` for details. |
||
2051 | """ |
||
2052 | cmd = XmlCommand("get_vulns") |
||
2053 | |||
2054 | add_filter(cmd, filter, filter_id) |
||
2055 | |||
2056 | return self._send_xml_command(cmd) |
||
2057 | |||
2058 | def get_vulnerability(self, vulnerability_id: str) -> Any: |
||
2059 | """Request a single vulnerability |
||
2060 | |||
2061 | Arguments: |
||
2062 | vulnerability_id: ID of an existing vulnerability |
||
2063 | |||
2064 | Returns: |
||
2065 | The response. See :py:meth:`send_command` for details. |
||
2066 | """ |
||
2067 | if not vulnerability_id: |
||
2068 | raise RequiredArgument( |
||
2069 | function=self.get_vulnerability.__name__, |
||
2070 | argument='vulnerability_id', |
||
2071 | ) |
||
2072 | |||
2073 | cmd = XmlCommand("get_vulns") |
||
2074 | cmd.set_attribute("vuln_id", vulnerability_id) |
||
2075 | return self._send_xml_command(cmd) |
||
2076 | |||
2077 | def modify_ticket( |
||
2078 | self, |
||
2079 | ticket_id: str, |
||
2080 | *, |
||
2081 | status: Optional[TicketStatus] = None, |
||
2082 | note: Optional[str] = None, |
||
2083 | assigned_to_user_id: Optional[str] = None, |
||
2084 | comment: Optional[str] = None, |
||
2085 | ) -> Any: |
||
2086 | """Modify a single ticket |
||
2087 | |||
2088 | Arguments: |
||
2089 | ticket_id: UUID of an existing ticket |
||
2090 | status: New status for the ticket |
||
2091 | note: Note for the status change. Required if status is set. |
||
2092 | assigned_to_user_id: UUID of the user the ticket should be assigned |
||
2093 | to |
||
2094 | comment: Comment for the ticket |
||
2095 | |||
2096 | Returns: |
||
2097 | The response. See :py:meth:`send_command` for details. |
||
2098 | """ |
||
2099 | if not ticket_id: |
||
2100 | raise RequiredArgument( |
||
2101 | function=self.modify_ticket.__name__, argument='ticket_id' |
||
2102 | ) |
||
2103 | |||
2104 | if status and not note: |
||
2105 | raise RequiredArgument( |
||
2106 | function=self.modify_ticket.__name__, argument='note' |
||
2107 | ) |
||
2108 | |||
2109 | if note and not status: |
||
2110 | raise RequiredArgument( |
||
2111 | function=self.modify_ticket.__name__, argument='status' |
||
2112 | ) |
||
2113 | |||
2114 | cmd = XmlCommand("modify_ticket") |
||
2115 | cmd.set_attribute("ticket_id", ticket_id) |
||
2116 | |||
2117 | if assigned_to_user_id: |
||
2118 | _assigned = cmd.add_element("assigned_to") |
||
2119 | _user = _assigned.add_element("user") |
||
2120 | _user.set_attribute("id", assigned_to_user_id) |
||
2121 | |||
2122 | if status: |
||
2123 | if not isinstance(status, self.types.TicketStatus): |
||
2124 | raise InvalidArgumentType( |
||
2125 | function=self.modify_ticket.__name__, |
||
2126 | argument='status', |
||
2127 | arg_type=TicketStatus.__name__, |
||
2128 | ) |
||
2129 | |||
2130 | cmd.add_element('status', status.value) |
||
2131 | cmd.add_element('{}_note'.format(status.name.lower()), note) |
||
2132 | |||
2133 | if comment: |
||
2134 | cmd.add_element("comment", comment) |
||
2135 | |||
2136 | return self._send_xml_command(cmd) |
||
2137 | |||
2138 | View Code Duplication | def create_filter( |
|
2139 | self, |
||
2140 | name: str, |
||
2141 | *, |
||
2142 | filter_type: Optional[FilterType] = None, |
||
2143 | comment: Optional[str] = None, |
||
2144 | term: Optional[str] = None, |
||
2145 | ) -> Any: |
||
2146 | """Create a new filter |
||
2147 | |||
2148 | Arguments: |
||
2149 | name: Name of the new filter |
||
2150 | filter_type: Filter for entity type |
||
2151 | comment: Comment for the filter |
||
2152 | term: Filter term e.g. 'name=foo' |
||
2153 | |||
2154 | Returns: |
||
2155 | The response. See :py:meth:`send_command` for details. |
||
2156 | """ |
||
2157 | if not name: |
||
2158 | raise RequiredArgument( |
||
2159 | function=self.create_filter.__name__, argument="name" |
||
2160 | ) |
||
2161 | |||
2162 | cmd = XmlCommand("create_filter") |
||
2163 | _xmlname = cmd.add_element("name", name) |
||
2164 | |||
2165 | if comment: |
||
2166 | cmd.add_element("comment", comment) |
||
2167 | |||
2168 | if term: |
||
2169 | cmd.add_element("term", term) |
||
2170 | |||
2171 | if filter_type: |
||
2172 | if not isinstance(filter_type, self.types.FilterType): |
||
2173 | raise InvalidArgumentType( |
||
2174 | function=self.create_filter.__name__, |
||
2175 | argument="filter_type", |
||
2176 | arg_type=self.types.FilterType.__name__, |
||
2177 | ) |
||
2178 | |||
2179 | cmd.add_element("type", filter_type.value) |
||
2180 | |||
2181 | return self._send_xml_command(cmd) |
||
2182 | |||
2183 | View Code Duplication | def modify_filter( |
|
2184 | self, |
||
2185 | filter_id: str, |
||
2186 | *, |
||
2187 | comment: Optional[str] = None, |
||
2188 | name: Optional[str] = None, |
||
2189 | term: Optional[str] = None, |
||
2190 | filter_type: Optional[FilterType] = None, |
||
2191 | ) -> Any: |
||
2192 | """Modifies an existing filter. |
||
2193 | |||
2194 | Arguments: |
||
2195 | filter_id: UUID of the filter to be modified |
||
2196 | comment: Comment on filter. |
||
2197 | name: Name of filter. |
||
2198 | term: Filter term. |
||
2199 | filter_type: Resource type filter applies to. |
||
2200 | |||
2201 | Returns: |
||
2202 | The response. See :py:meth:`send_command` for details. |
||
2203 | """ |
||
2204 | if not filter_id: |
||
2205 | raise RequiredArgument( |
||
2206 | function=self.modify_filter.__name__, argument='filter_id' |
||
2207 | ) |
||
2208 | |||
2209 | cmd = XmlCommand("modify_filter") |
||
2210 | cmd.set_attribute("filter_id", filter_id) |
||
2211 | |||
2212 | if comment: |
||
2213 | cmd.add_element("comment", comment) |
||
2214 | |||
2215 | if name: |
||
2216 | cmd.add_element("name", name) |
||
2217 | |||
2218 | if term: |
||
2219 | cmd.add_element("term", term) |
||
2220 | |||
2221 | if filter_type: |
||
2222 | if not isinstance(filter_type, self.types.FilterType): |
||
2223 | raise InvalidArgumentType( |
||
2224 | function=self.modify_filter.__name__, |
||
2225 | argument='filter_type', |
||
2226 | arg_type=FilterType.__name__, |
||
2227 | ) |
||
2228 | cmd.add_element("type", filter_type.value) |
||
2229 | |||
2230 | return self._send_xml_command(cmd) |
||
2231 | |||
2232 | def create_schedule( |
||
2233 | self, |
||
2234 | name: str, |
||
2235 | icalendar: str, |
||
2236 | timezone: str, |
||
2237 | *, |
||
2238 | comment: Optional[str] = None, |
||
2239 | ) -> Any: |
||
2240 | """Create a new schedule based in `iCalendar`_ data. |
||
2241 | |||
2242 | Example: |
||
2243 | Requires https://pypi.org/project/icalendar/ |
||
2244 | |||
2245 | .. code-block:: python |
||
2246 | |||
2247 | import pytz |
||
2248 | |||
2249 | from datetime import datetime |
||
2250 | |||
2251 | from icalendar import Calendar, Event |
||
2252 | |||
2253 | cal = Calendar() |
||
2254 | |||
2255 | cal.add('prodid', '-//Foo Bar//') |
||
2256 | cal.add('version', '2.0') |
||
2257 | |||
2258 | event = Event() |
||
2259 | event.add('dtstamp', datetime.now(tz=pytz.UTC)) |
||
2260 | event.add('dtstart', datetime(2020, 1, 1, tzinfo=pytz.utc)) |
||
2261 | |||
2262 | cal.add_component(event) |
||
2263 | |||
2264 | gmp.create_schedule( |
||
2265 | name="My Schedule", |
||
2266 | icalendar=cal.to_ical(), |
||
2267 | timezone='UTC' |
||
2268 | ) |
||
2269 | |||
2270 | |||
2271 | Arguments: |
||
2272 | name: Name of the new schedule |
||
2273 | icalendar: `iCalendar`_ (RFC 5545) based data. |
||
2274 | timezone: Timezone to use for the icalender events e.g |
||
2275 | Europe/Berlin. If the datetime values in the icalendar data are |
||
2276 | missing timezone information this timezone gets applied. |
||
2277 | Otherwise the datetime values from the icalendar data are |
||
2278 | displayed in this timezone |
||
2279 | comment: Comment on schedule. |
||
2280 | |||
2281 | Returns: |
||
2282 | The response. See :py:meth:`send_command` for details. |
||
2283 | |||
2284 | .. _iCalendar: |
||
2285 | https://tools.ietf.org/html/rfc5545 |
||
2286 | """ |
||
2287 | if not name: |
||
2288 | raise RequiredArgument( |
||
2289 | function=self.create_schedule.__name__, argument='name' |
||
2290 | ) |
||
2291 | if not icalendar: |
||
2292 | raise RequiredArgument( |
||
2293 | function=self.create_schedule.__name__, argument='icalendar' |
||
2294 | ) |
||
2295 | if not timezone: |
||
2296 | raise RequiredArgument( |
||
2297 | function=self.create_schedule.__name__, argument='timezone' |
||
2298 | ) |
||
2299 | |||
2300 | cmd = XmlCommand("create_schedule") |
||
2301 | |||
2302 | cmd.add_element("name", name) |
||
2303 | cmd.add_element("icalendar", icalendar) |
||
2304 | cmd.add_element("timezone", timezone) |
||
2305 | |||
2306 | if comment: |
||
2307 | cmd.add_element("comment", comment) |
||
2308 | |||
2309 | return self._send_xml_command(cmd) |
||
2310 | |||
2311 | def modify_schedule( |
||
2312 | self, |
||
2313 | schedule_id: str, |
||
2314 | *, |
||
2315 | name: Optional[str] = None, |
||
2316 | icalendar: Optional[str] = None, |
||
2317 | timezone: Optional[str] = None, |
||
2318 | comment: Optional[str] = None, |
||
2319 | ) -> Any: |
||
2320 | """Modifies an existing schedule |
||
2321 | |||
2322 | Arguments: |
||
2323 | schedule_id: UUID of the schedule to be modified |
||
2324 | name: Name of the schedule |
||
2325 | icalendar: `iCalendar`_ (RFC 5545) based data. |
||
2326 | timezone: Timezone to use for the icalender events e.g |
||
2327 | Europe/Berlin. If the datetime values in the icalendar data are |
||
2328 | missing timezone information this timezone gets applied. |
||
2329 | Otherwise the datetime values from the icalendar data are |
||
2330 | displayed in this timezone |
||
2331 | commenhedule. |
||
2332 | |||
2333 | Returns: |
||
2334 | The response. See :py:meth:`send_command` for details. |
||
2335 | |||
2336 | .. _iCalendar: |
||
2337 | https://tools.ietf.org/html/rfc5545 |
||
2338 | """ |
||
2339 | if not schedule_id: |
||
2340 | raise RequiredArgument( |
||
2341 | function=self.modify_schedule.__name__, argument='schedule_id' |
||
2342 | ) |
||
2343 | |||
2344 | cmd = XmlCommand("modify_schedule") |
||
2345 | cmd.set_attribute("schedule_id", schedule_id) |
||
2346 | |||
2347 | if name: |
||
2348 | cmd.add_element("name", name) |
||
2349 | |||
2350 | if icalendar: |
||
2351 | cmd.add_element("icalendar", icalendar) |
||
2352 | |||
2353 | if timezone: |
||
2354 | cmd.add_element("timezone", timezone) |
||
2355 | |||
2356 | if comment: |
||
2357 | cmd.add_element("comment", comment) |
||
2358 | |||
2359 | return self._send_xml_command(cmd) |
||
2360 | |||
2361 | def clone_config(self, config_id: str) -> Any: |
||
2362 | """Clone a scan config from an existing one |
||
2363 | |||
2364 | Arguments: |
||
2365 | config_id: UUID of the existing scan config |
||
2366 | |||
2367 | Returns: |
||
2368 | The response. See :py:meth:`send_command` for details. |
||
2369 | """ |
||
2370 | if not config_id: |
||
2371 | raise RequiredArgument( |
||
2372 | function=self.clone_config.__name__, argument='config_id' |
||
2373 | ) |
||
2374 | |||
2375 | cmd = XmlCommand("create_config") |
||
2376 | cmd.add_element("copy", config_id) |
||
2377 | return self._send_xml_command(cmd) |
||
2378 | |||
2379 | def import_config(self, config: str) -> Any: |
||
2380 | """Import a scan config from XML |
||
2381 | |||
2382 | Arguments: |
||
2383 | config: Scan Config XML as string to import. This XML must |
||
2384 | contain a :code:`<get_configs_response>` root element. |
||
2385 | |||
2386 | Returns: |
||
2387 | The response. See :py:meth:`send_command` for details. |
||
2388 | """ |
||
2389 | if not config: |
||
2390 | raise RequiredArgument( |
||
2391 | function=self.import_config.__name__, argument='config' |
||
2392 | ) |
||
2393 | |||
2394 | cmd = XmlCommand("create_config") |
||
2395 | |||
2396 | try: |
||
2397 | cmd.append_xml_str(config) |
||
2398 | except etree.XMLSyntaxError as e: |
||
2399 | raise InvalidArgument( |
||
2400 | function=self.import_config.__name__, argument='config' |
||
2401 | ) from e |
||
2402 | |||
2403 | return self._send_xml_command(cmd) |
||
2404 | |||
2405 | def clone_credential(self, credential_id: str) -> Any: |
||
2406 | """Clone an existing credential |
||
2407 | |||
2408 | Arguments: |
||
2409 | credential_id: UUID of an existing credential to clone from |
||
2410 | |||
2411 | Returns: |
||
2412 | The response. See :py:meth:`send_command` for details. |
||
2413 | """ |
||
2414 | if not credential_id: |
||
2415 | raise RequiredArgument( |
||
2416 | function=self.clone_credential.__name__, |
||
2417 | argument='credential_id', |
||
2418 | ) |
||
2419 | |||
2420 | cmd = XmlCommand("create_credential") |
||
2421 | cmd.add_element("copy", credential_id) |
||
2422 | return self._send_xml_command(cmd) |
||
2423 | |||
2424 | def clone_filter(self, filter_id: str) -> Any: |
||
2425 | """Clone an existing filter |
||
2426 | |||
2427 | Arguments: |
||
2428 | filter_id: UUID of an existing filter to clone from |
||
2429 | |||
2430 | Returns: |
||
2431 | The response. See :py:meth:`send_command` for details. |
||
2432 | """ |
||
2433 | if not filter_id: |
||
2434 | raise RequiredArgument( |
||
2435 | function=self.clone_filter.__name__, argument='filter_id' |
||
2436 | ) |
||
2437 | |||
2438 | cmd = XmlCommand("create_filter") |
||
2439 | cmd.add_element("copy", filter_id) |
||
2440 | return self._send_xml_command(cmd) |
||
2441 | |||
2442 | def create_group( |
||
2443 | self, |
||
2444 | name: str, |
||
2445 | *, |
||
2446 | comment: Optional[str] = None, |
||
2447 | special: Optional[bool] = False, |
||
2448 | users: Optional[List[str]] = None, |
||
2449 | ) -> Any: |
||
2450 | """Create a new group |
||
2451 | |||
2452 | Arguments: |
||
2453 | name: Name of the new group |
||
2454 | comment: Comment for the group |
||
2455 | special: Create permission giving members full access to each |
||
2456 | other's entities |
||
2457 | users: List of user names to be in the group |
||
2458 | |||
2459 | Returns: |
||
2460 | The response. See :py:meth:`send_command` for details. |
||
2461 | """ |
||
2462 | if not name: |
||
2463 | raise RequiredArgument( |
||
2464 | function=self.create_group.__name__, argument='name' |
||
2465 | ) |
||
2466 | |||
2467 | cmd = XmlCommand("create_group") |
||
2468 | cmd.add_element("name", name) |
||
2469 | |||
2470 | if comment: |
||
2471 | cmd.add_element("comment", comment) |
||
2472 | |||
2473 | if special: |
||
2474 | _xmlspecial = cmd.add_element("specials") |
||
2475 | _xmlspecial.add_element("full") |
||
2476 | |||
2477 | if users: |
||
2478 | cmd.add_element("users", to_comma_list(users)) |
||
2479 | |||
2480 | return self._send_xml_command(cmd) |
||
2481 | |||
2482 | def clone_group(self, group_id: str) -> Any: |
||
2483 | """Clone an existing group |
||
2484 | |||
2485 | Arguments: |
||
2486 | group_id: UUID of an existing group to clone from |
||
2487 | |||
2488 | Returns: |
||
2489 | The response. See :py:meth:`send_command` for details. |
||
2490 | """ |
||
2491 | if not group_id: |
||
2492 | raise RequiredArgument( |
||
2493 | function=self.clone_group.__name__, argument='group_id' |
||
2494 | ) |
||
2495 | |||
2496 | cmd = XmlCommand("create_group") |
||
2497 | cmd.add_element("copy", group_id) |
||
2498 | return self._send_xml_command(cmd) |
||
2499 | |||
2500 | def clone_permission(self, permission_id: str) -> Any: |
||
2501 | """Clone an existing permission |
||
2502 | |||
2503 | Arguments: |
||
2504 | permission_id: UUID of an existing permission to clone from |
||
2505 | |||
2506 | Returns: |
||
2507 | The response. See :py:meth:`send_command` for details. |
||
2508 | """ |
||
2509 | if not permission_id: |
||
2510 | raise RequiredArgument( |
||
2511 | function=self.clone_permission.__name__, |
||
2512 | argument='permission_id', |
||
2513 | ) |
||
2514 | |||
2515 | cmd = XmlCommand("create_permission") |
||
2516 | cmd.add_element("copy", permission_id) |
||
2517 | return self._send_xml_command(cmd) |
||
2518 | |||
2519 | def clone_report_format( |
||
2520 | self, report_format_id: [Union[str, ReportFormatType]] |
||
2521 | ) -> Any: |
||
2522 | """Clone a report format from an existing one |
||
2523 | |||
2524 | Arguments: |
||
2525 | report_format_id: UUID of the existing report format |
||
2526 | or ReportFormatType (enum) |
||
2527 | |||
2528 | Returns: |
||
2529 | The response. See :py:meth:`send_command` for details. |
||
2530 | """ |
||
2531 | if not report_format_id: |
||
2532 | raise RequiredArgument( |
||
2533 | function=self.clone_report_format.__name__, |
||
2534 | argument='report_format_id', |
||
2535 | ) |
||
2536 | |||
2537 | cmd = XmlCommand("create_report_format") |
||
2538 | |||
2539 | if isinstance(report_format_id, ReportFormatType): |
||
2540 | report_format_id = report_format_id.value |
||
2541 | |||
2542 | cmd.add_element("copy", report_format_id) |
||
2543 | return self._send_xml_command(cmd) |
||
2544 | |||
2545 | def import_report_format(self, report_format: str) -> Any: |
||
2546 | """Import a report format from XML |
||
2547 | |||
2548 | Arguments: |
||
2549 | report_format: Report format XML as string to import. This XML must |
||
2550 | contain a :code:`<get_report_formats_response>` root element. |
||
2551 | |||
2552 | Returns: |
||
2553 | The response. See :py:meth:`send_command` for details. |
||
2554 | """ |
||
2555 | if not report_format: |
||
2556 | raise RequiredArgument( |
||
2557 | function=self.import_report_format.__name__, |
||
2558 | argument='report_format', |
||
2559 | ) |
||
2560 | |||
2561 | cmd = XmlCommand("create_report_format") |
||
2562 | |||
2563 | try: |
||
2564 | cmd.append_xml_str(report_format) |
||
2565 | except etree.XMLSyntaxError as e: |
||
2566 | raise InvalidArgument( |
||
2567 | function=self.import_report_format.__name__, |
||
2568 | argument='report_format', |
||
2569 | ) from e |
||
2570 | |||
2571 | return self._send_xml_command(cmd) |
||
2572 | |||
2573 | def create_role( |
||
2574 | self, |
||
2575 | name: str, |
||
2576 | *, |
||
2577 | comment: Optional[str] = None, |
||
2578 | users: Optional[List[str]] = None, |
||
2579 | ) -> Any: |
||
2580 | """Create a new role |
||
2581 | |||
2582 | Arguments: |
||
2583 | name: Name of the role |
||
2584 | comment: Comment for the role |
||
2585 | users: List of user names to add to the role |
||
2586 | |||
2587 | Returns: |
||
2588 | The response. See :py:meth:`send_command` for details. |
||
2589 | """ |
||
2590 | |||
2591 | if not name: |
||
2592 | raise RequiredArgument( |
||
2593 | function=self.create_role.__name__, argument='name' |
||
2594 | ) |
||
2595 | |||
2596 | cmd = XmlCommand("create_role") |
||
2597 | cmd.add_element("name", name) |
||
2598 | |||
2599 | if comment: |
||
2600 | cmd.add_element("comment", comment) |
||
2601 | |||
2602 | if users: |
||
2603 | cmd.add_element("users", to_comma_list(users)) |
||
2604 | |||
2605 | return self._send_xml_command(cmd) |
||
2606 | |||
2607 | def clone_role(self, role_id: str) -> Any: |
||
2608 | """Clone an existing role |
||
2609 | |||
2610 | Arguments: |
||
2611 | role_id: UUID of an existing role to clone from |
||
2612 | |||
2613 | Returns: |
||
2614 | The response. See :py:meth:`send_command` for details. |
||
2615 | """ |
||
2616 | if not role_id: |
||
2617 | raise RequiredArgument( |
||
2618 | function=self.clone_role.__name__, argument='role_id' |
||
2619 | ) |
||
2620 | |||
2621 | cmd = XmlCommand("create_role") |
||
2622 | cmd.add_element("copy", role_id) |
||
2623 | return self._send_xml_command(cmd) |
||
2624 | |||
2625 | def create_scanner( |
||
2626 | self, |
||
2627 | name: str, |
||
2628 | host: str, |
||
2629 | port: int, |
||
2630 | scanner_type: ScannerType, |
||
2631 | credential_id: str, |
||
2632 | *, |
||
2633 | ca_pub: Optional[str] = None, |
||
2634 | comment: Optional[str] = None, |
||
2635 | ) -> Any: |
||
2636 | """Create a new scanner |
||
2637 | |||
2638 | Arguments: |
||
2639 | name: Name of the scanner |
||
2640 | host: The host of the scanner |
||
2641 | port: The port of the scanner |
||
2642 | scanner_type: Type of the scanner. |
||
2643 | credential_id: UUID of client certificate credential for the |
||
2644 | scanner |
||
2645 | ca_pub: Certificate of CA to verify scanner certificate |
||
2646 | comment: Comment for the scanner |
||
2647 | Returns: |
||
2648 | The response. See :py:meth:`send_command` for details. |
||
2649 | """ |
||
2650 | if not name: |
||
2651 | raise RequiredArgument( |
||
2652 | function=self.create_scanner.__name__, argument='name' |
||
2653 | ) |
||
2654 | |||
2655 | if not host: |
||
2656 | raise RequiredArgument( |
||
2657 | function=self.create_scanner.__name__, argument='host' |
||
2658 | ) |
||
2659 | |||
2660 | if not port: |
||
2661 | raise RequiredArgument( |
||
2662 | function=self.create_scanner.__name__, argument='port' |
||
2663 | ) |
||
2664 | |||
2665 | if not scanner_type: |
||
2666 | raise RequiredArgument( |
||
2667 | function=self.create_scanner.__name__, argument='scanner_type' |
||
2668 | ) |
||
2669 | |||
2670 | if not credential_id: |
||
2671 | raise RequiredArgument( |
||
2672 | function=self.create_scanner.__name__, argument='credential_id' |
||
2673 | ) |
||
2674 | |||
2675 | if not isinstance(scanner_type, self.types.ScannerType): |
||
2676 | raise InvalidArgumentType( |
||
2677 | function=self.create_scanner.__name__, |
||
2678 | argument='scanner_type', |
||
2679 | arg_type=self.types.ScannerType.__name__, |
||
2680 | ) |
||
2681 | |||
2682 | cmd = XmlCommand("create_scanner") |
||
2683 | cmd.add_element("name", name) |
||
2684 | cmd.add_element("host", host) |
||
2685 | cmd.add_element("port", str(port)) |
||
2686 | cmd.add_element("type", scanner_type.value) |
||
2687 | |||
2688 | if ca_pub: |
||
2689 | cmd.add_element("ca_pub", ca_pub) |
||
2690 | |||
2691 | cmd.add_element("credential", attrs={"id": str(credential_id)}) |
||
2692 | |||
2693 | if comment: |
||
2694 | cmd.add_element("comment", comment) |
||
2695 | |||
2696 | return self._send_xml_command(cmd) |
||
2697 | |||
2698 | def clone_scanner(self, scanner_id: str) -> Any: |
||
2699 | """Clone an existing scanner |
||
2700 | |||
2701 | Arguments: |
||
2702 | scanner_id: UUID of an existing scanner to clone from |
||
2703 | |||
2704 | Returns: |
||
2705 | The response. See :py:meth:`send_command` for details. |
||
2706 | """ |
||
2707 | if not scanner_id: |
||
2708 | raise RequiredArgument( |
||
2709 | function=self.clone_scanner.__name__, argument='scanner_id' |
||
2710 | ) |
||
2711 | |||
2712 | cmd = XmlCommand("create_scanner") |
||
2713 | cmd.add_element("copy", scanner_id) |
||
2714 | return self._send_xml_command(cmd) |
||
2715 | |||
2716 | def clone_schedule(self, schedule_id: str) -> Any: |
||
2717 | """Clone an existing schedule |
||
2718 | |||
2719 | Arguments: |
||
2720 | schedule_id: UUID of an existing schedule to clone from |
||
2721 | |||
2722 | Returns: |
||
2723 | The response. See :py:meth:`send_command` for details. |
||
2724 | """ |
||
2725 | if not schedule_id: |
||
2726 | raise RequiredArgument( |
||
2727 | function=self.clone_schedule.__name__, argument='schedule_id' |
||
2728 | ) |
||
2729 | |||
2730 | cmd = XmlCommand("create_schedule") |
||
2731 | cmd.add_element("copy", schedule_id) |
||
2732 | return self._send_xml_command(cmd) |
||
2733 | |||
2734 | def clone_tag(self, tag_id: str) -> Any: |
||
2735 | """Clone an existing tag |
||
2736 | |||
2737 | Arguments: |
||
2738 | tag_id: UUID of an existing tag to clone from |
||
2739 | |||
2740 | Returns: |
||
2741 | The response. See :py:meth:`send_command` for details. |
||
2742 | """ |
||
2743 | if not tag_id: |
||
2744 | raise RequiredArgument( |
||
2745 | function=self.clone_tag.__name__, argument='tag_id' |
||
2746 | ) |
||
2747 | |||
2748 | cmd = XmlCommand("create_tag") |
||
2749 | cmd.add_element("copy", tag_id) |
||
2750 | return self._send_xml_command(cmd) |
||
2751 | |||
2752 | def create_user( |
||
2753 | self, |
||
2754 | name: str, |
||
2755 | *, |
||
2756 | password: Optional[str] = None, |
||
2757 | hosts: Optional[List[str]] = None, |
||
2758 | hosts_allow: Optional[bool] = False, |
||
2759 | ifaces: Optional[List[str]] = None, |
||
2760 | ifaces_allow: Optional[bool] = False, |
||
2761 | role_ids: Optional[List[str]] = None, |
||
2762 | ) -> Any: |
||
2763 | """Create a new user |
||
2764 | |||
2765 | Arguments: |
||
2766 | name: Name of the user |
||
2767 | password: Password of the user |
||
2768 | hosts: A list of host addresses (IPs, DNS names) |
||
2769 | hosts_allow: If True allow only access to passed hosts otherwise |
||
2770 | deny access. Default is False for deny hosts. |
||
2771 | ifaces: A list of interface names |
||
2772 | ifaces_allow: If True allow only access to passed interfaces |
||
2773 | otherwise deny access. Default is False for deny interfaces. |
||
2774 | role_ids: A list of role UUIDs for the user |
||
2775 | |||
2776 | Returns: |
||
2777 | The response. See :py:meth:`send_command` for details. |
||
2778 | """ |
||
2779 | if not name: |
||
2780 | raise RequiredArgument( |
||
2781 | function=self.create_user.__name__, argument='name' |
||
2782 | ) |
||
2783 | |||
2784 | cmd = XmlCommand("create_user") |
||
2785 | cmd.add_element("name", name) |
||
2786 | |||
2787 | if password: |
||
2788 | cmd.add_element("password", password) |
||
2789 | |||
2790 | if hosts: |
||
2791 | cmd.add_element( |
||
2792 | "hosts", |
||
2793 | to_comma_list(hosts), |
||
2794 | attrs={"allow": to_bool(hosts_allow)}, |
||
2795 | ) |
||
2796 | |||
2797 | if ifaces: |
||
2798 | cmd.add_element( |
||
2799 | "ifaces", |
||
2800 | to_comma_list(ifaces), |
||
2801 | attrs={"allow": to_bool(ifaces_allow)}, |
||
2802 | ) |
||
2803 | |||
2804 | if role_ids: |
||
2805 | for role in role_ids: |
||
2806 | cmd.add_element("role", attrs={"id": role}) |
||
2807 | |||
2808 | return self._send_xml_command(cmd) |
||
2809 | |||
2810 | def clone_user(self, user_id: str) -> Any: |
||
2811 | """Clone an existing user |
||
2812 | |||
2813 | Arguments: |
||
2814 | user_id: UUID of existing user to clone from |
||
2815 | |||
2816 | Returns: |
||
2817 | The response. See :py:meth:`send_command` for details. |
||
2818 | """ |
||
2819 | if not user_id: |
||
2820 | raise RequiredArgument( |
||
2821 | function=self.clone_user.__name__, argument='user_id' |
||
2822 | ) |
||
2823 | |||
2824 | cmd = XmlCommand("create_user") |
||
2825 | cmd.add_element("copy", user_id) |
||
2826 | return self._send_xml_command(cmd) |
||
2827 | |||
2828 | def delete_config( |
||
2829 | self, config_id: str, *, ultimate: Optional[bool] = False |
||
2830 | ) -> Any: |
||
2831 | """Deletes an existing config |
||
2832 | |||
2833 | Arguments: |
||
2834 | config_id: UUID of the config to be deleted. |
||
2835 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2836 | """ |
||
2837 | if not config_id: |
||
2838 | raise RequiredArgument( |
||
2839 | function=self.delete_config.__name__, argument='config_id' |
||
2840 | ) |
||
2841 | |||
2842 | cmd = XmlCommand("delete_config") |
||
2843 | cmd.set_attribute("config_id", config_id) |
||
2844 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2845 | |||
2846 | return self._send_xml_command(cmd) |
||
2847 | |||
2848 | def delete_credential( |
||
2849 | self, credential_id: str, *, ultimate: Optional[bool] = False |
||
2850 | ) -> Any: |
||
2851 | """Deletes an existing credential |
||
2852 | |||
2853 | Arguments: |
||
2854 | credential_id: UUID of the credential to be deleted. |
||
2855 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2856 | """ |
||
2857 | if not credential_id: |
||
2858 | raise RequiredArgument( |
||
2859 | function=self.delete_credential.__name__, |
||
2860 | argument='credential_id', |
||
2861 | ) |
||
2862 | |||
2863 | cmd = XmlCommand("delete_credential") |
||
2864 | cmd.set_attribute("credential_id", credential_id) |
||
2865 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2866 | |||
2867 | return self._send_xml_command(cmd) |
||
2868 | |||
2869 | def delete_filter( |
||
2870 | self, filter_id: str, *, ultimate: Optional[bool] = False |
||
2871 | ) -> Any: |
||
2872 | """Deletes an existing filter |
||
2873 | |||
2874 | Arguments: |
||
2875 | filter_id: UUID of the filter to be deleted. |
||
2876 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2877 | """ |
||
2878 | if not filter_id: |
||
2879 | raise RequiredArgument( |
||
2880 | function=self.delete_filter.__name__, argument='filter_id' |
||
2881 | ) |
||
2882 | |||
2883 | cmd = XmlCommand("delete_filter") |
||
2884 | cmd.set_attribute("filter_id", filter_id) |
||
2885 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2886 | |||
2887 | return self._send_xml_command(cmd) |
||
2888 | |||
2889 | def delete_group( |
||
2890 | self, group_id: str, *, ultimate: Optional[bool] = False |
||
2891 | ) -> Any: |
||
2892 | """Deletes an existing group |
||
2893 | |||
2894 | Arguments: |
||
2895 | group_id: UUID of the group to be deleted. |
||
2896 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2897 | """ |
||
2898 | if not group_id: |
||
2899 | raise RequiredArgument( |
||
2900 | function=self.delete_group.__name__, argument='group_id' |
||
2901 | ) |
||
2902 | |||
2903 | cmd = XmlCommand("delete_group") |
||
2904 | cmd.set_attribute("group_id", group_id) |
||
2905 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2906 | |||
2907 | return self._send_xml_command(cmd) |
||
2908 | |||
2909 | def delete_permission( |
||
2910 | self, permission_id: str, *, ultimate: Optional[bool] = False |
||
2911 | ) -> Any: |
||
2912 | """Deletes an existing permission |
||
2913 | |||
2914 | Arguments: |
||
2915 | permission_id: UUID of the permission to be deleted. |
||
2916 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2917 | """ |
||
2918 | if not permission_id: |
||
2919 | raise RequiredArgument( |
||
2920 | function=self.delete_permission.__name__, |
||
2921 | argument='permission_id', |
||
2922 | ) |
||
2923 | |||
2924 | cmd = XmlCommand("delete_permission") |
||
2925 | cmd.set_attribute("permission_id", permission_id) |
||
2926 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2927 | |||
2928 | return self._send_xml_command(cmd) |
||
2929 | |||
2930 | def delete_report_format( |
||
2931 | self, |
||
2932 | report_format_id: Optional[Union[str, ReportFormatType]] = None, |
||
2933 | *, |
||
2934 | ultimate: Optional[bool] = False, |
||
2935 | ) -> Any: |
||
2936 | """Deletes an existing report format |
||
2937 | |||
2938 | Arguments: |
||
2939 | report_format_id: UUID of the report format to be deleted. |
||
2940 | or ReportFormatType (enum) |
||
2941 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2942 | """ |
||
2943 | if not report_format_id: |
||
2944 | raise RequiredArgument( |
||
2945 | function=self.delete_report_format.__name__, |
||
2946 | argument='report_format_id', |
||
2947 | ) |
||
2948 | |||
2949 | cmd = XmlCommand("delete_report_format") |
||
2950 | |||
2951 | if isinstance(report_format_id, ReportFormatType): |
||
2952 | report_format_id = report_format_id.value |
||
2953 | |||
2954 | cmd.set_attribute("report_format_id", report_format_id) |
||
2955 | |||
2956 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2957 | |||
2958 | return self._send_xml_command(cmd) |
||
2959 | |||
2960 | def delete_role( |
||
2961 | self, role_id: str, *, ultimate: Optional[bool] = False |
||
2962 | ) -> Any: |
||
2963 | """Deletes an existing role |
||
2964 | |||
2965 | Arguments: |
||
2966 | role_id: UUID of the role to be deleted. |
||
2967 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2968 | """ |
||
2969 | if not role_id: |
||
2970 | raise RequiredArgument( |
||
2971 | function=self.delete_role.__name__, argument='role_id' |
||
2972 | ) |
||
2973 | |||
2974 | cmd = XmlCommand("delete_role") |
||
2975 | cmd.set_attribute("role_id", role_id) |
||
2976 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2977 | |||
2978 | return self._send_xml_command(cmd) |
||
2979 | |||
2980 | def delete_scanner( |
||
2981 | self, scanner_id: str, *, ultimate: Optional[bool] = False |
||
2982 | ) -> Any: |
||
2983 | """Deletes an existing scanner |
||
2984 | |||
2985 | Arguments: |
||
2986 | scanner_id: UUID of the scanner to be deleted. |
||
2987 | ultimate: Whether to remove entirely, or to the trashcan. |
||
2988 | """ |
||
2989 | if not scanner_id: |
||
2990 | raise RequiredArgument( |
||
2991 | function=self.delete_scanner.__name__, argument='scanner_id' |
||
2992 | ) |
||
2993 | |||
2994 | cmd = XmlCommand("delete_scanner") |
||
2995 | cmd.set_attribute("scanner_id", scanner_id) |
||
2996 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
2997 | |||
2998 | return self._send_xml_command(cmd) |
||
2999 | |||
3000 | def delete_schedule( |
||
3001 | self, schedule_id: str, *, ultimate: Optional[bool] = False |
||
3002 | ) -> Any: |
||
3003 | """Deletes an existing schedule |
||
3004 | |||
3005 | Arguments: |
||
3006 | schedule_id: UUID of the schedule to be deleted. |
||
3007 | ultimate: Whether to remove entirely, or to the trashcan. |
||
3008 | """ |
||
3009 | if not schedule_id: |
||
3010 | raise RequiredArgument( |
||
3011 | function=self.delete_schedule.__name__, argument='schedule_id' |
||
3012 | ) |
||
3013 | |||
3014 | cmd = XmlCommand("delete_schedule") |
||
3015 | cmd.set_attribute("schedule_id", schedule_id) |
||
3016 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
3017 | |||
3018 | return self._send_xml_command(cmd) |
||
3019 | |||
3020 | def delete_tag( |
||
3021 | self, tag_id: str, *, ultimate: Optional[bool] = False |
||
3022 | ) -> Any: |
||
3023 | """Deletes an existing tag |
||
3024 | |||
3025 | Arguments: |
||
3026 | tag_id: UUID of the tag to be deleted. |
||
3027 | ultimate: Whether to remove entirely, or to the trashcan. |
||
3028 | """ |
||
3029 | if not tag_id: |
||
3030 | raise RequiredArgument( |
||
3031 | function=self.delete_tag.__name__, argument='tag_id' |
||
3032 | ) |
||
3033 | |||
3034 | cmd = XmlCommand("delete_tag") |
||
3035 | cmd.set_attribute("tag_id", tag_id) |
||
3036 | cmd.set_attribute("ultimate", to_bool(ultimate)) |
||
3037 | |||
3038 | return self._send_xml_command(cmd) |
||
3039 | |||
3040 | def delete_user( |
||
3041 | self, |
||
3042 | user_id: str = None, |
||
3043 | *, |
||
3044 | name: Optional[str] = None, |
||
3045 | inheritor_id: Optional[str] = None, |
||
3046 | inheritor_name: Optional[str] = None, |
||
3047 | ) -> Any: |
||
3048 | """Deletes an existing user |
||
3049 | |||
3050 | Either user_id or name must be passed. |
||
3051 | |||
3052 | Arguments: |
||
3053 | user_id: UUID of the task to be deleted. |
||
3054 | name: The name of the user to be deleted. |
||
3055 | inheritor_id: The ID of the inheriting user or "self". Overrides |
||
3056 | inheritor_name. |
||
3057 | inheritor_name: The name of the inheriting user. |
||
3058 | |||
3059 | """ |
||
3060 | if not user_id and not name: |
||
3061 | raise RequiredArgument( |
||
3062 | function=self.delete_user.__name__, argument='user_id or name' |
||
3063 | ) |
||
3064 | |||
3065 | cmd = XmlCommand("delete_user") |
||
3066 | |||
3067 | if user_id: |
||
3068 | cmd.set_attribute("user_id", user_id) |
||
3069 | |||
3070 | if name: |
||
3071 | cmd.set_attribute("name", name) |
||
3072 | |||
3073 | if inheritor_id: |
||
3074 | cmd.set_attribute("inheritor_id", inheritor_id) |
||
3075 | if inheritor_name: |
||
3076 | cmd.set_attribute("inheritor_name", inheritor_name) |
||
3077 | |||
3078 | return self._send_xml_command(cmd) |
||
3079 | |||
3080 | def describe_auth(self) -> Any: |
||
3081 | """Describe authentication methods |
||
3082 | |||
3083 | Returns a list of all used authentication methods if such a list is |
||
3084 | available. |
||
3085 | |||
3086 | Returns: |
||
3087 | The response. See :py:meth:`send_command` for details. |
||
3088 | """ |
||
3089 | return self._send_xml_command(XmlCommand("describe_auth")) |
||
3090 | |||
3091 | def empty_trashcan(self) -> Any: |
||
3092 | """Empty the trashcan |
||
3093 | |||
3094 | Remove all entities from the trashcan. **Attention:** this command can |
||
3095 | not be reverted |
||
3096 | |||
3097 | Returns: |
||
3098 | The response. See :py:meth:`send_command` for details. |
||
3099 | """ |
||
3100 | return self._send_xml_command(XmlCommand("empty_trashcan")) |
||
3101 | |||
3102 | View Code Duplication | def get_credentials( |
|
3103 | self, |
||
3104 | *, |
||
3105 | filter: Optional[str] = None, |
||
3106 | filter_id: Optional[str] = None, |
||
3107 | scanners: Optional[bool] = None, |
||
3108 | trash: Optional[bool] = None, |
||
3109 | targets: Optional[bool] = None, |
||
3110 | ) -> Any: |
||
3111 | """Request a list of credentials |
||
3112 | |||
3113 | Arguments: |
||
3114 | filter: Filter term to use for the query |
||
3115 | filter_id: UUID of an existing filter to use for the query |
||
3116 | scanners: Whether to include a list of scanners using the |
||
3117 | credentials |
||
3118 | trash: Whether to get the trashcan credentials instead |
||
3119 | targets: Whether to include a list of targets using the credentials |
||
3120 | |||
3121 | Returns: |
||
3122 | The response. See :py:meth:`send_command` for details. |
||
3123 | """ |
||
3124 | cmd = XmlCommand("get_credentials") |
||
3125 | |||
3126 | add_filter(cmd, filter, filter_id) |
||
3127 | |||
3128 | if scanners is not None: |
||
3129 | cmd.set_attribute("scanners", to_bool(scanners)) |
||
3130 | |||
3131 | if trash is not None: |
||
3132 | cmd.set_attribute("trash", to_bool(trash)) |
||
3133 | |||
3134 | if targets is not None: |
||
3135 | cmd.set_attribute("targets", to_bool(targets)) |
||
3136 | |||
3137 | return self._send_xml_command(cmd) |
||
3138 | |||
3139 | def get_credential( |
||
3140 | self, |
||
3141 | credential_id: str, |
||
3142 | *, |
||
3143 | scanners: Optional[bool] = None, |
||
3144 | targets: Optional[bool] = None, |
||
3145 | credential_format: Optional[CredentialFormat] = None, |
||
3146 | ) -> Any: |
||
3147 | """Request a single credential |
||
3148 | |||
3149 | Arguments: |
||
3150 | credential_id: UUID of an existing credential |
||
3151 | scanners: Whether to include a list of scanners using the |
||
3152 | credentials |
||
3153 | targets: Whether to include a list of targets using the credentials |
||
3154 | credential_format: One of "key", "rpm", "deb", "exe" or "pem" |
||
3155 | |||
3156 | Returns: |
||
3157 | The response. See :py:meth:`send_command` for details. |
||
3158 | """ |
||
3159 | if not credential_id: |
||
3160 | raise RequiredArgument( |
||
3161 | function=self.get_credential.__name__, argument='credential_id' |
||
3162 | ) |
||
3163 | |||
3164 | cmd = XmlCommand("get_credentials") |
||
3165 | cmd.set_attribute("credential_id", credential_id) |
||
3166 | |||
3167 | if credential_format: |
||
3168 | if not isinstance(credential_format, CredentialFormat): |
||
3169 | raise InvalidArgumentType( |
||
3170 | function=self.get_credential.__name__, |
||
3171 | argument='credential_format', |
||
3172 | arg_type=CredentialFormat.__name__, |
||
3173 | ) |
||
3174 | |||
3175 | cmd.set_attribute("format", credential_format.value) |
||
3176 | |||
3177 | if scanners is not None: |
||
3178 | cmd.set_attribute("scanners", to_bool(scanners)) |
||
3179 | |||
3180 | if targets is not None: |
||
3181 | cmd.set_attribute("targets", to_bool(targets)) |
||
3182 | |||
3183 | return self._send_xml_command(cmd) |
||
3184 | |||
3185 | def get_feeds(self) -> Any: |
||
3186 | """Request the list of feeds |
||
3187 | |||
3188 | Returns: |
||
3189 | The response. See :py:meth:`send_command` for details. |
||
3190 | """ |
||
3191 | return self._send_xml_command(XmlCommand("get_feeds")) |
||
3192 | |||
3193 | View Code Duplication | def get_filters( |
|
3194 | self, |
||
3195 | *, |
||
3196 | filter: Optional[str] = None, |
||
3197 | filter_id: Optional[str] = None, |
||
3198 | trash: Optional[bool] = None, |
||
3199 | alerts: Optional[bool] = None, |
||
3200 | ) -> Any: |
||
3201 | """Request a list of filters |
||
3202 | |||
3203 | Arguments: |
||
3204 | filter: Filter term to use for the query |
||
3205 | filter_id: UUID of an existing filter to use for the query |
||
3206 | trash: Whether to get the trashcan filters instead |
||
3207 | alerts: Whether to include list of alerts that use the filter. |
||
3208 | |||
3209 | Returns: |
||
3210 | The response. See :py:meth:`send_command` for details. |
||
3211 | """ |
||
3212 | cmd = XmlCommand("get_filters") |
||
3213 | |||
3214 | add_filter(cmd, filter, filter_id) |
||
3215 | |||
3216 | if trash is not None: |
||
3217 | cmd.set_attribute("trash", to_bool(trash)) |
||
3218 | |||
3219 | if alerts is not None: |
||
3220 | cmd.set_attribute("alerts", to_bool(alerts)) |
||
3221 | |||
3222 | return self._send_xml_command(cmd) |
||
3223 | |||
3224 | def get_filter( |
||
3225 | self, filter_id: str, *, alerts: Optional[bool] = None |
||
3226 | ) -> Any: |
||
3227 | """Request a single filter |
||
3228 | |||
3229 | Arguments: |
||
3230 | filter_id: UUID of an existing filter |
||
3231 | alerts: Whether to include list of alerts that use the filter. |
||
3232 | |||
3233 | Returns: |
||
3234 | The response. See :py:meth:`send_command` for details. |
||
3235 | """ |
||
3236 | cmd = XmlCommand("get_filters") |
||
3237 | |||
3238 | if not filter_id: |
||
3239 | raise RequiredArgument( |
||
3240 | function=self.get_filter.__name__, argument='filter_id' |
||
3241 | ) |
||
3242 | |||
3243 | cmd.set_attribute("filter_id", filter_id) |
||
3244 | |||
3245 | if alerts is not None: |
||
3246 | cmd.set_attribute("alerts", to_bool(alerts)) |
||
3247 | |||
3248 | return self._send_xml_command(cmd) |
||
3249 | |||
3250 | View Code Duplication | def get_groups( |
|
3251 | self, |
||
3252 | *, |
||
3253 | filter: Optional[str] = None, |
||
3254 | filter_id: Optional[str] = None, |
||
3255 | trash: Optional[bool] = None, |
||
3256 | ) -> Any: |
||
3257 | """Request a list of groups |
||
3258 | |||
3259 | Arguments: |
||
3260 | filter: Filter term to use for the query |
||
3261 | filter_id: UUID of an existing filter to use for the query |
||
3262 | trash: Whether to get the trashcan groups instead |
||
3263 | |||
3264 | Returns: |
||
3265 | The response. See :py:meth:`send_command` for details. |
||
3266 | """ |
||
3267 | cmd = XmlCommand("get_groups") |
||
3268 | |||
3269 | add_filter(cmd, filter, filter_id) |
||
3270 | |||
3271 | if trash is not None: |
||
3272 | cmd.set_attribute("trash", to_bool(trash)) |
||
3273 | |||
3274 | return self._send_xml_command(cmd) |
||
3275 | |||
3276 | def get_group(self, group_id: str) -> Any: |
||
3277 | """Request a single group |
||
3278 | |||
3279 | Arguments: |
||
3280 | group_id: UUID of an existing group |
||
3281 | |||
3282 | Returns: |
||
3283 | The response. See :py:meth:`send_command` for details. |
||
3284 | """ |
||
3285 | cmd = XmlCommand("get_groups") |
||
3286 | |||
3287 | if not group_id: |
||
3288 | raise RequiredArgument( |
||
3289 | function=self.get_group.__name__, argument='group_id' |
||
3290 | ) |
||
3291 | |||
3292 | cmd.set_attribute("group_id", group_id) |
||
3293 | return self._send_xml_command(cmd) |
||
3294 | |||
3295 | View Code Duplication | def get_permissions( |
|
3296 | self, |
||
3297 | *, |
||
3298 | filter: Optional[str] = None, |
||
3299 | filter_id: Optional[str] = None, |
||
3300 | trash: Optional[bool] = None, |
||
3301 | ) -> Any: |
||
3302 | """Request a list of permissions |
||
3303 | |||
3304 | Arguments: |
||
3305 | filter: Filter term to use for the query |
||
3306 | filter_id: UUID of an existing filter to use for the query |
||
3307 | trash: Whether to get permissions in the trashcan instead |
||
3308 | |||
3309 | Returns: |
||
3310 | The response. See :py:meth:`send_command` for details. |
||
3311 | """ |
||
3312 | cmd = XmlCommand("get_permissions") |
||
3313 | |||
3314 | add_filter(cmd, filter, filter_id) |
||
3315 | |||
3316 | if trash is not None: |
||
3317 | cmd.set_attribute("trash", to_bool(trash)) |
||
3318 | |||
3319 | return self._send_xml_command(cmd) |
||
3320 | |||
3321 | def get_permission(self, permission_id: str) -> Any: |
||
3322 | """Request a single permission |
||
3323 | |||
3324 | Arguments: |
||
3325 | permission_id: UUID of an existing permission |
||
3326 | |||
3327 | Returns: |
||
3328 | The response. See :py:meth:`send_command` for details. |
||
3329 | """ |
||
3330 | cmd = XmlCommand("get_permissions") |
||
3331 | |||
3332 | if not permission_id: |
||
3333 | raise RequiredArgument( |
||
3334 | function=self.get_permission.__name__, argument='permission_id' |
||
3335 | ) |
||
3336 | |||
3337 | cmd.set_attribute("permission_id", permission_id) |
||
3338 | return self._send_xml_command(cmd) |
||
3339 | |||
3340 | def get_preferences( |
||
3341 | self, *, nvt_oid: Optional[str] = None, config_id: Optional[str] = None |
||
3342 | ) -> Any: |
||
3343 | """Request a list of preferences |
||
3344 | |||
3345 | When the command includes a config_id attribute, the preference element |
||
3346 | includes the preference name, type and value, and the NVT to which the |
||
3347 | preference applies. Otherwise, the preference element includes just the |
||
3348 | name and value, with the NVT and type built into the name. |
||
3349 | |||
3350 | Arguments: |
||
3351 | nvt_oid: OID of nvt |
||
3352 | config_id: UUID of scan config of which to show preference values |
||
3353 | |||
3354 | Returns: |
||
3355 | The response. See :py:meth:`send_command` for details. |
||
3356 | """ |
||
3357 | cmd = XmlCommand("get_preferences") |
||
3358 | |||
3359 | if nvt_oid: |
||
3360 | cmd.set_attribute("nvt_oid", nvt_oid) |
||
3361 | |||
3362 | if config_id: |
||
3363 | cmd.set_attribute("config_id", config_id) |
||
3364 | |||
3365 | return self._send_xml_command(cmd) |
||
3366 | |||
3367 | def get_preference( |
||
3368 | self, |
||
3369 | name: str, |
||
3370 | *, |
||
3371 | nvt_oid: Optional[str] = None, |
||
3372 | config_id: Optional[str] = None, |
||
3373 | ) -> Any: |
||
3374 | """Request a nvt preference |
||
3375 | |||
3376 | Arguments: |
||
3377 | name: name of a particular preference |
||
3378 | nvt_oid: OID of nvt |
||
3379 | config_id: UUID of scan config of which to show preference values |
||
3380 | |||
3381 | Returns: |
||
3382 | The response. See :py:meth:`send_command` for details. |
||
3383 | """ |
||
3384 | cmd = XmlCommand("get_preferences") |
||
3385 | |||
3386 | if not name: |
||
3387 | raise RequiredArgument( |
||
3388 | function=self.get_preference.__name__, argument='name' |
||
3389 | ) |
||
3390 | |||
3391 | cmd.set_attribute("preference", name) |
||
3392 | |||
3393 | if nvt_oid: |
||
3394 | cmd.set_attribute("nvt_oid", nvt_oid) |
||
3395 | |||
3396 | if config_id: |
||
3397 | cmd.set_attribute("config_id", config_id) |
||
3398 | |||
3399 | return self._send_xml_command(cmd) |
||
3400 | |||
3401 | def get_report_formats( |
||
3402 | self, |
||
3403 | *, |
||
3404 | filter: Optional[str] = None, |
||
3405 | filter_id: Optional[str] = None, |
||
3406 | trash: Optional[bool] = None, |
||
3407 | alerts: Optional[bool] = None, |
||
3408 | params: Optional[bool] = None, |
||
3409 | details: Optional[bool] = None, |
||
3410 | ) -> Any: |
||
3411 | """Request a list of report formats |
||
3412 | |||
3413 | Arguments: |
||
3414 | filter: Filter term to use for the query |
||
3415 | filter_id: UUID of an existing filter to use for the query |
||
3416 | trash: Whether to get the trashcan report formats instead |
||
3417 | alerts: Whether to include alerts that use the report format |
||
3418 | params: Whether to include report format parameters |
||
3419 | details: Include report format file, signature and parameters |
||
3420 | |||
3421 | Returns: |
||
3422 | The response. See :py:meth:`send_command` for details. |
||
3423 | """ |
||
3424 | cmd = XmlCommand("get_report_formats") |
||
3425 | |||
3426 | add_filter(cmd, filter, filter_id) |
||
3427 | |||
3428 | if details is not None: |
||
3429 | cmd.set_attribute("details", to_bool(details)) |
||
3430 | |||
3431 | if alerts is not None: |
||
3432 | cmd.set_attribute("alerts", to_bool(alerts)) |
||
3433 | |||
3434 | if params is not None: |
||
3435 | cmd.set_attribute("params", to_bool(params)) |
||
3436 | |||
3437 | if trash is not None: |
||
3438 | cmd.set_attribute("trash", to_bool(trash)) |
||
3439 | |||
3440 | return self._send_xml_command(cmd) |
||
3441 | |||
3442 | View Code Duplication | def get_report_format( |
|
3443 | self, report_format_id: Union[str, ReportFormatType] |
||
3444 | ) -> Any: |
||
3445 | """Request a single report format |
||
3446 | |||
3447 | Arguments: |
||
3448 | report_format_id: UUID of an existing report format |
||
3449 | or ReportFormatType (enum) |
||
3450 | |||
3451 | Returns: |
||
3452 | The response. See :py:meth:`send_command` for details. |
||
3453 | """ |
||
3454 | cmd = XmlCommand("get_report_formats") |
||
3455 | |||
3456 | if not report_format_id: |
||
3457 | raise RequiredArgument( |
||
3458 | function=self.get_report_format.__name__, |
||
3459 | argument='report_format_id', |
||
3460 | ) |
||
3461 | |||
3462 | if isinstance(report_format_id, ReportFormatType): |
||
3463 | report_format_id = report_format_id.value |
||
3464 | |||
3465 | cmd.set_attribute("report_format_id", report_format_id) |
||
3466 | |||
3467 | # for single entity always request all details |
||
3468 | cmd.set_attribute("details", "1") |
||
3469 | return self._send_xml_command(cmd) |
||
3470 | |||
3471 | View Code Duplication | def get_roles( |
|
3472 | self, |
||
3473 | *, |
||
3474 | filter: Optional[str] = None, |
||
3475 | filter_id: Optional[str] = None, |
||
3476 | trash: Optional[bool] = None, |
||
3477 | ) -> Any: |
||
3478 | """Request a list of roles |
||
3479 | |||
3480 | Arguments: |
||
3481 | filter: Filter term to use for the query |
||
3482 | filter_id: UUID of an existing filter to use for the query |
||
3483 | trash: Whether to get the trashcan roles instead |
||
3484 | |||
3485 | Returns: |
||
3486 | The response. See :py:meth:`send_command` for details. |
||
3487 | """ |
||
3488 | cmd = XmlCommand("get_roles") |
||
3489 | |||
3490 | add_filter(cmd, filter, filter_id) |
||
3491 | |||
3492 | if trash is not None: |
||
3493 | cmd.set_attribute("trash", to_bool(trash)) |
||
3494 | |||
3495 | return self._send_xml_command(cmd) |
||
3496 | |||
3497 | def get_role(self, role_id: str) -> Any: |
||
3498 | """Request a single role |
||
3499 | |||
3500 | Arguments: |
||
3501 | role_id: UUID of an existing role |
||
3502 | |||
3503 | Returns: |
||
3504 | The response. See :py:meth:`send_command` for details. |
||
3505 | """ |
||
3506 | if not role_id: |
||
3507 | raise RequiredArgument( |
||
3508 | function=self.get_role.__name__, argument='role_id' |
||
3509 | ) |
||
3510 | |||
3511 | cmd = XmlCommand("get_roles") |
||
3512 | cmd.set_attribute("role_id", role_id) |
||
3513 | return self._send_xml_command(cmd) |
||
3514 | |||
3515 | View Code Duplication | def get_scanners( |
|
3516 | self, |
||
3517 | *, |
||
3518 | filter: Optional[str] = None, |
||
3519 | filter_id: Optional[str] = None, |
||
3520 | trash: Optional[bool] = None, |
||
3521 | details: Optional[bool] = None, |
||
3522 | ) -> Any: |
||
3523 | """Request a list of scanners |
||
3524 | |||
3525 | Arguments: |
||
3526 | filter: Filter term to use for the query |
||
3527 | filter_id: UUID of an existing filter to use for the query |
||
3528 | trash: Whether to get the trashcan scanners instead |
||
3529 | details: Whether to include extra details like tasks using this |
||
3530 | scanner |
||
3531 | |||
3532 | Returns: |
||
3533 | The response. See :py:meth:`send_command` for details. |
||
3534 | """ |
||
3535 | cmd = XmlCommand("get_scanners") |
||
3536 | |||
3537 | add_filter(cmd, filter, filter_id) |
||
3538 | |||
3539 | if trash is not None: |
||
3540 | cmd.set_attribute("trash", to_bool(trash)) |
||
3541 | |||
3542 | if details is not None: |
||
3543 | cmd.set_attribute("details", to_bool(details)) |
||
3544 | |||
3545 | return self._send_xml_command(cmd) |
||
3546 | |||
3547 | def get_scanner(self, scanner_id: str) -> Any: |
||
3548 | """Request a single scanner |
||
3549 | |||
3550 | Arguments: |
||
3551 | scanner_id: UUID of an existing scanner |
||
3552 | |||
3553 | Returns: |
||
3554 | The response. See :py:meth:`send_command` for details. |
||
3555 | """ |
||
3556 | cmd = XmlCommand("get_scanners") |
||
3557 | |||
3558 | if not scanner_id: |
||
3559 | raise RequiredArgument( |
||
3560 | function=self.get_scanner.__name__, argument='scanner_id' |
||
3561 | ) |
||
3562 | |||
3563 | cmd.set_attribute("scanner_id", scanner_id) |
||
3564 | |||
3565 | # for single entity always request all details |
||
3566 | cmd.set_attribute("details", "1") |
||
3567 | return self._send_xml_command(cmd) |
||
3568 | |||
3569 | View Code Duplication | def get_schedules( |
|
3570 | self, |
||
3571 | *, |
||
3572 | filter: Optional[str] = None, |
||
3573 | filter_id: Optional[str] = None, |
||
3574 | trash: Optional[bool] = None, |
||
3575 | tasks: Optional[bool] = None, |
||
3576 | ) -> Any: |
||
3577 | """Request a list of schedules |
||
3578 | |||
3579 | Arguments: |
||
3580 | filter: Filter term to use for the query |
||
3581 | filter_id: UUID of an existing filter to use for the query |
||
3582 | trash: Whether to get the trashcan schedules instead |
||
3583 | tasks: Whether to include tasks using the schedules |
||
3584 | |||
3585 | Returns: |
||
3586 | The response. See :py:meth:`send_command` for details. |
||
3587 | """ |
||
3588 | cmd = XmlCommand("get_schedules") |
||
3589 | |||
3590 | add_filter(cmd, filter, filter_id) |
||
3591 | |||
3592 | if trash is not None: |
||
3593 | cmd.set_attribute("trash", to_bool(trash)) |
||
3594 | |||
3595 | if tasks is not None: |
||
3596 | cmd.set_attribute("tasks", to_bool(tasks)) |
||
3597 | |||
3598 | return self._send_xml_command(cmd) |
||
3599 | |||
3600 | def get_schedule( |
||
3601 | self, schedule_id: str, *, tasks: Optional[bool] = None |
||
3602 | ) -> Any: |
||
3603 | """Request a single schedule |
||
3604 | |||
3605 | Arguments: |
||
3606 | schedule_id: UUID of an existing schedule |
||
3607 | tasks: Whether to include tasks using the schedules |
||
3608 | |||
3609 | Returns: |
||
3610 | The response. See :py:meth:`send_command` for details. |
||
3611 | """ |
||
3612 | cmd = XmlCommand("get_schedules") |
||
3613 | |||
3614 | if not schedule_id: |
||
3615 | raise RequiredArgument( |
||
3616 | function=self.get_schedule.__name__, argument='schedule_id' |
||
3617 | ) |
||
3618 | |||
3619 | cmd.set_attribute("schedule_id", schedule_id) |
||
3620 | |||
3621 | if tasks is not None: |
||
3622 | cmd.set_attribute("tasks", to_bool(tasks)) |
||
3623 | |||
3624 | return self._send_xml_command(cmd) |
||
3625 | |||
3626 | def get_settings(self, *, filter: Optional[str] = None) -> Any: |
||
3627 | """Request a list of user settings |
||
3628 | |||
3629 | Arguments: |
||
3630 | filter: Filter term to use for the query |
||
3631 | |||
3632 | Returns: |
||
3633 | The response. See :py:meth:`send_command` for details. |
||
3634 | """ |
||
3635 | cmd = XmlCommand("get_settings") |
||
3636 | |||
3637 | if filter: |
||
3638 | cmd.set_attribute("filter", filter) |
||
3639 | |||
3640 | return self._send_xml_command(cmd) |
||
3641 | |||
3642 | def get_setting(self, setting_id: str) -> Any: |
||
3643 | """Request a single setting |
||
3644 | |||
3645 | Arguments: |
||
3646 | setting_id: UUID of an existing setting |
||
3647 | |||
3648 | Returns: |
||
3649 | The response. See :py:meth:`send_command` for details. |
||
3650 | """ |
||
3651 | cmd = XmlCommand("get_settings") |
||
3652 | |||
3653 | if not setting_id: |
||
3654 | raise RequiredArgument( |
||
3655 | function=self.get_setting.__name__, argument='setting_id' |
||
3656 | ) |
||
3657 | |||
3658 | cmd.set_attribute("setting_id", setting_id) |
||
3659 | return self._send_xml_command(cmd) |
||
3660 | |||
3661 | def get_system_reports( |
||
3662 | self, |
||
3663 | *, |
||
3664 | name: Optional[str] = None, |
||
3665 | duration: Optional[int] = None, |
||
3666 | start_time: Optional[str] = None, |
||
3667 | end_time: Optional[str] = None, |
||
3668 | brief: Optional[bool] = None, |
||
3669 | slave_id: Optional[str] = None, |
||
3670 | ) -> Any: |
||
3671 | """Request a list of system reports |
||
3672 | |||
3673 | Arguments: |
||
3674 | name: A string describing the required system report |
||
3675 | duration: The number of seconds into the past that the system report |
||
3676 | should include |
||
3677 | start_time: The start of the time interval the system report should |
||
3678 | include in ISO time format |
||
3679 | end_time: The end of the time interval the system report should |
||
3680 | include in ISO time format |
||
3681 | brief: Whether to include the actual system reports |
||
3682 | slave_id: UUID of GMP scanner from which to get the system reports |
||
3683 | |||
3684 | Returns: |
||
3685 | The response. See :py:meth:`send_command` for details. |
||
3686 | """ |
||
3687 | cmd = XmlCommand("get_system_reports") |
||
3688 | |||
3689 | if name: |
||
3690 | cmd.set_attribute("name", name) |
||
3691 | |||
3692 | if duration is not None: |
||
3693 | if not isinstance(duration, Integral): |
||
3694 | raise InvalidArgument("duration needs to be an integer number") |
||
3695 | |||
3696 | cmd.set_attribute("duration", str(duration)) |
||
3697 | |||
3698 | if start_time: |
||
3699 | cmd.set_attribute("start_time", str(start_time)) |
||
3700 | |||
3701 | if end_time: |
||
3702 | cmd.set_attribute("end_time", str(end_time)) |
||
3703 | |||
3704 | if brief is not None: |
||
3705 | cmd.set_attribute("brief", to_bool(brief)) |
||
3706 | |||
3707 | if slave_id: |
||
3708 | cmd.set_attribute("slave_id", slave_id) |
||
3709 | |||
3710 | return self._send_xml_command(cmd) |
||
3711 | |||
3712 | View Code Duplication | def get_tags( |
|
3713 | self, |
||
3714 | *, |
||
3715 | filter: Optional[str] = None, |
||
3716 | filter_id: Optional[str] = None, |
||
3717 | trash: Optional[bool] = None, |
||
3718 | names_only: Optional[bool] = None, |
||
3719 | ) -> Any: |
||
3720 | """Request a list of tags |
||
3721 | |||
3722 | Arguments: |
||
3723 | filter: Filter term to use for the query |
||
3724 | filter_id: UUID of an existing filter to use for the query |
||
3725 | trash: Whether to get tags from the trashcan instead |
||
3726 | names_only: Whether to get only distinct tag names |
||
3727 | |||
3728 | Returns: |
||
3729 | The response. See :py:meth:`send_command` for details. |
||
3730 | """ |
||
3731 | cmd = XmlCommand("get_tags") |
||
3732 | |||
3733 | add_filter(cmd, filter, filter_id) |
||
3734 | |||
3735 | if trash is not None: |
||
3736 | cmd.set_attribute("trash", to_bool(trash)) |
||
3737 | |||
3738 | if names_only is not None: |
||
3739 | cmd.set_attribute("names_only", to_bool(names_only)) |
||
3740 | |||
3741 | return self._send_xml_command(cmd) |
||
3742 | |||
3743 | def get_tag(self, tag_id: str) -> Any: |
||
3744 | """Request a single tag |
||
3745 | |||
3746 | Arguments: |
||
3747 | tag_id: UUID of an existing tag |
||
3748 | |||
3749 | Returns: |
||
3750 | The response. See :py:meth:`send_command` for details. |
||
3751 | """ |
||
3752 | cmd = XmlCommand("get_tags") |
||
3753 | |||
3754 | if not tag_id: |
||
3755 | raise RequiredArgument( |
||
3756 | function=self.get_tag.__name__, argument='tag_id' |
||
3757 | ) |
||
3758 | |||
3759 | cmd.set_attribute("tag_id", tag_id) |
||
3760 | return self._send_xml_command(cmd) |
||
3761 | |||
3762 | def get_users( |
||
3763 | self, *, filter: Optional[str] = None, filter_id: Optional[str] = None |
||
3764 | ) -> Any: |
||
3765 | """Request a list of users |
||
3766 | |||
3767 | Arguments: |
||
3768 | filter: Filter term to use for the query |
||
3769 | filter_id: UUID of an existing filter to use for the query |
||
3770 | |||
3771 | Returns: |
||
3772 | The response. See :py:meth:`send_command` for details. |
||
3773 | """ |
||
3774 | cmd = XmlCommand("get_users") |
||
3775 | |||
3776 | add_filter(cmd, filter, filter_id) |
||
3777 | |||
3778 | return self._send_xml_command(cmd) |
||
3779 | |||
3780 | def get_user(self, user_id: str) -> Any: |
||
3781 | """Request a single user |
||
3782 | |||
3783 | Arguments: |
||
3784 | user_id: UUID of an existing user |
||
3785 | |||
3786 | Returns: |
||
3787 | The response. See :py:meth:`send_command` for details. |
||
3788 | """ |
||
3789 | cmd = XmlCommand("get_users") |
||
3790 | |||
3791 | if not user_id: |
||
3792 | raise RequiredArgument( |
||
3793 | function=self.get_user.__name__, argument='user_id' |
||
3794 | ) |
||
3795 | |||
3796 | cmd.set_attribute("user_id", user_id) |
||
3797 | return self._send_xml_command(cmd) |
||
3798 | |||
3799 | def get_version(self) -> Any: |
||
3800 | """Get the Greenbone Manager Protocol version used by the remote gvmd |
||
3801 | |||
3802 | Returns: |
||
3803 | The response. See :py:meth:`send_command` for details. |
||
3804 | """ |
||
3805 | return self._send_xml_command(XmlCommand("get_version")) |
||
3806 | |||
3807 | def help( |
||
3808 | self, *, format: Optional[str] = None, help_type: Optional[str] = "" |
||
3809 | ) -> Any: |
||
3810 | """Get the help text |
||
3811 | |||
3812 | Arguments: |
||
3813 | format: One of "html", "rnc", "text" or "xml |
||
3814 | help_type: One of "brief" or "". Default "" |
||
3815 | |||
3816 | Returns: |
||
3817 | The response. See :py:meth:`send_command` for details. |
||
3818 | """ |
||
3819 | cmd = XmlCommand("help") |
||
3820 | |||
3821 | if help_type not in ("", "brief"): |
||
3822 | raise InvalidArgument( |
||
3823 | 'help_type argument must be an empty string or "brief"' |
||
3824 | ) |
||
3825 | |||
3826 | cmd.set_attribute("type", help_type) |
||
3827 | |||
3828 | if format: |
||
3829 | if not format.lower() in ("html", "rnc", "text", "xml"): |
||
3830 | raise InvalidArgument( |
||
3831 | "help format Argument must be one of html, rnc, text or " |
||
3832 | "xml" |
||
3833 | ) |
||
3834 | |||
3835 | cmd.set_attribute("format", format) |
||
3836 | |||
3837 | return self._send_xml_command(cmd) |
||
3838 | |||
3839 | def modify_auth(self, group_name: str, auth_conf_settings: dict) -> Any: |
||
3840 | """Modifies an existing auth. |
||
3841 | |||
3842 | Arguments: |
||
3843 | group_name: Name of the group to be modified. |
||
3844 | auth_conf_settings: The new auth config. |
||
3845 | |||
3846 | Returns: |
||
3847 | The response. See :py:meth:`send_command` for details. |
||
3848 | """ |
||
3849 | if not group_name: |
||
3850 | raise RequiredArgument( |
||
3851 | function=self.modify_auth.__name__, argument='group_name' |
||
3852 | ) |
||
3853 | if not auth_conf_settings: |
||
3854 | raise RequiredArgument( |
||
3855 | function=self.modify_auth.__name__, |
||
3856 | argument='auth_conf_settings', |
||
3857 | ) |
||
3858 | cmd = XmlCommand("modify_auth") |
||
3859 | _xmlgroup = cmd.add_element("group", attrs={"name": str(group_name)}) |
||
3860 | |||
3861 | for key, value in auth_conf_settings.items(): |
||
3862 | _xmlauthconf = _xmlgroup.add_element("auth_conf_setting") |
||
3863 | _xmlauthconf.add_element("key", key) |
||
3864 | _xmlauthconf.add_element("value", value) |
||
3865 | |||
3866 | return self._send_xml_command(cmd) |
||
3867 | |||
3868 | def modify_config_set_nvt_preference( |
||
3869 | self, |
||
3870 | config_id: str, |
||
3871 | name: str, |
||
3872 | nvt_oid: str, |
||
3873 | *, |
||
3874 | value: Optional[str] = None, |
||
3875 | ) -> Any: |
||
3876 | """Modifies the nvt preferences of an existing scan config. |
||
3877 | |||
3878 | Arguments: |
||
3879 | config_id: UUID of scan config to modify. |
||
3880 | name: Name for nvt preference to change. |
||
3881 | nvt_oid: OID of the NVT associated with preference to modify |
||
3882 | value: New value for the preference. None to delete the preference |
||
3883 | and to use the default instead. |
||
3884 | """ |
||
3885 | if not config_id: |
||
3886 | raise RequiredArgument( |
||
3887 | function=self.modify_config_set_nvt_preference.__name__, |
||
3888 | argument='config_id', |
||
3889 | ) |
||
3890 | |||
3891 | if not nvt_oid: |
||
3892 | raise RequiredArgument( |
||
3893 | function=self.modify_config_set_nvt_preference.__name__, |
||
3894 | argument='nvt_oid', |
||
3895 | ) |
||
3896 | |||
3897 | if not name: |
||
3898 | raise RequiredArgument( |
||
3899 | function=self.modify_config_set_nvt_preference.__name__, |
||
3900 | argument='name', |
||
3901 | ) |
||
3902 | |||
3903 | cmd = XmlCommand("modify_config") |
||
3904 | cmd.set_attribute("config_id", str(config_id)) |
||
3905 | |||
3906 | _xmlpref = cmd.add_element("preference") |
||
3907 | |||
3908 | _xmlpref.add_element("nvt", attrs={"oid": nvt_oid}) |
||
3909 | _xmlpref.add_element("name", name) |
||
3910 | |||
3911 | if value: |
||
3912 | _xmlpref.add_element("value", to_base64(value)) |
||
3913 | |||
3914 | return self._send_xml_command(cmd) |
||
3915 | |||
3916 | def modify_config_set_name(self, config_id: str, name: str) -> Any: |
||
3917 | """Modifies the name of an existing scan config |
||
3918 | |||
3919 | Arguments: |
||
3920 | config_id: UUID of scan config to modify. |
||
3921 | name: New name for the config. |
||
3922 | """ |
||
3923 | if not config_id: |
||
3924 | raise RequiredArgument( |
||
3925 | function=self.modify_config_set_name.__name__, |
||
3926 | argument='config_id', |
||
3927 | ) |
||
3928 | |||
3929 | if not name: |
||
3930 | raise RequiredArgument( |
||
3931 | function=self.modify_config_set_name.__name__, argument='name' |
||
3932 | ) |
||
3933 | |||
3934 | cmd = XmlCommand("modify_config") |
||
3935 | cmd.set_attribute("config_id", str(config_id)) |
||
3936 | |||
3937 | cmd.add_element("name", name) |
||
3938 | |||
3939 | return self._send_xml_command(cmd) |
||
3940 | |||
3941 | def modify_config_set_comment( |
||
3942 | self, config_id: str, comment: Optional[str] = "" |
||
3943 | ) -> Any: |
||
3944 | """Modifies the comment of an existing scan config |
||
3945 | |||
3946 | Arguments: |
||
3947 | config_id: UUID of scan config to modify. |
||
3948 | comment: Comment to set on a config. Default: '' |
||
3949 | """ |
||
3950 | if not config_id: |
||
3951 | raise RequiredArgument( |
||
3952 | function=self.modify_config_set_comment.__name__, |
||
3953 | argument='config_id argument', |
||
3954 | ) |
||
3955 | |||
3956 | cmd = XmlCommand("modify_config") |
||
3957 | cmd.set_attribute("config_id", str(config_id)) |
||
3958 | |||
3959 | cmd.add_element("comment", comment) |
||
3960 | |||
3961 | return self._send_xml_command(cmd) |
||
3962 | |||
3963 | def modify_config_set_scanner_preference( |
||
3964 | self, config_id: str, name: str, *, value: Optional[str] = None |
||
3965 | ) -> Any: |
||
3966 | """Modifies the scanner preferences of an existing scan config |
||
3967 | |||
3968 | Arguments: |
||
3969 | config_id: UUID of scan config to modify. |
||
3970 | name: Name of the scanner preference to change |
||
3971 | value: New value for the preference. None to delete the preference |
||
3972 | and to use the default instead. |
||
3973 | |||
3974 | """ |
||
3975 | if not config_id: |
||
3976 | raise RequiredArgument( |
||
3977 | function=self.modify_config_set_scanner_preference.__name__, |
||
3978 | argument='config_id', |
||
3979 | ) |
||
3980 | |||
3981 | if not name: |
||
3982 | raise RequiredArgument( |
||
3983 | function=self.modify_config_set_scanner_preference.__name__, |
||
3984 | argument='name argument', |
||
3985 | ) |
||
3986 | |||
3987 | cmd = XmlCommand("modify_config") |
||
3988 | cmd.set_attribute("config_id", str(config_id)) |
||
3989 | |||
3990 | _xmlpref = cmd.add_element("preference") |
||
3991 | |||
3992 | _xmlpref.add_element("name", name) |
||
3993 | |||
3994 | if value: |
||
3995 | _xmlpref.add_element("value", to_base64(value)) |
||
3996 | |||
3997 | return self._send_xml_command(cmd) |
||
3998 | |||
3999 | def modify_config_set_nvt_selection( |
||
4000 | self, config_id: str, family: str, nvt_oids: List[str] |
||
4001 | ) -> Any: |
||
4002 | """Modifies the selected nvts of an existing scan config |
||
4003 | |||
4004 | The manager updates the given family in the config to include only the |
||
4005 | given NVTs. |
||
4006 | |||
4007 | Arguments: |
||
4008 | config_id: UUID of scan config to modify. |
||
4009 | family: Name of the NVT family to include NVTs from |
||
4010 | nvt_oids: List of NVTs to select for the family. |
||
4011 | """ |
||
4012 | if not config_id: |
||
4013 | raise RequiredArgument( |
||
4014 | function=self.modify_config_set_nvt_selection.__name__, |
||
4015 | argument='config_id', |
||
4016 | ) |
||
4017 | |||
4018 | if not family: |
||
4019 | raise RequiredArgument( |
||
4020 | function=self.modify_config_set_nvt_selection.__name__, |
||
4021 | argument='family argument', |
||
4022 | ) |
||
4023 | |||
4024 | if not is_list_like(nvt_oids): |
||
4025 | raise InvalidArgumentType( |
||
4026 | function=self.modify_config_set_nvt_selection.__name__, |
||
4027 | argument='nvt_oids', |
||
4028 | arg_type='list', |
||
4029 | ) |
||
4030 | |||
4031 | cmd = XmlCommand("modify_config") |
||
4032 | cmd.set_attribute("config_id", str(config_id)) |
||
4033 | |||
4034 | _xmlnvtsel = cmd.add_element("nvt_selection") |
||
4035 | _xmlnvtsel.add_element("family", family) |
||
4036 | |||
4037 | for nvt in nvt_oids: |
||
4038 | _xmlnvtsel.add_element("nvt", attrs={"oid": nvt}) |
||
4039 | |||
4040 | return self._send_xml_command(cmd) |
||
4041 | |||
4042 | def modify_config_set_family_selection( |
||
4043 | self, |
||
4044 | config_id: str, |
||
4045 | families: List[Tuple[str, bool, bool]], |
||
4046 | *, |
||
4047 | auto_add_new_families: Optional[bool] = True, |
||
4048 | ) -> Any: |
||
4049 | """ |
||
4050 | Selected the NVTs of a scan config at a family level. |
||
4051 | |||
4052 | Arguments: |
||
4053 | config_id: UUID of scan config to modify. |
||
4054 | families: A list of tuples (str, bool, bool): |
||
4055 | str: the name of the NVT family selected, |
||
4056 | bool: add new NVTs to the family automatically, |
||
4057 | bool: include all NVTs from the family |
||
4058 | auto_add_new_families: Whether new families should be added to the |
||
4059 | scan config automatically. Default: True. |
||
4060 | """ |
||
4061 | if not config_id: |
||
4062 | raise RequiredArgument( |
||
4063 | function=self.modify_config_set_family_selection.__name__, |
||
4064 | argument='config_id', |
||
4065 | ) |
||
4066 | |||
4067 | if not is_list_like(families): |
||
4068 | raise InvalidArgumentType( |
||
4069 | function=self.modify_config_set_family_selection.__name__, |
||
4070 | argument='families', |
||
4071 | arg_type='list', |
||
4072 | ) |
||
4073 | |||
4074 | cmd = XmlCommand("modify_config") |
||
4075 | cmd.set_attribute("config_id", str(config_id)) |
||
4076 | |||
4077 | _xmlfamsel = cmd.add_element("family_selection") |
||
4078 | _xmlfamsel.add_element("growing", to_bool(auto_add_new_families)) |
||
4079 | |||
4080 | for family in families: |
||
4081 | _xmlfamily = _xmlfamsel.add_element("family") |
||
4082 | _xmlfamily.add_element("name", family[0]) |
||
4083 | |||
4084 | if len(family) != 3: |
||
4085 | raise InvalidArgument( |
||
4086 | "Family must be a tuple of 3. (str, bool, bool)" |
||
4087 | ) |
||
4088 | |||
4089 | if not isinstance(family[1], bool) or not isinstance( |
||
4090 | family[2], bool |
||
4091 | ): |
||
4092 | raise InvalidArgumentType( |
||
4093 | function=self.modify_config_set_family_selection.__name__, |
||
4094 | argument='families', |
||
4095 | arg_type='[tuple(str, bool, bool)]', |
||
4096 | ) |
||
4097 | |||
4098 | _xmlfamily.add_element("all", to_bool(family[2])) |
||
4099 | _xmlfamily.add_element("growing", to_bool(family[1])) |
||
4100 | |||
4101 | return self._send_xml_command(cmd) |
||
4102 | |||
4103 | def modify_config( |
||
4104 | self, config_id: str, selection: Optional[str] = None, **kwargs |
||
4105 | ) -> Any: |
||
4106 | """Modifies an existing scan config. |
||
4107 | |||
4108 | DEPRECATED. Please use *modify_config_set_* methods instead. |
||
4109 | |||
4110 | modify_config has four modes to operate depending on the selection. |
||
4111 | |||
4112 | Arguments: |
||
4113 | config_id: UUID of scan config to modify. |
||
4114 | selection: one of 'scan_pref', 'nvt_pref', 'nvt_selection' or |
||
4115 | 'family_selection' |
||
4116 | name: New name for preference. |
||
4117 | value: New value for preference. |
||
4118 | nvt_oids: List of NVTs associated with preference to modify. |
||
4119 | family: Name of family to modify. |
||
4120 | |||
4121 | Returns: |
||
4122 | The response. See :py:meth:`send_command` for details. |
||
4123 | """ |
||
4124 | if not config_id: |
||
4125 | raise RequiredArgument( |
||
4126 | function=self.modify_config.__name__, |
||
4127 | argument='config_id argument', |
||
4128 | ) |
||
4129 | |||
4130 | if selection is None: |
||
4131 | deprecation( |
||
4132 | "Using modify_config to update the comment of a scan config is" |
||
4133 | "deprecated. Please use modify_config_set_comment instead." |
||
4134 | ) |
||
4135 | return self.modify_config_set_comment( |
||
4136 | config_id, kwargs.get("comment") |
||
4137 | ) |
||
4138 | |||
4139 | if selection not in ( |
||
4140 | "nvt_pref", |
||
4141 | "scan_pref", |
||
4142 | "family_selection", |
||
4143 | "nvt_selection", |
||
4144 | ): |
||
4145 | raise InvalidArgument( |
||
4146 | "selection must be one of nvt_pref, " |
||
4147 | "scan_pref, family_selection or " |
||
4148 | "nvt_selection" |
||
4149 | ) |
||
4150 | |||
4151 | if selection == "nvt_pref": |
||
4152 | deprecation( |
||
4153 | "Using modify_config to update a nvt preference of a scan " |
||
4154 | "config is deprecated. Please use " |
||
4155 | "modify_config_set_nvt_preference instead." |
||
4156 | ) |
||
4157 | return self.modify_config_set_nvt_preference(config_id, **kwargs) |
||
4158 | |||
4159 | if selection == "scan_pref": |
||
4160 | deprecation( |
||
4161 | "Using modify_config to update a scanner preference of a " |
||
4162 | "scan config is deprecated. Please use " |
||
4163 | "modify_config_set_scanner_preference instead." |
||
4164 | ) |
||
4165 | return self.modify_config_set_scanner_preference( |
||
4166 | config_id, **kwargs |
||
4167 | ) |
||
4168 | |||
4169 | if selection == "nvt_selection": |
||
4170 | deprecation( |
||
4171 | "Using modify_config to update a nvt selection of a " |
||
4172 | "scan config is deprecated. Please use " |
||
4173 | "modify_config_set_nvt_selection instead." |
||
4174 | ) |
||
4175 | return self.modify_config_set_nvt_selection(config_id, **kwargs) |
||
4176 | |||
4177 | deprecation( |
||
4178 | "Using modify_config to update a family selection of a " |
||
4179 | "scan config is deprecated. Please use " |
||
4180 | "modify_config_set_family_selection instead." |
||
4181 | ) |
||
4182 | return self.modify_config_set_family_selection(config_id, **kwargs) |
||
4183 | |||
4184 | View Code Duplication | def modify_group( |
|
4185 | self, |
||
4186 | group_id: str, |
||
4187 | *, |
||
4188 | comment: Optional[str] = None, |
||
4189 | name: Optional[str] = None, |
||
4190 | users: Optional[List[str]] = None, |
||
4191 | ) -> Any: |
||
4192 | """Modifies an existing group. |
||
4193 | |||
4194 | Arguments: |
||
4195 | group_id: UUID of group to modify. |
||
4196 | comment: Comment on group. |
||
4197 | name: Name of group. |
||
4198 | users: List of user names to be in the group |
||
4199 | |||
4200 | Returns: |
||
4201 | The response. See :py:meth:`send_command` for details. |
||
4202 | """ |
||
4203 | if not group_id: |
||
4204 | raise RequiredArgument( |
||
4205 | function=self.modify_group.__name__, argument='group_id' |
||
4206 | ) |
||
4207 | |||
4208 | cmd = XmlCommand("modify_group") |
||
4209 | cmd.set_attribute("group_id", group_id) |
||
4210 | |||
4211 | if comment: |
||
4212 | cmd.add_element("comment", comment) |
||
4213 | |||
4214 | if name: |
||
4215 | cmd.add_element("name", name) |
||
4216 | |||
4217 | if users: |
||
4218 | cmd.add_element("users", to_comma_list(users)) |
||
4219 | |||
4220 | return self._send_xml_command(cmd) |
||
4221 | |||
4222 | def modify_report_format( |
||
4223 | self, |
||
4224 | report_format_id: Optional[Union[str, ReportFormatType]] = None, |
||
4225 | *, |
||
4226 | active: Optional[bool] = None, |
||
4227 | name: Optional[str] = None, |
||
4228 | summary: Optional[str] = None, |
||
4229 | param_name: Optional[str] = None, |
||
4230 | param_value: Optional[str] = None, |
||
4231 | ) -> Any: |
||
4232 | """Modifies an existing report format. |
||
4233 | |||
4234 | Arguments: |
||
4235 | report_format_id: UUID of report format to modify |
||
4236 | or ReportFormatType (enum) |
||
4237 | active: Whether the report format is active. |
||
4238 | name: The name of the report format. |
||
4239 | summary: A summary of the report format. |
||
4240 | param_name: The name of the param. |
||
4241 | param_value: The value of the param. |
||
4242 | |||
4243 | Returns: |
||
4244 | The response. See :py:meth:`send_command` for details. |
||
4245 | """ |
||
4246 | if not report_format_id: |
||
4247 | raise RequiredArgument( |
||
4248 | function=self.modify_report_format.__name__, |
||
4249 | argument='report_format_id ', |
||
4250 | ) |
||
4251 | |||
4252 | cmd = XmlCommand("modify_report_format") |
||
4253 | |||
4254 | if isinstance(report_format_id, ReportFormatType): |
||
4255 | report_format_id = report_format_id.value |
||
4256 | |||
4257 | cmd.set_attribute("report_format_id", report_format_id) |
||
4258 | |||
4259 | if active is not None: |
||
4260 | cmd.add_element("active", to_bool(active)) |
||
4261 | |||
4262 | if name: |
||
4263 | cmd.add_element("name", name) |
||
4264 | |||
4265 | if summary: |
||
4266 | cmd.add_element("summary", summary) |
||
4267 | |||
4268 | if param_name: |
||
4269 | _xmlparam = cmd.add_element("param") |
||
4270 | _xmlparam.add_element("name", param_name) |
||
4271 | |||
4272 | if param_value is not None: |
||
4273 | _xmlparam.add_element("value", param_value) |
||
4274 | |||
4275 | return self._send_xml_command(cmd) |
||
4276 | |||
4277 | View Code Duplication | def modify_role( |
|
4278 | self, |
||
4279 | role_id: str, |
||
4280 | *, |
||
4281 | comment: Optional[str] = None, |
||
4282 | name: Optional[str] = None, |
||
4283 | users: Optional[List[str]] = None, |
||
4284 | ) -> Any: |
||
4285 | """Modifies an existing role. |
||
4286 | |||
4287 | Arguments: |
||
4288 | role_id: UUID of role to modify. |
||
4289 | comment: Name of role. |
||
4290 | name: Comment on role. |
||
4291 | users: List of user names. |
||
4292 | |||
4293 | Returns: |
||
4294 | The response. See :py:meth:`send_command` for details. |
||
4295 | """ |
||
4296 | if not role_id: |
||
4297 | raise RequiredArgument( |
||
4298 | function=self.modify_role.__name__, argument='role_id argument' |
||
4299 | ) |
||
4300 | |||
4301 | cmd = XmlCommand("modify_role") |
||
4302 | cmd.set_attribute("role_id", role_id) |
||
4303 | |||
4304 | if comment: |
||
4305 | cmd.add_element("comment", comment) |
||
4306 | |||
4307 | if name: |
||
4308 | cmd.add_element("name", name) |
||
4309 | |||
4310 | if users: |
||
4311 | cmd.add_element("users", to_comma_list(users)) |
||
4312 | |||
4313 | return self._send_xml_command(cmd) |
||
4314 | |||
4315 | def modify_scanner( |
||
4316 | self, |
||
4317 | scanner_id: str, |
||
4318 | *, |
||
4319 | scanner_type: Optional[ScannerType] = None, |
||
4320 | host: Optional[str] = None, |
||
4321 | port: Optional[int] = None, |
||
4322 | comment: Optional[str] = None, |
||
4323 | name: Optional[str] = None, |
||
4324 | ca_pub: Optional[str] = None, |
||
4325 | credential_id: Optional[str] = None, |
||
4326 | ) -> Any: |
||
4327 | """Modifies an existing scanner. |
||
4328 | |||
4329 | Arguments: |
||
4330 | scanner_id: UUID of scanner to modify. |
||
4331 | scanner_type: New type of the Scanner. |
||
4332 | host: Host of the scanner. |
||
4333 | port: Port of the scanner. |
||
4334 | comment: Comment on scanner. |
||
4335 | name: Name of scanner. |
||
4336 | ca_pub: Certificate of CA to verify scanner's certificate. |
||
4337 | credential_id: UUID of the client certificate credential for the |
||
4338 | Scanner. |
||
4339 | |||
4340 | Returns: |
||
4341 | The response. See :py:meth:`send_command` for details. |
||
4342 | """ |
||
4343 | if not scanner_id: |
||
4344 | raise RequiredArgument( |
||
4345 | function=self.modify_scanner.__name__, |
||
4346 | argument='scanner_id argument', |
||
4347 | ) |
||
4348 | |||
4349 | cmd = XmlCommand("modify_scanner") |
||
4350 | cmd.set_attribute("scanner_id", scanner_id) |
||
4351 | |||
4352 | if scanner_type is not None: |
||
4353 | if not isinstance(scanner_type, self.types.ScannerType): |
||
4354 | raise InvalidArgumentType( |
||
4355 | function=self.modify_scanner.__name__, |
||
4356 | argument='scanner_type', |
||
4357 | arg_type=self.types.ScannerType.__name__, |
||
4358 | ) |
||
4359 | |||
4360 | cmd.add_element("type", scanner_type.value) |
||
4361 | |||
4362 | if host: |
||
4363 | cmd.add_element("host", host) |
||
4364 | |||
4365 | if port: |
||
4366 | cmd.add_element("port", str(port)) |
||
4367 | |||
4368 | if comment: |
||
4369 | cmd.add_element("comment", comment) |
||
4370 | |||
4371 | if name: |
||
4372 | cmd.add_element("name", name) |
||
4373 | |||
4374 | if ca_pub: |
||
4375 | cmd.add_element("ca_pub", ca_pub) |
||
4376 | |||
4377 | if credential_id: |
||
4378 | cmd.add_element("credential", attrs={"id": str(credential_id)}) |
||
4379 | |||
4380 | return self._send_xml_command(cmd) |
||
4381 | |||
4382 | def modify_setting( |
||
4383 | self, |
||
4384 | setting_id: Optional[str] = None, |
||
4385 | name: Optional[str] = None, |
||
4386 | value: Optional[str] = None, |
||
4387 | ) -> Any: |
||
4388 | """Modifies an existing setting. |
||
4389 | |||
4390 | Arguments: |
||
4391 | setting_id: UUID of the setting to be changed. |
||
4392 | name: The name of the setting. Either setting_id or name must be |
||
4393 | passed. |
||
4394 | value: The value of the setting. |
||
4395 | |||
4396 | Returns: |
||
4397 | The response. See :py:meth:`send_command` for details. |
||
4398 | """ |
||
4399 | if not setting_id and not name: |
||
4400 | raise RequiredArgument( |
||
4401 | function=self.modify_setting.__name__, |
||
4402 | argument='setting_id or name argument', |
||
4403 | ) |
||
4404 | |||
4405 | if value is None: |
||
4406 | raise RequiredArgument( |
||
4407 | function=self.modify_setting.__name__, argument='value argument' |
||
4408 | ) |
||
4409 | |||
4410 | cmd = XmlCommand("modify_setting") |
||
4411 | |||
4412 | if setting_id: |
||
4413 | cmd.set_attribute("setting_id", setting_id) |
||
4414 | else: |
||
4415 | cmd.add_element("name", name) |
||
4416 | |||
4417 | cmd.add_element("value", to_base64(value)) |
||
4418 | |||
4419 | return self._send_xml_command(cmd) |
||
4420 | |||
4421 | View Code Duplication | def modify_user( |
|
4422 | self, |
||
4423 | user_id: str = None, |
||
4424 | name: str = None, |
||
4425 | *, |
||
4426 | new_name: Optional[str] = None, |
||
4427 | comment: Optional[str] = None, |
||
4428 | password: Optional[str] = None, |
||
4429 | auth_source: Optional[UserAuthType] = None, |
||
4430 | role_ids: Optional[List[str]] = None, |
||
4431 | hosts: Optional[List[str]] = None, |
||
4432 | hosts_allow: Optional[bool] = False, |
||
4433 | ifaces: Optional[List[str]] = None, |
||
4434 | ifaces_allow: Optional[bool] = False, |
||
4435 | group_ids: Optional[List[str]] = None, |
||
4436 | ) -> Any: |
||
4437 | """Modifies an existing user. Most of the fields need to be supplied |
||
4438 | for changing a single field even if no change is wanted for those. |
||
4439 | Else empty values are inserted for the missing fields instead. |
||
4440 | Arguments: |
||
4441 | user_id: UUID of the user to be modified. Overrides name element |
||
4442 | argument. |
||
4443 | name: The name of the user to be modified. Either user_id or name |
||
4444 | must be passed. |
||
4445 | new_name: The new name for the user. |
||
4446 | comment: Comment on the user. |
||
4447 | password: The password for the user. |
||
4448 | auth_source: Source allowed for authentication for this user. |
||
4449 | roles_id: List of roles UUIDs for the user. |
||
4450 | hosts: User access rules: List of hosts. |
||
4451 | hosts_allow: Defines how the hosts list is to be interpreted. |
||
4452 | If False (default) the list is treated as a deny list. |
||
4453 | All hosts are allowed by default except those provided by |
||
4454 | the hosts parameter. If True the list is treated as a |
||
4455 | allow list. All hosts are denied by default except those |
||
4456 | provided by the hosts parameter. |
||
4457 | ifaces: User access rules: List of ifaces. |
||
4458 | ifaces_allow: Defines how the ifaces list is to be interpreted. |
||
4459 | If False (default) the list is treated as a deny list. |
||
4460 | All ifaces are allowed by default except those provided by |
||
4461 | the ifaces parameter. If True the list is treated as a |
||
4462 | allow list. All ifaces are denied by default except those |
||
4463 | provided by the ifaces parameter. |
||
4464 | group_ids: List of group UUIDs for the user. |
||
4465 | |||
4466 | Returns: |
||
4467 | The response. See :py:meth:`send_command` for details. |
||
4468 | """ |
||
4469 | if not user_id and not name: |
||
4470 | raise RequiredArgument( |
||
4471 | function=self.modify_user.__name__, argument='user_id or name' |
||
4472 | ) |
||
4473 | |||
4474 | cmd = XmlCommand("modify_user") |
||
4475 | |||
4476 | if user_id: |
||
4477 | cmd.set_attribute("user_id", user_id) |
||
4478 | else: |
||
4479 | cmd.add_element("name", name) |
||
4480 | |||
4481 | if new_name: |
||
4482 | cmd.add_element("new_name", new_name) |
||
4483 | |||
4484 | if role_ids: |
||
4485 | for role in role_ids: |
||
4486 | cmd.add_element("role", attrs={"id": role}) |
||
4487 | |||
4488 | if hosts: |
||
4489 | cmd.add_element( |
||
4490 | "hosts", |
||
4491 | to_comma_list(hosts), |
||
4492 | attrs={"allow": to_bool(hosts_allow)}, |
||
4493 | ) |
||
4494 | |||
4495 | if ifaces: |
||
4496 | cmd.add_element( |
||
4497 | "ifaces", |
||
4498 | to_comma_list(ifaces), |
||
4499 | attrs={"allow": to_bool(ifaces_allow)}, |
||
4500 | ) |
||
4501 | |||
4502 | if comment: |
||
4503 | cmd.add_element("comment", comment) |
||
4504 | |||
4505 | if password: |
||
4506 | cmd.add_element("password", password) |
||
4507 | |||
4508 | if auth_source: |
||
4509 | _xmlauthsrc = cmd.add_element("sources") |
||
4510 | _xmlauthsrc.add_element("source", auth_source.value) |
||
4511 | |||
4512 | if group_ids: |
||
4513 | _xmlgroups = cmd.add_element("groups") |
||
4514 | for group_id in group_ids: |
||
4515 | _xmlgroups.add_element("group", attrs={"id": group_id}) |
||
4516 | |||
4517 | return self._send_xml_command(cmd) |
||
4518 | |||
4519 | def restore(self, entity_id: str) -> Any: |
||
4520 | """Restore an entity from the trashcan |
||
4521 | |||
4522 | Arguments: |
||
4523 | entity_id: ID of the entity to be restored from the trashcan |
||
4524 | |||
4525 | Returns: |
||
4526 | The response. See :py:meth:`send_command` for details. |
||
4527 | """ |
||
4528 | if not entity_id: |
||
4529 | raise RequiredArgument( |
||
4530 | function=self.restore.__name__, argument='entity_id' |
||
4531 | ) |
||
4532 | |||
4533 | cmd = XmlCommand("restore") |
||
4534 | cmd.set_attribute("id", entity_id) |
||
4535 | |||
4536 | return self._send_xml_command(cmd) |
||
4537 | |||
4538 | def sync_cert(self) -> Any: |
||
4539 | """Request a synchronization with the CERT feed service |
||
4540 | |||
4541 | Returns: |
||
4542 | The response. See :py:meth:`send_command` for details. |
||
4543 | """ |
||
4544 | return self._send_xml_command(XmlCommand("sync_cert")) |
||
4545 | |||
4546 | def sync_config(self) -> Any: |
||
4547 | """Request an OSP config synchronization with scanner |
||
4548 | |||
4549 | Returns: |
||
4550 | The response. See :py:meth:`send_command` for details. |
||
4551 | """ |
||
4552 | return self._send_xml_command(XmlCommand("sync_config")) |
||
4553 | |||
4554 | def sync_feed(self) -> Any: |
||
4555 | """Request a synchronization with the NVT feed service |
||
4556 | |||
4557 | Returns: |
||
4558 | The response. See :py:meth:`send_command` for details. |
||
4559 | """ |
||
4560 | return self._send_xml_command(XmlCommand("sync_feed")) |
||
4561 | |||
4562 | def sync_scap(self) -> Any: |
||
4563 | """Request a synchronization with the SCAP feed service |
||
4564 | |||
4565 | Returns: |
||
4566 | The response. See :py:meth:`send_command` for details. |
||
4567 | """ |
||
4568 | return self._send_xml_command(XmlCommand("sync_scap")) |
||
4569 | |||
4570 | View Code Duplication | def verify_report_format( |
|
4571 | self, report_format_id: Union[str, ReportFormatType] |
||
4572 | ) -> Any: |
||
4573 | """Verify an existing report format |
||
4574 | |||
4575 | Verifies the trust level of an existing report format. It will be |
||
4576 | checked whether the signature of the report format currently matches the |
||
4577 | report format. This includes the script and files used to generate |
||
4578 | reports of this format. It is *not* verified if the report format works |
||
4579 | as expected by the user. |
||
4580 | |||
4581 | Arguments: |
||
4582 | report_format_id: UUID of the report format to be verified |
||
4583 | or ReportFormatType (enum) |
||
4584 | |||
4585 | Returns: |
||
4586 | The response. See :py:meth:`send_command` for details. |
||
4587 | """ |
||
4588 | if not report_format_id: |
||
4589 | raise RequiredArgument( |
||
4590 | function=self.verify_report_format.__name__, |
||
4591 | argument='report_format_id', |
||
4592 | ) |
||
4593 | |||
4594 | cmd = XmlCommand("verify_report_format") |
||
4595 | |||
4596 | if isinstance(report_format_id, ReportFormatType): |
||
4597 | report_format_id = report_format_id.value |
||
4598 | |||
4599 | cmd.set_attribute("report_format_id", report_format_id) |
||
4600 | |||
4601 | return self._send_xml_command(cmd) |
||
4602 | |||
4603 | def verify_scanner(self, scanner_id: str) -> Any: |
||
4604 | """Verify an existing scanner |
||
4605 | |||
4606 | Verifies if it is possible to connect to an existing scanner. It is |
||
4607 | *not* verified if the scanner works as expected by the user. |
||
4608 | |||
4609 | Arguments: |
||
4610 | scanner_id: UUID of the scanner to be verified |
||
4611 | |||
4612 | Returns: |
||
4613 | The response. See :py:meth:`send_command` for details. |
||
4614 | """ |
||
4615 | if not scanner_id: |
||
4616 | raise RequiredArgument( |
||
4617 | function=self.verify_scanner.__name__, argument='scanner_id' |
||
4618 | ) |
||
4619 | |||
4620 | cmd = XmlCommand("verify_scanner") |
||
4621 | cmd.set_attribute("scanner_id", scanner_id) |
||
4622 | |||
4623 | return self._send_xml_command(cmd) |
||
4624 |