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