Completed
Push — master ( d780f6...2d8018 )
by Juan José
27s queued 18s
created

gvm.protocols.gmpv7.Gmp.get_reports()   A

Complexity

Conditions 5

Size

Total Lines 33
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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