Completed
Push — master ( 4ac98b...5a81ff )
by Juan José
12s
created

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

Complexity

Conditions 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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