Completed
Push — master ( de9ad4...519cea )
by Björn
25s queued 18s
created

GmpV7Mixin.clone_report_format()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 2
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 - 2019 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
# pylint: disable=too-many-lines,redefined-builtin
20
"""
21
Module for communication with gvmd in `Greenbone Management Protocol version 7`_
22
23
.. _Greenbone Management Protocol version 7:
24
    https://docs.greenbone.net/API/GMP/gmp-7.0.html
25
"""
26
import base64
27
import collections
28
import logging
29
import numbers
30
31
from typing import Any, List, Optional, Callable
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(self, report_format_id: str) -> Any:
1359
        """Clone a report format from an existing one
1360
1361
        Arguments:
1362
            report_format_id: UUID of the existing report_ ormat
1363
1364
        Returns:
1365
            The response. See :py:meth:`send_command` for details.
1366
        """
1367
        if not report_format_id:
1368
            raise RequiredArgument(
1369
                function=self.clone_report_format.__name__,
1370
                argument='report_format_id',
1371
            )
1372
1373
        cmd = XmlCommand("create_report_format")
1374
        cmd.add_element("copy", report_format_id)
1375
        return self._send_xml_command(cmd)
1376
1377
    def import_report_format(self, report_format: str) -> Any:
1378
        """Import a report format from XML
1379
1380
        Arguments:
1381
            report_format: Report format XML as string to import. This XML must
1382
                contain a :code:`<get_report_formats_response>` root element.
1383
1384
        Returns:
1385
            The response. See :py:meth:`send_command` for details.
1386
        """
1387
        if not report_format:
1388
            raise RequiredArgument(
1389
                function=self.import_report_format.__name__,
1390
                argument='report_format',
1391
            )
1392
1393
        cmd = XmlCommand("create_report_format")
1394
1395
        try:
1396
            cmd.append_xml_str(report_format)
1397
        except etree.XMLSyntaxError as e:
1398
            raise InvalidArgument(
1399
                function=self.import_report_format.__name__,
1400
                argument='report_format',
1401
            ) from e
1402
1403
        return self._send_xml_command(cmd)
1404
1405
    def create_role(
1406
        self,
1407
        name: str,
1408
        *,
1409
        comment: Optional[str] = None,
1410
        users: Optional[List[str]] = None
1411
    ) -> Any:
1412
        """Create a new role
1413
1414
        Arguments:
1415
            name: Name of the role
1416
            comment: Comment for the role
1417
            users: List of user names to add to the role
1418
1419
        Returns:
1420
            The response. See :py:meth:`send_command` for details.
1421
        """
1422
1423
        if not name:
1424
            raise RequiredArgument(
1425
                function=self.create_role.__name__, argument='name'
1426
            )
1427
1428
        cmd = XmlCommand("create_role")
1429
        cmd.add_element("name", name)
1430
1431
        if comment:
1432
            cmd.add_element("comment", comment)
1433
1434
        if users:
1435
            cmd.add_element("users", _to_comma_list(users))
1436
1437
        return self._send_xml_command(cmd)
1438
1439
    def clone_role(self, role_id: str) -> Any:
1440
        """Clone an existing role
1441
1442
        Arguments:
1443
            role_id: UUID of an existing role to clone from
1444
1445
        Returns:
1446
            The response. See :py:meth:`send_command` for details.
1447
        """
1448
        if not role_id:
1449
            raise RequiredArgument(
1450
                function=self.clone_role.__name__, argument='role_id'
1451
            )
1452
1453
        cmd = XmlCommand("create_role")
1454
        cmd.add_element("copy", role_id)
1455
        return self._send_xml_command(cmd)
1456
1457
    def create_scanner(
1458
        self,
1459
        name: str,
1460
        host: str,
1461
        port: int,
1462
        scanner_type: ScannerType,
1463
        credential_id: str,
1464
        *,
1465
        ca_pub: Optional[str] = None,
1466
        comment: Optional[str] = None
1467
    ) -> Any:
1468
        """Create a new scanner
1469
1470
        Arguments:
1471
            name: Name of the scanner
1472
            host: The host of the scanner
1473
            port: The port of the scanner
1474
            scanner_type: Type of the scanner.
1475
            credential_id: UUID of client certificate credential for the
1476
                scanner
1477
            ca_pub: Certificate of CA to verify scanner certificate
1478
            comment: Comment for the scanner
1479
1480
        Returns:
1481
            The response. See :py:meth:`send_command` for details.
1482
        """
1483
        if not name:
1484
            raise RequiredArgument(
1485
                function=self.create_scanner.__name__, argument='name'
1486
            )
1487
1488
        if not host:
1489
            raise RequiredArgument(
1490
                function=self.create_scanner.__name__, argument='host'
1491
            )
1492
1493
        if not port:
1494
            raise RequiredArgument(
1495
                function=self.create_scanner.__name__, argument='port'
1496
            )
1497
1498
        if not scanner_type:
1499
            raise RequiredArgument(
1500
                function=self.create_scanner.__name__, argument='scanner_type'
1501
            )
1502
1503
        if not credential_id:
1504
            raise RequiredArgument(
1505
                function=self.create_scanner.__name__, argument='credential_id'
1506
            )
1507
1508
        if not isinstance(scanner_type, self.types.ScannerType):
1509
            raise InvalidArgumentType(
1510
                function=self.create_scanner.__name__,
1511
                argument='scanner_type',
1512
                arg_type=self.types.ScannerType.__name__,
1513
            )
1514
1515
        cmd = XmlCommand("create_scanner")
1516
        cmd.add_element("name", name)
1517
        cmd.add_element("host", host)
1518
        cmd.add_element("port", str(port))
1519
        cmd.add_element("type", scanner_type.value)
1520
1521
        if ca_pub:
1522
            cmd.add_element("ca_pub", ca_pub)
1523
1524
        cmd.add_element("credential", attrs={"id": str(credential_id)})
1525
1526
        if comment:
1527
            cmd.add_element("comment", comment)
1528
1529
        return self._send_xml_command(cmd)
1530
1531
    def clone_scanner(self, scanner_id: str) -> Any:
1532
        """Clone an existing scanner
1533
1534
        Arguments:
1535
            scanner_id: UUID of an existing scanner to clone from
1536
1537
        Returns:
1538
            The response. See :py:meth:`send_command` for details.
1539
        """
1540
        if not scanner_id:
1541
            raise RequiredArgument(
1542
                function=self.clone_scanner.__name__, argument='scanner_id'
1543
            )
1544
1545
        cmd = XmlCommand("create_scanner")
1546
        cmd.add_element("copy", scanner_id)
1547
        return self._send_xml_command(cmd)
1548
1549 View Code Duplication
    def create_schedule(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1550
        self,
1551
        name: str,
1552
        *,
1553
        comment: Optional[str] = None,
1554
        first_time_minute: Optional[int] = None,
1555
        first_time_hour: Optional[int] = None,
1556
        first_time_day_of_month: Optional[int] = None,
1557
        first_time_month: Optional[int] = None,
1558
        first_time_year: Optional[int] = None,
1559
        duration: Optional[int] = None,
1560
        duration_unit: Optional[TimeUnit] = None,
1561
        period: Optional[int] = None,
1562
        period_unit: Optional[TimeUnit] = None,
1563
        timezone: Optional[str] = None
1564
    ) -> Any:
1565
        """Create a new schedule
1566
1567
        Arguments:
1568
            name: Name of the schedule
1569
            comment: Comment for the schedule
1570
            first_time_minute: First time minute the schedule will run. Must be
1571
                an integer >= 0.
1572
            first_time_hour: First time hour the schedule will run. Must be an
1573
                integer >= 0.
1574
            first_time_day_of_month: First time day of month the schedule will
1575
                run. Must be an integer > 0 <= 31.
1576
            first_time_month: First time month the schedule will run. Must be an
1577
                integer >= 1 <= 12.
1578
            first_time_year: First time year the schedule will run. Must be an
1579
                integer >= 1970.
1580
            duration: How long the Manager will run the scheduled task for until
1581
                it gets paused if not finished yet. Must be an integer > 0.
1582
            duration_unit: Unit of the duration. One of second,
1583
                minute, hour, day, week, month, year, decade. Required if
1584
                duration is set.
1585
            period: How often the Manager will repeat the
1586
                scheduled task. Must be an integer > 0.
1587
            period_unit: Unit of the period. One of second,
1588
                minute, hour, day, week, month, year, decade. Required if
1589
                period is set.
1590
            timezone: The timezone the schedule will follow
1591
1592
        Returns:
1593
            The response. See :py:meth:`send_command` for details.
1594
        """
1595
        if not name:
1596
            raise RequiredArgument(
1597
                function=self.create_schedule.__name__, argument='name'
1598
            )
1599
1600
        cmd = XmlCommand("create_schedule")
1601
        cmd.add_element("name", name)
1602
1603
        if comment:
1604
            cmd.add_element("comment", comment)
1605
1606
        if (
1607
            first_time_minute is not None
1608
            or first_time_hour is not None
1609
            or first_time_day_of_month is not None
1610
            or first_time_month is not None
1611
            or first_time_year is not None
1612
        ):
1613
1614
            if first_time_minute is None:
1615
                raise RequiredArgument(
1616
                    function=self.create_schedule.__name__,
1617
                    argument='first_time_minute',
1618
                )
1619
            elif (
1620
                not isinstance(first_time_minute, numbers.Integral)
1621
                or first_time_minute < 0
1622
            ):
1623
                raise InvalidArgument(
1624
                    "first_time_minute argument of create_schedule needs to be "
1625
                    "an integer greater or equal 0"
1626
                )
1627
1628
            if first_time_hour is None:
1629
                raise RequiredArgument(
1630
                    function=self.create_schedule.__name__,
1631
                    argument='first_time_hour',
1632
                )
1633
            elif (
1634
                not isinstance(first_time_hour, numbers.Integral)
1635
                or first_time_hour < 0
1636
            ):
1637
                raise InvalidArgument(
1638
                    "first_time_hour argument of create_schedule needs to be "
1639
                    "an integer greater or equal 0"
1640
                )
1641
1642
            if first_time_day_of_month is None:
1643
                raise RequiredArgument(
1644
                    function=self.create_schedule.__name__,
1645
                    argument='first_time_day_of_month',
1646
                )
1647
            elif (
1648
                not isinstance(first_time_day_of_month, numbers.Integral)
1649
                or first_time_day_of_month < 1
1650
                or first_time_day_of_month > 31
1651
            ):
1652
                raise InvalidArgument(
1653
                    "first_time_day_of_month argument of create_schedule needs "
1654
                    "to be an integer between 1 and 31"
1655
                )
1656
1657
            if first_time_month is None:
1658
                raise RequiredArgument(
1659
                    function=self.create_schedule.__name__,
1660
                    argument='first_time_month',
1661
                )
1662
            elif (
1663
                not isinstance(first_time_month, numbers.Integral)
1664
                or first_time_month < 1
1665
                or first_time_month > 12
1666
            ):
1667
                raise InvalidArgument(
1668
                    "first_time_month argument of create_schedule needs "
1669
                    "to be an integer between 1 and 12"
1670
                )
1671
1672
            if first_time_year is None:
1673
                raise RequiredArgument(
1674
                    function=self.create_schedule.__name__,
1675
                    argument='first_time_year',
1676
                )
1677
            elif (
1678
                not isinstance(first_time_year, numbers.Integral)
1679
                or first_time_year < 1970
1680
            ):
1681
                raise InvalidArgument(
1682
                    "first_time_year argument of create_schedule needs "
1683
                    "to be an integer greater or equal 1970"
1684
                )
1685
1686
            _xmlftime = cmd.add_element("first_time")
1687
            _xmlftime.add_element("minute", str(first_time_minute))
1688
            _xmlftime.add_element("hour", str(first_time_hour))
1689
            _xmlftime.add_element("day_of_month", str(first_time_day_of_month))
1690
            _xmlftime.add_element("month", str(first_time_month))
1691
            _xmlftime.add_element("year", str(first_time_year))
1692
1693
        if duration is not None:
1694
            if not duration_unit:
1695
                raise RequiredArgument(
1696
                    function=self.create_schedule.__name__,
1697
                    argument='duration_unit',
1698
                )
1699
1700
            if not isinstance(duration_unit, TimeUnit):
1701
                raise InvalidArgumentType(
1702
                    function=self.create_schedule.__name__,
1703
                    argument='duration_unit',
1704
                    arg_type=TimeUnit.__name__,
1705
                )
1706
1707
            if not isinstance(duration, numbers.Integral) or duration < 1:
1708
                raise InvalidArgument(
1709
                    "duration argument must be an integer greater than 0"
1710
                )
1711
1712
            _xmlduration = cmd.add_element("duration", str(duration))
1713
            _xmlduration.add_element("unit", duration_unit.value)
1714
1715
        if period is not None:
1716
            if not period_unit:
1717
                raise RequiredArgument(
1718
                    function=self.create_schedule.__name__,
1719
                    argument='period_unit',
1720
                )
1721
1722
            if not isinstance(period_unit, TimeUnit):
1723
                raise InvalidArgumentType(
1724
                    function=self.create_schedule.__name__,
1725
                    argument='period_unit',
1726
                    arg_type=TimeUnit.__name__,
1727
                )
1728
1729
            if not isinstance(period, numbers.Integral) or period < 0:
1730
                raise InvalidArgument(
1731
                    "period argument must be a positive integer"
1732
                )
1733
1734
            _xmlperiod = cmd.add_element("period", str(period))
1735
            _xmlperiod.add_element("unit", period_unit.value)
1736
1737
        if timezone:
1738
            cmd.add_element("timezone", timezone)
1739
1740
        return self._send_xml_command(cmd)
1741
1742
    def clone_schedule(self, schedule_id: str) -> Any:
1743
        """Clone an existing schedule
1744
1745
        Arguments:
1746
            schedule_id: UUID of an existing schedule to clone from
1747
1748
        Returns:
1749
            The response. See :py:meth:`send_command` for details.
1750
        """
1751
        if not schedule_id:
1752
            raise RequiredArgument(
1753
                function=self.clone_schedule.__name__, argument='schedule_id'
1754
            )
1755
1756
        cmd = XmlCommand("create_schedule")
1757
        cmd.add_element("copy", schedule_id)
1758
        return self._send_xml_command(cmd)
1759
1760
    def create_tag(
1761
        self,
1762
        name: str,
1763
        resource_type: EntityType,
1764
        *,
1765
        resource_id: Optional[str] = None,
1766
        value: Optional[str] = None,
1767
        comment: Optional[str] = None,
1768
        active: Optional[bool] = None
1769
    ) -> Any:
1770
        """Create a new tag
1771
1772
        Arguments:
1773
            name: Name of the tag. A full tag name consisting of namespace
1774
                and predicate e.g. `foo:bar`.
1775
            resource_id: ID of the resource the tag is to be attached to.
1776
            resource_type: Entity type the tag is to be attached to
1777
            value: Value associated with the tag
1778
            comment: Comment for the tag
1779
            active: Whether the tag should be active
1780
1781
        Returns:
1782
            The response. See :py:meth:`send_command` for details.
1783
        """
1784
        if not name:
1785
            raise RequiredArgument(
1786
                function=self.create_tag.__name__, argument='name'
1787
            )
1788
1789
        if not resource_type:
1790
            raise RequiredArgument(
1791
                function=self.create_tag.__name__, argument='resource_type'
1792
            )
1793
1794
        if not isinstance(resource_type, self.types.EntityType):
1795
            raise InvalidArgumentType(
1796
                function=self.create_tag.__name__,
1797
                argument='resource_type',
1798
                arg_type=self.types.EntityType.__name__,
1799
            )
1800
1801
        cmd = XmlCommand("create_tag")
1802
        cmd.add_element("name", name)
1803
1804
        if not resource_id:
1805
            resource_id = ''
1806
1807
        _xmlresource = cmd.add_element(
1808
            "resource", attrs={"id": str(resource_id)}
1809
        )
1810
        _xmlresource.add_element("type", resource_type.value)
1811
1812
        if comment:
1813
            cmd.add_element("comment", comment)
1814
1815
        if value:
1816
            cmd.add_element("value", value)
1817
1818
        if active is not None:
1819
            if active:
1820
                cmd.add_element("active", "1")
1821
            else:
1822
                cmd.add_element("active", "0")
1823
1824
        return self._send_xml_command(cmd)
1825
1826
    def clone_tag(self, tag_id: str) -> Any:
1827
        """Clone an existing tag
1828
1829
        Arguments:
1830
            tag_id: UUID of an existing tag to clone from
1831
1832
        Returns:
1833
            The response. See :py:meth:`send_command` for details.
1834
        """
1835
        if not tag_id:
1836
            raise RequiredArgument(
1837
                function=self.clone_tag.__name__, argument='tag_id'
1838
            )
1839
1840
        cmd = XmlCommand("create_tag")
1841
        cmd.add_element("copy", tag_id)
1842
        return self._send_xml_command(cmd)
1843
1844
    def create_target(
1845
        self,
1846
        name: str,
1847
        *,
1848
        make_unique: Optional[bool] = None,
1849
        asset_hosts_filter: Optional[str] = None,
1850
        hosts: Optional[List[str]] = None,
1851
        comment: Optional[str] = None,
1852
        exclude_hosts: Optional[List[str]] = None,
1853
        ssh_credential_id: Optional[str] = None,
1854
        ssh_credential_port: Optional[int] = None,
1855
        smb_credential_id: Optional[str] = None,
1856
        esxi_credential_id: Optional[str] = None,
1857
        snmp_credential_id: Optional[str] = None,
1858
        alive_test: Optional[AliveTest] = None,
1859
        reverse_lookup_only: Optional[bool] = None,
1860
        reverse_lookup_unify: Optional[bool] = None,
1861
        port_range: Optional[str] = None,
1862
        port_list_id: Optional[str] = None
1863
    ) -> Any:
1864
        """Create a new target
1865
1866
        Arguments:
1867
            name: Name of the target
1868
            make_unique: Append a unique suffix if the name already exists
1869
            asset_hosts_filter: Filter to select target host from assets hosts
1870
            hosts: List of hosts addresses to scan
1871
            exclude_hosts: List of hosts addresses to exclude from scan
1872
            comment: Comment for the target
1873
            ssh_credential_id: UUID of a ssh credential to use on target
1874
            ssh_credential_port: The port to use for ssh credential
1875
            smb_credential_id: UUID of a smb credential to use on target
1876
            snmp_credential_id: UUID of a snmp credential to use on target
1877
            esxi_credential_id: UUID of a esxi credential to use on target
1878
            alive_test: Which alive test to use
1879
            reverse_lookup_only: Whether to scan only hosts that have names
1880
            reverse_lookup_unify: Whether to scan only one IP when multiple IPs
1881
                have the same name.
1882
            port_range: Port range for the target
1883
            port_list_id: UUID of the port list to use on target
1884
1885
        Returns:
1886
            The response. See :py:meth:`send_command` for details.
1887
        """
1888
        if not name:
1889
            raise RequiredArgument(
1890
                function=self.create_target.__name__, argument='name'
1891
            )
1892
1893
        cmd = XmlCommand("create_target")
1894
        _xmlname = cmd.add_element("name", name)
1895
1896
        if make_unique is not None:
1897
            _xmlname.add_element("make_unique", _to_bool(make_unique))
1898
1899
        if asset_hosts_filter:
1900
            cmd.add_element(
1901
                "asset_hosts", attrs={"filter": str(asset_hosts_filter)}
1902
            )
1903
        elif hosts:
1904
            cmd.add_element("hosts", _to_comma_list(hosts))
1905
        else:
1906
            raise RequiredArgument(
1907
                function=self.create_target.__name__,
1908
                argument='hosts or asset_hosts_filter',
1909
            )
1910
1911
        if comment:
1912
            cmd.add_element("comment", comment)
1913
1914
        if exclude_hosts:
1915
            cmd.add_element("exclude_hosts", _to_comma_list(exclude_hosts))
1916
1917
        if ssh_credential_id:
1918
            _xmlssh = cmd.add_element(
1919
                "ssh_credential", attrs={"id": ssh_credential_id}
1920
            )
1921
            if ssh_credential_port:
1922
                _xmlssh.add_element("port", str(ssh_credential_port))
1923
1924
        if smb_credential_id:
1925
            cmd.add_element("smb_credential", attrs={"id": smb_credential_id})
1926
1927
        if esxi_credential_id:
1928
            cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id})
1929
1930
        if snmp_credential_id:
1931
            cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id})
1932
1933
        if alive_test:
1934
            if not isinstance(alive_test, AliveTest):
1935
                raise InvalidArgumentType(
1936
                    function=self.create_target.__name__,
1937
                    argument='alive_test',
1938
                    arg_type=AliveTest.__name__,
1939
                )
1940
1941
            cmd.add_element("alive_tests", alive_test.value)
1942
1943
        if reverse_lookup_only is not None:
1944
            cmd.add_element(
1945
                "reverse_lookup_only", _to_bool(reverse_lookup_only)
1946
            )
1947
1948
        if reverse_lookup_unify is not None:
1949
            cmd.add_element(
1950
                "reverse_lookup_unify", _to_bool(reverse_lookup_unify)
1951
            )
1952
1953
        if port_range:
1954
            cmd.add_element("port_range", port_range)
1955
1956
        if port_list_id:
1957
            cmd.add_element("port_list", attrs={"id": port_list_id})
1958
1959
        return self._send_xml_command(cmd)
1960
1961
    def clone_target(self, target_id: str) -> Any:
1962
        """Clone an existing target
1963
1964
        Arguments:
1965
            target_id: UUID of an existing target to clone from
1966
1967
        Returns:
1968
            The response. See :py:meth:`send_command` for details.
1969
        """
1970
        if not target_id:
1971
            raise RequiredArgument(
1972
                function=self.clone_target.__name__, argument='target_id'
1973
            )
1974
1975
        cmd = XmlCommand("create_target")
1976
        cmd.add_element("copy", target_id)
1977
        return self._send_xml_command(cmd)
1978
1979 View Code Duplication
    def create_task(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1980
        self,
1981
        name: str,
1982
        config_id: str,
1983
        target_id: str,
1984
        scanner_id: str,
1985
        *,
1986
        alterable: Optional[bool] = None,
1987
        hosts_ordering: Optional[HostsOrdering] = None,
1988
        schedule_id: Optional[str] = None,
1989
        alert_ids: Optional[List[str]] = None,
1990
        comment: Optional[str] = None,
1991
        schedule_periods: Optional[int] = None,
1992
        observers: Optional[List[str]] = None,
1993
        preferences: Optional[dict] = None
1994
    ) -> Any:
1995
        """Create a new task
1996
1997
        Arguments:
1998
            name: Name of the task
1999
            config_id: UUID of scan config to use by the task
2000
            target_id: UUID of target to be scanned
2001
            scanner_id: UUID of scanner to use for scanning the target
2002
            comment: Comment for the task
2003
            alterable: Whether the task should be alterable
2004
            alert_ids: List of UUIDs for alerts to be applied to the task
2005
            hosts_ordering: The order hosts are scanned in
2006
            schedule_id: UUID of a schedule when the task should be run.
2007
            schedule_periods: A limit to the number of times the task will be
2008
                scheduled, or 0 for no limit
2009
            observers: List of names or ids of users which should be allowed to
2010
                observe this task
2011
            preferences: Name/Value pairs of scanner preferences.
2012
2013
        Returns:
2014
            The response. See :py:meth:`send_command` for details.
2015
        """
2016
        if not name:
2017
            raise RequiredArgument(
2018
                function=self.create_task.__name__, argument='name'
2019
            )
2020
2021
        if not config_id:
2022
            raise RequiredArgument(
2023
                function=self.create_task.__name__, argument='config_id'
2024
            )
2025
2026
        if not target_id:
2027
            raise RequiredArgument(
2028
                function=self.create_task.__name__, argument='target_id'
2029
            )
2030
2031
        if not scanner_id:
2032
            raise RequiredArgument(
2033
                function=self.create_task.__name__, argument='scanner_id'
2034
            )
2035
2036
        # don't allow to create a container task with create_task
2037
        if target_id == '0':
2038
            raise InvalidArgument(
2039
                'Invalid argument {} for target_id'.format(target_id)
2040
            )
2041
2042
        cmd = XmlCommand("create_task")
2043
        cmd.add_element("name", name)
2044
        cmd.add_element("config", attrs={"id": config_id})
2045
        cmd.add_element("target", attrs={"id": target_id})
2046
        cmd.add_element("scanner", attrs={"id": scanner_id})
2047
2048
        if comment:
2049
            cmd.add_element("comment", comment)
2050
2051
        if alterable is not None:
2052
            cmd.add_element("alterable", _to_bool(alterable))
2053
2054
        if hosts_ordering:
2055
            if not isinstance(hosts_ordering, HostsOrdering):
2056
                raise InvalidArgumentType(
2057
                    function=self.create_task.__name__,
2058
                    argument='hosts_ordering',
2059
                    arg_type=HostsOrdering.__name__,
2060
                )
2061
            cmd.add_element("hosts_ordering", hosts_ordering.value)
2062
2063
        if alert_ids:
2064
            if isinstance(alert_ids, str):
2065
                deprecation(
2066
                    "Please pass a list as alert_ids parameter to create_task. "
2067
                    "Passing a string is deprecated and will be removed in "
2068
                    "future."
2069
                )
2070
2071
                # if a single id is given as a string wrap it into a list
2072
                alert_ids = [alert_ids]
2073
            if _is_list_like(alert_ids):
2074
                # parse all given alert id's
2075
                for alert in alert_ids:
2076
                    cmd.add_element("alert", attrs={"id": str(alert)})
2077
2078
        if schedule_id:
2079
            cmd.add_element("schedule", attrs={"id": schedule_id})
2080
2081
            if schedule_periods is not None:
2082
                if (
2083
                    not isinstance(schedule_periods, numbers.Integral)
2084
                    or schedule_periods < 0
2085
                ):
2086
                    raise InvalidArgument(
2087
                        "schedule_periods must be an integer greater or equal "
2088
                        "than 0"
2089
                    )
2090
                cmd.add_element("schedule_periods", str(schedule_periods))
2091
2092
        if observers is not None:
2093
            if not _is_list_like(observers):
2094
                raise InvalidArgumentType(
2095
                    function=self.create_task.__name__,
2096
                    argument='observers',
2097
                    arg_type='list',
2098
                )
2099
2100
            # gvmd splits by comma and space
2101
            # gvmd tries to lookup each value as user name and afterwards as
2102
            # user id. So both user name and user id are possible
2103
            cmd.add_element("observers", _to_comma_list(observers))
2104
2105
        if preferences is not None:
2106
            if not isinstance(preferences, collections.abc.Mapping):
2107
                raise InvalidArgumentType(
2108
                    function=self.create_task.__name__,
2109
                    argument='preferences',
2110
                    arg_type=collections.abc.Mapping.__name__,
2111
                )
2112
2113
            _xmlprefs = cmd.add_element("preferences")
2114
            for pref_name, pref_value in preferences.items():
2115
                _xmlpref = _xmlprefs.add_element("preference")
2116
                _xmlpref.add_element("scanner_name", pref_name)
2117
                _xmlpref.add_element("value", str(pref_value))
2118
2119
        return self._send_xml_command(cmd)
2120
2121
    def create_container_task(
2122
        self, name: str, *, comment: Optional[str] = None
2123
    ) -> Any:
2124
        """Create a new container task
2125
2126
        A container task is a "meta" task to import and view reports from other
2127
        systems.
2128
2129
        Arguments:
2130
            name: Name of the task
2131
            comment: Comment for the task
2132
2133
        Returns:
2134
            The response. See :py:meth:`send_command` for details.
2135
        """
2136
        if not name:
2137
            raise RequiredArgument(
2138
                function=self.create_container_task.__name__, argument='name'
2139
            )
2140
2141
        cmd = XmlCommand("create_task")
2142
        cmd.add_element("name", name)
2143
        cmd.add_element("target", attrs={"id": "0"})
2144
2145
        if comment:
2146
            cmd.add_element("comment", comment)
2147
2148
        return self._send_xml_command(cmd)
2149
2150
    def clone_task(self, task_id: str) -> Any:
2151
        """Clone an existing task
2152
2153
        Arguments:
2154
            task_id: UUID of existing task to clone from
2155
2156
        Returns:
2157
            The response. See :py:meth:`send_command` for details.
2158
        """
2159
        if not task_id:
2160
            raise RequiredArgument(
2161
                function=self.clone_task.__name__, argument='task_id'
2162
            )
2163
2164
        cmd = XmlCommand("create_task")
2165
        cmd.add_element("copy", task_id)
2166
        return self._send_xml_command(cmd)
2167
2168
    def create_user(
2169
        self,
2170
        name: str,
2171
        *,
2172
        password: Optional[str] = None,
2173
        hosts: Optional[List[str]] = None,
2174
        hosts_allow: Optional[bool] = False,
2175
        ifaces: Optional[List[str]] = None,
2176
        ifaces_allow: Optional[bool] = False,
2177
        role_ids: Optional[List[str]] = None
2178
    ) -> Any:
2179
        """Create a new user
2180
2181
        Arguments:
2182
            name: Name of the user
2183
            password: Password of the user
2184
            hosts: A list of host addresses (IPs, DNS names)
2185
            hosts_allow: If True allow only access to passed hosts otherwise
2186
                deny access. Default is False for deny hosts.
2187
            ifaces: A list of interface names
2188
            ifaces_allow: If True allow only access to passed interfaces
2189
                otherwise deny access. Default is False for deny interfaces.
2190
            role_ids: A list of role UUIDs for the user
2191
2192
        Returns:
2193
            The response. See :py:meth:`send_command` for details.
2194
        """
2195
        if not name:
2196
            raise RequiredArgument(
2197
                function=self.create_user.__name__, argument='name'
2198
            )
2199
2200
        cmd = XmlCommand("create_user")
2201
        cmd.add_element("name", name)
2202
2203
        if password:
2204
            cmd.add_element("password", password)
2205
2206
        if hosts:
2207
            cmd.add_element(
2208
                "hosts",
2209
                _to_comma_list(hosts),
2210
                attrs={"allow": _to_bool(hosts_allow)},
2211
            )
2212
2213
        if ifaces:
2214
            cmd.add_element(
2215
                "ifaces",
2216
                _to_comma_list(ifaces),
2217
                attrs={"allow": _to_bool(ifaces_allow)},
2218
            )
2219
2220
        if role_ids:
2221
            for role in role_ids:
2222
                cmd.add_element("role", attrs={"id": role})
2223
2224
        return self._send_xml_command(cmd)
2225
2226
    def clone_user(self, user_id: str) -> Any:
2227
        """Clone an existing user
2228
2229
        Arguments:
2230
            user_id: UUID of existing user to clone from
2231
2232
        Returns:
2233
            The response. See :py:meth:`send_command` for details.
2234
        """
2235
        if not user_id:
2236
            raise RequiredArgument(
2237
                function=self.clone_user.__name__, argument='user_id'
2238
            )
2239
2240
        cmd = XmlCommand("create_user")
2241
        cmd.add_element("copy", user_id)
2242
        return self._send_xml_command(cmd)
2243
2244
    def delete_agent(
2245
        self, agent_id: str, *, ultimate: Optional[bool] = False
2246
    ) -> Any:
2247
        """Deletes an existing agent
2248
2249
        Arguments:
2250
            agent_id: UUID of the agent to be deleted.
2251
            ultimate: Whether to remove entirely, or to the trashcan.
2252
        """
2253
        if not agent_id:
2254
            raise RequiredArgument(
2255
                function=self.delete_agent.__name__, argument='agent_id'
2256
            )
2257
2258
        cmd = XmlCommand("delete_agent")
2259
        cmd.set_attribute("agent_id", agent_id)
2260
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2261
2262
        return self._send_xml_command(cmd)
2263
2264
    def delete_alert(
2265
        self, alert_id: str, *, ultimate: Optional[bool] = False
2266
    ) -> Any:
2267
        """Deletes an existing alert
2268
2269
        Arguments:
2270
            alert_id: UUID of the alert to be deleted.
2271
            ultimate: Whether to remove entirely, or to the trashcan.
2272
        """
2273
        if not alert_id:
2274
            raise RequiredArgument(
2275
                function=self.delete_alert.__name__, argument='alert_id'
2276
            )
2277
2278
        cmd = XmlCommand("delete_alert")
2279
        cmd.set_attribute("alert_id", alert_id)
2280
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2281
2282
        return self._send_xml_command(cmd)
2283
2284
    def delete_asset(
2285
        self, *, asset_id: Optional[str] = None, report_id: Optional[str] = None
2286
    ) -> Any:
2287
        """Deletes an existing asset
2288
2289
        Arguments:
2290
            asset_id: UUID of the single asset to delete.
2291
            report_id: UUID of report from which to get all
2292
                assets to delete.
2293
        """
2294
        if not asset_id and not report_id:
2295
            raise RequiredArgument(
2296
                function=self.delete_asset.__name__,
2297
                argument='asset_id or report_id',
2298
            )
2299
2300
        cmd = XmlCommand("delete_asset")
2301
        if asset_id:
2302
            cmd.set_attribute("asset_id", asset_id)
2303
        else:
2304
            cmd.set_attribute("report_id", report_id)
2305
2306
        return self._send_xml_command(cmd)
2307
2308
    def delete_config(
2309
        self, config_id: str, *, ultimate: Optional[bool] = False
2310
    ) -> Any:
2311
        """Deletes an existing config
2312
2313
        Arguments:
2314
            config_id: UUID of the config to be deleted.
2315
            ultimate: Whether to remove entirely, or to the trashcan.
2316
        """
2317
        if not config_id:
2318
            raise RequiredArgument(
2319
                function=self.delete_config.__name__, argument='config_id'
2320
            )
2321
2322
        cmd = XmlCommand("delete_config")
2323
        cmd.set_attribute("config_id", config_id)
2324
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2325
2326
        return self._send_xml_command(cmd)
2327
2328
    def delete_credential(
2329
        self, credential_id: str, *, ultimate: Optional[bool] = False
2330
    ) -> Any:
2331
        """Deletes an existing credential
2332
2333
        Arguments:
2334
            credential_id: UUID of the credential to be deleted.
2335
            ultimate: Whether to remove entirely, or to the trashcan.
2336
        """
2337
        if not credential_id:
2338
            raise RequiredArgument(
2339
                function=self.delete_credential.__name__,
2340
                argument='credential_id',
2341
            )
2342
2343
        cmd = XmlCommand("delete_credential")
2344
        cmd.set_attribute("credential_id", credential_id)
2345
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2346
2347
        return self._send_xml_command(cmd)
2348
2349
    def delete_filter(
2350
        self, filter_id: str, *, ultimate: Optional[bool] = False
2351
    ) -> Any:
2352
        """Deletes an existing filter
2353
2354
        Arguments:
2355
            filter_id: UUID of the filter to be deleted.
2356
            ultimate: Whether to remove entirely, or to the trashcan.
2357
        """
2358
        if not filter_id:
2359
            raise RequiredArgument(
2360
                function=self.delete_filter.__name__, argument='filter_id'
2361
            )
2362
2363
        cmd = XmlCommand("delete_filter")
2364
        cmd.set_attribute("filter_id", filter_id)
2365
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2366
2367
        return self._send_xml_command(cmd)
2368
2369
    def delete_group(
2370
        self, group_id: str, *, ultimate: Optional[bool] = False
2371
    ) -> Any:
2372
        """Deletes an existing group
2373
2374
        Arguments:
2375
            group_id: UUID of the group to be deleted.
2376
            ultimate: Whether to remove entirely, or to the trashcan.
2377
        """
2378
        if not group_id:
2379
            raise RequiredArgument(
2380
                function=self.delete_group.__name__, argument='group_id'
2381
            )
2382
2383
        cmd = XmlCommand("delete_group")
2384
        cmd.set_attribute("group_id", group_id)
2385
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2386
2387
        return self._send_xml_command(cmd)
2388
2389
    def delete_note(
2390
        self, note_id: str, *, ultimate: Optional[bool] = False
2391
    ) -> Any:
2392
        """Deletes an existing note
2393
2394
        Arguments:
2395
            note_id: UUID of the note to be deleted.
2396
            ultimate: Whether to remove entirely,or to the trashcan.
2397
        """
2398
        if not note_id:
2399
            raise RequiredArgument(
2400
                function=self.delete_note.__name__, argument='note_id'
2401
            )
2402
2403
        cmd = XmlCommand("delete_note")
2404
        cmd.set_attribute("note_id", note_id)
2405
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2406
2407
        return self._send_xml_command(cmd)
2408
2409
    def delete_override(
2410
        self, override_id: str, *, ultimate: Optional[bool] = False
2411
    ) -> Any:
2412
        """Deletes an existing override
2413
2414
        Arguments:
2415
            override_id: UUID of the override to be deleted.
2416
            ultimate: Whether to remove entirely, or to the trashcan.
2417
        """
2418
        if not override_id:
2419
            raise RequiredArgument(
2420
                function=self.delete_override.__name__, argument='override_id'
2421
            )
2422
2423
        cmd = XmlCommand("delete_override")
2424
        cmd.set_attribute("override_id", override_id)
2425
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2426
2427
        return self._send_xml_command(cmd)
2428
2429
    def delete_permission(
2430
        self, permission_id: str, *, ultimate: Optional[bool] = False
2431
    ) -> Any:
2432
        """Deletes an existing permission
2433
2434
        Arguments:
2435
            permission_id: UUID of the permission to be deleted.
2436
            ultimate: Whether to remove entirely, or to the trashcan.
2437
        """
2438
        if not permission_id:
2439
            raise RequiredArgument(
2440
                function=self.delete_permission.__name__,
2441
                argument='permission_id',
2442
            )
2443
2444
        cmd = XmlCommand("delete_permission")
2445
        cmd.set_attribute("permission_id", permission_id)
2446
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2447
2448
        return self._send_xml_command(cmd)
2449
2450
    def delete_port_list(
2451
        self, port_list_id: str, *, ultimate: Optional[bool] = False
2452
    ) -> Any:
2453
        """Deletes an existing port list
2454
2455
        Arguments:
2456
            port_list_id: UUID of the port list to be deleted.
2457
            ultimate: Whether to remove entirely, or to the trashcan.
2458
        """
2459
        if not port_list_id:
2460
            raise RequiredArgument(
2461
                function=self.delete_port_list.__name__, argument='port_list_id'
2462
            )
2463
2464
        cmd = XmlCommand("delete_port_list")
2465
        cmd.set_attribute("port_list_id", port_list_id)
2466
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2467
2468
        return self._send_xml_command(cmd)
2469
2470
    def delete_port_range(self, port_range_id: str) -> Any:
2471
        """Deletes an existing port range
2472
2473
        Arguments:
2474
            port_range_id: UUID of the port range to be deleted.
2475
        """
2476
        if not port_range_id:
2477
            raise RequiredArgument(
2478
                function=self.delete_port_range.__name__,
2479
                argument='port_range_id',
2480
            )
2481
2482
        cmd = XmlCommand("delete_port_range")
2483
        cmd.set_attribute("port_range_id", port_range_id)
2484
2485
        return self._send_xml_command(cmd)
2486
2487
    def delete_report(self, report_id: str) -> Any:
2488
        """Deletes an existing report
2489
2490
        Arguments:
2491
            report_id: UUID of the report to be deleted.
2492
        """
2493
        if not report_id:
2494
            raise RequiredArgument(
2495
                function=self.delete_report.__name__, argument='report_id'
2496
            )
2497
2498
        cmd = XmlCommand("delete_report")
2499
        cmd.set_attribute("report_id", report_id)
2500
2501
        return self._send_xml_command(cmd)
2502
2503
    def delete_report_format(
2504
        self, report_format_id: str, *, ultimate: Optional[bool] = False
2505
    ) -> Any:
2506
        """Deletes an existing report format
2507
2508
        Arguments:
2509
            report_format_id: UUID of the report format to be deleted.
2510
            ultimate: Whether to remove entirely, or to the trashcan.
2511
        """
2512
        if not report_format_id:
2513
            raise RequiredArgument(
2514
                function=self.delete_report_format.__name__,
2515
                argument='report_format_id',
2516
            )
2517
2518
        cmd = XmlCommand("delete_report_format")
2519
        cmd.set_attribute("report_format_id", report_format_id)
2520
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2521
2522
        return self._send_xml_command(cmd)
2523
2524
    def delete_role(
2525
        self, role_id: str, *, ultimate: Optional[bool] = False
2526
    ) -> Any:
2527
        """Deletes an existing role
2528
2529
        Arguments:
2530
            role_id: UUID of the role to be deleted.
2531
            ultimate: Whether to remove entirely, or to the trashcan.
2532
        """
2533
        if not role_id:
2534
            raise RequiredArgument(
2535
                function=self.delete_role.__name__, argument='role_id'
2536
            )
2537
2538
        cmd = XmlCommand("delete_role")
2539
        cmd.set_attribute("role_id", role_id)
2540
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2541
2542
        return self._send_xml_command(cmd)
2543
2544
    def delete_scanner(
2545
        self, scanner_id: str, *, ultimate: Optional[bool] = False
2546
    ) -> Any:
2547
        """Deletes an existing scanner
2548
2549
        Arguments:
2550
            scanner_id: UUID of the scanner to be deleted.
2551
            ultimate: Whether to remove entirely, or to the trashcan.
2552
        """
2553
        if not scanner_id:
2554
            raise RequiredArgument(
2555
                function=self.delete_scanner.__name__, argument='scanner_id'
2556
            )
2557
2558
        cmd = XmlCommand("delete_scanner")
2559
        cmd.set_attribute("scanner_id", scanner_id)
2560
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2561
2562
        return self._send_xml_command(cmd)
2563
2564
    def delete_schedule(
2565
        self, schedule_id: str, *, ultimate: Optional[bool] = False
2566
    ) -> Any:
2567
        """Deletes an existing schedule
2568
2569
        Arguments:
2570
            schedule_id: UUID of the schedule to be deleted.
2571
            ultimate: Whether to remove entirely, or to the trashcan.
2572
        """
2573
        if not schedule_id:
2574
            raise RequiredArgument(
2575
                function=self.delete_schedule.__name__, argument='schedule_id'
2576
            )
2577
2578
        cmd = XmlCommand("delete_schedule")
2579
        cmd.set_attribute("schedule_id", schedule_id)
2580
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2581
2582
        return self._send_xml_command(cmd)
2583
2584
    def delete_tag(
2585
        self, tag_id: str, *, ultimate: Optional[bool] = False
2586
    ) -> Any:
2587
        """Deletes an existing tag
2588
2589
        Arguments:
2590
            tag_id: UUID of the tag to be deleted.
2591
            ultimate: Whether to remove entirely, or to the trashcan.
2592
        """
2593
        if not tag_id:
2594
            raise RequiredArgument(
2595
                function=self.delete_tag.__name__, argument='tag_id'
2596
            )
2597
2598
        cmd = XmlCommand("delete_tag")
2599
        cmd.set_attribute("tag_id", tag_id)
2600
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2601
2602
        return self._send_xml_command(cmd)
2603
2604
    def delete_target(
2605
        self, target_id: str, *, ultimate: Optional[bool] = False
2606
    ) -> Any:
2607
        """Deletes an existing target
2608
2609
        Arguments:
2610
            target_id: UUID of the target to be deleted.
2611
            ultimate: Whether to remove entirely, or to the trashcan.
2612
        """
2613
        if not target_id:
2614
            raise RequiredArgument(
2615
                function=self.delete_target.__name__, argument='target_id'
2616
            )
2617
2618
        cmd = XmlCommand("delete_target")
2619
        cmd.set_attribute("target_id", target_id)
2620
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2621
2622
        return self._send_xml_command(cmd)
2623
2624
    def delete_task(
2625
        self, task_id: str, *, ultimate: Optional[bool] = False
2626
    ) -> Any:
2627
        """Deletes an existing task
2628
2629
        Arguments:
2630
            task_id: UUID of the task to be deleted.
2631
            ultimate: Whether to remove entirely, or to the trashcan.
2632
        """
2633
        if not task_id:
2634
            raise RequiredArgument(
2635
                function=self.delete_task.__name__, argument='task_id'
2636
            )
2637
2638
        cmd = XmlCommand("delete_task")
2639
        cmd.set_attribute("task_id", task_id)
2640
        cmd.set_attribute("ultimate", _to_bool(ultimate))
2641
2642
        return self._send_xml_command(cmd)
2643
2644
    def delete_user(
2645
        self,
2646
        user_id: str = None,
2647
        *,
2648
        name: Optional[str] = None,
2649
        inheritor_id: Optional[str] = None,
2650
        inheritor_name: Optional[str] = None
2651
    ) -> Any:
2652
        """Deletes an existing user
2653
2654
        Either user_id or name must be passed.
2655
2656
        Arguments:
2657
            user_id: UUID of the task to be deleted.
2658
            name: The name of the user to be deleted.
2659
            inheritor_id: The ID of the inheriting user or "self". Overrides
2660
                inheritor_name.
2661
            inheritor_name: The name of the inheriting user.
2662
2663
        """
2664
        if not user_id and not name:
2665
            raise RequiredArgument(
2666
                function=self.delete_user.__name__, argument='user_id or name'
2667
            )
2668
2669
        cmd = XmlCommand("delete_user")
2670
2671
        if user_id:
2672
            cmd.set_attribute("user_id", user_id)
2673
2674
        if name:
2675
            cmd.set_attribute("name", name)
2676
2677
        if inheritor_id:
2678
            cmd.set_attribute("inheritor_id", inheritor_id)
2679
2680
        if inheritor_name:
2681
            cmd.set_attribute("inheritor_name", inheritor_name)
2682
2683
        return self._send_xml_command(cmd)
2684
2685
    def describe_auth(self) -> Any:
2686
        """Describe authentication methods
2687
2688
        Returns a list of all used authentication methods if such a list is
2689
        available.
2690
2691
        Returns:
2692
            The response. See :py:meth:`send_command` for details.
2693
        """
2694
        return self._send_xml_command(XmlCommand("describe_auth"))
2695
2696
    def empty_trashcan(self) -> Any:
2697
        """Empty the trashcan
2698
2699
        Remove all entities from the trashcan. **Attention:** this command can
2700
        not be reverted
2701
2702
        Returns:
2703
            The response. See :py:meth:`send_command` for details.
2704
        """
2705
        return self._send_xml_command(XmlCommand("empty_trashcan"))
2706
2707
    def get_agents(
2708
        self,
2709
        *,
2710
        filter: Optional[str] = None,
2711
        filter_id: Optional[str] = None,
2712
        trash: Optional[bool] = None,
2713
        details: Optional[bool] = None,
2714
        format: Optional[str] = None
2715
    ) -> Any:
2716
        """Request a list of agents
2717
2718
        Arguments:
2719
            filter: Filter term to use for the query
2720
            filter_id: UUID of an existing filter to use for the query
2721
            trash: True to request the agents in the trashcan
2722
            details: Whether to include agents packageinformation when no format
2723
                was provided
2724
            format: One of "installer", "howto_install" or "howto_use"
2725
2726
        Returns:
2727
            The response. See :py:meth:`send_command` for details.
2728
        """
2729
        cmd = XmlCommand("get_agents")
2730
2731
        _add_filter(cmd, filter, filter_id)
2732
2733
        if trash is not None:
2734
            cmd.set_attribute("trash", _to_bool(trash))
2735
2736
        if details is not None:
2737
            cmd.set_attribute("details", _to_bool(details))
2738
2739
        if format:
2740
            if format not in ("installer", "howto_install", "howto_use"):
2741
                raise InvalidArgument(
2742
                    "installer argument needs to be one of installer, "
2743
                    "howto_install or howto_use"
2744
                )
2745
2746
            cmd.set_attribute("format", format)
2747
2748
        return self._send_xml_command(cmd)
2749
2750
    def get_agent(self, agent_id: str) -> Any:
2751
        """Request a single agent
2752
2753
        Arguments:
2754
            agent_id: UUID of an existing agent
2755
2756
        Returns:
2757
            The response. See :py:meth:`send_command` for details.
2758
        """
2759
        if not agent_id:
2760
            raise RequiredArgument(
2761
                function=self.get_agent.__name__, argument='agent_id'
2762
            )
2763
2764
        cmd = XmlCommand("get_agents")
2765
        cmd.set_attribute("agent_id", agent_id)
2766
2767
        # for single entity always request all details
2768
        cmd.set_attribute("details", "1")
2769
        return self._send_xml_command(cmd)
2770
2771
    def get_aggregates(self, resource_type: EntityType, **kwargs) -> Any:
2772
        """Request aggregated information on a resource type
2773
2774
        Additional arguments can be set via the kwargs parameter, but are not
2775
        yet validated.
2776
2777
        Arguments:
2778
           resource_type: The entity type to gather data from
2779
2780
        Returns:
2781
            The response. See :py:meth:`send_command` for details.
2782
        """
2783
        if not resource_type:
2784
            raise RequiredArgument(
2785
                function=self.get_aggregates.__name__, argument='resource_type'
2786
            )
2787
2788
        if not isinstance(resource_type, self.types.EntityType):
2789
            raise InvalidArgumentType(
2790
                function=self.get_aggregates.__name__,
2791
                argument='resource_type',
2792
                arg_type=self.types.EntityType.__name__,
2793
            )
2794
2795
        cmd = XmlCommand("get_aggregates")
2796
2797
        cmd.set_attribute("type", resource_type.value)
2798
2799
        cmd.set_attributes(kwargs)
2800
        return self._send_xml_command(cmd)
2801
2802
    def get_alerts(
2803
        self,
2804
        *,
2805
        filter: Optional[str] = None,
2806
        filter_id: Optional[str] = None,
2807
        trash: Optional[bool] = None,
2808
        tasks: Optional[bool] = None
2809
    ) -> Any:
2810
        """Request a list of alerts
2811
2812
        Arguments:
2813
            filter: Filter term to use for the query
2814
            filter_id: UUID of an existing filter to use for the query
2815
            trash: True to request the alerts in the trashcan
2816
            tasks: Whether to include the tasks using the alerts
2817
        Returns:
2818
            The response. See :py:meth:`send_command` for details.
2819
        """
2820
        cmd = XmlCommand("get_alerts")
2821
2822
        _add_filter(cmd, filter, filter_id)
2823
2824
        if trash is not None:
2825
            cmd.set_attribute("trash", _to_bool(trash))
2826
2827
        if tasks is not None:
2828
            cmd.set_attribute("tasks", _to_bool(tasks))
2829
2830
        return self._send_xml_command(cmd)
2831
2832
    def get_alert(self, alert_id: str, *, tasks: Optional[bool] = None) -> Any:
2833
        """Request a single alert
2834
2835
        Arguments:
2836
            alert_id: UUID of an existing alert
2837
2838
        Returns:
2839
            The response. See :py:meth:`send_command` for details.
2840
        """
2841
        cmd = XmlCommand("get_alerts")
2842
2843
        if not alert_id:
2844
            raise RequiredArgument(
2845
                function=self.get_alert.__name__, argument='alert_id'
2846
            )
2847
2848
        cmd.set_attribute("alert_id", alert_id)
2849
2850
        if tasks is not None:
2851
            cmd.set_attribute("tasks", _to_bool(tasks))
2852
2853
        return self._send_xml_command(cmd)
2854
2855
    def get_assets(
2856
        self,
2857
        asset_type: AssetType,
2858
        *,
2859
        filter: Optional[str] = None,
2860
        filter_id: Optional[str] = None
2861
    ) -> Any:
2862
        """Request a list of assets
2863
2864
        Arguments:
2865
            asset_type: Either 'os' or 'host'
2866
            filter: Filter term to use for the query
2867
            filter_id: UUID of an existing filter to use for the query
2868
2869
        Returns:
2870
            The response. See :py:meth:`send_command` for details.
2871
        """
2872
        if not isinstance(asset_type, AssetType):
2873
            raise InvalidArgumentType(
2874
                function=self.get_assets.__name__,
2875
                argument='asset_type',
2876
                arg_type=AssetType.__name__,
2877
            )
2878
2879
        cmd = XmlCommand("get_assets")
2880
2881
        cmd.set_attribute("type", asset_type.value)
2882
2883
        _add_filter(cmd, filter, filter_id)
2884
2885
        return self._send_xml_command(cmd)
2886
2887
    def get_asset(self, asset_id: str, asset_type: AssetType) -> Any:
2888
        """Request a single asset
2889
2890
        Arguments:
2891
            asset_id: UUID of an existing asset
2892
            asset_type: Either 'os' or 'host'
2893
2894
        Returns:
2895
            The response. See :py:meth:`send_command` for details.
2896
        """
2897
        cmd = XmlCommand("get_assets")
2898
2899
        if not isinstance(asset_type, AssetType):
2900
            raise InvalidArgumentType(
2901
                function=self.get_asset.__name__,
2902
                argument='asset_type',
2903
                arg_type=AssetType.__name__,
2904
            )
2905
2906
        if not asset_id:
2907
            raise RequiredArgument(
2908
                function=self.get_asset.__name__, argument='asset_id'
2909
            )
2910
2911
        cmd.set_attribute("asset_id", asset_id)
2912
        cmd.set_attribute("type", asset_type.value)
2913
2914
        return self._send_xml_command(cmd)
2915
2916 View Code Duplication
    def get_credentials(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2917
        self,
2918
        *,
2919
        filter: Optional[str] = None,
2920
        filter_id: Optional[str] = None,
2921
        scanners: Optional[bool] = None,
2922
        trash: Optional[bool] = None,
2923
        targets: Optional[bool] = None
2924
    ) -> Any:
2925
        """Request a list of credentials
2926
2927
        Arguments:
2928
            filter: Filter term to use for the query
2929
            filter_id: UUID of an existing filter to use for the query
2930
            scanners: Whether to include a list of scanners using the
2931
                credentials
2932
            trash: Whether to get the trashcan credentials instead
2933
            targets: Whether to include a list of targets using the credentials
2934
2935
        Returns:
2936
            The response. See :py:meth:`send_command` for details.
2937
        """
2938
        cmd = XmlCommand("get_credentials")
2939
2940
        _add_filter(cmd, filter, filter_id)
2941
2942
        if scanners is not None:
2943
            cmd.set_attribute("scanners", _to_bool(scanners))
2944
2945
        if trash is not None:
2946
            cmd.set_attribute("trash", _to_bool(trash))
2947
2948
        if targets is not None:
2949
            cmd.set_attribute("targets", _to_bool(targets))
2950
2951
        return self._send_xml_command(cmd)
2952
2953
    def get_credential(
2954
        self,
2955
        credential_id: str,
2956
        *,
2957
        scanners: Optional[bool] = None,
2958
        targets: Optional[bool] = None,
2959
        credential_format: Optional[CredentialFormat] = None
2960
    ) -> Any:
2961
        """Request a single credential
2962
2963
        Arguments:
2964
            credential_id: UUID of an existing credential
2965
            scanners: Whether to include a list of scanners using the
2966
                credentials
2967
            targets: Whether to include a list of targets using the credentials
2968
            credential_format: One of "key", "rpm", "deb", "exe" or "pem"
2969
2970
        Returns:
2971
            The response. See :py:meth:`send_command` for details.
2972
        """
2973
        if not credential_id:
2974
            raise RequiredArgument(
2975
                function=self.get_credential.__name__, argument='credential_id'
2976
            )
2977
2978
        cmd = XmlCommand("get_credentials")
2979
        cmd.set_attribute("credential_id", credential_id)
2980
2981
        if credential_format:
2982
            if not isinstance(credential_format, CredentialFormat):
2983
                raise InvalidArgumentType(
2984
                    function=self.get_credential.__name__,
2985
                    argument='credential_format',
2986
                    arg_type=CredentialFormat.__name__,
2987
                )
2988
2989
            cmd.set_attribute("format", credential_format.value)
2990
2991
        if scanners is not None:
2992
            cmd.set_attribute("scanners", _to_bool(scanners))
2993
2994
        if targets is not None:
2995
            cmd.set_attribute("targets", _to_bool(targets))
2996
2997
        return self._send_xml_command(cmd)
2998
2999 View Code Duplication
    def get_configs(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3000
        self,
3001
        *,
3002
        filter: Optional[str] = None,
3003
        filter_id: Optional[str] = None,
3004
        trash: Optional[bool] = None,
3005
        details: Optional[bool] = None,
3006
        families: Optional[bool] = None,
3007
        preferences: Optional[bool] = None,
3008
        tasks: Optional[bool] = None
3009
    ) -> Any:
3010
        """Request a list of scan configs
3011
3012
        Arguments:
3013
            filter: Filter term to use for the query
3014
            filter_id: UUID of an existing filter to use for the query
3015
            trash: Whether to get the trashcan scan configs instead
3016
            details: Whether to get config families, preferences, nvt selectors
3017
                and tasks.
3018
            families: Whether to include the families if no details are
3019
                requested
3020
            preferences: Whether to include the preferences if no details are
3021
                requested
3022
            tasks: Whether to get tasks using this config
3023
3024
        Returns:
3025
            The response. See :py:meth:`send_command` for details.
3026
        """
3027
        cmd = XmlCommand("get_configs")
3028
3029
        _add_filter(cmd, filter, filter_id)
3030
3031
        if trash is not None:
3032
            cmd.set_attribute("trash", _to_bool(trash))
3033
3034
        if details is not None:
3035
            cmd.set_attribute("details", _to_bool(details))
3036
3037
        if families is not None:
3038
            cmd.set_attribute("families", _to_bool(families))
3039
3040
        if preferences is not None:
3041
            cmd.set_attribute("preferences", _to_bool(preferences))
3042
3043
        if tasks is not None:
3044
            cmd.set_attribute("tasks", _to_bool(tasks))
3045
3046
        return self._send_xml_command(cmd)
3047
3048
    def get_config(
3049
        self, config_id: str, *, tasks: Optional[bool] = None
3050
    ) -> Any:
3051
        """Request a single scan config
3052
3053
        Arguments:
3054
            config_id: UUID of an existing scan config
3055
            tasks: Whether to get tasks using this config
3056
3057
        Returns:
3058
            The response. See :py:meth:`send_command` for details.
3059
        """
3060
        cmd = XmlCommand("get_configs")
3061
3062
        if not config_id:
3063
            raise RequiredArgument(
3064
                function=self.get_config.__name__, argument='config_id'
3065
            )
3066
3067
        cmd.set_attribute("config_id", config_id)
3068
3069
        if tasks is not None:
3070
            cmd.set_attribute("tasks", _to_bool(tasks))
3071
3072
        # for single entity always request all details
3073
        cmd.set_attribute("details", "1")
3074
        return self._send_xml_command(cmd)
3075
3076
    def get_feeds(self) -> Any:
3077
        """Request the list of feeds
3078
3079
        Returns:
3080
            The response. See :py:meth:`send_command` for details.
3081
        """
3082
        return self._send_xml_command(XmlCommand("get_feeds"))
3083
3084
    def get_feed(self, feed_type: Optional[FeedType]) -> Any:
3085
        """Request a single feed
3086
3087
        Arguments:
3088
            feed_type: Type of single feed to get: NVT, CERT or SCAP
3089
3090
        Returns:
3091
            The response. See :py:meth:`send_command` for details.
3092
        """
3093
        if not feed_type:
3094
            raise RequiredArgument(
3095
                function=self.get_feed.__name__, argument='feed_type'
3096
            )
3097
3098
        if not isinstance(feed_type, FeedType):
3099
            raise InvalidArgumentType(
3100
                function=self.get_feed.__name__,
3101
                argument='feed_type',
3102
                arg_type=FeedType.__name__,
3103
            )
3104
3105
        cmd = XmlCommand("get_feeds")
3106
        cmd.set_attribute("type", feed_type.value)
3107
3108
        return self._send_xml_command(cmd)
3109
3110
    def get_filters(
3111
        self,
3112
        *,
3113
        filter: Optional[str] = None,
3114
        filter_id: Optional[str] = None,
3115
        trash: Optional[bool] = None,
3116
        alerts: Optional[bool] = None
3117
    ) -> Any:
3118
        """Request a list of filters
3119
3120
        Arguments:
3121
            filter: Filter term to use for the query
3122
            filter_id: UUID of an existing filter to use for the query
3123
            trash: Whether to get the trashcan filters instead
3124
            alerts: Whether to include list of alerts that use the filter.
3125
3126
        Returns:
3127
            The response. See :py:meth:`send_command` for details.
3128
        """
3129
        cmd = XmlCommand("get_filters")
3130
3131
        _add_filter(cmd, filter, filter_id)
3132
3133
        if trash is not None:
3134
            cmd.set_attribute("trash", _to_bool(trash))
3135
3136
        if alerts is not None:
3137
            cmd.set_attribute("alerts", _to_bool(alerts))
3138
3139
        return self._send_xml_command(cmd)
3140
3141
    def get_filter(
3142
        self, filter_id: str, *, alerts: Optional[bool] = None
3143
    ) -> Any:
3144
        """Request a single filter
3145
3146
        Arguments:
3147
            filter_id: UUID of an existing filter
3148
            alerts: Whether to include list of alerts that use the filter.
3149
3150
        Returns:
3151
            The response. See :py:meth:`send_command` for details.
3152
        """
3153
        cmd = XmlCommand("get_filters")
3154
3155
        if not filter_id:
3156
            raise RequiredArgument(
3157
                function=self.get_filter.__name__, argument='filter_id'
3158
            )
3159
3160
        cmd.set_attribute("filter_id", filter_id)
3161
3162
        if alerts is not None:
3163
            cmd.set_attribute("alerts", _to_bool(alerts))
3164
3165
        return self._send_xml_command(cmd)
3166
3167 View Code Duplication
    def get_groups(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3168
        self,
3169
        *,
3170
        filter: Optional[str] = None,
3171
        filter_id: Optional[str] = None,
3172
        trash: Optional[bool] = None
3173
    ) -> Any:
3174
        """Request a list of groups
3175
3176
        Arguments:
3177
            filter: Filter term to use for the query
3178
            filter_id: UUID of an existing filter to use for the query
3179
            trash: Whether to get the trashcan groups instead
3180
3181
        Returns:
3182
            The response. See :py:meth:`send_command` for details.
3183
        """
3184
        cmd = XmlCommand("get_groups")
3185
3186
        _add_filter(cmd, filter, filter_id)
3187
3188
        if trash is not None:
3189
            cmd.set_attribute("trash", _to_bool(trash))
3190
3191
        return self._send_xml_command(cmd)
3192
3193
    def get_group(self, group_id: str) -> Any:
3194
        """Request a single group
3195
3196
        Arguments:
3197
            group_id: UUID of an existing group
3198
3199
        Returns:
3200
            The response. See :py:meth:`send_command` for details.
3201
        """
3202
        cmd = XmlCommand("get_groups")
3203
3204
        if not group_id:
3205
            raise RequiredArgument(
3206
                function=self.get_group.__name__, argument='group_id'
3207
            )
3208
3209
        cmd.set_attribute("group_id", group_id)
3210
        return self._send_xml_command(cmd)
3211
3212
    def get_info_list(
3213
        self,
3214
        info_type: InfoType,
3215
        *,
3216
        filter: Optional[str] = None,
3217
        filter_id: Optional[str] = None,
3218
        name: Optional[str] = None,
3219
        details: Optional[bool] = None
3220
    ) -> Any:
3221
        """Request a list of security information
3222
3223
        Arguments:
3224
            info_type: Type must be either CERT_BUND_ADV, CPE, CVE,
3225
                DFN_CERT_ADV, OVALDEF, NVT or ALLINFO
3226
            filter: Filter term to use for the query
3227
            filter_id: UUID of an existing filter to use for the query
3228
            name: Name or identifier of the requested information
3229
            details: Whether to include information about references to this
3230
                information
3231
3232
        Returns:
3233
            The response. See :py:meth:`send_command` for details.
3234
        """
3235
        if not info_type:
3236
            raise RequiredArgument(
3237
                function=self.get_info_list.__name__, argument='info_type'
3238
            )
3239
3240
        if not isinstance(info_type, InfoType):
3241
            raise InvalidArgumentType(
3242
                function=self.get_info_list.__name__,
3243
                argument='info_type',
3244
                arg_type=InfoType.__name__,
3245
            )
3246
3247
        cmd = XmlCommand("get_info")
3248
3249
        cmd.set_attribute("type", info_type.value)
3250
3251
        _add_filter(cmd, filter, filter_id)
3252
3253
        if name:
3254
            cmd.set_attribute("name", name)
3255
3256
        if details is not None:
3257
            cmd.set_attribute("details", _to_bool(details))
3258
3259
        return self._send_xml_command(cmd)
3260
3261 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...
3262
        """Request a single secinfo
3263
3264
        Arguments:
3265
            info_id: UUID of an existing secinfo
3266
            info_type: Type must be either CERT_BUND_ADV, CPE, CVE,
3267
                DFN_CERT_ADV, OVALDEF, NVT or ALLINFO
3268
3269
        Returns:
3270
            The response. See :py:meth:`send_command` for details.
3271
        """
3272
        cmd = XmlCommand("get_info")
3273
3274
        if not info_type:
3275
            raise RequiredArgument(
3276
                function=self.get_info.__name__, argument='info_type'
3277
            )
3278
3279
        if not isinstance(info_type, InfoType):
3280
            raise InvalidArgumentType(
3281
                function=self.get_info.__name__,
3282
                argument='info_type',
3283
                arg_type=InfoType.__name__,
3284
            )
3285
3286
        if not info_id:
3287
            raise RequiredArgument(
3288
                function=self.get_info.__name__, argument='info_id'
3289
            )
3290
3291
        cmd.set_attribute("info_id", info_id)
3292
3293
        cmd.set_attribute("type", info_type.value)
3294
3295
        # for single entity always request all details
3296
        cmd.set_attribute("details", "1")
3297
3298
        return self._send_xml_command(cmd)
3299
3300 View Code Duplication
    def get_notes(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3301
        self,
3302
        *,
3303
        filter: Optional[str] = None,
3304
        filter_id: Optional[str] = None,
3305
        details: Optional[bool] = None,
3306
        result: Optional[bool] = None
3307
    ) -> Any:
3308
        """Request a list of notes
3309
3310
        Arguments:
3311
            filter: Filter term to use for the query
3312
            filter_id: UUID of an existing filter to use for the query
3313
            details: Add info about connected results and tasks
3314
            result: Return the details of possible connected results.
3315
3316
        Returns:
3317
            The response. See :py:meth:`send_command` for details.
3318
        """
3319
        cmd = XmlCommand("get_notes")
3320
3321
        _add_filter(cmd, filter, filter_id)
3322
3323
        if details is not None:
3324
            cmd.set_attribute("details", _to_bool(details))
3325
3326
        if result is not None:
3327
            cmd.set_attribute("result", _to_bool(result))
3328
3329
        return self._send_xml_command(cmd)
3330
3331
    def get_note(self, note_id: str) -> Any:
3332
        """Request a single note
3333
3334
        Arguments:
3335
            note_id: UUID of an existing note
3336
3337
        Returns:
3338
            The response. See :py:meth:`send_command` for details.
3339
        """
3340
        if not note_id:
3341
            raise RequiredArgument(
3342
                function=self.get_note.__name__, argument='note_id'
3343
            )
3344
3345
        cmd = XmlCommand("get_notes")
3346
        cmd.set_attribute("note_id", note_id)
3347
3348
        # for single entity always request all details
3349
        cmd.set_attribute("details", "1")
3350
        return self._send_xml_command(cmd)
3351
3352
    def get_nvts(
3353
        self,
3354
        *,
3355
        details: Optional[bool] = None,
3356
        preferences: Optional[bool] = None,
3357
        preference_count: Optional[bool] = None,
3358
        timeout: Optional[bool] = None,
3359
        config_id: Optional[str] = None,
3360
        preferences_config_id: Optional[str] = None,
3361
        family: Optional[str] = None,
3362
        sort_order: Optional[str] = None,
3363
        sort_field: Optional[str] = None
3364
    ):
3365
        """Request a list of nvts
3366
3367
        Arguments:
3368
            details: Whether to include full details
3369
            preferences: Whether to include nvt preferences
3370
            preference_count: Whether to include preference count
3371
            timeout: Whether to include the special timeout preference
3372
            config_id: UUID of scan config to which to limit the NVT listing
3373
            preferences_config_id: UUID of scan config to use for preference
3374
                values
3375
            family: Family to which to limit NVT listing
3376
            sort_order: Sort order
3377
            sort_field: Sort field
3378
3379
        Returns:
3380
            The response. See :py:meth:`send_command` for details.
3381
        """
3382
        cmd = XmlCommand("get_nvts")
3383
3384
        if details is not None:
3385
            cmd.set_attribute("details", _to_bool(details))
3386
3387
        if preferences is not None:
3388
            cmd.set_attribute("preferences", _to_bool(preferences))
3389
3390
        if preference_count is not None:
3391
            cmd.set_attribute("preference_count", _to_bool(preference_count))
3392
3393
        if timeout is not None:
3394
            cmd.set_attribute("timeout", _to_bool(timeout))
3395
3396
        if config_id:
3397
            cmd.set_attribute("config_id", config_id)
3398
3399
        if preferences_config_id:
3400
            cmd.set_attribute("preferences_config_id", preferences_config_id)
3401
3402
        if family:
3403
            cmd.set_attribute("family", family)
3404
3405
        if sort_order:
3406
            cmd.set_attribute("sort_order", sort_order)
3407
3408
        if sort_field:
3409
            cmd.set_attribute("sort_field", sort_field)
3410
3411
        return self._send_xml_command(cmd)
3412
3413
    def get_nvt(self, nvt_oid: str):
3414
        """Request a single nvt
3415
3416
        Arguments:
3417
            nvt_oid: OID of an existing nvt
3418
3419
        Returns:
3420
            The response. See :py:meth:`send_command` for details.
3421
        """
3422
        cmd = XmlCommand("get_nvts")
3423
3424
        if not nvt_oid:
3425
            raise RequiredArgument(
3426
                function=self.get_nvt.__name__, argument='nvt_oid'
3427
            )
3428
3429
        cmd.set_attribute("nvt_oid", nvt_oid)
3430
3431
        # for single entity always request all details
3432
        cmd.set_attribute("details", "1")
3433
        return self._send_xml_command(cmd)
3434
3435
    def get_nvt_families(self, *, sort_order: Optional[str] = None):
3436
        """Request a list of nvt families
3437
3438
        Arguments:
3439
            sort_order: Sort order
3440
3441
        Returns:
3442
            The response. See :py:meth:`send_command` for details.
3443
        """
3444
        cmd = XmlCommand("get_nvt_families")
3445
3446
        if sort_order:
3447
            cmd.set_attribute("sort_order", sort_order)
3448
3449
        return self._send_xml_command(cmd)
3450
3451 View Code Duplication
    def get_overrides(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3452
        self,
3453
        *,
3454
        filter: Optional[str] = None,
3455
        filter_id: Optional[str] = None,
3456
        details: Optional[bool] = None,
3457
        result: Optional[bool] = None
3458
    ) -> Any:
3459
        """Request a list of overrides
3460
3461
        Arguments:
3462
            filter: Filter term to use for the query
3463
            filter_id: UUID of an existing filter to use for the query
3464
            details: Whether to include full details
3465
            result: Whether to include results using the override
3466
3467
        Returns:
3468
            The response. See :py:meth:`send_command` for details.
3469
        """
3470
        cmd = XmlCommand("get_overrides")
3471
3472
        _add_filter(cmd, filter, filter_id)
3473
3474
        if details is not None:
3475
            cmd.set_attribute("details", _to_bool(details))
3476
3477
        if result is not None:
3478
            cmd.set_attribute("result", _to_bool(result))
3479
3480
        return self._send_xml_command(cmd)
3481
3482
    def get_override(self, override_id: str) -> Any:
3483
        """Request a single override
3484
3485
        Arguments:
3486
            override_id: UUID of an existing override
3487
3488
        Returns:
3489
            The response. See :py:meth:`send_command` for details.
3490
        """
3491
        cmd = XmlCommand("get_overrides")
3492
3493
        if not override_id:
3494
            raise RequiredArgument(
3495
                function=self.get_override.__name__, argument='override_id'
3496
            )
3497
3498
        cmd.set_attribute("override_id", override_id)
3499
3500
        # for single entity always request all details
3501
        cmd.set_attribute("details", "1")
3502
        return self._send_xml_command(cmd)
3503
3504 View Code Duplication
    def get_permissions(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3505
        self,
3506
        *,
3507
        filter: Optional[str] = None,
3508
        filter_id: Optional[str] = None,
3509
        trash: Optional[bool] = None
3510
    ) -> Any:
3511
        """Request a list of permissions
3512
3513
        Arguments:
3514
            filter: Filter term to use for the query
3515
            filter_id: UUID of an existing filter to use for the query
3516
            trash: Whether to get permissions in the trashcan instead
3517
3518
        Returns:
3519
            The response. See :py:meth:`send_command` for details.
3520
        """
3521
        cmd = XmlCommand("get_permissions")
3522
3523
        _add_filter(cmd, filter, filter_id)
3524
3525
        if trash is not None:
3526
            cmd.set_attribute("trash", _to_bool(trash))
3527
3528
        return self._send_xml_command(cmd)
3529
3530
    def get_permission(self, permission_id: str) -> Any:
3531
        """Request a single permission
3532
3533
        Arguments:
3534
            permission_id: UUID of an existing permission
3535
3536
        Returns:
3537
            The response. See :py:meth:`send_command` for details.
3538
        """
3539
        cmd = XmlCommand("get_permissions")
3540
3541
        if not permission_id:
3542
            raise RequiredArgument(
3543
                function=self.get_permission.__name__, argument='permission_id'
3544
            )
3545
3546
        cmd.set_attribute("permission_id", permission_id)
3547
        return self._send_xml_command(cmd)
3548
3549 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...
3550
        self,
3551
        *,
3552
        filter: Optional[str] = None,
3553
        filter_id: Optional[str] = None,
3554
        details: Optional[bool] = None,
3555
        targets: Optional[bool] = None,
3556
        trash: Optional[bool] = None
3557
    ) -> Any:
3558
        """Request a list of port lists
3559
3560
        Arguments:
3561
            filter: Filter term to use for the query
3562
            filter_id: UUID of an existing filter to use for the query
3563
            details: Whether to include full port list details
3564
            targets: Whether to include targets using this port list
3565
            trash: Whether to get port lists in the trashcan instead
3566
3567
        Returns:
3568
            The response. See :py:meth:`send_command` for details.
3569
        """
3570
        cmd = XmlCommand("get_port_lists")
3571
3572
        _add_filter(cmd, filter, filter_id)
3573
3574
        if details is not None:
3575
            cmd.set_attribute("details", _to_bool(details))
3576
3577
        if targets is not None:
3578
            cmd.set_attribute("targets", _to_bool(targets))
3579
3580
        if trash is not None:
3581
            cmd.set_attribute("trash", _to_bool(trash))
3582
3583
        return self._send_xml_command(cmd)
3584
3585
    def get_port_list(self, port_list_id: str):
3586
        """Request a single port list
3587
3588
        Arguments:
3589
            port_list_id: UUID of an existing port list
3590
3591
        Returns:
3592
            The response. See :py:meth:`send_command` for details.
3593
        """
3594
        cmd = XmlCommand("get_port_lists")
3595
3596
        if not port_list_id:
3597
            raise RequiredArgument(
3598
                function=self.get_port_list.__name__, argument='port_list_id'
3599
            )
3600
3601
        cmd.set_attribute("port_list_id", port_list_id)
3602
3603
        # for single entity always request all details
3604
        cmd.set_attribute("details", "1")
3605
        return self._send_xml_command(cmd)
3606
3607
    def get_preferences(
3608
        self, *, nvt_oid: Optional[str] = None, config_id: Optional[str] = None
3609
    ) -> Any:
3610
        """Request a list of preferences
3611
3612
        When the command includes a config_id attribute, the preference element
3613
        includes the preference name, type and value, and the NVT to which the
3614
        preference applies. Otherwise, the preference element includes just the
3615
        name and value, with the NVT and type built into the name.
3616
3617
        Arguments:
3618
            nvt_oid: OID of nvt
3619
            config_id: UUID of scan config of which to show preference values
3620
3621
        Returns:
3622
            The response. See :py:meth:`send_command` for details.
3623
        """
3624
        cmd = XmlCommand("get_preferences")
3625
3626
        if nvt_oid:
3627
            cmd.set_attribute("nvt_oid", nvt_oid)
3628
3629
        if config_id:
3630
            cmd.set_attribute("config_id", config_id)
3631
3632
        return self._send_xml_command(cmd)
3633
3634
    def get_preference(
3635
        self,
3636
        name: str,
3637
        *,
3638
        nvt_oid: Optional[str] = None,
3639
        config_id: Optional[str] = None
3640
    ) -> Any:
3641
        """Request a nvt preference
3642
3643
        Arguments:
3644
            name: name of a particular preference
3645
            nvt_oid: OID of nvt
3646
            config_id: UUID of scan config of which to show preference values
3647
3648
        Returns:
3649
            The response. See :py:meth:`send_command` for details.
3650
        """
3651
        cmd = XmlCommand("get_preferences")
3652
3653
        if not name:
3654
            raise RequiredArgument(
3655
                function=self.get_preference.__name__, argument='name'
3656
            )
3657
3658
        cmd.set_attribute("preference", name)
3659
3660
        if nvt_oid:
3661
            cmd.set_attribute("nvt_oid", nvt_oid)
3662
3663
        if config_id:
3664
            cmd.set_attribute("config_id", config_id)
3665
3666
        return self._send_xml_command(cmd)
3667
3668 View Code Duplication
    def get_reports(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3669
        self,
3670
        *,
3671
        filter: Optional[str] = None,
3672
        filter_id: Optional[str] = None,
3673
        note_details: Optional[bool] = None,
3674
        override_details: Optional[bool] = None,
3675
        details: Optional[bool] = None
3676
    ) -> Any:
3677
        """Request a list of reports
3678
3679
        Arguments:
3680
            filter: Filter term to use for the query
3681
            filter_id: UUID of an existing filter to use for the query
3682
            note_details: If notes are included, whether to include note details
3683
            override_details: If overrides are included, whether to include
3684
                override details
3685
            details: Whether to exclude results
3686
3687
        Returns:
3688
            The response. See :py:meth:`send_command` for details.
3689
        """
3690
        cmd = XmlCommand("get_reports")
3691
3692
        if filter:
3693
            cmd.set_attribute("report_filter", filter)
3694
3695
        if filter_id:
3696
            cmd.set_attribute("report_filt_id", filter_id)
3697
3698
        if note_details is not None:
3699
            cmd.set_attribute("note_details", _to_bool(note_details))
3700
3701
        if override_details is not None:
3702
            cmd.set_attribute("override_details", _to_bool(override_details))
3703
3704
        if details is not None:
3705
            cmd.set_attribute("details", _to_bool(details))
3706
3707
        cmd.set_attribute("ignore_pagination", "1")
3708
3709
        return self._send_xml_command(cmd)
3710
3711
    def get_report(
3712
        self,
3713
        report_id: str,
3714
        *,
3715
        filter: Optional[str] = None,
3716
        filter_id: Optional[str] = None,
3717
        delta_report_id: Optional[str] = None,
3718
        report_format_id: Optional[str] = None,
3719
        ignore_pagination: Optional[bool] = None,
3720
        details: Optional[bool] = None
3721
    ) -> Any:
3722
        """Request a single report
3723
3724
        Arguments:
3725
            report_id: UUID of an existing report
3726
            filter: Filter term to use to filter results in the report
3727
            filter_id: UUID of filter to use to filter results in the report
3728
            delta_report_id: UUID of an existing report to compare report to.
3729
            report_format_id: UUID of report format to use
3730
            ignore_pagination: Whether to ignore the filter terms "first" and
3731
                "rows".
3732
            details: Request additional report information details
3733
3734
        Returns:
3735
            The response. See :py:meth:`send_command` for details.
3736
        """
3737
        cmd = XmlCommand("get_reports")
3738
3739
        if not report_id:
3740
            raise RequiredArgument(
3741
                function=self.get_report.__name__, argument='report_id'
3742
            )
3743
3744
        cmd.set_attribute("report_id", report_id)
3745
3746
        _add_filter(cmd, filter, filter_id)
3747
3748
        if delta_report_id:
3749
            cmd.set_attribute("delta_report_id", delta_report_id)
3750
3751
        if report_format_id:
3752
            cmd.set_attribute("format_id", report_format_id)
3753
3754
        if ignore_pagination is not None:
3755
            cmd.set_attribute("ignore_pagination", _to_bool(ignore_pagination))
3756
3757
        if details is not None:
3758
            cmd.set_attribute("details", _to_bool(details))
3759
3760
        return self._send_xml_command(cmd)
3761
3762
    def get_report_formats(
3763
        self,
3764
        *,
3765
        filter: Optional[str] = None,
3766
        filter_id: Optional[str] = None,
3767
        trash: Optional[bool] = None,
3768
        alerts: Optional[bool] = None,
3769
        params: Optional[bool] = None,
3770
        details: Optional[bool] = None
3771
    ) -> Any:
3772
        """Request a list of report formats
3773
3774
        Arguments:
3775
            filter: Filter term to use for the query
3776
            filter_id: UUID of an existing filter to use for the query
3777
            trash: Whether to get the trashcan report formats instead
3778
            alerts: Whether to include alerts that use the report format
3779
            params: Whether to include report format parameters
3780
            details: Include report format file, signature and parameters
3781
3782
        Returns:
3783
            The response. See :py:meth:`send_command` for details.
3784
        """
3785
        cmd = XmlCommand("get_report_formats")
3786
3787
        _add_filter(cmd, filter, filter_id)
3788
3789
        if details is not None:
3790
            cmd.set_attribute("details", _to_bool(details))
3791
3792
        if alerts is not None:
3793
            cmd.set_attribute("alerts", _to_bool(alerts))
3794
3795
        if params is not None:
3796
            cmd.set_attribute("params", _to_bool(params))
3797
3798
        if trash is not None:
3799
            cmd.set_attribute("trash", _to_bool(trash))
3800
3801
        return self._send_xml_command(cmd)
3802
3803
    def get_report_format(self, report_format_id: str) -> Any:
3804
        """Request a single report format
3805
3806
        Arguments:
3807
            report_format_id: UUID of an existing report format
3808
3809
        Returns:
3810
            The response. See :py:meth:`send_command` for details.
3811
        """
3812
        cmd = XmlCommand("get_report_formats")
3813
3814
        if not report_format_id:
3815
            raise RequiredArgument(
3816
                function=self.get_report_format.__name__,
3817
                argument='report_format_id',
3818
            )
3819
3820
        cmd.set_attribute("report_format_id", report_format_id)
3821
3822
        # for single entity always request all details
3823
        cmd.set_attribute("details", "1")
3824
        return self._send_xml_command(cmd)
3825
3826 View Code Duplication
    def get_results(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3827
        self,
3828
        *,
3829
        filter: Optional[str] = None,
3830
        filter_id: Optional[str] = None,
3831
        task_id: Optional[str] = None,
3832
        note_details: Optional[bool] = None,
3833
        override_details: Optional[bool] = None,
3834
        details: Optional[bool] = None
3835
    ) -> Any:
3836
        """Request a list of results
3837
3838
        Arguments:
3839
            filter: Filter term to use for the query
3840
            filter_id: UUID of an existing filter to use for the query
3841
            task_id: UUID of task for note and override handling
3842
            note_details: If notes are included, whether to include note details
3843
            override_details: If overrides are included, whether to include
3844
                override details
3845
            details: Whether to include additional details of the results
3846
3847
        Returns:
3848
            The response. See :py:meth:`send_command` for details.
3849
        """
3850
        cmd = XmlCommand("get_results")
3851
3852
        _add_filter(cmd, filter, filter_id)
3853
3854
        if task_id:
3855
            cmd.set_attribute("task_id", task_id)
3856
3857
        if details is not None:
3858
            cmd.set_attribute("details", _to_bool(details))
3859
3860
        if note_details is not None:
3861
            cmd.set_attribute("note_details", _to_bool(note_details))
3862
3863
        if override_details is not None:
3864
            cmd.set_attribute("override_details", _to_bool(override_details))
3865
3866
        return self._send_xml_command(cmd)
3867
3868
    def get_result(self, result_id: str) -> Any:
3869
        """Request a single result
3870
3871
        Arguments:
3872
            result_id: UUID of an existing result
3873
3874
        Returns:
3875
            The response. See :py:meth:`send_command` for details.
3876
        """
3877
        cmd = XmlCommand("get_results")
3878
3879
        if not result_id:
3880
            raise RequiredArgument(
3881
                function=self.get_result.__name__, argument='result_id'
3882
            )
3883
3884
        cmd.set_attribute("result_id", result_id)
3885
3886
        # for single entity always request all details
3887
        cmd.set_attribute("details", "1")
3888
        return self._send_xml_command(cmd)
3889
3890 View Code Duplication
    def get_roles(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3891
        self,
3892
        *,
3893
        filter: Optional[str] = None,
3894
        filter_id: Optional[str] = None,
3895
        trash: Optional[bool] = None
3896
    ) -> Any:
3897
        """Request a list of roles
3898
3899
        Arguments:
3900
            filter: Filter term to use for the query
3901
            filter_id: UUID of an existing filter to use for the query
3902
            trash: Whether to get the trashcan roles instead
3903
3904
        Returns:
3905
            The response. See :py:meth:`send_command` for details.
3906
        """
3907
        cmd = XmlCommand("get_roles")
3908
3909
        _add_filter(cmd, filter, filter_id)
3910
3911
        if trash is not None:
3912
            cmd.set_attribute("trash", _to_bool(trash))
3913
3914
        return self._send_xml_command(cmd)
3915
3916
    def get_role(self, role_id: str) -> Any:
3917
        """Request a single role
3918
3919
        Arguments:
3920
            role_id: UUID of an existing role
3921
3922
        Returns:
3923
            The response. See :py:meth:`send_command` for details.
3924
        """
3925
        if not role_id:
3926
            raise RequiredArgument(
3927
                function=self.get_role.__name__, argument='role_id'
3928
            )
3929
3930
        cmd = XmlCommand("get_roles")
3931
        cmd.set_attribute("role_id", role_id)
3932
        return self._send_xml_command(cmd)
3933
3934 View Code Duplication
    def get_scanners(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3935
        self,
3936
        *,
3937
        filter: Optional[str] = None,
3938
        filter_id: Optional[str] = None,
3939
        trash: Optional[bool] = None,
3940
        details: Optional[bool] = None
3941
    ) -> Any:
3942
        """Request a list of scanners
3943
3944
        Arguments:
3945
            filter: Filter term to use for the query
3946
            filter_id: UUID of an existing filter to use for the query
3947
            trash: Whether to get the trashcan scanners instead
3948
            details:  Whether to include extra details like tasks using this
3949
                scanner
3950
3951
        Returns:
3952
            The response. See :py:meth:`send_command` for details.
3953
        """
3954
        cmd = XmlCommand("get_scanners")
3955
3956
        _add_filter(cmd, filter, filter_id)
3957
3958
        if trash is not None:
3959
            cmd.set_attribute("trash", _to_bool(trash))
3960
3961
        if details is not None:
3962
            cmd.set_attribute("details", _to_bool(details))
3963
3964
        return self._send_xml_command(cmd)
3965
3966
    def get_scanner(self, scanner_id: str) -> Any:
3967
        """Request a single scanner
3968
3969
        Arguments:
3970
            scanner_id: UUID of an existing scanner
3971
3972
        Returns:
3973
            The response. See :py:meth:`send_command` for details.
3974
        """
3975
        cmd = XmlCommand("get_scanners")
3976
3977
        if not scanner_id:
3978
            raise RequiredArgument(
3979
                function=self.get_scanner.__name__, argument='scanner_id'
3980
            )
3981
3982
        cmd.set_attribute("scanner_id", scanner_id)
3983
3984
        # for single entity always request all details
3985
        cmd.set_attribute("details", "1")
3986
        return self._send_xml_command(cmd)
3987
3988
    def get_schedules(
3989
        self,
3990
        *,
3991
        filter: Optional[str] = None,
3992
        filter_id: Optional[str] = None,
3993
        trash: Optional[bool] = None,
3994
        tasks: Optional[bool] = None
3995
    ) -> Any:
3996
        """Request a list of schedules
3997
3998
        Arguments:
3999
            filter: Filter term to use for the query
4000
            filter_id: UUID of an existing filter to use for the query
4001
            trash: Whether to get the trashcan schedules instead
4002
            tasks: Whether to include tasks using the schedules
4003
4004
        Returns:
4005
            The response. See :py:meth:`send_command` for details.
4006
        """
4007
        cmd = XmlCommand("get_schedules")
4008
4009
        _add_filter(cmd, filter, filter_id)
4010
4011
        if trash is not None:
4012
            cmd.set_attribute("trash", _to_bool(trash))
4013
4014
        if tasks is not None:
4015
            cmd.set_attribute("tasks", _to_bool(tasks))
4016
4017
        return self._send_xml_command(cmd)
4018
4019
    def get_schedule(
4020
        self, schedule_id: str, *, tasks: Optional[bool] = None
4021
    ) -> Any:
4022
        """Request a single schedule
4023
4024
        Arguments:
4025
            schedule_id: UUID of an existing schedule
4026
            tasks: Whether to include tasks using the schedules
4027
4028
        Returns:
4029
            The response. See :py:meth:`send_command` for details.
4030
        """
4031
        cmd = XmlCommand("get_schedules")
4032
4033
        if not schedule_id:
4034
            raise RequiredArgument(
4035
                function=self.get_schedule.__name__, argument='schedule_id'
4036
            )
4037
4038
        cmd.set_attribute("schedule_id", schedule_id)
4039
4040
        if tasks is not None:
4041
            cmd.set_attribute("tasks", _to_bool(tasks))
4042
4043
        return self._send_xml_command(cmd)
4044
4045
    def get_settings(self, *, filter: Optional[str] = None) -> Any:
4046
        """Request a list of user settings
4047
4048
        Arguments:
4049
            filter: Filter term to use for the query
4050
4051
        Returns:
4052
            The response. See :py:meth:`send_command` for details.
4053
        """
4054
        cmd = XmlCommand("get_settings")
4055
4056
        if filter:
4057
            cmd.set_attribute("filter", filter)
4058
4059
        return self._send_xml_command(cmd)
4060
4061
    def get_setting(self, setting_id: str) -> Any:
4062
        """Request a single setting
4063
4064
        Arguments:
4065
            setting_id: UUID of an existing setting
4066
4067
        Returns:
4068
            The response. See :py:meth:`send_command` for details.
4069
        """
4070
        cmd = XmlCommand("get_settings")
4071
4072
        if not setting_id:
4073
            raise RequiredArgument(
4074
                function=self.get_setting.__name__, argument='setting_id'
4075
            )
4076
4077
        cmd.set_attribute("setting_id", setting_id)
4078
        return self._send_xml_command(cmd)
4079
4080
    def get_system_reports(
4081
        self,
4082
        *,
4083
        name: Optional[str] = None,
4084
        duration: Optional[int] = None,
4085
        start_time: Optional[str] = None,
4086
        end_time: Optional[str] = None,
4087
        brief: Optional[bool] = None,
4088
        slave_id: Optional[str] = None
4089
    ) -> Any:
4090
        """Request a list of system reports
4091
4092
        Arguments:
4093
            name: A string describing the required system report
4094
            duration: The number of seconds into the past that the system report
4095
                should include
4096
            start_time: The start of the time interval the system report should
4097
                include in ISO time format
4098
            end_time: The end of the time interval the system report should
4099
                include in ISO time format
4100
            brief: Whether to include the actual system reports
4101
            slave_id: UUID of GMP scanner from which to get the system reports
4102
4103
        Returns:
4104
            The response. See :py:meth:`send_command` for details.
4105
        """
4106
        cmd = XmlCommand("get_system_reports")
4107
4108
        if name:
4109
            cmd.set_attribute("name", name)
4110
4111
        if duration is not None:
4112
            if not isinstance(duration, numbers.Integral):
4113
                raise InvalidArgument("duration needs to be an integer number")
4114
4115
            cmd.set_attribute("duration", str(duration))
4116
4117
        if start_time:
4118
            cmd.set_attribute("start_time", str(start_time))
4119
4120
        if end_time:
4121
            cmd.set_attribute("end_time", str(end_time))
4122
4123
        if brief is not None:
4124
            cmd.set_attribute("brief", _to_bool(brief))
4125
4126
        if slave_id:
4127
            cmd.set_attribute("slave_id", slave_id)
4128
4129
        return self._send_xml_command(cmd)
4130
4131
    def get_tags(
4132
        self,
4133
        *,
4134
        filter: Optional[str] = None,
4135
        filter_id: Optional[str] = None,
4136
        trash: Optional[bool] = None,
4137
        names_only: Optional[bool] = None
4138
    ) -> Any:
4139
        """Request a list of tags
4140
4141
        Arguments:
4142
            filter: Filter term to use for the query
4143
            filter_id: UUID of an existing filter to use for the query
4144
            trash: Whether to get tags from the trashcan instead
4145
            names_only: Whether to get only distinct tag names
4146
4147
        Returns:
4148
            The response. See :py:meth:`send_command` for details.
4149
        """
4150
        cmd = XmlCommand("get_tags")
4151
4152
        _add_filter(cmd, filter, filter_id)
4153
4154
        if trash is not None:
4155
            cmd.set_attribute("trash", _to_bool(trash))
4156
4157
        if names_only is not None:
4158
            cmd.set_attribute("names_only", _to_bool(names_only))
4159
4160
        return self._send_xml_command(cmd)
4161
4162
    def get_tag(self, tag_id: str) -> Any:
4163
        """Request a single tag
4164
4165
        Arguments:
4166
            tag_id: UUID of an existing tag
4167
4168
        Returns:
4169
            The response. See :py:meth:`send_command` for details.
4170
        """
4171
        cmd = XmlCommand("get_tags")
4172
4173
        if not tag_id:
4174
            raise RequiredArgument(
4175
                function=self.get_tag.__name__, argument='tag_id'
4176
            )
4177
4178
        cmd.set_attribute("tag_id", tag_id)
4179
        return self._send_xml_command(cmd)
4180
4181
    def get_targets(
4182
        self,
4183
        *,
4184
        filter: Optional[str] = None,
4185
        filter_id: Optional[str] = None,
4186
        trash: Optional[bool] = None,
4187
        tasks: Optional[bool] = None
4188
    ) -> Any:
4189
        """Request a list of targets
4190
4191
        Arguments:
4192
            filter: Filter term to use for the query
4193
            filter_id: UUID of an existing filter to use for the query
4194
            trash: Whether to get the trashcan targets instead
4195
            tasks: Whether to include list of tasks that use the target
4196
4197
        Returns:
4198
            The response. See :py:meth:`send_command` for details.
4199
        """
4200
        cmd = XmlCommand("get_targets")
4201
4202
        _add_filter(cmd, filter, filter_id)
4203
4204
        if trash is not None:
4205
            cmd.set_attribute("trash", _to_bool(trash))
4206
4207
        if tasks is not None:
4208
            cmd.set_attribute("tasks", _to_bool(tasks))
4209
4210
        return self._send_xml_command(cmd)
4211
4212
    def get_target(
4213
        self, target_id: str, *, tasks: Optional[bool] = None
4214
    ) -> Any:
4215
        """Request a single target
4216
4217
        Arguments:
4218
            target_id: UUID of an existing target
4219
            tasks: Whether to include list of tasks that use the target
4220
4221
        Returns:
4222
            The response. See :py:meth:`send_command` for details.
4223
        """
4224
        cmd = XmlCommand("get_targets")
4225
4226
        if not target_id:
4227
            raise RequiredArgument(
4228
                function=self.get_target.__name__, argument='target_id'
4229
            )
4230
4231
        cmd.set_attribute("target_id", target_id)
4232
4233
        if tasks is not None:
4234
            cmd.set_attribute("tasks", _to_bool(tasks))
4235
4236
        return self._send_xml_command(cmd)
4237
4238
    def get_tasks(
4239
        self,
4240
        *,
4241
        filter: Optional[str] = None,
4242
        filter_id: Optional[str] = None,
4243
        trash: Optional[bool] = None,
4244
        details: Optional[bool] = None,
4245
        schedules_only: Optional[bool] = None
4246
    ) -> Any:
4247
        """Request a list of tasks
4248
4249
        Arguments:
4250
            filter: Filter term to use for the query
4251
            filter_id: UUID of an existing filter to use for the query
4252
            trash: Whether to get the trashcan tasks instead
4253
            details: Whether to include full task details
4254
            schedules_only: Whether to only include id, name and schedule
4255
                details
4256
4257
        Returns:
4258
            The response. See :py:meth:`send_command` for details.
4259
        """
4260
        cmd = XmlCommand("get_tasks")
4261
4262
        _add_filter(cmd, filter, filter_id)
4263
4264
        if trash is not None:
4265
            cmd.set_attribute("trash", _to_bool(trash))
4266
4267
        if details is not None:
4268
            cmd.set_attribute("details", _to_bool(details))
4269
4270
        if schedules_only is not None:
4271
            cmd.set_attribute("schedules_only", _to_bool(schedules_only))
4272
4273
        return self._send_xml_command(cmd)
4274
4275
    def get_task(self, task_id: str) -> Any:
4276
        """Request a single task
4277
4278
        Arguments:
4279
            task_id: UUID of an existing task
4280
4281
        Returns:
4282
            The response. See :py:meth:`send_command` for details.
4283
        """
4284
        cmd = XmlCommand("get_tasks")
4285
4286
        if not task_id:
4287
            raise RequiredArgument(
4288
                function=self.get_task.__name__, argument='task_id'
4289
            )
4290
4291
        cmd.set_attribute("task_id", task_id)
4292
4293
        # for single entity always request all details
4294
        cmd.set_attribute("details", "1")
4295
        return self._send_xml_command(cmd)
4296
4297
    def get_users(
4298
        self, *, filter: Optional[str] = None, filter_id: Optional[str] = None
4299
    ) -> Any:
4300
        """Request a list of users
4301
4302
        Arguments:
4303
            filter: Filter term to use for the query
4304
            filter_id: UUID of an existing filter to use for the query
4305
4306
        Returns:
4307
            The response. See :py:meth:`send_command` for details.
4308
        """
4309
        cmd = XmlCommand("get_users")
4310
4311
        _add_filter(cmd, filter, filter_id)
4312
4313
        return self._send_xml_command(cmd)
4314
4315
    def get_user(self, user_id: str) -> Any:
4316
        """Request a single user
4317
4318
        Arguments:
4319
            user_id: UUID of an existing user
4320
4321
        Returns:
4322
            The response. See :py:meth:`send_command` for details.
4323
        """
4324
        cmd = XmlCommand("get_users")
4325
4326
        if not user_id:
4327
            raise RequiredArgument(
4328
                function=self.get_user.__name__, argument='user_id'
4329
            )
4330
4331
        cmd.set_attribute("user_id", user_id)
4332
        return self._send_xml_command(cmd)
4333
4334
    def get_version(self) -> Any:
4335
        """Get the Greenbone Manager Protocol version used by the remote gvmd
4336
4337
        Returns:
4338
            The response. See :py:meth:`send_command` for details.
4339
        """
4340
        return self._send_xml_command(XmlCommand("get_version"))
4341
4342
    def help(
4343
        self, *, format: Optional[str] = None, help_type: Optional[str] = ""
4344
    ) -> Any:
4345
        """Get the help text
4346
4347
        Arguments:
4348
            format: One of "html", "rnc", "text" or "xml
4349
            help_type: One of "brief" or "". Default ""
4350
4351
        Returns:
4352
            The response. See :py:meth:`send_command` for details.
4353
        """
4354
        cmd = XmlCommand("help")
4355
4356
        if help_type not in ("", "brief"):
4357
            raise InvalidArgument(
4358
                'help_type argument must be an empty string or "brief"'
4359
            )
4360
4361
        cmd.set_attribute("type", help_type)
4362
4363
        if format:
4364
            if not format.lower() in ("html", "rnc", "text", "xml"):
4365
                raise InvalidArgument(
4366
                    "help format Argument must be one of html, rnc, text or "
4367
                    "xml"
4368
                )
4369
4370
            cmd.set_attribute("format", format)
4371
4372
        return self._send_xml_command(cmd)
4373
4374
    def modify_agent(
4375
        self,
4376
        agent_id: str,
4377
        *,
4378
        name: Optional[str] = None,
4379
        comment: Optional[str] = None
4380
    ) -> Any:
4381
        """Modifies an existing agent
4382
4383
        Arguments:
4384
            agent_id: UUID of the agent to be modified.
4385
            name: Name of the new credential
4386
            comment: Comment for the credential
4387
4388
        Returns:
4389
            The response. See :py:meth:`send_command` for details.
4390
        """
4391
        if not agent_id:
4392
            raise RequiredArgument(
4393
                function=self.modify_agent.__name__, argument='agent_id'
4394
            )
4395
4396
        cmd = XmlCommand("modify_agent")
4397
        cmd.set_attribute("agent_id", str(agent_id))
4398
4399
        if name:
4400
            cmd.add_element("name", name)
4401
4402
        if comment:
4403
            cmd.add_element("comment", comment)
4404
4405
        return self._send_xml_command(cmd)
4406
4407 View Code Duplication
    def modify_alert(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4408
        self,
4409
        alert_id: str,
4410
        *,
4411
        name: Optional[str] = None,
4412
        comment: Optional[str] = None,
4413
        filter_id: Optional[str] = None,
4414
        event: Optional[AlertEvent] = None,
4415
        event_data: Optional[dict] = None,
4416
        condition: Optional[AlertCondition] = None,
4417
        condition_data: Optional[dict] = None,
4418
        method: Optional[AlertMethod] = None,
4419
        method_data: Optional[dict] = None
4420
    ) -> Any:
4421
        """Modifies an existing alert.
4422
4423
        Arguments:
4424
            alert_id: UUID of the alert to be modified.
4425
            name: Name of the Alert.
4426
            condition: The condition that must be satisfied for the alert to
4427
                occur. If the event is either 'Updated SecInfo
4428
                arrived' or 'New SecInfo arrived', condition must be 'Always'.
4429
                Otherwise, condition can also be on of 'Severity at least',
4430
                'Filter count changed' or 'Filter count at least'.
4431
            condition_data: Data that defines the condition
4432
            event: The event that must happen for the alert to occur, one of
4433
                'Task run status changed', 'Updated SecInfo arrived' or
4434
                'New SecInfo arrived'
4435
            event_data: Data that defines the event
4436
            method: The method by which the user is alerted, one of 'SCP',
4437
                'Send', 'SMB', 'SNMP', 'Syslog' or 'Email';
4438
                if the event is neither 'Updated SecInfo arrived' nor
4439
                'New SecInfo arrived', method can also be one of 'Start Task',
4440
                'HTTP Get', 'Sourcefire Connector' or 'verinice Connector'.
4441
            method_data: Data that defines the method
4442
            filter_id: Filter to apply when executing alert
4443
            comment: Comment for the alert
4444
4445
        Returns:
4446
            The response. See :py:meth:`send_command` for details.
4447
        """
4448
4449
        if not alert_id:
4450
            raise RequiredArgument(
4451
                function=self.modify_alert.__name__, argument='alert_id'
4452
            )
4453
4454
        cmd = XmlCommand("modify_alert")
4455
        cmd.set_attribute("alert_id", str(alert_id))
4456
4457
        if name:
4458
            cmd.add_element("name", name)
4459
4460
        if comment:
4461
            cmd.add_element("comment", comment)
4462
4463
        if filter_id:
4464
            cmd.add_element("filter", attrs={"id": filter_id})
4465
4466
        if condition:
4467
            if not isinstance(condition, AlertCondition):
4468
                raise InvalidArgumentType(
4469
                    function=self.modify_alert.__name__,
4470
                    argument='condition',
4471
                    arg_type=AlertCondition.__name__,
4472
                )
4473
4474
            conditions = cmd.add_element("condition", condition.value)
4475
4476
            if condition_data is not None:
4477
                for key, value in condition_data.items():
4478
                    _data = conditions.add_element("data", value)
4479
                    _data.add_element("name", key)
4480
4481
        if method:
4482
            if not isinstance(method, AlertMethod):
4483
                raise InvalidArgumentType(
4484
                    function=self.modify_alert.__name__,
4485
                    argument='method',
4486
                    arg_type=AlertMethod.__name__,
4487
                )
4488
4489
            methods = cmd.add_element("method", method.value)
4490
4491
            if method_data is not None:
4492
                for key, value in method_data.items():
4493
                    _data = methods.add_element("data", value)
4494
                    _data.add_element("name", key)
4495
4496
        if event:
4497
            if not isinstance(event, AlertEvent):
4498
                raise InvalidArgumentType(
4499
                    function=self.modify_alert.__name__,
4500
                    argument='event',
4501
                    arg_type=AlertEvent.__name__,
4502
                )
4503
4504
            _check_event(event, condition, method)
4505
4506
            events = cmd.add_element("event", event.value)
4507
4508
            if event_data is not None:
4509
                for key, value in event_data.items():
4510
                    _data = events.add_element("data", value)
4511
                    _data.add_element("name", key)
4512
4513
        return self._send_xml_command(cmd)
4514
4515
    def modify_asset(self, asset_id: str, comment: Optional[str] = "") -> Any:
4516
        """Modifies an existing asset.
4517
4518
        Arguments:
4519
            asset_id: UUID of the asset to be modified.
4520
            comment: Comment for the asset.
4521
4522
        Returns:
4523
            The response. See :py:meth:`send_command` for details.
4524
        """
4525
        if not asset_id:
4526
            raise RequiredArgument(
4527
                function=self.modify_asset.__name__, argument='asset_id'
4528
            )
4529
4530
        cmd = XmlCommand("modify_asset")
4531
        cmd.set_attribute("asset_id", asset_id)
4532
        cmd.add_element("comment", comment)
4533
4534
        return self._send_xml_command(cmd)
4535
4536
    def modify_auth(self, group_name: str, auth_conf_settings: dict) -> Any:
4537
        """Modifies an existing auth.
4538
4539
        Arguments:
4540
            group_name: Name of the group to be modified.
4541
            auth_conf_settings: The new auth config.
4542
4543
        Returns:
4544
            The response. See :py:meth:`send_command` for details.
4545
        """
4546
        if not group_name:
4547
            raise RequiredArgument(
4548
                function=self.modify_auth.__name__, argument='group_name'
4549
            )
4550
        if not auth_conf_settings:
4551
            raise RequiredArgument(
4552
                function=self.modify_auth.__name__,
4553
                argument='auth_conf_settings',
4554
            )
4555
        cmd = XmlCommand("modify_auth")
4556
        _xmlgroup = cmd.add_element("group", attrs={"name": str(group_name)})
4557
4558
        for key, value in auth_conf_settings.items():
4559
            _xmlauthconf = _xmlgroup.add_element("auth_conf_setting")
4560
            _xmlauthconf.add_element("key", key)
4561
            _xmlauthconf.add_element("value", value)
4562
4563
        return self._send_xml_command(cmd)
4564
4565
    def modify_config_set_nvt_preference(
4566
        self,
4567
        config_id: str,
4568
        name: str,
4569
        nvt_oid: str,
4570
        *,
4571
        value: Optional[str] = None
4572
    ) -> Any:
4573
        """Modifies the nvt preferences of an existing scan config.
4574
4575
        Arguments:
4576
            config_id: UUID of scan config to modify.
4577
            name: Name for preference to change.
4578
            nvt_oid: OID of the NVT associated with preference to modify
4579
            value: New value for the preference. None to delete the preference
4580
                and to use the default instead.
4581
        """
4582
        if not config_id:
4583
            raise RequiredArgument(
4584
                function=self.modify_config_set_nvt_preference.__name__,
4585
                argument='config_id',
4586
            )
4587
4588
        if not nvt_oid:
4589
            raise RequiredArgument(
4590
                function=self.modify_config_set_nvt_preference.__name__,
4591
                argument='nvt_oid',
4592
            )
4593
4594
        if not name:
4595
            raise RequiredArgument(
4596
                function=self.modify_config_set_nvt_preference.__name__,
4597
                argument='name',
4598
            )
4599
4600
        cmd = XmlCommand("modify_config")
4601
        cmd.set_attribute("config_id", str(config_id))
4602
4603
        _xmlpref = cmd.add_element("preference")
4604
4605
        _xmlpref.add_element("nvt", attrs={"oid": nvt_oid})
4606
        _xmlpref.add_element("name", name)
4607
4608
        if value:
4609
            _xmlpref.add_element("value", _to_base64(value))
4610
4611
        return self._send_xml_command(cmd)
4612
4613
    def modify_config_set_name(self, config_id: str, name: str) -> Any:
4614
        """Modifies the name of an existing scan config
4615
4616
        Arguments:
4617
            config_id: UUID of scan config to modify.
4618
            name: New name for the config.
4619
        """
4620
        if not config_id:
4621
            raise RequiredArgument(
4622
                function=self.modify_config_set_name.__name__,
4623
                argument='config_id',
4624
            )
4625
4626
        if not name:
4627
            raise RequiredArgument(
4628
                function=self.modify_config_set_name.__name__,
4629
                argument='name',
4630
            )
4631
4632
        cmd = XmlCommand("modify_config")
4633
        cmd.set_attribute("config_id", str(config_id))
4634
4635
        cmd.add_element("name", name)
4636
4637
        return self._send_xml_command(cmd)
4638
4639
    def modify_config_set_comment(
4640
        self, config_id: str, comment: Optional[str] = ""
4641
    ) -> Any:
4642
        """Modifies the comment of an existing scan config
4643
4644
        Arguments:
4645
            config_id: UUID of scan config to modify.
4646
            comment: Comment to set on a config. Default: ''
4647
        """
4648
        if not config_id:
4649
            raise RequiredArgument(
4650
                function=self.modify_config_set_comment.__name__,
4651
                argument='config_id argument',
4652
            )
4653
4654
        cmd = XmlCommand("modify_config")
4655
        cmd.set_attribute("config_id", str(config_id))
4656
4657
        cmd.add_element("comment", comment)
4658
4659
        return self._send_xml_command(cmd)
4660
4661
    def modify_config_set_scanner_preference(
4662
        self, config_id: str, name: str, *, value: Optional[str] = None
4663
    ) -> Any:
4664
        """Modifies the scanner preferences of an existing scan config
4665
4666
        Arguments:
4667
            config_id: UUID of scan config to modify.
4668
            name: Name of the scanner preference to change
4669
            value: New value for the preference. None to delete the preference
4670
                and to use the default instead.
4671
4672
        """
4673
        if not config_id:
4674
            raise RequiredArgument(
4675
                function=self.modify_config_set_scanner_preference.__name__,
4676
                argument='config_id',
4677
            )
4678
4679
        if not name:
4680
            raise RequiredArgument(
4681
                function=self.modify_config_set_scanner_preference.__name__,
4682
                argument='name argument',
4683
            )
4684
4685
        cmd = XmlCommand("modify_config")
4686
        cmd.set_attribute("config_id", str(config_id))
4687
4688
        _xmlpref = cmd.add_element("preference")
4689
4690
        _xmlpref.add_element("name", name)
4691
4692
        if value:
4693
            _xmlpref.add_element("value", _to_base64(value))
4694
4695
        return self._send_xml_command(cmd)
4696
4697
    def modify_config_set_nvt_selection(
4698
        self, config_id: str, family: str, nvt_oids: List[str]
4699
    ) -> Any:
4700
        """Modifies the selected nvts of an existing scan config
4701
4702
        The manager updates the given family in the config to include only the
4703
        given NVTs.
4704
4705
        Arguments:
4706
            config_id: UUID of scan config to modify.
4707
            family: Name of the NVT family to include NVTs from
4708
            nvt_oids: List of NVTs to select for the family.
4709
        """
4710
        if not config_id:
4711
            raise RequiredArgument(
4712
                function=self.modify_config_set_nvt_selection.__name__,
4713
                argument='config_id',
4714
            )
4715
4716
        if not family:
4717
            raise RequiredArgument(
4718
                function=self.modify_config_set_nvt_selection.__name__,
4719
                argument='family argument',
4720
            )
4721
4722
        if not _is_list_like(nvt_oids):
4723
            raise InvalidArgumentType(
4724
                function=self.modify_config_set_nvt_selection.__name__,
4725
                argument='nvt_oids',
4726
                arg_type='list',
4727
            )
4728
4729
        cmd = XmlCommand("modify_config")
4730
        cmd.set_attribute("config_id", str(config_id))
4731
4732
        _xmlnvtsel = cmd.add_element("nvt_selection")
4733
        _xmlnvtsel.add_element("family", family)
4734
4735
        for nvt in nvt_oids:
4736
            _xmlnvtsel.add_element("nvt", attrs={"oid": nvt})
4737
4738
        return self._send_xml_command(cmd)
4739
4740
    def modify_config_set_family_selection(
4741
        self,
4742
        config_id: str,
4743
        families: List[str],
4744
        *,
4745
        auto_add_new_families: Optional[bool] = True,
4746
        auto_add_new_nvts: Optional[bool] = True
4747
    ) -> Any:
4748
        """
4749
        Selected the NVTs of a scan config at a family level.
4750
4751
        Arguments:
4752
            config_id: UUID of scan config to modify.
4753
            families: List of NVT family names to select.
4754
            auto_add_new_families: Whether new families should be added to the
4755
                scan config automatically. Default: True.
4756
            auto_add_new_nvts: Whether new NVTs in the selected families should
4757
                be added to the scan config automatically. Default: True.
4758
        """
4759
        if not config_id:
4760
            raise RequiredArgument(
4761
                function=self.modify_config_set_family_selection.__name__,
4762
                argument='config_id',
4763
            )
4764
4765
        if not _is_list_like(families):
4766
            raise InvalidArgumentType(
4767
                function=self.modify_config_set_family_selection.__name__,
4768
                argument='families',
4769
                arg_type='list',
4770
            )
4771
4772
        cmd = XmlCommand("modify_config")
4773
        cmd.set_attribute("config_id", str(config_id))
4774
4775
        _xmlfamsel = cmd.add_element("family_selection")
4776
        _xmlfamsel.add_element("growing", _to_bool(auto_add_new_families))
4777
4778
        for family in families:
4779
            _xmlfamily = _xmlfamsel.add_element("family")
4780
            _xmlfamily.add_element("name", family)
4781
            _xmlfamily.add_element("all", "1")
4782
            _xmlfamily.add_element("growing", _to_bool(auto_add_new_nvts))
4783
4784
        return self._send_xml_command(cmd)
4785
4786
    def modify_config(
4787
        self, config_id: str, selection: Optional[str] = None, **kwargs
4788
    ) -> Any:
4789
        """Modifies an existing scan config.
4790
4791
        DEPRECATED. Please use *modify_config_set_* methods instead.
4792
4793
        modify_config has four modes to operate depending on the selection.
4794
4795
        Arguments:
4796
            config_id: UUID of scan config to modify.
4797
            selection: one of 'scan_pref', 'nvt_pref', 'nvt_selection' or
4798
                'family_selection'
4799
            name: New name for preference.
4800
            value: New value for preference.
4801
            nvt_oids: List of NVTs associated with preference to modify.
4802
            family: Name of family to modify.
4803
4804
        Returns:
4805
            The response. See :py:meth:`send_command` for details.
4806
        """
4807
        if not config_id:
4808
            raise RequiredArgument(
4809
                function=self.modify_config.__name__,
4810
                argument='config_id argument',
4811
            )
4812
4813
        if selection is None:
4814
            deprecation(
4815
                "Using modify_config to update the comment of a scan config is"
4816
                "deprecated. Please use modify_config_set_comment instead."
4817
            )
4818
            return self.modify_config_set_comment(
4819
                config_id, kwargs.get("comment")
4820
            )
4821
4822
        if selection not in (
4823
            "nvt_pref",
4824
            "scan_pref",
4825
            "family_selection",
4826
            "nvt_selection",
4827
        ):
4828
            raise InvalidArgument(
4829
                "selection must be one of nvt_pref, "
4830
                "scan_pref, family_selection or "
4831
                "nvt_selection"
4832
            )
4833
4834
        if selection == "nvt_pref":
4835
            deprecation(
4836
                "Using modify_config to update a nvt preference of a scan "
4837
                "config is deprecated. Please use "
4838
                "modify_config_set_nvt_preference instead."
4839
            )
4840
            return self.modify_config_set_nvt_preference(config_id, **kwargs)
4841
4842
        if selection == "scan_pref":
4843
            deprecation(
4844
                "Using modify_config to update a scanner preference of a "
4845
                "scan config is deprecated. Please use "
4846
                "modify_config_set_scanner_preference instead."
4847
            )
4848
            return self.modify_config_set_scanner_preference(
4849
                config_id, **kwargs
4850
            )
4851
4852
        if selection == "nvt_selection":
4853
            deprecation(
4854
                "Using modify_config to update a nvt selection of a "
4855
                "scan config is deprecated. Please use "
4856
                "modify_config_set_nvt_selection instead."
4857
            )
4858
            return self.modify_config_set_nvt_selection(config_id, **kwargs)
4859
4860
        deprecation(
4861
            "Using modify_config to update a family selection of a "
4862
            "scan config is deprecated. Please use "
4863
            "modify_config_set_family_selection instead."
4864
        )
4865
        return self.modify_config_set_family_selection(config_id, **kwargs)
4866
4867
    def modify_credential(
4868
        self,
4869
        credential_id: str,
4870
        *,
4871
        name: Optional[str] = None,
4872
        comment: Optional[str] = None,
4873
        allow_insecure: Optional[bool] = None,
4874
        certificate: Optional[str] = None,
4875
        key_phrase: Optional[str] = None,
4876
        private_key: Optional[str] = None,
4877
        login: Optional[str] = None,
4878
        password: Optional[str] = None,
4879
        auth_algorithm: Optional[SnmpAuthAlgorithm] = None,
4880
        community: Optional[str] = None,
4881
        privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None,
4882
        privacy_password: Optional[str] = None
4883
    ) -> Any:
4884
        """Modifies an existing credential.
4885
4886
        Arguments:
4887
            credential_id: UUID of the credential
4888
            name: Name of the credential
4889
            comment: Comment for the credential
4890
            allow_insecure: Whether to allow insecure use of the credential
4891
            certificate: Certificate for the credential
4892
            key_phrase: Key passphrase for the private key
4893
            private_key: Private key to use for login
4894
            login: Username for the credential
4895
            password: Password for the credential
4896
            auth_algorithm: The SNMP auth algorithm.
4897
            community: The SNMP community
4898
            privacy_algorithm: The SNMP privacy algorithm.
4899
            privacy_password: The SNMP privacy password
4900
4901
        Returns:
4902
            The response. See :py:meth:`send_command` for details.
4903
        """
4904
        if not credential_id:
4905
            raise RequiredArgument(
4906
                function=self.modify_credential.__name__,
4907
                argument='credential_id attribute',
4908
            )
4909
4910
        cmd = XmlCommand("modify_credential")
4911
        cmd.set_attribute("credential_id", credential_id)
4912
4913
        if comment:
4914
            cmd.add_element("comment", comment)
4915
4916
        if name:
4917
            cmd.add_element("name", name)
4918
4919
        if allow_insecure is not None:
4920
            cmd.add_element("allow_insecure", _to_bool(allow_insecure))
4921
4922
        if certificate:
4923
            cmd.add_element("certificate", certificate)
4924
4925
        if key_phrase is not None and not private_key:
4926
            raise RequiredArgument(
4927
                function=self.modify_credential.__name__, argument='private_key'
4928
            )
4929
4930
        if private_key:
4931
            _xmlkey = cmd.add_element("key")
4932
            _xmlkey.add_element("private", private_key)
4933
4934
            if key_phrase is not None:
4935
                _xmlkey.add_element("phrase", key_phrase)
4936
4937
        if login:
4938
            cmd.add_element("login", login)
4939
4940
        if password:
4941
            cmd.add_element("password", password)
4942
4943
        if auth_algorithm is not None:
4944
            if not isinstance(auth_algorithm, SnmpAuthAlgorithm):
4945
                raise InvalidArgumentType(
4946
                    function=self.modify_credential.__name__,
4947
                    argument='auth_algorithm',
4948
                    arg_type=SnmpAuthAlgorithm.__name__,
4949
                )
4950
            cmd.add_element("auth_algorithm", auth_algorithm.value)
4951
4952
        if community:
4953
            cmd.add_element("community", community)
4954
4955
        if privacy_algorithm is not None:
4956
            if not isinstance(privacy_algorithm, SnmpPrivacyAlgorithm):
4957
                raise InvalidArgumentType(
4958
                    function=self.modify_credential.__name__,
4959
                    argument='privacy_algorithm',
4960
                    arg_type=SnmpPrivacyAlgorithm.__name__,
4961
                )
4962
4963
            _xmlprivacy = cmd.add_element("privacy")
4964
            _xmlprivacy.add_element("algorithm", privacy_algorithm.value)
4965
4966
            if privacy_password is not None:
4967
                _xmlprivacy.add_element("password", privacy_password)
4968
4969
        return self._send_xml_command(cmd)
4970
4971 View Code Duplication
    def modify_filter(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4972
        self,
4973
        filter_id: str,
4974
        *,
4975
        comment: Optional[str] = None,
4976
        name: Optional[str] = None,
4977
        term: Optional[str] = None,
4978
        filter_type: Optional[FilterType] = None
4979
    ) -> Any:
4980
        """Modifies an existing filter.
4981
4982
        Arguments:
4983
            filter_id: UUID of the filter to be modified
4984
            comment: Comment on filter.
4985
            name: Name of filter.
4986
            term: Filter term.
4987
            filter_type: Filter type the filter applies to.
4988
4989
        Returns:
4990
            The response. See :py:meth:`send_command` for details.
4991
        """
4992
        if not filter_id:
4993
            raise RequiredArgument(
4994
                function=self.modify_filter.__name__, argument='filter_id'
4995
            )
4996
4997
        cmd = XmlCommand("modify_filter")
4998
        cmd.set_attribute("filter_id", filter_id)
4999
5000
        if comment:
5001
            cmd.add_element("comment", comment)
5002
5003
        if name:
5004
            cmd.add_element("name", name)
5005
5006
        if term:
5007
            cmd.add_element("term", term)
5008
5009
        if filter_type:
5010
            if not isinstance(filter_type, self.types.FilterType):
5011
                raise InvalidArgumentType(
5012
                    function=self.modify_filter.__name__,
5013
                    argument='filter_type',
5014
                    arg_type=self.types.FilterType.__name__,
5015
                )
5016
            cmd.add_element("type", filter_type.value)
5017
5018
        return self._send_xml_command(cmd)
5019
5020 View Code Duplication
    def modify_group(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5021
        self,
5022
        group_id: str,
5023
        *,
5024
        comment: Optional[str] = None,
5025
        name: Optional[str] = None,
5026
        users: Optional[List[str]] = None
5027
    ) -> Any:
5028
        """Modifies an existing group.
5029
5030
        Arguments:
5031
            group_id: UUID of group to modify.
5032
            comment: Comment on group.
5033
            name: Name of group.
5034
            users: List of user names to be in the group
5035
5036
        Returns:
5037
            The response. See :py:meth:`send_command` for details.
5038
        """
5039
        if not group_id:
5040
            raise RequiredArgument(
5041
                function=self.modify_group.__name__, argument='group_id'
5042
            )
5043
5044
        cmd = XmlCommand("modify_group")
5045
        cmd.set_attribute("group_id", group_id)
5046
5047
        if comment:
5048
            cmd.add_element("comment", comment)
5049
5050
        if name:
5051
            cmd.add_element("name", name)
5052
5053
        if users:
5054
            cmd.add_element("users", _to_comma_list(users))
5055
5056
        return self._send_xml_command(cmd)
5057
5058 View Code Duplication
    def modify_note(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5059
        self,
5060
        note_id: str,
5061
        text: str,
5062
        *,
5063
        days_active: Optional[int] = None,
5064
        hosts: Optional[List[str]] = None,
5065
        port: Optional[int] = None,
5066
        result_id: Optional[str] = None,
5067
        severity: Optional[Severity] = None,
5068
        task_id: Optional[str] = None,
5069
        threat: Optional[SeverityLevel] = None
5070
    ) -> Any:
5071
        """Modifies an existing note.
5072
5073
        Arguments:
5074
            note_id: UUID of note to modify.
5075
            text: The text of the note.
5076
            days_active: Days note will be active. -1 on always, 0 off.
5077
            hosts: A list of hosts addresses
5078
            port: Port to which note applies.
5079
            result_id: Result to which note applies.
5080
            severity: Severity to which note applies.
5081
            task_id: Task to which note applies.
5082
            threat: Threat level to which note applies. Will be converted to
5083
                severity.
5084
5085
        Returns:
5086
            The response. See :py:meth:`send_command` for details.
5087
        """
5088
        if not note_id:
5089
            raise RequiredArgument(
5090
                function=self.modify_note.__name__, argument='note_id'
5091
            )
5092
5093
        if not text:
5094
            raise RequiredArgument(
5095
                function=self.modify_note.__name__, argument='text'
5096
            )
5097
5098
        cmd = XmlCommand("modify_note")
5099
        cmd.set_attribute("note_id", note_id)
5100
        cmd.add_element("text", text)
5101
5102
        if days_active is not None:
5103
            cmd.add_element("active", str(days_active))
5104
5105
        if hosts:
5106
            cmd.add_element("hosts", _to_comma_list(hosts))
5107
5108
        if port:
5109
            cmd.add_element("port", str(port))
5110
5111
        if result_id:
5112
            cmd.add_element("result", attrs={"id": result_id})
5113
5114
        if severity:
5115
            cmd.add_element("severity", str(severity))
5116
5117
        if task_id:
5118
            cmd.add_element("task", attrs={"id": task_id})
5119
5120
        if threat is not None:
5121
5122
            if not isinstance(threat, SeverityLevel):
5123
                raise InvalidArgumentType(
5124
                    function=self.modify_note.__name__,
5125
                    argument='threat',
5126
                    arg_type=SeverityLevel.__name__,
5127
                )
5128
5129
            cmd.add_element("threat", threat.value)
5130
5131
        return self._send_xml_command(cmd)
5132
5133 View Code Duplication
    def modify_override(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5134
        self,
5135
        override_id: str,
5136
        text: str,
5137
        *,
5138
        days_active: Optional[int] = None,
5139
        hosts: Optional[List[str]] = None,
5140
        port: Optional[int] = None,
5141
        result_id: Optional[str] = None,
5142
        severity: Optional[Severity] = None,
5143
        new_severity: Optional[Severity] = None,
5144
        task_id: Optional[str] = None,
5145
        threat: Optional[SeverityLevel] = None,
5146
        new_threat: Optional[SeverityLevel] = None
5147
    ) -> Any:
5148
        """Modifies an existing override.
5149
5150
        Arguments:
5151
            override_id: UUID of override to modify.
5152
            text: The text of the override.
5153
            days_active: Days override will be active. -1 on always,
5154
                0 off.
5155
            hosts: A list of host addresses
5156
            port: Port to which override applies.
5157
            result_id: Result to which override applies.
5158
            severity: Severity to which override applies.
5159
            new_severity: New severity score for result.
5160
            task_id: Task to which override applies.
5161
            threat: Threat level to which override applies.
5162
                Will be converted to severity.
5163
            new_threat: New threat level for results. Will be converted to
5164
                new_severity.
5165
5166
        Returns:
5167
            The response. See :py:meth:`send_command` for details.
5168
        """
5169
        if not override_id:
5170
            raise RequiredArgument(
5171
                function=self.modify_override.__name__, argument='override_id'
5172
            )
5173
        if not text:
5174
            raise RequiredArgument(
5175
                function=self.modify_override.__name__, argument='text'
5176
            )
5177
5178
        cmd = XmlCommand("modify_override")
5179
        cmd.set_attribute("override_id", override_id)
5180
        cmd.add_element("text", text)
5181
5182
        if days_active is not None:
5183
            cmd.add_element("active", str(days_active))
5184
5185
        if hosts:
5186
            cmd.add_element("hosts", _to_comma_list(hosts))
5187
5188
        if port:
5189
            cmd.add_element("port", str(port))
5190
5191
        if result_id:
5192
            cmd.add_element("result", attrs={"id": result_id})
5193
5194
        if severity:
5195
            cmd.add_element("severity", str(severity))
5196
5197
        if new_severity:
5198
            cmd.add_element("new_severity", str(new_severity))
5199
5200
        if task_id:
5201
            cmd.add_element("task", attrs={"id": task_id})
5202
5203
        if threat is not None:
5204
            if not isinstance(threat, SeverityLevel):
5205
                raise InvalidArgumentType(
5206
                    function=self.modify_override.__name__,
5207
                    argument='threat',
5208
                    arg_type=SeverityLevel.__name__,
5209
                )
5210
            cmd.add_element("threat", threat.value)
5211
5212
        if new_threat is not None:
5213
            if not isinstance(new_threat, SeverityLevel):
5214
                raise InvalidArgumentType(
5215
                    function=self.modify_override.__name__,
5216
                    argument='new_threat',
5217
                    arg_type=SeverityLevel.__name__,
5218
                )
5219
5220
            cmd.add_element("new_threat", new_threat.value)
5221
5222
        return self._send_xml_command(cmd)
5223
5224
    def modify_permission(
5225
        self,
5226
        permission_id: str,
5227
        *,
5228
        comment: Optional[str] = None,
5229
        name: Optional[str] = None,
5230
        resource_id: Optional[str] = None,
5231
        resource_type: Optional[EntityType] = None,
5232
        subject_id: Optional[str] = None,
5233
        subject_type: Optional[PermissionSubjectType] = None
5234
    ) -> Any:
5235
        """Modifies an existing permission.
5236
5237
        Arguments:
5238
            permission_id: UUID of permission to be modified.
5239
            comment: The comment on the permission.
5240
            name: Permission name, currently the name of a command.
5241
            subject_id: UUID of subject to whom the permission is granted
5242
            subject_type: Type of the subject user, group or role
5243
            resource_id: UUID of entity to which the permission applies
5244
            resource_type: Type of the resource. For Super permissions user,
5245
                group or role
5246
5247
        Returns:
5248
            The response. See :py:meth:`send_command` for details.
5249
        """
5250
        if not permission_id:
5251
            raise RequiredArgument(
5252
                function=self.modify_permission.__name__,
5253
                argument='permission_id',
5254
            )
5255
5256
        cmd = XmlCommand("modify_permission")
5257
        cmd.set_attribute("permission_id", permission_id)
5258
5259
        if comment:
5260
            cmd.add_element("comment", comment)
5261
5262
        if name:
5263
            cmd.add_element("name", name)
5264
5265 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...
5266
            if not resource_id:
5267
                raise RequiredArgument(
5268
                    function=self.modify_permission.__name__,
5269
                    argument='resource_id',
5270
                )
5271
5272
            if not resource_type:
5273
                raise RequiredArgument(
5274
                    function=self.modify_permission.__name__,
5275
                    argument='resource_type',
5276
                )
5277
5278
            if not isinstance(resource_type, self.types.EntityType):
5279
                raise InvalidArgumentType(
5280
                    function=self.modify_permission.__name__,
5281
                    argument='resource_type',
5282
                    arg_type=self.types.EntityType.__name__,
5283
                )
5284
5285
            _xmlresource = cmd.add_element(
5286
                "resource", attrs={"id": resource_id}
5287
            )
5288
            _xmlresource.add_element("type", resource_type.value)
5289
5290
        if subject_id or subject_type:
5291
            if not subject_id:
5292
                raise RequiredArgument(
5293
                    function=self.modify_permission.__name__,
5294
                    argument='subject_id',
5295
                )
5296
5297
            if not isinstance(subject_type, PermissionSubjectType):
5298
                raise InvalidArgumentType(
5299
                    function=self.modify_permission.__name__,
5300
                    argument='subject_type',
5301
                    arg_type=PermissionSubjectType.__name__,
5302
                )
5303
5304
            _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id})
5305
            _xmlsubject.add_element("type", subject_type.value)
5306
5307
        return self._send_xml_command(cmd)
5308
5309
    def modify_port_list(
5310
        self,
5311
        port_list_id: str,
5312
        *,
5313
        comment: Optional[str] = None,
5314
        name: Optional[str] = None
5315
    ) -> Any:
5316
        """Modifies an existing port list.
5317
5318
        Arguments:
5319
            port_list_id: UUID of port list to modify.
5320
            name: Name of port list.
5321
            comment: Comment on port list.
5322
5323
        Returns:
5324
            The response. See :py:meth:`send_command` for details.
5325
        """
5326
        if not port_list_id:
5327
            raise RequiredArgument(
5328
                function=self.modify_port_list.__name__, argument='port_list_id'
5329
            )
5330
        cmd = XmlCommand("modify_port_list")
5331
        cmd.set_attribute("port_list_id", port_list_id)
5332
5333
        if comment:
5334
            cmd.add_element("comment", comment)
5335
5336
        if name:
5337
            cmd.add_element("name", name)
5338
5339
        return self._send_xml_command(cmd)
5340
5341
    def modify_report_format(
5342
        self,
5343
        report_format_id: str,
5344
        *,
5345
        active: Optional[bool] = None,
5346
        name: Optional[str] = None,
5347
        summary: Optional[str] = None,
5348
        param_name: Optional[str] = None,
5349
        param_value: Optional[str] = None
5350
    ) -> Any:
5351
        """Modifies an existing report format.
5352
5353
        Arguments:
5354
            report_format_id: UUID of report format to modify.
5355
            active: Whether the report format is active.
5356
            name: The name of the report format.
5357
            summary: A summary of the report format.
5358
            param_name: The name of the param.
5359
            param_value: The value of the param.
5360
5361
        Returns:
5362
            The response. See :py:meth:`send_command` for details.
5363
        """
5364
        if not report_format_id:
5365
            raise RequiredArgument(
5366
                function=self.modify_report_format.__name__,
5367
                argument='report_format_id',
5368
            )
5369
5370
        cmd = XmlCommand("modify_report_format")
5371
        cmd.set_attribute("report_format_id", report_format_id)
5372
5373
        if active is not None:
5374
            cmd.add_element("active", _to_bool(active))
5375
5376
        if name:
5377
            cmd.add_element("name", name)
5378
5379
        if summary:
5380
            cmd.add_element("summary", summary)
5381
5382
        if param_name:
5383
            _xmlparam = cmd.add_element("param")
5384
            _xmlparam.add_element("name", param_name)
5385
5386
            if param_value is not None:
5387
                _xmlparam.add_element("value", param_value)
5388
5389
        return self._send_xml_command(cmd)
5390
5391 View Code Duplication
    def modify_role(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5392
        self,
5393
        role_id: str,
5394
        *,
5395
        comment: Optional[str] = None,
5396
        name: Optional[str] = None,
5397
        users: Optional[List[str]] = None
5398
    ) -> Any:
5399
        """Modifies an existing role.
5400
5401
        Arguments:
5402
            role_id: UUID of role to modify.
5403
            comment: Name of role.
5404
            name: Comment on role.
5405
            users: List of user names.
5406
5407
        Returns:
5408
            The response. See :py:meth:`send_command` for details.
5409
        """
5410
        if not role_id:
5411
            raise RequiredArgument(
5412
                function=self.modify_role.__name__, argument='role_id argument'
5413
            )
5414
5415
        cmd = XmlCommand("modify_role")
5416
        cmd.set_attribute("role_id", role_id)
5417
5418
        if comment:
5419
            cmd.add_element("comment", comment)
5420
5421
        if name:
5422
            cmd.add_element("name", name)
5423
5424
        if users:
5425
            cmd.add_element("users", _to_comma_list(users))
5426
5427
        return self._send_xml_command(cmd)
5428
5429
    def modify_scanner(
5430
        self,
5431
        scanner_id: str,
5432
        *,
5433
        scanner_type: Optional[ScannerType] = None,
5434
        host: Optional[str] = None,
5435
        port: Optional[int] = None,
5436
        comment: Optional[str] = None,
5437
        name: Optional[str] = None,
5438
        ca_pub: Optional[str] = None,
5439
        credential_id: Optional[str] = None
5440
    ) -> Any:
5441
        """Modifies an existing scanner.
5442
5443
        Arguments:
5444
            scanner_id: UUID of scanner to modify.
5445
            scanner_type: New type of the Scanner.
5446
            host: Host of the scanner.
5447
            port: Port of the scanner.
5448
            comment: Comment on scanner.
5449
            name: Name of scanner.
5450
            ca_pub: Certificate of CA to verify scanner's certificate.
5451
            credential_id: UUID of the client certificate credential for the
5452
                Scanner.
5453
5454
        Returns:
5455
            The response. See :py:meth:`send_command` for details.
5456
        """
5457
        if not scanner_id:
5458
            raise RequiredArgument(
5459
                function=self.modify_scanner.__name__,
5460
                argument='scanner_id argument',
5461
            )
5462
5463
        cmd = XmlCommand("modify_scanner")
5464
        cmd.set_attribute("scanner_id", scanner_id)
5465
5466
        if scanner_type is not None:
5467
            if not isinstance(scanner_type, self.types.ScannerType):
5468
                raise InvalidArgumentType(
5469
                    function=self.modify_scanner.__name__,
5470
                    argument='scanner_type',
5471
                    arg_type=self.types.ScannerType.__name__,
5472
                )
5473
5474
            cmd.add_element("type", scanner_type.value)
5475
5476
        if host:
5477
            cmd.add_element("host", host)
5478
5479
        if port:
5480
            cmd.add_element("port", str(port))
5481
5482
        if comment:
5483
            cmd.add_element("comment", comment)
5484
5485
        if name:
5486
            cmd.add_element("name", name)
5487
5488
        if ca_pub:
5489
            cmd.add_element("ca_pub", ca_pub)
5490
5491
        if credential_id:
5492
            cmd.add_element("credential", attrs={"id": str(credential_id)})
5493
5494
        return self._send_xml_command(cmd)
5495
5496 View Code Duplication
    def modify_schedule(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5497
        self,
5498
        schedule_id: str,
5499
        *,
5500
        comment: Optional[str] = None,
5501
        name: Optional[str] = None,
5502
        first_time_minute: Optional[int] = None,
5503
        first_time_hour: Optional[int] = None,
5504
        first_time_day_of_month: Optional[int] = None,
5505
        first_time_month: Optional[int] = None,
5506
        first_time_year: Optional[int] = None,
5507
        duration: Optional[int] = None,
5508
        duration_unit: Optional[TimeUnit] = None,
5509
        period: Optional[int] = None,
5510
        period_unit: Optional[TimeUnit] = None,
5511
        timezone: Optional[str] = None
5512
    ) -> Any:
5513
        """Modifies an existing schedule.
5514
5515
        Arguments:
5516
            schedule_id: UUID of schedule to modify.
5517
            name: Name of the schedule
5518
            comment: Comment for the schedule
5519
            first_time_minute: First time minute the schedule will run. Must be
5520
                an integer >= 0.
5521
            first_time_hour: First time hour the schedule will run. Must be an
5522
                integer >= 0.
5523
            first_time_day_of_month: First time day of month the schedule will
5524
                run. Must be an integer > 0 <= 31.
5525
            first_time_month: First time month the schedule will run. Must be an
5526
                integer >= 1 <= 12.
5527
            first_time_year: First time year the schedule will run
5528
            duration: How long the Manager will run the scheduled task for until
5529
                it gets paused if not finished yet.
5530
            duration_unit: Unit of the duration. One of second, minute, hour,
5531
                day, week, month, year, decade. Required if duration is set.
5532
            period: How often the Manager will repeat the scheduled task. Must
5533
                be an integer > 0.
5534
            period_unit: Unit of the period. One of second, minute, hour, day,
5535
                week, month, year, decade. Required if period is set.
5536
            timezone: The timezone the schedule will follow
5537
5538
        Returns:
5539
            The response. See :py:meth:`send_command` for details.
5540
        """
5541
        if not schedule_id:
5542
            raise RequiredArgument(
5543
                function=self.modify_schedule.__name__, argument='schedule_id'
5544
            )
5545
5546
        cmd = XmlCommand("modify_schedule")
5547
        cmd.set_attribute("schedule_id", schedule_id)
5548
5549
        if comment:
5550
            cmd.add_element("comment", comment)
5551
5552
        if name:
5553
            cmd.add_element("name", name)
5554
5555
        if (
5556
            first_time_minute is not None
5557
            or first_time_hour is not None
5558
            or first_time_day_of_month is not None
5559
            or first_time_month is not None
5560
            or first_time_year is not None
5561
        ):
5562
5563
            if first_time_minute is None:
5564
                raise RequiredArgument(
5565
                    function=self.modify_schedule.__name__,
5566
                    argument='first_time_minute',
5567
                )
5568
            elif (
5569
                not isinstance(first_time_minute, numbers.Integral)
5570
                or first_time_minute < 0
5571
            ):
5572
                raise InvalidArgument(
5573
                    "first_time_minute argument of modify_schedule needs to be "
5574
                    "an integer greater or equal 0"
5575
                )
5576
5577
            if first_time_hour is None:
5578
                raise RequiredArgument(
5579
                    function=self.modify_schedule.__name__,
5580
                    argument='first_time_hour',
5581
                )
5582
            elif (
5583
                not isinstance(first_time_hour, numbers.Integral)
5584
                or first_time_hour < 0
5585
            ):
5586
                raise InvalidArgument(
5587
                    "first_time_hour argument of modify_schedule needs to be "
5588
                    "an integer greater or equal 0"
5589
                )
5590
5591
            if first_time_day_of_month is None:
5592
                raise RequiredArgument(
5593
                    function=self.modify_schedule.__name__,
5594
                    argument='first_time_day_of_month',
5595
                )
5596
            elif (
5597
                not isinstance(first_time_day_of_month, numbers.Integral)
5598
                or first_time_day_of_month < 1
5599
                or first_time_day_of_month > 31
5600
            ):
5601
                raise InvalidArgument(
5602
                    "first_time_day_of_month argument of modify_schedule needs "
5603
                    "to be an integer between 1 and 31"
5604
                )
5605
5606
            if first_time_month is None:
5607
                raise RequiredArgument(
5608
                    function=self.modify_schedule.__name__,
5609
                    argument='first_time_month',
5610
                )
5611
            elif (
5612
                not isinstance(first_time_month, numbers.Integral)
5613
                or first_time_month < 1
5614
                or first_time_month > 12
5615
            ):
5616
                raise InvalidArgument(
5617
                    "first_time_month argument of modify_schedule needs "
5618
                    "to be an integer between 1 and 12"
5619
                )
5620
5621
            if first_time_year is None:
5622
                raise RequiredArgument(
5623
                    function=self.modify_schedule.__name__,
5624
                    argument='first_time_year',
5625
                )
5626
            elif (
5627
                not isinstance(first_time_year, numbers.Integral)
5628
                or first_time_year < 1970
5629
            ):
5630
                raise InvalidArgument(
5631
                    "first_time_year argument of create_schedule needs "
5632
                    "to be an integer greater or equal 1970"
5633
                )
5634
5635
            _xmlftime = cmd.add_element("first_time")
5636
            _xmlftime.add_element("minute", str(first_time_minute))
5637
            _xmlftime.add_element("hour", str(first_time_hour))
5638
            _xmlftime.add_element("day_of_month", str(first_time_day_of_month))
5639
            _xmlftime.add_element("month", str(first_time_month))
5640
            _xmlftime.add_element("year", str(first_time_year))
5641
5642
        if duration is not None:
5643
            if not duration_unit:
5644
                raise RequiredArgument(
5645
                    function=self.modify_schedule.__name__,
5646
                    argument='duration_unit',
5647
                )
5648
5649
            if not isinstance(duration_unit, TimeUnit):
5650
                raise InvalidArgumentType(
5651
                    function=self.modify_schedule.__name__,
5652
                    argument='duration_unit',
5653
                    arg_type=TimeUnit.__name__,
5654
                )
5655
5656
            if not isinstance(duration, numbers.Integral) or duration < 1:
5657
                raise InvalidArgument(
5658
                    "duration argument must be an integer greater than 0"
5659
                )
5660
5661
            _xmlduration = cmd.add_element("duration", str(duration))
5662
            _xmlduration.add_element("unit", duration_unit.value)
5663
5664
        if period is not None:
5665
            if not period_unit:
5666
                raise RequiredArgument(
5667
                    function=self.modify_schedule.__name__,
5668
                    argument='period_unit',
5669
                )
5670
5671
            if not isinstance(period_unit, TimeUnit):
5672
                raise InvalidArgumentType(
5673
                    function=self.modify_schedule.__name__,
5674
                    argument='period_unit',
5675
                    arg_type=TimeUnit.__name__,
5676
                )
5677
5678
            if not isinstance(period, numbers.Integral) or period < 1:
5679
                raise InvalidArgument(
5680
                    "period argument must be an integer greater than 0"
5681
                )
5682
5683
            _xmlperiod = cmd.add_element("period", str(period))
5684
            _xmlperiod.add_element("unit", period_unit.value)
5685
5686
        if timezone:
5687
            cmd.add_element("timezone", timezone)
5688
5689
        return self._send_xml_command(cmd)
5690
5691
    def modify_setting(
5692
        self,
5693
        setting_id: Optional[str] = None,
5694
        name: Optional[str] = None,
5695
        value: Optional[str] = None,
5696
    ) -> Any:
5697
        """Modifies an existing setting.
5698
5699
        Arguments:
5700
            setting_id: UUID of the setting to be changed.
5701
            name: The name of the setting. Either setting_id or name must be
5702
                passed.
5703
            value: The value of the setting.
5704
5705
        Returns:
5706
            The response. See :py:meth:`send_command` for details.
5707
        """
5708
        if not setting_id and not name:
5709
            raise RequiredArgument(
5710
                function=self.modify_setting.__name__,
5711
                argument='setting_id or name argument',
5712
            )
5713
5714
        if value is None:
5715
            raise RequiredArgument(
5716
                function=self.modify_setting.__name__, argument='value argument'
5717
            )
5718
5719
        cmd = XmlCommand("modify_setting")
5720
5721
        if setting_id:
5722
            cmd.set_attribute("setting_id", setting_id)
5723
        else:
5724
            cmd.add_element("name", name)
5725
5726
        cmd.add_element("value", _to_base64(value))
5727
5728
        return self._send_xml_command(cmd)
5729
5730
    def modify_tag(
5731
        self,
5732
        tag_id: str,
5733
        *,
5734
        comment: Optional[str] = None,
5735
        name: Optional[str] = None,
5736
        value: Optional[str] = None,
5737
        active: Optional[bool] = None,
5738
        resource_id: Optional[str] = None,
5739
        resource_type: Optional[EntityType] = None
5740
    ) -> Any:
5741
        """Modifies an existing tag.
5742
5743
        Arguments:
5744
            tag_id: UUID of the tag.
5745
            comment: Comment to add to the tag.
5746
            name: Name of the tag.
5747
            value: Value of the tag.
5748
            active: Whether the tag is active.
5749
            resource_id: ID of the resource to which to attach the tag.
5750
                Required if resource_type is set.
5751
            resource_type: Type of the resource to which to attach the tag.
5752
                Required if resource_id is set.
5753
5754
        Returns:
5755
            The response. See :py:meth:`send_command` for details.
5756
        """
5757
        if not tag_id:
5758
            raise RequiredArgument(
5759
                function=self.modify_tag.__name__, argument='tag_id'
5760
            )
5761
5762
        cmd = XmlCommand("modify_tag")
5763
        cmd.set_attribute("tag_id", str(tag_id))
5764
5765
        if comment:
5766
            cmd.add_element("comment", comment)
5767
5768
        if name:
5769
            cmd.add_element("name", name)
5770
5771
        if value:
5772
            cmd.add_element("value", value)
5773
5774
        if active is not None:
5775
            cmd.add_element("active", _to_bool(active))
5776
5777 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...
5778
            if not resource_id:
5779
                raise RequiredArgument(
5780
                    function=self.modify_tag.__name__, argument='resource_id'
5781
                )
5782
5783
            if not resource_type:
5784
                raise RequiredArgument(
5785
                    function=self.modify_tag.__name__, argument='resource_type'
5786
                )
5787
5788
            if not isinstance(resource_type, self.types.EntityType):
5789
                raise InvalidArgumentType(
5790
                    function=self.modify_tag.__name__,
5791
                    argument='resource_type',
5792
                    arg_type=self.types.EntityType.__name__,
5793
                )
5794
5795
            _xmlresource = cmd.add_element(
5796
                "resource", attrs={"id": resource_id}
5797
            )
5798
            _xmlresource.add_element("type", resource_type.value)
5799
5800
        return self._send_xml_command(cmd)
5801
5802
    def modify_target(
5803
        self,
5804
        target_id: str,
5805
        *,
5806
        name: Optional[str] = None,
5807
        comment: Optional[str] = None,
5808
        hosts: Optional[List[str]] = None,
5809
        exclude_hosts: Optional[List[str]] = None,
5810
        ssh_credential_id: Optional[str] = None,
5811
        ssh_credential_port: Optional[bool] = None,
5812
        smb_credential_id: Optional[str] = None,
5813
        esxi_credential_id: Optional[str] = None,
5814
        snmp_credential_id: Optional[str] = None,
5815
        alive_test: Optional[AliveTest] = None,
5816
        reverse_lookup_only: Optional[bool] = None,
5817
        reverse_lookup_unify: Optional[bool] = None,
5818
        port_list_id: Optional[str] = None
5819
    ) -> Any:
5820
        """Modifies an existing target.
5821
5822
        Arguments:
5823
            target_id: ID of target to modify.
5824
            comment: Comment on target.
5825
            name: Name of target.
5826
            hosts: List of target hosts.
5827
            exclude_hosts: A list of hosts to exclude.
5828
            ssh_credential_id: UUID of SSH credential to use on target.
5829
            ssh_credential_port: The port to use for ssh credential
5830
            smb_credential_id: UUID of SMB credential to use on target.
5831
            esxi_credential_id: UUID of ESXi credential to use on target.
5832
            snmp_credential_id: UUID of SNMP credential to use on target.
5833
            port_list_id: UUID of port list describing ports to scan.
5834
            alive_test: Which alive tests to use.
5835
            reverse_lookup_only: Whether to scan only hosts that have names.
5836
            reverse_lookup_unify: Whether to scan only one IP when multiple IPs
5837
                have the same name.
5838
5839
        Returns:
5840
            The response. See :py:meth:`send_command` for details.
5841
        """
5842
        if not target_id:
5843
            raise RequiredArgument(
5844
                function=self.modify_target.__name__, argument='target_id'
5845
            )
5846
5847
        cmd = XmlCommand("modify_target")
5848
        cmd.set_attribute("target_id", target_id)
5849
5850
        if comment:
5851
            cmd.add_element("comment", comment)
5852
5853
        if name:
5854
            cmd.add_element("name", name)
5855
5856
        if hosts:
5857
            cmd.add_element("hosts", _to_comma_list(hosts))
5858
            if exclude_hosts is None:
5859
                exclude_hosts = ['']
5860
5861
        if exclude_hosts:
5862
            cmd.add_element("exclude_hosts", _to_comma_list(exclude_hosts))
5863
5864
        if alive_test:
5865
            if not isinstance(alive_test, AliveTest):
5866
                raise InvalidArgumentType(
5867
                    function=self.modify_target.__name__,
5868
                    argument='alive_test',
5869
                    arg_type=AliveTest.__name__,
5870
                )
5871
            cmd.add_element("alive_tests", alive_test.value)
5872
5873
        if ssh_credential_id:
5874
            _xmlssh = cmd.add_element(
5875
                "ssh_credential", attrs={"id": ssh_credential_id}
5876
            )
5877
5878
            if ssh_credential_port:
5879
                _xmlssh.add_element("port", str(ssh_credential_port))
5880
5881
        if smb_credential_id:
5882
            cmd.add_element("smb_credential", attrs={"id": smb_credential_id})
5883
5884
        if esxi_credential_id:
5885
            cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id})
5886
5887
        if snmp_credential_id:
5888
            cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id})
5889
5890
        if reverse_lookup_only is not None:
5891
            cmd.add_element(
5892
                "reverse_lookup_only", _to_bool(reverse_lookup_only)
5893
            )
5894
5895
        if reverse_lookup_unify is not None:
5896
            cmd.add_element(
5897
                "reverse_lookup_unify", _to_bool(reverse_lookup_unify)
5898
            )
5899
5900
        if port_list_id:
5901
            cmd.add_element("port_list", attrs={"id": port_list_id})
5902
5903
        return self._send_xml_command(cmd)
5904
5905
    def modify_task(
5906
        self,
5907
        task_id: str,
5908
        *,
5909
        name: Optional[str] = None,
5910
        config_id: Optional[str] = None,
5911
        target_id: Optional[str] = None,
5912
        scanner_id: Optional[str] = None,
5913
        alterable: Optional[bool] = None,
5914
        hosts_ordering: Optional[HostsOrdering] = None,
5915
        schedule_id: Optional[str] = None,
5916
        schedule_periods: Optional[int] = None,
5917
        comment: Optional[str] = None,
5918
        alert_ids: Optional[List[str]] = None,
5919
        observers: Optional[List[str]] = None,
5920
        preferences: Optional[dict] = None
5921
    ) -> Any:
5922
        """Modifies an existing task.
5923
5924
        Arguments:
5925
            task_id: UUID of task to modify.
5926
            name: The name of the task.
5927
            config_id: UUID of scan config to use by the task
5928
            target_id: UUID of target to be scanned
5929
            scanner_id: UUID of scanner to use for scanning the target
5930
            comment: The comment on the task.
5931
            alert_ids: List of UUIDs for alerts to be applied to the task
5932
            hosts_ordering: The order hosts are scanned in
5933
            schedule_id: UUID of a schedule when the task should be run.
5934
            schedule_periods: A limit to the number of times the task will be
5935
                scheduled, or 0 for no limit.
5936
            observers: List of names or ids of users which should be allowed to
5937
                observe this task
5938
            preferences: Name/Value pairs of scanner preferences.
5939
5940
        Returns:
5941
            The response. See :py:meth:`send_command` for details.
5942
        """
5943
        if not task_id:
5944
            raise RequiredArgument(
5945
                function=self.modify_task.__name__, argument='task_id argument'
5946
            )
5947
5948
        cmd = XmlCommand("modify_task")
5949
        cmd.set_attribute("task_id", task_id)
5950
5951
        if name:
5952
            cmd.add_element("name", name)
5953
5954
        if comment:
5955
            cmd.add_element("comment", comment)
5956
5957
        if config_id:
5958
            cmd.add_element("config", attrs={"id": config_id})
5959
5960
        if target_id:
5961
            cmd.add_element("target", attrs={"id": target_id})
5962
5963
        if alterable is not None:
5964
            cmd.add_element("alterable", _to_bool(alterable))
5965
5966
        if hosts_ordering:
5967
            if not isinstance(hosts_ordering, HostsOrdering):
5968
                raise InvalidArgumentType(
5969
                    function=self.modify_task.__name__,
5970
                    argument='hosts_ordering',
5971
                    arg_type=HostsOrdering.__name__,
5972
                )
5973
            cmd.add_element("hosts_ordering", hosts_ordering.value)
5974
5975
        if scanner_id:
5976
            cmd.add_element("scanner", attrs={"id": scanner_id})
5977
5978
        if schedule_id:
5979
            cmd.add_element("schedule", attrs={"id": schedule_id})
5980
5981
        if schedule_periods is not None:
5982
            if (
5983
                not isinstance(schedule_periods, numbers.Integral)
5984
                or schedule_periods < 0
5985
            ):
5986
                raise InvalidArgument(
5987
                    "schedule_periods must be an integer greater or equal "
5988
                    "than 0"
5989
                )
5990
            cmd.add_element("schedule_periods", str(schedule_periods))
5991
5992
        if alert_ids is not None:
5993
            if not _is_list_like(alert_ids):
5994
                raise InvalidArgumentType(
5995
                    function=self.modify_task.__name__,
5996
                    argument='alert_ids',
5997
                    arg_type='list',
5998
                )
5999
6000
            if len(alert_ids) == 0:
6001
                cmd.add_element("alert", attrs={"id": "0"})
6002
            else:
6003
                for alert in alert_ids:
6004
                    cmd.add_element("alert", attrs={"id": str(alert)})
6005
6006
        if observers is not None:
6007
            if not _is_list_like(observers):
6008
                raise InvalidArgumentType(
6009
                    function=self.modify_task.__name__,
6010
                    argument='observers',
6011
                    arg_type='list',
6012
                )
6013
6014
            cmd.add_element("observers", _to_comma_list(observers))
6015
6016
        if preferences is not None:
6017
            if not isinstance(preferences, collections.abc.Mapping):
6018
                raise InvalidArgumentType(
6019
                    function=self.modify_task.__name__,
6020
                    argument='preferences',
6021
                    arg_type=collections.abc.Mapping.__name__,
6022
                )
6023
6024
            _xmlprefs = cmd.add_element("preferences")
6025
            for pref_name, pref_value in preferences.items():
6026
                _xmlpref = _xmlprefs.add_element("preference")
6027
                _xmlpref.add_element("scanner_name", pref_name)
6028
                _xmlpref.add_element("value", str(pref_value))
6029
6030
        return self._send_xml_command(cmd)
6031
6032
    def modify_user(
6033
        self,
6034
        user_id: str = None,
6035
        name: str = None,
6036
        *,
6037
        new_name: Optional[str] = None,
6038
        password: Optional[str] = None,
6039
        role_ids: Optional[List[str]] = None,
6040
        hosts: Optional[List[str]] = None,
6041
        hosts_allow: Optional[bool] = False,
6042
        ifaces: Optional[List[str]] = None,
6043
        ifaces_allow: Optional[bool] = False
6044
    ) -> Any:
6045
        """Modifies an existing user.
6046
6047
        Arguments:
6048
            user_id: UUID of the user to be modified. Overrides name element
6049
                argument.
6050
            name: The name of the user to be modified. Either user_id or name
6051
                must be passed.
6052
            new_name: The new name for the user.
6053
            password: The password for the user.
6054
            roles_id: List of roles UUIDs for the user.
6055
            hosts: User access rules: List of hosts.
6056
            hosts_allow: If True, allow only listed, otherwise forbid listed.
6057
            ifaces: User access rules: List of ifaces.
6058
            ifaces_allow: If True, allow only listed, otherwise forbid listed.
6059
6060
        Returns:
6061
            The response. See :py:meth:`send_command` for details.
6062
        """
6063
        if not user_id and not name:
6064
            raise RequiredArgument(
6065
                function=self.modify_user.__name__, argument='user_id or name'
6066
            )
6067
6068
        cmd = XmlCommand("modify_user")
6069
6070
        if user_id:
6071
            cmd.set_attribute("user_id", user_id)
6072
        else:
6073
            cmd.add_element("name", name)
6074
6075
        if new_name:
6076
            cmd.add_element("new_name", new_name)
6077
6078
        if password:
6079
            cmd.add_element("password", password)
6080
6081
        if role_ids:
6082
            for role in role_ids:
6083
                cmd.add_element("role", attrs={"id": role})
6084
6085
        if hosts:
6086
            cmd.add_element(
6087
                "hosts",
6088
                _to_comma_list(hosts),
6089
                attrs={"allow": _to_bool(hosts_allow)},
6090
            )
6091
6092
        if ifaces:
6093
            cmd.add_element(
6094
                "ifaces",
6095
                _to_comma_list(ifaces),
6096
                attrs={"allow": _to_bool(ifaces_allow)},
6097
            )
6098
6099
        return self._send_xml_command(cmd)
6100
6101
    def move_task(self, task_id: str, *, slave_id: Optional[str] = None) -> Any:
6102
        """Move an existing task to another GMP slave scanner or the master
6103
6104
        Arguments:
6105
            task_id: UUID of the task to be moved
6106
            slave_id: UUID of slave to reassign the task to, empty for master.
6107
6108
        Returns:
6109
            The response. See :py:meth:`send_command` for details.
6110
        """
6111
        if not task_id:
6112
            raise RequiredArgument(
6113
                function=self.move_task.__name__, argument='task_id'
6114
            )
6115
6116
        cmd = XmlCommand("move_task")
6117
        cmd.set_attribute("task_id", task_id)
6118
6119
        if slave_id is not None:
6120
            cmd.set_attribute("slave_id", slave_id)
6121
6122
        return self._send_xml_command(cmd)
6123
6124
    def restore(self, entity_id: str) -> Any:
6125
        """Restore an entity from the trashcan
6126
6127
        Arguments:
6128
            entity_id: ID of the entity to be restored from the trashcan
6129
6130
        Returns:
6131
            The response. See :py:meth:`send_command` for details.
6132
        """
6133
        if not entity_id:
6134
            raise RequiredArgument(
6135
                function=self.restore.__name__, argument='entity_id'
6136
            )
6137
6138
        cmd = XmlCommand("restore")
6139
        cmd.set_attribute("id", entity_id)
6140
6141
        return self._send_xml_command(cmd)
6142
6143
    def resume_task(self, task_id: str) -> Any:
6144
        """Resume an existing stopped task
6145
6146
        Arguments:
6147
            task_id: UUID of the task to be resumed
6148
6149
        Returns:
6150
            The response. See :py:meth:`send_command` for details.
6151
        """
6152
        if not task_id:
6153
            raise RequiredArgument(
6154
                function=self.resume_task.__name__, argument='task_id'
6155
            )
6156
6157
        cmd = XmlCommand("resume_task")
6158
        cmd.set_attribute("task_id", task_id)
6159
6160
        return self._send_xml_command(cmd)
6161
6162
    def start_task(self, task_id: str) -> Any:
6163
        """Start an existing task
6164
6165
        Arguments:
6166
            task_id: UUID of the task to be started
6167
6168
        Returns:
6169
            The response. See :py:meth:`send_command` for details.
6170
        """
6171
        if not task_id:
6172
            raise RequiredArgument(
6173
                function=self.start_task.__name__, argument='task_id'
6174
            )
6175
6176
        cmd = XmlCommand("start_task")
6177
        cmd.set_attribute("task_id", task_id)
6178
6179
        return self._send_xml_command(cmd)
6180
6181
    def stop_task(self, task_id: str) -> Any:
6182
        """Stop an existing running task
6183
6184
        Arguments:
6185
            task_id: UUID of the task to be stopped
6186
6187
        Returns:
6188
            The response. See :py:meth:`send_command` for details.
6189
        """
6190
        if not task_id:
6191
            raise RequiredArgument(
6192
                function=self.stop_task.__name__, argument='task_id'
6193
            )
6194
6195
        cmd = XmlCommand("stop_task")
6196
        cmd.set_attribute("task_id", task_id)
6197
6198
        return self._send_xml_command(cmd)
6199
6200
    def sync_cert(self) -> Any:
6201
        """Request a synchronization with the CERT feed service
6202
6203
        Returns:
6204
            The response. See :py:meth:`send_command` for details.
6205
        """
6206
        return self._send_xml_command(XmlCommand("sync_cert"))
6207
6208
    def sync_config(self) -> Any:
6209
        """Request an OSP config synchronization with scanner
6210
6211
        Returns:
6212
            The response. See :py:meth:`send_command` for details.
6213
        """
6214
        return self._send_xml_command(XmlCommand("sync_config"))
6215
6216
    def sync_feed(self) -> Any:
6217
        """Request a synchronization with the NVT feed service
6218
6219
        Returns:
6220
            The response. See :py:meth:`send_command` for details.
6221
        """
6222
        return self._send_xml_command(XmlCommand("sync_feed"))
6223
6224
    def sync_scap(self) -> Any:
6225
        """Request a synchronization with the SCAP feed service
6226
6227
        Returns:
6228
            The response. See :py:meth:`send_command` for details.
6229
        """
6230
        return self._send_xml_command(XmlCommand("sync_scap"))
6231
6232
    def test_alert(self, alert_id: str) -> Any:
6233
        """Run an alert
6234
6235
        Invoke a test run of an alert
6236
6237
        Arguments:
6238
            alert_id: UUID of the alert to be tested
6239
6240
        Returns:
6241
            The response. See :py:meth:`send_command` for details.
6242
        """
6243
        if not alert_id:
6244
            raise InvalidArgument("test_alert requires an alert_id argument")
6245
6246
        cmd = XmlCommand("test_alert")
6247
        cmd.set_attribute("alert_id", alert_id)
6248
6249
        return self._send_xml_command(cmd)
6250
6251
    def trigger_alert(
6252
        self,
6253
        alert_id: str,
6254
        report_id: str,
6255
        *,
6256
        filter: Optional[str] = None,
6257
        filter_id: Optional[str] = None,
6258
        report_format_id: Optional[str] = None,
6259
        delta_report_id: Optional[str] = None
6260
    ) -> Any:
6261
        """Run an alert by ignoring its event and conditions
6262
6263
        The alert is triggered to run immediately with the provided filtered
6264
        report by ignoring the even and condition settings.
6265
6266
        Arguments:
6267
            alert_id: UUID of the alert to be run
6268
            report_id: UUID of the report to be provided to the alert
6269
            filter: Filter term to use to filter results in the report
6270
            filter_id: UUID of filter to use to filter results in the report
6271
            report_format_id: UUID of report format to use
6272
            delta_report_id: UUID of an existing report to compare report to.
6273
6274
        Returns:
6275
            The response. See :py:meth:`send_command` for details.
6276
        """
6277
        if not alert_id:
6278
            raise RequiredArgument(
6279
                function=self.trigger_alert.__name__,
6280
                argument='alert_id argument',
6281
            )
6282
6283
        if not report_id:
6284
            raise RequiredArgument(
6285
                function=self.trigger_alert.__name__,
6286
                argument='report_id argument',
6287
            )
6288
6289
        cmd = XmlCommand("get_reports")
6290
        cmd.set_attribute("report_id", report_id)
6291
        cmd.set_attribute("alert_id", alert_id)
6292
6293
        if filter:
6294
            cmd.set_attribute("filter", filter)
6295
6296
        if filter_id:
6297
            cmd.set_attribute("filt_id", filter_id)
6298
6299
        if report_format_id:
6300
            cmd.set_attribute("format_id", report_format_id)
6301
6302
        if delta_report_id:
6303
            cmd.set_attribute("delta_report_id", delta_report_id)
6304
6305
        return self._send_xml_command(cmd)
6306
6307
    def verify_agent(self, agent_id: str) -> Any:
6308
        """Verify an existing agent
6309
6310
        Verifies the trust level of an existing agent. It will be checked
6311
        whether signature of the agent currently matches the agent. This
6312
        includes the agent installer file. It is *not* verified if the agent
6313
        works as expected by the user.
6314
6315
        Arguments:
6316
            agent_id: UUID of the agent to be verified
6317
6318
        Returns:
6319
            The response. See :py:meth:`send_command` for details.
6320
        """
6321
        if not agent_id:
6322
            raise InvalidArgument("verify_agent requires an agent_id argument")
6323
6324
        cmd = XmlCommand("verify_agent")
6325
        cmd.set_attribute("agent_id", agent_id)
6326
6327
        return self._send_xml_command(cmd)
6328
6329
    def verify_report_format(self, report_format_id: str) -> Any:
6330
        """Verify an existing report format
6331
6332
        Verifies the trust level of an existing report format. It will be
6333
        checked whether the signature of the report format currently matches the
6334
        report format. This includes the script and files used to generate
6335
        reports of this format. It is *not* verified if the report format works
6336
        as expected by the user.
6337
6338
        Arguments:
6339
            report_format_id: UUID of the report format to be verified
6340
6341
        Returns:
6342
            The response. See :py:meth:`send_command` for details.
6343
        """
6344
        if not report_format_id:
6345
            raise RequiredArgument(
6346
                function=self.verify_report_format.__name__,
6347
                argument='report_format_id',
6348
            )
6349
6350
        cmd = XmlCommand("verify_report_format")
6351
        cmd.set_attribute("report_format_id", report_format_id)
6352
6353
        return self._send_xml_command(cmd)
6354
6355
    def verify_scanner(self, scanner_id: str) -> Any:
6356
        """Verify an existing scanner
6357
6358
        Verifies if it is possible to connect to an existing scanner. It is
6359
        *not* verified if the scanner works as expected by the user.
6360
6361
        Arguments:
6362
            scanner_id: UUID of the scanner to be verified
6363
6364
        Returns:
6365
            The response. See :py:meth:`send_command` for details.
6366
        """
6367
        if not scanner_id:
6368
            raise RequiredArgument(
6369
                function=self.verify_scanner.__name__, argument='scanner_id'
6370
            )
6371
6372
        cmd = XmlCommand("verify_scanner")
6373
        cmd.set_attribute("scanner_id", scanner_id)
6374
6375
        return self._send_xml_command(cmd)
6376