Total Complexity | 137 |
Total Lines | 1021 |
Duplicated Lines | 60.82 % |
Changes | 0 |
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:
Complex classes like gvm.protocols.gmpv208.types often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
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 | "AlertCondition", |
||
30 | "AlertEvent", |
||
31 | "AlertMethod", |
||
32 | "AliveTest", |
||
33 | "AssetType", |
||
34 | "CredentialFormat", |
||
35 | "CredentialType", |
||
36 | "EntityType", |
||
37 | "FeedType", |
||
38 | "FilterType", |
||
39 | "HostsOrdering", |
||
40 | "InfoType", |
||
41 | "PermissionSubjectType", |
||
42 | "PortRangeType", |
||
43 | "ReportFormatType", |
||
44 | "ScannerType", |
||
45 | "SeverityLevel", |
||
46 | "SnmpAuthAlgorithm", |
||
47 | "SnmpPrivacyAlgorithm", |
||
48 | "SortOrder", |
||
49 | "TicketStatus", |
||
50 | "TimeUnit", |
||
51 | "UserAuthType", |
||
52 | "get_aggregate_statistic_from_string", |
||
53 | "get_alert_condition_from_string", |
||
54 | "get_alert_event_from_string", |
||
55 | "get_alert_method_from_string", |
||
56 | "get_alive_test_from_string", |
||
57 | "get_asset_type_from_string", |
||
58 | "get_credential_format_from_string", |
||
59 | "get_credential_type_from_string", |
||
60 | "get_entity_type_from_string", |
||
61 | "get_feed_type_from_string", |
||
62 | "get_filter_type_from_string", |
||
63 | "get_hosts_ordering_from_string", |
||
64 | "get_info_type_from_string", |
||
65 | "get_permission_subject_type_from_string", |
||
66 | "get_port_range_type_from_string", |
||
67 | "get_report_format_id_from_string", |
||
68 | "get_scanner_type_from_string", |
||
69 | "get_severity_level_from_string", |
||
70 | "get_snmp_auth_algorithm_from_string", |
||
71 | "get_snmp_privacy_algorithm_from_string", |
||
72 | "get_sort_order_from_string", |
||
73 | "get_ticket_status_from_string", |
||
74 | "get_time_unit_from_string", |
||
75 | "get_user_auth_type_from_string", |
||
76 | ] |
||
77 | |||
78 | |||
79 | class EntityType(Enum): |
||
80 | """Enum for entity types""" |
||
81 | |||
82 | ALERT = "alert" |
||
83 | ASSET = "asset" |
||
84 | AUDIT = "audit" |
||
85 | CERT_BUND_ADV = "cert_bund_adv" |
||
86 | CPE = "cpe" |
||
87 | CREDENTIAL = "credential" |
||
88 | CVE = "cve" |
||
89 | DFN_CERT_ADV = "dfn_cert_adv" |
||
90 | FILTER = "filter" |
||
91 | GROUP = "group" |
||
92 | HOST = "host" |
||
93 | INFO = "info" |
||
94 | NOTE = "note" |
||
95 | NVT = "nvt" |
||
96 | OPERATING_SYSTEM = "os" |
||
97 | OVALDEF = "ovaldef" |
||
98 | OVERRIDE = "override" |
||
99 | PERMISSION = "permission" |
||
100 | POLICY = "policy" |
||
101 | PORT_LIST = "port_list" |
||
102 | REPORT = "report" |
||
103 | REPORT_FORMAT = "report_format" |
||
104 | RESULT = "result" |
||
105 | ROLE = "role" |
||
106 | SCAN_CONFIG = "config" |
||
107 | SCANNER = "scanner" |
||
108 | SCHEDULE = "schedule" |
||
109 | TAG = "tag" |
||
110 | TARGET = "target" |
||
111 | TASK = "task" |
||
112 | TICKET = "ticket" |
||
113 | TLS_CERTIFICATE = "tls_certificate" |
||
114 | USER = "user" |
||
115 | VULNERABILITY = "vuln" |
||
116 | |||
117 | |||
118 | View Code Duplication | def get_entity_type_from_string( |
|
|
|||
119 | entity_type: Optional[str], |
||
120 | ) -> Optional[EntityType]: |
||
121 | """Convert a entity type string to an actual EntityType instance |
||
122 | |||
123 | Arguments: |
||
124 | entity_type: Entity type string to convert to a EntityType |
||
125 | """ |
||
126 | if not entity_type: |
||
127 | return None |
||
128 | |||
129 | if entity_type == 'vuln': |
||
130 | return EntityType.VULNERABILITY |
||
131 | |||
132 | if entity_type == 'os': |
||
133 | return EntityType.OPERATING_SYSTEM |
||
134 | |||
135 | if entity_type == 'config': |
||
136 | return EntityType.SCAN_CONFIG |
||
137 | |||
138 | if entity_type == 'tls_certificate': |
||
139 | return EntityType.TLS_CERTIFICATE |
||
140 | |||
141 | try: |
||
142 | return EntityType[entity_type.upper()] |
||
143 | except KeyError: |
||
144 | raise InvalidArgument( |
||
145 | argument='entity_type', |
||
146 | function=get_entity_type_from_string.__name__, |
||
147 | ) from None |
||
148 | |||
149 | |||
150 | class FeedType(Enum): |
||
151 | """Enum for feed types""" |
||
152 | |||
153 | NVT = "NVT" |
||
154 | CERT = "CERT" |
||
155 | SCAP = "SCAP" |
||
156 | GVMD_DATA = "GVMD_DATA" |
||
157 | |||
158 | |||
159 | View Code Duplication | def get_feed_type_from_string(feed_type: Optional[str]) -> Optional[FeedType]: |
|
160 | """Convert a feed type string into a FeedType instance""" |
||
161 | if not feed_type: |
||
162 | return None |
||
163 | |||
164 | try: |
||
165 | return FeedType[feed_type.upper()] |
||
166 | except KeyError: |
||
167 | raise InvalidArgument( |
||
168 | argument='feed_type', function=get_feed_type_from_string.__name__ |
||
169 | ) from None |
||
170 | |||
171 | |||
172 | View Code Duplication | class FilterType(Enum): |
|
173 | """Enum for filter types""" |
||
174 | |||
175 | ALERT = "alert" |
||
176 | ASSET = "asset" |
||
177 | SCAN_CONFIG = "config" |
||
178 | CREDENTIAL = "credential" |
||
179 | FILTER = "filter" |
||
180 | GROUP = "group" |
||
181 | HOST = "host" |
||
182 | NOTE = "note" |
||
183 | OPERATING_SYSTEM = "os" |
||
184 | OVERRIDE = "override" |
||
185 | PERMISSION = "permission" |
||
186 | PORT_LIST = "port_list" |
||
187 | REPORT = "report" |
||
188 | REPORT_FORMAT = "report_format" |
||
189 | RESULT = "result" |
||
190 | ROLE = "role" |
||
191 | SCHEDULE = "schedule" |
||
192 | ALL_SECINFO = "secinfo" |
||
193 | TAG = "tag" |
||
194 | TARGET = "target" |
||
195 | TASK = "task" |
||
196 | TICKET = "ticket" |
||
197 | TLS_CERTIFICATE = "tls_certificate" |
||
198 | USER = "user" |
||
199 | VULNERABILITY = "vuln" |
||
200 | |||
201 | |||
202 | View Code Duplication | def get_filter_type_from_string( |
|
203 | filter_type: Optional[str], |
||
204 | ) -> Optional[FilterType]: |
||
205 | """Convert a filter type string to an actual FilterType instance |
||
206 | |||
207 | Arguments: |
||
208 | filter_type (str): Filter type string to convert to a FilterType |
||
209 | """ |
||
210 | if not filter_type: |
||
211 | return None |
||
212 | |||
213 | if filter_type == 'vuln': |
||
214 | return FilterType.VULNERABILITY |
||
215 | |||
216 | if filter_type == 'os': |
||
217 | return FilterType.OPERATING_SYSTEM |
||
218 | |||
219 | if filter_type == 'config': |
||
220 | return FilterType.SCAN_CONFIG |
||
221 | |||
222 | if filter_type == 'secinfo': |
||
223 | return FilterType.ALL_SECINFO |
||
224 | |||
225 | if filter_type == 'tls_certificate': |
||
226 | return FilterType.TLS_CERTIFICATE |
||
227 | |||
228 | try: |
||
229 | return FilterType[filter_type.upper()] |
||
230 | except KeyError: |
||
231 | raise InvalidArgument( |
||
232 | argument='filter_type', |
||
233 | function=get_filter_type_from_string.__name__, |
||
234 | ) from None |
||
235 | |||
236 | |||
237 | class InfoType(Enum): |
||
238 | """Enum for info types""" |
||
239 | |||
240 | CERT_BUND_ADV = "CERT_BUND_ADV" |
||
241 | CPE = "CPE" |
||
242 | CVE = "CVE" |
||
243 | DFN_CERT_ADV = "DFN_CERT_ADV" |
||
244 | OVALDEF = "OVALDEF" |
||
245 | NVT = "NVT" |
||
246 | |||
247 | |||
248 | View Code Duplication | def get_info_type_from_string(info_type: Optional[str]) -> Optional[InfoType]: |
|
249 | """Convert a info type string to an actual InfoType instance |
||
250 | |||
251 | Arguments: |
||
252 | info_type: Info type string to convert to a InfoType |
||
253 | """ |
||
254 | if not info_type: |
||
255 | return None |
||
256 | try: |
||
257 | return InfoType[info_type.upper()] |
||
258 | except KeyError: |
||
259 | raise InvalidArgument( |
||
260 | argument='info_type', function=get_info_type_from_string.__name__ |
||
261 | ) from None |
||
262 | |||
263 | |||
264 | class _UsageType(Enum): |
||
265 | """Enum for usage types""" |
||
266 | |||
267 | AUDIT = "audit" |
||
268 | POLICY = "policy" |
||
269 | SCAN = "scan" |
||
270 | |||
271 | |||
272 | View Code Duplication | def __get_usage_type_from_string( |
|
273 | usage_type: Optional[str], |
||
274 | ) -> Optional[_UsageType]: |
||
275 | """Convert a usage type string to an actual _UsageType instance |
||
276 | |||
277 | Arguments: |
||
278 | entity_type: Usage type string to convert to a _UsageType |
||
279 | """ |
||
280 | if not usage_type: |
||
281 | return None |
||
282 | |||
283 | try: |
||
284 | return _UsageType[usage_type.upper()] |
||
285 | except KeyError: |
||
286 | raise InvalidArgument( |
||
287 | argument='usage_type', |
||
288 | function=__get_usage_type_from_string.__name__, |
||
289 | ) from None |
||
290 | |||
291 | |||
292 | class AggregateStatistic(Enum): |
||
293 | """Enum for aggregate statistic types""" |
||
294 | |||
295 | COUNT = "count" # Number of items |
||
296 | C_COUNT = "c_count" # Cumulative number of items |
||
297 | C_SUM = "c_sum" # Cumulative sum of values |
||
298 | MAX = "max" # Maximum value |
||
299 | MEAN = "mean" # Arithmetic mean of values |
||
300 | MIN = "min" # Minimum value |
||
301 | SUM = "sum" # Sum of values |
||
302 | TEXT = "text" # Text column value |
||
303 | VALUE = "value" # Group or subgroup column value |
||
304 | |||
305 | |||
306 | View Code Duplication | def get_aggregate_statistic_from_string( |
|
307 | aggregate_statistic: Optional[str], |
||
308 | ) -> Optional[AggregateStatistic]: |
||
309 | """ |
||
310 | Convert a aggregate statistic string to an actual AggregateStatistic |
||
311 | instance. |
||
312 | |||
313 | Arguments: |
||
314 | aggregate_statistic: Aggregate statistic string to convert to a |
||
315 | AggregateStatistic |
||
316 | """ |
||
317 | if not aggregate_statistic: |
||
318 | return None |
||
319 | |||
320 | try: |
||
321 | return AggregateStatistic[aggregate_statistic.upper()] |
||
322 | except KeyError: |
||
323 | raise InvalidArgument( |
||
324 | argument='aggregate_statistic', |
||
325 | function=get_aggregate_statistic_from_string.__name__, |
||
326 | ) from None |
||
327 | |||
328 | |||
329 | class AlertEvent(Enum): |
||
330 | """Enum for alert event types""" |
||
331 | |||
332 | TASK_RUN_STATUS_CHANGED = 'Task run status changed' |
||
333 | UPDATED_SECINFO_ARRIVED = 'Updated SecInfo arrived' |
||
334 | NEW_SECINFO_ARRIVED = 'New SecInfo arrived' |
||
335 | TICKET_RECEIVED = 'Ticket received' |
||
336 | ASSIGNED_TICKET_CHANGED = 'Assigned ticket changed' |
||
337 | OWNED_TICKET_CHANGED = 'Owned ticket changed' |
||
338 | |||
339 | |||
340 | View Code Duplication | def get_alert_event_from_string( |
|
341 | alert_event: Optional[str], |
||
342 | ) -> Optional[AlertEvent]: |
||
343 | """Convert an alert event string into a AlertEvent instance""" |
||
344 | if not alert_event: |
||
345 | return None |
||
346 | |||
347 | alert_event = alert_event.lower() |
||
348 | |||
349 | if alert_event == 'task run status changed': |
||
350 | return AlertEvent.TASK_RUN_STATUS_CHANGED |
||
351 | |||
352 | if alert_event == 'updated secinfo arrived': |
||
353 | return AlertEvent.UPDATED_SECINFO_ARRIVED |
||
354 | |||
355 | if alert_event == 'new secinfo arrived': |
||
356 | return AlertEvent.NEW_SECINFO_ARRIVED |
||
357 | |||
358 | if alert_event == 'ticket received': |
||
359 | return AlertEvent.TICKET_RECEIVED |
||
360 | |||
361 | if alert_event == 'assigned ticket changed': |
||
362 | return AlertEvent.ASSIGNED_TICKET_CHANGED |
||
363 | |||
364 | if alert_event == 'owned ticket changed': |
||
365 | return AlertEvent.OWNED_TICKET_CHANGED |
||
366 | |||
367 | raise InvalidArgument( |
||
368 | argument='alert_event', function=get_alert_event_from_string.__name__ |
||
369 | ) |
||
370 | |||
371 | |||
372 | class AlertCondition(Enum): |
||
373 | """Enum for alert condition types""" |
||
374 | |||
375 | ALWAYS = 'Always' |
||
376 | ERROR = 'Error' |
||
377 | SEVERITY_AT_LEAST = 'Severity at least' |
||
378 | SEVERITY_CHANGED = 'Severity changed' |
||
379 | FILTER_COUNT_CHANGED = 'Filter count changed' |
||
380 | FILTER_COUNT_AT_LEAST = 'Filter count at least' |
||
381 | |||
382 | |||
383 | View Code Duplication | def get_alert_condition_from_string( |
|
384 | alert_condition: Optional[str], |
||
385 | ) -> Optional[AlertCondition]: |
||
386 | """Convert an alert condition string into a AlertCondition instance""" |
||
387 | if not alert_condition: |
||
388 | return None |
||
389 | |||
390 | alert_condition = alert_condition.lower() |
||
391 | |||
392 | if alert_condition == 'error': |
||
393 | return AlertCondition.ERROR |
||
394 | |||
395 | if alert_condition == 'always': |
||
396 | return AlertCondition.ALWAYS |
||
397 | |||
398 | if alert_condition == 'filter count changed': |
||
399 | return AlertCondition.FILTER_COUNT_CHANGED |
||
400 | |||
401 | if alert_condition == 'filter count at least': |
||
402 | return AlertCondition.FILTER_COUNT_AT_LEAST |
||
403 | |||
404 | if alert_condition == 'severity at least': |
||
405 | return AlertCondition.SEVERITY_AT_LEAST |
||
406 | |||
407 | if alert_condition == 'severity changed': |
||
408 | return AlertCondition.SEVERITY_CHANGED |
||
409 | |||
410 | raise InvalidArgument( |
||
411 | argument='alert_condition', |
||
412 | function=get_alert_condition_from_string.__name__, |
||
413 | ) |
||
414 | |||
415 | |||
416 | class AlertMethod(Enum): |
||
417 | """Enum for alert method type""" |
||
418 | |||
419 | SCP = "SCP" |
||
420 | SEND = "Send" |
||
421 | SMB = "SMB" |
||
422 | SNMP = "SNMP" |
||
423 | SYSLOG = "Syslog" |
||
424 | EMAIL = "Email" |
||
425 | START_TASK = "Start Task" |
||
426 | HTTP_GET = "HTTP Get" |
||
427 | SOURCEFIRE_CONNECTOR = "Sourcefire Connector" |
||
428 | VERINICE_CONNECTOR = "verinice Connector" |
||
429 | TIPPINGPOINT = "TippingPoint SMS" |
||
430 | ALEMBA_VFIRE = "Alemba vFire" |
||
431 | |||
432 | |||
433 | View Code Duplication | def get_alert_method_from_string( |
|
434 | alert_method: Optional[str], |
||
435 | ) -> Optional[AlertMethod]: |
||
436 | """Convert an alert method string into a AlertCondition instance""" |
||
437 | if not alert_method: |
||
438 | return None |
||
439 | |||
440 | alert_method = alert_method.upper() |
||
441 | |||
442 | if alert_method == 'START TASK': |
||
443 | return AlertMethod.START_TASK |
||
444 | |||
445 | if alert_method == 'HTTP GET': |
||
446 | return AlertMethod.HTTP_GET |
||
447 | |||
448 | if alert_method == 'SOURCEFIRE CONNECTOR': |
||
449 | return AlertMethod.SOURCEFIRE_CONNECTOR |
||
450 | |||
451 | if alert_method == 'VERINICE CONNECTOR': |
||
452 | return AlertMethod.VERINICE_CONNECTOR |
||
453 | |||
454 | if alert_method == 'TIPPINGPOINT SMS': |
||
455 | return AlertMethod.TIPPINGPOINT |
||
456 | |||
457 | if alert_method == 'ALEMBA VFIRE': |
||
458 | return AlertMethod.ALEMBA_VFIRE |
||
459 | |||
460 | try: |
||
461 | return AlertMethod[alert_method] |
||
462 | except KeyError: |
||
463 | raise InvalidArgument( |
||
464 | argument='alert_method', |
||
465 | function=get_alert_method_from_string.__name__, |
||
466 | ) from None |
||
467 | |||
468 | |||
469 | class ScannerType(Enum): |
||
470 | """Enum for scanner type""" |
||
471 | |||
472 | OSP_SCANNER_TYPE = "1" |
||
473 | OPENVAS_SCANNER_TYPE = "2" |
||
474 | CVE_SCANNER_TYPE = "3" |
||
475 | GMP_SCANNER_TYPE = "4" # formerly slave scanner |
||
476 | GREENBONE_SENSOR_SCANNER_TYPE = "5" |
||
477 | |||
478 | |||
479 | View Code Duplication | def get_scanner_type_from_string( |
|
480 | scanner_type: Optional[str], |
||
481 | ) -> Optional[ScannerType]: |
||
482 | """Convert a scanner type string to an actual ScannerType instance |
||
483 | |||
484 | Arguments: |
||
485 | scanner_type: Scanner type string to convert to a ScannerType |
||
486 | """ |
||
487 | if not scanner_type: |
||
488 | return None |
||
489 | |||
490 | scanner_type = scanner_type.lower() |
||
491 | |||
492 | if ( |
||
493 | scanner_type == ScannerType.OSP_SCANNER_TYPE.value |
||
494 | or scanner_type == 'osp' |
||
495 | ): |
||
496 | return ScannerType.OSP_SCANNER_TYPE |
||
497 | |||
498 | if ( |
||
499 | scanner_type == ScannerType.OPENVAS_SCANNER_TYPE.value |
||
500 | or scanner_type == 'openvas' |
||
501 | ): |
||
502 | return ScannerType.OPENVAS_SCANNER_TYPE |
||
503 | |||
504 | if ( |
||
505 | scanner_type == ScannerType.CVE_SCANNER_TYPE.value |
||
506 | or scanner_type == 'cve' |
||
507 | ): |
||
508 | return ScannerType.CVE_SCANNER_TYPE |
||
509 | |||
510 | if ( |
||
511 | scanner_type == ScannerType.GMP_SCANNER_TYPE.value |
||
512 | or scanner_type == 'gmp' |
||
513 | ): |
||
514 | return ScannerType.GMP_SCANNER_TYPE |
||
515 | |||
516 | if ( |
||
517 | scanner_type == ScannerType.GREENBONE_SENSOR_SCANNER_TYPE.value |
||
518 | or scanner_type == 'greenbone' |
||
519 | ): |
||
520 | return ScannerType.GREENBONE_SENSOR_SCANNER_TYPE |
||
521 | |||
522 | raise InvalidArgument( |
||
523 | argument='scanner_type', function=get_scanner_type_from_string.__name__ |
||
524 | ) |
||
525 | |||
526 | |||
527 | class SortOrder(Enum): |
||
528 | """Enum for sort order""" |
||
529 | |||
530 | ASCENDING = "ascending" |
||
531 | DESCENDING = "descending" |
||
532 | |||
533 | |||
534 | View Code Duplication | def get_sort_order_from_string( |
|
535 | sort_order: Optional[str], |
||
536 | ) -> Optional[SortOrder]: |
||
537 | """ |
||
538 | Convert a sort order string to an actual SortOrder instance. |
||
539 | |||
540 | Arguments: |
||
541 | sort_order: Sort order string to convert to a SortOrder |
||
542 | """ |
||
543 | if not sort_order: |
||
544 | return None |
||
545 | |||
546 | try: |
||
547 | return SortOrder[sort_order.upper()] |
||
548 | except KeyError: |
||
549 | raise InvalidArgument( |
||
550 | argument='sort_order', function=get_sort_order_from_string.__name__ |
||
551 | ) from None |
||
552 | |||
553 | |||
554 | class CredentialType(Enum): |
||
555 | """Enum for credential types""" |
||
556 | |||
557 | CLIENT_CERTIFICATE = 'cc' |
||
558 | SNMP = 'snmp' |
||
559 | USERNAME_PASSWORD = 'up' |
||
560 | USERNAME_SSH_KEY = 'usk' |
||
561 | SMIME_CERTIFICATE = 'smime' |
||
562 | PGP_ENCRYPTION_KEY = 'pgp' |
||
563 | PASSWORD_ONLY = 'pw' |
||
564 | |||
565 | |||
566 | View Code Duplication | def get_credential_type_from_string( |
|
567 | credential_type: Optional[str], |
||
568 | ) -> Optional[CredentialType]: |
||
569 | """Convert a credential type string into a CredentialType instance""" |
||
570 | if not credential_type: |
||
571 | return None |
||
572 | |||
573 | try: |
||
574 | return CredentialType[credential_type.upper()] |
||
575 | except KeyError: |
||
576 | raise InvalidArgument( |
||
577 | argument='credential_type', |
||
578 | function=get_credential_type_from_string.__name__, |
||
579 | ) from None |
||
580 | |||
581 | |||
582 | class TicketStatus(Enum): |
||
583 | """Enum for ticket status""" |
||
584 | |||
585 | OPEN = 'Open' |
||
586 | FIXED = 'Fixed' |
||
587 | CLOSED = 'Closed' |
||
588 | |||
589 | |||
590 | View Code Duplication | def get_ticket_status_from_string( |
|
591 | ticket_status: Optional[str], |
||
592 | ) -> Optional[TicketStatus]: |
||
593 | """Convert a ticket status string into a TicketStatus instance""" |
||
594 | if not ticket_status: |
||
595 | return None |
||
596 | |||
597 | try: |
||
598 | return TicketStatus[ticket_status.upper()] |
||
599 | except KeyError: |
||
600 | raise InvalidArgument( |
||
601 | argument='ticket_status', |
||
602 | function=get_ticket_status_from_string.__name__, |
||
603 | ) from None |
||
604 | |||
605 | |||
606 | class AliveTest(Enum): |
||
607 | """Enum for choosing an alive test""" |
||
608 | |||
609 | SCAN_CONFIG_DEFAULT = 'Scan Config Default' |
||
610 | ICMP_PING = 'ICMP Ping' |
||
611 | TCP_ACK_SERVICE_PING = 'TCP-ACK Service Ping' |
||
612 | TCP_SYN_SERVICE_PING = 'TCP-SYN Service Ping' |
||
613 | ARP_PING = 'ARP Ping' |
||
614 | APR_PING = 'ARP Ping' # Alias for ARP_PING |
||
615 | ICMP_AND_TCP_ACK_SERVICE_PING = 'ICMP & TCP-ACK Service Ping' |
||
616 | ICMP_AND_ARP_PING = 'ICMP & ARP Ping' |
||
617 | TCP_ACK_SERVICE_AND_ARP_PING = 'TCP-ACK Service & ARP Ping' |
||
618 | ICMP_TCP_ACK_SERVICE_AND_ARP_PING = ( # pylint: disable=invalid-name |
||
619 | 'ICMP, TCP-ACK Service & ARP Ping' |
||
620 | ) |
||
621 | CONSIDER_ALIVE = 'Consider Alive' |
||
622 | |||
623 | |||
624 | View Code Duplication | def get_alive_test_from_string( |
|
625 | alive_test: Optional[str], |
||
626 | ) -> Optional[AliveTest]: |
||
627 | """Convert an alive test string into a AliveTest instance""" |
||
628 | if not alive_test: |
||
629 | return None |
||
630 | |||
631 | alive_test = alive_test.lower() |
||
632 | |||
633 | if alive_test == 'scan config default': |
||
634 | return AliveTest.SCAN_CONFIG_DEFAULT |
||
635 | |||
636 | if alive_test == 'icmp ping': |
||
637 | return AliveTest.ICMP_PING |
||
638 | |||
639 | if alive_test == 'tcp-ack service ping': |
||
640 | return AliveTest.TCP_ACK_SERVICE_PING |
||
641 | |||
642 | if alive_test == 'tcp-syn service ping': |
||
643 | return AliveTest.TCP_SYN_SERVICE_PING |
||
644 | |||
645 | if alive_test == 'arp ping': |
||
646 | return AliveTest.ARP_PING |
||
647 | |||
648 | if alive_test == 'icmp & tcp-ack service ping': |
||
649 | return AliveTest.ICMP_AND_TCP_ACK_SERVICE_PING |
||
650 | |||
651 | if alive_test == 'icmp & arp ping': |
||
652 | return AliveTest.ICMP_AND_ARP_PING |
||
653 | |||
654 | if alive_test == 'tcp-ack service & arp ping': |
||
655 | return AliveTest.TCP_ACK_SERVICE_AND_ARP_PING |
||
656 | |||
657 | if alive_test == 'icmp, tcp-ack service & arp ping': |
||
658 | return AliveTest.ICMP_TCP_ACK_SERVICE_AND_ARP_PING |
||
659 | |||
660 | if alive_test == 'consider alive': |
||
661 | return AliveTest.CONSIDER_ALIVE |
||
662 | |||
663 | raise InvalidArgument( |
||
664 | argument='alive_test', function=get_alive_test_from_string.__name__ |
||
665 | ) |
||
666 | |||
667 | |||
668 | class AssetType(Enum): |
||
669 | """ "Enum for asset types""" |
||
670 | |||
671 | OPERATING_SYSTEM = 'os' |
||
672 | HOST = 'host' |
||
673 | |||
674 | |||
675 | View Code Duplication | def get_asset_type_from_string( |
|
676 | asset_type: Optional[str], |
||
677 | ) -> Optional[AssetType]: |
||
678 | if not asset_type: |
||
679 | return None |
||
680 | |||
681 | if asset_type == 'os': |
||
682 | return AssetType.OPERATING_SYSTEM |
||
683 | |||
684 | try: |
||
685 | return AssetType[asset_type.upper()] |
||
686 | except KeyError: |
||
687 | raise InvalidArgument( |
||
688 | argument='asset_type', function=get_asset_type_from_string.__name__ |
||
689 | ) from None |
||
690 | |||
691 | |||
692 | class CredentialFormat(Enum): |
||
693 | """Enum for credential format""" |
||
694 | |||
695 | KEY = 'key' |
||
696 | RPM = 'rpm' |
||
697 | DEB = 'deb' |
||
698 | EXE = 'exe' |
||
699 | PEM = 'pem' |
||
700 | |||
701 | |||
702 | View Code Duplication | def get_credential_format_from_string( |
|
703 | credential_format: Optional[str], |
||
704 | ) -> Optional[CredentialFormat]: |
||
705 | if not credential_format: |
||
706 | return None |
||
707 | |||
708 | try: |
||
709 | return CredentialFormat[credential_format.upper()] |
||
710 | except KeyError: |
||
711 | raise InvalidArgument( |
||
712 | argument='credential_format', |
||
713 | function=get_credential_format_from_string.__name__, |
||
714 | ) from None |
||
715 | |||
716 | |||
717 | class HostsOrdering(Enum): |
||
718 | """Enum for host ordering during scans""" |
||
719 | |||
720 | SEQUENTIAL = "sequential" |
||
721 | RANDOM = "random" |
||
722 | REVERSE = "reverse" |
||
723 | |||
724 | |||
725 | View Code Duplication | def get_hosts_ordering_from_string( |
|
726 | hosts_ordering: Optional[str], |
||
727 | ) -> Optional[HostsOrdering]: |
||
728 | """Convert a hosts ordering string to an actual HostsOrdering instance |
||
729 | |||
730 | Arguments: |
||
731 | hosts_ordering: Host ordering string to convert to a HostsOrdering |
||
732 | """ |
||
733 | if not hosts_ordering: |
||
734 | return None |
||
735 | try: |
||
736 | return HostsOrdering[hosts_ordering.upper()] |
||
737 | except KeyError: |
||
738 | raise InvalidArgument( |
||
739 | argument='hosts_ordering', |
||
740 | function=get_hosts_ordering_from_string.__name__, |
||
741 | ) from None |
||
742 | |||
743 | |||
744 | class PermissionSubjectType(Enum): |
||
745 | """Enum for permission subject type""" |
||
746 | |||
747 | USER = 'user' |
||
748 | GROUP = 'group' |
||
749 | ROLE = 'role' |
||
750 | |||
751 | |||
752 | View Code Duplication | def get_permission_subject_type_from_string( |
|
753 | subject_type: Optional[str], |
||
754 | ) -> Optional[PermissionSubjectType]: |
||
755 | """Convert a permission subject type string to an actual |
||
756 | PermissionSubjectType instance |
||
757 | |||
758 | Arguments: |
||
759 | subject_type: Permission subject type string to convert to a |
||
760 | PermissionSubjectType |
||
761 | """ |
||
762 | if not subject_type: |
||
763 | return None |
||
764 | |||
765 | try: |
||
766 | return PermissionSubjectType[subject_type.upper()] |
||
767 | except KeyError: |
||
768 | raise InvalidArgument( |
||
769 | argument='subject_type', |
||
770 | function=get_permission_subject_type_from_string.__name__, |
||
771 | ) from None |
||
772 | |||
773 | |||
774 | class PortRangeType(Enum): |
||
775 | """Enum for port range type""" |
||
776 | |||
777 | TCP = 'TCP' |
||
778 | UDP = 'UDP' |
||
779 | |||
780 | |||
781 | View Code Duplication | def get_port_range_type_from_string( |
|
782 | port_range_type: Optional[str], |
||
783 | ) -> Optional[PortRangeType]: |
||
784 | """Convert a port range type string to an actual PortRangeType instance |
||
785 | |||
786 | Arguments: |
||
787 | port_range_type: Port range type string to convert to a PortRangeType |
||
788 | """ |
||
789 | if not port_range_type: |
||
790 | return None |
||
791 | |||
792 | try: |
||
793 | return PortRangeType[port_range_type.upper()] |
||
794 | except KeyError: |
||
795 | raise InvalidArgument( |
||
796 | argument='port_range_type', |
||
797 | function=get_port_range_type_from_string.__name__, |
||
798 | ) from None |
||
799 | |||
800 | |||
801 | View Code Duplication | class ReportFormatType(Enum): |
|
802 | """Enum for builtin report formats""" |
||
803 | |||
804 | ANONYMOUS_XML = '5057e5cc-b825-11e4-9d0e-28d24461215b' |
||
805 | ARF = '910200ca-dc05-11e1-954f-406186ea4fc5' |
||
806 | CPE = '5ceff8ba-1f62-11e1-ab9f-406186ea4fc5' |
||
807 | CSV_HOSTS = '9087b18c-626c-11e3-8892-406186ea4fc5"' |
||
808 | CSV_RESULTS = 'c1645568-627a-11e3-a660-406186ea4fc5' |
||
809 | GCR_PDF = 'dc51a40a-c022-11e9-b02d-3f7ca5bdcb11' |
||
810 | GSR_HTML = 'ffa123c9-a2d2-409e-bbbb-a6c1385dbeaa' |
||
811 | GSR_PDF = '35ba7077-dc85-42ef-87c9-b0eda7e903b6' |
||
812 | GXCR_PDF = 'f0d348de-c022-11e9-bc4c-4bf1d5e1a8ca' |
||
813 | GXR_PDF = 'ebbc7f34-8ae5-11e1-b07b-001f29eadec8' |
||
814 | ITG = '77bd6c4a-1f62-11e1-abf0-406186ea4fc5' |
||
815 | LATEX = 'a684c02c-b531-11e1-bdc2-406186ea4fc5' |
||
816 | NBE = '9ca6fe72-1f62-11e1-9e7c-406186ea4fc5' |
||
817 | PDF = 'c402cc3e-b531-11e1-9163-406186ea4fc5' |
||
818 | SVG = '9e5e5deb-879e-4ecc-8be6-a71cd0875cdd' |
||
819 | TXT = 'a3810a62-1f62-11e1-9219-406186ea4fc5' |
||
820 | VERINICE_ISM = 'c15ad349-bd8d-457a-880a-c7056532ee15' |
||
821 | VERINICE_ITG = '50c9950a-f326-11e4-800c-28d24461215b' |
||
822 | XML = 'a994b278-1f62-11e1-96ac-406186ea4fc5' |
||
823 | |||
824 | |||
825 | View Code Duplication | def get_report_format_id_from_string( |
|
826 | report_format: Optional[str], |
||
827 | ) -> Optional[ReportFormatType]: |
||
828 | """Convert an report format name into a ReportFormatType instance""" |
||
829 | if not report_format: |
||
830 | return None |
||
831 | |||
832 | report_format = report_format.lower() |
||
833 | |||
834 | if report_format == 'anonymous xml': |
||
835 | return ReportFormatType.ANONYMOUS_XML |
||
836 | |||
837 | if report_format == 'arf': |
||
838 | return ReportFormatType.ARF |
||
839 | |||
840 | if report_format == 'cpe': |
||
841 | return ReportFormatType.CPE |
||
842 | |||
843 | if report_format == 'csv hosts': |
||
844 | return ReportFormatType.CSV_HOSTS |
||
845 | |||
846 | if report_format == 'csv results': |
||
847 | return ReportFormatType.CSV_RESULTS |
||
848 | |||
849 | if report_format == 'gcr pdf': |
||
850 | return ReportFormatType.GCR_PDF |
||
851 | |||
852 | if report_format == 'gsr html': |
||
853 | return ReportFormatType.GSR_HTML |
||
854 | |||
855 | if report_format == 'gsr pdf': |
||
856 | return ReportFormatType.GSR_PDF |
||
857 | |||
858 | if report_format == 'gxcr pdf': |
||
859 | return ReportFormatType.GXCR_PDF |
||
860 | |||
861 | if report_format == 'gxr pdf': |
||
862 | return ReportFormatType.GXR_PDF |
||
863 | |||
864 | if report_format == 'itg': |
||
865 | return ReportFormatType.ITG |
||
866 | |||
867 | if report_format == 'latex': |
||
868 | return ReportFormatType.LATEX |
||
869 | |||
870 | if report_format == 'nbe': |
||
871 | return ReportFormatType.NBE |
||
872 | |||
873 | if report_format == 'pdf': |
||
874 | return ReportFormatType.PDF |
||
875 | |||
876 | if report_format == 'svg': |
||
877 | return ReportFormatType.SVG |
||
878 | |||
879 | if report_format == 'txt': |
||
880 | return ReportFormatType.TXT |
||
881 | |||
882 | if report_format == 'verinice ism': |
||
883 | return ReportFormatType.VERINICE_ISM |
||
884 | |||
885 | if report_format == 'verinice itg': |
||
886 | return ReportFormatType.VERINICE_ITG |
||
887 | |||
888 | if report_format == 'xml': |
||
889 | return ReportFormatType.XML |
||
890 | |||
891 | raise InvalidArgument( |
||
892 | argument='report_format', |
||
893 | function=get_report_format_id_from_string.__name__, |
||
894 | ) |
||
895 | |||
896 | |||
897 | class SnmpAuthAlgorithm(Enum): |
||
898 | """Enum for SNMP auth algorithm""" |
||
899 | |||
900 | SHA1 = 'sha1' |
||
901 | MD5 = 'md5' |
||
902 | |||
903 | |||
904 | View Code Duplication | def get_snmp_auth_algorithm_from_string( |
|
905 | algorithm: Optional[str], |
||
906 | ) -> Optional[SnmpAuthAlgorithm]: |
||
907 | """Convert a SNMP auth algorithm string into a SnmpAuthAlgorithm instance""" |
||
908 | if not algorithm: |
||
909 | return None |
||
910 | |||
911 | try: |
||
912 | return SnmpAuthAlgorithm[algorithm.upper()] |
||
913 | except KeyError: |
||
914 | raise InvalidArgument( |
||
915 | argument='algorithm', |
||
916 | function=get_snmp_auth_algorithm_from_string.__name__, |
||
917 | ) from None |
||
918 | |||
919 | |||
920 | class SnmpPrivacyAlgorithm(Enum): |
||
921 | """Enum for SNMP privacy algorithm""" |
||
922 | |||
923 | AES = 'aes' |
||
924 | DES = 'des' |
||
925 | |||
926 | |||
927 | View Code Duplication | def get_snmp_privacy_algorithm_from_string( |
|
928 | algorithm: Optional[str], |
||
929 | ) -> Optional[SnmpPrivacyAlgorithm]: |
||
930 | """Convert a SNMP privacy algorithm string into a SnmpPrivacyAlgorithm |
||
931 | instance |
||
932 | """ |
||
933 | if not algorithm: |
||
934 | return None |
||
935 | |||
936 | try: |
||
937 | return SnmpPrivacyAlgorithm[algorithm.upper()] |
||
938 | except KeyError: |
||
939 | raise InvalidArgument( |
||
940 | argument='algorithm', |
||
941 | function=get_snmp_privacy_algorithm_from_string.__name__, |
||
942 | ) from None |
||
943 | |||
944 | |||
945 | class SeverityLevel(Enum): |
||
946 | """Enum for severity levels""" |
||
947 | |||
948 | HIGH = "High" |
||
949 | MEDIUM = "Medium" |
||
950 | LOW = "Low" |
||
951 | LOG = "Log" |
||
952 | ALARM = "Alarm" |
||
953 | DEBUG = "Debug" |
||
954 | |||
955 | |||
956 | View Code Duplication | def get_severity_level_from_string( |
|
957 | severity_level: Optional[str], |
||
958 | ) -> Optional[SeverityLevel]: |
||
959 | """Convert a severity level string into a SeverityLevel instance""" |
||
960 | if not severity_level: |
||
961 | return None |
||
962 | |||
963 | try: |
||
964 | return SeverityLevel[severity_level.upper()] |
||
965 | except KeyError: |
||
966 | raise InvalidArgument( |
||
967 | argument='severity_level', |
||
968 | function=get_severity_level_from_string.__name__, |
||
969 | ) from None |
||
970 | |||
971 | |||
972 | class TimeUnit(Enum): |
||
973 | """Enum for time units""" |
||
974 | |||
975 | SECOND = "second" |
||
976 | MINUTE = "minute" |
||
977 | HOUR = "hour" |
||
978 | DAY = "day" |
||
979 | WEEK = "week" |
||
980 | MONTH = "month" |
||
981 | YEAR = "year" |
||
982 | DECADE = "decade" |
||
983 | |||
984 | |||
985 | View Code Duplication | def get_time_unit_from_string(time_unit: Optional[str]) -> Optional[TimeUnit]: |
|
986 | """Convert a time unit string into a TimeUnit instance""" |
||
987 | if not time_unit: |
||
988 | return None |
||
989 | |||
990 | try: |
||
991 | return TimeUnit[time_unit.upper()] |
||
992 | except KeyError: |
||
993 | raise InvalidArgument( |
||
994 | argument='severity_level', |
||
995 | function=get_severity_level_from_string.__name__, |
||
996 | ) from None |
||
997 | |||
998 | |||
999 | class UserAuthType(Enum): |
||
1000 | """Enum for Sources allowed for authentication for the user""" |
||
1001 | |||
1002 | FILE = 'file' |
||
1003 | LDAP_CONNECT = 'ldap_connect' |
||
1004 | RADIUS_CONNECT = 'radius_connect' |
||
1005 | |||
1006 | |||
1007 | View Code Duplication | def get_user_auth_type_from_string( |
|
1008 | user_auth_type: Optional[str], |
||
1009 | ) -> Optional[UserAuthType]: |
||
1010 | """Convert a user auth type string into a UserAuthType instance""" |
||
1011 | if not user_auth_type: |
||
1012 | return None |
||
1013 | |||
1014 | try: |
||
1015 | return UserAuthType[user_auth_type.upper()] |
||
1016 | except KeyError: |
||
1017 | raise InvalidArgument( |
||
1018 | argument='user_auth_type', |
||
1019 | function=get_user_auth_type_from_string.__name__, |
||
1020 | ) from None |
||
1021 |