fixing config management for plugins.

This commit is contained in:
Stefan Midjich 2016-04-17 11:21:37 +02:00
parent f7d5aae9b3
commit d3bf991048
3 changed files with 41 additions and 7 deletions

View file

@ -1,8 +1,15 @@
# Add an iptables rule # Add an iptables rule
import re import re
import socket
from io import BytesIO from io import BytesIO
from logging import getLogger, DEBUG, WARN, INFO from logging import getLogger, DEBUG, WARN, INFO
try:
from congiparser import RawConfigParser
except ImportError:
from ConfigParser import RawConfigParser
from portal import logHandler, logFormatter from portal import logHandler, logFormatter
# Try to import arping for mac_from_ip() # Try to import arping for mac_from_ip()
@ -18,12 +25,15 @@ from sh import sudo, ErrorReturnCode
def run(arg): def run(arg):
# Some info from the plugin dispatcher. # Some info from the plugin dispatcher.
environ = arg['environ'] environ = arg['environ']
config = arg['config'] plugin_config = arg['config']
config = RawConfigParser(defaults=plugin_config)
config.add_section(plugin_config.get('__name__', 'iptables'))
# Setup plugin logging # Setup plugin logging
l = getLogger('plugin_iptables') l = getLogger('plugin_iptables')
l.addHandler(logHandler) l.addHandler(logHandler)
if config.get('debug', False): if config.getboolean('iptables', 'debug', False):
l.setLevel(DEBUG) l.setLevel(DEBUG)
client_ip = environ.get( client_ip = environ.get(
@ -34,11 +44,21 @@ def run(arg):
error_msg = None error_msg = None
iptables_failed = False iptables_failed = False
# Verify IP
try:
socket.inet_aton(client_ip)
except socket.error:
l.error('Client IP-address is invalid')
return {
'error': str(e),
'failed': True
}
# Attempt to get client HW address first. # Attempt to get client HW address first.
try: try:
client_mac = mac_from_ip( client_mac = mac_from_ip(
l, l,
config.get('arping'), config.get('iptables', 'arping'),
client_ip client_ip
) )
except Exception as e: except Exception as e:
@ -55,7 +75,7 @@ def run(arg):
)) ))
# Create tuple out of iptables command # Create tuple out of iptables command
iptables_mac = config.get('iptables_mac').format( iptables_mac = config.get('iptables', 'iptables_mac').format(
mac_address=client_mac mac_address=client_mac
) )
iptables_mac = tuple(iptables_mac.split(' ')) iptables_mac = tuple(iptables_mac.split(' '))
@ -95,7 +115,7 @@ def run(arg):
ip=client_ip ip=client_ip
)) ))
iptables_ip = config.get('iptables_ip').format( iptables_ip = config.get('iptables', 'iptables_ip').format(
ip_address=client_ip ip_address=client_ip
) )
iptables_ip = tuple(iptables_ip.split(' ')) iptables_ip = tuple(iptables_ip.split(' '))

View file

@ -2,16 +2,26 @@
# Sets up logging by importing from the bottle app in the parent dir. # Sets up logging by importing from the bottle app in the parent dir.
from logging import getLogger, DEBUG, WARN, INFO from logging import getLogger, DEBUG, WARN, INFO
try:
from congiparser import RawConfigParser
except ImportError:
from ConfigParser import RawConfigParser
from portal import logHandler, logFormatter from portal import logHandler, logFormatter
def run(arg): def run(arg):
# The WSGI environ dict should always be there, sans any special objects # The WSGI environ dict should always be there, sans any special objects
# like io streams. # like io streams.
environ = arg['environ'] environ = arg['environ']
plugin_config = arg['config']
config = RawConfigParser(defaults=plugin_config)
config.add_section(plugin_config.get('__name__', 'sample_log'))
l = getLogger('plugin_log') l = getLogger('plugin_log')
l.addHandler(logHandler) l.addHandler(logHandler)
if config.get('debug', False): if config.getboolean('sample_log', 'debug', False):
l.setLevel(DEBUG) l.setLevel(DEBUG)
log_url = '{proto}://{server}:{port}{request}'.format( log_url = '{proto}://{server}:{port}{request}'.format(

View file

@ -104,7 +104,11 @@ def dispatch_plugins():
# Import all the plugin configuration values as OrderedDict # Import all the plugin configuration values as OrderedDict
config_sections = plugin_config._sections config_sections = plugin_config._sections
arg['config'] = config_sections[plugin] arg['config'] = dict(config_sections[plugin])
# Is plugin enabled?
if not plugin_config.getboolean(plugin, 'enabled'):
continue
# Import the plugin # Import the plugin
try: try: