Completed
Push — master ( f3cde9...b114e7 )
by
unknown
13s queued 11s
created

gvm.protocols.gmpv7.Gmp.modify_user()   C

Complexity

Conditions 10

Size

Total Lines 55
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nop 11
dl 0
loc 55
rs 5.9999
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

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

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

Commonly applied refactorings include:

Complexity

Complex classes like gvm.protocols.gmpv7.Gmp.modify_user() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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