Passed
Push — master ( 698cc1...9545fa )
by
unknown
02:41 queued 46s
created

gvm.protocols.gmpv7   F

Complexity

Total Complexity 632

Size/Duplication

Total Lines 4287
Duplicated Lines 12.29 %

Importance

Changes 0
Metric Value
eloc 1808
dl 527
loc 4287
rs 0.8
c 0
b 0
f 0
wmc 632

3 Functions

Rating   Name   Duplication   Size   Complexity  
A _check_command_status() 0 26 4
A _add_filter() 0 6 3
A _to_bool() 0 2 2

154 Methods

Rating   Name   Duplication   Size   Complexity  
A Gmp.clone_group() 0 12 1
B Gmp.import_report() 0 46 8
A Gmp.clone_port_list() 0 12 1
A Gmp.delete_agent() 0 16 2
A Gmp.clone_target() 0 12 1
B Gmp.create_filter() 0 37 7
B Gmp.create_agent() 0 42 7
A Gmp.create_asset() 0 27 4
A Gmp.get_protocol_version() 0 8 1
A Gmp.clone_user() 0 12 1
A Gmp.delete_alert() 0 16 2
C Gmp.create_note() 0 57 11
B Gmp.create_permission() 46 46 7
A Gmp.clone_filter() 0 12 1
F Gmp.create_task() 0 80 16
A Gmp.is_authenticated() 0 11 1
F Gmp.create_alert() 0 60 14
A Gmp.clone_agent() 0 12 1
B Gmp.create_scanner() 0 48 8
F Gmp.create_schedule() 52 103 20
F Gmp.create_target() 0 100 19
C Gmp.create_user() 0 41 9
B Gmp.create_port_range() 0 40 6
A Gmp.clone_scanner() 0 12 1
A Gmp.clone_note() 0 12 1
A Gmp.authenticate() 0 32 4
D Gmp.create_override() 69 69 13
A Gmp.create_port_list() 0 27 4
A Gmp.clone_alert() 0 12 1
F Gmp.create_credential() 0 80 17
A Gmp.create_group() 0 30 5
A Gmp.__init__() 0 5 1
A Gmp.clone_task() 0 12 1
A Gmp.clone_permission() 0 12 1
A Gmp.clone_override() 0 12 1
A Gmp.clone_schedule() 0 12 1
A Gmp.clone_role() 0 12 1
A Gmp.clone_credential() 0 12 1
A Gmp.clone_tag() 0 12 1
A Gmp.create_config() 0 20 3
A Gmp.create_role() 0 25 4
A Gmp.create_tag() 0 35 5
A Gmp.get_result() 0 12 1
F Gmp.modify_credential() 0 87 19
A Gmp.get_permission() 0 12 1
A Gmp.get_permissions() 0 21 2
A Gmp.get_port_list() 0 12 1
A Gmp.modify_asset() 0 15 2
B Gmp.get_configs() 0 41 6
A Gmp.get_notes() 33 33 5
A Gmp.get_setting() 0 12 1
A Gmp.sync_config() 0 7 1
F Gmp.modify_schedule() 52 105 21
A Gmp.delete_asset() 0 19 4
A Gmp.get_agents() 0 37 5
A Gmp.get_report() 0 19 1
F Gmp.modify_user() 0 52 14
A Gmp.describe_auth() 0 10 1
B Gmp.modify_report_format() 0 33 8
A Gmp.get_alert() 0 12 1
A Gmp.get_aggregates() 0 4 1
A Gmp.modify_report() 0 18 3
B Gmp.delete_user() 0 32 7
A Gmp.modify_role() 0 25 5
B Gmp.get_reports() 0 41 7
A Gmp.get_version() 0 7 1
A Gmp.move_task() 0 21 3
A Gmp.delete_tag() 0 17 2
A Gmp.get_agent() 0 12 1
A Gmp.resume_task() 0 16 2
A Gmp.get_settings() 0 15 2
A Gmp.get_filter() 0 12 1
A Gmp.modify_port_list() 0 21 4
A Gmp.get_nvt() 0 12 1
C Gmp.get_nvts() 0 53 10
A Gmp.delete_permission() 0 17 2
A Gmp.verify_scanner() 0 20 2
B Gmp.modify_permission() 43 43 8
A Gmp.get_note() 0 12 1
A Gmp.get_assets() 0 16 1
A Gmp.get_nvt_families() 0 15 2
A Gmp.get_report_format() 0 12 1
A Gmp.get_asset() 0 12 1
A Gmp.delete_schedule() 0 17 2
A Gmp.get_filters() 0 26 3
A Gmp.get_user() 0 12 1
A Gmp.get_groups() 0 21 2
A Gmp.delete_port_range() 0 14 2
A Gmp.get_report_formats() 0 37 5
A Gmp.delete_report() 0 14 2
A Gmp.delete_credential() 0 17 2
A Gmp.delete_port_list() 0 17 2
A Gmp.delete_override() 0 17 2
A Gmp.get_results() 0 36 5
A Gmp.test_alert() 0 18 2
A Gmp.get_tags() 0 27 3
A Gmp.get_schedule() 0 12 1
A Gmp.delete_scanner() 0 17 2
A Gmp.get_tasks() 30 30 4
B Gmp.modify_config() 0 52 8
A Gmp.get_credential() 0 12 1
A Gmp.modify_group() 0 26 5
C Gmp.modify_scanner() 0 52 10
A Gmp.empty_trashcan() 0 10 1
F Gmp.modify_target() 0 90 18
A Gmp.get_tag() 0 12 1
A Gmp.get_override() 0 12 1
A Gmp.start_task() 0 16 2
A Gmp.get_targets() 27 27 3
A Gmp.get_alerts() 25 25 3
A Gmp.get_feed() 0 19 2
A Gmp.get_config() 0 12 1
A Gmp.delete_report_format() 0 17 2
A Gmp.verify_agent() 0 21 2
A Gmp.get_info_list() 0 38 4
A Gmp.modify_agent() 0 19 4
A Gmp.get_port_lists() 32 32 4
A Gmp.verify_report_format() 0 23 2
A Gmp.delete_config() 0 17 2
F Gmp.modify_task() 0 64 14
A Gmp.sync_scap() 0 7 1
A Gmp.get_task() 0 12 1
B Gmp.get_system_reports() 0 41 7
B Gmp.modify_filter() 0 36 7
D Gmp.modify_override() 58 58 12
C Gmp.modify_alert() 0 58 11
A Gmp.get_preferences() 0 29 4
A Gmp.get_target() 0 12 1
B Gmp.get_credentials() 0 40 6
A Gmp.help() 0 23 3
C Gmp.modify_tag() 0 42 9
A Gmp.delete_group() 0 17 2
A Gmp.get_scanners() 0 27 3
A Gmp.delete_target() 0 17 2
C Gmp.modify_note() 0 48 10
A Gmp.delete_note() 0 17 2
A Gmp.modify_auth() 0 20 4
A Gmp.get_users() 0 16 1
A Gmp.delete_role() 0 17 2
A Gmp.get_info() 0 12 1
A Gmp.get_scanner() 0 12 1
A Gmp.modify_setting() 0 22 4
A Gmp.get_schedules() 27 27 3
A Gmp.sync_cert() 0 7 1
A Gmp.get_overrides() 33 33 5
A Gmp.stop_task() 0 16 2
A Gmp.get_roles() 0 20 2
A Gmp.get_feeds() 0 7 1
A Gmp.restore() 0 16 2
A Gmp.sync_feed() 0 7 1
A Gmp.delete_task() 0 17 2
A Gmp.delete_filter() 0 17 2
A Gmp.get_role() 0 12 1
A Gmp.get_group() 0 12 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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

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

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