|
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
|
|
|
|
|
21
|
|
|
from enum import Enum |
|
22
|
|
|
from typing import Any, Optional |
|
23
|
|
|
|
|
24
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument |
|
25
|
|
|
from gvm.utils import add_filter |
|
26
|
|
|
from gvm.xml import XmlCommand |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class HostsOrdering(Enum): |
|
30
|
|
|
"""Enum for host ordering during scans""" |
|
31
|
|
|
|
|
32
|
|
|
SEQUENTIAL = "sequential" |
|
33
|
|
|
RANDOM = "random" |
|
34
|
|
|
REVERSE = "reverse" |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def get_hosts_ordering_from_string( |
|
38
|
|
|
hosts_ordering: Optional[str], |
|
39
|
|
|
) -> Optional[HostsOrdering]: |
|
40
|
|
|
"""Convert a hosts ordering string to an actual HostsOrdering instance |
|
41
|
|
|
|
|
42
|
|
|
Arguments: |
|
43
|
|
|
hosts_ordering: Host ordering string to convert to a HostsOrdering |
|
44
|
|
|
""" |
|
45
|
|
|
if not hosts_ordering: |
|
46
|
|
|
return None |
|
47
|
|
|
try: |
|
48
|
|
|
return HostsOrdering[hosts_ordering.upper()] |
|
49
|
|
|
except KeyError: |
|
50
|
|
|
raise InvalidArgument( |
|
51
|
|
|
argument='hosts_ordering', |
|
52
|
|
|
function=get_hosts_ordering_from_string.__name__, |
|
53
|
|
|
) from None |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
class HostsMixin: |
|
57
|
|
|
def create_host(self, name: str, *, comment: Optional[str] = None) -> Any: |
|
58
|
|
|
"""Create a new host host |
|
59
|
|
|
|
|
60
|
|
|
Arguments: |
|
61
|
|
|
name: Name for the new host host |
|
62
|
|
|
comment: Comment for the new host host |
|
63
|
|
|
|
|
64
|
|
|
Returns: |
|
65
|
|
|
The response. See :py:meth:`send_command` for details. |
|
66
|
|
|
""" |
|
67
|
|
|
if not name: |
|
68
|
|
|
raise RequiredArgument( |
|
69
|
|
|
function=self.create_host.__name__, argument='name' |
|
70
|
|
|
) |
|
71
|
|
|
|
|
72
|
|
|
cmd = XmlCommand("create_asset") |
|
73
|
|
|
host = cmd.add_element("asset") |
|
74
|
|
|
host.add_element("type", "host") |
|
75
|
|
|
host.add_element("name", name) |
|
76
|
|
|
|
|
77
|
|
|
if comment: |
|
78
|
|
|
host.add_element("comment", comment) |
|
79
|
|
|
|
|
80
|
|
|
return self._send_xml_command(cmd) |
|
81
|
|
|
|
|
82
|
|
|
def delete_host(self, host_id: str) -> Any: |
|
83
|
|
|
"""Deletes an existing host |
|
84
|
|
|
|
|
85
|
|
|
Arguments: |
|
86
|
|
|
host_id: UUID of the single host to delete. |
|
87
|
|
|
""" |
|
88
|
|
|
if not host_id: |
|
89
|
|
|
raise RequiredArgument( |
|
90
|
|
|
function=self.delete_host.__name__, |
|
91
|
|
|
argument='host_id', |
|
92
|
|
|
) |
|
93
|
|
|
|
|
94
|
|
|
cmd = XmlCommand("delete_asset") |
|
95
|
|
|
cmd.set_attribute("asset_id", host_id) |
|
96
|
|
|
|
|
97
|
|
|
return self._send_xml_command(cmd) |
|
98
|
|
|
|
|
99
|
|
|
def get_hosts( |
|
100
|
|
|
self, |
|
101
|
|
|
*, |
|
102
|
|
|
filter: Optional[str] = None, |
|
103
|
|
|
filter_id: Optional[str] = None, |
|
104
|
|
|
) -> Any: |
|
105
|
|
|
"""Request a list of hosts |
|
106
|
|
|
|
|
107
|
|
|
Arguments: |
|
108
|
|
|
filter: Filter term to use for the query |
|
109
|
|
|
filter_id: UUID of an existing filter to use for the query |
|
110
|
|
|
|
|
111
|
|
|
Returns: |
|
112
|
|
|
The response. See :py:meth:`send_command` for details. |
|
113
|
|
|
""" |
|
114
|
|
|
|
|
115
|
|
|
cmd = XmlCommand("get_assets") |
|
116
|
|
|
|
|
117
|
|
|
cmd.set_attribute("type", "host") |
|
118
|
|
|
|
|
119
|
|
|
add_filter(cmd, filter, filter_id) |
|
120
|
|
|
|
|
121
|
|
|
return self._send_xml_command(cmd) |
|
122
|
|
|
|
|
123
|
|
|
def get_host(self, host_id: str) -> Any: |
|
124
|
|
|
"""Request a single host |
|
125
|
|
|
|
|
126
|
|
|
Arguments: |
|
127
|
|
|
host_id: UUID of an existing host |
|
128
|
|
|
|
|
129
|
|
|
Returns: |
|
130
|
|
|
The response. See :py:meth:`send_command` for details. |
|
131
|
|
|
""" |
|
132
|
|
|
cmd = XmlCommand("get_assets") |
|
133
|
|
|
|
|
134
|
|
|
if not host_id: |
|
135
|
|
|
raise RequiredArgument( |
|
136
|
|
|
function=self.get_host.__name__, argument='host_id' |
|
137
|
|
|
) |
|
138
|
|
|
|
|
139
|
|
|
cmd.set_attribute("asset_id", host_id) |
|
140
|
|
|
cmd.set_attribute("type", "host") |
|
141
|
|
|
|
|
142
|
|
|
return self._send_xml_command(cmd) |
|
143
|
|
|
|
|
144
|
|
|
def modify_host( |
|
145
|
|
|
self, host_id: str, *, comment: Optional[str] = None |
|
146
|
|
|
) -> Any: |
|
147
|
|
|
"""Modifies an existing host. |
|
148
|
|
|
|
|
149
|
|
|
Arguments: |
|
150
|
|
|
host_id: UUID of the host to be modified. |
|
151
|
|
|
comment: Comment for the host. Not passing a comment |
|
152
|
|
|
arguments clears the comment for this host. |
|
153
|
|
|
|
|
154
|
|
|
Returns: |
|
155
|
|
|
The response. See :py:meth:`send_command` for details. |
|
156
|
|
|
""" |
|
157
|
|
|
if not host_id: |
|
158
|
|
|
raise RequiredArgument( |
|
159
|
|
|
function=self.modify_host.__name__, argument='host_id' |
|
160
|
|
|
) |
|
161
|
|
|
|
|
162
|
|
|
cmd = XmlCommand("modify_asset") |
|
163
|
|
|
cmd.set_attribute("asset_id", host_id) |
|
164
|
|
|
if not comment: |
|
165
|
|
|
comment = "" |
|
166
|
|
|
cmd.add_element("comment", comment) |
|
167
|
|
|
|
|
168
|
|
|
return self._send_xml_command(cmd) |
|
169
|
|
|
|