| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2020 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import socket |
| 10 |
import re |
| 11 |
from random import sample, randrange |
| 12 |
|
| 13 |
import pytest |
| 14 |
from ldap.controls import SimplePagedResultsControl, GetEffectiveRightsControl |
| 15 |
from lib389.tasks import * |
| 16 |
from lib389.utils import * |
| 17 |
from test389.topologies import topology_st |
| 18 |
from lib389._constants import DN_LDBM, DN_DM, DEFAULT_SUFFIX |
| 19 |
from lib389._controls import SSSRequestControl |
| 20 |
from lib389.idm.user import UserAccount, UserAccounts |
| 21 |
from lib389.cli_base import FakeArgs |
| 22 |
from lib389.config import LDBMConfig |
| 23 |
from lib389.dbgen import dbgen_users |
| 24 |
|
| 25 |
from lib389.idm.organization import Organization |
| 26 |
from lib389.idm.organizationalunit import OrganizationalUnit |
| 27 |
from lib389.backend import Backends |
| 28 |
from lib389._mapped_object import DSLdapObject |
| 29 |
|
| 30 |
pytestmark = pytest.mark.tier1 |
| 31 |
|
| 32 |
DEBUGGING = os.getenv('DEBUGGING', False) |
| 33 |
|
| 34 |
if DEBUGGING: |
| 35 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 36 |
else: |
| 37 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 38 |
|
| 39 |
log = logging.getLogger(__name__) |
| 40 |
|
| 41 |
TEST_USER_PWD = 'simplepaged_test' |
| 42 |
|
| 43 |
NEW_SUFFIX_1_NAME = 'test_parent' |
| 44 |
NEW_SUFFIX_1 = 'o={}'.format(NEW_SUFFIX_1_NAME) |
| 45 |
NEW_SUFFIX_2_NAME = 'child' |
| 46 |
NEW_SUFFIX_2 = 'ou={},{}'.format(NEW_SUFFIX_2_NAME, NEW_SUFFIX_1) |
| 47 |
NEW_BACKEND_1 = 'parent_base' |
| 48 |
NEW_BACKEND_2 = 'child_base' |
| 49 |
|
| 50 |
@pytest.fixture(scope="module") |
| 51 |
def create_40k_users(topology_st, request): |
| 52 |
inst = topology_st.standalone |
| 53 |
|
| 54 |
# Prepare return value |
| 55 |
retval = FakeArgs() |
| 56 |
retval.inst = inst |
| 57 |
retval.bename = '40k' |
| 58 |
retval.suffix = f'o={retval.bename}' |
| 59 |
retval.ldif_file = f'{inst.get_ldif_dir()}/{retval.bename}.ldif' |
| 60 |
|
| 61 |
# Create new backend |
| 62 |
bes = Backends(inst) |
| 63 |
be_1 = bes.create(properties={ |
| 64 |
'cn': retval.bename, |
| 65 |
'nsslapd-suffix': retval.suffix, |
| 66 |
}) |
| 67 |
|
| 68 |
# Set paged search lookthrough limit |
| 69 |
ldbmconfig = LDBMConfig(inst) |
| 70 |
ldbmconfig.replace('nsslapd-pagedlookthroughlimit', b'100000') |
| 71 |
|
| 72 |
# Create ldif and import it. |
| 73 |
dbgen_users(inst, 40000, retval.ldif_file, retval.suffix) |
| 74 |
# tasks = Tasks(inst) |
| 75 |
# args = {TASK_WAIT: True} |
| 76 |
# tasks.importLDIF(retval.suffix, None, retval.ldif_file, args) |
| 77 |
inst.stop() |
| 78 |
assert inst.ldif2db(retval.bename, None, None, None, retval.ldif_file, None) |
| 79 |
inst.start() |
| 80 |
|
| 81 |
# And set an aci allowing anonymous read |
| 82 |
log.info('Adding ACI to allow our test user to search') |
| 83 |
ACI_TARGET = '(targetattr != "userPassword || aci")' |
| 84 |
ACI_ALLOW = '(version 3.0; acl "Enable anonymous access";allow (read, search, compare)' |
| 85 |
ACI_SUBJECT = '(userdn = "ldap:///anyone");)' |
| 86 |
ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT |
| 87 |
o_1 = Organization(inst, retval.suffix) |
| 88 |
o_1.set('aci', ACI_BODY) |
| 89 |
|
| 90 |
return retval |
| 91 |
|
| 92 |
|
| 93 |
@pytest.fixture(scope="module") |
| 94 |
def create_user(topology_st, request): |
| 95 |
"""User for binding operation""" |
| 96 |
|
| 97 |
log.info('Adding user simplepaged_test') |
| 98 |
users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX) |
| 99 |
user = users.create(properties={ |
| 100 |
'uid': 'simplepaged_test', |
| 101 |
'cn': 'simplepaged_test', |
| 102 |
'sn': 'simplepaged_test', |
| 103 |
'uidNumber': '1234', |
| 104 |
'gidNumber': '1234', |
| 105 |
'homeDirectory': '/home/simplepaged_test', |
| 106 |
'userPassword': TEST_USER_PWD, |
| 107 |
}) |
| 108 |
|
| 109 |
# Now add the ACI so simplepage_test can read the users ... |
| 110 |
ACI_BODY = ensure_bytes('(targetattr= "uid || sn || dn")(version 3.0; acl "Allow read for user"; allow (read,search,compare) userdn = "ldap:///all";)') |
| 111 |
topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_REPLACE, 'aci', ACI_BODY)]) |
| 112 |
|
| 113 |
def fin(): |
| 114 |
log.info('Deleting user simplepaged_test') |
| 115 |
if not DEBUGGING: |
| 116 |
user.delete() |
| 117 |
|
| 118 |
request.addfinalizer(fin) |
| 119 |
|
| 120 |
return user |
| 121 |
|
| 122 |
@pytest.fixture(scope="module") |
| 123 |
def new_suffixes(topology_st): |
| 124 |
"""Add two suffixes with backends, one is a parent |
| 125 |
of the another |
| 126 |
""" |
| 127 |
|
| 128 |
log.info('Adding suffix:{} and backend: {}'.format(NEW_SUFFIX_1, NEW_BACKEND_1)) |
| 129 |
|
| 130 |
bes = Backends(topology_st.standalone) |
| 131 |
|
| 132 |
bes.create(properties={ |
| 133 |
'cn': 'NEW_BACKEND_1', |
| 134 |
'nsslapd-suffix': NEW_SUFFIX_1, |
| 135 |
}) |
| 136 |
# Create the root objects with their ACI |
| 137 |
log.info('Adding ACI to allow our test user to search') |
| 138 |
ACI_TARGET = '(targetattr != "userPassword || aci")' |
| 139 |
ACI_ALLOW = '(version 3.0; acl "Enable anonymous access";allow (read, search, compare)' |
| 140 |
ACI_SUBJECT = '(userdn = "ldap:///anyone");)' |
| 141 |
ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT |
| 142 |
|
| 143 |
o_1 = Organization(topology_st.standalone, NEW_SUFFIX_1) |
| 144 |
o_1.create(properties={ |
| 145 |
'o': NEW_SUFFIX_1_NAME, |
| 146 |
'aci': ACI_BODY, |
| 147 |
}) |
| 148 |
|
| 149 |
log.info('Adding suffix:{} and backend: {}'.format(NEW_SUFFIX_2, NEW_BACKEND_2)) |
| 150 |
be_2 = bes.create(properties={ |
| 151 |
'cn': 'NEW_BACKEND_2', |
| 152 |
'nsslapd-suffix': NEW_SUFFIX_2, |
| 153 |
}) |
| 154 |
|
| 155 |
# We have to adjust the MT to say that BE_1 is a parent. |
| 156 |
mt = be_2.get_mapping_tree() |
| 157 |
mt.set_parent(NEW_SUFFIX_1) |
| 158 |
|
| 159 |
ou_2 = OrganizationalUnit(topology_st.standalone, NEW_SUFFIX_2) |
| 160 |
ou_2.create(properties={ |
| 161 |
'ou': NEW_SUFFIX_2_NAME |
| 162 |
}) |
| 163 |
|
| 164 |
|
| 165 |
def add_users(topology_st, users_num, suffix): |
| 166 |
"""Add users to the default suffix |
| 167 |
|
| 168 |
Return the list of added user DNs. |
| 169 |
""" |
| 170 |
|
| 171 |
users_list = [] |
| 172 |
users = UserAccounts(topology_st.standalone, suffix, rdn=None) |
| 173 |
|
| 174 |
log.info('Adding %d users' % users_num) |
| 175 |
for num in sample(range(1000), users_num): |
| 176 |
num_ran = int(round(num)) |
| 177 |
USER_NAME = 'test%05d' % num_ran |
| 178 |
|
| 179 |
user = users.create(properties={ |
| 180 |
'uid': USER_NAME, |
| 181 |
'sn': USER_NAME, |
| 182 |
'cn': USER_NAME, |
| 183 |
'uidNumber': '%s' % num_ran, |
| 184 |
'gidNumber': '%s' % num_ran, |
| 185 |
'homeDirectory': '/home/%s' % USER_NAME, |
| 186 |
'mail': '%[email protected]' % USER_NAME, |
| 187 |
'userpassword': 'pass%s' % num_ran, |
| 188 |
}) |
| 189 |
users_list.append(user) |
| 190 |
return users_list |
| 191 |
|
| 192 |
|
| 193 |
def del_users(users_list): |
| 194 |
"""Delete users with DNs from given list""" |
| 195 |
|
| 196 |
log.info('Deleting %d users' % len(users_list)) |
| 197 |
for user in users_list: |
| 198 |
user.delete() |
| 199 |
|
| 200 |
|
| 201 |
def change_conf_attr(topology_st, suffix, attr_name, attr_value): |
| 202 |
"""Change configuration attribute in the given suffix. |
| 203 |
|
| 204 |
Returns previous attribute value. |
| 205 |
""" |
| 206 |
|
| 207 |
entry = DSLdapObject(topology_st.standalone, suffix) |
| 208 |
|
| 209 |
attr_value_bck = entry.get_attr_val_bytes(attr_name) |
| 210 |
log.info('Set %s to %s. Previous value - %s. Modified suffix - %s.' % ( |
| 211 |
attr_name, attr_value, attr_value_bck, suffix)) |
| 212 |
if attr_value is None: |
| 213 |
entry.remove_all(attr_name) |
| 214 |
else: |
| 215 |
entry.replace(attr_name, attr_value) |
| 216 |
return attr_value_bck |
| 217 |
|
| 218 |
|
| 219 |
def paged_search(conn, suffix, controls, search_flt, searchreq_attrlist, abandon_rate=0): |
| 220 |
"""Search at the DEFAULT_SUFFIX with ldap.SCOPE_SUBTREE |
| 221 |
using Simple Paged Control(should the first item in the |
| 222 |
list controls. |
| 223 |
Assert that no cookie left at the end. |
| 224 |
|
| 225 |
Return the list with results summarized from all pages. |
| 226 |
""" |
| 227 |
|
| 228 |
pages = 0 |
| 229 |
pctrls = [] |
| 230 |
all_results = [] |
| 231 |
req_pr_ctrl = controls[0] |
| 232 |
log.info('Running simple paged result search with - ' |
| 233 |
'search suffix: {}; filter: {}; attr list {}; ' |
| 234 |
'page_size = {}; controls: {}.'.format(suffix, search_flt, |
| 235 |
searchreq_attrlist, |
| 236 |
req_pr_ctrl.size, |
| 237 |
str(controls))) |
| 238 |
msgid = conn.search_ext(suffix, ldap.SCOPE_SUBTREE, search_flt, searchreq_attrlist, serverctrls=controls) |
| 239 |
log.info('Getting page %d' % (pages,)) |
| 240 |
while True: |
| 241 |
try: |
| 242 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid, timeout=0.001) |
| 243 |
except ldap.TIMEOUT: |
| 244 |
if pages > 0 and abandon_rate>0 and randrange(100)<abandon_rate: |
| 245 |
conn.abandon(msgid) |
| 246 |
log.info('Paged result search is abandonned.') |
| 247 |
return all_results |
| 248 |
continue |
| 249 |
log.debug('Data: {}'.format(rdata)) |
| 250 |
all_results.extend(rdata) |
| 251 |
pages += 1 |
| 252 |
pctrls = [ |
| 253 |
c |
| 254 |
for c in rctrls |
| 255 |
if c.controlType == SimplePagedResultsControl.controlType |
| 256 |
] |
| 257 |
|
| 258 |
if pctrls: |
| 259 |
if pctrls[0].cookie: |
| 260 |
# Copy cookie from response control to request control |
| 261 |
log.debug('Cookie: {}'.format(pctrls[0].cookie)) |
| 262 |
req_pr_ctrl.cookie = pctrls[0].cookie |
| 263 |
msgid = conn.search_ext(suffix, ldap.SCOPE_SUBTREE, search_flt, searchreq_attrlist, serverctrls=controls) |
| 264 |
else: |
| 265 |
break # No more pages available |
| 266 |
else: |
| 267 |
break |
| 268 |
log.info('Getting page %d' % (pages,)) |
| 269 |
|
| 270 |
assert not pctrls[0].cookie |
| 271 |
return all_results |
| 272 |
|
| 273 |
|
| 274 |
@pytest.mark.parametrize("page_size,users_num", [(6, 5), (5, 5), (5, 25)]) |
| 275 |
def test_search_success(topology_st, create_user, page_size, users_num): |
| 276 |
"""Verify that search with a simple paged results control |
| 277 |
returns all entries it should without errors. |
| 278 |
|
| 279 |
:id: ddd15b70-64f1-4a85-a793-b24761e50354 |
| 280 |
:customerscenario: True |
| 281 |
:parametrized: yes |
| 282 |
:feature: Simple paged results |
| 283 |
:setup: Standalone instance, test user for binding, |
| 284 |
varying number of users for the search base |
| 285 |
:steps: |
| 286 |
1. Bind as test user |
| 287 |
2. Search through added users with a simple paged control |
| 288 |
:expectedresults: |
| 289 |
1. Bind should be successful |
| 290 |
2. All users should be found |
| 291 |
""" |
| 292 |
|
| 293 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 294 |
search_flt = r'(uid=test*)' |
| 295 |
searchreq_attrlist = ['dn', 'sn'] |
| 296 |
|
| 297 |
log.info('Set user bind %s ' % create_user) |
| 298 |
conn = create_user.bind(TEST_USER_PWD) |
| 299 |
|
| 300 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 301 |
all_results = paged_search(conn, DEFAULT_SUFFIX, [req_ctrl], search_flt, searchreq_attrlist) |
| 302 |
|
| 303 |
log.info('%d results' % len(all_results)) |
| 304 |
assert len(all_results) == len(users_list) |
| 305 |
|
| 306 |
del_users(users_list) |
| 307 |
|
| 308 |
|
| 309 |
@pytest.mark.parametrize("page_size,users_num,suffix,attr_name,attr_value,expected_err", [ |
| 310 |
(50, 200, 'cn=config,%s' % DN_LDBM, 'nsslapd-idlistscanlimit', '100', |
| 311 |
ldap.UNWILLING_TO_PERFORM), |
| 312 |
(5, 15, DN_CONFIG, 'nsslapd-timelimit', '20', |
| 313 |
ldap.UNAVAILABLE_CRITICAL_EXTENSION), |
| 314 |
(21, 50, DN_CONFIG, 'nsslapd-sizelimit', '20', |
| 315 |
ldap.SIZELIMIT_EXCEEDED), |
| 316 |
(21, 50, DN_CONFIG, 'nsslapd-pagedsizelimit', '5', |
| 317 |
ldap.SIZELIMIT_EXCEEDED), |
| 318 |
(5, 50, 'cn=config,%s' % DN_LDBM, 'nsslapd-lookthroughlimit', '20', |
| 319 |
ldap.ADMINLIMIT_EXCEEDED)]) |
| 320 |
def test_search_limits_fail(topology_st, create_user, page_size, users_num, |
| 321 |
suffix, attr_name, attr_value, expected_err): |
| 322 |
"""Verify that search with a simple paged results control |
| 323 |
throws expected exceptoins when corresponding limits are |
| 324 |
exceeded. |
| 325 |
|
| 326 |
:id: e3067107-bd6d-493d-9989-3e641a9337b0 |
| 327 |
:customerscenario: True |
| 328 |
:parametrized: yes |
| 329 |
:setup: Standalone instance, test user for binding, |
| 330 |
varying number of users for the search base |
| 331 |
:steps: |
| 332 |
1. Bind as test user |
| 333 |
2. Set limit attribute to the value that will cause |
| 334 |
an expected exception |
| 335 |
3. Search through added users with a simple paged control |
| 336 |
:expectedresults: |
| 337 |
1. Bind should be successful |
| 338 |
2. Operation should be successful |
| 339 |
3. Should fail with appropriate exception |
| 340 |
""" |
| 341 |
|
| 342 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 343 |
attr_value_bck = change_conf_attr(topology_st, suffix, attr_name, attr_value) |
| 344 |
conf_param_dict = {attr_name: attr_value} |
| 345 |
search_flt = r'(uid=test*)' |
| 346 |
searchreq_attrlist = ['dn', 'sn'] |
| 347 |
controls = [] |
| 348 |
|
| 349 |
try: |
| 350 |
log.info('Set user bind') |
| 351 |
conn = create_user.bind(TEST_USER_PWD) |
| 352 |
|
| 353 |
log.info('Create simple paged results control instance') |
| 354 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 355 |
controls.append(req_ctrl) |
| 356 |
if attr_name == 'nsslapd-idlistscanlimit': |
| 357 |
sort_ctrl = SSSRequestControl(True, ['sn']) |
| 358 |
controls.append(sort_ctrl) |
| 359 |
log.info('Initiate ldapsearch with created control instance') |
| 360 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 361 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 362 |
|
| 363 |
time_val = conf_param_dict.get('nsslapd-timelimit') |
| 364 |
if time_val: |
| 365 |
time.sleep(int(time_val) + 10) |
| 366 |
|
| 367 |
pages = 0 |
| 368 |
all_results = [] |
| 369 |
pctrls = [] |
| 370 |
while True: |
| 371 |
log.info('Getting page %d' % (pages,)) |
| 372 |
if pages == 0 and (time_val or attr_name == 'nsslapd-pagesizelimit'): |
| 373 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 374 |
else: |
| 375 |
with pytest.raises(expected_err): |
| 376 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 377 |
all_results.extend(rdata) |
| 378 |
pages += 1 |
| 379 |
pctrls = [ |
| 380 |
c |
| 381 |
for c in rctrls |
| 382 |
if c.controlType == SimplePagedResultsControl.controlType |
| 383 |
] |
| 384 |
|
| 385 |
if pctrls: |
| 386 |
if pctrls[0].cookie: |
| 387 |
# Copy cookie from response control to request control |
| 388 |
req_ctrl.cookie = pctrls[0].cookie |
| 389 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 390 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 391 |
else: |
| 392 |
break # No more pages available |
| 393 |
else: |
| 394 |
break |
| 395 |
finally: |
| 396 |
del_users(users_list) |
| 397 |
change_conf_attr(topology_st, suffix, attr_name, attr_value_bck) |
| 398 |
|
| 399 |
|
| 400 |
def test_search_sort_success(topology_st, create_user): |
| 401 |
"""Verify that search with a simple paged results control |
| 402 |
and a server side sort control returns all entries |
| 403 |
it should without errors. |
| 404 |
|
| 405 |
:id: 17d8b150-ed43-41e1-b80f-ee9b4ce45155 |
| 406 |
:customerscenario: True |
| 407 |
:setup: Standalone instance, test user for binding, |
| 408 |
varying number of users for the search base |
| 409 |
:steps: |
| 410 |
1. Bind as test user |
| 411 |
2. Search through added users with a simple paged control |
| 412 |
and a server side sort control |
| 413 |
:expectedresults: |
| 414 |
1. Bind should be successful |
| 415 |
2. All users should be found and sorted |
| 416 |
""" |
| 417 |
|
| 418 |
users_num = 50 |
| 419 |
page_size = 5 |
| 420 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 421 |
search_flt = r'(uid=test*)' |
| 422 |
searchreq_attrlist = ['dn', 'sn'] |
| 423 |
|
| 424 |
try: |
| 425 |
conn = create_user.bind(TEST_USER_PWD) |
| 426 |
|
| 427 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 428 |
sort_ctrl = SSSRequestControl(True, ['sn']) |
| 429 |
|
| 430 |
log.info('Initiate ldapsearch with created control instance') |
| 431 |
log.info('Collect data with sorting') |
| 432 |
controls = [req_ctrl, sort_ctrl] |
| 433 |
results_sorted = paged_search(conn, DEFAULT_SUFFIX, controls, |
| 434 |
search_flt, searchreq_attrlist) |
| 435 |
|
| 436 |
log.info('Substring numbers from user DNs') |
| 437 |
# r_nums = map(lambda x: int(x[0][8:13]), results_sorted) |
| 438 |
r_nums = [int(x[0][8:13]) for x in results_sorted] |
| 439 |
|
| 440 |
log.info('Assert that list is sorted') |
| 441 |
assert all(r_nums[i] <= r_nums[i + 1] for i in range(len(r_nums) - 1)) |
| 442 |
finally: |
| 443 |
del_users(users_list) |
| 444 |
|
| 445 |
|
| 446 |
def test_search_abandon(topology_st, create_user): |
| 447 |
"""Verify that search with simple paged results control |
| 448 |
can be abandon |
| 449 |
|
| 450 |
:id: 0008538b-7585-4356-839f-268828066978 |
| 451 |
:customerscenario: True |
| 452 |
:setup: Standalone instance, test user for binding, |
| 453 |
varying number of users for the search base |
| 454 |
:steps: |
| 455 |
1. Bind as test user |
| 456 |
2. Search through added users with a simple paged control |
| 457 |
3. Abandon the search |
| 458 |
:expectedresults: |
| 459 |
1. Bind should be successful |
| 460 |
2. Search should be started successfully |
| 461 |
3. It should throw an ldap.TIMEOUT exception |
| 462 |
while trying to get the rest of the search results |
| 463 |
""" |
| 464 |
|
| 465 |
users_num = 10 |
| 466 |
page_size = 2 |
| 467 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 468 |
search_flt = r'(uid=test*)' |
| 469 |
searchreq_attrlist = ['dn', 'sn'] |
| 470 |
|
| 471 |
try: |
| 472 |
log.info('Set user bind') |
| 473 |
conn = create_user.bind(TEST_USER_PWD) |
| 474 |
|
| 475 |
log.info('Create simple paged results control instance') |
| 476 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 477 |
controls = [req_ctrl] |
| 478 |
|
| 479 |
log.info('Initiate a search with a paged results control') |
| 480 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 481 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 482 |
log.info('Abandon the search') |
| 483 |
conn.abandon(msgid) |
| 484 |
|
| 485 |
log.info('Expect an ldap.TIMEOUT exception, while trying to get the search results') |
| 486 |
with pytest.raises(ldap.TIMEOUT): |
| 487 |
conn.result3(msgid, timeout=5) |
| 488 |
finally: |
| 489 |
del_users(users_list) |
| 490 |
|
| 491 |
|
| 492 |
def test_search_with_timelimit(topology_st, create_user): |
| 493 |
"""Verify that after performing multiple simple paged searches |
| 494 |
to completion, each with a timelimit, it wouldn't fail, if we sleep |
| 495 |
for a time more than the timelimit. |
| 496 |
|
| 497 |
:id: 6cd7234b-136c-419f-bf3e-43aa73592cff |
| 498 |
:customerscenario: True |
| 499 |
:setup: Standalone instance, test user for binding, |
| 500 |
varying number of users for the search base |
| 501 |
:steps: |
| 502 |
1. Bind as test user |
| 503 |
2. Search through added users with a simple paged control |
| 504 |
and timelimit set to 5 |
| 505 |
3. When the returned cookie is empty, wait 10 seconds |
| 506 |
4. Perform steps 2 and 3 three times in a row |
| 507 |
:expectedresults: |
| 508 |
1. Bind should be successful |
| 509 |
2. No error should happen |
| 510 |
3. 10 seconds should pass |
| 511 |
4. No error should happen |
| 512 |
""" |
| 513 |
|
| 514 |
users_num = 100 |
| 515 |
page_size = 50 |
| 516 |
timelimit = 5 |
| 517 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 518 |
search_flt = r'(uid=test*)' |
| 519 |
searchreq_attrlist = ['dn', 'sn'] |
| 520 |
|
| 521 |
try: |
| 522 |
log.info('Set user bind') |
| 523 |
conn = create_user.bind(TEST_USER_PWD) |
| 524 |
|
| 525 |
log.info('Create simple paged results control instance') |
| 526 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 527 |
controls = [req_ctrl] |
| 528 |
|
| 529 |
for ii in range(3): |
| 530 |
log.info('Iteration %d' % ii) |
| 531 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, search_flt, |
| 532 |
searchreq_attrlist, serverctrls=controls, timeout=timelimit) |
| 533 |
|
| 534 |
pages = 0 |
| 535 |
pctrls = [] |
| 536 |
while True: |
| 537 |
log.info('Getting page %d' % (pages,)) |
| 538 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 539 |
pages += 1 |
| 540 |
pctrls = [ |
| 541 |
c |
| 542 |
for c in rctrls |
| 543 |
if c.controlType == SimplePagedResultsControl.controlType |
| 544 |
] |
| 545 |
|
| 546 |
if pctrls: |
| 547 |
if pctrls[0].cookie: |
| 548 |
# Copy cookie from response control to request control |
| 549 |
req_ctrl.cookie = pctrls[0].cookie |
| 550 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, search_flt, |
| 551 |
searchreq_attrlist, serverctrls=controls, timeout=timelimit) |
| 552 |
else: |
| 553 |
log.info('Done with this search - sleeping %d seconds' % ( |
| 554 |
timelimit * 2)) |
| 555 |
time.sleep(timelimit * 2) |
| 556 |
break # No more pages available |
| 557 |
else: |
| 558 |
break |
| 559 |
finally: |
| 560 |
del_users(users_list) |
| 561 |
|
| 562 |
|
| 563 |
def test_search_ip_aci(topology_st, create_user): |
| 564 |
"""Verify that after performing multiple simple paged searches |
| 565 |
to completion on the suffix with IP based ACI |
| 566 |
|
| 567 |
:id: bbfddc46-a8c8-49ae-8c90-7265d05b22a9 |
| 568 |
:customerscenario: True |
| 569 |
:parametrized: yes |
| 570 |
:setup: Standalone instance, test user for binding, |
| 571 |
varying number of users for the search base |
| 572 |
:steps: |
| 573 |
1. Back up and remove all previous ACI from suffix |
| 574 |
2. Add an anonymous ACI for IP check |
| 575 |
3. Bind as test user |
| 576 |
4. Search through added users with a simple paged control |
| 577 |
5. Perform steps 4 three times in a row |
| 578 |
6. Return ACI to the initial state |
| 579 |
:expectedresults: |
| 580 |
1. Operation should be successful |
| 581 |
2. Anonymous ACI should be successfully added |
| 582 |
3. Bind should be successful |
| 583 |
4. No error happens, all users should be found and sorted |
| 584 |
5. Results should remain the same |
| 585 |
6. ACI should be successfully returned |
| 586 |
""" |
| 587 |
users_num = 20 |
| 588 |
page_size = 5 |
| 589 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 590 |
search_flt = r'(uid=test*)' |
| 591 |
searchreq_attrlist = ['dn', 'sn'] |
| 592 |
HOSTNAME = socket.gethostname() |
| 593 |
IP_ADDRESS = socket.gethostbyname(HOSTNAME) |
| 594 |
|
| 595 |
try: |
| 596 |
log.info('Back up current suffix ACI') |
| 597 |
acis_bck = topology_st.standalone.aci.list(DEFAULT_SUFFIX, ldap.SCOPE_BASE) |
| 598 |
|
| 599 |
log.info('Add test ACI') |
| 600 |
bind_rule = f'ip = "{IP_ADDRESS}" or ip = "::1"' |
| 601 |
ACI_TARGET = '(targetattr != "userPassword")' |
| 602 |
ACI_ALLOW = '(version 3.0;acl "Anonymous access within domain"; allow (read,compare,search)' |
| 603 |
ACI_SUBJECT = '(userdn = "ldap:///anyone") and (%s);)' % bind_rule |
| 604 |
ACI_BODY = ensure_bytes(ACI_TARGET + ACI_ALLOW + ACI_SUBJECT) |
| 605 |
topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_REPLACE, 'aci', ACI_BODY)]) |
| 606 |
time.sleep(.5) |
| 607 |
|
| 608 |
log.info('Set user bind') |
| 609 |
conn = create_user.bind(TEST_USER_PWD, uri=f'ldap://{HOSTNAME}:{topology_st.standalone.port}') |
| 610 |
|
| 611 |
log.info('Create simple paged results control instance') |
| 612 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 613 |
controls = [req_ctrl] |
| 614 |
|
| 615 |
log.info('Initiate three searches with a paged results control') |
| 616 |
for ii in range(3): |
| 617 |
log.info('%d search' % (ii + 1)) |
| 618 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, |
| 619 |
search_flt, searchreq_attrlist) |
| 620 |
log.info('%d results' % len(all_results)) |
| 621 |
assert len(all_results) == len(users_list) |
| 622 |
log.info('If we are here, then no error has happened. We are good.') |
| 623 |
|
| 624 |
finally: |
| 625 |
log.info('Restore ACI') |
| 626 |
topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', None)]) |
| 627 |
for aci in acis_bck: |
| 628 |
topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', aci.getRawAci())]) |
| 629 |
time.sleep(1) |
| 630 |
del_users(users_list) |
| 631 |
|
| 632 |
|
| 633 |
def test_search_multiple_paging(topology_st, create_user): |
| 634 |
"""Verify that after performing multiple simple paged searches |
| 635 |
on a single connection without a complition, it wouldn't fail. |
| 636 |
|
| 637 |
:id: 628b29a6-2d47-4116-a88d-00b87405ef7f |
| 638 |
:customerscenario: True |
| 639 |
:setup: Standalone instance, test user for binding, |
| 640 |
varying number of users for the search base |
| 641 |
:steps: |
| 642 |
1. Bind as test user |
| 643 |
2. Initiate the search with a simple paged control |
| 644 |
3. Acquire the returned cookie only one time |
| 645 |
4. Perform steps 2 and 3 three times in a row |
| 646 |
:expectedresults: |
| 647 |
1. Bind should be successful |
| 648 |
2. Search should be successfully initiated |
| 649 |
3. Cookie should be successfully acquired |
| 650 |
4. No error happens |
| 651 |
""" |
| 652 |
|
| 653 |
users_num = 20 |
| 654 |
page_size = 5 |
| 655 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 656 |
search_flt = r'(uid=test*)' |
| 657 |
searchreq_attrlist = ['dn', 'sn'] |
| 658 |
|
| 659 |
try: |
| 660 |
log.info('Set user bind') |
| 661 |
conn = create_user.bind(TEST_USER_PWD) |
| 662 |
|
| 663 |
log.info('Create simple paged results control instance') |
| 664 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 665 |
controls = [req_ctrl] |
| 666 |
|
| 667 |
for ii in range(3): |
| 668 |
log.info('Iteration %d' % ii) |
| 669 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 670 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 671 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 672 |
pctrls = [ |
| 673 |
c |
| 674 |
for c in rctrls |
| 675 |
if c.controlType == SimplePagedResultsControl.controlType |
| 676 |
] |
| 677 |
|
| 678 |
# Copy cookie from response control to request control |
| 679 |
req_ctrl.cookie = pctrls[0].cookie |
| 680 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 681 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 682 |
finally: |
| 683 |
del_users(users_list) |
| 684 |
|
| 685 |
|
| 686 |
@pytest.mark.parametrize("invalid_cookie", [1000, -1]) |
| 687 |
def test_search_invalid_cookie(topology_st, create_user, invalid_cookie): |
| 688 |
"""Verify that using invalid cookie while performing |
| 689 |
search with the simple paged results control throws |
| 690 |
a TypeError exception |
| 691 |
|
| 692 |
:id: 107be12d-4fe4-47fe-ae86-f3e340a56f42 |
| 693 |
:customerscenario: True |
| 694 |
:parametrized: yes |
| 695 |
:setup: Standalone instance, test user for binding, |
| 696 |
varying number of users for the search base |
| 697 |
:steps: |
| 698 |
1. Bind as test user |
| 699 |
2. Initiate the search with a simple paged control |
| 700 |
3. Put an invalid cookie (-1, 1000) to the control |
| 701 |
4. Continue the search |
| 702 |
:expectedresults: |
| 703 |
1. Bind should be successful |
| 704 |
2. Search should be successfully initiated |
| 705 |
3. Cookie should be added |
| 706 |
4. It should throw a TypeError exception |
| 707 |
""" |
| 708 |
|
| 709 |
users_num = 20 |
| 710 |
page_size = 5 |
| 711 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 712 |
search_flt = r'(uid=test*)' |
| 713 |
searchreq_attrlist = ['dn', 'sn'] |
| 714 |
|
| 715 |
try: |
| 716 |
log.info('Set user bind') |
| 717 |
conn = create_user.bind(TEST_USER_PWD) |
| 718 |
|
| 719 |
log.info('Create simple paged results control instance') |
| 720 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 721 |
controls = [req_ctrl] |
| 722 |
|
| 723 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 724 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 725 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 726 |
|
| 727 |
log.info('Put an invalid cookie (%d) to the control. TypeError is expected' % |
| 728 |
invalid_cookie) |
| 729 |
req_ctrl.cookie = invalid_cookie |
| 730 |
with pytest.raises(TypeError): |
| 731 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 732 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 733 |
finally: |
| 734 |
del_users(users_list) |
| 735 |
|
| 736 |
|
| 737 |
def test_search_abandon_with_zero_size(topology_st, create_user): |
| 738 |
"""Verify that search with simple paged results control |
| 739 |
can be abandon using page_size = 0 |
| 740 |
|
| 741 |
:id: d2fd9a10-84e1-4b69-a8a7-36ca1427c171 |
| 742 |
:customerscenario: True |
| 743 |
:setup: Standalone instance, test user for binding, |
| 744 |
varying number of users for the search base |
| 745 |
:steps: |
| 746 |
1. Bind as test user |
| 747 |
2. Search through added users with a simple paged control |
| 748 |
and page_size = 0 |
| 749 |
:expectedresults: |
| 750 |
1. Bind should be successful |
| 751 |
2. No cookie should be returned at all |
| 752 |
""" |
| 753 |
|
| 754 |
users_num = 10 |
| 755 |
page_size = 0 |
| 756 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 757 |
search_flt = r'(uid=test*)' |
| 758 |
searchreq_attrlist = ['dn', 'sn'] |
| 759 |
|
| 760 |
try: |
| 761 |
log.info('Set user bind') |
| 762 |
conn = create_user.bind(TEST_USER_PWD) |
| 763 |
|
| 764 |
log.info('Create simple paged results control instance') |
| 765 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 766 |
controls = [req_ctrl] |
| 767 |
|
| 768 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 769 |
search_flt, searchreq_attrlist, serverctrls=controls) |
| 770 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 771 |
pctrls = [ |
| 772 |
c |
| 773 |
for c in rctrls |
| 774 |
if c.controlType == SimplePagedResultsControl.controlType |
| 775 |
] |
| 776 |
assert not pctrls[0].cookie |
| 777 |
finally: |
| 778 |
del_users(users_list) |
| 779 |
|
| 780 |
|
| 781 |
def test_search_pagedsizelimit_success(topology_st, create_user): |
| 782 |
"""Verify that search with a simple paged results control |
| 783 |
returns all entries it should without errors while |
| 784 |
valid value set to nsslapd-pagedsizelimit. |
| 785 |
|
| 786 |
:id: 88193f10-f6f0-42f5-ae9c-ff34b8f9ee8c |
| 787 |
:customerscenario: True |
| 788 |
:setup: Standalone instance, test user for binding, |
| 789 |
10 users for the search base |
| 790 |
:steps: |
| 791 |
1. Set nsslapd-pagedsizelimit: 20 |
| 792 |
2. Bind as test user |
| 793 |
3. Search through added users with a simple paged control |
| 794 |
using page_size = 10 |
| 795 |
:expectedresults: |
| 796 |
1. nsslapd-pagedsizelimit should be successfully set |
| 797 |
2. Bind should be successful |
| 798 |
3. All users should be found |
| 799 |
""" |
| 800 |
|
| 801 |
users_num = 10 |
| 802 |
page_size = 10 |
| 803 |
attr_name = 'nsslapd-pagedsizelimit' |
| 804 |
attr_value = '20' |
| 805 |
attr_value_bck = change_conf_attr(topology_st, DN_CONFIG, attr_name, attr_value) |
| 806 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 807 |
search_flt = r'(uid=test*)' |
| 808 |
searchreq_attrlist = ['dn', 'sn'] |
| 809 |
|
| 810 |
try: |
| 811 |
log.info('Set user bind') |
| 812 |
conn = create_user.bind(TEST_USER_PWD) |
| 813 |
|
| 814 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 815 |
controls = [req_ctrl] |
| 816 |
|
| 817 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, search_flt, searchreq_attrlist) |
| 818 |
|
| 819 |
log.info('%d results' % len(all_results)) |
| 820 |
assert len(all_results) == len(users_list) |
| 821 |
|
| 822 |
finally: |
| 823 |
del_users(users_list) |
| 824 |
change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-pagedsizelimit', attr_value_bck) |
| 825 |
|
| 826 |
|
| 827 |
@pytest.mark.parametrize('conf_attr,user_attr,expected_rs', |
| 828 |
(('5', '15', 'PASS'), ('15', '5', ldap.SIZELIMIT_EXCEEDED))) |
| 829 |
def test_search_nspagedsizelimit(topology_st, create_user, |
| 830 |
conf_attr, user_attr, expected_rs): |
| 831 |
"""Verify that nsPagedSizeLimit attribute overrides |
| 832 |
nsslapd-pagedsizelimit while performing search with |
| 833 |
the simple paged results control. |
| 834 |
|
| 835 |
:id: b08c6ad2-ba28-447a-9f04-5377c3661d0d |
| 836 |
:customerscenario: True |
| 837 |
:parametrized: yes |
| 838 |
:setup: Standalone instance, test user for binding, |
| 839 |
10 users for the search base |
| 840 |
:steps: |
| 841 |
1. Set nsslapd-pagedsizelimit: 5 |
| 842 |
2. Set nsPagedSizeLimit: 15 |
| 843 |
3. Bind as test user |
| 844 |
4. Search through added users with a simple paged control |
| 845 |
using page_size = 10 |
| 846 |
5. Bind as Directory Manager |
| 847 |
6. Restore all values |
| 848 |
7. Set nsslapd-pagedsizelimit: 15 |
| 849 |
8. Set nsPagedSizeLimit: 5 |
| 850 |
9. Bind as test user |
| 851 |
10. Search through added users with a simple paged control |
| 852 |
using page_size = 10 |
| 853 |
:expectedresults: |
| 854 |
1. nsslapd-pagedsizelimit should be successfully set |
| 855 |
2. nsPagedSizeLimit should be successfully set |
| 856 |
3. Bind should be successful |
| 857 |
4. No error happens, all users should be found |
| 858 |
5. Bind should be successful |
| 859 |
6. All values should be restored |
| 860 |
7. nsslapd-pagedsizelimit should be successfully set |
| 861 |
8. nsPagedSizeLimit should be successfully set |
| 862 |
9. Bind should be successful |
| 863 |
10. It should throw SIZELIMIT_EXCEEDED exception |
| 864 |
""" |
| 865 |
|
| 866 |
users_num = 10 |
| 867 |
page_size = 10 |
| 868 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 869 |
search_flt = r'(uid=test*)' |
| 870 |
searchreq_attrlist = ['dn', 'sn'] |
| 871 |
conf_attr_bck = change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-pagedsizelimit', conf_attr) |
| 872 |
user_attr_bck = change_conf_attr(topology_st, create_user.dn, 'nsPagedSizeLimit', user_attr) |
| 873 |
|
| 874 |
try: |
| 875 |
log.info('Set user bind') |
| 876 |
conn = create_user.bind(TEST_USER_PWD) |
| 877 |
|
| 878 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 879 |
controls = [req_ctrl] |
| 880 |
|
| 881 |
if expected_rs == ldap.SIZELIMIT_EXCEEDED: |
| 882 |
log.info('Expect to fail with SIZELIMIT_EXCEEDED') |
| 883 |
with pytest.raises(expected_rs): |
| 884 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, search_flt, searchreq_attrlist) |
| 885 |
elif expected_rs == 'PASS': |
| 886 |
log.info('Expect to pass') |
| 887 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, search_flt, searchreq_attrlist) |
| 888 |
log.info('%d results' % len(all_results)) |
| 889 |
assert len(all_results) == len(users_list) |
| 890 |
|
| 891 |
finally: |
| 892 |
del_users(users_list) |
| 893 |
change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-pagedsizelimit', conf_attr_bck) |
| 894 |
change_conf_attr(topology_st, create_user.dn, 'nsPagedSizeLimit', user_attr_bck) |
| 895 |
|
| 896 |
|
| 897 |
@pytest.mark.parametrize('conf_attr_values,expected_rs', |
| 898 |
((('5000', '100', '100'), ldap.ADMINLIMIT_EXCEEDED), |
| 899 |
(('5000', '120', '122'), 'PASS'))) |
| 900 |
def test_search_paged_limits(topology_st, create_user, conf_attr_values, expected_rs): |
| 901 |
"""Verify that nsslapd-idlistscanlimit and |
| 902 |
nsslapd-lookthroughlimit can limit the administrator |
| 903 |
search abilities. |
| 904 |
|
| 905 |
:id: e0f8b916-7276-4bd3-9e73-8696a4468811 |
| 906 |
:customerscenario: True |
| 907 |
:parametrized: yes |
| 908 |
:setup: Standalone instance, test user for binding, |
| 909 |
10 users for the search base |
| 910 |
:steps: |
| 911 |
1. Set nsslapd-sizelimit and nsslapd-pagedsizelimit to 5000 |
| 912 |
2. Set nsslapd-idlistscanlimit: 120 |
| 913 |
3. Set nsslapd-lookthroughlimit: 122 |
| 914 |
4. Bind as test user |
| 915 |
5. Search through added users with a simple paged control |
| 916 |
using page_size = 10 |
| 917 |
6. Bind as Directory Manager |
| 918 |
7. Set nsslapd-idlistscanlimit: 100 |
| 919 |
8. Set nsslapd-lookthroughlimit: 100 |
| 920 |
9. Bind as test user |
| 921 |
10. Search through added users with a simple paged control |
| 922 |
using page_size = 10 |
| 923 |
:expectedresults: |
| 924 |
1. nsslapd-sizelimit and nsslapd-pagedsizelimit |
| 925 |
should be successfully set |
| 926 |
2. nsslapd-idlistscanlimit should be successfully set |
| 927 |
3. nsslapd-lookthroughlimit should be successfully set |
| 928 |
4. Bind should be successful |
| 929 |
5. No error happens, all users should be found |
| 930 |
6. Bind should be successful |
| 931 |
7. nsslapd-idlistscanlimit should be successfully set |
| 932 |
8. nsslapd-lookthroughlimit should be successfully set |
| 933 |
9. Bind should be successful |
| 934 |
10. It should throw ADMINLIMIT_EXCEEDED exception |
| 935 |
""" |
| 936 |
|
| 937 |
users_num = 101 |
| 938 |
page_size = 10 |
| 939 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 940 |
search_flt = r'(uid=test*)' |
| 941 |
searchreq_attrlist = ['dn', 'sn'] |
| 942 |
size_attr_bck = change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-sizelimit', conf_attr_values[0]) |
| 943 |
pagedsize_attr_bck = change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-pagedsizelimit', conf_attr_values[0]) |
| 944 |
idlistscan_attr_bck = change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-idlistscanlimit', conf_attr_values[1]) |
| 945 |
lookthrough_attr_bck = change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-lookthroughlimit', conf_attr_values[2]) |
| 946 |
|
| 947 |
try: |
| 948 |
log.info('Set user bind') |
| 949 |
conn = create_user.bind(TEST_USER_PWD) |
| 950 |
|
| 951 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 952 |
controls = [req_ctrl] |
| 953 |
|
| 954 |
if expected_rs == ldap.ADMINLIMIT_EXCEEDED: |
| 955 |
log.info('Expect to fail with ADMINLIMIT_EXCEEDED') |
| 956 |
with pytest.raises(expected_rs): |
| 957 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, search_flt, searchreq_attrlist) |
| 958 |
elif expected_rs == 'PASS': |
| 959 |
log.info('Expect to pass') |
| 960 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, search_flt, searchreq_attrlist) |
| 961 |
log.info('%d results' % len(all_results)) |
| 962 |
assert len(all_results) == len(users_list) |
| 963 |
finally: |
| 964 |
del_users(users_list) |
| 965 |
change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-sizelimit', size_attr_bck) |
| 966 |
change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-pagedsizelimit', pagedsize_attr_bck) |
| 967 |
change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-lookthroughlimit', lookthrough_attr_bck) |
| 968 |
change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-idlistscanlimit', idlistscan_attr_bck) |
| 969 |
|
| 970 |
|
| 971 |
@pytest.mark.parametrize('conf_attr_values,expected_rs', |
| 972 |
((('1000', '100', '100'), ldap.ADMINLIMIT_EXCEEDED), |
| 973 |
(('1000', '120', '122'), 'PASS'))) |
| 974 |
def test_search_paged_user_limits(topology_st, create_user, conf_attr_values, expected_rs): |
| 975 |
"""Verify that nsPagedIDListScanLimit and nsPagedLookthroughLimit |
| 976 |
override nsslapd-idlistscanlimit and nsslapd-lookthroughlimit |
| 977 |
while performing search with the simple paged results control. |
| 978 |
|
| 979 |
:id: 69e393e9-1ab8-4f4e-b4a1-06ca63dc7b1b |
| 980 |
:customerscenario: True |
| 981 |
:parametrized: yes |
| 982 |
:setup: Standalone instance, test user for binding, |
| 983 |
10 users for the search base |
| 984 |
:steps: |
| 985 |
1. Set nsslapd-idlistscanlimit: 1000 |
| 986 |
2. Set nsslapd-lookthroughlimit: 1000 |
| 987 |
3. Set nsPagedIDListScanLimit: 120 |
| 988 |
4. Set nsPagedLookthroughLimit: 122 |
| 989 |
5. Bind as test user |
| 990 |
6. Search through added users with a simple paged control |
| 991 |
using page_size = 10 |
| 992 |
7. Bind as Directory Manager |
| 993 |
8. Set nsPagedIDListScanLimit: 100 |
| 994 |
9. Set nsPagedLookthroughLimit: 100 |
| 995 |
10. Bind as test user |
| 996 |
11. Search through added users with a simple paged control |
| 997 |
using page_size = 10 |
| 998 |
:expectedresults: |
| 999 |
1. nsslapd-idlistscanlimit should be successfully set |
| 1000 |
2. nsslapd-lookthroughlimit should be successfully set |
| 1001 |
3. nsPagedIDListScanLimit should be successfully set |
| 1002 |
4. nsPagedLookthroughLimit should be successfully set |
| 1003 |
5. Bind should be successful |
| 1004 |
6. No error happens, all users should be found |
| 1005 |
7. Bind should be successful |
| 1006 |
8. nsPagedIDListScanLimit should be successfully set |
| 1007 |
9. nsPagedLookthroughLimit should be successfully set |
| 1008 |
10. Bind should be successful |
| 1009 |
11. It should throw ADMINLIMIT_EXCEEDED exception |
| 1010 |
""" |
| 1011 |
|
| 1012 |
users_num = 101 |
| 1013 |
page_size = 10 |
| 1014 |
users_list = add_users(topology_st, users_num, DEFAULT_SUFFIX) |
| 1015 |
search_flt = r'(uid=test*)' |
| 1016 |
searchreq_attrlist = ['dn', 'sn'] |
| 1017 |
lookthrough_attr_bck = change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-lookthroughlimit', conf_attr_values[0]) |
| 1018 |
idlistscan_attr_bck = change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-idlistscanlimit', conf_attr_values[0]) |
| 1019 |
user_idlistscan_attr_bck = change_conf_attr(topology_st, create_user.dn, 'nsPagedIDListScanLimit', conf_attr_values[1]) |
| 1020 |
user_lookthrough_attr_bck = change_conf_attr(topology_st, create_user.dn, 'nsPagedLookthroughLimit', conf_attr_values[2]) |
| 1021 |
|
| 1022 |
try: |
| 1023 |
log.info('Set user bind') |
| 1024 |
conn = create_user.bind(TEST_USER_PWD) |
| 1025 |
|
| 1026 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 1027 |
controls = [req_ctrl] |
| 1028 |
|
| 1029 |
if expected_rs == ldap.ADMINLIMIT_EXCEEDED: |
| 1030 |
log.info('Expect to fail with ADMINLIMIT_EXCEEDED') |
| 1031 |
with pytest.raises(expected_rs): |
| 1032 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, search_flt, searchreq_attrlist) |
| 1033 |
elif expected_rs == 'PASS': |
| 1034 |
log.info('Expect to pass') |
| 1035 |
all_results = paged_search(conn, DEFAULT_SUFFIX, controls, search_flt, searchreq_attrlist) |
| 1036 |
log.info('%d results' % len(all_results)) |
| 1037 |
assert len(all_results) == len(users_list) |
| 1038 |
finally: |
| 1039 |
del_users(users_list) |
| 1040 |
change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-lookthroughlimit', lookthrough_attr_bck) |
| 1041 |
change_conf_attr(topology_st, 'cn=config,%s' % DN_LDBM, 'nsslapd-idlistscanlimit', idlistscan_attr_bck) |
| 1042 |
change_conf_attr(topology_st, create_user.dn, 'nsPagedIDListScanLimit', user_idlistscan_attr_bck) |
| 1043 |
change_conf_attr(topology_st, create_user.dn, 'nsPagedLookthroughLimit', user_lookthrough_attr_bck) |
| 1044 |
|
| 1045 |
|
| 1046 |
def test_ger_basic(topology_st, create_user): |
| 1047 |
"""Verify that search with a simple paged results control |
| 1048 |
and get effective rights control returns all entries |
| 1049 |
it should without errors. |
| 1050 |
|
| 1051 |
:id: 7b0bdfc7-a2f2-4c1a-bcab-f1eb8b330d45 |
| 1052 |
:customerscenario: True |
| 1053 |
:setup: Standalone instance, test user for binding, |
| 1054 |
varying number of users for the search base |
| 1055 |
:steps: |
| 1056 |
1. Search through added users with a simple paged control |
| 1057 |
and get effective rights control |
| 1058 |
:expectedresults: |
| 1059 |
1. All users should be found, every found entry should have |
| 1060 |
an 'attributeLevelRights' returned |
| 1061 |
""" |
| 1062 |
|
| 1063 |
users_list = add_users(topology_st, 20, DEFAULT_SUFFIX) |
| 1064 |
search_flt = r'(uid=test*)' |
| 1065 |
searchreq_attrlist = ['dn', 'sn'] |
| 1066 |
page_size = 4 |
| 1067 |
|
| 1068 |
try: |
| 1069 |
spr_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 1070 |
ger_ctrl = GetEffectiveRightsControl(True, ensure_bytes("dn: " + DN_DM)) |
| 1071 |
|
| 1072 |
all_results = paged_search(topology_st.standalone, DEFAULT_SUFFIX, [spr_ctrl, ger_ctrl], |
| 1073 |
search_flt, searchreq_attrlist) |
| 1074 |
|
| 1075 |
log.info('{} results'.format(len(all_results))) |
| 1076 |
assert len(all_results) == len(users_list) |
| 1077 |
log.info('Check for attributeLevelRights') |
| 1078 |
assert all(attrs['attributeLevelRights'][0] for dn, attrs in all_results) |
| 1079 |
finally: |
| 1080 |
log.info('Remove added users') |
| 1081 |
del_users(users_list) |
| 1082 |
|
| 1083 |
|
| 1084 |
def test_multi_suffix_search(topology_st, create_user, new_suffixes): |
| 1085 |
"""Verify that page result search returns empty cookie |
| 1086 |
if there is no returned entry. |
| 1087 |
|
| 1088 |
:id: 9712345b-9e38-4df6-8794-05f12c457d39 |
| 1089 |
:customerscenario: True |
| 1090 |
:setup: Standalone instance, test user for binding, |
| 1091 |
two suffixes with backends, one is inserted into another, |
| 1092 |
10 users for the search base within each suffix |
| 1093 |
:steps: |
| 1094 |
1. Bind as test user |
| 1095 |
2. Search through all 20 added users with a simple paged control |
| 1096 |
using page_size = 4 |
| 1097 |
3. Wait some time for the logs to be updated |
| 1098 |
4. Check access log |
| 1099 |
:expectedresults: |
| 1100 |
1. Bind should be successful |
| 1101 |
2. All users should be found |
| 1102 |
3. Some time should pass |
| 1103 |
4. The access log should contain the pr_cookie for each page request |
| 1104 |
and it should be equal 0, except the last one should be equal -1 |
| 1105 |
""" |
| 1106 |
|
| 1107 |
search_flt = r'(uid=test*)' |
| 1108 |
searchreq_attrlist = ['dn', 'sn'] |
| 1109 |
page_size = 4 |
| 1110 |
users_num = 20 |
| 1111 |
|
| 1112 |
log.info('Clear the access log') |
| 1113 |
topology_st.standalone.deleteAccessLogs() |
| 1114 |
|
| 1115 |
users_list_1 = add_users(topology_st, 10, NEW_SUFFIX_1) |
| 1116 |
users_list_2 = add_users(topology_st, 10, NEW_SUFFIX_2) |
| 1117 |
|
| 1118 |
try: |
| 1119 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 1120 |
|
| 1121 |
all_results = paged_search(topology_st.standalone, NEW_SUFFIX_1, [req_ctrl], search_flt, searchreq_attrlist) |
| 1122 |
|
| 1123 |
log.info('{} results'.format(len(all_results))) |
| 1124 |
assert len(all_results) == users_num |
| 1125 |
|
| 1126 |
log.info('Restart the server to flush the logs') |
| 1127 |
topology_st.standalone.restart(timeout=10) |
| 1128 |
|
| 1129 |
access_log_lines = topology_st.standalone.ds_access_log.match('.*pr_cookie=.*') |
| 1130 |
# Sort access_log_lines by op number to mitigate race condition effects. |
| 1131 |
access_log_lines.sort(key=lambda x: int(re.search(r"op=(\d+) RESULT", x).group(1))) |
| 1132 |
pr_cookie_list = ([line.rsplit('=', 1)[-1] for line in access_log_lines]) |
| 1133 |
pr_cookie_list = [int(pr_cookie) for pr_cookie in pr_cookie_list] |
| 1134 |
log.info('Assert that last pr_cookie == -1 and others pr_cookie == 0') |
| 1135 |
pr_cookie_zeros = list(pr_cookie == 0 for pr_cookie in pr_cookie_list[0:-1]) |
| 1136 |
assert all(pr_cookie_zeros) |
| 1137 |
assert pr_cookie_list[-1] == -1 |
| 1138 |
finally: |
| 1139 |
log.info('Remove added users') |
| 1140 |
del_users(users_list_1) |
| 1141 |
del_users(users_list_2) |
| 1142 |
|
| 1143 |
|
| 1144 |
@pytest.mark.parametrize('conf_attr_value', (None, '-1', '1000')) |
| 1145 |
def test_maxsimplepaged_per_conn_success(topology_st, create_user, conf_attr_value): |
| 1146 |
"""Verify that nsslapd-maxsimplepaged-per-conn acts according design |
| 1147 |
|
| 1148 |
:id: 192e2f25-04ee-4ff9-9340-d875dcbe8011 |
| 1149 |
:customerscenario: True |
| 1150 |
:parametrized: yes |
| 1151 |
:setup: Standalone instance, test user for binding, |
| 1152 |
20 users for the search base |
| 1153 |
:steps: |
| 1154 |
1. Set nsslapd-maxsimplepaged-per-conn in cn=config |
| 1155 |
to the next values: no value, -1, some positive |
| 1156 |
2. Search through the added users with a simple paged control |
| 1157 |
using page size = 4 |
| 1158 |
:expectedresults: |
| 1159 |
1. nsslapd-maxsimplepaged-per-conn should be successfully set |
| 1160 |
2. If no value or value = -1 - all users should be found, |
| 1161 |
default behaviour; If the value is positive, |
| 1162 |
the value is the max simple paged results requests per connection. |
| 1163 |
""" |
| 1164 |
|
| 1165 |
users_list = add_users(topology_st, 20, DEFAULT_SUFFIX) |
| 1166 |
search_flt = r'(uid=test*)' |
| 1167 |
searchreq_attrlist = ['dn', 'sn'] |
| 1168 |
page_size = 4 |
| 1169 |
if conf_attr_value: |
| 1170 |
max_per_con_bck = change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-maxsimplepaged-per-conn', conf_attr_value) |
| 1171 |
|
| 1172 |
try: |
| 1173 |
log.info('Set user bind') |
| 1174 |
conn = create_user.bind(TEST_USER_PWD) |
| 1175 |
|
| 1176 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 1177 |
|
| 1178 |
all_results = paged_search(conn, DEFAULT_SUFFIX, [req_ctrl], search_flt, searchreq_attrlist) |
| 1179 |
|
| 1180 |
log.info('{} results'.format(len(all_results))) |
| 1181 |
assert len(all_results) == len(users_list) |
| 1182 |
finally: |
| 1183 |
log.info('Remove added users') |
| 1184 |
del_users(users_list) |
| 1185 |
if conf_attr_value: |
| 1186 |
change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-maxsimplepaged-per-conn', max_per_con_bck) |
| 1187 |
|
| 1188 |
|
| 1189 |
@pytest.mark.parametrize('conf_attr_value', ('0', '1')) |
| 1190 |
def test_maxsimplepaged_per_conn_failure(topology_st, create_user, conf_attr_value): |
| 1191 |
"""Verify that nsslapd-maxsimplepaged-per-conn acts according design |
| 1192 |
|
| 1193 |
:id: eb609e63-2829-4331-8439-a35f99694efa |
| 1194 |
:customerscenario: True |
| 1195 |
:parametrized: yes |
| 1196 |
:setup: Standalone instance, test user for binding, |
| 1197 |
20 users for the search base |
| 1198 |
:steps: |
| 1199 |
1. Set nsslapd-maxsimplepaged-per-conn = 0 in cn=config |
| 1200 |
2. Search through the added users with a simple paged control |
| 1201 |
using page size = 4 |
| 1202 |
3. Set nsslapd-maxsimplepaged-per-conn = 1 in cn=config |
| 1203 |
4. Search through the added users with a simple paged control |
| 1204 |
using page size = 4 two times, but don't close the connections |
| 1205 |
:expectedresults: |
| 1206 |
1. nsslapd-maxsimplepaged-per-conn should be successfully set |
| 1207 |
2. UNWILLING_TO_PERFORM should be thrown |
| 1208 |
3. Bind should be successful |
| 1209 |
4. UNWILLING_TO_PERFORM should be thrown |
| 1210 |
""" |
| 1211 |
|
| 1212 |
users_list = add_users(topology_st, 20, DEFAULT_SUFFIX) |
| 1213 |
search_flt = r'(uid=test*)' |
| 1214 |
searchreq_attrlist = ['dn', 'sn'] |
| 1215 |
page_size = 4 |
| 1216 |
max_per_con_bck = change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-maxsimplepaged-per-conn', conf_attr_value) |
| 1217 |
|
| 1218 |
try: |
| 1219 |
log.info('Set user bind') |
| 1220 |
conn = create_user.bind(TEST_USER_PWD) |
| 1221 |
|
| 1222 |
log.info('Create simple paged results control instance') |
| 1223 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 1224 |
|
| 1225 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 1226 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 1227 |
search_flt, searchreq_attrlist, serverctrls=[req_ctrl]) |
| 1228 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 1229 |
|
| 1230 |
# If nsslapd-maxsimplepaged-per-conn = 1, |
| 1231 |
# it should pass this point, but failed on the next search |
| 1232 |
assert conf_attr_value == '1' |
| 1233 |
msgid = conn.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, |
| 1234 |
search_flt, searchreq_attrlist, serverctrls=[req_ctrl]) |
| 1235 |
rtype, rdata, rmsgid, rctrls = conn.result3(msgid) |
| 1236 |
finally: |
| 1237 |
log.info('Remove added users') |
| 1238 |
del_users(users_list) |
| 1239 |
change_conf_attr(topology_st, DN_CONFIG, 'nsslapd-maxsimplepaged-per-conn', max_per_con_bck) |
| 1240 |
|
| 1241 |
|
| 1242 |
def test_search_stress_abandon(create_40k_users, create_user): |
| 1243 |
"""Verify that search with a simple paged results control |
| 1244 |
returns all entries it should without errors. |
| 1245 |
|
| 1246 |
:id: e154b24a-83d6-11ee-90d1-482ae39447e5 |
| 1247 |
:customerscenario: True |
| 1248 |
:feature: Simple paged results |
| 1249 |
:setup: Standalone instance, test user for binding, |
| 1250 |
40K users in a second backend |
| 1251 |
:steps: |
| 1252 |
1. Bind as test user |
| 1253 |
2. Loops a number of times doing: |
| 1254 |
- search through added users with a simple paged control |
| 1255 |
- randomly abandoning the search after a few ms. |
| 1256 |
:expectedresults: |
| 1257 |
1. Bind should be successful |
| 1258 |
2. The loop should complete successfully. |
| 1259 |
""" |
| 1260 |
|
| 1261 |
abandon_rate = 10 |
| 1262 |
page_size = 500 |
| 1263 |
nbloops = 1000 |
| 1264 |
search_flt = r'(uid=*)' |
| 1265 |
searchreq_attrlist = ['dn', 'sn'] |
| 1266 |
log.info('Set user bind %s ' % create_user) |
| 1267 |
conn = create_user.bind(TEST_USER_PWD) |
| 1268 |
for idx in range(nbloops): |
| 1269 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 1270 |
# If the issue #5984 is not fixed the server crashs and the paged search fails with ldap.SERVER_DOWN exception |
| 1271 |
paged_search(conn, create_40k_users.suffix, [req_ctrl], search_flt, searchreq_attrlist, abandon_rate=abandon_rate) |
| 1272 |
|
| 1273 |
|
| 1274 |
def test_search_referral(topology_st): |
| 1275 |
"""Test a paged search on a referred suffix doesnt crash the server. |
| 1276 |
|
| 1277 |
:id: c788bdbf-965b-4f12-ac24-d4d695e2cce2 |
| 1278 |
|
| 1279 |
:setup: Standalone instance |
| 1280 |
|
| 1281 |
:steps: |
| 1282 |
1. Configure a default referral. |
| 1283 |
2. Create a paged result search control. |
| 1284 |
3. Paged result search on referral suffix (doesnt exist on the instance, triggering a referral). |
| 1285 |
4. Check the server is still running. |
| 1286 |
5. Remove referral. |
| 1287 |
|
| 1288 |
:expectedresults: |
| 1289 |
1. Referral sucessfully set. |
| 1290 |
2. Control created. |
| 1291 |
3. Search returns ldap.REFERRAL (10). |
| 1292 |
4. Server still running. |
| 1293 |
5. Referral removed. |
| 1294 |
""" |
| 1295 |
|
| 1296 |
page_size = 5 |
| 1297 |
SEARCH_SUFFIX = "dc=referme,dc=com" |
| 1298 |
REFERRAL = "ldap://localhost.localdomain:389/o%3dnetscaperoot" |
| 1299 |
|
| 1300 |
log.info('Configuring referral') |
| 1301 |
topology_st.standalone.config.set('nsslapd-referral', REFERRAL) |
| 1302 |
referral = topology_st.standalone.config.get_attr_val_utf8('nsslapd-referral') |
| 1303 |
assert (referral == REFERRAL) |
| 1304 |
|
| 1305 |
log.info('Create paged result search control') |
| 1306 |
req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='') |
| 1307 |
|
| 1308 |
log.info('Perform a paged result search on referred suffix, no chase') |
| 1309 |
with pytest.raises(ldap.REFERRAL): |
| 1310 |
topology_st.standalone.search_ext_s(SEARCH_SUFFIX, ldap.SCOPE_SUBTREE, serverctrls=[req_ctrl]) |
| 1311 |
|
| 1312 |
log.info('Confirm instance is still running') |
| 1313 |
assert (topology_st.standalone.status()) |
| 1314 |
|
| 1315 |
log.info('Remove referral') |
| 1316 |
topology_st.standalone.config.remove_all('nsslapd-referral') |
| 1317 |
referral = topology_st.standalone.config.get_attr_val_utf8('nsslapd-referral') |
| 1318 |
assert (referral == None) |
| 1319 |
|
| 1320 |
if __name__ == '__main__': |
| 1321 |
# Run isolated |
| 1322 |
# -s for DEBUG mode |
| 1323 |
CURRENT_FILE = os.path.realpath(__file__) |
| 1324 |
pytest.main("-s %s" % CURRENT_FILE) |