Passed
Pull Request — master (#347)
by Jaspar
01:22
created

gvm.protocols.gmpv7.gmpv7.GmpV7Mixin.sync_config()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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