Skip to content

Commit

Permalink
Add basedn functionality check
Browse files Browse the repository at this point in the history
  • Loading branch information
droideck committed Feb 6, 2024
1 parent 8fe7586 commit df24b88
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/lib389/cli/dsidm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions src/lib389/lib389/cli_idm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = {}
Expand Down
3 changes: 2 additions & 1 deletion src/lib389/lib389/idm/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'homeDirectory',
]
RDN = 'uid'
DEFAULT_BASEDN = 'ou=People'

TEST_USER_PROPERTIES = {
'uid': 'testuser',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit df24b88

Please sign in to comment.