Passed
Pull Request — master (#77)
by
unknown
01:37
created

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

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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