From 99b6a6605dcbf575b80739435b8c97226ec2ca94 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 12 Jan 2018 10:59:19 +0100 Subject: [PATCH] Splitted method perform_config() in pp_lib/ldap_app.py --- pp_lib/ldap_app.py | 124 +++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/pp_lib/ldap_app.py b/pp_lib/ldap_app.py index 8048fdd..24defa3 100644 --- a/pp_lib/ldap_app.py +++ b/pp_lib/ldap_app.py @@ -31,7 +31,7 @@ from .common import pp, to_bool from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.4.7' +__version__ = '0.4.8' LOG = logging.getLogger(__name__) @@ -133,64 +133,76 @@ class PpLdapApplication(PpConfigApplication): methods in descendant classes. """ - got_host = False for section_name in self.cfg.keys(): - if not section_name.lower() == 'ldap': - continue - ldap_section = self.cfg[section_name] if self.verbose > 2: - LOG.debug("Evaluating config section {n!r}:\n{s}".format( - n=section_name, s=pp(ldap_section))) - - if 'host' in ldap_section: - hosts = self.fs_re.split(ldap_section['host']) - for host in hosts: - if not host: - continue - if not got_host: - self.ldap_hosts = [] - got_host = True - host = host.lower() - if host in self.ldap_hosts: - continue - self.ldap_hosts.append(host) - - if 'port' in ldap_section: - try: - port = int(ldap_section['port']) - except (ValueError, TypeError): - msg = "Invalid LDAP port ({s}/port => {v!r}) found in configuration.".format( - s=section_name, v=ldap_section['port']) - raise PpLdapAppError(msg) - if port <= 0 or port >= 2 ** 16: - msg = "Invalid LDAP port ({s}/port => {v!r}) found in configuration.".format( - s=section_name, v=port) - raise PpLdapAppError(msg) - self.ldap_port = port - - if 'ssl' in ldap_section: - self.ldap_use_ssl = to_bool(ldap_section['ssl']) - - if 'tls' in ldap_section: - self.ldap_use_ssl = to_bool(ldap_section['tls']) - - if 'base_dn' in ldap_section: - self.ldap_base_dn = ldap_section['base_dn'].strip() - if 'bind_dn' in ldap_section: - self.ldap_bind_dn = ldap_section['bind_dn'].strip() - if 'bind_pw' in ldap_section: - self.ldap_bind_pw = ldap_section['bind_pw'] - if 'timeout' in ldap_section: - try: - timeout = int(ldap_section['timeout']) - except (ValueError, TypeError): - msg = ( - "Invalid LDAP timeout ({s}/port => {v!r}) found in configuration.").format( - s=section_name, v=ldap_section['timeout']) - LOG.error(msg) - if timeout > 0: - self.ldap_timeout = timeout + LOG.debug("Checking config section {!r} ...".format(section_name)) + section = self.cfg[section_name] + + if section_name.lower() == 'ldap': + self.do_ldap_cfg(section_name, section) + + # ------------------------------------------------------------------------- + def do_ldap_cfg(self, section_name, section): + + if self.verbose > 2: + LOG.debug("Evaluating config section {n!r}:\n{s}".format( + n=section_name, s=pp(section))) + + if self.verbose > 2: + LOG.debug("Evaluating config section {n!r}:\n{s}".format( + n=section_name, s=pp(section))) + + got_host = False + + if 'host' in section: + hosts = self.fs_re.split(section['host']) + for host in hosts: + if not host: + continue + if not got_host: + self.ldap_hosts = [] + got_host = True + host = host.lower() + if host in self.ldap_hosts: + continue + self.ldap_hosts.append(host) + + if 'port' in section: + try: + port = int(section['port']) + except (ValueError, TypeError): + msg = "Invalid LDAP port ({s}/port => {v!r}) found in configuration.".format( + s=section_name, v=section['port']) + raise PpLdapAppError(msg) + if port <= 0 or port >= 2 ** 16: + msg = "Invalid LDAP port ({s}/port => {v!r}) found in configuration.".format( + s=section_name, v=port) + raise PpLdapAppError(msg) + self.ldap_port = port + + if 'ssl' in section: + self.ldap_use_ssl = to_bool(section['ssl']) + + if 'tls' in section: + self.ldap_use_ssl = to_bool(section['tls']) + + if 'base_dn' in section: + self.ldap_base_dn = section['base_dn'].strip() + if 'bind_dn' in section: + self.ldap_bind_dn = section['bind_dn'].strip() + if 'bind_pw' in section: + self.ldap_bind_pw = section['bind_pw'] + if 'timeout' in section: + try: + timeout = int(section['timeout']) + except (ValueError, TypeError): + msg = ( + "Invalid LDAP timeout ({s}/port => {v!r}) found in configuration.").format( + s=section_name, v=section['timeout']) + LOG.error(msg) + if timeout > 0: + self.ldap_timeout = timeout # ---------------------- def _get_ldap_server(host): -- 2.39.5