From ca9ffb8b9d29422c3e59de6290cef21556821c40 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 10 May 2023 11:55:02 +0200 Subject: [PATCH] Adding Python script bin/eval-duplicate-ldap-attributes and its application module --- bin/eval-duplicate-ldap-attributes | 65 +++++++++++++++ lib/pp_admintools/app/duplicate_attribs.py | 92 ++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100755 bin/eval-duplicate-ldap-attributes create mode 100644 lib/pp_admintools/app/duplicate_attribs.py diff --git a/bin/eval-duplicate-ldap-attributes b/bin/eval-duplicate-ldap-attributes new file mode 100755 index 0000000..7f33b17 --- /dev/null +++ b/bin/eval-duplicate-ldap-attributes @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +from __future__ import print_function + +# Standard modules +import sys + +__exp_py_version_major__ = 3 +__min_py_version_minor__ = 6 + +if sys.version_info[0] != __exp_py_version_major__: + print("This script is intended to use with Python {}.".format( + __exp_py_version_major__), file=sys.stderr) + print("You are using Python: {0}.{1}.{2}-{3}-{4}.".format( + *sys.version_info) + "\n", file=sys.stderr) + sys.exit(1) + +if sys.version_info[1] < __min_py_version_minor__: + print("A minimal Python version of {maj}.{min} is necessary to execute this script.".format( + maj=__exp_py_version_major__, min=__min_py_version_minor__), file=sys.stderr) + print("You are using Python: {0}.{1}.{2}-{3}-{4}.".format( + *sys.version_info) + "\n", file=sys.stderr) + sys.exit(1) + +# Standard modules +import os +import locale + +try: + from pathlib import Path +except ImportError: + from pathlib2 import Path + +__author__ = 'Frank Brehm ' +__copyright__ = '(C) 2023 by Frank Brehm, Digitas Pixelpark GmbH, Berlin' + +# own modules: + +my_path = Path(__file__) +my_real_path = my_path.resolve() +bin_path = my_real_path.parent +base_dir = bin_path.parent +lib_dir = base_dir.joinpath('lib') +module_dir = lib_dir.joinpath('pp_admintools') + +if module_dir.exists(): + sys.path.insert(0, str(lib_dir)) + +from pp_admintools.app.duplicate_attribs import EvalDuplicateAttribsApplication + +appname = os.path.basename(sys.argv[0]) + +locale.setlocale(locale.LC_ALL, '') + +app = EvalDuplicateAttribsApplication(appname=appname, base_dir=base_dir) +app.initialized = True + +if app.verbose > 2: + print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) + +app() + +sys.exit(0) + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/lib/pp_admintools/app/duplicate_attribs.py b/lib/pp_admintools/app/duplicate_attribs.py new file mode 100644 index 0000000..f8591ad --- /dev/null +++ b/lib/pp_admintools/app/duplicate_attribs.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2023 by Frank Brehm, Berlin +@summary: An application module for evaluating duplicate attributes, which should be unique +""" +from __future__ import absolute_import + +# Standard modules +import logging +import copy + +# Third party modules +from fb_tools.xlate import format_list + +# Own modules +from .. import pp + +from ..xlate import XLATOR + +from .ldap import LdapAppError, FatalLDAPError +from .ldap import BaseLdapApplication + +__version__ = '0.1.0' +LOG = logging.getLogger(__name__) + +_ = XLATOR.gettext +ngettext = XLATOR.ngettext + + +# ============================================================================= +class EvalDuplicateAttribsApplication(BaseLdapApplication): + """Application class for evaluating duplicate attributes, which should be unique, in LDAP.""" + + default_uniq_attributes = ['uid', 'uidNumber', 'mail'] + default_dpendend_uniq_attribs = { + 'gidNumber': 'objectClass=posixGroup', + } + + # ------------------------------------------------------------------------- + def __init__(self, appname=None, base_dir=None): + """Constructor.""" + self.uniq_attributes = copy.copy(self.default_uniq_attributes) + self.dpendend_uniq_attribs = copy.copy(self.default_dpendend_uniq_attribs) + self.result = {} + + desc = _( + "Evaluating all LDAP entries, which are using duplicate attributes, which " + "should be unique.") + + super(EvalDuplicateAttribsApplication, self).__init__( + appname=appname, description=desc, base_dir=base_dir, + version=__version__, initialized=False) + + self.initialized = True + + # ------------------------------------------------------------------------- + def init_arg_parser(self): + """Initialize additional command line parameters.""" + eval_group = self.arg_parser.add_argument_group(_('Evaluation options')) + + eval_group.add_argument( + '-U', '--uniq-attribs', nargs='*', dest='uniq_attribs', metavar=_('ATTRIBUTE'), + help=_( + "All attributes, which should be unique over the complete LDAP tree. " + "Per default the following attributes should be unique:" + ) + ' ' + format_list(self.default_uniq_attributes, do_repr=True), + ) + + super(EvalDuplicateAttribsApplication, self).init_arg_parser() + + # ------------------------------------------------------------------------- + def _verify_instances(self): + + super(EvalDuplicateAttribsApplication, self)._verify_instances( + is_admin=False, readonly=True) + + # ------------------------------------------------------------------------- + def _run(self): + + LOG.info("And here we go ...") + + +# ============================================================================= +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5