Passed
Pull Request — master (#289)
by Jaspar
01:38
created

()   B

Complexity

Conditions 6

Size

Total Lines 49
Code Lines 22

Duplication

Lines 49
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nop 7
dl 49
loc 49
rs 8.4186
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 - 2019 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=arguments-differ, redefined-builtin, too-many-lines
20
21
"""
22
Module for communication with gvmd in
23
`Greenbone Management Protocol version 20.08`_
24
25
.. _Greenbone Management Protocol version 20.08:
26
    https://docs.greenbone.net/API/GMP/gmp-20.08.html
27
"""
28
import warnings
29
30
from typing import Any, List, Optional, Callable
31
32
from gvm.utils import deprecation
33
from gvm.xml import XmlCommand
34
35
from gvm.protocols.gmpv7.gmpv7 import _to_bool, _to_comma_list
36
from gvm.connections import GvmConnection
37
from gvm.errors import InvalidArgumentType, RequiredArgument
38
39
from gvm.protocols.base import GvmProtocol
40
41
from . import types
42
from .types import *  # pylint: disable=unused-wildcard-import, wildcard-import
43
44
_EMPTY_POLICY_ID = '085569ce-73ed-11df-83c3-002264764cea'
45
46
PROTOCOL_VERSION = (20, 8)
47
48
49
class GmpV208Mixin(GvmProtocol):
50
    types = types
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable types does not seem to be defined.
Loading history...
51
52
    def __init__(
53
        self,
54
        connection: GvmConnection,
55
        *,
56
        transform: Optional[Callable[[str], Any]] = None
57
    ):
58
        super().__init__(connection, transform=transform)
59
60
        # Is authenticated on gvmd
61
        self._authenticated = False
62
63
    def create_agent(
64
        self,
65
        installer: str,
66
        signature: str,
67
        name: str,
68
        *,
69
        comment: Optional[str] = None,
70
        howto_install: Optional[str] = None,
71
        howto_use: Optional[str] = None
72
    ) -> None:
73
        # pylint: disable=unused-argument
74
        deprecation(
75
            "{} has been removed in GMP version {}.{}".format(
76
                self.create_agent.__name__,
77
                self.get_protocol_version()[0],
78
                self.get_protocol_version()[1],
79
            )
80
        )
81
82
    def clone_agent(self, agent_id: str) -> None:
83
        # pylint: disable=unused-argument
84
        deprecation(
85
            "{} has been removed in GMP version {}.{}".format(
86
                self.clone_agent.__name__,
87
                self.get_protocol_version()[0],
88
                self.get_protocol_version()[1],
89
            )
90
        )
91
92
    def modify_agent(
93
        self,
94
        agent_id: str,
95
        *,
96
        name: Optional[str] = None,
97
        comment: Optional[str] = None
98
    ) -> None:
99
        # pylint: disable=unused-argument
100
        deprecation(
101
            "{} has been removed in GMP version {}.{}".format(
102
                self.clone_agent.__name__,
103
                self.get_protocol_version()[0],
104
                self.get_protocol_version()[1],
105
            )
106
        )
107
108
    def delete_agent(
109
        self,
110
        agent_id: str,
111
        *,
112
        ultimate: Optional[bool] = False
113
        # pylint: disable=unused-argument
114
    ) -> None:
115
        deprecation(
116
            "{} has been removed in GMP version {}.{}".format(
117
                self.delete_agent.__name__,
118
                self.get_protocol_version()[0],
119
                self.get_protocol_version()[1],
120
            )
121
        )
122
123
    def verify_agent(self, agent_id: str) -> None:
124
        # pylint: disable=unused-argument
125
        deprecation(
126
            "{} has been removed in GMP version {}.{}".format(
127
                self.verify_agent.__name__,
128
                self.get_protocol_version()[0],
129
                self.get_protocol_version()[1],
130
            )
131
        )
132
133
    def get_agent(self, agent_id: str) -> None:
134
        # pylint: disable=unused-argument
135
        deprecation(
136
            "{} has been removed in GMP version {}.{}".format(
137
                self.get_agent.__name__,
138
                self.get_protocol_version()[0],
139
                self.get_protocol_version()[1],
140
            )
141
        )
142
143 View Code Duplication
    def get_info(self, info_id: str, info_type: InfoType) -> Any:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
144
        """Request a single secinfo
145
146
        Arguments:
147
            info_id: UUID of an existing secinfo
148
            info_type: Type must be either CERT_BUND_ADV, CPE, CVE,
149
                DFN_CERT_ADV, OVALDEF, NVT
150
151
        Returns:
152
            The response. See :py:meth:`send_command` for details.
153
        """
154
        if not info_type:
155
            raise RequiredArgument(
156
                function=self.get_info.__name__, argument='info_type'
157
            )
158
159
        if not isinstance(info_type, InfoType):
160
            raise InvalidArgumentType(
161
                function=self.get_info.__name__,
162
                argument='info_type',
163
                arg_type=InfoType.__name__,
164
            )
165
166
        if not info_id:
167
            raise RequiredArgument(
168
                function=self.get_info.__name__, argument='info_id'
169
            )
170
171
        cmd = XmlCommand("get_info")
172
        cmd.set_attribute("info_id", info_id)
173
174
        cmd.set_attribute("type", info_type.value)
175
176
        # for single entity always request all details
177
        cmd.set_attribute("details", "1")
178
        return self._send_xml_command(cmd)
179
180
    def create_target(
181
        self,
182
        name: str,
183
        *,
184
        make_unique: Optional[bool] = None,
185
        asset_hosts_filter: Optional[str] = None,
186
        hosts: Optional[List[str]] = None,
187
        comment: Optional[str] = None,
188
        exclude_hosts: Optional[List[str]] = None,
189
        ssh_credential_id: Optional[str] = None,
190
        ssh_credential_port: Optional[int] = None,
191
        smb_credential_id: Optional[str] = None,
192
        esxi_credential_id: Optional[str] = None,
193
        snmp_credential_id: Optional[str] = None,
194
        alive_test: Optional[AliveTest] = None,
195
        reverse_lookup_only: Optional[bool] = None,
196
        reverse_lookup_unify: Optional[bool] = None,
197
        port_range: Optional[str] = None,
198
        port_list_id: Optional[str] = None
199
    ) -> Any:
200
        """Create a new target
201
202
        Arguments:
203
            name: Name of the target
204
            make_unique: Deprecated. Will be ignored.
205
            asset_hosts_filter: Filter to select target host from assets hosts
206
            hosts: List of hosts addresses to scan
207
            exclude_hosts: List of hosts addresses to exclude from scan
208
            comment: Comment for the target
209
            ssh_credential_id: UUID of a ssh credential to use on target
210
            ssh_credential_port: The port to use for ssh credential
211
            smb_credential_id: UUID of a smb credential to use on target
212
            snmp_credential_id: UUID of a snmp credential to use on target
213
            esxi_credential_id: UUID of a esxi credential to use on target
214
            alive_test: Which alive test to use
215
            reverse_lookup_only: Whether to scan only hosts that have names
216
            reverse_lookup_unify: Whether to scan only one IP when multiple IPs
217
                have the same name.
218
            port_range: Port range for the target
219
            port_list_id: UUID of the port list to use on target
220
221
        Returns:
222
            The response. See :py:meth:`send_command` for details.
223
        """
224
225
        cmd = XmlCommand("create_target")
226
        _xmlname = cmd.add_element("name", name)
227
228
        if make_unique is not None:
229
            warnings.warn(
230
                'create_target make_unique argument is deprecated '
231
                'and will be ignored.',
232
                DeprecationWarning,
233
            )
234
235
        if not name:
236
            raise RequiredArgument(
237
                function=self.create_target.__name__, argument='name'
238
            )
239
240
        if asset_hosts_filter:
241
            cmd.add_element(
242
                "asset_hosts", attrs={"filter": str(asset_hosts_filter)}
243
            )
244
        elif hosts:
245
            cmd.add_element("hosts", _to_comma_list(hosts))
246
        else:
247
            raise RequiredArgument(
248
                function=self.create_target.__name__,
249
                argument='hosts or asset_hosts_filter',
250
            )
251
252
        if comment:
253
            cmd.add_element("comment", comment)
254
255
        if exclude_hosts:
256
            cmd.add_element("exclude_hosts", _to_comma_list(exclude_hosts))
257
258
        if ssh_credential_id:
259
            _xmlssh = cmd.add_element(
260
                "ssh_credential", attrs={"id": ssh_credential_id}
261
            )
262
            if ssh_credential_port:
263
                _xmlssh.add_element("port", str(ssh_credential_port))
264
265
        if smb_credential_id:
266
            cmd.add_element("smb_credential", attrs={"id": smb_credential_id})
267
268
        if esxi_credential_id:
269
            cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id})
270
271
        if snmp_credential_id:
272
            cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id})
273
274
        if alive_test:
275
            if not isinstance(alive_test, AliveTest):
276
                raise InvalidArgumentType(
277
                    function=self.create_target.__name__,
278
                    argument='alive_test',
279
                    arg_type=AliveTest.__name__,
280
                )
281
282
            cmd.add_element("alive_tests", alive_test.value)
283
284
        if reverse_lookup_only is not None:
285
            cmd.add_element(
286
                "reverse_lookup_only", _to_bool(reverse_lookup_only)
287
            )
288
289
        if reverse_lookup_unify is not None:
290
            cmd.add_element(
291
                "reverse_lookup_unify", _to_bool(reverse_lookup_unify)
292
            )
293
294
        # since 20.08 one of port_range or port_list_id is required!
295
        if not port_range and not port_list_id:
296
            raise RequiredArgument(
297
                function=self.create_target.__name__,
298
                argument='port_range or port_list_id',
299
            )
300
301
        if port_range:
302
            cmd.add_element("port_range", port_range)
303
304
        if port_list_id:
305
            cmd.add_element("port_list", attrs={"id": port_list_id})
306
307
        return self._send_xml_command(cmd)
308