ScanConfigsMixin.modify_scan_config_set_comment()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nop 4
dl 0
loc 24
rs 9.8
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2021 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
from typing import Any, List, Optional, Tuple
20
from lxml.etree import XMLSyntaxError
21
22
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType
23
from gvm.utils import add_filter, deprecation, is_list_like, to_base64, to_bool
24
from gvm.xml import XmlCommand
25
26
27
class ScanConfigsMixin:
28
    def clone_scan_config(self, config_id: str) -> Any:
29
        """Clone a scan config from an existing one
30
31
        Arguments:
32
            config_id: UUID of the existing scan config
33
34
        Returns:
35
            The response. See :py:meth:`send_command` for details.
36
        """
37
        if not config_id:
38
            raise RequiredArgument(
39
                function=self.clone_scan_config.__name__, argument='config_id'
40
            )
41
42
        cmd = XmlCommand("create_config")
43
        cmd.add_element("copy", config_id)
44
        return self._send_xml_command(cmd)
45
46
    def create_scan_config(
47
        self, config_id: str, name: str, *, comment: Optional[str] = None
48
    ) -> Any:
49
        """Create a new scan config
50
51
        Arguments:
52
            config_id: UUID of the existing scan config
53
            name: Name of the new scan config
54
            comment: A comment on the config
55
56
        Returns:
57
            The response. See :py:meth:`send_command` for details.
58
        """
59
        if not name:
60
            raise RequiredArgument(
61
                function=self.create_scan_config.__name__, argument='name'
62
            )
63
64
        if not config_id:
65
            raise RequiredArgument(
66
                function=self.create_scan_config.__name__, argument='config_id'
67
            )
68
69
        cmd = XmlCommand("create_config")
70
        if comment is not None:
71
            cmd.add_element("comment", comment)
72
        cmd.add_element("copy", config_id)
73
        cmd.add_element("name", name)
74
        cmd.add_element("usage_type", "scan")
75
        return self._send_xml_command(cmd)
76
77
    def create_scan_config_from_osp_scanner(
78
        self, scanner_id: str, name: str, *, comment: Optional[str] = None
79
    ) -> Any:
80
        """Create a new scan config from an ospd scanner.
81
82
        Create config by retrieving the expected preferences from the given
83
        scanner via OSP.
84
85
        Arguments:
86
            scanner_id: UUID of an OSP scanner to get config data from
87
            name: Name of the new scan config
88
            comment: A comment on the config
89
90
        Returns:
91
            The response. See :py:meth:`send_command` for details.
92
        """
93
        if not name:
94
            raise RequiredArgument(
95
                function=self.create_scan_config_from_osp_scanner.__name__,
96
                argument='name',
97
            )
98
99
        if not scanner_id:
100
            raise RequiredArgument(
101
                function=self.create_scan_config_from_osp_scanner.__name__,
102
                argument='scanner_id',
103
            )
104
105
        cmd = XmlCommand("create_config")
106
        if comment is not None:
107
            cmd.add_element("comment", comment)
108
        cmd.add_element("scanner", scanner_id)
109
        cmd.add_element("name", name)
110
        cmd.add_element("usage_type", "scan")
111
        return self._send_xml_command(cmd)
112
113
    def delete_scan_config(
114
        self, config_id: str, *, ultimate: Optional[bool] = False
115
    ) -> Any:
116
        """Deletes an existing config
117
118
        Arguments:
119
            config_id: UUID of the config to be deleted.
120
            ultimate: Whether to remove entirely, or to the trashcan.
121
        """
122
        if not config_id:
123
            raise RequiredArgument(
124
                function=self.delete_scan_config.__name__, argument='config_id'
125
            )
126
127
        cmd = XmlCommand("delete_config")
128
        cmd.set_attribute("config_id", config_id)
129
        cmd.set_attribute("ultimate", to_bool(ultimate))
130
131
        return self._send_xml_command(cmd)
132
133 View Code Duplication
    def get_scan_configs(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
134
        self,
135
        *,
136
        filter_string: Optional[str] = None,
137
        filter_id: Optional[str] = None,
138
        trash: Optional[bool] = None,
139
        details: Optional[bool] = None,
140
        families: Optional[bool] = None,
141
        preferences: Optional[bool] = None,
142
        tasks: Optional[bool] = None,
143
    ) -> Any:
144
        """Request a list of scan configs
145
146
        Arguments:
147
            filter_string: Filter term to use for the query
148
            filter_id: UUID of an existing filter to use for the query
149
            trash: Whether to get the trashcan scan configs instead
150
            details: Whether to get config families, preferences, nvt selectors
151
                and tasks.
152
            families: Whether to include the families if no details are
153
                requested
154
            preferences: Whether to include the preferences if no details are
155
                requested
156
            tasks: Whether to get tasks using this config
157
158
        Returns:
159
            The response. See :py:meth:`send_command` for details.
160
        """
161
        cmd = XmlCommand("get_configs")
162
        cmd.set_attribute("usage_type", "scan")
163
164
        add_filter(cmd, filter_string, filter_id)
165
166
        if trash is not None:
167
            cmd.set_attribute("trash", to_bool(trash))
168
169
        if details is not None:
170
            cmd.set_attribute("details", to_bool(details))
171
172
        if families is not None:
173
            cmd.set_attribute("families", to_bool(families))
174
175
        if preferences is not None:
176
            cmd.set_attribute("preferences", to_bool(preferences))
177
178
        if tasks is not None:
179
            cmd.set_attribute("tasks", to_bool(tasks))
180
181
        return self._send_xml_command(cmd)
182
183
    def get_scan_config(
184
        self, config_id: str, *, tasks: Optional[bool] = None
185
    ) -> Any:
186
        """Request a single scan config
187
188
        Arguments:
189
            config_id: UUID of an existing scan config
190
            tasks: Whether to get tasks using this config
191
192
        Returns:
193
            The response. See :py:meth:`send_command` for details.
194
        """
195
        if not config_id:
196
            raise RequiredArgument(
197
                function=self.get_scan_config.__name__, argument='config_id'
198
            )
199
200
        cmd = XmlCommand("get_configs")
201
        cmd.set_attribute("config_id", config_id)
202
203
        cmd.set_attribute("usage_type", "scan")
204
205
        if tasks is not None:
206
            cmd.set_attribute("tasks", to_bool(tasks))
207
208
        # for single entity always request all details
209
        cmd.set_attribute("details", "1")
210
211
        return self._send_xml_command(cmd)
212
213
    def get_scan_config_preferences(
214
        self, *, nvt_oid: Optional[str] = None, config_id: Optional[str] = None
215
    ) -> Any:
216
        """Request a list of scan_config preferences
217
218
        When the command includes a config_id attribute, the preference element
219
        includes the preference name, type and value, and the NVT to which the
220
        preference applies.
221
        If the command includes a config_id and an nvt_oid, the preferencces for
222
        the given nvt in the config will be shown.
223
224
        Arguments:
225
            nvt_oid: OID of nvt
226
            config_id: UUID of scan config of which to show preference values
227
228
        Returns:
229
            The response. See :py:meth:`send_command` for details.
230
        """
231
        cmd = XmlCommand("get_preferences")
232
233
        if nvt_oid:
234
            cmd.set_attribute("nvt_oid", nvt_oid)
235
236
        if config_id:
237
            cmd.set_attribute("config_id", config_id)
238
239
        return self._send_xml_command(cmd)
240
241
    def get_scan_config_preference(
242
        self,
243
        name: str,
244
        *,
245
        nvt_oid: Optional[str] = None,
246
        config_id: Optional[str] = None,
247
    ) -> Any:
248
        """Request a nvt preference
249
250
        Arguments:
251
            name: name of a particular preference
252
            nvt_oid: OID of nvt
253
            config_id: UUID of scan config of which to show preference values
254
255
        Returns:
256
            The response. See :py:meth:`send_command` for details.
257
        """
258
        cmd = XmlCommand("get_preferences")
259
260
        if not name:
261
            raise RequiredArgument(
262
                function=self.get_scan_config_preference.__name__,
263
                argument='name',
264
            )
265
266
        cmd.set_attribute("preference", name)
267
268
        if nvt_oid:
269
            cmd.set_attribute("nvt_oid", nvt_oid)
270
271
        if config_id:
272
            cmd.set_attribute("config_id", config_id)
273
274
        return self._send_xml_command(cmd)
275
276
    def import_scan_config(self, config: str) -> Any:
277
        """Import a scan config from XML
278
279
        Arguments:
280
            config: Scan Config XML as string to import. This XML must
281
                contain a :code:`<get_configs_response>` root element.
282
283
        Returns:
284
            The response. See :py:meth:`send_command` for details.
285
        """
286
        if not config:
287
            raise RequiredArgument(
288
                function=self.import_scan_config.__name__, argument='config'
289
            )
290
291
        cmd = XmlCommand("create_config")
292
293
        try:
294
            cmd.append_xml_str(config)
295
        except XMLSyntaxError as e:
296
            raise InvalidArgument(
297
                function=self.import_scan_config.__name__, argument='config'
298
            ) from e
299
300
        return self._send_xml_command(cmd)
301
302 View Code Duplication
    def modify_scan_config_set_nvt_preference(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
303
        self,
304
        config_id: str,
305
        name: str,
306
        nvt_oid: str,
307
        *,
308
        value: Optional[str] = None,
309
    ) -> Any:
310
        """Modifies the nvt preferences of an existing scan config.
311
312
        Arguments:
313
            config_id: UUID of scan config to modify.
314
            name: Name for nvt preference to change.
315
            nvt_oid: OID of the NVT associated with preference to modify
316
            value: New value for the preference. None to delete the preference
317
                and to use the default instead.
318
        """
319
        if not config_id:
320
            raise RequiredArgument(
321
                function=self.modify_scan_config_set_nvt_preference.__name__,
322
                argument='config_id',
323
            )
324
325
        if not nvt_oid:
326
            raise RequiredArgument(
327
                function=self.modify_scan_config_set_nvt_preference.__name__,
328
                argument='nvt_oid',
329
            )
330
331
        if not name:
332
            raise RequiredArgument(
333
                function=self.modify_scan_config_set_nvt_preference.__name__,
334
                argument='name',
335
            )
336
337
        cmd = XmlCommand("modify_config")
338
        cmd.set_attribute("config_id", str(config_id))
339
340
        _xmlpref = cmd.add_element("preference")
341
342
        _xmlpref.add_element("nvt", attrs={"oid": nvt_oid})
343
        _xmlpref.add_element("name", name)
344
345
        if value:
346
            _xmlpref.add_element("value", to_base64(value))
347
348
        return self._send_xml_command(cmd)
349
350
    def modify_scan_config_set_name(self, config_id: str, name: str) -> Any:
351
        """Modifies the name of an existing scan config
352
353
        Arguments:
354
            config_id: UUID of scan config to modify.
355
            name: New name for the config.
356
        """
357
        if not config_id:
358
            raise RequiredArgument(
359
                function=self.modify_scan_config_set_name.__name__,
360
                argument='config_id',
361
            )
362
363
        if not name:
364
            raise RequiredArgument(
365
                function=self.modify_scan_config_set_name.__name__,
366
                argument='name',
367
            )
368
369
        cmd = XmlCommand("modify_config")
370
        cmd.set_attribute("config_id", str(config_id))
371
372
        cmd.add_element("name", name)
373
374
        return self._send_xml_command(cmd)
375
376
    def modify_scan_config_set_comment(
377
        self, config_id: str, *, comment: Optional[str] = None
378
    ) -> Any:
379
        """Modifies the comment of an existing scan config
380
381
        Arguments:
382
            config_id: UUID of scan config to modify.
383
            comment: Comment to set on a config. Default is an
384
                empty comment and the previous comment will be
385
                removed.
386
        """
387
        if not config_id:
388
            raise RequiredArgument(
389
                function=self.modify_scan_config_set_comment.__name__,
390
                argument='config_id argument',
391
            )
392
393
        cmd = XmlCommand("modify_config")
394
        cmd.set_attribute("config_id", str(config_id))
395
        if not comment:
396
            comment = ""
397
        cmd.add_element("comment", comment)
398
399
        return self._send_xml_command(cmd)
400
401
    def modify_scan_config_set_scanner_preference(
402
        self, config_id: str, name: str, *, value: Optional[str] = None
403
    ) -> Any:
404
        """Modifies the scanner preferences of an existing scan config
405
406
        Arguments:
407
            config_id: UUID of scan config to modify.
408
            name: Name of the scanner preference to change
409
            value: New value for the preference. None to delete the preference
410
                and to use the default instead.
411
412
        """
413
        if not config_id:
414
            raise RequiredArgument(
415
                function=(
416
                    self.modify_scan_config_set_scanner_preference.__name__
417
                ),
418
                argument='config_id',
419
            )
420
421
        if not name:
422
            raise RequiredArgument(
423
                function=(
424
                    self.modify_scan_config_set_scanner_preference.__name__
425
                ),
426
                argument='name argument',
427
            )
428
429
        cmd = XmlCommand("modify_config")
430
        cmd.set_attribute("config_id", str(config_id))
431
432
        _xmlpref = cmd.add_element("preference")
433
434
        _xmlpref.add_element("name", name)
435
436
        if value:
437
            _xmlpref.add_element("value", to_base64(value))
438
439
        return self._send_xml_command(cmd)
440
441 View Code Duplication
    def modify_scan_config_set_nvt_selection(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
442
        self, config_id: str, family: str, nvt_oids: List[str]
443
    ) -> Any:
444
        """Modifies the selected nvts of an existing scan config
445
446
        The manager updates the given family in the config to include only the
447
        given NVTs.
448
449
        Arguments:
450
            config_id: UUID of scan config to modify.
451
            family: Name of the NVT family to include NVTs from
452
            nvt_oids: List of NVTs to select for the family.
453
        """
454
        if not config_id:
455
            raise RequiredArgument(
456
                function=self.modify_scan_config_set_nvt_selection.__name__,
457
                argument='config_id',
458
            )
459
460
        if not family:
461
            raise RequiredArgument(
462
                function=self.modify_scan_config_set_nvt_selection.__name__,
463
                argument='family argument',
464
            )
465
466
        if not is_list_like(nvt_oids):
467
            raise InvalidArgumentType(
468
                function=self.modify_scan_config_set_nvt_selection.__name__,
469
                argument='nvt_oids',
470
                arg_type='list',
471
            )
472
473
        cmd = XmlCommand("modify_config")
474
        cmd.set_attribute("config_id", str(config_id))
475
476
        _xmlnvtsel = cmd.add_element("nvt_selection")
477
        _xmlnvtsel.add_element("family", family)
478
479
        for nvt in nvt_oids:
480
            _xmlnvtsel.add_element("nvt", attrs={"oid": nvt})
481
482
        return self._send_xml_command(cmd)
483
484 View Code Duplication
    def modify_scan_config_set_family_selection(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
485
        self,
486
        config_id: str,
487
        families: List[Tuple[str, bool, bool]],
488
        *,
489
        auto_add_new_families: Optional[bool] = True,
490
    ) -> Any:
491
        """
492
        Selected the NVTs of a scan config at a family level.
493
494
        Arguments:
495
            config_id: UUID of scan config to modify.
496
            families: A list of tuples (str, bool, bool):
497
                str: the name of the NVT family selected,
498
                bool: add new NVTs  to the family automatically,
499
                bool: include all NVTs from the family
500
            auto_add_new_families: Whether new families should be added to the
501
                scan config automatically. Default: True.
502
        """
503
        if not config_id:
504
            raise RequiredArgument(
505
                function=self.modify_scan_config_set_family_selection.__name__,
506
                argument='config_id',
507
            )
508
509
        if not is_list_like(families):
510
            raise InvalidArgumentType(
511
                function=self.modify_scan_config_set_family_selection.__name__,
512
                argument='families',
513
                arg_type='list',
514
            )
515
516
        cmd = XmlCommand("modify_config")
517
        cmd.set_attribute("config_id", str(config_id))
518
519
        _xmlfamsel = cmd.add_element("family_selection")
520
        _xmlfamsel.add_element("growing", to_bool(auto_add_new_families))
521
522
        for family in families:
523
            _xmlfamily = _xmlfamsel.add_element("family")
524
            _xmlfamily.add_element("name", family[0])
525
526
            if len(family) != 3:
527
                raise InvalidArgument(
528
                    "Family must be a tuple of 3. (str, bool, bool)"
529
                )
530
531
            if not isinstance(family[1], bool) or not isinstance(
532
                family[2], bool
533
            ):
534
                raise InvalidArgumentType(
535
                    function=(
536
                        self.modify_scan_config_set_family_selection.__name__
537
                    ),
538
                    argument='families',
539
                    arg_type='[tuple(str, bool, bool)]',
540
                )
541
542
            _xmlfamily.add_element("all", to_bool(family[2]))
543
            _xmlfamily.add_element("growing", to_bool(family[1]))
544
545
        return self._send_xml_command(cmd)
546
547
    def modify_scan_config(
548
        self, config_id: str, selection: Optional[str] = None, **kwargs
549
    ) -> Any:
550
        """Modifies an existing scan config.
551
552
        DEPRECATED. Please use *modify_config_set_* methods instead.
553
554
        modify_config has four modes to operate depending on the selection.
555
556
        Arguments:
557
            config_id: UUID of scan config to modify.
558
            selection: one of 'scan_pref', 'nvt_pref', 'nvt_selection' or
559
                'family_selection'
560
            name: New name for preference.
561
            value: New value for preference.
562
            nvt_oids: List of NVTs associated with preference to modify.
563
            family: Name of family to modify.
564
565
        Returns:
566
            The response. See :py:meth:`send_command` for details.
567
        """
568
        if not config_id:
569
            raise RequiredArgument(
570
                function=self.modify_scan_config.__name__,
571
                argument='config_id argument',
572
            )
573
574
        if selection is None:
575
            deprecation(
576
                "Using modify_config to update the comment of a scan config is"
577
                "deprecated. Please use modify_scan_config_set_comment instead."
578
            )
579
            return self.modify_scan_config_set_comment(
580
                config_id, comment=kwargs.get("comment")
581
            )
582
583
        if selection not in (
584
            "nvt_pref",
585
            "scan_pref",
586
            "family_selection",
587
            "nvt_selection",
588
        ):
589
            raise InvalidArgument(
590
                "selection must be one of nvt_pref, "
591
                "scan_pref, family_selection or "
592
                "nvt_selection"
593
            )
594
595
        if selection == "nvt_pref":
596
            deprecation(
597
                "Using modify_scan_config to update a nvt preference of a scan "
598
                "config is deprecated. Please use "
599
                "modify_scan_config_set_nvt_preference instead."
600
            )
601
            return self.modify_scan_config_set_nvt_preference(
602
                config_id, **kwargs
603
            )
604
605
        if selection == "scan_pref":
606
            deprecation(
607
                "Using modify_scan_config to update a scanner preference of a "
608
                "scan config is deprecated. Please use "
609
                "modify_scan_config_set_scanner_preference instead."
610
            )
611
            return self.modify_scan_config_set_scanner_preference(
612
                config_id, **kwargs
613
            )
614
615
        if selection == "nvt_selection":
616
            deprecation(
617
                "Using modify_scan_config to update a nvt selection of a "
618
                "scan config is deprecated. Please use "
619
                "modify_scan_config_set_nvt_selection instead."
620
            )
621
            return self.modify_scan_config_set_nvt_selection(
622
                config_id, **kwargs
623
            )
624
625
        deprecation(
626
            "Using modify_scan_config to update a family selection of a "
627
            "scan config is deprecated. Please use "
628
            "modify_scan_config_set_family_selection instead."
629
        )
630
        return self.modify_scan_config_set_family_selection(config_id, **kwargs)
631
632
    def sync_scan_config(self) -> Any:
633
        """Request an OSP config synchronization with scanner
634
635
        Returns:
636
            The response. See :py:meth:`send_command` for details.
637
        """
638
        return self._send_xml_command(XmlCommand("sync_config"))
639