Completed
Push — master ( a4987b...bdfb6a )
by Björn
28s queued 14s
created

GmpV208Mixin.get_reports()   B

Complexity

Conditions 6

Size

Total Lines 42
Code Lines 21

Duplication

Lines 42
Ratio 100 %

Importance

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