-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathinitialize.py
97 lines (79 loc) · 2.71 KB
/
initialize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Intelligent initialization of plugins."""
try:
from functools import lru_cache
except ImportError:
from functools32 import lru_cache
from ..base import BasePlugin
from ..basic_auth import BasicAuthDetector # noqa: F401
from ..high_entropy_strings import Base64HighEntropyString # noqa: F401
from ..high_entropy_strings import HexHighEntropyString # noqa: F401
from ..keyword import KeywordDetector # noqa: F401
from ..private_key import PrivateKeyDetector # noqa: F401
from detect_secrets.core.log import log
def from_parser_builder(plugins_dict):
"""
:param plugins_dict: plugins dictionary received from ParserBuilder.
See example in tests.core.usage_test.
:returns: tuple of initialized plugins
"""
output = []
for plugin_name in plugins_dict:
output.append(from_plugin_classname(
plugin_name,
**plugins_dict[plugin_name]
))
return tuple(output)
def from_plugin_classname(plugin_classname, **kwargs):
"""Initializes a plugin class, given a classname and kwargs.
:type plugin_classname: str
:param plugin_classname: subclass of BasePlugin
"""
klass = globals()[plugin_classname]
# Make sure the instance is a BasePlugin type, before creating it.
if not issubclass(klass, BasePlugin):
raise TypeError
try:
instance = klass(**kwargs)
except TypeError:
log.warning(
'Unable to initialize plugin!',
)
raise
return instance
def from_secret_type(secret_type, settings):
"""
:type secret_type: str
:param secret_type: unique identifier for plugin type
:type settings: list
:param settings: output of "plugins_used" in baseline. e.g.
>>> [
... {
... 'name': 'Base64HighEntropyString',
... 'base64_limit': 4.5,
... },
... ]
"""
mapping = _get_mapping_from_secret_type_to_class_name()
try:
classname = mapping[secret_type]
except KeyError:
return None
for plugin in settings:
if plugin['name'] == classname:
plugin_init_vars = plugin.copy()
plugin_init_vars.pop('name')
return from_plugin_classname(
classname,
**plugin_init_vars
)
@lru_cache(maxsize=1)
def _get_mapping_from_secret_type_to_class_name():
"""Returns secret_type => plugin classname"""
mapping = {}
for key, value in globals().items():
try:
if issubclass(value, BasePlugin) and value != BasePlugin:
mapping[value.secret_type] = key
except TypeError:
pass
return mapping