From 8ef35830fdc828e101850c4d0958697be2799f9a Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 18 May 2022 17:38:05 +0200 Subject: [PATCH] Output of LDAP options with bin/dpx-show-mail-ldap-config --- lib/pp_admintools/show_ldap_mail_config.py | 111 ++++++++++++++++++++- 1 file changed, 107 insertions(+), 4 deletions(-) diff --git a/lib/pp_admintools/show_ldap_mail_config.py b/lib/pp_admintools/show_ldap_mail_config.py index a1587b9..e77c23c 100644 --- a/lib/pp_admintools/show_ldap_mail_config.py +++ b/lib/pp_admintools/show_ldap_mail_config.py @@ -12,6 +12,7 @@ import os import logging import textwrap import datetime +import argparse # Third party modules from pytz import timezone, UnknownTimeZoneError @@ -33,7 +34,7 @@ from .ldap_app import LdapAppError, BaseLdapApplication from .xlate import XLATOR -__version__ = '0.1.0' +__version__ = '0.2.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -65,6 +66,10 @@ class ShowMailLdapConfigApp(BaseMailApplication, BaseLdapApplication): self.local_tz = None self.local_tz_name = self.default_local_tz_name + self.use_default_ldap_connection = False + self.show_cmdline_ldap_timeout = False + self.show_cmdline_mail_options = False + description = _( 'Shows all relevant configured options for Mail and LDAP ' 'supporting applications.') @@ -81,11 +86,22 @@ class ShowMailLdapConfigApp(BaseMailApplication, BaseLdapApplication): return datetime.datetime.now(self.local_tz).strftime('%Y-%m-%d %H:%M:%S %Z') return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + # ------------------------------------------------------------------------- + def init_arg_parser(self): + """ + Public available method to initiate the argument parser. + """ + + super(ShowMailLdapConfigApp, self).init_arg_parser() + + self.arg_parser.add_argument( + '-P', '--show-passwords', action="store_true", dest="show_passwords", + help=argparse.SUPPRESS) + # ------------------------------------------------------------------------- def post_init(self): - if not self.quiet: - print('') + print('') LOG.debug(_("Post init phase.")) @@ -104,7 +120,94 @@ class ShowMailLdapConfigApp(BaseMailApplication, BaseLdapApplication): # ------------------------------------------------------------------------- def _run(self): - LOG.info(_("Starting: {}").format(self.current_timestamp())) + if self.verbose: + LOG.info(_("Starting: {}").format(self.current_timestamp())) + print('') + + self.show_ldap_config() + + # ------------------------------------------------------------------------- + def show_ldap_config(self): + + show_pw = getattr(self.args, 'show_passwords', False) + + title = _("LDAP configuration") + title_length = len(title) + print(title) + print('=' * title_length) + print('') + print(_("Timeout: {} seconds").format(self.cfg.ldap_timeout)) + print('') + + title = _("Configured LDAP connections:") + title_length = len(title) + print(title) + print('-' * title_length) + + header_data = { + 'name': _('Name'), + 'url': 'URL', + 'base_dn': _('Search base DN'), + 'bind_dn': _('Bind DN'), + 'bind_pw': _('Bind password'), + } + length_data = { + 'name': len(header_data['name']), + 'url': len(header_data['url']), + 'base_dn': len(header_data['base_dn']), + 'bind_dn': len(header_data['bind_dn']), + 'bind_pw': len(header_data['bind_pw']), + } + conn_list = [] + + for name in sorted(self.cfg.ldap_connection.keys(), key=str.lower): + conn_data = self.cfg.ldap_connection[name] + + bind_dn = conn_data.bind_dn + if bind_dn is None: + bind_dn = '' + + bind_pw = conn_data.bind_pw + if bind_pw is None: + bind_pw = '' + else: + if not show_pw: + bind_pw = '******' + + connection = { + 'name': name, + 'url': conn_data.url, + 'base_dn': conn_data.base_dn, + 'bind_dn': bind_dn, + 'bind_pw': bind_pw, + } + + conn_list.append(connection) + + if len(connection['name']) > length_data['name']: + length_data['name'] = len(connection['name']) + if len(connection['url']) > length_data['url']: + length_data['url'] = len(connection['url']) + if len(connection['base_dn']) > length_data['base_dn']: + length_data['base_dn'] = len(connection['base_dn']) + if len(connection['bind_dn']) > length_data['bind_dn']: + length_data['bind_dn'] = len(connection['bind_dn']) + if len(connection['bind_pw']) > length_data['bind_pw']: + length_data['bind_pw'] = len(connection['bind_pw']) + + length_total = 8 + for key in length_data.keys(): + length_total += length_data[key] + + template = ( + '{{name:<{name}}} {{url:<{url}}} {{base_dn:<{base_dn}}} ' + '{{bind_dn:<{bind_dn}}} {{bind_pw:<{bind_pw}}}').format(**length_data) + if self.verbose > 2: + LOG.debug("Line template: {}".format(template)) + print(template.format(**header_data)) + print('-' * length_total) + for connection in conn_list: + print(template.format(**connection)) # ============================================================================= -- 2.39.5