Completed
Push — master ( a9e580...460381 )
by Jaspar
18s queued 16s
created

GmpV208Mixin.modify_ticket()   C

Complexity

Conditions 10

Size

Total Lines 60
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 34
nop 7
dl 0
loc 60
rs 5.9999
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like gvm.protocols.gmpv208.gmpv208.GmpV208Mixin.modify_ticket() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
# pylint: disable=arguments-differ, redefined-builtin, too-many-lines
20
21
"""
22
Module for communication with gvmd in
23
`Greenbone Management Protocol version 20.08`_
24
25
.. _Greenbone Management Protocol version 20.08:
26
    https://docs.greenbone.net/API/GMP/gmp-20.08.html
27
"""
28
import collections
29
import logging
30
import numbers
31
32
from typing import Any, List, Optional, Callable, Union, Tuple
33
from lxml import etree
34
35
from gvm.connections import GvmConnection
36
from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument
37
from gvm.protocols.base import GvmProtocol
38
from gvm.protocols.gmpv208.entities.report_formats import (
39
    ReportFormatType,
40
)
41
from gvm.utils import (
42
    deprecation,
43
    check_command_status,
44
    is_list_like,
45
    to_base64,
46
    to_bool,
47
    to_comma_list,
48
    add_filter,
49
)
50
from gvm.xml import XmlCommand
51
52
from . import types
53
from .types import *  # pylint: disable=unused-wildcard-import, wildcard-import
54
from .types import _UsageType as UsageType
55
56
_EMPTY_POLICY_ID = '085569ce-73ed-11df-83c3-002264764cea'
57
58
PROTOCOL_VERSION = (20, 8)
59
60
61
logger = logging.getLogger(__name__)
62
63
Severity = numbers.Real
64
65
# put this into the Alert Entity
66
def _check_event(
67
    event: AlertEvent, condition: AlertCondition, method: AlertMethod
68
):
69
    if event == AlertEvent.TASK_RUN_STATUS_CHANGED:
70
        if not condition:
71
            raise RequiredArgument(
72
                "condition is required for event {}".format(event.name)
73
            )
74
75
        if not method:
76
            raise RequiredArgument(
77
                "method is required for event {}".format(event.name)
78
            )
79
80
        if condition not in (
81
            AlertCondition.ALWAYS,
82
            AlertCondition.FILTER_COUNT_CHANGED,
83
            AlertCondition.FILTER_COUNT_AT_LEAST,
84
            AlertCondition.SEVERITY_AT_LEAST,
85
            AlertCondition.SEVERITY_CHANGED,
86
        ):
87
            raise InvalidArgument(
88
                "Invalid condition {} for event {}".format(
89
                    condition.name, event.name
90
                )
91
            )
92
    elif event in (
93
        AlertEvent.NEW_SECINFO_ARRIVED,
94
        AlertEvent.UPDATED_SECINFO_ARRIVED,
95
    ):
96
        if not condition:
97
            raise RequiredArgument(
98
                "condition is required for event {}".format(event.name)
99
            )
100
101
        if not method:
102
            raise RequiredArgument(
103
                "method is required for event {}".format(event.name)
104
            )
105
106
        if condition != AlertCondition.ALWAYS:
107
            raise InvalidArgument(
108
                "Invalid condition {} for event {}".format(
109
                    condition.name, event.name
110
                )
111
            )
112
        if method not in (
113
            AlertMethod.SCP,
114
            AlertMethod.SEND,
115
            AlertMethod.SMB,
116
            AlertMethod.SNMP,
117
            AlertMethod.SYSLOG,
118
            AlertMethod.EMAIL,
119
        ):
120
            raise InvalidArgument(
121
                "Invalid method {} for event {}".format(method.name, event.name)
122
            )
123
    elif event in (
124
        AlertEvent.TICKET_RECEIVED,
125
        AlertEvent.OWNED_TICKET_CHANGED,
126
        AlertEvent.ASSIGNED_TICKET_CHANGED,
127
    ):
128
        if not condition:
129
            raise RequiredArgument(
130
                "condition is required for event {}".format(event.name)
131
            )
132
133
        if not method:
134
            raise RequiredArgument(
135
                "method is required for event {}".format(event.name)
136
            )
137
        if condition != AlertCondition.ALWAYS:
138
            raise InvalidArgument(
139
                "Invalid condition {} for event {}".format(
140
                    condition.name, event.name
141
                )
142
            )
143
        if method not in (
144
            AlertMethod.EMAIL,
145
            AlertMethod.START_TASK,
146
            AlertMethod.SYSLOG,
147
        ):
148
            raise InvalidArgument(
149
                "Invalid method {} for event {}".format(method.name, event.name)
150
            )
151
    elif event is not None:
152
        raise InvalidArgument('Invalid event "{}"'.format(event.name))
153
154
155
class GmpV208Mixin(GvmProtocol):
156
    """Python interface for Greenbone Management Protocol
157
158
    This class implements the `Greenbone Management Protocol version 20.08`_
159
160
    Arguments:
161
        connection: Connection to use to talk with the gvmd daemon. See
162
            :mod:`gvm.connections` for possible connection types.
163
        transform: Optional transform `callable`_ to convert response data.
164
            After each request the callable gets passed the plain response data
165
            which can be used to check the data and/or conversion into different
166
            representations like a xml dom.
167
168
            See :mod:`gvm.transforms` for existing transforms.
169
170
    .. _Greenbone Management Protocol version 20.08:
171
        https://docs.greenbone.net/API/GMP/gmp-20.08.html
172
    .. _callable:
173
        https://docs.python.org/3/library/functions.html#callable
174
    """
175
176
    types = types
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable types does not seem to be defined.
Loading history...
177
178
    def __init__(
179
        self,
180
        connection: GvmConnection,
181
        *,
182
        transform: Optional[Callable[[str], Any]] = None,
183
    ):
184
        super().__init__(connection, transform=transform)
185
186
        # Is authenticated on gvmd
187
        self._authenticated = False
188
189
    def is_authenticated(self) -> bool:
190
        """Checks if the user is authenticated
191
192
        If the user is authenticated privileged GMP commands like get_tasks
193
        may be send to gvmd.
194
195
        Returns:
196
            bool: True if an authenticated connection to gvmd has been
197
            established.
198
        """
199
        return self._authenticated
200
201
    def authenticate(self, username: str, password: str) -> Any:
202
        """Authenticate to gvmd.
203
204
        The generated authenticate command will be send to server.
205
        Afterwards the response is read, transformed and returned.
206
207
        Arguments:
208
            username: Username
209
            password: Password
210
211
        Returns:
212
            Transformed response from server.
213
        """
214
        cmd = XmlCommand("authenticate")
215
216
        if not username:
217
            raise RequiredArgument(
218
                function=self.authenticate.__name__, argument='username'
219
            )
220
221
        if not password:
222
            raise RequiredArgument(
223
                function=self.authenticate.__name__, argument='password'
224
            )
225
226
        credentials = cmd.add_element("credentials")
227
        credentials.add_element("username", username)
228
        credentials.add_element("password", password)
229
230
        self._send(cmd.to_string())
231
        response = self._read()
232
233
        if check_command_status(response):
234
            self._authenticated = True
235
236
        return self._transform(response)
237
238
    def create_alert(
239
        self,
240
        name: str,
241
        condition: AlertCondition,
242
        event: AlertEvent,
243
        method: AlertMethod,
244
        *,
245
        method_data: Optional[dict] = None,
246
        event_data: Optional[dict] = None,
247
        condition_data: Optional[dict] = None,
248
        filter_id: Optional[int] = None,
249
        comment: Optional[str] = None,
250
    ) -> Any:
251
        """Create a new alert
252
253
        Arguments:
254
            name: Name of the new Alert
255
            condition: The condition that must be satisfied for the alert
256
                to occur; if the event is either 'Updated SecInfo arrived' or
257
                'New SecInfo arrived', condition must be 'Always'. Otherwise,
258
                condition can also be on of 'Severity at least', 'Filter count
259
                changed' or 'Filter count at least'.
260
            event: The event that must happen for the alert to occur, one
261
                of 'Task run status changed', 'Updated SecInfo arrived' or 'New
262
                SecInfo arrived'
263
            method: The method by which the user is alerted, one of 'SCP',
264
                'Send', 'SMB', 'SNMP', 'Syslog' or 'Email'; if the event is
265
                neither 'Updated SecInfo arrived' nor 'New SecInfo arrived',
266
                method can also be one of 'Start Task', 'HTTP Get', 'Sourcefire
267
                Connector' or 'verinice Connector'.
268
            condition_data: Data that defines the condition
269
            event_data: Data that defines the event
270
            method_data: Data that defines the method
271
            filter_id: Filter to apply when executing alert
272
            comment: Comment for the alert
273
274
        Returns:
275
            The response. See :py:meth:`send_command` for details.
276
        """
277
        if not name:
278
            raise RequiredArgument(
279
                function=self.create_alert.__name__, argument='name'
280
            )
281
282
        if not condition:
283
            raise RequiredArgument(
284
                function=self.create_alert.__name__, argument='condition'
285
            )
286
287
        if not event:
288
            raise RequiredArgument(
289
                function=self.create_alert.__name__, argument='event'
290
            )
291
292
        if not method:
293
            raise RequiredArgument(
294
                function=self.create_alert.__name__, argument='method'
295
            )
296
297
        if not isinstance(condition, AlertCondition):
298
            raise InvalidArgumentType(
299
                function=self.create_alert.__name__,
300
                argument='condition',
301
                arg_type=AlertCondition.__name__,
302
            )
303
304
        if not isinstance(event, AlertEvent):
305
            raise InvalidArgumentType(
306
                function=self.create_alert.__name__,
307
                argument='even',
308
                arg_type=AlertEvent.__name__,
309
            )
310
311
        if not isinstance(method, AlertMethod):
312
            raise InvalidArgumentType(
313
                function=self.create_alert.__name__,
314
                argument='method',
315
                arg_type=AlertMethod.__name__,
316
            )
317
318
        _check_event(event, condition, method)
319
320
        cmd = XmlCommand("create_alert")
321
        cmd.add_element("name", name)
322
323
        conditions = cmd.add_element("condition", condition.value)
324
325
        if condition_data is not None:
326
            for key, value in condition_data.items():
327
                _data = conditions.add_element("data", value)
328
                _data.add_element("name", key)
329
330
        events = cmd.add_element("event", event.value)
331
332
        if event_data is not None:
333
            for key, value in event_data.items():
334
                _data = events.add_element("data", value)
335
                _data.add_element("name", key)
336
337
        methods = cmd.add_element("method", method.value)
338
339
        if method_data is not None:
340
            for key, value in method_data.items():
341
                _data = methods.add_element("data", value)
342
                _data.add_element("name", key)
343
344
        if filter_id:
345
            cmd.add_element("filter", attrs={"id": filter_id})
346
347
        if comment:
348
            cmd.add_element("comment", comment)
349
350
        return self._send_xml_command(cmd)
351
352
    def create_audit(
353
        self,
354
        name: str,
355
        policy_id: str,
356
        target_id: str,
357
        scanner_id: str,
358
        *,
359
        alterable: Optional[bool] = None,
360
        hosts_ordering: Optional[HostsOrdering] = None,
361
        schedule_id: Optional[str] = None,
362
        alert_ids: Optional[List[str]] = None,
363
        comment: Optional[str] = None,
364
        schedule_periods: Optional[int] = None,
365
        observers: Optional[List[str]] = None,
366
        preferences: Optional[dict] = None,
367
    ) -> Any:
368
        """Create a new audit task
369
370
        Arguments:
371
            name: Name of the new audit
372
            policy_id: UUID of policy to use by the audit
373
            target_id: UUID of target to be scanned
374
            scanner_id: UUID of scanner to use for scanning the target
375
            comment: Comment for the audit
376
            alterable: Whether the task should be alterable
377
            alert_ids: List of UUIDs for alerts to be applied to the audit
378
            hosts_ordering: The order hosts are scanned in
379
            schedule_id: UUID of a schedule when the audit should be run.
380
            schedule_periods: A limit to the number of times the audit will be
381
                scheduled, or 0 for no limit
382
            observers: List of names or ids of users which should be allowed to
383
                observe this audit
384
            preferences: Name/Value pairs of scanner preferences.
385
386
        Returns:
387
            The response. See :py:meth:`send_command` for details.
388
        """
389
390
        return self.__create_task(
391
            name=name,
392
            config_id=policy_id,
393
            target_id=target_id,
394
            scanner_id=scanner_id,
395
            usage_type=UsageType.AUDIT,
396
            function=self.create_audit.__name__,
397
            alterable=alterable,
398
            hosts_ordering=hosts_ordering,
399
            schedule_id=schedule_id,
400
            alert_ids=alert_ids,
401
            comment=comment,
402
            schedule_periods=schedule_periods,
403
            observers=observers,
404
            preferences=preferences,
405
        )
406
407
    def create_config(
408
        self, config_id: str, name: str, *, comment: Optional[str] = None
409
    ) -> Any:
410
        """Create a new scan config
411
412
        Arguments:
413
            config_id: UUID of the existing scan config
414
            name: Name of the new scan config
415
            comment: A comment on the config
416
417
        Returns:
418
            The response. See :py:meth:`send_command` for details.
419
        """
420
        return self.__create_config(
421
            config_id=config_id,
422
            name=name,
423
            comment=comment,
424
            usage_type=UsageType.SCAN,
425
            function=self.create_config.__name__,
426
        )
427
428
    def create_config_from_osp_scanner(
429
        self, scanner_id: str, name: str, *, comment: Optional[str] = None
430
    ) -> Any:
431
        """Create a new scan config from an ospd scanner.
432
433
        Create config by retrieving the expected preferences from the given
434
        scanner via OSP.
435
436
        Arguments:
437
            scanner_id: UUID of an OSP scanner to get config data from
438
            name: Name of the new scan config
439
            comment: A comment on the config
440
441
        Returns:
442
            The response. See :py:meth:`send_command` for details.
443
        """
444
        return self.__create_config_from_osp_scanner(
445
            scanner_id=scanner_id,
446
            name=name,
447
            comment=comment,
448
            usage_type=UsageType.SCAN,
449
            function=self.create_config.__name__,
450
        )
451
452
    def create_permission(
453
        self,
454
        name: str,
455
        subject_id: str,
456
        subject_type: PermissionSubjectType,
457
        *,
458
        resource_id: Optional[str] = None,
459
        resource_type: Optional[EntityType] = None,
460
        comment: Optional[str] = None,
461
    ) -> Any:
462
        """Create a new permission
463
464
        Arguments:
465
            name: Name of the new permission
466
            subject_id: UUID of subject to whom the permission is granted
467
            subject_type: Type of the subject user, group or role
468
            comment: Comment for the permission
469
            resource_id: UUID of entity to which the permission applies
470
            resource_type: Type of the resource. For Super permissions user,
471
                group or role
472
473
        Returns:
474
            The response. See :py:meth:`send_command` for details.
475
        """
476
        if not name:
477
            raise RequiredArgument(
478
                function=self.create_permission.__name__, argument='name'
479
            )
480
481
        if not subject_id:
482
            raise RequiredArgument(
483
                function=self.create_permission.__name__, argument='subject_id'
484
            )
485
486
        if not isinstance(subject_type, PermissionSubjectType):
487
            raise InvalidArgumentType(
488
                function=self.create_permission.__name__,
489
                argument='subject_type',
490
                arg_type=PermissionSubjectType.__name__,
491
            )
492
493
        cmd = XmlCommand("create_permission")
494
        cmd.add_element("name", name)
495
496
        _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id})
497
        _xmlsubject.add_element("type", subject_type.value)
498
499
        if comment:
500
            cmd.add_element("comment", comment)
501
502 View Code Duplication
        if resource_id or resource_type:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
503
            if not resource_id:
504
                raise RequiredArgument(
505
                    function=self.create_permission.__name__,
506
                    argument='resource_id',
507
                )
508
509
            if not resource_type:
510
                raise RequiredArgument(
511
                    function=self.create_permission.__name__,
512
                    argument='resource_type',
513
                )
514
515
            if not isinstance(resource_type, self.types.EntityType):
516
                raise InvalidArgumentType(
517
                    function=self.create_permission.__name__,
518
                    argument='resource_type',
519
                    arg_type=self.types.EntityType.__name__,
520
                )
521
522
            _xmlresource = cmd.add_element(
523
                "resource", attrs={"id": resource_id}
524
            )
525
526
            _actual_resource_type = resource_type
527
            if resource_type.value == EntityType.AUDIT.value:
528
                _actual_resource_type = EntityType.TASK
529
            elif resource_type.value == EntityType.POLICY.value:
530
                _actual_resource_type = EntityType.SCAN_CONFIG
531
532
            _xmlresource.add_element("type", _actual_resource_type.value)
533
534
        return self._send_xml_command(cmd)
535
536
    def create_policy(
537
        self, name: str, *, policy_id: str = None, comment: Optional[str] = None
538
    ) -> Any:
539
        """Create a new policy config
540
541
        Arguments:
542
            name: Name of the new policy
543
            policy_id: UUID of an existing policy as base. By default the empty
544
                policy is used.
545
            comment: A comment on the policy
546
547
        Returns:
548
            The response. See :py:meth:`send_command` for details.
549
        """
550
        if policy_id is None:
551
            policy_id = _EMPTY_POLICY_ID
552
        return self.__create_config(
553
            config_id=policy_id,
554
            name=name,
555
            comment=comment,
556
            usage_type=UsageType.POLICY,
557
            function=self.create_policy.__name__,
558
        )
559
560
    def create_tag(
561
        self,
562
        name: str,
563
        resource_type: EntityType,
564
        *,
565
        resource_filter: Optional[str] = None,
566
        resource_ids: Optional[List[str]] = None,
567
        value: Optional[str] = None,
568
        comment: Optional[str] = None,
569
        active: Optional[bool] = None,
570
    ) -> Any:
571
        """Create a tag.
572
573
        Arguments:
574
            name: Name of the tag. A full tag name consisting of namespace and
575
                predicate e.g. `foo:bar`.
576
            resource_type: Entity type the tag is to be attached to.
577
            resource_filter: Filter term to select resources the tag is to be
578
                attached to. Only one of resource_filter or resource_ids can be
579
                provided.
580
            resource_ids: IDs of the resources the tag is to be attached to.
581
                Only one of resource_filter or resource_ids can be provided.
582
            value: Value associated with the tag.
583
            comment: Comment for the tag.
584
            active: Whether the tag should be active.
585
586
        Returns:
587
            The response. See :py:meth:`send_command` for details.
588
        """
589
        if not name:
590
            raise RequiredArgument(
591
                function=self.create_tag.__name__, argument='name'
592
            )
593
594
        if resource_filter and resource_ids:
595
            raise InvalidArgument(
596
                "create_tag accepts either resource_filter or resource_ids "
597
                "argument",
598
                function=self.create_tag.__name__,
599
            )
600
601
        if not resource_type:
602
            raise RequiredArgument(
603
                function=self.create_tag.__name__, argument='resource_type'
604
            )
605
606
        if not isinstance(resource_type, self.types.EntityType):
607
            raise InvalidArgumentType(
608
                function=self.create_tag.__name__,
609
                argument='resource_type',
610
                arg_type=EntityType.__name__,
611
            )
612
613
        cmd = XmlCommand('create_tag')
614
        cmd.add_element('name', name)
615
616
        _xmlresources = cmd.add_element("resources")
617
        if resource_filter is not None:
618
            _xmlresources.set_attribute("filter", resource_filter)
619
620
        for resource_id in resource_ids or []:
621
            _xmlresources.add_element(
622
                "resource", attrs={"id": str(resource_id)}
623
            )
624
625
        _actual_resource_type = resource_type
626
        if resource_type.value == EntityType.AUDIT.value:
627
            _actual_resource_type = EntityType.TASK
628
        elif resource_type.value == EntityType.POLICY.value:
629
            _actual_resource_type = EntityType.SCAN_CONFIG
630
        _xmlresources.add_element("type", _actual_resource_type.value)
631
632
        if comment:
633
            cmd.add_element("comment", comment)
634
635
        if value:
636
            cmd.add_element("value", value)
637
638
        if active is not None:
639
            if active:
640
                cmd.add_element("active", "1")
641
            else:
642
                cmd.add_element("active", "0")
643
644
        return self._send_xml_command(cmd)
645
646 View Code Duplication
    def create_tls_certificate(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
647
        self,
648
        name: str,
649
        certificate: str,
650
        *,
651
        comment: Optional[str] = None,
652
        trust: Optional[bool] = None,
653
    ) -> Any:
654
        """Create a new TLS certificate
655
656
        Arguments:
657
            name: Name of the TLS certificate, defaulting to the MD5
658
                fingerprint.
659
            certificate: The Base64 encoded certificate data (x.509 DER or PEM).
660
            comment: Comment for the TLS certificate.
661
            trust: Whether the certificate is trusted.
662
663
        Returns:
664
            The response. See :py:meth:`send_command` for details.
665
        """
666
        if not name:
667
            raise RequiredArgument(
668
                function=self.create_tls_certificate.__name__, argument='name'
669
            )
670
        if not certificate:
671
            raise RequiredArgument(
672
                function=self.create_tls_certificate.__name__,
673
                argument='certificate',
674
            )
675
676
        cmd = XmlCommand("create_tls_certificate")
677
678
        if comment:
679
            cmd.add_element("comment", comment)
680
681
        cmd.add_element("name", name)
682
        cmd.add_element("certificate", certificate)
683
684
        if trust:
685
            cmd.add_element("trust", to_bool(trust))
686
687
        return self._send_xml_command(cmd)
688
689
    def get_aggregates(
690
        self,
691
        resource_type: EntityType,
692
        *,
693
        filter: Optional[str] = None,
694
        filter_id: Optional[str] = None,
695
        sort_criteria: Optional[list] = None,
696
        data_columns: Optional[list] = None,
697
        group_column: Optional[str] = None,
698
        subgroup_column: Optional[str] = None,
699
        text_columns: Optional[list] = None,
700
        first_group: Optional[int] = None,
701
        max_groups: Optional[int] = None,
702
        mode: Optional[int] = None,
703
        **kwargs,
704
    ) -> Any:
705
        """Request aggregated information on a resource / entity type
706
707
        Additional arguments can be set via the kwargs parameter for backward
708
        compatibility with older versions of python-gvm, but are not validated.
709
710
        Arguments:
711
            resource_type: The entity type to gather data from
712
            filter: Filter term to use for the query
713
            filter_id: UUID of an existing filter to use for the query
714
            sort_criteria: List of sort criteria (dicts that can contain
715
                a field, stat and order)
716
            data_columns: List of fields to aggregate data from
717
            group_column: The field to group the entities by
718
            subgroup_column: The field to further group the entities
719
                inside groups by
720
            text_columns: List of simple text columns which no statistics
721
                are calculated for
722
            first_group: The index of the first aggregate group to return
723
            max_groups: The maximum number of aggregate groups to return,
724
                -1 for all
725
            mode: Special mode for aggregation
726
727
        Returns:
728
            The response. See :py:meth:`send_command` for details.
729
        """
730
        if not resource_type:
731
            raise RequiredArgument(
732
                function=self.get_aggregates.__name__, argument='resource_type'
733
            )
734
735
        if not isinstance(resource_type, self.types.EntityType):
736
            raise InvalidArgumentType(
737
                function=self.get_aggregates.__name__,
738
                argument='resource_type',
739
                arg_type=self.types.EntityType.__name__,
740
            )
741
742
        cmd = XmlCommand('get_aggregates')
743
744
        _actual_resource_type = resource_type
745
        if resource_type.value == EntityType.AUDIT.value:
746
            _actual_resource_type = EntityType.TASK
747
            cmd.set_attribute('usage_type', 'audit')
748
        elif resource_type.value == EntityType.POLICY.value:
749
            _actual_resource_type = EntityType.SCAN_CONFIG
750
            cmd.set_attribute('usage_type', 'policy')
751
        elif resource_type.value == EntityType.SCAN_CONFIG.value:
752
            cmd.set_attribute('usage_type', 'scan')
753
        elif resource_type.value == EntityType.TASK.value:
754
            cmd.set_attribute('usage_type', 'scan')
755
        cmd.set_attribute('type', _actual_resource_type.value)
756
757
        add_filter(cmd, filter, filter_id)
758
759
        if first_group is not None:
760
            if not isinstance(first_group, int):
761
                raise InvalidArgumentType(
762
                    function=self.get_aggregates.__name__,
763
                    argument='first_group',
764
                    arg_type=int.__name__,
765
                )
766
            cmd.set_attribute('first_group', str(first_group))
767
768
        if max_groups is not None:
769
            if not isinstance(max_groups, int):
770
                raise InvalidArgumentType(
771
                    function=self.get_aggregates.__name__,
772
                    argument='max_groups',
773
                    arg_type=int.__name__,
774
                )
775
            cmd.set_attribute('max_groups', str(max_groups))
776
777
        if sort_criteria is not None:
778
            if not isinstance(sort_criteria, list):
779
                raise InvalidArgumentType(
780
                    function=self.get_aggregates.__name__,
781
                    argument='sort_criteria',
782
                    arg_type=list.__name__,
783
                )
784
            for sort in sort_criteria:
785
                if not isinstance(sort, dict):
786
                    raise InvalidArgumentType(
787
                        function=self.get_aggregates.__name__,
788
                        argument='sort_criteria',
789
                    )
790
791
                sort_elem = cmd.add_element('sort')
792
                if sort.get('field'):
793
                    sort_elem.set_attribute('field', sort.get('field'))
794
795
                if sort.get('stat'):
796
                    if isinstance(sort['stat'], AggregateStatistic):
797
                        sort_elem.set_attribute('stat', sort['stat'].value)
798
                    else:
799
                        stat = get_aggregate_statistic_from_string(sort['stat'])
800
                        sort_elem.set_attribute('stat', stat.value)
801
802
                if sort.get('order'):
803
                    if isinstance(sort['order'], SortOrder):
804
                        sort_elem.set_attribute('order', sort['order'].value)
805
                    else:
806
                        so = get_sort_order_from_string(sort['order'])
807
                        sort_elem.set_attribute('order', so.value)
808
809
        if data_columns is not None:
810
            if not isinstance(data_columns, list):
811
                raise InvalidArgumentType(
812
                    function=self.get_aggregates.__name__,
813
                    argument='data_columns',
814
                    arg_type=list.__name__,
815
                )
816
            for column in data_columns:
817
                cmd.add_element('data_column', column)
818
819
        if group_column is not None:
820
            cmd.set_attribute('group_column', group_column)
821
822
        if subgroup_column is not None:
823
            if not group_column:
824
                raise RequiredArgument(
825
                    '{} requires a group_column argument'
826
                    ' if subgroup_column is given'.format(
827
                        self.get_aggregates.__name__
828
                    ),
829
                    function=self.get_aggregates.__name__,
830
                    argument='subgroup_column',
831
                )
832
            cmd.set_attribute('subgroup_column', subgroup_column)
833
834
        if text_columns is not None:
835
            if not isinstance(text_columns, list):
836
                raise InvalidArgumentType(
837
                    function=self.get_aggregates.__name__,
838
                    argument='text_columns',
839
                    arg_type=list.__name__,
840
                )
841
            for column in text_columns:
842
                cmd.add_element('text_column', column)
843
844
        if mode is not None:
845
            cmd.set_attribute('mode', mode)
846
847
        # Add additional keyword args as attributes for backward compatibility.
848
        cmd.set_attributes(kwargs)
849
850
        return self._send_xml_command(cmd)
851
852 View Code Duplication
    def get_tls_certificates(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
853
        self,
854
        *,
855
        filter: Optional[str] = None,
856
        filter_id: Optional[str] = None,
857
        include_certificate_data: Optional[bool] = None,
858
        details: Optional[bool] = None,
859
    ) -> Any:
860
        """Request a list of TLS certificates
861
862
        Arguments:
863
            filter: Filter term to use for the query
864
            filter_id: UUID of an existing filter to use for the query
865
            include_certificate_data: Whether to include the certificate data in
866
                the response
867
868
        Returns:
869
            The response. See :py:meth:`send_command` for details.
870
        """
871
872
        cmd = XmlCommand("get_tls_certificates")
873
874
        add_filter(cmd, filter, filter_id)
875
876
        if details is not None:
877
            cmd.set_attribute("details", to_bool(details))
878
879
        if include_certificate_data is not None:
880
            cmd.set_attribute(
881
                "include_certificate_data", to_bool(include_certificate_data)
882
            )
883
884
        return self._send_xml_command(cmd)
885
886
    def get_tls_certificate(self, tls_certificate_id: str) -> Any:
887
        """Request a single TLS certificate
888
889
        Arguments:
890
            tls_certificate_id: UUID of an existing TLS certificate
891
892
        Returns:
893
            The response. See :py:meth:`send_command` for details.
894
        """
895
        cmd = XmlCommand("get_tls_certificates")
896
897
        if not tls_certificate_id:
898
            raise RequiredArgument(
899
                function=self.get_tls_certificate.__name__,
900
                argument='tls_certificate_id',
901
            )
902
903
        cmd.set_attribute("tls_certificate_id", tls_certificate_id)
904
905
        # for single tls certificate always request cert data
906
        cmd.set_attribute("include_certificate_data", "1")
907
908
        # for single entity always request all details
909
        cmd.set_attribute("details", "1")
910
911
        return self._send_xml_command(cmd)
912
913
    def modify_alert(
914
        self,
915
        alert_id: str,
916
        *,
917
        name: Optional[str] = None,
918
        comment: Optional[str] = None,
919
        filter_id: Optional[str] = None,
920
        event: Optional[AlertEvent] = None,
921
        event_data: Optional[dict] = None,
922
        condition: Optional[AlertCondition] = None,
923
        condition_data: Optional[dict] = None,
924
        method: Optional[AlertMethod] = None,
925
        method_data: Optional[dict] = None,
926
    ) -> Any:
927
        """Modifies an existing alert.
928
929
        Arguments:
930
            alert_id: UUID of the alert to be modified.
931
            name: Name of the Alert.
932
            condition: The condition that must be satisfied for the alert to
933
                occur. If the event is either 'Updated SecInfo
934
                arrived' or 'New SecInfo arrived', condition must be 'Always'.
935
                Otherwise, condition can also be on of 'Severity at least',
936
                'Filter count changed' or 'Filter count at least'.
937
            condition_data: Data that defines the condition
938
            event: The event that must happen for the alert to occur, one of
939
                'Task run status changed', 'Updated SecInfo arrived' or
940
                'New SecInfo arrived'
941
            event_data: Data that defines the event
942
            method: The method by which the user is alerted, one of 'SCP',
943
                'Send', 'SMB', 'SNMP', 'Syslog' or 'Email';
944
                if the event is neither 'Updated SecInfo arrived' nor
945
                'New SecInfo arrived', method can also be one of 'Start Task',
946
                'HTTP Get', 'Sourcefire Connector' or 'verinice Connector'.
947
            method_data: Data that defines the method
948
            filter_id: Filter to apply when executing alert
949
            comment: Comment for the alert
950
951
        Returns:
952
            The response. See :py:meth:`send_command` for details.
953
        """
954
955
        if not alert_id:
956
            raise RequiredArgument(
957
                function=self.modify_alert.__name__, argument='alert_id'
958
            )
959
960
        cmd = XmlCommand("modify_alert")
961
        cmd.set_attribute("alert_id", str(alert_id))
962
963
        if name:
964
            cmd.add_element("name", name)
965
966
        if comment:
967
            cmd.add_element("comment", comment)
968
969
        if filter_id:
970
            cmd.add_element("filter", attrs={"id": filter_id})
971
972
        if condition:
973
            if not isinstance(condition, AlertCondition):
974
                raise InvalidArgumentType(
975
                    function=self.modify_alert.__name__,
976
                    argument='condition',
977
                    arg_type=AlertCondition.__name__,
978
                )
979
980
            conditions = cmd.add_element("condition", condition.value)
981
982
            if condition_data is not None:
983
                for key, value in condition_data.items():
984
                    _data = conditions.add_element("data", value)
985
                    _data.add_element("name", key)
986
987
        if method:
988
            if not isinstance(method, AlertMethod):
989
                raise InvalidArgumentType(
990
                    function=self.modify_alert.__name__,
991
                    argument='method',
992
                    arg_type=AlertMethod.__name__,
993
                )
994
995
            methods = cmd.add_element("method", method.value)
996
997
            if method_data is not None:
998
                for key, value in method_data.items():
999
                    _data = methods.add_element("data", value)
1000
                    _data.add_element("name", key)
1001
1002
        if event:
1003
            if not isinstance(event, AlertEvent):
1004
                raise InvalidArgumentType(
1005
                    function=self.modify_alert.__name__,
1006
                    argument='event',
1007
                    arg_type=AlertEvent.__name__,
1008
                )
1009
1010
            _check_event(event, condition, method)
1011
1012
            events = cmd.add_element("event", event.value)
1013
1014
            if event_data is not None:
1015
                for key, value in event_data.items():
1016
                    _data = events.add_element("data", value)
1017
                    _data.add_element("name", key)
1018
1019
        return self._send_xml_command(cmd)
1020
1021
    def modify_audit(
1022
        self,
1023
        audit_id: str,
1024
        *,
1025
        name: Optional[str] = None,
1026
        policy_id: Optional[str] = None,
1027
        target_id: Optional[str] = None,
1028
        scanner_id: Optional[str] = None,
1029
        alterable: Optional[bool] = None,
1030
        hosts_ordering: Optional[HostsOrdering] = None,
1031
        schedule_id: Optional[str] = None,
1032
        schedule_periods: Optional[int] = None,
1033
        comment: Optional[str] = None,
1034
        alert_ids: Optional[List[str]] = None,
1035
        observers: Optional[List[str]] = None,
1036
        preferences: Optional[dict] = None,
1037
    ) -> Any:
1038
        """Modifies an existing task.
1039
1040
        Arguments:
1041
            audit_id: UUID of audit to modify.
1042
            name: The name of the audit.
1043
            policy_id: UUID of policy to use by the audit
1044
            target_id: UUID of target to be scanned
1045
            scanner_id: UUID of scanner to use for scanning the target
1046
            comment: The comment on the audit.
1047
            alert_ids: List of UUIDs for alerts to be applied to the audit
1048
            hosts_ordering: The order hosts are scanned in
1049
            schedule_id: UUID of a schedule when the audit should be run.
1050
            schedule_periods: A limit to the number of times the audit will be
1051
                scheduled, or 0 for no limit.
1052
            observers: List of names or ids of users which should be allowed to
1053
                observe this audit
1054
            preferences: Name/Value pairs of scanner preferences.
1055
1056
        Returns:
1057
            The response. See :py:meth:`send_command` for details.
1058
        """
1059
        self.modify_task(
1060
            task_id=audit_id,
1061
            name=name,
1062
            config_id=policy_id,
1063
            target_id=target_id,
1064
            scanner_id=scanner_id,
1065
            alterable=alterable,
1066
            hosts_ordering=hosts_ordering,
1067
            schedule_id=schedule_id,
1068
            schedule_periods=schedule_periods,
1069
            comment=comment,
1070
            alert_ids=alert_ids,
1071
            observers=observers,
1072
            preferences=preferences,
1073
        )
1074
1075
    def modify_permission(
1076
        self,
1077
        permission_id: str,
1078
        *,
1079
        comment: Optional[str] = None,
1080
        name: Optional[str] = None,
1081
        resource_id: Optional[str] = None,
1082
        resource_type: Optional[EntityType] = None,
1083
        subject_id: Optional[str] = None,
1084
        subject_type: Optional[PermissionSubjectType] = None,
1085
    ) -> Any:
1086
        """Modifies an existing permission.
1087
1088
        Arguments:
1089
            permission_id: UUID of permission to be modified.
1090
            comment: The comment on the permission.
1091
            name: Permission name, currently the name of a command.
1092
            subject_id: UUID of subject to whom the permission is granted
1093
            subject_type: Type of the subject user, group or role
1094
            resource_id: UUID of entity to which the permission applies
1095
            resource_type: Type of the resource. For Super permissions user,
1096
                group or role
1097
1098
        Returns:
1099
            The response. See :py:meth:`send_command` for details.
1100
        """
1101
        if not permission_id:
1102
            raise RequiredArgument(
1103
                function=self.modify_permission.__name__,
1104
                argument='permission_id',
1105
            )
1106
1107
        cmd = XmlCommand("modify_permission")
1108
        cmd.set_attribute("permission_id", permission_id)
1109
1110
        if comment:
1111
            cmd.add_element("comment", comment)
1112
1113
        if name:
1114
            cmd.add_element("name", name)
1115
1116 View Code Duplication
        if resource_id or resource_type:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1117
            if not resource_id:
1118
                raise RequiredArgument(
1119
                    function=self.modify_permission.__name__,
1120
                    argument='resource_id',
1121
                )
1122
1123
            if not resource_type:
1124
                raise RequiredArgument(
1125
                    function=self.modify_permission.__name__,
1126
                    argument='resource_type',
1127
                )
1128
1129
            if not isinstance(resource_type, self.types.EntityType):
1130
                raise InvalidArgumentType(
1131
                    function=self.modify_permission.__name__,
1132
                    argument='resource_type',
1133
                    arg_type=self.types.EntityType.__name__,
1134
                )
1135
1136
            _xmlresource = cmd.add_element(
1137
                "resource", attrs={"id": resource_id}
1138
            )
1139
            _actual_resource_type = resource_type
1140
            if resource_type.value == EntityType.AUDIT.value:
1141
                _actual_resource_type = EntityType.TASK
1142
            elif resource_type.value == EntityType.POLICY.value:
1143
                _actual_resource_type = EntityType.SCAN_CONFIG
1144
            _xmlresource.add_element("type", _actual_resource_type.value)
1145
1146
        if subject_id or subject_type:
1147
            if not subject_id:
1148
                raise RequiredArgument(
1149
                    function=self.modify_permission.__name__,
1150
                    argument='subject_id',
1151
                )
1152
1153
            if not isinstance(subject_type, PermissionSubjectType):
1154
                raise InvalidArgumentType(
1155
                    function=self.modify_permission.__name__,
1156
                    argument='subject_type',
1157
                    arg_type=PermissionSubjectType.__name__,
1158
                )
1159
1160
            _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id})
1161
            _xmlsubject.add_element("type", subject_type.value)
1162
1163
        return self._send_xml_command(cmd)
1164
1165
    def modify_policy_set_nvt_preference(
1166
        self,
1167
        policy_id: str,
1168
        name: str,
1169
        nvt_oid: str,
1170
        *,
1171
        value: Optional[str] = None,
1172
    ) -> Any:
1173
        """Modifies the nvt preferences of an existing policy.
1174
1175
        Arguments:
1176
            policy_id: UUID of policy to modify.
1177
            name: Name for preference to change.
1178
            nvt_oid: OID of the NVT associated with preference to modify
1179
            value: New value for the preference. None to delete the preference
1180
                and to use the default instead.
1181
        """
1182
        self.modify_config_set_nvt_preference(
1183
            config_id=policy_id, name=name, nvt_oid=nvt_oid, value=value
1184
        )
1185
1186
    def modify_policy_set_name(self, policy_id: str, name: str) -> Any:
1187
        """Modifies the name of an existing policy
1188
1189
        Arguments:
1190
            config_id: UUID of policy to modify.
1191
            name: New name for the config.
1192
        """
1193
        self.modify_config_set_name(config_id=policy_id, name=name)
1194
1195
    def modify_policy_set_comment(
1196
        self, policy_id: str, comment: Optional[str] = ""
1197
    ) -> Any:
1198
        """Modifies the comment of an existing policy
1199
1200
        Arguments:
1201
            policy_id: UUID of policy to modify.
1202
            comment: Comment to set on a config. Default: ''
1203
        """
1204
        self.modify_config_set_comment(config_id=policy_id, comment=comment)
1205
1206
    def modify_policy_set_scanner_preference(
1207
        self, policy_id: str, name: str, *, value: Optional[str] = None
1208
    ) -> Any:
1209
        """Modifies the scanner preferences of an existing policy
1210
1211
        Arguments:
1212
            policy_id: UUID of policy to modify.
1213
            name: Name of the scanner preference to change
1214
            value: New value for the preference. None to delete the preference
1215
                and to use the default instead.
1216
1217
        """
1218
        self.modify_config_set_scanner_preference(
1219
            config_id=policy_id, name=name, value=value
1220
        )
1221
1222
    def modify_policy_set_nvt_selection(
1223
        self, policy_id: str, family: str, nvt_oids: List[str]
1224
    ) -> Any:
1225
        """Modifies the selected nvts of an existing policy
1226
1227
        The manager updates the given family in the config to include only the
1228
        given NVTs.
1229
1230
        Arguments:
1231
            policy_id: UUID of policy to modify.
1232
            family: Name of the NVT family to include NVTs from
1233
            nvt_oids: List of NVTs to select for the family.
1234
        """
1235
        self.modify_config_set_nvt_selection(
1236
            config_id=policy_id, family=family, nvt_oids=nvt_oids
1237
        )
1238
1239
    def modify_policy_set_family_selection(
1240
        self,
1241
        policy_id: str,
1242
        families: List[Tuple[str, bool, bool]],
1243
        *,
1244
        auto_add_new_families: Optional[bool] = True,
1245
    ) -> Any:
1246
        """
1247
        Selected the NVTs of a policy at a family level.
1248
1249
        Arguments:
1250
            policy_id: UUID of policy to modify.
1251
            families: A list of tuples with the first entry being the name
1252
                of the NVT family selected, second entry a boolean indicating
1253
                whether new NVTs should be added to the family automatically,
1254
                and third entry a boolean indicating whether all nvts from
1255
                the family should be included.
1256
            auto_add_new_families: Whether new families should be added to the
1257
                policy automatically. Default: True.
1258
        """
1259
        self.modify_config_set_family_selection(
1260
            config_id=policy_id,
1261
            families=families,
1262
            auto_add_new_families=auto_add_new_families,
1263
        )
1264
1265
    def modify_tag(
1266
        self,
1267
        tag_id: str,
1268
        *,
1269
        comment: Optional[str] = None,
1270
        name: Optional[str] = None,
1271
        value=None,
1272
        active=None,
1273
        resource_action: Optional[str] = None,
1274
        resource_type: Optional[EntityType] = None,
1275
        resource_filter: Optional[str] = None,
1276
        resource_ids: Optional[List[str]] = None,
1277
    ) -> Any:
1278
        """Modifies an existing tag.
1279
1280
        Arguments:
1281
            tag_id: UUID of the tag.
1282
            comment: Comment to add to the tag.
1283
            name: Name of the tag.
1284
            value: Value of the tag.
1285
            active: Whether the tag is active.
1286
            resource_action: Whether to add or remove resources instead of
1287
                overwriting. One of '', 'add', 'set' or 'remove'.
1288
            resource_type: Type of the resources to which to attach the tag.
1289
                Required if resource_filter is set.
1290
            resource_filter: Filter term to select resources the tag is to be
1291
                attached to.
1292
            resource_ids: IDs of the resources to which to attach the tag.
1293
1294
        Returns:
1295
            The response. See :py:meth:`send_command` for details.
1296
        """
1297
        if not tag_id:
1298
            raise RequiredArgument(
1299
                function=self.modify_tag.__name__, argument='tag_id'
1300
            )
1301
1302
        cmd = XmlCommand("modify_tag")
1303
        cmd.set_attribute("tag_id", str(tag_id))
1304
1305
        if comment:
1306
            cmd.add_element("comment", comment)
1307
1308
        if name:
1309
            cmd.add_element("name", name)
1310
1311
        if value:
1312
            cmd.add_element("value", value)
1313
1314
        if active is not None:
1315
            cmd.add_element("active", to_bool(active))
1316
1317
        if resource_action or resource_filter or resource_ids or resource_type:
1318
            if resource_filter and not resource_type:
1319
                raise RequiredArgument(
1320
                    function=self.modify_tag.__name__, argument='resource_type'
1321
                )
1322
1323
            _xmlresources = cmd.add_element("resources")
1324
            if resource_action is not None:
1325
                _xmlresources.set_attribute("action", resource_action)
1326
1327
            if resource_filter is not None:
1328
                _xmlresources.set_attribute("filter", resource_filter)
1329
1330
            for resource_id in resource_ids or []:
1331
                _xmlresources.add_element(
1332
                    "resource", attrs={"id": str(resource_id)}
1333
                )
1334
1335
            if resource_type is not None:
1336
                if not isinstance(resource_type, self.types.EntityType):
1337
                    raise InvalidArgumentType(
1338
                        function=self.modify_tag.__name__,
1339
                        argument="resource_type",
1340
                        arg_type=EntityType.__name__,
1341
                    )
1342
                _actual_resource_type = resource_type
1343
                if resource_type.value == EntityType.AUDIT.value:
1344
                    _actual_resource_type = EntityType.TASK
1345
                elif resource_type.value == EntityType.POLICY.value:
1346
                    _actual_resource_type = EntityType.SCAN_CONFIG
1347
                _xmlresources.add_element("type", _actual_resource_type.value)
1348
1349
        return self._send_xml_command(cmd)
1350
1351 View Code Duplication
    def modify_tls_certificate(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1352
        self,
1353
        tls_certificate_id: str,
1354
        *,
1355
        name: Optional[str] = None,
1356
        comment: Optional[str] = None,
1357
        trust: Optional[bool] = None,
1358
    ) -> Any:
1359
        """Modifies an existing TLS certificate.
1360
1361
        Arguments:
1362
            tls_certificate_id: UUID of the TLS certificate to be modified.
1363
            name: Name of the TLS certificate, defaulting to the MD5 fingerprint
1364
            comment: Comment for the TLS certificate.
1365
            trust: Whether the certificate is trusted.
1366
1367
        Returns:
1368
            The response. See :py:meth:`send_command` for details.
1369
        """
1370
        if not tls_certificate_id:
1371
            raise RequiredArgument(
1372
                function=self.modify_tls_certificate.__name__,
1373
                argument='tls_certificate_id',
1374
            )
1375
1376
        cmd = XmlCommand("modify_tls_certificate")
1377
        cmd.set_attribute("tls_certificate_id", str(tls_certificate_id))
1378
1379
        if comment:
1380
            cmd.add_element("comment", comment)
1381
1382
        if name:
1383
            cmd.add_element("name", name)
1384
1385
        if trust:
1386
            cmd.add_element("trust", to_bool(trust))
1387
1388
        return self._send_xml_command(cmd)
1389
1390
    def clone_alert(self, alert_id: str) -> Any:
1391
        """Clone an existing alert
1392
1393
        Arguments:
1394
            alert_id: UUID of an existing alert to clone from
1395
1396
        Returns:
1397
            The response. See :py:meth:`send_command` for details.
1398
        """
1399
        if not alert_id:
1400
            raise RequiredArgument(
1401
                function=self.clone_alert.__name__, argument='alert_id'
1402
            )
1403
1404
        cmd = XmlCommand("create_alert")
1405
        cmd.add_element("copy", alert_id)
1406
        return self._send_xml_command(cmd)
1407
1408
    def clone_ticket(self, ticket_id: str) -> Any:
1409
        """Clone an existing ticket
1410
1411
        Arguments:
1412
            ticket_id: UUID of an existing ticket to clone from
1413
1414
        Returns:
1415
            The response. See :py:meth:`send_command` for details.
1416
        """
1417
        if not ticket_id:
1418
            raise RequiredArgument(
1419
                function=self.clone_ticket.__name__, argument='ticket_id'
1420
            )
1421
1422
        cmd = XmlCommand("create_ticket")
1423
1424
        _copy = cmd.add_element("copy", ticket_id)
1425
1426
        return self._send_xml_command(cmd)
1427
1428
    def clone_tls_certificate(self, tls_certificate_id: str) -> Any:
1429
        """Modifies an existing TLS certificate.
1430
1431
        Arguments:
1432
            tls_certificate_id: The UUID of an existing TLS certificate
1433
1434
        Returns:
1435
            The response. See :py:meth:`send_command` for details.
1436
        """
1437
        if not tls_certificate_id:
1438
            raise RequiredArgument(
1439
                function=self.clone_tls_certificate.__name__,
1440
                argument='tls_certificate_id',
1441
            )
1442
1443
        cmd = XmlCommand("create_tls_certificate")
1444
1445
        cmd.add_element("copy", tls_certificate_id)
1446
1447
        return self._send_xml_command(cmd)
1448
1449
    def get_configs(
1450
        self,
1451
        *,
1452
        filter: Optional[str] = None,
1453
        filter_id: Optional[str] = None,
1454
        trash: Optional[bool] = None,
1455
        details: Optional[bool] = None,
1456
        families: Optional[bool] = None,
1457
        preferences: Optional[bool] = None,
1458
        tasks: Optional[bool] = None,
1459
    ) -> Any:
1460
        """Request a list of scan configs
1461
1462
        Arguments:
1463
            filter: Filter term to use for the query
1464
            filter_id: UUID of an existing filter to use for the query
1465
            trash: Whether to get the trashcan scan configs instead
1466
            details: Whether to get config families, preferences, nvt selectors
1467
                and tasks.
1468
            families: Whether to include the families if no details are
1469
                requested
1470
            preferences: Whether to include the preferences if no details are
1471
                requested
1472
            tasks: Whether to get tasks using this config
1473
1474
        Returns:
1475
            The response. See :py:meth:`send_command` for details.
1476
        """
1477
        return self.__get_configs(
1478
            UsageType.SCAN,
1479
            filter=filter,
1480
            filter_id=filter_id,
1481
            trash=trash,
1482
            details=details,
1483
            families=families,
1484
            preferences=preferences,
1485
            tasks=tasks,
1486
        )
1487
1488
    def get_policies(
1489
        self,
1490
        *,
1491
        audits: Optional[bool] = None,
1492
        filter: Optional[str] = None,
1493
        filter_id: Optional[str] = None,
1494
        details: Optional[bool] = None,
1495
        families: Optional[bool] = None,
1496
        preferences: Optional[bool] = None,
1497
        trash: Optional[bool] = None,
1498
    ) -> Any:
1499
        """Request a list of policies
1500
1501
        Arguments:
1502
            audits: Whether to get audits using the policy
1503
            filter: Filter term to use for the query
1504
            filter_id: UUID of an existing filter to use for the query
1505
            details: Whether to get  families, preferences, nvt selectors
1506
                and tasks.
1507
            families: Whether to include the families if no details are
1508
                requested
1509
            preferences: Whether to include the preferences if no details are
1510
                requested
1511
            trash: Whether to get the trashcan audits instead
1512
1513
        Returns:
1514
            The response. See :py:meth:`send_command` for details.
1515
        """
1516
        return self.__get_configs(
1517
            UsageType.POLICY,
1518
            filter=filter,
1519
            filter_id=filter_id,
1520
            details=details,
1521
            families=families,
1522
            preferences=preferences,
1523
            tasks=audits,
1524
            trash=trash,
1525
        )
1526
1527
    def get_config(
1528
        self, config_id: str, *, tasks: Optional[bool] = None
1529
    ) -> Any:
1530
        """Request a single scan config
1531
1532
        Arguments:
1533
            config_id: UUID of an existing scan config
1534
            tasks: Whether to get tasks using this config
1535
1536
        Returns:
1537
            The response. See :py:meth:`send_command` for details.
1538
        """
1539
        return self.__get_config(
1540
            config_id=config_id, usage_type=UsageType.SCAN, tasks=tasks
1541
        )
1542
1543
    def get_policy(
1544
        self, policy_id: str, *, audits: Optional[bool] = None
1545
    ) -> Any:
1546
        """Request a single policy
1547
1548
        Arguments:
1549
            policy_id: UUID of an existing policy
1550
            audits: Whether to get audits using this config
1551
1552
        Returns:
1553
            The response. See :py:meth:`send_command` for details.
1554
        """
1555
        return self.__get_config(policy_id, UsageType.POLICY, tasks=audits)
1556
1557
    def get_tasks(
1558
        self,
1559
        *,
1560
        filter: Optional[str] = None,
1561
        filter_id: Optional[str] = None,
1562
        trash: Optional[bool] = None,
1563
        details: Optional[bool] = None,
1564
        schedules_only: Optional[bool] = None,
1565
    ) -> Any:
1566
        """Request a list of tasks
1567
1568
        Arguments:
1569
            filter: Filter term to use for the query
1570
            filter_id: UUID of an existing filter to use for the query
1571
            trash: Whether to get the trashcan tasks instead
1572
            details: Whether to include full task details
1573
            schedules_only: Whether to only include id, name and schedule
1574
                details
1575
1576
        Returns:
1577
            The response. See :py:meth:`send_command` for details.
1578
        """
1579
        return self.__get_tasks(
1580
            UsageType.SCAN,
1581
            filter=filter,
1582
            filter_id=filter_id,
1583
            trash=trash,
1584
            details=details,
1585
            schedules_only=schedules_only,
1586
        )
1587
1588
    def get_audits(
1589
        self,
1590
        *,
1591
        filter: Optional[str] = None,
1592
        filter_id: Optional[str] = None,
1593
        trash: Optional[bool] = None,
1594
        details: Optional[bool] = None,
1595
        schedules_only: Optional[bool] = None,
1596
    ) -> Any:
1597
        """Request a list of audits
1598
1599
        Arguments:
1600
            filter: Filter term to use for the query
1601
            filter_id: UUID of an existing filter to use for the query
1602
            trash: Whether to get the trashcan audits instead
1603
            details: Whether to include full audit details
1604
            schedules_only: Whether to only include id, name and schedule
1605
                details
1606
1607
        Returns:
1608
            The response. See :py:meth:`send_command` for details.
1609
        """
1610
        return self.__get_tasks(
1611
            UsageType.AUDIT,
1612
            filter=filter,
1613
            filter_id=filter_id,
1614
            trash=trash,
1615
            details=details,
1616
            schedules_only=schedules_only,
1617
        )
1618
1619
    def get_task(self, task_id: str) -> Any:
1620
        """Request a single task
1621
1622
        Arguments:
1623
            task_id: UUID of an existing task
1624
1625
        Returns:
1626
            The response. See :py:meth:`send_command` for details.
1627
        """
1628
        return self.__get_task(task_id, UsageType.SCAN)
1629
1630
    def get_audit(self, audit_id: str) -> Any:
1631
        """Request a single audit
1632
1633
        Arguments:
1634
            audit_id: UUID of an existing audit
1635
1636
        Returns:
1637
            The response. See :py:meth:`send_command` for details.
1638
        """
1639
        return self.__get_task(audit_id, UsageType.AUDIT)
1640
1641
    def clone_audit(self, audit_id: str) -> Any:
1642
        """Clone an existing audit
1643
1644
        Arguments:
1645
            audit_id: UUID of existing audit to clone from
1646
1647
        Returns:
1648
            The response. See :py:meth:`send_command` for details.
1649
        """
1650
        if not audit_id:
1651
            raise RequiredArgument(
1652
                function=self.clone_audit.__name__, argument='audit_id'
1653
            )
1654
1655
        cmd = XmlCommand("create_task")
1656
        cmd.add_element("copy", audit_id)
1657
        return self._send_xml_command(cmd)
1658
1659
    def clone_policy(self, policy_id: str) -> Any:
1660
        """Clone a policy from an existing one
1661
1662
        Arguments:
1663
            policy_id: UUID of the existing policy
1664
1665
        Returns:
1666
            The response. See :py:meth:`send_command` for details.
1667
        """
1668
        if not policy_id:
1669
            raise RequiredArgument(
1670
                function=self.clone_policy.__name__, argument='policy_id'
1671
            )
1672
1673
        cmd = XmlCommand("create_config")
1674
        cmd.add_element("copy", policy_id)
1675
        return self._send_xml_command(cmd)
1676
1677
    def delete_audit(
1678
        self, audit_id: str, *, ultimate: Optional[bool] = False
1679
    ) -> Any:
1680
        """Deletes an existing audit
1681
1682
        Arguments:
1683
            audit_id: UUID of the audit to be deleted.
1684
            ultimate: Whether to remove entirely, or to the trashcan.
1685
        """
1686
        if not audit_id:
1687
            raise RequiredArgument(
1688
                function=self.delete_audit.__name__, argument='audit_id'
1689
            )
1690
1691
        cmd = XmlCommand("delete_task")
1692
        cmd.set_attribute("task_id", audit_id)
1693
        cmd.set_attribute("ultimate", to_bool(ultimate))
1694
1695
        return self._send_xml_command(cmd)
1696
1697
    def delete_policy(
1698
        self, policy_id: str, *, ultimate: Optional[bool] = False
1699
    ) -> Any:
1700
        """Deletes an existing policy
1701
1702
        Arguments:
1703
            policy_id: UUID of the policy to be deleted.
1704
            ultimate: Whether to remove entirely, or to the trashcan.
1705
        """
1706
        if not policy_id:
1707
            raise RequiredArgument(
1708
                function=self.delete_policy.__name__, argument='policy_id'
1709
            )
1710
1711
        cmd = XmlCommand("delete_config")
1712
        cmd.set_attribute("config_id", policy_id)
1713
        cmd.set_attribute("ultimate", to_bool(ultimate))
1714
1715
        return self._send_xml_command(cmd)
1716
1717
    def delete_tls_certificate(self, tls_certificate_id: str) -> Any:
1718
        """Deletes an existing tls certificate
1719
1720
        Arguments:
1721
            tls_certificate_id: UUID of the tls certificate to be deleted.
1722
        """
1723
        if not tls_certificate_id:
1724
            raise RequiredArgument(
1725
                function=self.delete_tls_certificate.__name__,
1726
                argument='tls_certificate_id',
1727
            )
1728
1729
        cmd = XmlCommand("delete_tls_certificate")
1730
        cmd.set_attribute("tls_certificate_id", tls_certificate_id)
1731
1732
        return self._send_xml_command(cmd)
1733
1734
    def __create_task(
1735
        self,
1736
        name: str,
1737
        config_id: str,
1738
        target_id: str,
1739
        scanner_id: str,
1740
        usage_type: UsageType,
1741
        function: str,
1742
        *,
1743
        alterable: Optional[bool] = None,
1744
        hosts_ordering: Optional[HostsOrdering] = None,
1745
        schedule_id: Optional[str] = None,
1746
        alert_ids: Optional[List[str]] = None,
1747
        comment: Optional[str] = None,
1748
        schedule_periods: Optional[int] = None,
1749
        observers: Optional[List[str]] = None,
1750
        preferences: Optional[dict] = None,
1751
    ) -> Any:
1752
        if not name:
1753
            raise RequiredArgument(function=function, argument='name')
1754
1755
        if not config_id:
1756
            raise RequiredArgument(function=function, argument='config_id')
1757
1758
        if not target_id:
1759
            raise RequiredArgument(function=function, argument='target_id')
1760
1761
        if not scanner_id:
1762
            raise RequiredArgument(function=function, argument='scanner_id')
1763
1764
        # don't allow to create a container task with create_task
1765
        if target_id == '0':
1766
            raise InvalidArgument(function=function, argument='target_id')
1767
1768
        cmd = XmlCommand("create_task")
1769
        cmd.add_element("name", name)
1770
        cmd.add_element("usage_type", usage_type.value)
1771
        cmd.add_element("config", attrs={"id": config_id})
1772
        cmd.add_element("target", attrs={"id": target_id})
1773
        cmd.add_element("scanner", attrs={"id": scanner_id})
1774
1775
        if comment:
1776
            cmd.add_element("comment", comment)
1777
1778
        if alterable is not None:
1779
            cmd.add_element("alterable", to_bool(alterable))
1780
1781
        if hosts_ordering:
1782
            if not isinstance(hosts_ordering, self.types.HostsOrdering):
1783
                raise InvalidArgumentType(
1784
                    function=function,
1785
                    argument='hosts_ordering',
1786
                    arg_type=HostsOrdering.__name__,
1787
                )
1788
            cmd.add_element("hosts_ordering", hosts_ordering.value)
1789
1790
        if alert_ids:
1791
            if isinstance(alert_ids, str):
1792
                deprecation(
1793
                    "Please pass a list as alert_ids parameter to {}. "
1794
                    "Passing a string is deprecated and will be removed in "
1795
                    "future.".format(function)
1796
                )
1797
1798
                # if a single id is given as a string wrap it into a list
1799
                alert_ids = [alert_ids]
1800
            if is_list_like(alert_ids):
1801
                # parse all given alert id's
1802
                for alert in alert_ids:
1803
                    cmd.add_element("alert", attrs={"id": str(alert)})
1804
1805 View Code Duplication
        if schedule_id:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1806
            cmd.add_element("schedule", attrs={"id": schedule_id})
1807
1808
            if schedule_periods is not None:
1809
                if (
1810
                    not isinstance(schedule_periods, numbers.Integral)
1811
                    or schedule_periods < 0
1812
                ):
1813
                    raise InvalidArgument(
1814
                        "schedule_periods must be an integer greater or equal "
1815
                        "than 0"
1816
                    )
1817
                cmd.add_element("schedule_periods", str(schedule_periods))
1818
1819
        if observers is not None:
1820
            if not is_list_like(observers):
1821
                raise InvalidArgumentType(
1822
                    function=function, argument='observers', arg_type='list'
1823
                )
1824
1825
            # gvmd splits by comma and space
1826
            # gvmd tries to lookup each value as user name and afterwards as
1827
            # user id. So both user name and user id are possible
1828
            cmd.add_element("observers", to_comma_list(observers))
1829
1830 View Code Duplication
        if preferences is not None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1831
            if not isinstance(preferences, collections.abc.Mapping):
1832
                raise InvalidArgumentType(
1833
                    function=function,
1834
                    argument='preferences',
1835
                    arg_type=collections.abc.Mapping.__name__,
1836
                )
1837
1838
            _xmlprefs = cmd.add_element("preferences")
1839
            for pref_name, pref_value in preferences.items():
1840
                _xmlpref = _xmlprefs.add_element("preference")
1841
                _xmlpref.add_element("scanner_name", pref_name)
1842
                _xmlpref.add_element("value", str(pref_value))
1843
1844
        return self._send_xml_command(cmd)
1845
1846
    def __create_config(
1847
        self,
1848
        config_id: str,
1849
        name: str,
1850
        usage_type: UsageType,
1851
        function: str,
1852
        *,
1853
        comment: Optional[str] = None,
1854
    ) -> Any:
1855
        if not name:
1856
            raise RequiredArgument(function=function, argument='name')
1857
1858
        if not config_id:
1859
            raise RequiredArgument(function=function, argument='config_id')
1860
1861
        cmd = XmlCommand("create_config")
1862
        if comment is not None:
1863
            cmd.add_element("comment", comment)
1864
        cmd.add_element("copy", config_id)
1865
        cmd.add_element("name", name)
1866
        cmd.add_element("usage_type", usage_type.value)
1867
        return self._send_xml_command(cmd)
1868
1869
    def __create_config_from_osp_scanner(
1870
        self,
1871
        scanner_id: str,
1872
        name: str,
1873
        usage_type: UsageType,
1874
        function: str,
1875
        *,
1876
        comment: Optional[str] = None,
1877
    ) -> Any:
1878
        if not name:
1879
            raise RequiredArgument(function=function, argument='name')
1880
1881
        if not scanner_id:
1882
            raise RequiredArgument(function=function, argument='scanner_id')
1883
1884
        cmd = XmlCommand("create_config")
1885
        if comment is not None:
1886
            cmd.add_element("comment", comment)
1887
        cmd.add_element("scanner", scanner_id)
1888
        cmd.add_element("name", name)
1889
        cmd.add_element("usage_type", usage_type.value)
1890
        return self._send_xml_command(cmd)
1891
1892
    def __get_configs(
1893
        self,
1894
        usage_type: UsageType,
1895
        *,
1896
        filter: Optional[str] = None,
1897
        filter_id: Optional[str] = None,
1898
        trash: Optional[bool] = None,
1899
        details: Optional[bool] = None,
1900
        families: Optional[bool] = None,
1901
        preferences: Optional[bool] = None,
1902
        tasks: Optional[bool] = None,
1903
    ) -> Any:
1904
        cmd = XmlCommand("get_configs")
1905
        cmd.set_attribute("usage_type", usage_type.value)
1906
1907
        add_filter(cmd, filter, filter_id)
1908
1909
        if trash is not None:
1910
            cmd.set_attribute("trash", to_bool(trash))
1911
1912
        if details is not None:
1913
            cmd.set_attribute("details", to_bool(details))
1914
1915
        if families is not None:
1916
            cmd.set_attribute("families", to_bool(families))
1917
1918
        if preferences is not None:
1919
            cmd.set_attribute("preferences", to_bool(preferences))
1920
1921
        if tasks is not None:
1922
            cmd.set_attribute("tasks", to_bool(tasks))
1923
1924
        return self._send_xml_command(cmd)
1925
1926
    def __get_config(
1927
        self,
1928
        config_id: str,
1929
        usage_type: UsageType,
1930
        *,
1931
        tasks: Optional[bool] = None,
1932
    ) -> Any:
1933
        if not config_id:
1934
            raise RequiredArgument(
1935
                function=self.get_config.__name__, argument='config_id'
1936
            )
1937
1938
        cmd = XmlCommand("get_configs")
1939
        cmd.set_attribute("config_id", config_id)
1940
1941
        cmd.set_attribute("usage_type", usage_type.value)
1942
1943
        if tasks is not None:
1944
            cmd.set_attribute("tasks", to_bool(tasks))
1945
1946
        # for single entity always request all details
1947
        cmd.set_attribute("details", "1")
1948
1949
        return self._send_xml_command(cmd)
1950
1951
    def __get_tasks(
1952
        self,
1953
        usage_type: UsageType,
1954
        *,
1955
        filter: Optional[str] = None,
1956
        filter_id: Optional[str] = None,
1957
        trash: Optional[bool] = None,
1958
        details: Optional[bool] = None,
1959
        schedules_only: Optional[bool] = None,
1960
    ) -> Any:
1961
        cmd = XmlCommand("get_tasks")
1962
        cmd.set_attribute("usage_type", usage_type.value)
1963
1964
        add_filter(cmd, filter, filter_id)
1965
1966
        if trash is not None:
1967
            cmd.set_attribute("trash", to_bool(trash))
1968
1969
        if details is not None:
1970
            cmd.set_attribute("details", to_bool(details))
1971
1972
        if schedules_only is not None:
1973
            cmd.set_attribute("schedules_only", to_bool(schedules_only))
1974
1975
        return self._send_xml_command(cmd)
1976
1977
    def __get_task(self, task_id: str, usage_type: UsageType) -> Any:
1978
        if not task_id:
1979
            raise RequiredArgument(
1980
                function=self.get_task.__name__, argument='task_id'
1981
            )
1982
1983
        cmd = XmlCommand("get_tasks")
1984
        cmd.set_attribute("task_id", task_id)
1985
        cmd.set_attribute("usage_type", usage_type.value)
1986
1987
        # for single entity always request all details
1988
        cmd.set_attribute("details", "1")
1989
        return self._send_xml_command(cmd)
1990
1991
    def resume_audit(self, audit_id: str) -> Any:
1992
        """Resume an existing stopped audit
1993
1994
        Arguments:
1995
            audit_id: UUID of the audit to be resumed
1996
1997
        Returns:
1998
            The response. See :py:meth:`send_command` for details.
1999
        """
2000
        if not audit_id:
2001
            raise RequiredArgument(
2002
                function=self.resume_audit.__name__, argument='audit_id'
2003
            )
2004
2005
        cmd = XmlCommand("resume_task")
2006
        cmd.set_attribute("task_id", audit_id)
2007
2008
        return self._send_xml_command(cmd)
2009
2010
    def start_audit(self, audit_id: str) -> Any:
2011
        """Start an existing audit
2012
2013
        Arguments:
2014
            audit_id: UUID of the audit to be started
2015
2016
        Returns:
2017
            The response. See :py:meth:`send_command` for details.
2018
        """
2019
        if not audit_id:
2020
            raise RequiredArgument(
2021
                function=self.start_audit.__name__, argument='audit_id'
2022
            )
2023
2024
        cmd = XmlCommand("start_task")
2025
        cmd.set_attribute("task_id", audit_id)
2026
2027
        return self._send_xml_command(cmd)
2028
2029
    def stop_audit(self, audit_id: str) -> Any:
2030
        """Stop an existing running audit
2031
2032
        Arguments:
2033
            audit_id: UUID of the audit to be stopped
2034
2035
        Returns:
2036
            The response. See :py:meth:`send_command` for details.
2037
        """
2038
        if not audit_id:
2039
            raise RequiredArgument(
2040
                function=self.stop_audit.__name__, argument='audit_id'
2041
            )
2042
2043
        cmd = XmlCommand("stop_task")
2044
        cmd.set_attribute("task_id", audit_id)
2045
2046
        return self._send_xml_command(cmd)
2047
2048
    def get_feed(self, feed_type: Optional[FeedType]) -> Any:
2049
        """Request a single feed
2050
2051
        Arguments:
2052
            feed_type: Type of single feed to get: NVT, CERT or SCAP
2053
2054
        Returns:
2055
            The response. See :py:meth:`send_command` for details.
2056
        """
2057
        if not feed_type:
2058
            raise RequiredArgument(
2059
                function=self.get_feed.__name__, argument='feed_type'
2060
            )
2061
2062
        if not isinstance(feed_type, FeedType):
2063
            raise InvalidArgumentType(
2064
                function=self.get_feed.__name__,
2065
                argument='feed_type',
2066
                arg_type=FeedType.__name__,
2067
            )
2068
2069
        cmd = XmlCommand("get_feeds")
2070
        cmd.set_attribute("type", feed_type.value)
2071
2072
        return self._send_xml_command(cmd)
2073
2074
    def create_credential(
2075
        self,
2076
        name: str,
2077
        credential_type: CredentialType,
2078
        *,
2079
        comment: Optional[str] = None,
2080
        allow_insecure: Optional[bool] = None,
2081
        certificate: Optional[str] = None,
2082
        key_phrase: Optional[str] = None,
2083
        private_key: Optional[str] = None,
2084
        login: Optional[str] = None,
2085
        password: Optional[str] = None,
2086
        auth_algorithm: Optional[SnmpAuthAlgorithm] = None,
2087
        community: Optional[str] = None,
2088
        privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None,
2089
        privacy_password: Optional[str] = None,
2090
        public_key: Optional[str] = None,
2091
    ) -> Any:
2092
        """Create a new credential
2093
2094
        Create a new credential e.g. to be used in the method of an alert.
2095
2096
        Currently the following credential types are supported:
2097
2098
            - Username + Password
2099
            - Username + SSH-Key
2100
            - Client Certificates
2101
            - SNMPv1 or SNMPv2c protocol
2102
            - S/MIME Certificate
2103
            - OpenPGP Key
2104
            - Password only
2105
2106
        Arguments:
2107
            name: Name of the new credential
2108
            credential_type: The credential type.
2109
            comment: Comment for the credential
2110
            allow_insecure: Whether to allow insecure use of the credential
2111
            certificate: Certificate for the credential.
2112
                Required for client-certificate and smime credential types.
2113
            key_phrase: Key passphrase for the private key.
2114
                Used for the username+ssh-key credential type.
2115
            private_key: Private key to use for login. Required
2116
                for usk credential type. Also used for the cc credential type.
2117
                The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM,
2118
                PKC#12, OpenSSL, ...) depend on your installed GnuTLS version.
2119
            login: Username for the credential. Required for username+password,
2120
                username+ssh-key and snmp credential type.
2121
            password: Password for the credential. Used for username+password
2122
                and snmp credential types.
2123
            community: The SNMP community
2124
            auth_algorithm: The SNMP authentication algorithm. Required for snmp
2125
                credential type.
2126
            privacy_algorithm: The SNMP privacy algorithm
2127
            privacy_password: The SNMP privacy password
2128
            public_key: PGP public key in *armor* plain text format. Required
2129
                for pgp credential type.
2130
2131
        Examples:
2132
            Creating a Username + Password credential
2133
2134
            .. code-block:: python
2135
2136
                gmp.create_credential(
2137
                    name='UP Credential',
2138
                    credential_type=CredentialType.USERNAME_PASSWORD,
2139
                    login='foo',
2140
                    password='bar',
2141
                )
2142
2143
            Creating a Username + SSH Key credential
2144
2145
            .. code-block:: python
2146
2147
                with open('path/to/private-ssh-key') as f:
2148
                    key = f.read()
2149
2150
                gmp.create_credential(
2151
                    name='USK Credential',
2152
                    credential_type=CredentialType.USERNAME_SSH_KEY,
2153
                    login='foo',
2154
                    key_phrase='foobar',
2155
                    private_key=key,
2156
                )
2157
2158
            Creating a PGP credential
2159
2160
            .. note::
2161
2162
                A compatible public pgp key file can be exported with GnuPG via
2163
                ::
2164
2165
                    $ gpg --armor --export [email protected] > alice.asc
2166
2167
            .. code-block:: python
2168
2169
                with open('path/to/pgp.key.asc') as f:
2170
                    key = f.read()
2171
2172
                gmp.create_credential(
2173
                    name='PGP Credential',
2174
                    credential_type=CredentialType.PGP_ENCRYPTION_KEY,
2175
                    public_key=key,
2176
                )
2177
2178
            Creating a S/MIME credential
2179
2180
            .. code-block:: python
2181
2182
                with open('path/to/smime-cert') as f:
2183
                    cert = f.read()
2184
2185
                gmp.create_credential(
2186
                    name='SMIME Credential',
2187
                    credential_type=CredentialType.SMIME_CERTIFICATE,
2188
                    certificate=cert,
2189
                )
2190
2191
            Creating a Password-Only credential
2192
2193
            .. code-block:: python
2194
2195
                gmp.create_credential(
2196
                    name='Password-Only Credential',
2197
                    credential_type=CredentialType.PASSWORD_ONLY,
2198
                    password='foo',
2199
                )
2200
        Returns:
2201
            The response. See :py:meth:`send_command` for details.
2202
        """
2203
        if not name:
2204
            raise RequiredArgument(
2205
                function=self.create_credential.__name__, argument='name'
2206
            )
2207
2208
        if not isinstance(credential_type, self.types.CredentialType):
2209
            raise InvalidArgumentType(
2210
                function=self.create_credential.__name__,
2211
                argument='credential_type',
2212
                arg_type=CredentialType.__name__,
2213
            )
2214
2215
        cmd = XmlCommand("create_credential")
2216
        cmd.add_element("name", name)
2217
2218
        cmd.add_element("type", credential_type.value)
2219
2220
        if comment:
2221
            cmd.add_element("comment", comment)
2222
2223
        if allow_insecure is not None:
2224
            cmd.add_element("allow_insecure", to_bool(allow_insecure))
2225
2226
        if (
2227
            credential_type == CredentialType.CLIENT_CERTIFICATE
2228
            or credential_type == CredentialType.SMIME_CERTIFICATE
2229
        ):
2230
            if not certificate:
2231
                raise RequiredArgument(
2232
                    function=self.create_credential.__name__,
2233
                    argument='certificate',
2234
                )
2235
2236
            cmd.add_element("certificate", certificate)
2237
2238
        if (
2239
            credential_type == CredentialType.USERNAME_PASSWORD
2240
            or credential_type == CredentialType.USERNAME_SSH_KEY
2241
            or credential_type == CredentialType.SNMP
2242
        ):
2243
            if not login:
2244
                raise RequiredArgument(
2245
                    function=self.create_credential.__name__, argument='login'
2246
                )
2247
2248
            cmd.add_element("login", login)
2249
2250
        if credential_type == CredentialType.PASSWORD_ONLY and not password:
2251
            raise RequiredArgument(
2252
                function=self.create_credential.__name__, argument='password'
2253
            )
2254
2255
        if (
2256
            credential_type == CredentialType.USERNAME_PASSWORD
2257
            or credential_type == CredentialType.SNMP
2258
            or credential_type == CredentialType.PASSWORD_ONLY
2259
        ) and password:
2260
            cmd.add_element("password", password)
2261
2262
        if credential_type == CredentialType.USERNAME_SSH_KEY:
2263
            if not private_key:
2264
                raise RequiredArgument(
2265
                    function=self.create_credential.__name__,
2266
                    argument='private_key',
2267
                )
2268
2269
            _xmlkey = cmd.add_element("key")
2270
            _xmlkey.add_element("private", private_key)
2271
2272
            if key_phrase:
2273
                _xmlkey.add_element("phrase", key_phrase)
2274
2275
        if credential_type == CredentialType.CLIENT_CERTIFICATE and private_key:
2276
            _xmlkey = cmd.add_element("key")
2277
            _xmlkey.add_element("private", private_key)
2278
2279
        if credential_type == CredentialType.SNMP:
2280
            if not isinstance(auth_algorithm, self.types.SnmpAuthAlgorithm):
2281
                raise InvalidArgumentType(
2282
                    function=self.create_credential.__name__,
2283
                    argument='auth_algorithm',
2284
                    arg_type=SnmpAuthAlgorithm.__name__,
2285
                )
2286
2287
            cmd.add_element("auth_algorithm", auth_algorithm.value)
2288
2289
            if community:
2290
                cmd.add_element("community", community)
2291
2292 View Code Duplication
            if privacy_algorithm is not None or privacy_password:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2293
                _xmlprivacy = cmd.add_element("privacy")
2294
2295
                if privacy_algorithm is not None:
2296
                    if not isinstance(
2297
                        privacy_algorithm, self.types.SnmpPrivacyAlgorithm
2298
                    ):
2299
                        raise InvalidArgumentType(
2300
                            function=self.create_credential.__name__,
2301
                            argument='privacy_algorithm',
2302
                            arg_type=SnmpPrivacyAlgorithm.__name__,
2303
                        )
2304
2305
                    _xmlprivacy.add_element(
2306
                        "algorithm", privacy_algorithm.value
2307
                    )
2308
2309
                if privacy_password:
2310
                    _xmlprivacy.add_element("password", privacy_password)
2311
2312
        if credential_type == CredentialType.PGP_ENCRYPTION_KEY:
2313
            if not public_key:
2314
                raise RequiredArgument(
2315
                    function=self.create_credential.__name__,
2316
                    argument='public_key',
2317
                )
2318
2319
            _xmlkey = cmd.add_element("key")
2320
            _xmlkey.add_element("public", public_key)
2321
2322
        return self._send_xml_command(cmd)
2323
2324
    def modify_credential(
2325
        self,
2326
        credential_id: str,
2327
        *,
2328
        name: Optional[str] = None,
2329
        comment: Optional[str] = None,
2330
        allow_insecure: Optional[bool] = None,
2331
        certificate: Optional[str] = None,
2332
        key_phrase: Optional[str] = None,
2333
        private_key: Optional[str] = None,
2334
        login: Optional[str] = None,
2335
        password: Optional[str] = None,
2336
        auth_algorithm: Optional[SnmpAuthAlgorithm] = None,
2337
        community: Optional[str] = None,
2338
        privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None,
2339
        privacy_password: Optional[str] = None,
2340
        public_key: Optional[str] = None,
2341
    ) -> Any:
2342
        """Modifies an existing credential.
2343
2344
        Arguments:
2345
            credential_id: UUID of the credential
2346
            name: Name of the credential
2347
            comment: Comment for the credential
2348
            allow_insecure: Whether to allow insecure use of the credential
2349
            certificate: Certificate for the credential
2350
            key_phrase: Key passphrase for the private key
2351
            private_key: Private key to use for login
2352
            login: Username for the credential
2353
            password: Password for the credential
2354
            auth_algorithm: The authentication algorithm for SNMP
2355
            community: The SNMP community
2356
            privacy_algorithm: The privacy algorithm for SNMP
2357
            privacy_password: The SNMP privacy password
2358
            public_key: PGP public key in *armor* plain text format
2359
2360
        Returns:
2361
            The response. See :py:meth:`send_command` for details.
2362
        """
2363
        if not credential_id:
2364
            raise RequiredArgument(
2365
                function=self.modify_credential.__name__,
2366
                argument='credential_id',
2367
            )
2368
2369
        cmd = XmlCommand("modify_credential")
2370
        cmd.set_attribute("credential_id", credential_id)
2371
2372
        if comment:
2373
            cmd.add_element("comment", comment)
2374
2375
        if name:
2376
            cmd.add_element("name", name)
2377
2378
        if allow_insecure is not None:
2379
            cmd.add_element("allow_insecure", to_bool(allow_insecure))
2380
2381
        if certificate:
2382
            cmd.add_element("certificate", certificate)
2383
2384
        if key_phrase and private_key:
2385
            _xmlkey = cmd.add_element("key")
2386
            _xmlkey.add_element("phrase", key_phrase)
2387
            _xmlkey.add_element("private", private_key)
2388
        elif (not key_phrase and private_key) or (
2389
            key_phrase and not private_key
2390
        ):
2391
            raise RequiredArgument(
2392
                function=self.modify_credential.__name__,
2393
                argument='key_phrase and private_key',
2394
            )
2395
2396
        if login:
2397
            cmd.add_element("login", login)
2398
2399
        if password:
2400
            cmd.add_element("password", password)
2401
2402
        if auth_algorithm:
2403
            if not isinstance(auth_algorithm, self.types.SnmpAuthAlgorithm):
2404
                raise InvalidArgumentType(
2405
                    function=self.modify_credential.__name__,
2406
                    argument='auth_algorithm',
2407
                    arg_type=SnmpAuthAlgorithm.__name__,
2408
                )
2409
            cmd.add_element("auth_algorithm", auth_algorithm.value)
2410
2411
        if community:
2412
            cmd.add_element("community", community)
2413
2414 View Code Duplication
        if privacy_algorithm is not None or privacy_password is not None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2415
            _xmlprivacy = cmd.add_element("privacy")
2416
2417
            if privacy_algorithm is not None:
2418
                if not isinstance(
2419
                    privacy_algorithm, self.types.SnmpPrivacyAlgorithm
2420
                ):
2421
                    raise InvalidArgumentType(
2422
                        function=self.modify_credential.__name__,
2423
                        argument='privacy_algorithm',
2424
                        arg_type=SnmpPrivacyAlgorithm.__name__,
2425
                    )
2426
2427
                _xmlprivacy.add_element("algorithm", privacy_algorithm.value)
2428
2429
            if privacy_password is not None:
2430
                _xmlprivacy.add_element("password", privacy_password)
2431
2432
        if public_key:
2433
            _xmlkey = cmd.add_element("key")
2434
            _xmlkey.add_element("public", public_key)
2435
2436
        return self._send_xml_command(cmd)
2437
2438
    def create_ticket(
2439
        self,
2440
        *,
2441
        result_id: str,
2442
        assigned_to_user_id: str,
2443
        note: str,
2444
        comment: Optional[str] = None,
2445
    ) -> Any:
2446
        """Create a new ticket
2447
2448
        Arguments:
2449
            result_id: UUID of the result the ticket applies to
2450
            assigned_to_user_id: UUID of a user the ticket should be assigned to
2451
            note: A note about opening the ticket
2452
            comment: Comment for the ticket
2453
2454
        Returns:
2455
            The response. See :py:meth:`send_command` for details.
2456
        """
2457
        if not result_id:
2458
            raise RequiredArgument(
2459
                function=self.create_ticket.__name__, argument='result_id'
2460
            )
2461
2462
        if not assigned_to_user_id:
2463
            raise RequiredArgument(
2464
                function=self.create_ticket.__name__,
2465
                argument='assigned_to_user_id',
2466
            )
2467
2468
        if not note:
2469
            raise RequiredArgument(
2470
                function=self.create_ticket.__name__, argument='note'
2471
            )
2472
2473
        cmd = XmlCommand("create_ticket")
2474
2475
        _result = cmd.add_element("result")
2476
        _result.set_attribute("id", result_id)
2477
2478
        _assigned = cmd.add_element("assigned_to")
2479
        _user = _assigned.add_element("user")
2480
        _user.set_attribute("id", assigned_to_user_id)
2481
2482
        _note = cmd.add_element("open_note", note)
2483
2484
        if comment:
2485
            cmd.add_element("comment", comment)
2486
2487
        return self._send_xml_command(cmd)
2488
2489
    def delete_ticket(
2490
        self, ticket_id: str, *, ultimate: Optional[bool] = False
2491
    ):
2492
        """Deletes an existing ticket
2493
2494
        Arguments:
2495
            ticket_id: UUID of the ticket to be deleted.
2496
            ultimate: Whether to remove entirely, or to the trashcan.
2497
        """
2498
        if not ticket_id:
2499
            raise RequiredArgument(
2500
                function=self.delete_ticket.__name__, argument='ticket_id'
2501
            )
2502
2503
        cmd = XmlCommand("delete_ticket")
2504
        cmd.set_attribute("ticket_id", ticket_id)
2505
        cmd.set_attribute("ultimate", to_bool(ultimate))
2506
2507
        return self._send_xml_command(cmd)
2508
2509 View Code Duplication
    def get_tickets(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2510
        self,
2511
        *,
2512
        trash: Optional[bool] = None,
2513
        filter: Optional[str] = None,
2514
        filter_id: Optional[str] = None,
2515
    ) -> Any:
2516
        """Request a list of tickets
2517
2518
        Arguments:
2519
            filter: Filter term to use for the query
2520
            filter_id: UUID of an existing filter to use for the query
2521
            trash: True to request the tickets in the trashcan
2522
2523
        Returns:
2524
            The response. See :py:meth:`send_command` for details.
2525
        """
2526
        cmd = XmlCommand("get_tickets")
2527
2528
        add_filter(cmd, filter, filter_id)
2529
2530
        if trash is not None:
2531
            cmd.set_attribute("trash", to_bool(trash))
2532
2533
        return self._send_xml_command(cmd)
2534
2535
    def get_ticket(self, ticket_id: str) -> Any:
2536
        """Request a single ticket
2537
2538
        Arguments:
2539
            ticket_id: UUID of an existing ticket
2540
2541
        Returns:
2542
            The response. See :py:meth:`send_command` for details.
2543
        """
2544
        if not ticket_id:
2545
            raise RequiredArgument(
2546
                function=self.get_ticket.__name__, argument='ticket_id'
2547
            )
2548
2549
        cmd = XmlCommand("get_tickets")
2550
        cmd.set_attribute("ticket_id", ticket_id)
2551
        return self._send_xml_command(cmd)
2552
2553
    def get_vulnerabilities(
2554
        self, *, filter: Optional[str] = None, filter_id: Optional[str] = None
2555
    ) -> Any:
2556
        """Request a list of vulnerabilities
2557
2558
        Arguments:
2559
            filter: Filter term to use for the query
2560
            filter_id: UUID of an existing filter to use for the query
2561
        Returns:
2562
            The response. See :py:meth:`send_command` for details.
2563
        """
2564
        cmd = XmlCommand("get_vulns")
2565
2566
        add_filter(cmd, filter, filter_id)
2567
2568
        return self._send_xml_command(cmd)
2569
2570
    def get_vulnerability(self, vulnerability_id: str) -> Any:
2571
        """Request a single vulnerability
2572
2573
        Arguments:
2574
            vulnerability_id: ID of an existing vulnerability
2575
2576
        Returns:
2577
            The response. See :py:meth:`send_command` for details.
2578
        """
2579
        if not vulnerability_id:
2580
            raise RequiredArgument(
2581
                function=self.get_vulnerability.__name__,
2582
                argument='vulnerability_id',
2583
            )
2584
2585
        cmd = XmlCommand("get_vulns")
2586
        cmd.set_attribute("vuln_id", vulnerability_id)
2587
        return self._send_xml_command(cmd)
2588
2589
    def modify_ticket(
2590
        self,
2591
        ticket_id: str,
2592
        *,
2593
        status: Optional[TicketStatus] = None,
2594
        note: Optional[str] = None,
2595
        assigned_to_user_id: Optional[str] = None,
2596
        comment: Optional[str] = None,
2597
    ) -> Any:
2598
        """Modify a single ticket
2599
2600
        Arguments:
2601
            ticket_id: UUID of an existing ticket
2602
            status: New status for the ticket
2603
            note: Note for the status change. Required if status is set.
2604
            assigned_to_user_id: UUID of the user the ticket should be assigned
2605
                to
2606
            comment: Comment for the ticket
2607
2608
        Returns:
2609
            The response. See :py:meth:`send_command` for details.
2610
        """
2611
        if not ticket_id:
2612
            raise RequiredArgument(
2613
                function=self.modify_ticket.__name__, argument='ticket_id'
2614
            )
2615
2616
        if status and not note:
2617
            raise RequiredArgument(
2618
                function=self.modify_ticket.__name__, argument='note'
2619
            )
2620
2621
        if note and not status:
2622
            raise RequiredArgument(
2623
                function=self.modify_ticket.__name__, argument='status'
2624
            )
2625
2626
        cmd = XmlCommand("modify_ticket")
2627
        cmd.set_attribute("ticket_id", ticket_id)
2628
2629
        if assigned_to_user_id:
2630
            _assigned = cmd.add_element("assigned_to")
2631
            _user = _assigned.add_element("user")
2632
            _user.set_attribute("id", assigned_to_user_id)
2633
2634
        if status:
2635
            if not isinstance(status, self.types.TicketStatus):
2636
                raise InvalidArgumentType(
2637
                    function=self.modify_ticket.__name__,
2638
                    argument='status',
2639
                    arg_type=TicketStatus.__name__,
2640
                )
2641
2642
            cmd.add_element('status', status.value)
2643
            cmd.add_element('{}_note'.format(status.name.lower()), note)
2644
2645
        if comment:
2646
            cmd.add_element("comment", comment)
2647
2648
        return self._send_xml_command(cmd)
2649
2650 View Code Duplication
    def create_filter(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2651
        self,
2652
        name: str,
2653
        *,
2654
        filter_type: Optional[FilterType] = None,
2655
        comment: Optional[str] = None,
2656
        term: Optional[str] = None,
2657
    ) -> Any:
2658
        """Create a new filter
2659
2660
        Arguments:
2661
            name: Name of the new filter
2662
            filter_type: Filter for entity type
2663
            comment: Comment for the filter
2664
            term: Filter term e.g. 'name=foo'
2665
2666
        Returns:
2667
            The response. See :py:meth:`send_command` for details.
2668
        """
2669
        if not name:
2670
            raise RequiredArgument(
2671
                function=self.create_filter.__name__, argument="name"
2672
            )
2673
2674
        cmd = XmlCommand("create_filter")
2675
        _xmlname = cmd.add_element("name", name)
2676
2677
        if comment:
2678
            cmd.add_element("comment", comment)
2679
2680
        if term:
2681
            cmd.add_element("term", term)
2682
2683
        if filter_type:
2684
            if not isinstance(filter_type, self.types.FilterType):
2685
                raise InvalidArgumentType(
2686
                    function=self.create_filter.__name__,
2687
                    argument="filter_type",
2688
                    arg_type=self.types.FilterType.__name__,
2689
                )
2690
2691
            cmd.add_element("type", filter_type.value)
2692
2693
        return self._send_xml_command(cmd)
2694
2695 View Code Duplication
    def modify_filter(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2696
        self,
2697
        filter_id: str,
2698
        *,
2699
        comment: Optional[str] = None,
2700
        name: Optional[str] = None,
2701
        term: Optional[str] = None,
2702
        filter_type: Optional[FilterType] = None,
2703
    ) -> Any:
2704
        """Modifies an existing filter.
2705
2706
        Arguments:
2707
            filter_id: UUID of the filter to be modified
2708
            comment: Comment on filter.
2709
            name: Name of filter.
2710
            term: Filter term.
2711
            filter_type: Resource type filter applies to.
2712
2713
        Returns:
2714
            The response. See :py:meth:`send_command` for details.
2715
        """
2716
        if not filter_id:
2717
            raise RequiredArgument(
2718
                function=self.modify_filter.__name__, argument='filter_id'
2719
            )
2720
2721
        cmd = XmlCommand("modify_filter")
2722
        cmd.set_attribute("filter_id", filter_id)
2723
2724
        if comment:
2725
            cmd.add_element("comment", comment)
2726
2727
        if name:
2728
            cmd.add_element("name", name)
2729
2730
        if term:
2731
            cmd.add_element("term", term)
2732
2733
        if filter_type:
2734
            if not isinstance(filter_type, self.types.FilterType):
2735
                raise InvalidArgumentType(
2736
                    function=self.modify_filter.__name__,
2737
                    argument='filter_type',
2738
                    arg_type=FilterType.__name__,
2739
                )
2740
            cmd.add_element("type", filter_type.value)
2741
2742
        return self._send_xml_command(cmd)
2743
2744
    def create_schedule(
2745
        self,
2746
        name: str,
2747
        icalendar: str,
2748
        timezone: str,
2749
        *,
2750
        comment: Optional[str] = None,
2751
    ) -> Any:
2752
        """Create a new schedule based in `iCalendar`_ data.
2753
2754
        Example:
2755
            Requires https://pypi.org/project/icalendar/
2756
2757
            .. code-block:: python
2758
2759
                import pytz
2760
2761
                from datetime import datetime
2762
2763
                from icalendar import Calendar, Event
2764
2765
                cal = Calendar()
2766
2767
                cal.add('prodid', '-//Foo Bar//')
2768
                cal.add('version', '2.0')
2769
2770
                event = Event()
2771
                event.add('dtstamp', datetime.now(tz=pytz.UTC))
2772
                event.add('dtstart', datetime(2020, 1, 1, tzinfo=pytz.utc))
2773
2774
                cal.add_component(event)
2775
2776
                gmp.create_schedule(
2777
                    name="My Schedule",
2778
                    icalendar=cal.to_ical(),
2779
                    timezone='UTC'
2780
                )
2781
2782
2783
        Arguments:
2784
            name: Name of the new schedule
2785
            icalendar: `iCalendar`_ (RFC 5545) based data.
2786
            timezone: Timezone to use for the icalender events e.g
2787
                Europe/Berlin. If the datetime values in the icalendar data are
2788
                missing timezone information this timezone gets applied.
2789
                Otherwise the datetime values from the icalendar data are
2790
                displayed in this timezone
2791
            comment: Comment on schedule.
2792
2793
        Returns:
2794
            The response. See :py:meth:`send_command` for details.
2795
2796
        .. _iCalendar:
2797
            https://tools.ietf.org/html/rfc5545
2798
        """
2799
        if not name:
2800
            raise RequiredArgument(
2801
                function=self.create_schedule.__name__, argument='name'
2802
            )
2803
        if not icalendar:
2804
            raise RequiredArgument(
2805
                function=self.create_schedule.__name__, argument='icalendar'
2806
            )
2807
        if not timezone:
2808
            raise RequiredArgument(
2809
                function=self.create_schedule.__name__, argument='timezone'
2810
            )
2811
2812
        cmd = XmlCommand("create_schedule")
2813
2814
        cmd.add_element("name", name)
2815
        cmd.add_element("icalendar", icalendar)
2816
        cmd.add_element("timezone", timezone)
2817
2818
        if comment:
2819
            cmd.add_element("comment", comment)
2820
2821
        return self._send_xml_command(cmd)
2822
2823
    def modify_schedule(
2824
        self,
2825
        schedule_id: str,
2826
        *,
2827
        name: Optional[str] = None,
2828
        icalendar: Optional[str] = None,
2829
        timezone: Optional[str] = None,
2830
        comment: Optional[str] = None,
2831
    ) -> Any:
2832
        """Modifies an existing schedule
2833
2834
        Arguments:
2835
            schedule_id: UUID of the schedule to be modified
2836
            name: Name of the schedule
2837
            icalendar: `iCalendar`_ (RFC 5545) based data.
2838
            timezone: Timezone to use for the icalender events e.g
2839
                Europe/Berlin. If the datetime values in the icalendar data are
2840
                missing timezone information this timezone gets applied.
2841
                Otherwise the datetime values from the icalendar data are
2842
                displayed in this timezone
2843
            commenhedule.
2844
2845
        Returns:
2846
            The response. See :py:meth:`send_command` for details.
2847
2848
        .. _iCalendar:
2849
            https://tools.ietf.org/html/rfc5545
2850
        """
2851
        if not schedule_id:
2852
            raise RequiredArgument(
2853
                function=self.modify_schedule.__name__, argument='schedule_id'
2854
            )
2855
2856
        cmd = XmlCommand("modify_schedule")
2857
        cmd.set_attribute("schedule_id", schedule_id)
2858
2859
        if name:
2860
            cmd.add_element("name", name)
2861
2862
        if icalendar:
2863
            cmd.add_element("icalendar", icalendar)
2864
2865
        if timezone:
2866
            cmd.add_element("timezone", timezone)
2867
2868
        if comment:
2869
            cmd.add_element("comment", comment)
2870
2871
        return self._send_xml_command(cmd)
2872
2873
    def clone_config(self, config_id: str) -> Any:
2874
        """Clone a scan config from an existing one
2875
2876
        Arguments:
2877
            config_id: UUID of the existing scan config
2878
2879
        Returns:
2880
            The response. See :py:meth:`send_command` for details.
2881
        """
2882
        if not config_id:
2883
            raise RequiredArgument(
2884
                function=self.clone_config.__name__, argument='config_id'
2885
            )
2886
2887
        cmd = XmlCommand("create_config")
2888
        cmd.add_element("copy", config_id)
2889
        return self._send_xml_command(cmd)
2890
2891
    def import_config(self, config: str) -> Any:
2892
        """Import a scan config from XML
2893
2894
        Arguments:
2895
            config: Scan Config XML as string to import. This XML must
2896
                contain a :code:`<get_configs_response>` root element.
2897
2898
        Returns:
2899
            The response. See :py:meth:`send_command` for details.
2900
        """
2901
        if not config:
2902
            raise RequiredArgument(
2903
                function=self.import_config.__name__, argument='config'
2904
            )
2905
2906
        cmd = XmlCommand("create_config")
2907
2908
        try:
2909
            cmd.append_xml_str(config)
2910
        except etree.XMLSyntaxError as e:
2911
            raise InvalidArgument(
2912
                function=self.import_config.__name__, argument='config'
2913
            ) from e
2914
2915
        return self._send_xml_command(cmd)
2916
2917
    def clone_credential(self, credential_id: str) -> Any:
2918
        """Clone an existing credential
2919
2920
        Arguments:
2921
            credential_id: UUID of an existing credential to clone from
2922
2923
        Returns:
2924
            The response. See :py:meth:`send_command` for details.
2925
        """
2926
        if not credential_id:
2927
            raise RequiredArgument(
2928
                function=self.clone_credential.__name__,
2929
                argument='credential_id',
2930
            )
2931
2932
        cmd = XmlCommand("create_credential")
2933
        cmd.add_element("copy", credential_id)
2934
        return self._send_xml_command(cmd)
2935
2936
    def clone_filter(self, filter_id: str) -> Any:
2937
        """Clone an existing filter
2938
2939
        Arguments:
2940
            filter_id: UUID of an existing filter to clone from
2941
2942
        Returns:
2943
            The response. See :py:meth:`send_command` for details.
2944
        """
2945
        if not filter_id:
2946
            raise RequiredArgument(
2947
                function=self.clone_filter.__name__, argument='filter_id'
2948
            )
2949
2950
        cmd = XmlCommand("create_filter")
2951
        cmd.add_element("copy", filter_id)
2952
        return self._send_xml_command(cmd)
2953
2954
    def create_group(
2955
        self,
2956
        name: str,
2957
        *,
2958
        comment: Optional[str] = None,
2959
        special: Optional[bool] = False,
2960
        users: Optional[List[str]] = None,
2961
    ) -> Any:
2962
        """Create a new group
2963
2964
        Arguments:
2965
            name: Name of the new group
2966
            comment: Comment for the group
2967
            special: Create permission giving members full access to each
2968
                other's entities
2969
            users: List of user names to be in the group
2970
2971
        Returns:
2972
            The response. See :py:meth:`send_command` for details.
2973
        """
2974
        if not name:
2975
            raise RequiredArgument(
2976
                function=self.create_group.__name__, argument='name'
2977
            )
2978
2979
        cmd = XmlCommand("create_group")
2980
        cmd.add_element("name", name)
2981
2982
        if comment:
2983
            cmd.add_element("comment", comment)
2984
2985
        if special:
2986
            _xmlspecial = cmd.add_element("specials")
2987
            _xmlspecial.add_element("full")
2988
2989
        if users:
2990
            cmd.add_element("users", to_comma_list(users))
2991
2992
        return self._send_xml_command(cmd)
2993
2994
    def clone_group(self, group_id: str) -> Any:
2995
        """Clone an existing group
2996
2997
        Arguments:
2998
            group_id: UUID of an existing group to clone from
2999
3000
        Returns:
3001
            The response. See :py:meth:`send_command` for details.
3002
        """
3003
        if not group_id:
3004
            raise RequiredArgument(
3005
                function=self.clone_group.__name__, argument='group_id'
3006
            )
3007
3008
        cmd = XmlCommand("create_group")
3009
        cmd.add_element("copy", group_id)
3010
        return self._send_xml_command(cmd)
3011
3012
    def create_host(self, name: str, *, comment: Optional[str] = None) -> Any:
3013
        """Create a new host asset
3014
3015
        Arguments:
3016
            name: Name for the new host asset
3017
            comment: Comment for the new host asset
3018
3019
        Returns:
3020
            The response. See :py:meth:`send_command` for details.
3021
        """
3022
        if not name:
3023
            raise RequiredArgument(
3024
                function=self.create_host.__name__, argument='name'
3025
            )
3026
3027
        cmd = XmlCommand("create_asset")
3028
        asset = cmd.add_element("asset")
3029
        asset.add_element("type", "host")  # ignored for gmp7, required for gmp8
3030
        asset.add_element("name", name)
3031
3032
        if comment:
3033
            asset.add_element("comment", comment)
3034
3035
        return self._send_xml_command(cmd)
3036
3037 View Code Duplication
    def create_note(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3038
        self,
3039
        text: str,
3040
        nvt_oid: str,
3041
        *,
3042
        days_active: Optional[int] = None,
3043
        hosts: Optional[List[str]] = None,
3044
        port: Optional[int] = None,
3045
        result_id: Optional[str] = None,
3046
        severity: Optional[Severity] = None,
3047
        task_id: Optional[str] = None,
3048
        threat: Optional[SeverityLevel] = None,
3049
    ) -> Any:
3050
        """Create a new note
3051
3052
        Arguments:
3053
            text: Text of the new note
3054
            nvt_id: OID of the nvt to which note applies
3055
            days_active: Days note will be active. -1 on
3056
                always, 0 off
3057
            hosts: A list of hosts addresses
3058
            port: Port to which the note applies
3059
            result_id: UUID of a result to which note applies
3060
            severity: Severity to which note applies
3061
            task_id: UUID of task to which note applies
3062
            threat: Severity level to which note applies. Will be converted to
3063
                severity.
3064
3065
        Returns:
3066
            The response. See :py:meth:`send_command` for details.
3067
        """
3068
        if not text:
3069
            raise RequiredArgument(
3070
                function=self.create_note.__name__, argument='text'
3071
            )
3072
3073
        if not nvt_oid:
3074
            raise RequiredArgument(
3075
                function=self.create_note.__name__, argument='nvt_oid'
3076
            )
3077
3078
        cmd = XmlCommand("create_note")
3079
        cmd.add_element("text", text)
3080
        cmd.add_element("nvt", attrs={"oid": nvt_oid})
3081
3082
        if days_active is not None:
3083
            cmd.add_element("active", str(days_active))
3084
3085
        if hosts:
3086
            cmd.add_element("hosts", to_comma_list(hosts))
3087
3088
        if port:
3089
            cmd.add_element("port", str(port))
3090
3091
        if result_id:
3092
            cmd.add_element("result", attrs={"id": result_id})
3093
3094
        if severity:
3095
            cmd.add_element("severity", str(severity))
3096
3097
        if task_id:
3098
            cmd.add_element("task", attrs={"id": task_id})
3099
3100
        if threat is not None:
3101
            if not isinstance(threat, SeverityLevel):
3102
                raise InvalidArgumentType(
3103
                    function="create_note",
3104
                    argument="threat",
3105
                    arg_type=SeverityLevel.__name__,
3106
                )
3107
3108
            cmd.add_element("threat", threat.value)
3109
3110
        return self._send_xml_command(cmd)
3111
3112
    def clone_note(self, note_id: str) -> Any:
3113
        """Clone an existing note
3114
3115
        Arguments:
3116
            note_id: UUID of an existing note to clone from
3117
3118
        Returns:
3119
            The response. See :py:meth:`send_command` for details.
3120
        """
3121
        if not note_id:
3122
            raise RequiredArgument(
3123
                function=self.clone_note.__name__, argument='note_id'
3124
            )
3125
3126
        cmd = XmlCommand("create_note")
3127
        cmd.add_element("copy", note_id)
3128
        return self._send_xml_command(cmd)
3129
3130 View Code Duplication
    def create_override(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3131
        self,
3132
        text: str,
3133
        nvt_oid: str,
3134
        *,
3135
        days_active: Optional[int] = None,
3136
        hosts: Optional[List[str]] = None,
3137
        port: Optional[int] = None,
3138
        result_id: Optional[str] = None,
3139
        severity: Optional[Severity] = None,
3140
        new_severity: Optional[Severity] = None,
3141
        task_id: Optional[str] = None,
3142
        threat: Optional[SeverityLevel] = None,
3143
        new_threat: Optional[SeverityLevel] = None,
3144
    ) -> Any:
3145
        """Create a new override
3146
3147
        Arguments:
3148
            text: Text of the new override
3149
            nvt_id: OID of the nvt to which override applies
3150
            days_active: Days override will be active. -1 on always, 0 off
3151
            hosts: A list of host addresses
3152
            port: Port to which the override applies
3153
            result_id: UUID of a result to which override applies
3154
            severity: Severity to which override applies
3155
            new_severity: New severity for result
3156
            task_id: UUID of task to which override applies
3157
            threat: Severity level to which override applies. Will be converted
3158
                to severity.
3159
            new_threat: New severity level for results. Will be converted to
3160
                new_severity.
3161
3162
        Returns:
3163
            The response. See :py:meth:`send_command` for details.
3164
        """
3165
        if not text:
3166
            raise RequiredArgument(
3167
                function=self.create_override.__name__, argument='text'
3168
            )
3169
3170
        if not nvt_oid:
3171
            raise RequiredArgument(
3172
                function=self.create_override.__name__, argument='nvt_oid'
3173
            )
3174
3175
        cmd = XmlCommand("create_override")
3176
        cmd.add_element("text", text)
3177
        cmd.add_element("nvt", attrs={"oid": nvt_oid})
3178
3179
        if days_active is not None:
3180
            cmd.add_element("active", str(days_active))
3181
3182
        if hosts:
3183
            cmd.add_element("hosts", to_comma_list(hosts))
3184
3185
        if port:
3186
            cmd.add_element("port", str(port))
3187
3188
        if result_id:
3189
            cmd.add_element("result", attrs={"id": result_id})
3190
3191
        if severity:
3192
            cmd.add_element("severity", str(severity))
3193
3194
        if new_severity:
3195
            cmd.add_element("new_severity", str(new_severity))
3196
3197
        if task_id:
3198
            cmd.add_element("task", attrs={"id": task_id})
3199
3200
        if threat is not None:
3201
            if not isinstance(threat, SeverityLevel):
3202
                raise InvalidArgumentType(
3203
                    function=self.create_override.__name__,
3204
                    argument="threat",
3205
                    arg_type=SeverityLevel.__name__,
3206
                )
3207
3208
            cmd.add_element("threat", threat.value)
3209
3210
        if new_threat is not None:
3211
            if not isinstance(new_threat, SeverityLevel):
3212
                raise InvalidArgumentType(
3213
                    function=self.create_override.__name__,
3214
                    argument="new_threat",
3215
                    arg_type=SeverityLevel.__name__,
3216
                )
3217
3218
            cmd.add_element("new_threat", new_threat.value)
3219
3220
        return self._send_xml_command(cmd)
3221
3222
    def clone_override(self, override_id: str) -> Any:
3223
        """Clone an existing override
3224
3225
        Arguments:
3226
            override_id: UUID of an existing override to clone from
3227
3228
        Returns:
3229
            The response. See :py:meth:`send_command` for details.
3230
        """
3231
        if not override_id:
3232
            raise RequiredArgument(
3233
                function=self.clone_override.__name__, argument='override_id'
3234
            )
3235
3236
        cmd = XmlCommand("create_override")
3237
        cmd.add_element("copy", override_id)
3238
        return self._send_xml_command(cmd)
3239
3240
    def clone_permission(self, permission_id: str) -> Any:
3241
        """Clone an existing permission
3242
3243
        Arguments:
3244
            permission_id: UUID of an existing permission to clone from
3245
3246
        Returns:
3247
            The response. See :py:meth:`send_command` for details.
3248
        """
3249
        if not permission_id:
3250
            raise RequiredArgument(
3251
                function=self.clone_permission.__name__,
3252
                argument='permission_id',
3253
            )
3254
3255
        cmd = XmlCommand("create_permission")
3256
        cmd.add_element("copy", permission_id)
3257
        return self._send_xml_command(cmd)
3258
3259
    def clone_report_format(
3260
        self, report_format_id: [Union[str, ReportFormatType]]
3261
    ) -> Any:
3262
        """Clone a report format from an existing one
3263
3264
        Arguments:
3265
            report_format_id: UUID of the existing report format
3266
                              or ReportFormatType (enum)
3267
3268
        Returns:
3269
            The response. See :py:meth:`send_command` for details.
3270
        """
3271
        if not report_format_id:
3272
            raise RequiredArgument(
3273
                function=self.clone_report_format.__name__,
3274
                argument='report_format_id',
3275
            )
3276
3277
        cmd = XmlCommand("create_report_format")
3278
3279
        if isinstance(report_format_id, ReportFormatType):
3280
            report_format_id = report_format_id.value
3281
3282
        cmd.add_element("copy", report_format_id)
3283
        return self._send_xml_command(cmd)
3284
3285
    def import_report_format(self, report_format: str) -> Any:
3286
        """Import a report format from XML
3287
3288
        Arguments:
3289
            report_format: Report format XML as string to import. This XML must
3290
                contain a :code:`<get_report_formats_response>` root element.
3291
3292
        Returns:
3293
            The response. See :py:meth:`send_command` for details.
3294
        """
3295
        if not report_format:
3296
            raise RequiredArgument(
3297
                function=self.import_report_format.__name__,
3298
                argument='report_format',
3299
            )
3300
3301
        cmd = XmlCommand("create_report_format")
3302
3303
        try:
3304
            cmd.append_xml_str(report_format)
3305
        except etree.XMLSyntaxError as e:
3306
            raise InvalidArgument(
3307
                function=self.import_report_format.__name__,
3308
                argument='report_format',
3309
            ) from e
3310
3311
        return self._send_xml_command(cmd)
3312
3313
    def create_role(
3314
        self,
3315
        name: str,
3316
        *,
3317
        comment: Optional[str] = None,
3318
        users: Optional[List[str]] = None,
3319
    ) -> Any:
3320
        """Create a new role
3321
3322
        Arguments:
3323
            name: Name of the role
3324
            comment: Comment for the role
3325
            users: List of user names to add to the role
3326
3327
        Returns:
3328
            The response. See :py:meth:`send_command` for details.
3329
        """
3330
3331
        if not name:
3332
            raise RequiredArgument(
3333
                function=self.create_role.__name__, argument='name'
3334
            )
3335
3336
        cmd = XmlCommand("create_role")
3337
        cmd.add_element("name", name)
3338
3339
        if comment:
3340
            cmd.add_element("comment", comment)
3341
3342
        if users:
3343
            cmd.add_element("users", to_comma_list(users))
3344
3345
        return self._send_xml_command(cmd)
3346
3347
    def clone_role(self, role_id: str) -> Any:
3348
        """Clone an existing role
3349
3350
        Arguments:
3351
            role_id: UUID of an existing role to clone from
3352
3353
        Returns:
3354
            The response. See :py:meth:`send_command` for details.
3355
        """
3356
        if not role_id:
3357
            raise RequiredArgument(
3358
                function=self.clone_role.__name__, argument='role_id'
3359
            )
3360
3361
        cmd = XmlCommand("create_role")
3362
        cmd.add_element("copy", role_id)
3363
        return self._send_xml_command(cmd)
3364
3365
    def create_scanner(
3366
        self,
3367
        name: str,
3368
        host: str,
3369
        port: int,
3370
        scanner_type: ScannerType,
3371
        credential_id: str,
3372
        *,
3373
        ca_pub: Optional[str] = None,
3374
        comment: Optional[str] = None,
3375
    ) -> Any:
3376
        """Create a new scanner
3377
3378
        Arguments:
3379
            name: Name of the scanner
3380
            host: The host of the scanner
3381
            port: The port of the scanner
3382
            scanner_type: Type of the scanner.
3383
            credential_id: UUID of client certificate credential for the
3384
                scanner
3385
            ca_pub: Certificate of CA to verify scanner certificate
3386
            comment: Comment for the scanner
3387
        Returns:
3388
            The response. See :py:meth:`send_command` for details.
3389
        """
3390
        if not name:
3391
            raise RequiredArgument(
3392
                function=self.create_scanner.__name__, argument='name'
3393
            )
3394
3395
        if not host:
3396
            raise RequiredArgument(
3397
                function=self.create_scanner.__name__, argument='host'
3398
            )
3399
3400
        if not port:
3401
            raise RequiredArgument(
3402
                function=self.create_scanner.__name__, argument='port'
3403
            )
3404
3405
        if not scanner_type:
3406
            raise RequiredArgument(
3407
                function=self.create_scanner.__name__, argument='scanner_type'
3408
            )
3409
3410
        if not credential_id:
3411
            raise RequiredArgument(
3412
                function=self.create_scanner.__name__, argument='credential_id'
3413
            )
3414
3415
        if not isinstance(scanner_type, self.types.ScannerType):
3416
            raise InvalidArgumentType(
3417
                function=self.create_scanner.__name__,
3418
                argument='scanner_type',
3419
                arg_type=self.types.ScannerType.__name__,
3420
            )
3421
3422
        cmd = XmlCommand("create_scanner")
3423
        cmd.add_element("name", name)
3424
        cmd.add_element("host", host)
3425
        cmd.add_element("port", str(port))
3426
        cmd.add_element("type", scanner_type.value)
3427
3428
        if ca_pub:
3429
            cmd.add_element("ca_pub", ca_pub)
3430
3431
        cmd.add_element("credential", attrs={"id": str(credential_id)})
3432
3433
        if comment:
3434
            cmd.add_element("comment", comment)
3435
3436
        return self._send_xml_command(cmd)
3437
3438
    def clone_scanner(self, scanner_id: str) -> Any:
3439
        """Clone an existing scanner
3440
3441
        Arguments:
3442
            scanner_id: UUID of an existing scanner to clone from
3443
3444
        Returns:
3445
            The response. See :py:meth:`send_command` for details.
3446
        """
3447
        if not scanner_id:
3448
            raise RequiredArgument(
3449
                function=self.clone_scanner.__name__, argument='scanner_id'
3450
            )
3451
3452
        cmd = XmlCommand("create_scanner")
3453
        cmd.add_element("copy", scanner_id)
3454
        return self._send_xml_command(cmd)
3455
3456
    def clone_schedule(self, schedule_id: str) -> Any:
3457
        """Clone an existing schedule
3458
3459
        Arguments:
3460
            schedule_id: UUID of an existing schedule to clone from
3461
3462
        Returns:
3463
            The response. See :py:meth:`send_command` for details.
3464
        """
3465
        if not schedule_id:
3466
            raise RequiredArgument(
3467
                function=self.clone_schedule.__name__, argument='schedule_id'
3468
            )
3469
3470
        cmd = XmlCommand("create_schedule")
3471
        cmd.add_element("copy", schedule_id)
3472
        return self._send_xml_command(cmd)
3473
3474
    def clone_tag(self, tag_id: str) -> Any:
3475
        """Clone an existing tag
3476
3477
        Arguments:
3478
            tag_id: UUID of an existing tag to clone from
3479
3480
        Returns:
3481
            The response. See :py:meth:`send_command` for details.
3482
        """
3483
        if not tag_id:
3484
            raise RequiredArgument(
3485
                function=self.clone_tag.__name__, argument='tag_id'
3486
            )
3487
3488
        cmd = XmlCommand("create_tag")
3489
        cmd.add_element("copy", tag_id)
3490
        return self._send_xml_command(cmd)
3491
3492
    def create_user(
3493
        self,
3494
        name: str,
3495
        *,
3496
        password: Optional[str] = None,
3497
        hosts: Optional[List[str]] = None,
3498
        hosts_allow: Optional[bool] = False,
3499
        ifaces: Optional[List[str]] = None,
3500
        ifaces_allow: Optional[bool] = False,
3501
        role_ids: Optional[List[str]] = None,
3502
    ) -> Any:
3503
        """Create a new user
3504
3505
        Arguments:
3506
            name: Name of the user
3507
            password: Password of the user
3508
            hosts: A list of host addresses (IPs, DNS names)
3509
            hosts_allow: If True allow only access to passed hosts otherwise
3510
                deny access. Default is False for deny hosts.
3511
            ifaces: A list of interface names
3512
            ifaces_allow: If True allow only access to passed interfaces
3513
                otherwise deny access. Default is False for deny interfaces.
3514
            role_ids: A list of role UUIDs for the user
3515
3516
        Returns:
3517
            The response. See :py:meth:`send_command` for details.
3518
        """
3519
        if not name:
3520
            raise RequiredArgument(
3521
                function=self.create_user.__name__, argument='name'
3522
            )
3523
3524
        cmd = XmlCommand("create_user")
3525
        cmd.add_element("name", name)
3526
3527
        if password:
3528
            cmd.add_element("password", password)
3529
3530
        if hosts:
3531
            cmd.add_element(
3532
                "hosts",
3533
                to_comma_list(hosts),
3534
                attrs={"allow": to_bool(hosts_allow)},
3535
            )
3536
3537
        if ifaces:
3538
            cmd.add_element(
3539
                "ifaces",
3540
                to_comma_list(ifaces),
3541
                attrs={"allow": to_bool(ifaces_allow)},
3542
            )
3543
3544
        if role_ids:
3545
            for role in role_ids:
3546
                cmd.add_element("role", attrs={"id": role})
3547
3548
        return self._send_xml_command(cmd)
3549
3550
    def clone_user(self, user_id: str) -> Any:
3551
        """Clone an existing user
3552
3553
        Arguments:
3554
            user_id: UUID of existing user to clone from
3555
3556
        Returns:
3557
            The response. See :py:meth:`send_command` for details.
3558
        """
3559
        if not user_id:
3560
            raise RequiredArgument(
3561
                function=self.clone_user.__name__, argument='user_id'
3562
            )
3563
3564
        cmd = XmlCommand("create_user")
3565
        cmd.add_element("copy", user_id)
3566
        return self._send_xml_command(cmd)
3567
3568
    def delete_alert(
3569
        self, alert_id: str, *, ultimate: Optional[bool] = False
3570
    ) -> Any:
3571
        """Deletes an existing alert
3572
3573
        Arguments:
3574
            alert_id: UUID of the alert to be deleted.
3575
            ultimate: Whether to remove entirely, or to the trashcan.
3576
        """
3577
        if not alert_id:
3578
            raise RequiredArgument(
3579
                function=self.delete_alert.__name__, argument='alert_id'
3580
            )
3581
3582
        cmd = XmlCommand("delete_alert")
3583
        cmd.set_attribute("alert_id", alert_id)
3584
        cmd.set_attribute("ultimate", to_bool(ultimate))
3585
3586
        return self._send_xml_command(cmd)
3587
3588
    def delete_asset(
3589
        self, *, asset_id: Optional[str] = None, report_id: Optional[str] = None
3590
    ) -> Any:
3591
        """Deletes an existing asset
3592
3593
        Arguments:
3594
            asset_id: UUID of the single asset to delete.
3595
            report_id: UUID of report from which to get all
3596
                assets to delete.
3597
        """
3598
        if not asset_id and not report_id:
3599
            raise RequiredArgument(
3600
                function=self.delete_asset.__name__,
3601
                argument='asset_id or report_id',
3602
            )
3603
3604
        cmd = XmlCommand("delete_asset")
3605
        if asset_id:
3606
            cmd.set_attribute("asset_id", asset_id)
3607
        else:
3608
            cmd.set_attribute("report_id", report_id)
3609
3610
        return self._send_xml_command(cmd)
3611
3612
    def delete_config(
3613
        self, config_id: str, *, ultimate: Optional[bool] = False
3614
    ) -> Any:
3615
        """Deletes an existing config
3616
3617
        Arguments:
3618
            config_id: UUID of the config to be deleted.
3619
            ultimate: Whether to remove entirely, or to the trashcan.
3620
        """
3621
        if not config_id:
3622
            raise RequiredArgument(
3623
                function=self.delete_config.__name__, argument='config_id'
3624
            )
3625
3626
        cmd = XmlCommand("delete_config")
3627
        cmd.set_attribute("config_id", config_id)
3628
        cmd.set_attribute("ultimate", to_bool(ultimate))
3629
3630
        return self._send_xml_command(cmd)
3631
3632
    def delete_credential(
3633
        self, credential_id: str, *, ultimate: Optional[bool] = False
3634
    ) -> Any:
3635
        """Deletes an existing credential
3636
3637
        Arguments:
3638
            credential_id: UUID of the credential to be deleted.
3639
            ultimate: Whether to remove entirely, or to the trashcan.
3640
        """
3641
        if not credential_id:
3642
            raise RequiredArgument(
3643
                function=self.delete_credential.__name__,
3644
                argument='credential_id',
3645
            )
3646
3647
        cmd = XmlCommand("delete_credential")
3648
        cmd.set_attribute("credential_id", credential_id)
3649
        cmd.set_attribute("ultimate", to_bool(ultimate))
3650
3651
        return self._send_xml_command(cmd)
3652
3653
    def delete_filter(
3654
        self, filter_id: str, *, ultimate: Optional[bool] = False
3655
    ) -> Any:
3656
        """Deletes an existing filter
3657
3658
        Arguments:
3659
            filter_id: UUID of the filter to be deleted.
3660
            ultimate: Whether to remove entirely, or to the trashcan.
3661
        """
3662
        if not filter_id:
3663
            raise RequiredArgument(
3664
                function=self.delete_filter.__name__, argument='filter_id'
3665
            )
3666
3667
        cmd = XmlCommand("delete_filter")
3668
        cmd.set_attribute("filter_id", filter_id)
3669
        cmd.set_attribute("ultimate", to_bool(ultimate))
3670
3671
        return self._send_xml_command(cmd)
3672
3673
    def delete_group(
3674
        self, group_id: str, *, ultimate: Optional[bool] = False
3675
    ) -> Any:
3676
        """Deletes an existing group
3677
3678
        Arguments:
3679
            group_id: UUID of the group to be deleted.
3680
            ultimate: Whether to remove entirely, or to the trashcan.
3681
        """
3682
        if not group_id:
3683
            raise RequiredArgument(
3684
                function=self.delete_group.__name__, argument='group_id'
3685
            )
3686
3687
        cmd = XmlCommand("delete_group")
3688
        cmd.set_attribute("group_id", group_id)
3689
        cmd.set_attribute("ultimate", to_bool(ultimate))
3690
3691
        return self._send_xml_command(cmd)
3692
3693
    def delete_note(
3694
        self, note_id: str, *, ultimate: Optional[bool] = False
3695
    ) -> Any:
3696
        """Deletes an existing note
3697
3698
        Arguments:
3699
            note_id: UUID of the note to be deleted.
3700
            ultimate: Whether to remove entirely,or to the trashcan.
3701
        """
3702
        if not note_id:
3703
            raise RequiredArgument(
3704
                function=self.delete_note.__name__, argument='note_id'
3705
            )
3706
3707
        cmd = XmlCommand("delete_note")
3708
        cmd.set_attribute("note_id", note_id)
3709
        cmd.set_attribute("ultimate", to_bool(ultimate))
3710
3711
        return self._send_xml_command(cmd)
3712
3713
    def delete_override(
3714
        self, override_id: str, *, ultimate: Optional[bool] = False
3715
    ) -> Any:
3716
        """Deletes an existing override
3717
3718
        Arguments:
3719
            override_id: UUID of the override to be deleted.
3720
            ultimate: Whether to remove entirely, or to the trashcan.
3721
        """
3722
        if not override_id:
3723
            raise RequiredArgument(
3724
                function=self.delete_override.__name__, argument='override_id'
3725
            )
3726
3727
        cmd = XmlCommand("delete_override")
3728
        cmd.set_attribute("override_id", override_id)
3729
        cmd.set_attribute("ultimate", to_bool(ultimate))
3730
3731
        return self._send_xml_command(cmd)
3732
3733
    def delete_permission(
3734
        self, permission_id: str, *, ultimate: Optional[bool] = False
3735
    ) -> Any:
3736
        """Deletes an existing permission
3737
3738
        Arguments:
3739
            permission_id: UUID of the permission to be deleted.
3740
            ultimate: Whether to remove entirely, or to the trashcan.
3741
        """
3742
        if not permission_id:
3743
            raise RequiredArgument(
3744
                function=self.delete_permission.__name__,
3745
                argument='permission_id',
3746
            )
3747
3748
        cmd = XmlCommand("delete_permission")
3749
        cmd.set_attribute("permission_id", permission_id)
3750
        cmd.set_attribute("ultimate", to_bool(ultimate))
3751
3752
        return self._send_xml_command(cmd)
3753
3754
    def delete_report_format(
3755
        self,
3756
        report_format_id: Optional[Union[str, ReportFormatType]] = None,
3757
        *,
3758
        ultimate: Optional[bool] = False,
3759
    ) -> Any:
3760
        """Deletes an existing report format
3761
3762
        Arguments:
3763
            report_format_id: UUID of the report format to be deleted.
3764
                              or ReportFormatType (enum)
3765
            ultimate: Whether to remove entirely, or to the trashcan.
3766
        """
3767
        if not report_format_id:
3768
            raise RequiredArgument(
3769
                function=self.delete_report_format.__name__,
3770
                argument='report_format_id',
3771
            )
3772
3773
        cmd = XmlCommand("delete_report_format")
3774
3775
        if isinstance(report_format_id, ReportFormatType):
3776
            report_format_id = report_format_id.value
3777
3778
        cmd.set_attribute("report_format_id", report_format_id)
3779
3780
        cmd.set_attribute("ultimate", to_bool(ultimate))
3781
3782
        return self._send_xml_command(cmd)
3783
3784
    def delete_role(
3785
        self, role_id: str, *, ultimate: Optional[bool] = False
3786
    ) -> Any:
3787
        """Deletes an existing role
3788
3789
        Arguments:
3790
            role_id: UUID of the role to be deleted.
3791
            ultimate: Whether to remove entirely, or to the trashcan.
3792
        """
3793
        if not role_id:
3794
            raise RequiredArgument(
3795
                function=self.delete_role.__name__, argument='role_id'
3796
            )
3797
3798
        cmd = XmlCommand("delete_role")
3799
        cmd.set_attribute("role_id", role_id)
3800
        cmd.set_attribute("ultimate", to_bool(ultimate))
3801
3802
        return self._send_xml_command(cmd)
3803
3804
    def delete_scanner(
3805
        self, scanner_id: str, *, ultimate: Optional[bool] = False
3806
    ) -> Any:
3807
        """Deletes an existing scanner
3808
3809
        Arguments:
3810
            scanner_id: UUID of the scanner to be deleted.
3811
            ultimate: Whether to remove entirely, or to the trashcan.
3812
        """
3813
        if not scanner_id:
3814
            raise RequiredArgument(
3815
                function=self.delete_scanner.__name__, argument='scanner_id'
3816
            )
3817
3818
        cmd = XmlCommand("delete_scanner")
3819
        cmd.set_attribute("scanner_id", scanner_id)
3820
        cmd.set_attribute("ultimate", to_bool(ultimate))
3821
3822
        return self._send_xml_command(cmd)
3823
3824
    def delete_schedule(
3825
        self, schedule_id: str, *, ultimate: Optional[bool] = False
3826
    ) -> Any:
3827
        """Deletes an existing schedule
3828
3829
        Arguments:
3830
            schedule_id: UUID of the schedule to be deleted.
3831
            ultimate: Whether to remove entirely, or to the trashcan.
3832
        """
3833
        if not schedule_id:
3834
            raise RequiredArgument(
3835
                function=self.delete_schedule.__name__, argument='schedule_id'
3836
            )
3837
3838
        cmd = XmlCommand("delete_schedule")
3839
        cmd.set_attribute("schedule_id", schedule_id)
3840
        cmd.set_attribute("ultimate", to_bool(ultimate))
3841
3842
        return self._send_xml_command(cmd)
3843
3844
    def delete_tag(
3845
        self, tag_id: str, *, ultimate: Optional[bool] = False
3846
    ) -> Any:
3847
        """Deletes an existing tag
3848
3849
        Arguments:
3850
            tag_id: UUID of the tag to be deleted.
3851
            ultimate: Whether to remove entirely, or to the trashcan.
3852
        """
3853
        if not tag_id:
3854
            raise RequiredArgument(
3855
                function=self.delete_tag.__name__, argument='tag_id'
3856
            )
3857
3858
        cmd = XmlCommand("delete_tag")
3859
        cmd.set_attribute("tag_id", tag_id)
3860
        cmd.set_attribute("ultimate", to_bool(ultimate))
3861
3862
        return self._send_xml_command(cmd)
3863
3864
    def delete_user(
3865
        self,
3866
        user_id: str = None,
3867
        *,
3868
        name: Optional[str] = None,
3869
        inheritor_id: Optional[str] = None,
3870
        inheritor_name: Optional[str] = None,
3871
    ) -> Any:
3872
        """Deletes an existing user
3873
3874
        Either user_id or name must be passed.
3875
3876
        Arguments:
3877
            user_id: UUID of the task to be deleted.
3878
            name: The name of the user to be deleted.
3879
            inheritor_id: The ID of the inheriting user or "self". Overrides
3880
                inheritor_name.
3881
            inheritor_name: The name of the inheriting user.
3882
3883
        """
3884
        if not user_id and not name:
3885
            raise RequiredArgument(
3886
                function=self.delete_user.__name__, argument='user_id or name'
3887
            )
3888
3889
        cmd = XmlCommand("delete_user")
3890
3891
        if user_id:
3892
            cmd.set_attribute("user_id", user_id)
3893
3894
        if name:
3895
            cmd.set_attribute("name", name)
3896
3897
        if inheritor_id:
3898
            cmd.set_attribute("inheritor_id", inheritor_id)
3899
        if inheritor_name:
3900
            cmd.set_attribute("inheritor_name", inheritor_name)
3901
3902
        return self._send_xml_command(cmd)
3903
3904
    def describe_auth(self) -> Any:
3905
        """Describe authentication methods
3906
3907
        Returns a list of all used authentication methods if such a list is
3908
        available.
3909
3910
        Returns:
3911
            The response. See :py:meth:`send_command` for details.
3912
        """
3913
        return self._send_xml_command(XmlCommand("describe_auth"))
3914
3915
    def empty_trashcan(self) -> Any:
3916
        """Empty the trashcan
3917
3918
        Remove all entities from the trashcan. **Attention:** this command can
3919
        not be reverted
3920
3921
        Returns:
3922
            The response. See :py:meth:`send_command` for details.
3923
        """
3924
        return self._send_xml_command(XmlCommand("empty_trashcan"))
3925
3926
    def get_alerts(
3927
        self,
3928
        *,
3929
        filter: Optional[str] = None,
3930
        filter_id: Optional[str] = None,
3931
        trash: Optional[bool] = None,
3932
        tasks: Optional[bool] = None,
3933
    ) -> Any:
3934
        """Request a list of alerts
3935
3936
        Arguments:
3937
            filter: Filter term to use for the query
3938
            filter_id: UUID of an existing filter to use for the query
3939
            trash: True to request the alerts in the trashcan
3940
            tasks: Whether to include the tasks using the alerts
3941
        Returns:
3942
            The response. See :py:meth:`send_command` for details.
3943
        """
3944
        cmd = XmlCommand("get_alerts")
3945
3946
        add_filter(cmd, filter, filter_id)
3947
3948
        if trash is not None:
3949
            cmd.set_attribute("trash", to_bool(trash))
3950
3951
        if tasks is not None:
3952
            cmd.set_attribute("tasks", to_bool(tasks))
3953
3954
        return self._send_xml_command(cmd)
3955
3956
    def get_alert(self, alert_id: str, *, tasks: Optional[bool] = None) -> Any:
3957
        """Request a single alert
3958
3959
        Arguments:
3960
            alert_id: UUID of an existing alert
3961
3962
        Returns:
3963
            The response. See :py:meth:`send_command` for details.
3964
        """
3965
        cmd = XmlCommand("get_alerts")
3966
3967
        if not alert_id:
3968
            raise RequiredArgument(
3969
                function=self.get_alert.__name__, argument='alert_id'
3970
            )
3971
3972
        cmd.set_attribute("alert_id", alert_id)
3973
3974
        if tasks is not None:
3975
            cmd.set_attribute("tasks", to_bool(tasks))
3976
3977
        return self._send_xml_command(cmd)
3978
3979
    def get_assets(
3980
        self,
3981
        asset_type: AssetType,
3982
        *,
3983
        filter: Optional[str] = None,
3984
        filter_id: Optional[str] = None,
3985
    ) -> Any:
3986
        """Request a list of assets
3987
3988
        Arguments:
3989
            asset_type: Either 'os' or 'host'
3990
            filter: Filter term to use for the query
3991
            filter_id: UUID of an existing filter to use for the query
3992
3993
        Returns:
3994
            The response. See :py:meth:`send_command` for details.
3995
        """
3996
        if not isinstance(asset_type, AssetType):
3997
            raise InvalidArgumentType(
3998
                function=self.get_assets.__name__,
3999
                argument='asset_type',
4000
                arg_type=AssetType.__name__,
4001
            )
4002
4003
        cmd = XmlCommand("get_assets")
4004
4005
        cmd.set_attribute("type", asset_type.value)
4006
4007
        add_filter(cmd, filter, filter_id)
4008
4009
        return self._send_xml_command(cmd)
4010
4011
    def get_asset(self, asset_id: str, asset_type: AssetType) -> Any:
4012
        """Request a single asset
4013
4014
        Arguments:
4015
            asset_id: UUID of an existing asset
4016
            asset_type: Either 'os' or 'host'
4017
4018
        Returns:
4019
            The response. See :py:meth:`send_command` for details.
4020
        """
4021
        cmd = XmlCommand("get_assets")
4022
4023
        if not isinstance(asset_type, AssetType):
4024
            raise InvalidArgumentType(
4025
                function=self.get_asset.__name__,
4026
                argument='asset_type',
4027
                arg_type=AssetType.__name__,
4028
            )
4029
4030
        if not asset_id:
4031
            raise RequiredArgument(
4032
                function=self.get_asset.__name__, argument='asset_id'
4033
            )
4034
4035
        cmd.set_attribute("asset_id", asset_id)
4036
        cmd.set_attribute("type", asset_type.value)
4037
4038
        return self._send_xml_command(cmd)
4039
4040 View Code Duplication
    def get_credentials(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4041
        self,
4042
        *,
4043
        filter: Optional[str] = None,
4044
        filter_id: Optional[str] = None,
4045
        scanners: Optional[bool] = None,
4046
        trash: Optional[bool] = None,
4047
        targets: Optional[bool] = None,
4048
    ) -> Any:
4049
        """Request a list of credentials
4050
4051
        Arguments:
4052
            filter: Filter term to use for the query
4053
            filter_id: UUID of an existing filter to use for the query
4054
            scanners: Whether to include a list of scanners using the
4055
                credentials
4056
            trash: Whether to get the trashcan credentials instead
4057
            targets: Whether to include a list of targets using the credentials
4058
4059
        Returns:
4060
            The response. See :py:meth:`send_command` for details.
4061
        """
4062
        cmd = XmlCommand("get_credentials")
4063
4064
        add_filter(cmd, filter, filter_id)
4065
4066
        if scanners is not None:
4067
            cmd.set_attribute("scanners", to_bool(scanners))
4068
4069
        if trash is not None:
4070
            cmd.set_attribute("trash", to_bool(trash))
4071
4072
        if targets is not None:
4073
            cmd.set_attribute("targets", to_bool(targets))
4074
4075
        return self._send_xml_command(cmd)
4076
4077
    def get_credential(
4078
        self,
4079
        credential_id: str,
4080
        *,
4081
        scanners: Optional[bool] = None,
4082
        targets: Optional[bool] = None,
4083
        credential_format: Optional[CredentialFormat] = None,
4084
    ) -> Any:
4085
        """Request a single credential
4086
4087
        Arguments:
4088
            credential_id: UUID of an existing credential
4089
            scanners: Whether to include a list of scanners using the
4090
                credentials
4091
            targets: Whether to include a list of targets using the credentials
4092
            credential_format: One of "key", "rpm", "deb", "exe" or "pem"
4093
4094
        Returns:
4095
            The response. See :py:meth:`send_command` for details.
4096
        """
4097
        if not credential_id:
4098
            raise RequiredArgument(
4099
                function=self.get_credential.__name__, argument='credential_id'
4100
            )
4101
4102
        cmd = XmlCommand("get_credentials")
4103
        cmd.set_attribute("credential_id", credential_id)
4104
4105
        if credential_format:
4106
            if not isinstance(credential_format, CredentialFormat):
4107
                raise InvalidArgumentType(
4108
                    function=self.get_credential.__name__,
4109
                    argument='credential_format',
4110
                    arg_type=CredentialFormat.__name__,
4111
                )
4112
4113
            cmd.set_attribute("format", credential_format.value)
4114
4115
        if scanners is not None:
4116
            cmd.set_attribute("scanners", to_bool(scanners))
4117
4118
        if targets is not None:
4119
            cmd.set_attribute("targets", to_bool(targets))
4120
4121
        return self._send_xml_command(cmd)
4122
4123
    def get_feeds(self) -> Any:
4124
        """Request the list of feeds
4125
4126
        Returns:
4127
            The response. See :py:meth:`send_command` for details.
4128
        """
4129
        return self._send_xml_command(XmlCommand("get_feeds"))
4130
4131
    def get_filters(
4132
        self,
4133
        *,
4134
        filter: Optional[str] = None,
4135
        filter_id: Optional[str] = None,
4136
        trash: Optional[bool] = None,
4137
        alerts: Optional[bool] = None,
4138
    ) -> Any:
4139
        """Request a list of filters
4140
4141
        Arguments:
4142
            filter: Filter term to use for the query
4143
            filter_id: UUID of an existing filter to use for the query
4144
            trash: Whether to get the trashcan filters instead
4145
            alerts: Whether to include list of alerts that use the filter.
4146
4147
        Returns:
4148
            The response. See :py:meth:`send_command` for details.
4149
        """
4150
        cmd = XmlCommand("get_filters")
4151
4152
        add_filter(cmd, filter, filter_id)
4153
4154
        if trash is not None:
4155
            cmd.set_attribute("trash", to_bool(trash))
4156
4157
        if alerts is not None:
4158
            cmd.set_attribute("alerts", to_bool(alerts))
4159
4160
        return self._send_xml_command(cmd)
4161
4162
    def get_filter(
4163
        self, filter_id: str, *, alerts: Optional[bool] = None
4164
    ) -> Any:
4165
        """Request a single filter
4166
4167
        Arguments:
4168
            filter_id: UUID of an existing filter
4169
            alerts: Whether to include list of alerts that use the filter.
4170
4171
        Returns:
4172
            The response. See :py:meth:`send_command` for details.
4173
        """
4174
        cmd = XmlCommand("get_filters")
4175
4176
        if not filter_id:
4177
            raise RequiredArgument(
4178
                function=self.get_filter.__name__, argument='filter_id'
4179
            )
4180
4181
        cmd.set_attribute("filter_id", filter_id)
4182
4183
        if alerts is not None:
4184
            cmd.set_attribute("alerts", to_bool(alerts))
4185
4186
        return self._send_xml_command(cmd)
4187
4188 View Code Duplication
    def get_groups(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4189
        self,
4190
        *,
4191
        filter: Optional[str] = None,
4192
        filter_id: Optional[str] = None,
4193
        trash: Optional[bool] = None,
4194
    ) -> Any:
4195
        """Request a list of groups
4196
4197
        Arguments:
4198
            filter: Filter term to use for the query
4199
            filter_id: UUID of an existing filter to use for the query
4200
            trash: Whether to get the trashcan groups instead
4201
4202
        Returns:
4203
            The response. See :py:meth:`send_command` for details.
4204
        """
4205
        cmd = XmlCommand("get_groups")
4206
4207
        add_filter(cmd, filter, filter_id)
4208
4209
        if trash is not None:
4210
            cmd.set_attribute("trash", to_bool(trash))
4211
4212
        return self._send_xml_command(cmd)
4213
4214
    def get_group(self, group_id: str) -> Any:
4215
        """Request a single group
4216
4217
        Arguments:
4218
            group_id: UUID of an existing group
4219
4220
        Returns:
4221
            The response. See :py:meth:`send_command` for details.
4222
        """
4223
        cmd = XmlCommand("get_groups")
4224
4225
        if not group_id:
4226
            raise RequiredArgument(
4227
                function=self.get_group.__name__, argument='group_id'
4228
            )
4229
4230
        cmd.set_attribute("group_id", group_id)
4231
        return self._send_xml_command(cmd)
4232
4233 View Code Duplication
    def get_notes(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4234
        self,
4235
        *,
4236
        filter: Optional[str] = None,
4237
        filter_id: Optional[str] = None,
4238
        details: Optional[bool] = None,
4239
        result: Optional[bool] = None,
4240
    ) -> Any:
4241
        """Request a list of notes
4242
4243
        Arguments:
4244
            filter: Filter term to use for the query
4245
            filter_id: UUID of an existing filter to use for the query
4246
            details: Add info about connected results and tasks
4247
            result: Return the details of possible connected results.
4248
4249
        Returns:
4250
            The response. See :py:meth:`send_command` for details.
4251
        """
4252
        cmd = XmlCommand("get_notes")
4253
4254
        add_filter(cmd, filter, filter_id)
4255
4256
        if details is not None:
4257
            cmd.set_attribute("details", to_bool(details))
4258
4259
        if result is not None:
4260
            cmd.set_attribute("result", to_bool(result))
4261
4262
        return self._send_xml_command(cmd)
4263
4264
    def get_note(self, note_id: str) -> Any:
4265
        """Request a single note
4266
4267
        Arguments:
4268
            note_id: UUID of an existing note
4269
4270
        Returns:
4271
            The response. See :py:meth:`send_command` for details.
4272
        """
4273
        if not note_id:
4274
            raise RequiredArgument(
4275
                function=self.get_note.__name__, argument='note_id'
4276
            )
4277
4278
        cmd = XmlCommand("get_notes")
4279
        cmd.set_attribute("note_id", note_id)
4280
4281
        # for single entity always request all details
4282
        cmd.set_attribute("details", "1")
4283
        return self._send_xml_command(cmd)
4284
4285 View Code Duplication
    def get_overrides(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4286
        self,
4287
        *,
4288
        filter: Optional[str] = None,
4289
        filter_id: Optional[str] = None,
4290
        details: Optional[bool] = None,
4291
        result: Optional[bool] = None,
4292
    ) -> Any:
4293
        """Request a list of overrides
4294
4295
        Arguments:
4296
            filter: Filter term to use for the query
4297
            filter_id: UUID of an existing filter to use for the query
4298
            details: Whether to include full details
4299
            result: Whether to include results using the override
4300
4301
        Returns:
4302
            The response. See :py:meth:`send_command` for details.
4303
        """
4304
        cmd = XmlCommand("get_overrides")
4305
4306
        add_filter(cmd, filter, filter_id)
4307
4308
        if details is not None:
4309
            cmd.set_attribute("details", to_bool(details))
4310
4311
        if result is not None:
4312
            cmd.set_attribute("result", to_bool(result))
4313
4314
        return self._send_xml_command(cmd)
4315
4316
    def get_override(self, override_id: str) -> Any:
4317
        """Request a single override
4318
4319
        Arguments:
4320
            override_id: UUID of an existing override
4321
4322
        Returns:
4323
            The response. See :py:meth:`send_command` for details.
4324
        """
4325
        cmd = XmlCommand("get_overrides")
4326
4327
        if not override_id:
4328
            raise RequiredArgument(
4329
                function=self.get_override.__name__, argument='override_id'
4330
            )
4331
4332
        cmd.set_attribute("override_id", override_id)
4333
4334
        # for single entity always request all details
4335
        cmd.set_attribute("details", "1")
4336
        return self._send_xml_command(cmd)
4337
4338 View Code Duplication
    def get_permissions(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4339
        self,
4340
        *,
4341
        filter: Optional[str] = None,
4342
        filter_id: Optional[str] = None,
4343
        trash: Optional[bool] = None,
4344
    ) -> Any:
4345
        """Request a list of permissions
4346
4347
        Arguments:
4348
            filter: Filter term to use for the query
4349
            filter_id: UUID of an existing filter to use for the query
4350
            trash: Whether to get permissions in the trashcan instead
4351
4352
        Returns:
4353
            The response. See :py:meth:`send_command` for details.
4354
        """
4355
        cmd = XmlCommand("get_permissions")
4356
4357
        add_filter(cmd, filter, filter_id)
4358
4359
        if trash is not None:
4360
            cmd.set_attribute("trash", to_bool(trash))
4361
4362
        return self._send_xml_command(cmd)
4363
4364
    def get_permission(self, permission_id: str) -> Any:
4365
        """Request a single permission
4366
4367
        Arguments:
4368
            permission_id: UUID of an existing permission
4369
4370
        Returns:
4371
            The response. See :py:meth:`send_command` for details.
4372
        """
4373
        cmd = XmlCommand("get_permissions")
4374
4375
        if not permission_id:
4376
            raise RequiredArgument(
4377
                function=self.get_permission.__name__, argument='permission_id'
4378
            )
4379
4380
        cmd.set_attribute("permission_id", permission_id)
4381
        return self._send_xml_command(cmd)
4382
4383
    def get_preferences(
4384
        self, *, nvt_oid: Optional[str] = None, config_id: Optional[str] = None
4385
    ) -> Any:
4386
        """Request a list of preferences
4387
4388
        When the command includes a config_id attribute, the preference element
4389
        includes the preference name, type and value, and the NVT to which the
4390
        preference applies. Otherwise, the preference element includes just the
4391
        name and value, with the NVT and type built into the name.
4392
4393
        Arguments:
4394
            nvt_oid: OID of nvt
4395
            config_id: UUID of scan config of which to show preference values
4396
4397
        Returns:
4398
            The response. See :py:meth:`send_command` for details.
4399
        """
4400
        cmd = XmlCommand("get_preferences")
4401
4402
        if nvt_oid:
4403
            cmd.set_attribute("nvt_oid", nvt_oid)
4404
4405
        if config_id:
4406
            cmd.set_attribute("config_id", config_id)
4407
4408
        return self._send_xml_command(cmd)
4409
4410
    def get_preference(
4411
        self,
4412
        name: str,
4413
        *,
4414
        nvt_oid: Optional[str] = None,
4415
        config_id: Optional[str] = None,
4416
    ) -> Any:
4417
        """Request a nvt preference
4418
4419
        Arguments:
4420
            name: name of a particular preference
4421
            nvt_oid: OID of nvt
4422
            config_id: UUID of scan config of which to show preference values
4423
4424
        Returns:
4425
            The response. See :py:meth:`send_command` for details.
4426
        """
4427
        cmd = XmlCommand("get_preferences")
4428
4429
        if not name:
4430
            raise RequiredArgument(
4431
                function=self.get_preference.__name__, argument='name'
4432
            )
4433
4434
        cmd.set_attribute("preference", name)
4435
4436
        if nvt_oid:
4437
            cmd.set_attribute("nvt_oid", nvt_oid)
4438
4439
        if config_id:
4440
            cmd.set_attribute("config_id", config_id)
4441
4442
        return self._send_xml_command(cmd)
4443
4444
    def get_report_formats(
4445
        self,
4446
        *,
4447
        filter: Optional[str] = None,
4448
        filter_id: Optional[str] = None,
4449
        trash: Optional[bool] = None,
4450
        alerts: Optional[bool] = None,
4451
        params: Optional[bool] = None,
4452
        details: Optional[bool] = None,
4453
    ) -> Any:
4454
        """Request a list of report formats
4455
4456
        Arguments:
4457
            filter: Filter term to use for the query
4458
            filter_id: UUID of an existing filter to use for the query
4459
            trash: Whether to get the trashcan report formats instead
4460
            alerts: Whether to include alerts that use the report format
4461
            params: Whether to include report format parameters
4462
            details: Include report format file, signature and parameters
4463
4464
        Returns:
4465
            The response. See :py:meth:`send_command` for details.
4466
        """
4467
        cmd = XmlCommand("get_report_formats")
4468
4469
        add_filter(cmd, filter, filter_id)
4470
4471
        if details is not None:
4472
            cmd.set_attribute("details", to_bool(details))
4473
4474
        if alerts is not None:
4475
            cmd.set_attribute("alerts", to_bool(alerts))
4476
4477
        if params is not None:
4478
            cmd.set_attribute("params", to_bool(params))
4479
4480
        if trash is not None:
4481
            cmd.set_attribute("trash", to_bool(trash))
4482
4483
        return self._send_xml_command(cmd)
4484
4485 View Code Duplication
    def get_report_format(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4486
        self, report_format_id: Union[str, ReportFormatType]
4487
    ) -> Any:
4488
        """Request a single report format
4489
4490
        Arguments:
4491
            report_format_id: UUID of an existing report format
4492
                              or ReportFormatType (enum)
4493
4494
        Returns:
4495
            The response. See :py:meth:`send_command` for details.
4496
        """
4497
        cmd = XmlCommand("get_report_formats")
4498
4499
        if not report_format_id:
4500
            raise RequiredArgument(
4501
                function=self.get_report_format.__name__,
4502
                argument='report_format_id',
4503
            )
4504
4505
        if isinstance(report_format_id, ReportFormatType):
4506
            report_format_id = report_format_id.value
4507
4508
        cmd.set_attribute("report_format_id", report_format_id)
4509
4510
        # for single entity always request all details
4511
        cmd.set_attribute("details", "1")
4512
        return self._send_xml_command(cmd)
4513
4514 View Code Duplication
    def get_roles(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4515
        self,
4516
        *,
4517
        filter: Optional[str] = None,
4518
        filter_id: Optional[str] = None,
4519
        trash: Optional[bool] = None,
4520
    ) -> Any:
4521
        """Request a list of roles
4522
4523
        Arguments:
4524
            filter: Filter term to use for the query
4525
            filter_id: UUID of an existing filter to use for the query
4526
            trash: Whether to get the trashcan roles instead
4527
4528
        Returns:
4529
            The response. See :py:meth:`send_command` for details.
4530
        """
4531
        cmd = XmlCommand("get_roles")
4532
4533
        add_filter(cmd, filter, filter_id)
4534
4535
        if trash is not None:
4536
            cmd.set_attribute("trash", to_bool(trash))
4537
4538
        return self._send_xml_command(cmd)
4539
4540
    def get_role(self, role_id: str) -> Any:
4541
        """Request a single role
4542
4543
        Arguments:
4544
            role_id: UUID of an existing role
4545
4546
        Returns:
4547
            The response. See :py:meth:`send_command` for details.
4548
        """
4549
        if not role_id:
4550
            raise RequiredArgument(
4551
                function=self.get_role.__name__, argument='role_id'
4552
            )
4553
4554
        cmd = XmlCommand("get_roles")
4555
        cmd.set_attribute("role_id", role_id)
4556
        return self._send_xml_command(cmd)
4557
4558 View Code Duplication
    def get_scanners(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4559
        self,
4560
        *,
4561
        filter: Optional[str] = None,
4562
        filter_id: Optional[str] = None,
4563
        trash: Optional[bool] = None,
4564
        details: Optional[bool] = None,
4565
    ) -> Any:
4566
        """Request a list of scanners
4567
4568
        Arguments:
4569
            filter: Filter term to use for the query
4570
            filter_id: UUID of an existing filter to use for the query
4571
            trash: Whether to get the trashcan scanners instead
4572
            details:  Whether to include extra details like tasks using this
4573
                scanner
4574
4575
        Returns:
4576
            The response. See :py:meth:`send_command` for details.
4577
        """
4578
        cmd = XmlCommand("get_scanners")
4579
4580
        add_filter(cmd, filter, filter_id)
4581
4582
        if trash is not None:
4583
            cmd.set_attribute("trash", to_bool(trash))
4584
4585
        if details is not None:
4586
            cmd.set_attribute("details", to_bool(details))
4587
4588
        return self._send_xml_command(cmd)
4589
4590
    def get_scanner(self, scanner_id: str) -> Any:
4591
        """Request a single scanner
4592
4593
        Arguments:
4594
            scanner_id: UUID of an existing scanner
4595
4596
        Returns:
4597
            The response. See :py:meth:`send_command` for details.
4598
        """
4599
        cmd = XmlCommand("get_scanners")
4600
4601
        if not scanner_id:
4602
            raise RequiredArgument(
4603
                function=self.get_scanner.__name__, argument='scanner_id'
4604
            )
4605
4606
        cmd.set_attribute("scanner_id", scanner_id)
4607
4608
        # for single entity always request all details
4609
        cmd.set_attribute("details", "1")
4610
        return self._send_xml_command(cmd)
4611
4612
    def get_schedules(
4613
        self,
4614
        *,
4615
        filter: Optional[str] = None,
4616
        filter_id: Optional[str] = None,
4617
        trash: Optional[bool] = None,
4618
        tasks: Optional[bool] = None,
4619
    ) -> Any:
4620
        """Request a list of schedules
4621
4622
        Arguments:
4623
            filter: Filter term to use for the query
4624
            filter_id: UUID of an existing filter to use for the query
4625
            trash: Whether to get the trashcan schedules instead
4626
            tasks: Whether to include tasks using the schedules
4627
4628
        Returns:
4629
            The response. See :py:meth:`send_command` for details.
4630
        """
4631
        cmd = XmlCommand("get_schedules")
4632
4633
        add_filter(cmd, filter, filter_id)
4634
4635
        if trash is not None:
4636
            cmd.set_attribute("trash", to_bool(trash))
4637
4638
        if tasks is not None:
4639
            cmd.set_attribute("tasks", to_bool(tasks))
4640
4641
        return self._send_xml_command(cmd)
4642
4643
    def get_schedule(
4644
        self, schedule_id: str, *, tasks: Optional[bool] = None
4645
    ) -> Any:
4646
        """Request a single schedule
4647
4648
        Arguments:
4649
            schedule_id: UUID of an existing schedule
4650
            tasks: Whether to include tasks using the schedules
4651
4652
        Returns:
4653
            The response. See :py:meth:`send_command` for details.
4654
        """
4655
        cmd = XmlCommand("get_schedules")
4656
4657
        if not schedule_id:
4658
            raise RequiredArgument(
4659
                function=self.get_schedule.__name__, argument='schedule_id'
4660
            )
4661
4662
        cmd.set_attribute("schedule_id", schedule_id)
4663
4664
        if tasks is not None:
4665
            cmd.set_attribute("tasks", to_bool(tasks))
4666
4667
        return self._send_xml_command(cmd)
4668
4669
    def get_settings(self, *, filter: Optional[str] = None) -> Any:
4670
        """Request a list of user settings
4671
4672
        Arguments:
4673
            filter: Filter term to use for the query
4674
4675
        Returns:
4676
            The response. See :py:meth:`send_command` for details.
4677
        """
4678
        cmd = XmlCommand("get_settings")
4679
4680
        if filter:
4681
            cmd.set_attribute("filter", filter)
4682
4683
        return self._send_xml_command(cmd)
4684
4685
    def get_setting(self, setting_id: str) -> Any:
4686
        """Request a single setting
4687
4688
        Arguments:
4689
            setting_id: UUID of an existing setting
4690
4691
        Returns:
4692
            The response. See :py:meth:`send_command` for details.
4693
        """
4694
        cmd = XmlCommand("get_settings")
4695
4696
        if not setting_id:
4697
            raise RequiredArgument(
4698
                function=self.get_setting.__name__, argument='setting_id'
4699
            )
4700
4701
        cmd.set_attribute("setting_id", setting_id)
4702
        return self._send_xml_command(cmd)
4703
4704
    def get_system_reports(
4705
        self,
4706
        *,
4707
        name: Optional[str] = None,
4708
        duration: Optional[int] = None,
4709
        start_time: Optional[str] = None,
4710
        end_time: Optional[str] = None,
4711
        brief: Optional[bool] = None,
4712
        slave_id: Optional[str] = None,
4713
    ) -> Any:
4714
        """Request a list of system reports
4715
4716
        Arguments:
4717
            name: A string describing the required system report
4718
            duration: The number of seconds into the past that the system report
4719
                should include
4720
            start_time: The start of the time interval the system report should
4721
                include in ISO time format
4722
            end_time: The end of the time interval the system report should
4723
                include in ISO time format
4724
            brief: Whether to include the actual system reports
4725
            slave_id: UUID of GMP scanner from which to get the system reports
4726
4727
        Returns:
4728
            The response. See :py:meth:`send_command` for details.
4729
        """
4730
        cmd = XmlCommand("get_system_reports")
4731
4732
        if name:
4733
            cmd.set_attribute("name", name)
4734
4735
        if duration is not None:
4736
            if not isinstance(duration, numbers.Integral):
4737
                raise InvalidArgument("duration needs to be an integer number")
4738
4739
            cmd.set_attribute("duration", str(duration))
4740
4741
        if start_time:
4742
            cmd.set_attribute("start_time", str(start_time))
4743
4744
        if end_time:
4745
            cmd.set_attribute("end_time", str(end_time))
4746
4747
        if brief is not None:
4748
            cmd.set_attribute("brief", to_bool(brief))
4749
4750
        if slave_id:
4751
            cmd.set_attribute("slave_id", slave_id)
4752
4753
        return self._send_xml_command(cmd)
4754
4755
    def get_tags(
4756
        self,
4757
        *,
4758
        filter: Optional[str] = None,
4759
        filter_id: Optional[str] = None,
4760
        trash: Optional[bool] = None,
4761
        names_only: Optional[bool] = None,
4762
    ) -> Any:
4763
        """Request a list of tags
4764
4765
        Arguments:
4766
            filter: Filter term to use for the query
4767
            filter_id: UUID of an existing filter to use for the query
4768
            trash: Whether to get tags from the trashcan instead
4769
            names_only: Whether to get only distinct tag names
4770
4771
        Returns:
4772
            The response. See :py:meth:`send_command` for details.
4773
        """
4774
        cmd = XmlCommand("get_tags")
4775
4776
        add_filter(cmd, filter, filter_id)
4777
4778
        if trash is not None:
4779
            cmd.set_attribute("trash", to_bool(trash))
4780
4781
        if names_only is not None:
4782
            cmd.set_attribute("names_only", to_bool(names_only))
4783
4784
        return self._send_xml_command(cmd)
4785
4786
    def get_tag(self, tag_id: str) -> Any:
4787
        """Request a single tag
4788
4789
        Arguments:
4790
            tag_id: UUID of an existing tag
4791
4792
        Returns:
4793
            The response. See :py:meth:`send_command` for details.
4794
        """
4795
        cmd = XmlCommand("get_tags")
4796
4797
        if not tag_id:
4798
            raise RequiredArgument(
4799
                function=self.get_tag.__name__, argument='tag_id'
4800
            )
4801
4802
        cmd.set_attribute("tag_id", tag_id)
4803
        return self._send_xml_command(cmd)
4804
4805
    def get_users(
4806
        self, *, filter: Optional[str] = None, filter_id: Optional[str] = None
4807
    ) -> Any:
4808
        """Request a list of users
4809
4810
        Arguments:
4811
            filter: Filter term to use for the query
4812
            filter_id: UUID of an existing filter to use for the query
4813
4814
        Returns:
4815
            The response. See :py:meth:`send_command` for details.
4816
        """
4817
        cmd = XmlCommand("get_users")
4818
4819
        add_filter(cmd, filter, filter_id)
4820
4821
        return self._send_xml_command(cmd)
4822
4823
    def get_user(self, user_id: str) -> Any:
4824
        """Request a single user
4825
4826
        Arguments:
4827
            user_id: UUID of an existing user
4828
4829
        Returns:
4830
            The response. See :py:meth:`send_command` for details.
4831
        """
4832
        cmd = XmlCommand("get_users")
4833
4834
        if not user_id:
4835
            raise RequiredArgument(
4836
                function=self.get_user.__name__, argument='user_id'
4837
            )
4838
4839
        cmd.set_attribute("user_id", user_id)
4840
        return self._send_xml_command(cmd)
4841
4842
    def get_version(self) -> Any:
4843
        """Get the Greenbone Manager Protocol version used by the remote gvmd
4844
4845
        Returns:
4846
            The response. See :py:meth:`send_command` for details.
4847
        """
4848
        return self._send_xml_command(XmlCommand("get_version"))
4849
4850
    def help(
4851
        self, *, format: Optional[str] = None, help_type: Optional[str] = ""
4852
    ) -> Any:
4853
        """Get the help text
4854
4855
        Arguments:
4856
            format: One of "html", "rnc", "text" or "xml
4857
            help_type: One of "brief" or "". Default ""
4858
4859
        Returns:
4860
            The response. See :py:meth:`send_command` for details.
4861
        """
4862
        cmd = XmlCommand("help")
4863
4864
        if help_type not in ("", "brief"):
4865
            raise InvalidArgument(
4866
                'help_type argument must be an empty string or "brief"'
4867
            )
4868
4869
        cmd.set_attribute("type", help_type)
4870
4871
        if format:
4872
            if not format.lower() in ("html", "rnc", "text", "xml"):
4873
                raise InvalidArgument(
4874
                    "help format Argument must be one of html, rnc, text or "
4875
                    "xml"
4876
                )
4877
4878
            cmd.set_attribute("format", format)
4879
4880
        return self._send_xml_command(cmd)
4881
4882
    def modify_asset(self, asset_id: str, comment: Optional[str] = "") -> Any:
4883
        """Modifies an existing asset.
4884
4885
        Arguments:
4886
            asset_id: UUID of the asset to be modified.
4887
            comment: Comment for the asset.
4888
4889
        Returns:
4890
            The response. See :py:meth:`send_command` for details.
4891
        """
4892
        if not asset_id:
4893
            raise RequiredArgument(
4894
                function=self.modify_asset.__name__, argument='asset_id'
4895
            )
4896
4897
        cmd = XmlCommand("modify_asset")
4898
        cmd.set_attribute("asset_id", asset_id)
4899
        cmd.add_element("comment", comment)
4900
4901
        return self._send_xml_command(cmd)
4902
4903
    def modify_auth(self, group_name: str, auth_conf_settings: dict) -> Any:
4904
        """Modifies an existing auth.
4905
4906
        Arguments:
4907
            group_name: Name of the group to be modified.
4908
            auth_conf_settings: The new auth config.
4909
4910
        Returns:
4911
            The response. See :py:meth:`send_command` for details.
4912
        """
4913
        if not group_name:
4914
            raise RequiredArgument(
4915
                function=self.modify_auth.__name__, argument='group_name'
4916
            )
4917
        if not auth_conf_settings:
4918
            raise RequiredArgument(
4919
                function=self.modify_auth.__name__,
4920
                argument='auth_conf_settings',
4921
            )
4922
        cmd = XmlCommand("modify_auth")
4923
        _xmlgroup = cmd.add_element("group", attrs={"name": str(group_name)})
4924
4925
        for key, value in auth_conf_settings.items():
4926
            _xmlauthconf = _xmlgroup.add_element("auth_conf_setting")
4927
            _xmlauthconf.add_element("key", key)
4928
            _xmlauthconf.add_element("value", value)
4929
4930
        return self._send_xml_command(cmd)
4931
4932
    def modify_config_set_nvt_preference(
4933
        self,
4934
        config_id: str,
4935
        name: str,
4936
        nvt_oid: str,
4937
        *,
4938
        value: Optional[str] = None,
4939
    ) -> Any:
4940
        """Modifies the nvt preferences of an existing scan config.
4941
4942
        Arguments:
4943
            config_id: UUID of scan config to modify.
4944
            name: Name for nvt preference to change.
4945
            nvt_oid: OID of the NVT associated with preference to modify
4946
            value: New value for the preference. None to delete the preference
4947
                and to use the default instead.
4948
        """
4949
        if not config_id:
4950
            raise RequiredArgument(
4951
                function=self.modify_config_set_nvt_preference.__name__,
4952
                argument='config_id',
4953
            )
4954
4955
        if not nvt_oid:
4956
            raise RequiredArgument(
4957
                function=self.modify_config_set_nvt_preference.__name__,
4958
                argument='nvt_oid',
4959
            )
4960
4961
        if not name:
4962
            raise RequiredArgument(
4963
                function=self.modify_config_set_nvt_preference.__name__,
4964
                argument='name',
4965
            )
4966
4967
        cmd = XmlCommand("modify_config")
4968
        cmd.set_attribute("config_id", str(config_id))
4969
4970
        _xmlpref = cmd.add_element("preference")
4971
4972
        _xmlpref.add_element("nvt", attrs={"oid": nvt_oid})
4973
        _xmlpref.add_element("name", name)
4974
4975
        if value:
4976
            _xmlpref.add_element("value", to_base64(value))
4977
4978
        return self._send_xml_command(cmd)
4979
4980
    def modify_config_set_name(self, config_id: str, name: str) -> Any:
4981
        """Modifies the name of an existing scan config
4982
4983
        Arguments:
4984
            config_id: UUID of scan config to modify.
4985
            name: New name for the config.
4986
        """
4987
        if not config_id:
4988
            raise RequiredArgument(
4989
                function=self.modify_config_set_name.__name__,
4990
                argument='config_id',
4991
            )
4992
4993
        if not name:
4994
            raise RequiredArgument(
4995
                function=self.modify_config_set_name.__name__, argument='name'
4996
            )
4997
4998
        cmd = XmlCommand("modify_config")
4999
        cmd.set_attribute("config_id", str(config_id))
5000
5001
        cmd.add_element("name", name)
5002
5003
        return self._send_xml_command(cmd)
5004
5005
    def modify_config_set_comment(
5006
        self, config_id: str, comment: Optional[str] = ""
5007
    ) -> Any:
5008
        """Modifies the comment of an existing scan config
5009
5010
        Arguments:
5011
            config_id: UUID of scan config to modify.
5012
            comment: Comment to set on a config. Default: ''
5013
        """
5014
        if not config_id:
5015
            raise RequiredArgument(
5016
                function=self.modify_config_set_comment.__name__,
5017
                argument='config_id argument',
5018
            )
5019
5020
        cmd = XmlCommand("modify_config")
5021
        cmd.set_attribute("config_id", str(config_id))
5022
5023
        cmd.add_element("comment", comment)
5024
5025
        return self._send_xml_command(cmd)
5026
5027
    def modify_config_set_scanner_preference(
5028
        self, config_id: str, name: str, *, value: Optional[str] = None
5029
    ) -> Any:
5030
        """Modifies the scanner preferences of an existing scan config
5031
5032
        Arguments:
5033
            config_id: UUID of scan config to modify.
5034
            name: Name of the scanner preference to change
5035
            value: New value for the preference. None to delete the preference
5036
                and to use the default instead.
5037
5038
        """
5039
        if not config_id:
5040
            raise RequiredArgument(
5041
                function=self.modify_config_set_scanner_preference.__name__,
5042
                argument='config_id',
5043
            )
5044
5045
        if not name:
5046
            raise RequiredArgument(
5047
                function=self.modify_config_set_scanner_preference.__name__,
5048
                argument='name argument',
5049
            )
5050
5051
        cmd = XmlCommand("modify_config")
5052
        cmd.set_attribute("config_id", str(config_id))
5053
5054
        _xmlpref = cmd.add_element("preference")
5055
5056
        _xmlpref.add_element("name", name)
5057
5058
        if value:
5059
            _xmlpref.add_element("value", to_base64(value))
5060
5061
        return self._send_xml_command(cmd)
5062
5063
    def modify_config_set_nvt_selection(
5064
        self, config_id: str, family: str, nvt_oids: List[str]
5065
    ) -> Any:
5066
        """Modifies the selected nvts of an existing scan config
5067
5068
        The manager updates the given family in the config to include only the
5069
        given NVTs.
5070
5071
        Arguments:
5072
            config_id: UUID of scan config to modify.
5073
            family: Name of the NVT family to include NVTs from
5074
            nvt_oids: List of NVTs to select for the family.
5075
        """
5076
        if not config_id:
5077
            raise RequiredArgument(
5078
                function=self.modify_config_set_nvt_selection.__name__,
5079
                argument='config_id',
5080
            )
5081
5082
        if not family:
5083
            raise RequiredArgument(
5084
                function=self.modify_config_set_nvt_selection.__name__,
5085
                argument='family argument',
5086
            )
5087
5088
        if not is_list_like(nvt_oids):
5089
            raise InvalidArgumentType(
5090
                function=self.modify_config_set_nvt_selection.__name__,
5091
                argument='nvt_oids',
5092
                arg_type='list',
5093
            )
5094
5095
        cmd = XmlCommand("modify_config")
5096
        cmd.set_attribute("config_id", str(config_id))
5097
5098
        _xmlnvtsel = cmd.add_element("nvt_selection")
5099
        _xmlnvtsel.add_element("family", family)
5100
5101
        for nvt in nvt_oids:
5102
            _xmlnvtsel.add_element("nvt", attrs={"oid": nvt})
5103
5104
        return self._send_xml_command(cmd)
5105
5106
    def modify_config_set_family_selection(
5107
        self,
5108
        config_id: str,
5109
        families: List[Tuple[str, bool, bool]],
5110
        *,
5111
        auto_add_new_families: Optional[bool] = True,
5112
    ) -> Any:
5113
        """
5114
        Selected the NVTs of a scan config at a family level.
5115
5116
        Arguments:
5117
            config_id: UUID of scan config to modify.
5118
            families: A list of tuples (str, bool, bool):
5119
                str: the name of the NVT family selected,
5120
                bool: add new NVTs  to the family automatically,
5121
                bool: include all NVTs from the family
5122
            auto_add_new_families: Whether new families should be added to the
5123
                scan config automatically. Default: True.
5124
        """
5125
        if not config_id:
5126
            raise RequiredArgument(
5127
                function=self.modify_config_set_family_selection.__name__,
5128
                argument='config_id',
5129
            )
5130
5131
        if not is_list_like(families):
5132
            raise InvalidArgumentType(
5133
                function=self.modify_config_set_family_selection.__name__,
5134
                argument='families',
5135
                arg_type='list',
5136
            )
5137
5138
        cmd = XmlCommand("modify_config")
5139
        cmd.set_attribute("config_id", str(config_id))
5140
5141
        _xmlfamsel = cmd.add_element("family_selection")
5142
        _xmlfamsel.add_element("growing", to_bool(auto_add_new_families))
5143
5144
        for family in families:
5145
            _xmlfamily = _xmlfamsel.add_element("family")
5146
            _xmlfamily.add_element("name", family[0])
5147
5148
            if len(family) != 3:
5149
                raise InvalidArgument(
5150
                    "Family must be a tuple of 3. (str, bool, bool)"
5151
                )
5152
5153
            if not isinstance(family[1], bool) or not isinstance(
5154
                family[2], bool
5155
            ):
5156
                raise InvalidArgumentType(
5157
                    function=self.modify_config_set_family_selection.__name__,
5158
                    argument='families',
5159
                    arg_type='[tuple(str, bool, bool)]',
5160
                )
5161
5162
            _xmlfamily.add_element("all", to_bool(family[2]))
5163
            _xmlfamily.add_element("growing", to_bool(family[1]))
5164
5165
        return self._send_xml_command(cmd)
5166
5167
    def modify_config(
5168
        self, config_id: str, selection: Optional[str] = None, **kwargs
5169
    ) -> Any:
5170
        """Modifies an existing scan config.
5171
5172
        DEPRECATED. Please use *modify_config_set_* methods instead.
5173
5174
        modify_config has four modes to operate depending on the selection.
5175
5176
        Arguments:
5177
            config_id: UUID of scan config to modify.
5178
            selection: one of 'scan_pref', 'nvt_pref', 'nvt_selection' or
5179
                'family_selection'
5180
            name: New name for preference.
5181
            value: New value for preference.
5182
            nvt_oids: List of NVTs associated with preference to modify.
5183
            family: Name of family to modify.
5184
5185
        Returns:
5186
            The response. See :py:meth:`send_command` for details.
5187
        """
5188
        if not config_id:
5189
            raise RequiredArgument(
5190
                function=self.modify_config.__name__,
5191
                argument='config_id argument',
5192
            )
5193
5194
        if selection is None:
5195
            deprecation(
5196
                "Using modify_config to update the comment of a scan config is"
5197
                "deprecated. Please use modify_config_set_comment instead."
5198
            )
5199
            return self.modify_config_set_comment(
5200
                config_id, kwargs.get("comment")
5201
            )
5202
5203
        if selection not in (
5204
            "nvt_pref",
5205
            "scan_pref",
5206
            "family_selection",
5207
            "nvt_selection",
5208
        ):
5209
            raise InvalidArgument(
5210
                "selection must be one of nvt_pref, "
5211
                "scan_pref, family_selection or "
5212
                "nvt_selection"
5213
            )
5214
5215
        if selection == "nvt_pref":
5216
            deprecation(
5217
                "Using modify_config to update a nvt preference of a scan "
5218
                "config is deprecated. Please use "
5219
                "modify_config_set_nvt_preference instead."
5220
            )
5221
            return self.modify_config_set_nvt_preference(config_id, **kwargs)
5222
5223
        if selection == "scan_pref":
5224
            deprecation(
5225
                "Using modify_config to update a scanner preference of a "
5226
                "scan config is deprecated. Please use "
5227
                "modify_config_set_scanner_preference instead."
5228
            )
5229
            return self.modify_config_set_scanner_preference(
5230
                config_id, **kwargs
5231
            )
5232
5233
        if selection == "nvt_selection":
5234
            deprecation(
5235
                "Using modify_config to update a nvt selection of a "
5236
                "scan config is deprecated. Please use "
5237
                "modify_config_set_nvt_selection instead."
5238
            )
5239
            return self.modify_config_set_nvt_selection(config_id, **kwargs)
5240
5241
        deprecation(
5242
            "Using modify_config to update a family selection of a "
5243
            "scan config is deprecated. Please use "
5244
            "modify_config_set_family_selection instead."
5245
        )
5246
        return self.modify_config_set_family_selection(config_id, **kwargs)
5247
5248 View Code Duplication
    def modify_group(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5249
        self,
5250
        group_id: str,
5251
        *,
5252
        comment: Optional[str] = None,
5253
        name: Optional[str] = None,
5254
        users: Optional[List[str]] = None,
5255
    ) -> Any:
5256
        """Modifies an existing group.
5257
5258
        Arguments:
5259
            group_id: UUID of group to modify.
5260
            comment: Comment on group.
5261
            name: Name of group.
5262
            users: List of user names to be in the group
5263
5264
        Returns:
5265
            The response. See :py:meth:`send_command` for details.
5266
        """
5267
        if not group_id:
5268
            raise RequiredArgument(
5269
                function=self.modify_group.__name__, argument='group_id'
5270
            )
5271
5272
        cmd = XmlCommand("modify_group")
5273
        cmd.set_attribute("group_id", group_id)
5274
5275
        if comment:
5276
            cmd.add_element("comment", comment)
5277
5278
        if name:
5279
            cmd.add_element("name", name)
5280
5281
        if users:
5282
            cmd.add_element("users", to_comma_list(users))
5283
5284
        return self._send_xml_command(cmd)
5285
5286 View Code Duplication
    def modify_note(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5287
        self,
5288
        note_id: str,
5289
        text: str,
5290
        *,
5291
        days_active: Optional[int] = None,
5292
        hosts: Optional[List[str]] = None,
5293
        port: Optional[int] = None,
5294
        result_id: Optional[str] = None,
5295
        severity: Optional[Severity] = None,
5296
        task_id: Optional[str] = None,
5297
        threat: Optional[SeverityLevel] = None,
5298
    ) -> Any:
5299
        """Modifies an existing note.
5300
5301
        Arguments:
5302
            note_id: UUID of note to modify.
5303
            text: The text of the note.
5304
            days_active: Days note will be active. -1 on always, 0 off.
5305
            hosts: A list of hosts addresses
5306
            port: Port to which note applies.
5307
            result_id: Result to which note applies.
5308
            severity: Severity to which note applies.
5309
            task_id: Task to which note applies.
5310
            threat: Threat level to which note applies. Will be converted to
5311
                severity.
5312
5313
        Returns:
5314
            The response. See :py:meth:`send_command` for details.
5315
        """
5316
        if not note_id:
5317
            raise RequiredArgument(
5318
                function=self.modify_note.__name__, argument='note_id'
5319
            )
5320
5321
        if not text:
5322
            raise RequiredArgument(
5323
                function=self.modify_note.__name__, argument='text'
5324
            )
5325
5326
        cmd = XmlCommand("modify_note")
5327
        cmd.set_attribute("note_id", note_id)
5328
        cmd.add_element("text", text)
5329
5330
        if days_active is not None:
5331
            cmd.add_element("active", str(days_active))
5332
5333
        if hosts:
5334
            cmd.add_element("hosts", to_comma_list(hosts))
5335
5336
        if port:
5337
            cmd.add_element("port", str(port))
5338
5339
        if result_id:
5340
            cmd.add_element("result", attrs={"id": result_id})
5341
5342
        if severity:
5343
            cmd.add_element("severity", str(severity))
5344
5345
        if task_id:
5346
            cmd.add_element("task", attrs={"id": task_id})
5347
5348
        if threat is not None:
5349
5350
            if not isinstance(threat, SeverityLevel):
5351
                raise InvalidArgumentType(
5352
                    function=self.modify_note.__name__,
5353
                    argument='threat',
5354
                    arg_type=SeverityLevel.__name__,
5355
                )
5356
5357
            cmd.add_element("threat", threat.value)
5358
5359
        return self._send_xml_command(cmd)
5360
5361 View Code Duplication
    def modify_override(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5362
        self,
5363
        override_id: str,
5364
        text: str,
5365
        *,
5366
        days_active: Optional[int] = None,
5367
        hosts: Optional[List[str]] = None,
5368
        port: Optional[int] = None,
5369
        result_id: Optional[str] = None,
5370
        severity: Optional[Severity] = None,
5371
        new_severity: Optional[Severity] = None,
5372
        task_id: Optional[str] = None,
5373
        threat: Optional[SeverityLevel] = None,
5374
        new_threat: Optional[SeverityLevel] = None,
5375
    ) -> Any:
5376
        """Modifies an existing override.
5377
5378
        Arguments:
5379
            override_id: UUID of override to modify.
5380
            text: The text of the override.
5381
            days_active: Days override will be active. -1 on always,
5382
                0 off.
5383
            hosts: A list of host addresses
5384
            port: Port to which override applies.
5385
            result_id: Result to which override applies.
5386
            severity: Severity to which override applies.
5387
            new_severity: New severity score for result.
5388
            task_id: Task to which override applies.
5389
            threat: Threat level to which override applies.
5390
                Will be converted to severity.
5391
            new_threat: New threat level for results. Will be converted to
5392
                new_severity.
5393
5394
        Returns:
5395
            The response. See :py:meth:`send_command` for details.
5396
        """
5397
        if not override_id:
5398
            raise RequiredArgument(
5399
                function=self.modify_override.__name__, argument='override_id'
5400
            )
5401
        if not text:
5402
            raise RequiredArgument(
5403
                function=self.modify_override.__name__, argument='text'
5404
            )
5405
5406
        cmd = XmlCommand("modify_override")
5407
        cmd.set_attribute("override_id", override_id)
5408
        cmd.add_element("text", text)
5409
5410
        if days_active is not None:
5411
            cmd.add_element("active", str(days_active))
5412
5413
        if hosts:
5414
            cmd.add_element("hosts", to_comma_list(hosts))
5415
5416
        if port:
5417
            cmd.add_element("port", str(port))
5418
5419
        if result_id:
5420
            cmd.add_element("result", attrs={"id": result_id})
5421
5422
        if severity:
5423
            cmd.add_element("severity", str(severity))
5424
5425
        if new_severity:
5426
            cmd.add_element("new_severity", str(new_severity))
5427
5428
        if task_id:
5429
            cmd.add_element("task", attrs={"id": task_id})
5430
5431
        if threat is not None:
5432
            if not isinstance(threat, SeverityLevel):
5433
                raise InvalidArgumentType(
5434
                    function=self.modify_override.__name__,
5435
                    argument='threat',
5436
                    arg_type=SeverityLevel.__name__,
5437
                )
5438
            cmd.add_element("threat", threat.value)
5439
5440
        if new_threat is not None:
5441
            if not isinstance(new_threat, SeverityLevel):
5442
                raise InvalidArgumentType(
5443
                    function=self.modify_override.__name__,
5444
                    argument='new_threat',
5445
                    arg_type=SeverityLevel.__name__,
5446
                )
5447
5448
            cmd.add_element("new_threat", new_threat.value)
5449
5450
        return self._send_xml_command(cmd)
5451
5452
    def modify_report_format(
5453
        self,
5454
        report_format_id: Optional[Union[str, ReportFormatType]] = None,
5455
        *,
5456
        active: Optional[bool] = None,
5457
        name: Optional[str] = None,
5458
        summary: Optional[str] = None,
5459
        param_name: Optional[str] = None,
5460
        param_value: Optional[str] = None,
5461
    ) -> Any:
5462
        """Modifies an existing report format.
5463
5464
        Arguments:
5465
            report_format_id: UUID of report format to modify
5466
                              or ReportFormatType (enum)
5467
            active: Whether the report format is active.
5468
            name: The name of the report format.
5469
            summary: A summary of the report format.
5470
            param_name: The name of the param.
5471
            param_value: The value of the param.
5472
5473
        Returns:
5474
            The response. See :py:meth:`send_command` for details.
5475
        """
5476
        if not report_format_id:
5477
            raise RequiredArgument(
5478
                function=self.modify_report_format.__name__,
5479
                argument='report_format_id ',
5480
            )
5481
5482
        cmd = XmlCommand("modify_report_format")
5483
5484
        if isinstance(report_format_id, ReportFormatType):
5485
            report_format_id = report_format_id.value
5486
5487
        cmd.set_attribute("report_format_id", report_format_id)
5488
5489
        if active is not None:
5490
            cmd.add_element("active", to_bool(active))
5491
5492
        if name:
5493
            cmd.add_element("name", name)
5494
5495
        if summary:
5496
            cmd.add_element("summary", summary)
5497
5498
        if param_name:
5499
            _xmlparam = cmd.add_element("param")
5500
            _xmlparam.add_element("name", param_name)
5501
5502
            if param_value is not None:
5503
                _xmlparam.add_element("value", param_value)
5504
5505
        return self._send_xml_command(cmd)
5506
5507 View Code Duplication
    def modify_role(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5508
        self,
5509
        role_id: str,
5510
        *,
5511
        comment: Optional[str] = None,
5512
        name: Optional[str] = None,
5513
        users: Optional[List[str]] = None,
5514
    ) -> Any:
5515
        """Modifies an existing role.
5516
5517
        Arguments:
5518
            role_id: UUID of role to modify.
5519
            comment: Name of role.
5520
            name: Comment on role.
5521
            users: List of user names.
5522
5523
        Returns:
5524
            The response. See :py:meth:`send_command` for details.
5525
        """
5526
        if not role_id:
5527
            raise RequiredArgument(
5528
                function=self.modify_role.__name__, argument='role_id argument'
5529
            )
5530
5531
        cmd = XmlCommand("modify_role")
5532
        cmd.set_attribute("role_id", role_id)
5533
5534
        if comment:
5535
            cmd.add_element("comment", comment)
5536
5537
        if name:
5538
            cmd.add_element("name", name)
5539
5540
        if users:
5541
            cmd.add_element("users", to_comma_list(users))
5542
5543
        return self._send_xml_command(cmd)
5544
5545
    def modify_scanner(
5546
        self,
5547
        scanner_id: str,
5548
        *,
5549
        scanner_type: Optional[ScannerType] = None,
5550
        host: Optional[str] = None,
5551
        port: Optional[int] = None,
5552
        comment: Optional[str] = None,
5553
        name: Optional[str] = None,
5554
        ca_pub: Optional[str] = None,
5555
        credential_id: Optional[str] = None,
5556
    ) -> Any:
5557
        """Modifies an existing scanner.
5558
5559
        Arguments:
5560
            scanner_id: UUID of scanner to modify.
5561
            scanner_type: New type of the Scanner.
5562
            host: Host of the scanner.
5563
            port: Port of the scanner.
5564
            comment: Comment on scanner.
5565
            name: Name of scanner.
5566
            ca_pub: Certificate of CA to verify scanner's certificate.
5567
            credential_id: UUID of the client certificate credential for the
5568
                Scanner.
5569
5570
        Returns:
5571
            The response. See :py:meth:`send_command` for details.
5572
        """
5573
        if not scanner_id:
5574
            raise RequiredArgument(
5575
                function=self.modify_scanner.__name__,
5576
                argument='scanner_id argument',
5577
            )
5578
5579
        cmd = XmlCommand("modify_scanner")
5580
        cmd.set_attribute("scanner_id", scanner_id)
5581
5582
        if scanner_type is not None:
5583
            if not isinstance(scanner_type, self.types.ScannerType):
5584
                raise InvalidArgumentType(
5585
                    function=self.modify_scanner.__name__,
5586
                    argument='scanner_type',
5587
                    arg_type=self.types.ScannerType.__name__,
5588
                )
5589
5590
            cmd.add_element("type", scanner_type.value)
5591
5592
        if host:
5593
            cmd.add_element("host", host)
5594
5595
        if port:
5596
            cmd.add_element("port", str(port))
5597
5598
        if comment:
5599
            cmd.add_element("comment", comment)
5600
5601
        if name:
5602
            cmd.add_element("name", name)
5603
5604
        if ca_pub:
5605
            cmd.add_element("ca_pub", ca_pub)
5606
5607
        if credential_id:
5608
            cmd.add_element("credential", attrs={"id": str(credential_id)})
5609
5610
        return self._send_xml_command(cmd)
5611
5612
    def modify_setting(
5613
        self,
5614
        setting_id: Optional[str] = None,
5615
        name: Optional[str] = None,
5616
        value: Optional[str] = None,
5617
    ) -> Any:
5618
        """Modifies an existing setting.
5619
5620
        Arguments:
5621
            setting_id: UUID of the setting to be changed.
5622
            name: The name of the setting. Either setting_id or name must be
5623
                passed.
5624
            value: The value of the setting.
5625
5626
        Returns:
5627
            The response. See :py:meth:`send_command` for details.
5628
        """
5629
        if not setting_id and not name:
5630
            raise RequiredArgument(
5631
                function=self.modify_setting.__name__,
5632
                argument='setting_id or name argument',
5633
            )
5634
5635
        if value is None:
5636
            raise RequiredArgument(
5637
                function=self.modify_setting.__name__, argument='value argument'
5638
            )
5639
5640
        cmd = XmlCommand("modify_setting")
5641
5642
        if setting_id:
5643
            cmd.set_attribute("setting_id", setting_id)
5644
        else:
5645
            cmd.add_element("name", name)
5646
5647
        cmd.add_element("value", to_base64(value))
5648
5649
        return self._send_xml_command(cmd)
5650
5651 View Code Duplication
    def modify_user(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5652
        self,
5653
        user_id: str = None,
5654
        name: str = None,
5655
        *,
5656
        new_name: Optional[str] = None,
5657
        comment: Optional[str] = None,
5658
        password: Optional[str] = None,
5659
        auth_source: Optional[UserAuthType] = None,
5660
        role_ids: Optional[List[str]] = None,
5661
        hosts: Optional[List[str]] = None,
5662
        hosts_allow: Optional[bool] = False,
5663
        ifaces: Optional[List[str]] = None,
5664
        ifaces_allow: Optional[bool] = False,
5665
        group_ids: Optional[List[str]] = None,
5666
    ) -> Any:
5667
        """Modifies an existing user. Most of the fields need to be supplied
5668
        for changing a single field even if no change is wanted for those.
5669
        Else empty values are inserted for the missing fields instead.
5670
        Arguments:
5671
            user_id: UUID of the user to be modified. Overrides name element
5672
                argument.
5673
            name: The name of the user to be modified. Either user_id or name
5674
                must be passed.
5675
            new_name: The new name for the user.
5676
            comment: Comment on the user.
5677
            password: The password for the user.
5678
            auth_source: Source allowed for authentication for this user.
5679
            roles_id: List of roles UUIDs for the user.
5680
            hosts: User access rules: List of hosts.
5681
            hosts_allow: Defines how the hosts list is to be interpreted.
5682
                If False (default) the list is treated as a deny list.
5683
                All hosts are allowed by default except those provided by
5684
                the hosts parameter. If True the list is treated as a
5685
                allow list. All hosts are denied by default except those
5686
                provided by the hosts parameter.
5687
            ifaces: User access rules: List of ifaces.
5688
            ifaces_allow: Defines how the ifaces list is to be interpreted.
5689
                If False (default) the list is treated as a deny list.
5690
                All ifaces are allowed by default except those provided by
5691
                the ifaces parameter. If True the list is treated as a
5692
                allow list. All ifaces are denied by default except those
5693
                provided by the ifaces parameter.
5694
            group_ids: List of group UUIDs for the user.
5695
5696
        Returns:
5697
            The response. See :py:meth:`send_command` for details.
5698
        """
5699
        if not user_id and not name:
5700
            raise RequiredArgument(
5701
                function=self.modify_user.__name__, argument='user_id or name'
5702
            )
5703
5704
        cmd = XmlCommand("modify_user")
5705
5706
        if user_id:
5707
            cmd.set_attribute("user_id", user_id)
5708
        else:
5709
            cmd.add_element("name", name)
5710
5711
        if new_name:
5712
            cmd.add_element("new_name", new_name)
5713
5714
        if role_ids:
5715
            for role in role_ids:
5716
                cmd.add_element("role", attrs={"id": role})
5717
5718
        if hosts:
5719
            cmd.add_element(
5720
                "hosts",
5721
                to_comma_list(hosts),
5722
                attrs={"allow": to_bool(hosts_allow)},
5723
            )
5724
5725
        if ifaces:
5726
            cmd.add_element(
5727
                "ifaces",
5728
                to_comma_list(ifaces),
5729
                attrs={"allow": to_bool(ifaces_allow)},
5730
            )
5731
5732
        if comment:
5733
            cmd.add_element("comment", comment)
5734
5735
        if password:
5736
            cmd.add_element("password", password)
5737
5738
        if auth_source:
5739
            _xmlauthsrc = cmd.add_element("sources")
5740
            _xmlauthsrc.add_element("source", auth_source.value)
5741
5742
        if group_ids:
5743
            _xmlgroups = cmd.add_element("groups")
5744
            for group_id in group_ids:
5745
                _xmlgroups.add_element("group", attrs={"id": group_id})
5746
5747
        return self._send_xml_command(cmd)
5748
5749
    def restore(self, entity_id: str) -> Any:
5750
        """Restore an entity from the trashcan
5751
5752
        Arguments:
5753
            entity_id: ID of the entity to be restored from the trashcan
5754
5755
        Returns:
5756
            The response. See :py:meth:`send_command` for details.
5757
        """
5758
        if not entity_id:
5759
            raise RequiredArgument(
5760
                function=self.restore.__name__, argument='entity_id'
5761
            )
5762
5763
        cmd = XmlCommand("restore")
5764
        cmd.set_attribute("id", entity_id)
5765
5766
        return self._send_xml_command(cmd)
5767
5768
    def sync_cert(self) -> Any:
5769
        """Request a synchronization with the CERT feed service
5770
5771
        Returns:
5772
            The response. See :py:meth:`send_command` for details.
5773
        """
5774
        return self._send_xml_command(XmlCommand("sync_cert"))
5775
5776
    def sync_config(self) -> Any:
5777
        """Request an OSP config synchronization with scanner
5778
5779
        Returns:
5780
            The response. See :py:meth:`send_command` for details.
5781
        """
5782
        return self._send_xml_command(XmlCommand("sync_config"))
5783
5784
    def sync_feed(self) -> Any:
5785
        """Request a synchronization with the NVT feed service
5786
5787
        Returns:
5788
            The response. See :py:meth:`send_command` for details.
5789
        """
5790
        return self._send_xml_command(XmlCommand("sync_feed"))
5791
5792
    def sync_scap(self) -> Any:
5793
        """Request a synchronization with the SCAP feed service
5794
5795
        Returns:
5796
            The response. See :py:meth:`send_command` for details.
5797
        """
5798
        return self._send_xml_command(XmlCommand("sync_scap"))
5799
5800
    def test_alert(self, alert_id: str) -> Any:
5801
        """Run an alert
5802
5803
        Invoke a test run of an alert
5804
5805
        Arguments:
5806
            alert_id: UUID of the alert to be tested
5807
5808
        Returns:
5809
            The response. See :py:meth:`send_command` for details.
5810
        """
5811
        if not alert_id:
5812
            raise InvalidArgument("test_alert requires an alert_id argument")
5813
5814
        cmd = XmlCommand("test_alert")
5815
        cmd.set_attribute("alert_id", alert_id)
5816
5817
        return self._send_xml_command(cmd)
5818
5819
    def trigger_alert(
5820
        self,
5821
        alert_id: str,
5822
        report_id: str,
5823
        *,
5824
        filter: Optional[str] = None,
5825
        filter_id: Optional[str] = None,
5826
        report_format_id: Optional[Union[str, ReportFormatType]] = None,
5827
        delta_report_id: Optional[str] = None,
5828
    ) -> Any:
5829
        """Run an alert by ignoring its event and conditions
5830
5831
        The alert is triggered to run immediately with the provided filtered
5832
        report by ignoring the even and condition settings.
5833
5834
        Arguments:
5835
            alert_id: UUID of the alert to be run
5836
            report_id: UUID of the report to be provided to the alert
5837
            filter: Filter term to use to filter results in the report
5838
            filter_id: UUID of filter to use to filter results in the report
5839
            report_format_id: UUID of report format to use
5840
                              or ReportFormatType (enum)
5841
            delta_report_id: UUID of an existing report to compare report to.
5842
5843
        Returns:
5844
            The response. See :py:meth:`send_command` for details.
5845
        """
5846
        if not alert_id:
5847
            raise RequiredArgument(
5848
                function=self.trigger_alert.__name__,
5849
                argument='alert_id argument',
5850
            )
5851
5852
        if not report_id:
5853
            raise RequiredArgument(
5854
                function=self.trigger_alert.__name__,
5855
                argument='report_id argument',
5856
            )
5857
5858
        cmd = XmlCommand("get_reports")
5859
        cmd.set_attribute("report_id", report_id)
5860
        cmd.set_attribute("alert_id", alert_id)
5861
5862
        if filter:
5863
            cmd.set_attribute("filter", filter)
5864
5865
        if filter_id:
5866
            cmd.set_attribute("filt_id", filter_id)
5867
5868
        if report_format_id:
5869
            if isinstance(report_format_id, ReportFormatType):
5870
                report_format_id = report_format_id.value
5871
5872
            cmd.set_attribute("format_id", report_format_id)
5873
5874
        if delta_report_id:
5875
            cmd.set_attribute("delta_report_id", delta_report_id)
5876
5877
        return self._send_xml_command(cmd)
5878
5879 View Code Duplication
    def verify_report_format(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5880
        self, report_format_id: Union[str, ReportFormatType]
5881
    ) -> Any:
5882
        """Verify an existing report format
5883
5884
        Verifies the trust level of an existing report format. It will be
5885
        checked whether the signature of the report format currently matches the
5886
        report format. This includes the script and files used to generate
5887
        reports of this format. It is *not* verified if the report format works
5888
        as expected by the user.
5889
5890
        Arguments:
5891
            report_format_id: UUID of the report format to be verified
5892
                              or ReportFormatType (enum)
5893
5894
        Returns:
5895
            The response. See :py:meth:`send_command` for details.
5896
        """
5897
        if not report_format_id:
5898
            raise RequiredArgument(
5899
                function=self.verify_report_format.__name__,
5900
                argument='report_format_id',
5901
            )
5902
5903
        cmd = XmlCommand("verify_report_format")
5904
5905
        if isinstance(report_format_id, ReportFormatType):
5906
            report_format_id = report_format_id.value
5907
5908
        cmd.set_attribute("report_format_id", report_format_id)
5909
5910
        return self._send_xml_command(cmd)
5911
5912
    def verify_scanner(self, scanner_id: str) -> Any:
5913
        """Verify an existing scanner
5914
5915
        Verifies if it is possible to connect to an existing scanner. It is
5916
        *not* verified if the scanner works as expected by the user.
5917
5918
        Arguments:
5919
            scanner_id: UUID of the scanner to be verified
5920
5921
        Returns:
5922
            The response. See :py:meth:`send_command` for details.
5923
        """
5924
        if not scanner_id:
5925
            raise RequiredArgument(
5926
                function=self.verify_scanner.__name__, argument='scanner_id'
5927
            )
5928
5929
        cmd = XmlCommand("verify_scanner")
5930
        cmd.set_attribute("scanner_id", scanner_id)
5931
5932
        return self._send_xml_command(cmd)
5933