From df24b88938fdc15efa3a5ddfc0b1e59784233a48 Mon Sep 17 00:00:00 2001 From: Simon Pichugin Date: Mon, 5 Feb 2024 20:04:40 -0800 Subject: [PATCH] Add basedn functionality check --- src/lib389/cli/dsidm | 20 +++++++++++--------- src/lib389/lib389/cli_idm/__init__.py | 19 +++++++++++++++++++ src/lib389/lib389/idm/user.py | 3 ++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/lib389/cli/dsidm b/src/lib389/cli/dsidm index 1b6762646a..d9fb1e3171 100755 --- a/src/lib389/cli/dsidm +++ b/src/lib389/cli/dsidm @@ -19,6 +19,7 @@ import argparse import argcomplete from lib389.utils import get_instance_list, instance_choices from lib389._constants import DSRC_HOME +from lib389.cli_idm import _get_basedn_arg from lib389.cli_idm import account as cli_account from lib389.cli_idm import initialise as cli_init from lib389.cli_idm import organizationalunit as cli_ou @@ -124,14 +125,6 @@ if __name__ == '__main__': parser.print_help() sys.exit(1) - if dsrc_inst['basedn'] is None: - errmsg = "Must provide a basedn!" - if args.json: - sys.stderr.write('{"desc": "%s"}\n' % errmsg) - else: - log.error(errmsg) - sys.exit(1) - if not args.verbose: signal.signal(signal.SIGINT, signal_handler) @@ -142,7 +135,16 @@ if __name__ == '__main__': result = False try: inst = connect_instance(dsrc_inst=dsrc_inst, verbose=args.verbose, args=args) - result = args.func(inst, dsrc_inst['basedn'], log, args) + if dsrc_inst['basedn'] is None: + basedn = _get_basedn_arg(inst, args, msg="Enter basedn") + if basedn is None: + errmsg = "Must provide a basedn!" + if args.json: + sys.stderr.write('{"desc": "%s"}\n' % errmsg) + else: + log.error(errmsg) + sys.exit(1) + result = args.func(inst, basedn, log, args) if args.verbose: log.info("Command successful.") except Exception as e: diff --git a/src/lib389/lib389/cli_idm/__init__.py b/src/lib389/lib389/cli_idm/__init__.py index 0dab54847e..dcbdcd1067 100644 --- a/src/lib389/lib389/cli_idm/__init__.py +++ b/src/lib389/lib389/cli_idm/__init__.py @@ -10,6 +10,14 @@ import ldap from getpass import getpass import json +from lib389._mapped_object import DSLdapObject +from lib389.cli_base import _get_dn_arg +from lib389.idm.user import DEFAULT_BASEDN as DEFAULT_BASEDN_USER + +# Create a dict where key is module and value is a DN to search +BASEDN_ENTRIES = { + 'user': DEFAULT_BASEDN_USER, +} def _get_arg(args, msg=None): @@ -37,6 +45,17 @@ def _get_args(args, kws): return kwargs +def _get_basedn_arg(inst, args, msg=None, ): + basedn_arg = _get_dn_arg(args.basedn, msg="Enter basedn") + + # Get the last part of the module name (lib389.cli_idm.user -> user) + object_rdn = BASEDN_ENTRIES[args.func.__module__.split('.')[-1]] + + if not DSLdapObject(args.inst, basedn_arg, rdn=object_rdn).exists(): + raise ValueError(f'The DN "{object_rdn},{basedn_arg}" does not exist') + return basedn_arg + + # This is really similar to get_args, but generates from an array def _get_attributes(args, attrs): kwargs = {} diff --git a/src/lib389/lib389/idm/user.py b/src/lib389/lib389/idm/user.py index 1206a6e082..82f8b2d460 100644 --- a/src/lib389/lib389/idm/user.py +++ b/src/lib389/lib389/idm/user.py @@ -23,6 +23,7 @@ 'homeDirectory', ] RDN = 'uid' +DEFAULT_BASEDN = 'ou=People' TEST_USER_PROPERTIES = { 'uid': 'testuser', @@ -201,7 +202,7 @@ class UserAccounts(DSLdapObjects): :type rdn: str """ - def __init__(self, instance, basedn, rdn='ou=People'): + def __init__(self, instance, basedn, rdn=DEFAULT_BASEDN): super(UserAccounts, self).__init__(instance) self._objectclasses = [ 'account',