|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Package containing the various security checker modules. |
|
8 """ |
|
9 |
|
10 import collections |
|
11 import os |
|
12 |
|
13 |
|
14 def generateCheckersDict(): |
|
15 """ |
|
16 Function to generate the dictionary with checkers. |
|
17 |
|
18 Checker modules are searched for inside this package. Each module |
|
19 defining some checks must contain a function 'getChecks()' returning |
|
20 a dictionary containing the check type as key and a list of tuples |
|
21 with the check function and associated message codes. |
|
22 |
|
23 @return dictionary containing list of tuples with checker data |
|
24 @rtype dict |
|
25 """ |
|
26 checkersDict = collections.defaultdict(list) |
|
27 |
|
28 checkersDirectory = os.path.dirname(__file__) |
|
29 checkerModules = [os.path.splitext(m)[0] |
|
30 for m in os.listdir(checkersDirectory) |
|
31 if m != "__init__.py" and m.endswith(".py")] |
|
32 |
|
33 for checkerModule in checkerModules: |
|
34 modName = "Security.Checks.{0}".format(checkerModule) |
|
35 try: |
|
36 mod = __import__(modName) |
|
37 components = modName.split('.') |
|
38 for comp in components[1:]: |
|
39 mod = getattr(mod, comp) |
|
40 except ImportError: |
|
41 continue |
|
42 |
|
43 if not hasattr(mod, "getChecks"): |
|
44 continue |
|
45 |
|
46 modCheckersDict = mod.getChecks() |
|
47 for checktype, checks in modCheckersDict.items(): |
|
48 checkersDict[checktype].extend(checks) |
|
49 |
|
50 return checkersDict |