Completed
Push — master ( 9d06ff...e4f0dd )
by Jaspar
23s queued 13s
created

GmpV7Mixin.delete_alert()   A

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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