iwla/iplugin.py

125 lines
3.9 KiB
Python
Raw Normal View History

2014-12-18 19:54:31 +01:00
# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
2014-11-24 17:13:59 +01:00
import importlib
import inspect
2014-12-16 20:23:33 +01:00
import logging
2014-11-24 17:13:59 +01:00
#
# IWLA Plugin interface
#
2014-11-24 17:13:59 +01:00
class IPlugin(object):
2014-11-24 21:42:57 +01:00
def __init__(self, iwla):
2014-11-24 17:13:59 +01:00
self.iwla = iwla
self.requires = []
self.conf_requires = []
2014-11-24 17:13:59 +01:00
self.API_VERSION = 1
self.ANALYSIS_CLASS = 'HTTP'
def isValid(self, analysis_class, api_version):
if analysis_class != self.ANALYSIS_CLASS: return False
# For now there is only version 1
if self.API_VERSION != api_version:
return False
return True
2014-11-24 21:37:37 +01:00
2014-11-24 17:13:59 +01:00
def getRequirements(self):
return self.requires
def getConfRequirements(self):
return self.conf_requires
2014-11-24 17:13:59 +01:00
def load(self):
return True
def hook(self):
2014-11-24 17:13:59 +01:00
pass
def validConfRequirements(conf_requirements, iwla, plugin_path):
for r in conf_requirements:
if iwla.getConfValue(r, None) is None:
print '\'%s\' conf value required for %s' % (r, plugin_path)
return False
return True
2014-11-24 17:13:59 +01:00
def preloadPlugins(plugins, iwla):
cache_plugins = {}
2014-12-16 20:23:33 +01:00
logger = logging.getLogger(__name__)
logger.info("==> Preload plugins")
2014-11-25 16:22:07 +01:00
for (root, plugins_filenames) in plugins:
for plugin_filename in plugins_filenames:
2014-11-24 17:13:59 +01:00
plugin_path = root + '.' + plugin_filename
try:
mod = importlib.import_module(plugin_path)
classes = [c for _,c in inspect.getmembers(mod)\
if inspect.isclass(c) and \
issubclass(c, IPlugin) and \
c.__name__ != 'IPlugin'
]
if not classes:
2014-12-16 20:23:33 +01:00
logger.warning('No plugin defined in %s' % (plugin_path))
2014-11-24 17:13:59 +01:00
continue
2014-11-24 21:42:57 +01:00
plugin = classes[0](iwla)
2014-11-24 17:13:59 +01:00
plugin_name = plugin.__class__.__name__
if not plugin.isValid(iwla.ANALYSIS_CLASS, iwla.API_VERSION):
#print 'Plugin not valid %s' % (plugin_filename)
continue
#print 'Load plugin %s' % (plugin_name)
conf_requirements = plugin.getConfRequirements()
if not validConfRequirements(conf_requirements, iwla, plugin_path):
continue
2014-11-24 17:13:59 +01:00
requirements = plugin.getRequirements()
requirement_validated = False
for r in requirements:
for (_,p) in cache_plugins.items():
if p.__class__.__name__ == r:
requirement_validated = True
2014-11-24 17:13:59 +01:00
break
if not requirement_validated:
2014-12-16 20:23:33 +01:00
logger.error('Missing requirements \'%s\' for plugin %s' % (r, plugin_path))
break
if requirements and not requirement_validated: continue
2014-11-24 17:13:59 +01:00
if not plugin.load():
2014-12-16 20:23:33 +01:00
logger.error('Plugin %s load failed' % (plugin_path))
2014-11-24 17:13:59 +01:00
continue
2014-12-16 20:23:33 +01:00
logger.info('\tRegister %s' % (plugin_path))
2014-11-24 17:13:59 +01:00
cache_plugins[plugin_path] = plugin
except Exception as e:
2014-12-16 20:23:33 +01:00
logger.exception('Error loading %s => %s' % (plugin_path, e))
2014-11-24 17:13:59 +01:00
return cache_plugins