Completed
Push — master ( 8c2aaa...776edf )
by
unknown
13s queued 11s
created

gvm.protocols.gmpv8.types   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 280
Duplicated Lines 47.5 %

Importance

Changes 0
Metric Value
eloc 199
dl 133
loc 280
rs 10
c 0
b 0
f 0
wmc 19

4 Functions

Rating   Name   Duplication   Size   Complexity  
A get_credential_type_from_string() 14 14 3
B get_filter_type_from_string() 29 29 7
B get_entity_type_from_string() 26 26 6
A get_ticket_status_from_string() 0 14 3

How to fix   Duplicated Code   

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:

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 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
from enum import Enum
19
20
from typing import Optional
21
22
from gvm.errors import InvalidArgument
23
24
from gvm.protocols.gmpv7.types import (
25
    AlertCondition,
26
    AlertEvent,
27
    AlertMethod,
28
    AliveTest,
29
    AssetType,
30
    CredentialFormat,
31
    FeedType,
32
    HostsOrdering,
33
    InfoType,
34
    PermissionSubjectType,
35
    PortRangeType,
36
    ScannerType,
37
    SeverityLevel,
38
    SnmpAuthAlgorithm,
39
    SnmpPrivacyAlgorithm,
40
    get_alert_condition_from_string,
41
    get_alert_event_from_string,
42
    get_alert_method_from_string,
43
    get_alive_test_from_string,
44
    get_asset_type_from_string,
45
    get_credential_format_from_string,
46
    get_feed_type_from_string,
47
    get_hosts_ordering_from_string,
48
    get_info_type_from_string,
49
    get_permission_subject_type_from_string,
50
    get_port_range_type_from_string,
51
    get_severity_level_from_string,
52
    get_scanner_type_from_string,
53
    get_snmp_auth_algorithm_from_string,
54
    get_snmp_privacy_algorithm_from_string,
55
)
56
57
58
__all__ = [
59
    "AlertCondition",
60
    "AlertEvent",
61
    "AlertMethod",
62
    "AliveTest",
63
    "AssetType",
64
    "CredentialType",
65
    "CredentialFormat",
66
    "EntityType",
67
    "FeedType",
68
    "FilterType",
69
    "HostsOrdering",
70
    "InfoType",
71
    "PermissionSubjectType",
72
    "PortRangeType",
73
    "ScannerType",
74
    "SeverityLevel",
75
    "SnmpAuthAlgorithm",
76
    "SnmpPrivacyAlgorithm",
77
    "TicketStatus",
78
    "get_alert_condition_from_string",
79
    "get_alert_event_from_string",
80
    "get_alert_method_from_string",
81
    "get_alive_test_from_string",
82
    "get_asset_type_from_string",
83
    "get_credential_format_from_string",
84
    "get_credential_type_from_string",
85
    "get_entity_type_from_string",
86
    "get_feed_type_from_string",
87
    "get_filter_type_from_string",
88
    "get_hosts_ordering_from_string",
89
    "get_info_type_from_string",
90
    "get_permission_subject_type_from_string",
91
    "get_port_range_type_from_string",
92
    "get_scanner_type_from_string",
93
    "get_severity_level_from_string",
94
    "get_snmp_auth_algorithm_from_string",
95
    "get_snmp_privacy_algorithm_from_string",
96
    "get_ticket_status_from_string",
97
]
98
99
100 View Code Duplication
class EntityType(Enum):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
101
    """ Enum for entity types """
102
103
    AGENT = "note"
104
    ALERT = "alert"
105
    ASSET = "asset"
106
    CERT_BUND_ADV = "cert_bund_adv"
107
    CPE = "cpe"
108
    CREDENTIAL = "credential"
109
    CVE = "cve"
110
    DFN_CERT_ADV = "dfn_cert_adv"
111
    FILTER = "filter"
112
    GROUP = "group"
113
    HOST = "host"
114
    INFO = "info"
115
    NOTE = "note"
116
    NVT = "nvt"
117
    OPERATING_SYSTEM = "os"
118
    OVALDEF = "ovaldef"
119
    OVERRIDE = "override"
120
    PERMISSION = "permission"
121
    PORT_LIST = "port_list"
122
    REPORT = "report"
123
    REPORT_FORMAT = "report_format"
124
    RESULT = "result"
125
    ROLE = "role"
126
    SCAN_CONFIG = "config"
127
    SCANNER = "scanner"
128
    SCHEDULE = "schedule"
129
    TAG = "tag"
130
    TARGET = "target"
131
    TASK = "task"
132
    USER = "user"
133
134
    TICKET = "ticket"
135
    VULNERABILITY = "vuln"
136
137
138 View Code Duplication
def get_entity_type_from_string(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
139
    entity_type: Optional[str]
140
) -> Optional[EntityType]:
141
    """ Convert a entity type string to an actual EntityType instance
142
143
    Arguments:
144
        entity_type: Entity type string to convert to a EntityType
145
    """
146
    if not entity_type:
147
        return None
148
149
    if entity_type == 'vuln':
150
        return EntityType.VULNERABILITY
151
152
    if entity_type == 'os':
153
        return EntityType.OPERATING_SYSTEM
154
155
    if entity_type == 'config':
156
        return EntityType.SCAN_CONFIG
157
158
    try:
159
        return EntityType[entity_type.upper()]
160
    except KeyError:
161
        raise InvalidArgument(
162
            argument='entity_type',
163
            function=get_entity_type_from_string.__name__,
164
        )
165
166
167 View Code Duplication
class FilterType(Enum):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
168
    """ Enum for filter types """
169
170
    AGENT = "agent"
171
    ALERT = "alert"
172
    ASSET = "asset"
173
    SCAN_CONFIG = "config"
174
    CREDENTIAL = "credential"
175
    FILTER = "filter"
176
    GROUP = "group"
177
    HOST = "host"
178
    NOTE = "note"
179
    OPERATING_SYSTEM = "os"
180
    OVERRIDE = "override"
181
    PERMISSION = "permission"
182
    PORT_LIST = "port_list"
183
    REPORT = "report"
184
    REPORT_FORMAT = "report_format"
185
    RESULT = "result"
186
    ROLE = "role"
187
    SCHEDULE = "schedule"
188
    ALL_SECINFO = "secinfo"
189
    TAG = "tag"
190
    TARGET = "target"
191
    TASK = "task"
192
    TICKET = "ticket"
193
    USER = "user"
194
    VULNERABILITY = "vuln"
195
196
197 View Code Duplication
def get_filter_type_from_string(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
198
    filter_type: Optional[str]
199
) -> Optional[FilterType]:
200
    """ Convert a filter type string to an actual FilterType instance
201
202
    Arguments:
203
        filter_type (str): Filter type string to convert to a FilterType
204
    """
205
    if not filter_type:
206
        return None
207
208
    if filter_type == 'vuln':
209
        return FilterType.VULNERABILITY
210
211
    if filter_type == 'os':
212
        return FilterType.OPERATING_SYSTEM
213
214
    if filter_type == 'config':
215
        return FilterType.SCAN_CONFIG
216
217
    if filter_type == 'secinfo':
218
        return FilterType.ALL_SECINFO
219
220
    try:
221
        return FilterType[filter_type.upper()]
222
    except KeyError:
223
        raise InvalidArgument(
224
            argument='filter_type',
225
            function=get_filter_type_from_string.__name__,
226
        )
227
228
229
class CredentialType(Enum):
230
    """ Enum for credential types """
231
232
    CLIENT_CERTIFICATE = 'cc'
233
    SNMP = 'snmp'
234
    USERNAME_PASSWORD = 'up'
235
    USERNAME_SSH_KEY = 'usk'
236
    SMIME_CERTIFICATE = 'smime'
237
    PGP_ENCRYPTION_KEY = 'pgp'
238
    PASSWORD_ONLY = 'pw'
239
240
241 View Code Duplication
def get_credential_type_from_string(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
242
    credential_type: Optional[str]
243
) -> Optional[CredentialType]:
244
    """ Convert a credential type string into a CredentialType instance
245
    """
246
    if not credential_type:
247
        return None
248
249
    try:
250
        return CredentialType[credential_type.upper()]
251
    except KeyError:
252
        raise InvalidArgument(
253
            argument='credential_type',
254
            function=get_credential_type_from_string.__name__,
255
        )
256
257
258
class TicketStatus(Enum):
259
    """ Enum for ticket status """
260
261
    OPEN = 'Open'
262
    FIXED = 'Fixed'
263
    CLOSED = 'Closed'
264
265
266
def get_ticket_status_from_string(
267
    ticket_status: Optional[str]
268
) -> Optional[TicketStatus]:
269
    """ Convert a ticket status string into a TicketStatus instance
270
    """
271
    if not ticket_status:
272
        return None
273
274
    try:
275
        return TicketStatus[ticket_status.upper()]
276
    except KeyError:
277
        raise InvalidArgument(
278
            argument='ticket_status',
279
            function=get_ticket_status_from_string.__name__,
280
        )
281