iwla

iwla Git Source Tree

Root/iplugin.py

1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé 2015
4
5# This file is part of iwla
6
7# iwla is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# iwla is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with iwla. If not, see <http://www.gnu.org/licenses/>.
19#
20
21import importlib
22import inspect
23import logging
24
25#
26# IWLA Plugin interface
27#
28
29class IPlugin(object):
30
31 def __init__(self, iwla):
32 self.iwla = iwla
33 self.requires = []
34 self.conf_requires = []
35 self.API_VERSION = 1
36 self.ANALYSIS_CLASS = 'HTTP'
37
38 def isValid(self, analysis_class, api_version):
39 if analysis_class != self.ANALYSIS_CLASS: return False
40
41 # For now there is only version 1
42 if self.API_VERSION != api_version:
43 return False
44
45 return True
46
47 def getRequirements(self):
48 return self.requires
49
50 def getConfRequirements(self):
51 return self.conf_requires
52
53 def load(self):
54 return True
55
56 def hook(self):
57 pass
58
59def validConfRequirements(conf_requirements, iwla, plugin_path):
60 for r in conf_requirements:
61 if iwla.getConfValue(r, None) is None:
62 print('\'%s\' conf value required for %s' % (r, plugin_path))
63 return False
64
65 return True
66
67def preloadPlugins(plugins, iwla):
68 cache_plugins = {}
69
70 logger = logging.getLogger(__name__)
71
72 logger.info("==> Preload plugins")
73
74 for (root, plugins_filenames) in plugins:
75 for plugin_filename in plugins_filenames:
76 plugin_path = root + '.' + plugin_filename
77 try:
78 mod = importlib.import_module(plugin_path)
79 classes = [c for _,c in inspect.getmembers(mod)\
80 if inspect.isclass(c) and \
81 issubclass(c, IPlugin) and \
82 c.__name__ != 'IPlugin' and \
83 not c.__subclasses__()
84 ]
85
86 if not classes:
87 logger.warning('No plugin defined in %s' % (plugin_path))
88 continue
89
90 if len(classes) > 1:
91 logger.warning('More than one class found in %s, loading may fail. Selecting %s' % (plugin_path, classes[0]))
92 print(classes)
93 continue
94
95 plugin = classes[0](iwla)
96 plugin_name = plugin.__class__.__name__
97
98 if not plugin.isValid(iwla.ANALYSIS_CLASS, iwla.API_VERSION):
99 #print 'Plugin not valid %s' % (plugin_filename)
100 continue
101
102 #print 'Load plugin %s' % (plugin_name)
103
104 conf_requirements = plugin.getConfRequirements()
105 if not validConfRequirements(conf_requirements, iwla, plugin_path):
106 continue
107
108 requirements = plugin.getRequirements()
109
110 requirement_validated = False
111 for r in requirements:
112 for p in cache_plugins.values():
113 if p.__class__.__name__ == r:
114 requirement_validated = True
115 break
116 if not requirement_validated:
117 logger.error('Missing requirements \'%s\' for plugin %s' % (r, plugin_path))
118 break
119 if requirements and not requirement_validated: continue
120
121 if not plugin.load():
122 logger.error('Plugin %s load failed' % (plugin_path))
123 continue
124
125 logger.info('\tRegister %s' % (plugin_path))
126 cache_plugins[plugin_path] = plugin
127 except Exception as e:
128 logger.exception('Error loading %s => %s' % (plugin_path, e))
129
130 return cache_plugins

Archive Download this file

Branches

Tags