Passed
Pull Request — master (#192)
by Jaspar
02:22
created

gvm.protocols.gmpv7.Gmp.import_report()   B

Complexity

Conditions 7

Size

Total Lines 56
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 30
nop 7
dl 0
loc 56
rs 7.76
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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