Debian Codegraph @

dirsrvtests/tests/suites/roles/basic_test.py

raw
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
10 """
11 Importing necessary Modules.
12 """
13
14 import logging
15 import time
16 import ldap
17 import os
18 import pytest
19
20 from lib389._constants import ErrorLog, PW_DM, DEFAULT_SUFFIX, DEFAULT_BENAME
21 from lib389.idm.user import UserAccount, UserAccounts
22 from lib389.idm.organization import Organization
23 from lib389.idm.organizationalunit import OrganizationalUnit
24 from test389.topologies import topology_st as topo
25 from lib389.idm.role import FilteredRoles, ManagedRoles, NestedRoles
26 from lib389.idm.domain import Domain
27 from lib389.dbgen import dbgen_users
28 from lib389.tasks import ImportTask
29 from lib389.utils import get_default_db_lib
30 from lib389.rewriters import *
31 from lib389._mapped_object import DSLdapObject
32 from lib389.backend import Backends
33
34 logging.getLogger(__name__).setLevel(logging.INFO)
35 log = logging.getLogger(__name__)
36
37 pytestmark = pytest.mark.tier1
38
39 DNBASE = "o=acivattr,{}".format(DEFAULT_SUFFIX)
40 ENG_USER = "cn=enguser1,ou=eng,{}".format(DNBASE)
41 SALES_UESER = "cn=salesuser1,ou=sales,{}".format(DNBASE)
42 ENG_MANAGER = "cn=engmanager1,ou=eng,{}".format(DNBASE)
43 SALES_MANAGER = "cn=salesmanager1,ou=sales,{}".format(DNBASE)
44 SALES_OU = "ou=sales,{}".format(DNBASE)
45 ENG_OU = "ou=eng,{}".format(DNBASE)
46 FILTERROLESALESROLE = "cn=FILTERROLESALESROLE,{}".format(DNBASE)
47 FILTERROLEENGROLE = "cn=FILTERROLEENGROLE,{}".format(DNBASE)
48
49
50 def test_filterrole(topo, request):
51 """Test Filter Role
52
53 :id: 8ada4064-786b-11e8-8634-8c16451d917b
54 :setup: server
55 :steps:
56 1. Add test entry
57 2. Add ACI
58 3. Search nsconsole role
59 :expectedresults:
60 1. Entry should be added
61 2. Operation should succeed
62 3. Operation should succeed
63 """
64 Organization(topo.standalone).create(properties={"o": "acivattr"}, basedn=DEFAULT_SUFFIX)
65 properties = {
66 'ou': 'eng',
67 }
68
69 ou_ou = OrganizationalUnit(topo.standalone, "ou=eng,o=acivattr,{}".format(DEFAULT_SUFFIX))
70 ou_ou.create(properties=properties)
71 properties = {'ou': 'sales'}
72 ou_ou = OrganizationalUnit(topo.standalone, "ou=sales,o=acivattr,{}".format(DEFAULT_SUFFIX))
73 ou_ou.create(properties=properties)
74
75 roles = FilteredRoles(topo.standalone, DNBASE)
76 roles.create(properties={'cn': 'FILTERROLEENGROLE', 'nsRoleFilter': 'cn=eng*'})
77 roles.create(properties={'cn': 'FILTERROLESALESROLE', 'nsRoleFilter': 'cn=sales*'})
78
79 properties = {
80 'uid': 'salesuser1',
81 'cn': 'salesuser1',
82 'sn': 'user',
83 'uidNumber': '1000',
84 'gidNumber': '2000',
85 'homeDirectory': '/home/' + 'salesuser1',
86 'userPassword': PW_DM
87 }
88 user = UserAccount(topo.standalone,
89 'cn=salesuser1,ou=sales,o=acivattr,{}'.format(DEFAULT_SUFFIX))
90 user.create(properties=properties)
91
92 properties = {
93 'uid': 'salesmanager1',
94 'cn': 'salesmanager1',
95 'sn': 'user',
96 'uidNumber': '1000',
97 'gidNumber': '2000',
98 'homeDirectory': '/home/' + 'salesmanager1',
99 'userPassword': PW_DM,
100 }
101 user = UserAccount(topo.standalone,
102 'cn=salesmanager1,ou=sales,o=acivattr,{}'.format(DEFAULT_SUFFIX))
103 user.create(properties=properties)
104
105 properties = {
106 'uid': 'enguser1',
107 'cn': 'enguser1',
108 'sn': 'user',
109 'uidNumber': '1000',
110 'gidNumber': '2000',
111 'homeDirectory': '/home/' + 'enguser1',
112 'userPassword': PW_DM
113 }
114 user = UserAccount(topo.standalone,
115 'cn=enguser1,ou=eng,o=acivattr,{}'.format(DEFAULT_SUFFIX))
116 user.create(properties=properties)
117
118 properties = {
119 'uid': 'engmanager1',
120 'cn': 'engmanager1',
121 'sn': 'user',
122 'uidNumber': '1000',
123 'gidNumber': '2000',
124 'homeDirectory': '/home/' + 'engmanager1',
125 'userPassword': PW_DM
126 }
127 user = UserAccount(topo.standalone,
128 'cn=engmanager1,ou=eng,o=acivattr,{}'.format(DEFAULT_SUFFIX))
129 user.create(properties=properties)
130
131 # user with cn=sales* will automatically memeber of nsfilterrole
132 # cn=filterrolesalesrole,o=acivattr,dc=example,dc=com
133 assert UserAccount(topo.standalone,
134 'cn=salesuser1,ou=sales,o=acivattr,dc=example,dc=com').\
135 get_attr_val_utf8('nsrole') == 'cn=filterrolesalesrole,o=acivattr,dc=example,dc=com'
136 # same goes to SALES_MANAGER
137 assert UserAccount(topo.standalone, SALES_MANAGER).get_attr_val_utf8(
138 'nsrole') == 'cn=filterrolesalesrole,o=acivattr,dc=example,dc=com'
139 # user with cn=eng* will automatically memeber of nsfilterrole
140 # cn=filterroleengrole,o=acivattr,dc=example,dc=com
141 assert UserAccount(topo.standalone, 'cn=enguser1,ou=eng,o=acivattr,dc=example,dc=com').\
142 get_attr_val_utf8('nsrole') == 'cn=filterroleengrole,o=acivattr,dc=example,dc=com'
143 # same goes to ENG_MANAGER
144 assert UserAccount(topo.standalone, ENG_MANAGER).get_attr_val_utf8(
145 'nsrole') == 'cn=filterroleengrole,o=acivattr,dc=example,dc=com'
146 for dn_dn in [ENG_USER, SALES_UESER, ENG_MANAGER, SALES_MANAGER,
147 FILTERROLESALESROLE, FILTERROLEENGROLE, ENG_OU,
148 SALES_OU, DNBASE]:
149 UserAccount(topo.standalone, dn_dn).delete()
150
151 def fin():
152 topo.standalone.restart()
153 try:
154 filtered_roles = FilteredRoles(topo.standalone, DEFAULT_SUFFIX)
155 for i in filtered_roles.list():
156 i.delete()
157 except:
158 pass
159 topo.standalone.config.set('nsslapd-ignore-virtual-attrs', 'on')
160
161 request.addfinalizer(fin)
162
163
164 def test_managedrole(topo, request):
165 """Test Managed Role
166
167 :id: d52a9c00-3bf6-11e9-9b7b-8c16451d917b
168 :setup: server
169 :steps:
170 1. Add test entry
171 2. Add ACI
172 3. Search managed role entries
173 :expectedresults:
174 1. Entry should be added
175 2. Operation should succeed
176 3. Operation should succeed
177 """
178 # Create Managed role entry
179 roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
180 role = roles.create(properties={"cn": 'ROLE1'})
181
182 # Create user and Assign the role to the entry
183 uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=None)
184 uas.create(properties={
185 'uid': 'Fail',
186 'cn': 'Fail',
187 'sn': 'user',
188 'uidNumber': '1000',
189 'gidNumber': '2000',
190 'homeDirectory': '/home/' + 'Fail',
191 'nsRoleDN': role.dn,
192 'userPassword': PW_DM
193 })
194
195 # Create user and do not Assign any role to the entry
196 uas.create(
197 properties={
198 'uid': 'Success',
199 'cn': 'Success',
200 'sn': 'user',
201 'uidNumber': '1000',
202 'gidNumber': '2000',
203 'homeDirectory': '/home/' + 'Success',
204 'userPassword': PW_DM
205 })
206
207 # Assert that Manage role entry is created and its searchable
208 assert ManagedRoles(topo.standalone, DEFAULT_SUFFIX).list()[0].dn \
209 == 'cn=ROLE1,dc=example,dc=com'
210
211 # Set an aci that will deny ROLE1 manage role
212 Domain(topo.standalone, DEFAULT_SUFFIX).\
213 add('aci', '(targetattr="*")(version 3.0; aci "role aci";'
214 ' deny(all) roledn="ldap:///{}";)'.format(role.dn),)
215 # Add self user modification and anonymous aci
216 ANON_ACI = "(targetattr=\"*\")(version 3.0; acl \"Anonymous Read access\"; allow (read,search,compare) userdn = \"ldap:///anyone\";)"
217 suffix = Domain(topo.standalone, DEFAULT_SUFFIX)
218 suffix.add('aci', ANON_ACI)
219
220 # Crate a connection with cn=Fail which is member of ROLE1
221 conn = UserAccount(topo.standalone, "uid=Fail,{}".format(DEFAULT_SUFFIX)).bind(PW_DM)
222 # Access denied to ROLE1 members
223 assert not ManagedRoles(conn, DEFAULT_SUFFIX).list()
224
225 # Now create a connection with cn=Success which is not a member of ROLE1
226 conn = UserAccount(topo.standalone, "uid=Success,{}".format(DEFAULT_SUFFIX)).bind(PW_DM)
227 # Access allowed here
228 assert ManagedRoles(conn, DEFAULT_SUFFIX).list()
229
230 for i in uas.list():
231 i.delete()
232
233 for i in roles.list():
234 i.delete()
235
236 def fin():
237 topo.standalone.restart()
238 try:
239 role = ManagedRoles(topo.standalone, DEFAULT_SUFFIX).get('ROLE1')
240 role.delete()
241 except:
242 pass
243 topo.standalone.config.set('nsslapd-ignore-virtual-attrs', 'on')
244
245 request.addfinalizer(fin)
246
247 @pytest.fixture(scope="function")
248 def _final(request, topo):
249 """
250 Removes and Restores ACIs after the test.
251 """
252 aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
253
254 def finofaci():
255 """
256 Removes and Restores ACIs and other users after the test.
257 And restore nsslapd-ignore-virtual-attrs to default
258 """
259 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
260 domain.remove_all('aci')
261
262 managed_roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
263 nested_roles = NestedRoles(topo.standalone, DEFAULT_SUFFIX)
264 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
265
266 for i in managed_roles.list() + nested_roles.list() + users.list():
267 i.delete()
268
269 for i in aci_list:
270 domain.add("aci", i)
271
272 topo.standalone.config.set('nsslapd-ignore-virtual-attrs', 'on')
273
274 request.addfinalizer(finofaci)
275
276
277 def test_nestedrole(topo, _final):
278 """Test Nested Role
279
280 :id: 867b40c0-7fcf-4332-afc7-bd01025b77f2
281 :setup: Standalone server
282 :steps:
283 1. Add test entry
284 2. Add ACI
285 3. Search managed role entries
286 :expectedresults:
287 1. Entry should be added
288 2. Operation should succeed
289 3. Operation should succeed
290 """
291 # Create Managed role entry
292 managed_roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
293 managed_role1 = managed_roles.create(properties={"cn": 'managed_role1'})
294 managed_role2 = managed_roles.create(properties={"cn": 'managed_role2'})
295
296 # Create nested role entry
297 nested_roles = NestedRoles(topo.standalone, DEFAULT_SUFFIX)
298 nested_role = nested_roles.create(properties={"cn": 'nested_role',
299 "nsroledn": [managed_role1.dn, managed_role2.dn]})
300
301 # Create user and assign managed role to it
302 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
303 user1 = users.create_test_user(uid=1, gid=1)
304 user1.set('nsroledn', managed_role1.dn)
305 user1.set('userPassword', PW_DM)
306
307 # Create another user and assign managed role to it
308 user2 = users.create_test_user(uid=2, gid=2)
309 user2.set('nsroledn', managed_role2.dn)
310 user2.set('userPassword', PW_DM)
311
312 # Create another user and do not assign any role to it
313 user3 = users.create_test_user(uid=3, gid=3)
314 user3.set('userPassword', PW_DM)
315
316 # Create a ACI with deny access to nested role entry
317 Domain(topo.standalone, DEFAULT_SUFFIX).\
318 add('aci', f'(targetattr="*")(version 3.0; aci '
319 f'"role aci"; deny(all) roledn="ldap:///{nested_role.dn}";)')
320
321 # Create connection with 'uid=test_user_1,ou=People,dc=example,dc=com' member of managed_role1
322 # and search while bound as the user
323 conn = users.get('test_user_1').bind(PW_DM)
324 assert not UserAccounts(conn, DEFAULT_SUFFIX).list()
325
326 # Create connection with 'uid=test_user_2,ou=People,dc=example,dc=com' member of managed_role2
327 # and search while bound as the user
328 conn = users.get('test_user_2').bind(PW_DM)
329 assert not UserAccounts(conn, DEFAULT_SUFFIX).list()
330
331 # Create connection with 'uid=test_user_3,ou=People,dc=example,dc=com' and
332 # search while bound as the user
333 conn = users.get('test_user_3').bind(PW_DM)
334 assert UserAccounts(conn, DEFAULT_SUFFIX).list()
335
336 def test_vattr_on_filtered_role(topo, request):
337 """Test nsslapd-ignore-virtual-attrs configuration attribute
338 The attribute is ON by default. If a filtered role is
339 added it is moved to OFF
340
341 :id: 88b3ad3c-f39a-4eb7-a8c9-07c685f11908
342 :customerscenario: True
343 :setup: Standalone instance
344 :steps:
345 1. Check the attribute nsslapd-ignore-virtual-attrs is present in cn=config
346 2. Check the default value of attribute nsslapd-ignore-virtual-attrs should be ON
347 3. Create a filtered role
348 4. Check the value of nsslapd-ignore-virtual-attrs should be OFF
349 5. Check a message "roles_cache_trigger_update_role - Because of virtual attribute.." in error logs
350 6. Check after deleting role definition value of attribute nsslapd-ignore-virtual-attrs is set back to ON
351 :expectedresults:
352 1. This should be successful
353 2. This should be successful
354 3. This should be successful
355 4. This should be successful
356 5. This should be successful
357 6. This should be successful
358 """
359
360 log.info("Check the attribute nsslapd-ignore-virtual-attrs is present in cn=config")
361 assert topo.standalone.config.present('nsslapd-ignore-virtual-attrs')
362
363 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs should be ON")
364 assert topo.standalone.config.get_attr_val_utf8('nsslapd-ignore-virtual-attrs') == "on"
365
366 log.info("Create a filtered role")
367 try:
368 Organization(topo.standalone).create(properties={"o": "acivattr"}, basedn=DEFAULT_SUFFIX)
369 except:
370 pass
371 roles = FilteredRoles(topo.standalone, DNBASE)
372 roles.create(properties={'cn': 'FILTERROLEENGROLE', 'nsRoleFilter': 'cn=eng*'})
373
374 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs should be OFF")
375 assert topo.standalone.config.present('nsslapd-ignore-virtual-attrs', 'off')
376
377 topo.standalone.stop()
378 assert topo.standalone.searchErrorsLog("roles_cache_trigger_update_role - Because of virtual attribute definition \(role\), nsslapd-ignore-virtual-attrs was set to \'off\'")
379
380 def fin():
381 topo.standalone.restart()
382 try:
383 filtered_roles = FilteredRoles(topo.standalone, DEFAULT_SUFFIX)
384 for i in filtered_roles.list():
385 i.delete()
386 except:
387 pass
388 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs is back to ON")
389 topo.standalone.restart()
390 assert topo.standalone.config.get_attr_val_utf8('nsslapd-ignore-virtual-attrs') == "on"
391
392 request.addfinalizer(fin)
393
394 def test_vattr_on_filtered_role_restart(topo, request):
395 """Test nsslapd-ignore-virtual-attrs configuration attribute
396 If it exists a filtered role definition at restart then
397 nsslapd-ignore-virtual-attrs should be set to 'off'
398
399 :id: 972183f7-d18f-40e0-94ab-580e7b7d78d0
400 :customerscenario: True
401 :setup: Standalone instance
402 :steps:
403 1. Check the attribute nsslapd-ignore-virtual-attrs is present in cn=config
404 2. Check the default value of attribute nsslapd-ignore-virtual-attrs should be ON
405 3. Create a filtered role
406 4. Check the value of nsslapd-ignore-virtual-attrs should be OFF
407 5. restart the instance
408 6. Check the presence of virtual attribute is detected
409 7. Check the value of nsslapd-ignore-virtual-attrs should be OFF
410 :expectedresults:
411 1. This should be successful
412 2. This should be successful
413 3. This should be successful
414 4. This should be successful
415 5. This should be successful
416 6. This should be successful
417 7. This should be successful
418 """
419
420 log.info("Check the attribute nsslapd-ignore-virtual-attrs is present in cn=config")
421 assert topo.standalone.config.present('nsslapd-ignore-virtual-attrs')
422
423 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs should be ON")
424 assert topo.standalone.config.get_attr_val_utf8('nsslapd-ignore-virtual-attrs') == "on"
425
426 log.info("Create a filtered role")
427 try:
428 Organization(topo.standalone).create(properties={"o": "acivattr"}, basedn=DEFAULT_SUFFIX)
429 except:
430 pass
431 roles = FilteredRoles(topo.standalone, DNBASE)
432 roles.create(properties={'cn': 'FILTERROLEENGROLE', 'nsRoleFilter': 'cn=eng*'})
433
434 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs should be OFF")
435 assert topo.standalone.config.present('nsslapd-ignore-virtual-attrs', 'off')
436
437 log.info("Check the virtual attribute definition is found (after a required delay)")
438 topo.standalone.restart()
439 time.sleep(5)
440 assert topo.standalone.searchErrorsLog("Found a role/cos definition in")
441 assert topo.standalone.searchErrorsLog("roles_cache_trigger_update_role - Because of virtual attribute definition \(role\), nsslapd-ignore-virtual-attrs was set to \'off\'")
442
443 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs should be OFF")
444 assert topo.standalone.config.present('nsslapd-ignore-virtual-attrs', 'off')
445
446 def fin():
447 topo.standalone.restart()
448 try:
449 filtered_roles = FilteredRoles(topo.standalone, DEFAULT_SUFFIX)
450 for i in filtered_roles.list():
451 i.delete()
452 except:
453 pass
454 topo.standalone.config.set('nsslapd-ignore-virtual-attrs', 'on')
455
456 request.addfinalizer(fin)
457
458
459 def test_vattr_on_managed_role(topo, request):
460 """Test nsslapd-ignore-virtual-attrs configuration attribute
461 The attribute is ON by default. If a managed role is
462 added it is moved to OFF
463
464 :id: 664b722d-c1ea-41e4-8f6c-f9c87a212346
465 :customerscenario: True
466 :setup: Standalone instance
467 :steps:
468 1. Check the attribute nsslapd-ignore-virtual-attrs is present in cn=config
469 2. Check the default value of attribute nsslapd-ignore-virtual-attrs should be ON
470 3. Create a managed role
471 4. Check the value of nsslapd-ignore-virtual-attrs should be OFF
472 5. Check a message "roles_cache_trigger_update_role - Because of virtual attribute.." in error logs
473 6. Check after deleting role definition value of attribute nsslapd-ignore-virtual-attrs is set back to ON
474 :expectedresults:
475 1. This should be successful
476 2. This should be successful
477 3. This should be successful
478 4. This should be successful
479 5. This should be successful
480 6. This should be successful
481 """
482
483 log.info("Check the attribute nsslapd-ignore-virtual-attrs is present in cn=config")
484 assert topo.standalone.config.present('nsslapd-ignore-virtual-attrs')
485
486 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs should be ON")
487 assert topo.standalone.config.get_attr_val_utf8('nsslapd-ignore-virtual-attrs') == "on"
488
489 log.info("Create a managed role")
490 roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
491 role = roles.create(properties={"cn": 'ROLE1'})
492
493 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs should be OFF")
494 assert topo.standalone.config.present('nsslapd-ignore-virtual-attrs', 'off')
495
496 topo.standalone.stop()
497 assert topo.standalone.searchErrorsLog("roles_cache_trigger_update_role - Because of virtual attribute definition \(role\), nsslapd-ignore-virtual-attrs was set to \'off\'")
498
499 def fin():
500 topo.standalone.restart()
501 try:
502 filtered_roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
503 for i in filtered_roles.list():
504 i.delete()
505 except:
506 pass
507 log.info("Check the default value of attribute nsslapd-ignore-virtual-attrs is back to ON")
508 topo.standalone.restart()
509 assert topo.standalone.config.get_attr_val_utf8('nsslapd-ignore-virtual-attrs') == "on"
510
511 request.addfinalizer(fin)
512
513 def test_rewriter_with_invalid_filter(topo, request):
514 """Test that server does not crash when having
515 invalid filter in filtered role
516
517 :id: 5013b0b2-0af6-11f0-8684-482ae39447e5
518 :setup: standalone server
519 :steps:
520 1. Setup filtered role with good filter
521 2. Setup nsrole rewriter
522 3. Restart the server
523 4. Search for entries
524 5. Setup filtered role with bad filter
525 6. Search for entries
526 :expectedresults:
527 1. Operation should succeed
528 2. Operation should succeed
529 3. Operation should succeed
530 4. Operation should succeed
531 5. Operation should succeed
532 6. Operation should succeed
533 """
534 inst = topo.standalone
535 entries = []
536
537 def fin():
538 inst.start()
539 for entry in entries:
540 entry.delete()
541 request.addfinalizer(fin)
542
543 # Setup filtered role
544 roles = FilteredRoles(inst, f'ou=people,{DEFAULT_SUFFIX}')
545 filter_ko = '(&((objectClass=top)(objectClass=nsPerson))'
546 filter_ok = '(&(objectClass=top)(objectClass=nsPerson))'
547 role_properties = {
548 'cn': 'TestFilteredRole',
549 'nsRoleFilter': filter_ok,
550 'description': 'Test good filter',
551 }
552 role = roles.create(properties=role_properties)
553 entries.append(role)
554
555 # Setup nsrole rewriter
556 rewriters = Rewriters(inst)
557 rewriter_properties = {
558 "cn": "nsrole",
559 "nsslapd-libpath": 'libroles-plugin',
560 "nsslapd-filterrewriter": 'role_nsRole_filter_rewriter',
561 }
562 rewriter = rewriters.ensure_state(properties=rewriter_properties)
563 entries.append(rewriter)
564
565 # Restart thge instance
566 inst.restart()
567
568 # Search for entries
569 entries = inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(nsrole=%s)" % role.dn)
570
571 # Set bad filter
572 role_properties = {
573 'cn': 'TestFilteredRole',
574 'nsRoleFilter': filter_ko,
575 'description': 'Test bad filter',
576 }
577 role.ensure_state(properties=role_properties)
578
579 # Search for entries
580 entries = inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(nsrole=%s)" % role.dn)
581
582
583 def test_managed_and_filtered_role_rewrite(topo, request):
584 """Test that filter components containing 'nsrole=xxx'
585 are reworked if xxx is either a filtered role or a managed
586 role.
587
588 :id: e30ff5ed-4f8b-48db-bb88-66f150fca31f
589 :setup: server
590 :steps:
591 1. Setup nsrole rewriter
592 2. Add a 'nsroleDN' indexes for managed roles
593 3. Create an 90K ldif files
594 This is large so that unindex search will last long
595 4. import/restart the instance
596 5. Create a managed role and add 4 entries in that role
597 6. Check that a search 'nsrole=managed_role' is fast
598 7. Create a filtered role that use an indexed attribute (givenName)
599 8. Check that a search 'nsrole=filtered_role' is fast
600 :expectedresults:
601 1. Operation should succeed
602 2. Operation should succeed
603 3. Operation should succeed
604 4. Operation should succeed
605 5. Operation should succeed
606 6. Operation should succeed
607 7. Operation should succeed
608 8. Operation should succeed
609 """
610 # Setup nsrole rewriter
611 rewriters = Rewriters(topo.standalone)
612 rewriter = rewriters.ensure_state(properties={"cn": "nsrole", "nsslapd-libpath": 'libroles-plugin'})
613 try:
614 rewriter.add('nsslapd-filterrewriter', "role_nsRole_filter_rewriter")
615 except:
616 pass
617
618 # Create an index for nsRoleDN that is used by managed role
619 attrname = 'nsRoleDN'
620 backends = Backends(topo.standalone)
621 backend = backends.get(DEFAULT_BENAME)
622 indexes = backend.get_indexes()
623 try:
624 index = indexes.create(properties={
625 'cn': attrname,
626 'nsSystemIndex': 'false',
627 'nsIndexType': ['eq', 'pres']
628 })
629 except:
630 pass
631
632 # Build LDIF file
633 bdb_values = {
634 'wait30': 30
635 }
636
637 # Note: I still sometime get failure with a 60s timeout so lets use 90s
638 mdb_values = {
639 'wait30': 90
640 }
641
642 if get_default_db_lib() == 'bdb':
643 values = bdb_values
644 else:
645 values = mdb_values
646
647 ldif_dir = topo.standalone.get_ldif_dir()
648 import_ldif = ldif_dir + '/perf_import.ldif'
649
650 RDN="userNew"
651 PARENT="ou=people,%s" % DEFAULT_SUFFIX
652 dbgen_users(topo.standalone, 90000, import_ldif, DEFAULT_SUFFIX, entry_name=RDN, generic=True, parent=PARENT)
653
654 # Online import
655 import_task = ImportTask(topo.standalone)
656 import_task.import_suffix_from_ldif(ldiffile=import_ldif, suffix=DEFAULT_SUFFIX)
657 import_task.wait(timeout=400)
658 assert import_task.get_exit_code() == 0
659 # Restart server
660 topo.standalone.restart()
661
662 # Create Managed role entry
663 managed_roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
664 role = managed_roles.create(properties={"cn": 'MANAGED_ROLE'})
665
666 # Assign managed role to 4 entries out of the 90K
667 for i in range(1, 5):
668 dn = "uid=%s0000%d,%s" % (RDN, i, PARENT)
669 topo.standalone.modify_s(dn, [(ldap.MOD_REPLACE, 'nsRoleDN', [role.dn.encode()])])
670
671 # Now check that search is fast, evaluating only 4 entries
672 search_start = time.time()
673 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(nsrole=%s)" % role.dn)
674 duration = time.time() - search_start
675 log.info("Duration of the search was %f", duration)
676 assert(len(entries) == 4)
677 assert (duration < 1)
678
679 # Restart server to refresh entrycache
680 topo.standalone.restart()
681
682 # Create Filtered Role entry
683 # it uses 'givenName' attribute that is indexed (eq) by default
684 filtered_roles = FilteredRoles(topo.standalone, DEFAULT_SUFFIX)
685 role = filtered_roles.create(properties={'cn': 'FILTERED_ROLE', 'nsRoleFilter': 'givenName=Technical'})
686
687 # Now check that search is fast
688 search_start = time.time()
689 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(nsrole=%s)" % role.dn)
690 duration = time.time() - search_start
691 log.info("Duration of the search was %f", duration)
692 assert (duration < 1)
693
694 def fin():
695 topo.standalone.restart()
696 try:
697 managed_roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
698 for i in managed_roles.list():
699 i.delete()
700 filtered_roles = FilteredRoles(topo.standalone, DEFAULT_SUFFIX)
701 for i in filtered_roles.list():
702 i.delete()
703 except:
704 pass
705 os.remove(import_ldif)
706
707 request.addfinalizer(fin)
708
709 def test_not_such_entry_role_rewrite(topo, request):
710 """Test that filter components containing 'nsrole=xxx'
711 ,where xxx does not refer to any role definition,
712 replace the component by 'nsuniqueid=-1'
713
714 :id: b098dda5-fc77-46c4-84a7-5d0c7035bb77
715 :setup: server
716 :steps:
717 1. Setup nsrole rewriter
718 2. Add a 'nsroleDN' indexes for managed roles
719 3. Create an 90K ldif files
720 This is large so that unindex search will last long
721 4. import/restart the instance
722 5. Create a managed role and add 4 entries in that role
723 6. Enable plugin log level to capture role plugin message
724 7. Check that a search is fast "(OR(nsrole=managed_role)(nsrole=not_existing_role))"
725 8. Stop the instance
726 9. Check that a message like this was logged: replace (nsrole=not_existing_role) by (nsuniqueid=-1)
727 :expectedresults:
728 1. Operation should succeed
729 2. Operation should succeed
730 3. Operation should succeed
731 4. Operation should succeed
732 5. Operation should succeed
733 6. Operation should succeed
734 7. Operation should succeed
735 8. Operation should succeed
736 9. Operation should succeed
737 """
738 # Setup nsrole rewriter
739 rewriters = Rewriters(topo.standalone)
740 rewriter = rewriters.ensure_state(properties={"cn": "nsrole", "nsslapd-libpath": 'libroles-plugin'})
741 try:
742 rewriter.add('nsslapd-filterrewriter', "role_nsRole_filter_rewriter")
743 except:
744 pass
745
746 # Create an index for nsRoleDN that is used by managed role
747 attrname = 'nsRoleDN'
748 backends = Backends(topo.standalone)
749 backend = backends.get(DEFAULT_BENAME)
750 indexes = backend.get_indexes()
751 try:
752 index = indexes.create(properties={
753 'cn': attrname,
754 'nsSystemIndex': 'false',
755 'nsIndexType': ['eq', 'pres']
756 })
757 except:
758 pass
759
760 # Build LDIF file
761 bdb_values = {
762 'wait60': 60
763 }
764
765 # Note: I still sometime get failure with a 60s timeout so lets use 90s
766 mdb_values = {
767 'wait60': 90
768 }
769
770 if get_default_db_lib() == 'bdb':
771 values = bdb_values
772 else:
773 values = mdb_values
774
775 ldif_dir = topo.standalone.get_ldif_dir()
776 import_ldif = ldif_dir + '/perf_import.ldif'
777
778 RDN="userNew"
779 PARENT="ou=people,%s" % DEFAULT_SUFFIX
780 dbgen_users(topo.standalone, 91000, import_ldif, DEFAULT_SUFFIX, entry_name=RDN, generic=True, parent=PARENT)
781
782 # Online import
783 import_task = ImportTask(topo.standalone)
784 import_task.import_suffix_from_ldif(ldiffile=import_ldif, suffix=DEFAULT_SUFFIX)
785 import_task.wait(timeout=400)
786 assert import_task.get_exit_code() == 0
787 # Restart server
788 topo.standalone.restart()
789
790 # Create Managed role entry
791 managed_roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
792 role = managed_roles.create(properties={"cn": 'MANAGED_ROLE'})
793
794 # Assign managed role to 4 entries out of the 90K
795 for i in range(1, 5):
796 dn = "uid=%s0000%d,%s" % (RDN, i, PARENT)
797 topo.standalone.modify_s(dn, [(ldap.MOD_REPLACE, 'nsRoleDN', [role.dn.encode()])])
798
799 # Enable plugin level to check message
800 topo.standalone.config.loglevel(vals=(ErrorLog.DEFAULT,ErrorLog.PLUGIN))
801
802 # Now check that search is fast, evaluating only 4 entries
803 search_start = time.time()
804 entries = topo.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(|(nsrole=%s)(nsrole=cn=not_such_entry_role,%s))" % (role.dn, DEFAULT_SUFFIX))
805 duration = time.time() - search_start
806 log.info("Duration of the search was %f", duration)
807 assert(len(entries) == 4)
808 assert (duration < 1)
809
810 # Restart server to refresh entrycache
811 topo.standalone.stop()
812
813 # Check that when the role does not exist it is translated into 'nsuniqueid=-1'
814 pattern = ".*replace \(nsRole=cn=not_such_entry_role,dc=example,dc=com\) by \(nsuniqueid=-1\).*"
815 assert topo.standalone.ds_error_log.match(pattern)
816
817 def fin():
818 topo.standalone.restart()
819 try:
820 managed_roles = ManagedRoles(topo.standalone, DEFAULT_SUFFIX)
821 for i in managed_roles.list():
822 i.delete()
823 except:
824 pass
825 os.remove(import_ldif)
826
827 request.addfinalizer(fin)
828
829
830 if __name__ == "__main__":
831 CURRENT_FILE = os.path.realpath(__file__)
832 pytest.main("-s -v %s" % CURRENT_FILE)