Completed
Push — master ( b519bb...486e1a )
by Jaspar
25s queued 15s
created

gvm.protocols.gmpv208.types   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 403
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 253
dl 0
loc 403
rs 9.28
c 0
b 0
f 0
wmc 39

10 Functions

Rating   Name   Duplication   Size   Complexity  
B get_filter_type_from_string() 0 33 8
A get_permission_subject_type_from_string() 0 20 3
A get_hosts_ordering_from_string() 0 17 3
A get_user_auth_type_from_string() 0 14 3
A get_aggregate_statistic_from_string() 0 21 3
A get_sort_order_from_string() 0 18 3
A get_feed_type_from_string() 0 11 3
A get_ticket_status_from_string() 0 14 3
B get_entity_type_from_string() 0 30 7
A get_time_unit_from_string() 0 12 3
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2019-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=too-many-lines
20
21
from enum import Enum
22
23
from typing import Optional
24
25
from gvm.errors import InvalidArgument
26
27
__all__ = [
28
    "AggregateStatistic",
29
    "EntityType",
30
    "FeedType",
31
    "FilterType",
32
    "HostsOrdering",
33
    "PermissionSubjectType",
34
    "SortOrder",
35
    "TicketStatus",
36
    "TimeUnit",
37
    "UserAuthType",
38
    "get_aggregate_statistic_from_string",
39
    "get_entity_type_from_string",
40
    "get_feed_type_from_string",
41
    "get_filter_type_from_string",
42
    "get_hosts_ordering_from_string",
43
    "get_permission_subject_type_from_string",
44
    "get_sort_order_from_string",
45
    "get_ticket_status_from_string",
46
    "get_time_unit_from_string",
47
    "get_user_auth_type_from_string",
48
]
49
50
51
class EntityType(Enum):
52
    """Enum for entity types"""
53
54
    ALERT = "alert"
55
    ASSET = "asset"
56
    AUDIT = "audit"
57
    CERT_BUND_ADV = "cert_bund_adv"
58
    CPE = "cpe"
59
    CREDENTIAL = "credential"
60
    CVE = "cve"
61
    DFN_CERT_ADV = "dfn_cert_adv"
62
    FILTER = "filter"
63
    GROUP = "group"
64
    HOST = "host"
65
    INFO = "info"
66
    NOTE = "note"
67
    NVT = "nvt"
68
    OPERATING_SYSTEM = "os"
69
    OVALDEF = "ovaldef"
70
    OVERRIDE = "override"
71
    PERMISSION = "permission"
72
    POLICY = "policy"
73
    PORT_LIST = "port_list"
74
    REPORT = "report"
75
    REPORT_FORMAT = "report_format"
76
    RESULT = "result"
77
    ROLE = "role"
78
    SCAN_CONFIG = "config"
79
    SCANNER = "scanner"
80
    SCHEDULE = "schedule"
81
    TAG = "tag"
82
    TARGET = "target"
83
    TASK = "task"
84
    TICKET = "ticket"
85
    TLS_CERTIFICATE = "tls_certificate"
86
    USER = "user"
87
    VULNERABILITY = "vuln"
88
89
90
def get_entity_type_from_string(
91
    entity_type: Optional[str],
92
) -> Optional[EntityType]:
93
    """Convert a entity type string to an actual EntityType instance
94
95
    Arguments:
96
        entity_type: Entity type string to convert to a EntityType
97
    """
98
    if not entity_type:
99
        return None
100
101
    if entity_type == 'vuln':
102
        return EntityType.VULNERABILITY
103
104
    if entity_type == 'os':
105
        return EntityType.OPERATING_SYSTEM
106
107
    if entity_type == 'config':
108
        return EntityType.SCAN_CONFIG
109
110
    if entity_type == 'tls_certificate':
111
        return EntityType.TLS_CERTIFICATE
112
113
    try:
114
        return EntityType[entity_type.upper()]
115
    except KeyError:
116
        raise InvalidArgument(
117
            argument='entity_type',
118
            function=get_entity_type_from_string.__name__,
119
        ) from None
120
121
122
class FeedType(Enum):
123
    """Enum for feed types"""
124
125
    NVT = "NVT"
126
    CERT = "CERT"
127
    SCAP = "SCAP"
128
    GVMD_DATA = "GVMD_DATA"
129
130
131
def get_feed_type_from_string(feed_type: Optional[str]) -> Optional[FeedType]:
132
    """Convert a feed type string into a FeedType instance"""
133
    if not feed_type:
134
        return None
135
136
    try:
137
        return FeedType[feed_type.upper()]
138
    except KeyError:
139
        raise InvalidArgument(
140
            argument='feed_type', function=get_feed_type_from_string.__name__
141
        ) from None
142
143
144
class FilterType(Enum):
145
    """Enum for filter types"""
146
147
    ALERT = "alert"
148
    ASSET = "asset"
149
    SCAN_CONFIG = "config"
150
    CREDENTIAL = "credential"
151
    FILTER = "filter"
152
    GROUP = "group"
153
    HOST = "host"
154
    NOTE = "note"
155
    OPERATING_SYSTEM = "os"
156
    OVERRIDE = "override"
157
    PERMISSION = "permission"
158
    PORT_LIST = "port_list"
159
    REPORT = "report"
160
    REPORT_FORMAT = "report_format"
161
    RESULT = "result"
162
    ROLE = "role"
163
    SCHEDULE = "schedule"
164
    ALL_SECINFO = "secinfo"
165
    TAG = "tag"
166
    TARGET = "target"
167
    TASK = "task"
168
    TICKET = "ticket"
169
    TLS_CERTIFICATE = "tls_certificate"
170
    USER = "user"
171
    VULNERABILITY = "vuln"
172
173
174
def get_filter_type_from_string(
175
    filter_type: Optional[str],
176
) -> Optional[FilterType]:
177
    """Convert a filter type string to an actual FilterType instance
178
179
    Arguments:
180
        filter_type (str): Filter type string to convert to a FilterType
181
    """
182
    if not filter_type:
183
        return None
184
185
    if filter_type == 'vuln':
186
        return FilterType.VULNERABILITY
187
188
    if filter_type == 'os':
189
        return FilterType.OPERATING_SYSTEM
190
191
    if filter_type == 'config':
192
        return FilterType.SCAN_CONFIG
193
194
    if filter_type == 'secinfo':
195
        return FilterType.ALL_SECINFO
196
197
    if filter_type == 'tls_certificate':
198
        return FilterType.TLS_CERTIFICATE
199
200
    try:
201
        return FilterType[filter_type.upper()]
202
    except KeyError:
203
        raise InvalidArgument(
204
            argument='filter_type',
205
            function=get_filter_type_from_string.__name__,
206
        ) from None
207
208
209
class AggregateStatistic(Enum):
210
    """Enum for aggregate statistic types"""
211
212
    COUNT = "count"  # Number of items
213
    C_COUNT = "c_count"  # Cumulative number of items
214
    C_SUM = "c_sum"  # Cumulative sum of values
215
    MAX = "max"  # Maximum value
216
    MEAN = "mean"  # Arithmetic mean of values
217
    MIN = "min"  # Minimum value
218
    SUM = "sum"  # Sum of values
219
    TEXT = "text"  # Text column value
220
    VALUE = "value"  # Group or subgroup column value
221
222
223
def get_aggregate_statistic_from_string(
224
    aggregate_statistic: Optional[str],
225
) -> Optional[AggregateStatistic]:
226
    """
227
    Convert a aggregate statistic string to an actual AggregateStatistic
228
    instance.
229
230
    Arguments:
231
        aggregate_statistic: Aggregate statistic string to convert to a
232
            AggregateStatistic
233
    """
234
    if not aggregate_statistic:
235
        return None
236
237
    try:
238
        return AggregateStatistic[aggregate_statistic.upper()]
239
    except KeyError:
240
        raise InvalidArgument(
241
            argument='aggregate_statistic',
242
            function=get_aggregate_statistic_from_string.__name__,
243
        ) from None
244
245
246
class SortOrder(Enum):
247
    """Enum for sort order"""
248
249
    ASCENDING = "ascending"
250
    DESCENDING = "descending"
251
252
253
def get_sort_order_from_string(
254
    sort_order: Optional[str],
255
) -> Optional[SortOrder]:
256
    """
257
    Convert a sort order string to an actual SortOrder instance.
258
259
    Arguments:
260
        sort_order: Sort order string to convert to a SortOrder
261
    """
262
    if not sort_order:
263
        return None
264
265
    try:
266
        return SortOrder[sort_order.upper()]
267
    except KeyError:
268
        raise InvalidArgument(
269
            argument='sort_order', function=get_sort_order_from_string.__name__
270
        ) from None
271
272
273
class TicketStatus(Enum):
274
    """Enum for ticket status"""
275
276
    OPEN = 'Open'
277
    FIXED = 'Fixed'
278
    CLOSED = 'Closed'
279
280
281
def get_ticket_status_from_string(
282
    ticket_status: Optional[str],
283
) -> Optional[TicketStatus]:
284
    """Convert a ticket status string into a TicketStatus instance"""
285
    if not ticket_status:
286
        return None
287
288
    try:
289
        return TicketStatus[ticket_status.upper()]
290
    except KeyError:
291
        raise InvalidArgument(
292
            argument='ticket_status',
293
            function=get_ticket_status_from_string.__name__,
294
        ) from None
295
296
297
class HostsOrdering(Enum):
298
    """Enum for host ordering during scans"""
299
300
    SEQUENTIAL = "sequential"
301
    RANDOM = "random"
302
    REVERSE = "reverse"
303
304
305
def get_hosts_ordering_from_string(
306
    hosts_ordering: Optional[str],
307
) -> Optional[HostsOrdering]:
308
    """Convert a hosts ordering string to an actual HostsOrdering instance
309
310
    Arguments:
311
        hosts_ordering: Host ordering string to convert to a HostsOrdering
312
    """
313
    if not hosts_ordering:
314
        return None
315
    try:
316
        return HostsOrdering[hosts_ordering.upper()]
317
    except KeyError:
318
        raise InvalidArgument(
319
            argument='hosts_ordering',
320
            function=get_hosts_ordering_from_string.__name__,
321
        ) from None
322
323
324
class PermissionSubjectType(Enum):
325
    """Enum for permission subject type"""
326
327
    USER = 'user'
328
    GROUP = 'group'
329
    ROLE = 'role'
330
331
332
def get_permission_subject_type_from_string(
333
    subject_type: Optional[str],
334
) -> Optional[PermissionSubjectType]:
335
    """Convert a permission subject type string to an actual
336
    PermissionSubjectType instance
337
338
    Arguments:
339
        subject_type: Permission subject type string to convert to a
340
            PermissionSubjectType
341
    """
342
    if not subject_type:
343
        return None
344
345
    try:
346
        return PermissionSubjectType[subject_type.upper()]
347
    except KeyError:
348
        raise InvalidArgument(
349
            argument='subject_type',
350
            function=get_permission_subject_type_from_string.__name__,
351
        ) from None
352
353
354
class TimeUnit(Enum):
355
    """Enum for time units"""
356
357
    SECOND = "second"
358
    MINUTE = "minute"
359
    HOUR = "hour"
360
    DAY = "day"
361
    WEEK = "week"
362
    MONTH = "month"
363
    YEAR = "year"
364
    DECADE = "decade"
365
366
367
def get_time_unit_from_string(time_unit: Optional[str]) -> Optional[TimeUnit]:
368
    """Convert a time unit string into a TimeUnit instance"""
369
    if not time_unit:
370
        return None
371
372
    try:
373
        return TimeUnit[time_unit.upper()]
374
    except KeyError:
375
        raise InvalidArgument(
376
            argument='severity_level',
377
            function=get_time_unit_from_string.__name__,
378
        ) from None
379
380
381
class UserAuthType(Enum):
382
    """Enum for Sources allowed for authentication for the user"""
383
384
    FILE = 'file'
385
    LDAP_CONNECT = 'ldap_connect'
386
    RADIUS_CONNECT = 'radius_connect'
387
388
389
def get_user_auth_type_from_string(
390
    user_auth_type: Optional[str],
391
) -> Optional[UserAuthType]:
392
    """Convert a user auth type string into a UserAuthType instance"""
393
    if not user_auth_type:
394
        return None
395
396
    try:
397
        return UserAuthType[user_auth_type.upper()]
398
    except KeyError:
399
        raise InvalidArgument(
400
            argument='user_auth_type',
401
            function=get_user_auth_type_from_string.__name__,
402
        ) from None
403