Passed
Pull Request — master (#278)
by Björn
02:03
created

GmpV7Mixin.get_credential()   B

Complexity

Conditions 6

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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