# --- BEGIN COPYRIGHT BLOCK --- # Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). # See LICENSE for details. # --- END COPYRIGHT BLOCK --- # import socket import re from random import sample, randrange import pytest from ldap.controls import SimplePagedResultsControl, GetEffectiveRightsControl from lib389.tasks import * from lib389.utils import * from test389.topologies import topology_st from lib389._constants import DN_LDBM, DN_DM, DEFAULT_SUFFIX from lib389._controls import SSSRequestControl from lib389.idm.user import UserAccount, UserAccounts from lib389.cli_base import FakeArgs from lib389.config import LDBMConfig from lib389.dbgen import dbgen_users from lib389.idm.organization import Organization from lib389.idm.organizationalunit import OrganizationalUnit from lib389.backend import Backends from lib389._mapped_object import DSLdapObject pytestmark = pytest.mark.tier1 DEBUGGING = os.getenv('DEBUGGING', False) if DEBUGGING: logging.getLogger(__name__).setLevel(logging.DEBUG) else: logging.getLogger(__name__).setLevel(logging.INFO) log = logging.getLogger(__name__) TEST_USER_PWD = 'simplepaged_test' NEW_SUFFIX_1_NAME = 'test_parent' NEW_SUFFIX_1 = 'o={}'.format(NEW_SUFFIX_1_NAME) NEW_SUFFIX_2_NAME = 'child' NEW_SUFFIX_2 = 'ou={},{}'.format(NEW_SUFFIX_2_NAME, NEW_SUFFIX_1) NEW_BACKEND_1 = 'parent_base' NEW_BACKEND_2 = 'child_base' @pytest.fixture(scope="module") def create_40k_users(topology_st, request): inst = topology_st.standalone # Prepare return value retval = FakeArgs() retval.inst = inst retval.bename = '40k' retval.suffix = f'o={retval.bename}' retval.ldif_file = f'{inst.get_ldif_dir()}/{retval.bename}.ldif' # Create new backend bes = Backends(inst) be_1 = bes.create(properties={ 'cn': retval.bename, 'nsslapd-suffix': retval.suffix, }) # Set paged search lookthrough limit ldbmconfig = LDBMConfig(inst) ldbmconfig.replace('nsslapd-pagedlookthroughlimit', b'100000') # Create ldif and import it. dbgen_users(inst, 40000, retval.ldif_file, retval.suffix) # tasks = Tasks(inst) # args = {TASK_WAIT: True} # tasks.importLDIF(retval.suffix, None, retval.ldif_file, args) inst.stop() assert inst.ldif2db(retval.bename, None, None, None, retval.ldif_file, None) inst.start() # And set an aci allowing anonymous read log.info('Adding ACI to allow our test user to search') ACI_TARGET = '(targetattr != "userPassword || aci")' ACI_ALLOW = '(version 3.0; acl "Enable anonymous access";allow (read, search, compare)' ACI_SUBJECT = '(userdn = "ldap:///anyone");)' ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT o_1 = Organization(inst, retval.suffix) o_1.set('aci', ACI_BODY) return retval @pytest.fixture(scope="module") def create_user(topology_st, request): """User for binding operation""" log.info('Adding user simplepaged_test') users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX) user = users.create(properties={ 'uid': 'simplepaged_test', 'cn': 'simplepaged_test', 'sn': 'simplepaged_test', 'uidNumber': '1234', 'gidNumber': '1234', 'homeDirectory': '/home/simplepaged_test', 'userPassword': TEST_USER_PWD, }) # Now add the ACI so simplepage_test can read the users ... ACI_BODY = ensure_bytes('(targetattr= "uid || sn || dn")(version 3.0; acl "Allow read for user"; allow (read,search,compare) userdn = "ldap:///all";)') topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_REPLACE, 'aci', ACI_BODY)]) def fin(): log.info('Deleting user simplepaged_test') if not DEBUGGING: user.delete() request.addfinalizer(fin) return user @pytest.fixture(scope="module") def new_suffixes(topology_st): """Add two suffixes with backends, one is a parent of the another """ log.info('Adding suffix:{} and backend: {}'.format(NEW_SUFFIX_1, NEW_BACKEND_1)) bes = Backends(topology_st.standalone) bes.create(properties={ 'cn': 'NEW_BACKEND_1', 'nsslapd-suffix': NEW_SUFFIX_1, }) # Create the root objects with their ACI log.info('Adding ACI to allow our test user to search') ACI_TARGET = '(targetattr != "userPassword || aci")' ACI_ALLOW = '(version 3.0; acl "Enable anonymous access";allow (read, search, compare)' ACI_SUBJECT = '(userdn = "ldap:///anyone");)' ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT o_1 = Organization(topology_st.standalone, NEW_SUFFIX_1) o_1.create(properties={ 'o': NEW_SUFFIX_1_NAME, 'aci': ACI_BODY, }) log.info('Adding suffix:{} and backend: {}'.format(NEW_SUFFIX_2, NEW_BACKEND_2)) be_2 = bes.create(properties={ 'cn': 'NEW_BACKEND_2', 'nsslapd-suffix': NEW_SUFFIX_2, }) # We have to adjust the MT to say that BE_1 is a parent. mt = be_2.get_mapping_tree() mt.set_parent(NEW_SUFFIX_1) ou_2 = OrganizationalUnit(topology_st.standalone, NEW_SUFFIX_2) ou_2.create(properties={ 'ou': NEW_SUFFIX_2_NAME }) def add_users(topology_st, users_num, suffix): """Add users to the default suffix Return the list of added user DNs. """ users_list = [] users = UserAccounts(topology_st.standalone, suffix, rdn=None) log.info('Adding %d users' % users_num) for num in sample(range(1000), users_num): num_ran = int(round(num)) USER_NAME = 'test%05d' % num_ran user = users.create(properties={ 'uid': USER_NAME, 'sn': USER_NAME, 'cn': USER_NAME, 'uidNumber': '%s' % num_ran, 'gidNumber': '%s' % num_ran, 'homeDirectory': '/home/%s' % USER_NAME, 'mail': '%s@redhat.com' % USER_NAME, 'userpassword': 'pass%s' % num_ran, }) users_list.append(user) return users_list def del_users(users_list): """Delete users with DNs from given list""" log.info('Deleting %d users' % len(users_list)) for user in users_list: user.delete() def change_conf_attr(topology_st, suffix, attr_name, attr_value): """Change configuration attribute in the given suffix. Returns previous attribute value. """ entry = DSLdapObject(topology_st.standalone, suffix) attr_value_bck = entry.get_attr_val_bytes(attr_name) log.info('Set %s to %s. Previous value - %s. Modified suffix - %s.' % ( attr_name, attr_value, attr_value_bck, suffix)) if attr_value is None: entry.remove_all(attr_name) else: entry.replace(attr_name, attr_value) return attr_value_bck def paged_search(conn, suffix, controls, search_flt, searchreq_attrlist, abandon_rate=0): """Search at the DEFAULT_SUFFIX with ldap.SCOPE_SUBTREE using Simple Paged Control(should the first item in the list controls. Assert that no cookie left at the end. Return the list with results summarized from all pages. """ pages = 0 pctrls = [] all_results = [] req_pr_ctrl = controls[0] log.info('Running simple paged result search with - ' 'search suffix: {}; filter: {}; attr list {}; ' 'page_size = {}; controls: {}.'.format(suffix, search_flt, searchreq_attrlist, req_pr_ctrl.size, str(controls))) msgid = conn.search_ext(suffix, ldap.SCOPE_SUBTREE, search_flt, searchreq_attrlist, serverctrls=controls) log.info('Getting page %d' % (pages,)) while True: try: rtype, rdata, rmsgid, rctrls = conn.result3(msgid, timeout=0.001) except ldap.TIMEOUT: if pages > 0 and abandon_rate>0 and randrange(100)