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'
83 ]
84
85 if not classes:
86 logger.warning('No plugin defined in %s' % (plugin_path))
87 continue
88
89 plugin = classes[0](iwla)
90 plugin_name = plugin.__class__.__name__
91
92 if not plugin.isValid(iwla.ANALYSIS_CLASS, iwla.API_VERSION):
93 #print 'Plugin not valid %s' % (plugin_filename)
94 continue
95
96 #print 'Load plugin %s' % (plugin_name)
97
98 conf_requirements = plugin.getConfRequirements()
99 if not validConfRequirements(conf_requirements, iwla, plugin_path):
100 continue
101
102 requirements = plugin.getRequirements()
103
104 requirement_validated = False
105 for r in requirements:
106 for (_,p) in cache_plugins.items():
107 if p.__class__.__name__ == r:
108 requirement_validated = True
109 break
110 if not requirement_validated:
111 logger.error('Missing requirements \'%s\' for plugin %s' % (r, plugin_path))
112 break
113 if requirements and not requirement_validated: continue
114
115 if not plugin.load():
116 logger.error('Plugin %s load failed' % (plugin_path))
117 continue
118
119 logger.info('\tRegister %s' % (plugin_path))
120 cache_plugins[plugin_path] = plugin
121 except Exception as e:
122 logger.exception('Error loading %s => %s' % (plugin_path, e))
123
124 return cache_plugins

Archive Download this file

Branches

Tags