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
|
|
|
# pylint: disable=redefined-builtin |
20
|
|
|
# MAYBE we should change filter to filter_string (everywhere) |
21
|
|
|
|
22
|
|
|
from enum import Enum |
23
|
|
|
from typing import Any, Optional |
24
|
|
|
|
25
|
|
|
from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument |
26
|
|
|
from gvm.xml import XmlCommand |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class FeedType(Enum): |
30
|
|
|
"""Enum for feed types""" |
31
|
|
|
|
32
|
|
|
NVT = "NVT" |
33
|
|
|
CERT = "CERT" |
34
|
|
|
SCAP = "SCAP" |
35
|
|
|
GVMD_DATA = "GVMD_DATA" |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def get_feed_type_from_string(feed_type: Optional[str]) -> Optional[FeedType]: |
39
|
|
|
"""Convert a feed type string into a FeedType instance""" |
40
|
|
|
if not feed_type: |
41
|
|
|
return None |
42
|
|
|
|
43
|
|
|
try: |
44
|
|
|
return FeedType[feed_type.upper()] |
45
|
|
|
except KeyError: |
46
|
|
|
raise InvalidArgument( |
47
|
|
|
argument='feed_type', function=get_feed_type_from_string.__name__ |
48
|
|
|
) from None |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class FeedMixin: |
52
|
|
|
def get_feeds(self) -> Any: |
53
|
|
|
"""Request the list of feeds |
54
|
|
|
|
55
|
|
|
Returns: |
56
|
|
|
The response. See :py:meth:`send_command` for details. |
57
|
|
|
""" |
58
|
|
|
return self._send_xml_command(XmlCommand("get_feeds")) |
59
|
|
|
|
60
|
|
|
def get_feed(self, feed_type: Optional[FeedType]) -> Any: |
61
|
|
|
"""Request a single feed |
62
|
|
|
|
63
|
|
|
Arguments: |
64
|
|
|
feed_type: Type of single feed to get: NVT, CERT or SCAP |
65
|
|
|
|
66
|
|
|
Returns: |
67
|
|
|
The response. See :py:meth:`send_command` for details. |
68
|
|
|
""" |
69
|
|
|
if not feed_type: |
70
|
|
|
raise RequiredArgument( |
71
|
|
|
function=self.get_feed.__name__, argument='feed_type' |
72
|
|
|
) |
73
|
|
|
|
74
|
|
|
if not isinstance(feed_type, FeedType): |
75
|
|
|
raise InvalidArgumentType( |
76
|
|
|
function=self.get_feed.__name__, |
77
|
|
|
argument='feed_type', |
78
|
|
|
arg_type=FeedType.__name__, |
79
|
|
|
) |
80
|
|
|
|
81
|
|
|
cmd = XmlCommand("get_feeds") |
82
|
|
|
cmd.set_attribute("type", feed_type.value) |
83
|
|
|
|
84
|
|
|
return self._send_xml_command(cmd) |
85
|
|
|
|