From 1e5f057e36d7b92e4d763f313f3955cff68783d5 Mon Sep 17 00:00:00 2001 From: kakwa Date: Wed, 20 May 2015 14:21:43 +0200 Subject: [PATCH] implementing loading backends * fix conf file * add exceptions * fix modules skeletons --- conf/ldapcherry.ini | 4 ++-- ldapcherry/__init__.py | 21 ++++++++++++++++----- ldapcherry/backend/ldap.py | 2 ++ ldapcherry/backend/samba4.py | 6 ++++++ ldapcherry/exceptions.py | 10 ++++++++++ tests/cfg/ldapcherry.ini | 4 ++-- 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/conf/ldapcherry.ini b/conf/ldapcherry.ini index f70081c..7deacf9 100644 --- a/conf/ldapcherry.ini +++ b/conf/ldapcherry.ini @@ -66,7 +66,7 @@ roles.file = '/etc/ldapcherry/roles.yml' [backends] -ldap.module = 'ldapcherry.backends.ldap' +ldap.module = 'ldapcherry.backend.ldap' ldap.groupdn = 'ou=group,dc=example,dc=com' ldap.people = 'ou=group,dc=example,dc=com' ldap.authdn = 'cn=ldapcherry,dc=example,dc=com' @@ -76,7 +76,7 @@ ldap.ca = '/etc/dnscherry/TEST-cacert.pem' ldap.starttls = 'on' ldap.checkcert = 'off' -ad.module = 'ldapcherry.backends.ad' +ad.module = 'ldapcherry.backend.samba4' ad.auth = 'Administrator' ad.password = 'password' diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index 5e9f320..04d74b3 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -87,6 +87,7 @@ class LdapCherry(object): def _init_backends(self, config): self.backends_params = {} + self.backends = {} for entry in config['backends']: # split at the first dot backend, sep, param = entry.partition('.') @@ -94,6 +95,21 @@ class LdapCherry(object): if not backend in self.backends_params: self.backends_params[backend] = {} self.backends_params[backend][param] = value + for backend in self.backends_params: + params = self.backends_params[backend] + # Loading the backend module + try: + module = params['module'] + except: + raise MissingParameter('backends', backend + '.module') + try: + bc = __import__(module, globals(), locals(), ['Backend'], -1) + except: + raise BackendModuleLoadingFail(module) + try: + self.backends[backend] = bc.Backend(params, cherrypy.log) + except: + raise BackendModuleInitFail(module) def _set_access_log(self, config, level): access_handler = self._get_param('global', 'log.access_handler', config, 'syslog') @@ -209,11 +225,6 @@ class LdapCherry(object): self.temp_error = self.temp_lookup.get_template('error.tmpl') self.temp_login = self.temp_lookup.get_template('login.tmpl') - # loading the authentification module - #auth_module = self._get_param('auth', 'auth.module', config) - #auth = __import__(auth_module, globals(), locals(), ['Auth'], -1) - #self.auth = auth.Auth(config['auth'], cherrypy.log) - self.roles_file = self._get_param('roles', 'roles.file', config) cherrypy.log.error( diff --git a/ldapcherry/backend/ldap.py b/ldapcherry/backend/ldap.py index bc750cd..1ffc00c 100644 --- a/ldapcherry/backend/ldap.py +++ b/ldapcherry/backend/ldap.py @@ -5,6 +5,8 @@ # LdapCherry # Copyright (c) 2014 Carpentier Pierre-Francois +import ldapcherry.backend + class Backend(ldapcherry.backend.Backend): def __init__(self, config, logger): diff --git a/ldapcherry/backend/samba4.py b/ldapcherry/backend/samba4.py index bc807ca..8f490e1 100644 --- a/ldapcherry/backend/samba4.py +++ b/ldapcherry/backend/samba4.py @@ -5,3 +5,9 @@ # LdapCherry # Copyright (c) 2014 Carpentier Pierre-Francois +import ldapcherry.backend + +class Backend(ldapcherry.backend.Backend): + + def __init__(self, config, logger): + pass diff --git a/ldapcherry/exceptions.py b/ldapcherry/exceptions.py index a9e1029..07f9d6d 100644 --- a/ldapcherry/exceptions.py +++ b/ldapcherry/exceptions.py @@ -54,6 +54,16 @@ class MissingAttributesFile(Exception): self.attributesfile = attributesfile self.log = "fail to open attributes file <%(attributesfile)s>" % { 'attributesfile' : attributesfile} +class BackendModuleLoadingFail(Exception): + def __init__(self, module): + self.module = module + self.log = "fail to load module <%(module)s>" % {'module': module} + +class BackendModuleInitFail(Exception): + def __init__(self, module): + self.module = module + self.log = "fail to init module <%(module)s>" % {'module': module} + class WrongAttributeType(Exception): def __init__(self, key, section, ymlfile): self.key = key diff --git a/tests/cfg/ldapcherry.ini b/tests/cfg/ldapcherry.ini index 0d8fc30..1778cdc 100644 --- a/tests/cfg/ldapcherry.ini +++ b/tests/cfg/ldapcherry.ini @@ -66,7 +66,7 @@ roles.file = './tests/cfg/roles.yml' [backends] -ldap.module = 'ldapcherry.backends.ldap' +ldap.module = 'ldapcherry.backend.ldap' ldap.groupdn = 'ou=group,dc=example,dc=com' ldap.people = 'ou=group,dc=example,dc=com' ldap.authdn = 'cn=ldapcherry,dc=example,dc=com' @@ -76,7 +76,7 @@ ldap.ca = '/etc/dnscherry/TEST-cacert.pem' ldap.starttls = 'on' ldap.checkcert = 'off' -ad.module = 'ldapcherry.backends.ad' +ad.module = 'ldapcherry.backend.samba4' ad.auth = 'Administrator' ad.password = 'password'