Completed
Push — master ( 9545fa...4cbf4d )
by Juan José
18s queued 10s
created

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

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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