Debian Codegraph @

dirsrvtests/tests/suites/plugins/attruniq_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2025 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 subprocess
10 import shutil
11 import re
12 import pytest
13 import ldap
14 import logging
15 from time import sleep
16 from lib389.plugins import AttributeUniquenessPlugin
17 from lib389.idm.nscontainer import nsContainers
18 from lib389.idm.user import UserAccounts
19 from lib389.idm.group import Groups
20 from lib389._constants import DEFAULT_SUFFIX
21 from test389.topologies import topology_st
22 from lib389.utils import ds_is_older
23
24 pytestmark = [pytest.mark.tier1,
25 pytest.mark.skipif(ds_is_older('1.3.3'), reason="Not implemented")]
26
27 logging.getLogger(__name__).setLevel(logging.DEBUG)
28 log = logging.getLogger(__name__)
29 MAIL_ATTR_VALUE = '[email protected]'
30 MAIL_ATTR_VALUE_ALT = '[email protected]'
31
32 EXCLUDED_CONTAINER_CN = "excluded_container"
33 EXCLUDED_CONTAINER_DN = "cn={},{}".format(EXCLUDED_CONTAINER_CN, DEFAULT_SUFFIX)
34
35 EXCLUDED_BIS_CONTAINER_CN = "excluded_bis_container"
36 EXCLUDED_BIS_CONTAINER_DN = "cn={},{}".format(EXCLUDED_BIS_CONTAINER_CN, DEFAULT_SUFFIX)
37
38 ENFORCED_CONTAINER_CN = "enforced_container"
39
40 USER_1_CN = "test_1"
41 USER_2_CN = "test_2"
42 USER_3_CN = "test_3"
43 USER_4_CN = "test_4"
44
45 PROVISIONING_CN = "provisioning"
46 PROVISIONING_DN = f"cn={PROVISIONING_CN},{DEFAULT_SUFFIX}"
47
48 ACTIVE_CN = "accounts"
49 STAGE_CN = "staged users"
50 DELETE_CN = "deleted users"
51 ACTIVE_DN = f"cn={ACTIVE_CN},{DEFAULT_SUFFIX}"
52 STAGE_DN = f"cn={STAGE_CN},{PROVISIONING_DN}"
53
54 ACTIVE_USER_1_CN = "active_1"
55 ACTIVE_USER_2_CN = "active_2"
56
57 STAGE_USER_1_CN = 'stage_1'
58 STAGE_USER_2_CN = 'stage_2'
59
60
61 @pytest.fixture(scope='function')
62 def containers(topology_st, request):
63 """Create containers for the tests"""
64 log.info('Create containers for the tests')
65 containers = nsContainers(topology_st.standalone, DEFAULT_SUFFIX)
66 cont_provisioning = containers.create(properties={'cn': PROVISIONING_CN})
67 cont_active = containers.create(properties={'cn': ACTIVE_CN})
68
69 containers_provisioning = nsContainers(topology_st.standalone, PROVISIONING_DN)
70 cont_stage = containers_provisioning.create(properties={'cn': STAGE_CN})
71 cont_delete = containers_provisioning.create(properties={'cn': DELETE_CN})
72
73 def fin():
74 log.info('Delete containers')
75 cont_stage.delete()
76 cont_delete.delete()
77 cont_provisioning.delete()
78 cont_active.delete()
79
80 request.addfinalizer(fin)
81
82
83 @pytest.fixture(scope='function')
84 def attruniq(topology_st, request):
85 log.info('Setup attribute uniqueness plugin')
86 attruniq = AttributeUniquenessPlugin(topology_st.standalone, dn="cn=attruniq,cn=plugins,cn=config")
87
88 if attruniq.exists():
89 attruniq.delete()
90 topology_st.standalone.restart()
91
92 attruniq.create(properties={
93 'cn': 'attruniq',
94 'uniqueness-attribute-name': 'cn',
95 'uniqueness-subtrees': 'cn=config',
96 'nsslapd-pluginEnabled': 'on'
97 })
98 topology_st.standalone.restart()
99
100 def fin():
101 if attruniq.exists():
102 attruniq.delete()
103 topology_st.standalone.restart()
104
105 request.addfinalizer(fin)
106
107 return attruniq
108
109
110 @pytest.fixture(scope='function')
111 def active_user_1(topology_st, request):
112 log.info('Create active user 1')
113 active_users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
114 rdn='cn={}'.format(ACTIVE_CN))
115 if active_users.exists(ACTIVE_USER_1_CN):
116 active_users.get(ACTIVE_USER_1_CN).delete()
117
118 active_user_1 = active_users.create(properties={'cn': ACTIVE_USER_1_CN,
119 'uid': ACTIVE_USER_1_CN,
120 'sn': ACTIVE_USER_1_CN,
121 'uidNumber': '1',
122 'gidNumber': '11',
123 'homeDirectory': f'/home/{ACTIVE_USER_1_CN}'})
124
125 def fin():
126 if active_user_1.exists():
127 log.info('Delete active user 1')
128 active_user_1.delete()
129
130 request.addfinalizer(fin)
131
132 return active_user_1
133
134
135 @pytest.fixture(scope='function')
136 def active_user_2(topology_st, request):
137 log.info('Create active user 2')
138 active_users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
139 rdn='cn={}'.format(ACTIVE_CN))
140 if active_users.exists(ACTIVE_USER_2_CN):
141 active_users.get(ACTIVE_USER_2_CN).delete()
142
143 active_user_2 = active_users.create(properties={'cn': ACTIVE_USER_2_CN,
144 'uid': ACTIVE_USER_2_CN,
145 'sn': ACTIVE_USER_2_CN,
146 'uidNumber': '2',
147 'gidNumber': '22',
148 'homeDirectory': f'/home/{ACTIVE_USER_2_CN}'})
149
150 def fin():
151 if active_user_2.exists():
152 log.info('Delete active user 2')
153 active_user_2.delete()
154
155 request.addfinalizer(fin)
156
157 return active_user_2
158
159
160 @pytest.fixture(scope='function')
161 def stage_user_1(topology_st, request):
162 log.info('Create stage user 1')
163 stage_users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
164 rdn=f'cn={STAGE_CN},cn={PROVISIONING_CN}')
165 if stage_users.exists(STAGE_USER_1_CN):
166 stage_users.get(STAGE_USER_1_CN).delete()
167
168 stage_user_1 = stage_users.create(properties={'cn': STAGE_USER_1_CN,
169 'uid': STAGE_USER_1_CN,
170 'sn': STAGE_USER_1_CN,
171 'uidNumber': '2',
172 'gidNumber': '22',
173 'homeDirectory': f'/home/{STAGE_USER_1_CN}'})
174
175 def fin():
176 if stage_user_1.exists():
177 log.info('Delete stage user 1')
178 stage_user_1.delete()
179
180 request.addfinalizer(fin)
181
182 return stage_user_1
183
184
185 @pytest.fixture(scope='function')
186 def stage_user_2(topology_st, request):
187 log.info('Create stage user 1')
188 stage_users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
189 rdn=f'cn={STAGE_CN},cn={PROVISIONING_CN}')
190 if stage_users.exists(STAGE_USER_2_CN):
191 stage_users.get(STAGE_USER_2_CN).delete()
192
193 stage_user_2 = stage_users.create(properties={'cn': STAGE_USER_2_CN,
194 'uid': STAGE_USER_2_CN,
195 'sn': STAGE_USER_2_CN,
196 'uidNumber': '2',
197 'gidNumber': '22',
198 'homeDirectory': f'/home/{STAGE_USER_2_CN}'})
199
200 def fin():
201 if stage_user_2.exists():
202 log.info('Delete stage user 2')
203 stage_user_2.delete()
204
205 request.addfinalizer(fin)
206
207 return stage_user_2
208
209
210 def create_active_user(topology_st, uid=ACTIVE_USER_2_CN, cn=ACTIVE_USER_2_CN):
211 active_users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
212 rdn=f'cn={ACTIVE_CN}')
213 return active_users.create(properties={'cn': cn,
214 'uid': uid,
215 'sn': uid,
216 'uidNumber': '2',
217 'gidNumber': '22',
218 'homeDirectory': f'/home/{uid}'})
219
220
221 def create_stage_user(topology_st, uid=STAGE_USER_2_CN, cn=STAGE_USER_2_CN):
222 stage_users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
223 rdn=f'cn={STAGE_CN},cn={PROVISIONING_CN}')
224 return stage_users.create(properties={'cn': cn,
225 'uid': uid,
226 'sn': uid,
227 'uidNumber': '3',
228 'gidNumber': '33',
229 'homeDirectory': f'/home/{uid}'})
230
231
232 def test_modrdn_attr_uniqueness(topology_st, attruniq):
233 """Test that we can not add two entries that have the same attr value that is
234 defined by the plugin
235
236 :id: dd763830-78b8-452e-888d-1d83d2e623f1
237
238 :setup: Standalone instance
239
240 :steps: 1. Create two groups
241 2. Setup PLUGIN_ATTR_UNIQUENESS plugin for 'mail' attribute for the group2
242 3. Enable PLUGIN_ATTR_UNIQUENESS plugin as "ON"
243 4. Add two test users at group1 and add not uniq 'mail' attribute to each of them
244 5. Move user1 to group2
245 6. Move user2 to group2
246 7. Move user2 back to group1
247
248 :expectedresults:
249 1. Success
250 2. Success
251 3. Success
252 4. Success
253 5. Success
254 6. Modrdn operation should FAIL
255 7. Success
256 """
257 log.debug('Create two groups')
258 groups = Groups(topology_st.standalone, DEFAULT_SUFFIX)
259 group1 = groups.create(properties={'cn': 'group1'})
260 group2 = groups.create(properties={'cn': 'group2'})
261
262 attruniq.replace('uniqueness-attribute-name', 'mail')
263 attruniq.replace('uniqueness-subtrees', group2.dn)
264 attruniq.enable_all_subtrees()
265 log.debug(f'Enable PLUGIN_ATTR_UNIQUENESS plugin as "ON"')
266 attruniq.enable()
267 topology_st.standalone.restart()
268
269 log.debug(f'Add two test users at group1 and add not uniq {MAIL_ATTR_VALUE} attribute to each of them')
270 users = UserAccounts(topology_st.standalone, basedn=group1.dn, rdn=None)
271 user1 = users.create_test_user(1)
272 user2 = users.create_test_user(2)
273 user1.add('mail', MAIL_ATTR_VALUE)
274 user2.add('mail', MAIL_ATTR_VALUE)
275
276 log.debug('Move user1 to group2')
277 user1.rename(f'uid={user1.rdn}', group2.dn)
278
279 log.debug('Move user2 to group2')
280 with pytest.raises(ldap.CONSTRAINT_VIOLATION) as excinfo:
281 user2.rename(f'uid={user2.rdn}', group2.dn)
282 log.fatal(f'Failed: Attribute "mail" with {MAIL_ATTR_VALUE} is accepted')
283 assert 'attribute value already exist' in str(excinfo.value)
284 log.debug(excinfo.value)
285
286 user1.delete()
287 user2.delete()
288
289
290 def test_multiple_attr_uniqueness(topology_st, attruniq):
291 """ Test that attribute uniqueness works properly with multiple attributes
292
293 :id: c49aa5c1-7e65-45fd-b064-55e0b815e9bc
294 :setup: Standalone instance
295 :steps:
296 1. Setup attribute uniqueness plugin to ensure uniqueness of attributes 'mail' and 'mailAlternateAddress'
297 2. Add user with unique '[email protected]' and '[email protected]'
298 3. Try adding another user with '[email protected]'
299 4. Try adding another user with '[email protected]'
300 5. Try adding another user with '[email protected]'
301 6. Try adding another user with '[email protected]'
302 :expectedresults:
303 1. Success
304 2. Success
305 3. Should raise CONSTRAINT_VIOLATION
306 4. Should raise CONSTRAINT_VIOLATION
307 5. Should raise CONSTRAINT_VIOLATION
308 6. Should raise CONSTRAINT_VIOLATION
309 """
310
311 attruniq.replace('uniqueness-attribute-name', ['mail', 'mailAlternateAddress'])
312 attruniq.replace('uniqueness-subtrees', DEFAULT_SUFFIX)
313 attruniq.enable_all_subtrees()
314 log.debug(f'Enable PLUGIN_ATTR_UNIQUENESS plugin as "ON"')
315 attruniq.enable()
316 topology_st.standalone.restart()
317
318 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
319
320 testuser1 = users.create_test_user(100,100)
321 testuser1.add('objectclass', 'extensibleObject')
322 testuser1.add('mail', MAIL_ATTR_VALUE)
323 testuser1.add('mailAlternateAddress', MAIL_ATTR_VALUE_ALT)
324
325 testuser2 = users.create_test_user(200, 200)
326 testuser2.add('objectclass', 'extensibleObject')
327
328 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
329 testuser2.add('mail', MAIL_ATTR_VALUE)
330
331 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
332 testuser2.add('mailAlternateAddress', MAIL_ATTR_VALUE_ALT)
333
334 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
335 testuser2.add('mail', MAIL_ATTR_VALUE_ALT)
336
337 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
338 testuser2.add('mailAlternateAddress', MAIL_ATTR_VALUE)
339
340 testuser1.delete()
341 testuser2.delete()
342
343
344 def test_exclude_subtrees(topology_st, attruniq):
345 """ Test attribute uniqueness with exclude scope
346
347 :id: 43d29a60-40e1-4ebd-b897-6ef9f20e9f27
348 :setup: Standalone instance
349 :steps:
350 1. Setup and enable attribute uniqueness plugin for telephonenumber unique attribute
351 2. Create subtrees and test users
352 3. Add a unique attribute to a user within uniqueness scope
353 4. Add exclude subtree
354 5. Try to add existing value attribute to an entry within uniqueness scope
355 6. Try to add existing value attribute to an entry within exclude scope
356 7. Remove the attribute from affected entries
357 8. Add a unique attribute to a user within exclude scope
358 9. Try to add existing value attribute to an entry within uniqueness scope
359 10. Try to add existing value attribute to another entry within uniqueness scope
360 11. Remove the attribute from affected entries
361 12. Add another exclude subtree
362 13. Add a unique attribute to a user within uniqueness scope
363 14. Try to add existing value attribute to an entry within uniqueness scope
364 15. Try to add existing value attribute to an entry within exclude scope
365 16. Try to add existing value attribute to an entry within another exclude scope
366 17. Clean up entries
367 :expectedresults:
368 1. Success
369 2. Success
370 3. Success
371 4. Success
372 5. Should raise CONSTRAINT_VIOLATION
373 6. Success
374 7. Success
375 8. Success
376 9. Success
377 10. Should raise CONSTRAINT_VIOLATION
378 11. Success
379 12. Success
380 13. Success
381 14. Should raise CONSTRAINT_VIOLATION
382 15. Success
383 16. Success
384 17. Success
385 """
386 # Replace dummy config with actual test config
387 attruniq.replace('uniqueness-attribute-name', 'telephonenumber')
388 attruniq.replace('uniqueness-subtrees', DEFAULT_SUFFIX)
389 attruniq.enable_all_subtrees()
390 attruniq.enable()
391 topology_st.standalone.restart()
392
393 log.info('Create subtrees container')
394 containers = nsContainers(topology_st.standalone, DEFAULT_SUFFIX)
395 cont1 = containers.create(properties={'cn': EXCLUDED_CONTAINER_CN})
396 cont2 = containers.create(properties={'cn': EXCLUDED_BIS_CONTAINER_CN})
397 cont3 = containers.create(properties={'cn': ENFORCED_CONTAINER_CN})
398
399 log.info('Create test users')
400 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
401 rdn='cn={}'.format(ENFORCED_CONTAINER_CN))
402 users_excluded = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
403 rdn='cn={}'.format(EXCLUDED_CONTAINER_CN))
404 users_excluded2 = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
405 rdn='cn={}'.format(EXCLUDED_BIS_CONTAINER_CN))
406
407 user1 = users.create(properties={'cn': USER_1_CN,
408 'uid': USER_1_CN,
409 'sn': USER_1_CN,
410 'uidNumber': '1',
411 'gidNumber': '11',
412 'homeDirectory': '/home/{}'.format(USER_1_CN)})
413 user2 = users.create(properties={'cn': USER_2_CN,
414 'uid': USER_2_CN,
415 'sn': USER_2_CN,
416 'uidNumber': '2',
417 'gidNumber': '22',
418 'homeDirectory': '/home/{}'.format(USER_2_CN)})
419 user3 = users_excluded.create(properties={'cn': USER_3_CN,
420 'uid': USER_3_CN,
421 'sn': USER_3_CN,
422 'uidNumber': '3',
423 'gidNumber': '33',
424 'homeDirectory': '/home/{}'.format(USER_3_CN)})
425 user4 = users_excluded2.create(properties={'cn': USER_4_CN,
426 'uid': USER_4_CN,
427 'sn': USER_4_CN,
428 'uidNumber': '4',
429 'gidNumber': '44',
430 'homeDirectory': '/home/{}'.format(USER_4_CN)})
431
432 UNIQUE_VALUE = '1234'
433
434 try:
435 log.info('Create user with unique attribute')
436 user1.add('telephonenumber', UNIQUE_VALUE)
437 assert user1.present('telephonenumber', UNIQUE_VALUE)
438
439 log.info('Add exclude subtree')
440 attruniq.add_exclude_subtree(EXCLUDED_CONTAINER_DN)
441 topology_st.standalone.restart()
442
443 log.info('Verify an already used attribute value cannot be added within the same subtree')
444 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
445 user2.add('telephonenumber', UNIQUE_VALUE)
446
447 log.info('Verify an entry with same attribute value can be added within exclude subtree')
448 user3.add('telephonenumber', UNIQUE_VALUE)
449 assert user3.present('telephonenumber', UNIQUE_VALUE)
450
451 log.info('Cleanup unique attribute values')
452 user1.remove_all('telephonenumber')
453 user3.remove_all('telephonenumber')
454
455 log.info('Add a unique value to an entry in excluded scope')
456 user3.add('telephonenumber', UNIQUE_VALUE)
457 assert user3.present('telephonenumber', UNIQUE_VALUE)
458
459 log.info('Verify the same value can be added to an entry within uniqueness scope')
460 user1.add('telephonenumber', UNIQUE_VALUE)
461 assert user1.present('telephonenumber', UNIQUE_VALUE)
462
463 log.info('Verify that yet another same value cannot be added to another entry within uniqueness scope')
464 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
465 user2.add('telephonenumber', UNIQUE_VALUE)
466
467 log.info('Cleanup unique attribute values')
468 user1.remove_all('telephonenumber')
469 user3.remove_all('telephonenumber')
470
471 log.info('Add another exclude subtree')
472 attruniq.add_exclude_subtree(EXCLUDED_BIS_CONTAINER_DN)
473 topology_st.standalone.restart()
474
475 user1.add('telephonenumber', UNIQUE_VALUE)
476 log.info('Verify an already used attribute value cannot be added within the same subtree')
477 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
478 user2.add('telephonenumber', UNIQUE_VALUE)
479
480 log.info('Verify an already used attribute can be added to an entry in exclude scope')
481 user3.add('telephonenumber', UNIQUE_VALUE)
482 assert user3.present('telephonenumber', UNIQUE_VALUE)
483 user4.add('telephonenumber', UNIQUE_VALUE)
484 assert user4.present('telephonenumber', UNIQUE_VALUE)
485
486 finally:
487 log.info('Clean up users, containers and attribute uniqueness plugin')
488 user1.delete()
489 user2.delete()
490 user3.delete()
491 user4.delete()
492 cont1.delete()
493 cont2.delete()
494 cont3.delete()
495 attruniq.disable()
496 attruniq.delete()
497
498
499 def test_matchingrule_attr(topology_st):
500 """ Test list extension MR attribute. Check for "cn" using CES (versus it
501 being defined as CIS)
502
503 :id: 5cde4342-6fa3-4225-b23d-0af918981075
504 :setup: Standalone instance
505 :steps:
506 1. Setup and enable attribute uniqueness plugin to use CN attribute
507 with a matching rule of CaseExactMatch.
508 2. Add user with CN value is lowercase
509 3. Add second user with same lowercase CN which should be rejected
510 4. Add second user with same CN value but with mixed case
511 5. Modify second user replacing CN value to lc which should be rejected
512 :expectedresults:
513 1. Success
514 2. Success
515 3. Success
516 4. Success
517 5. Success
518 """
519
520 inst = topology_st.standalone
521 attruniq = AttributeUniquenessPlugin(inst,
522 dn="cn=attribute uniqueness,cn=plugins,cn=config")
523
524 if attruniq.exists():
525 attruniq.delete()
526 inst.restart()
527
528 attruniq.create(properties={
529 'cn': 'attribute uniqueness',
530 'uniqueness-attribute-name': 'cn:CaseExactMatch:',
531 'uniqueness-subtrees': DEFAULT_SUFFIX
532 })
533 attruniq.enable_all_subtrees()
534 attruniq.enable()
535 inst.restart()
536
537 users = UserAccounts(inst, DEFAULT_SUFFIX)
538 users.create(properties={'cn': "common_name",
539 'uid': "uid_name",
540 'sn': "uid_name",
541 'uidNumber': '1',
542 'gidNumber': '11',
543 'homeDirectory': '/home/uid_name'})
544
545 log.info('Add entry with the exact CN value which should be rejected')
546 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
547 users.create(properties={'cn': "common_name",
548 'uid': "uid_name2",
549 'sn': "uid_name2",
550 'uidNumber': '11',
551 'gidNumber': '111',
552 'homeDirectory': '/home/uid_name2'})
553
554 log.info('Add entry with the mixed case CN value which should be allowed')
555 user = users.create(properties={'cn': "Common_Name",
556 'uid': "uid_name2",
557 'sn': "uid_name2",
558 'uidNumber': '11',
559 'gidNumber': '111',
560 'homeDirectory': '/home/uid_name2'})
561
562 log.info('Mod entry with exact case CN value which should be rejected')
563 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
564 user.replace('cn', 'common_name')
565
566 attruniq.disable()
567 attruniq.delete()
568 inst.restart()
569
570
571 def test_one_container_add(topology_st, attruniq, containers, active_user_1):
572 """Test ADD operations with attribute uniqueness in a single container.
573
574 This test verifies that the attribute uniqueness plugin correctly prevents
575 the addition of entries with duplicate attribute values within a single
576 container when the plugin is enabled.
577
578 :id: 05960fb7-3ef8-4b53-b616-126f7ac737c4
579 :setup: Standalone instance with containers and one active user
580 :steps:
581 1. Verify user with duplicate 'cn' attribute can be added
582 without enforced uniqueness
583 2. Remove the duplicate user
584 3. Configure and enable attribute uniqueness plugin for 'cn' in active container
585 4. Try to add user with duplicate 'cn' attribute (should fail)
586 :expectedresults:
587 1. Success
588 2. Success
589 3. Success
590 4. Duplicate entry creation fails with CONSTRAINT_VIOLATION
591 """
592
593 log.info('Verify that there is no uniqueness rule enforced')
594 active_users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX,
595 rdn='cn={}'.format(ACTIVE_CN))
596 active_2 = active_users.create(properties={'cn': [ACTIVE_USER_1_CN, ACTIVE_USER_2_CN],
597 'uid': ACTIVE_USER_2_CN,
598 'sn': ACTIVE_USER_2_CN,
599 'uidNumber': '2',
600 'gidNumber': '22',
601 'homeDirectory': f'/home/{ACTIVE_USER_2_CN}'})
602
603 log.info('Remove the second user')
604 active_2.delete()
605
606 log.info('Setup attribute uniqueness plugin for "cn" attribute')
607 attruniq.replace('uniqueness-subtrees', ACTIVE_DN)
608 attruniq.enable()
609 topology_st.standalone.restart()
610
611 log.info('Verify that uniqueness rule is enforced')
612 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
613 active_users.create(properties={'cn': [ACTIVE_USER_1_CN, ACTIVE_USER_2_CN],
614 'uid': ACTIVE_USER_2_CN,
615 'sn': ACTIVE_USER_2_CN,
616 'uidNumber': '2',
617 'gidNumber': '22',
618 'homeDirectory': f'/home/{ACTIVE_USER_2_CN}'})
619
620
621 def test_one_container_mod(topology_st, attruniq, containers,
622 active_user_1, active_user_2):
623 """Test MOD operations with attribute uniqueness in a single container.
624
625 This test verifies that the attribute uniqueness plugin correctly prevents
626 modification of entries that would create duplicate attribute values within
627 a single container.
628
629 :id: c566f279-6073-4fb7-8956-f30d1bdda8e2
630 :setup: Standalone instance with containers and two active users
631 :steps:
632 1. Configure and enable attribute uniqueness plugin for "cn" attribute
633 2. Try to modify user2's "cn" to match user1's "cn" (should fail)
634 :expectedresults:
635 1. Plugin configured and enabled successfully
636 2. Modification fails with CONSTRAINT_VIOLATION
637 """
638
639 log.info('Setup attribute uniqueness plugin for "cn" attribute')
640 attruniq.replace('uniqueness-subtrees', ACTIVE_DN)
641 attruniq.enable()
642 topology_st.standalone.restart()
643
644 log.info('Verify that uniqueness rule is enforced')
645 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
646 active_user_2.replace('cn', ACTIVE_USER_1_CN)
647
648
649 def test_one_container_modrdn(topology_st, attruniq, containers,
650 active_user_1, active_user_2):
651 """Test MODRDN operations with attribute uniqueness in a single container.
652
653 This test verifies that the attribute uniqueness plugin correctly prevents
654 ModRDN operations that would create duplicate attribute values within
655 a single container.
656
657 :id: cabfec46-bea7-4175-913e-2f7ab84a2919
658 :setup: Standalone instance with containers and two active users
659 :steps:
660 1. Configure and enable attribute uniqueness plugin for "cn" attribute
661 2. Try to rename user2 to have the same "cn" as user1 (should fail)
662 :expectedresults:
663 1. Plugin configured and enabled successfully
664 2. ModRDN operation fails with CONSTRAINT_VIOLATION
665 """
666
667 log.info('Setup attribute uniqueness plugin for "cn" attribute')
668 attruniq.replace('uniqueness-subtrees', ACTIVE_DN)
669 attruniq.enable()
670 topology_st.standalone.restart()
671
672 log.info('Verify that uniqueness rule is enforced')
673 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
674 active_user_2.rename(f'cn={ACTIVE_USER_1_CN}', deloldrdn=False)
675
676
677 def test_multiple_containers_add(topology_st, attruniq, containers,
678 active_user_1, stage_user_1):
679 """Test ADD operations with attribute uniqueness across multiple containers.
680
681 This test verifies that when multiple containers are configured for
682 attribute uniqueness, the plugin enforces uniqueness within each container
683 separately, but allows duplicate values across different containers.
684
685 :id: 400cdf54-499a-4c23-91f5-835f44835e3e
686 :setup: Standalone instance with containers and users in different containers
687 :steps:
688 1. Configure plugin for 'cn' attribute in both active and stage containers
689 2. Try to add user with duplicate 'cn' in active container (should fail)
690 3. Try to add user with duplicate 'cn' in stage container (should fail)
691 4. Try to add user in active container with 'cn' matching stage user (should succeed)
692 5. Try to add user in stage container with 'cn' matching active user (should succeed)
693 :expectedresults:
694 1. Plugin configured successfully
695 2. Duplicate in active container fails with CONSTRAINT_VIOLATION
696 3. Duplicate in stage container fails with CONSTRAINT_VIOLATION
697 4. Cross-container duplicate in active succeeds
698 5. Cross-container duplicate in stage succeeds
699 """
700
701 log.info('Setup attribute uniqueness plugin for "cn" attribute')
702 attruniq.replace('uniqueness-subtrees', [ACTIVE_DN, STAGE_DN])
703 attruniq.enable()
704 topology_st.standalone.restart()
705
706 log.info("Verify both subtrees apply attruniqueness separately")
707 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
708 create_active_user(topology_st, uid=ACTIVE_USER_2_CN, cn=[ACTIVE_USER_1_CN, ACTIVE_USER_2_CN])
709 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
710 create_stage_user(topology_st, uid=STAGE_USER_2_CN, cn=[STAGE_USER_1_CN, STAGE_USER_2_CN])
711
712 log.info("Verify that the subtrees do not enforce uniqueness across each other")
713 try:
714 active_user_2 = create_active_user(topology_st, uid=ACTIVE_USER_2_CN,
715 cn=[STAGE_USER_1_CN, ACTIVE_USER_2_CN])
716 stage_user_2 = create_stage_user(topology_st, uid=STAGE_USER_2_CN,
717 cn=[ACTIVE_USER_1_CN, STAGE_USER_2_CN])
718 except:
719 assert False, "Failed to create users necessary for the test"
720 finally:
721 active_user_2.delete()
722 stage_user_2.delete()
723
724
725 def test_multiple_containers_add_across_subtrees(topology_st, attruniq, containers,
726 active_user_1, stage_user_1):
727 """Test ADD operations with attribute uniqueness across multiple containers with cross-subtree enforcement.
728
729 This test verifies that when the 'across-all-subtrees' option is enabled,
730 the attribute uniqueness plugin enforces uniqueness across all configured
731 containers, preventing duplicate values anywhere in the configured scope.
732
733 :id: 7b58c46a-9ef1-4bbc-929d-578dbd3f4910
734 :setup: Standalone instance with containers and users in different containers
735 :steps:
736 1. Configure plugin for 'cn' attribute in both containers with across-subtrees enabled
737 2. Try to add user with duplicate 'cn' in active container (should fail)
738 3. Try to add user with duplicate 'cn' in stage container (should fail)
739 4. Try to add user in stage container with 'cn' matching active user (should fail)
740 5. Try to add user in active container with 'cn' matching stage user (should fail)
741 :expectedresults:
742 1. Plugin configured successfully with across-subtrees enabled
743 2. Duplicate in active container fails with CONSTRAINT_VIOLATION
744 3. Duplicate in stage container fails with CONSTRAINT_VIOLATION
745 4. Cross-container duplicate in stage fails with CONSTRAINT_VIOLATION
746 5. Cross-container duplicate in active fails with CONSTRAINT_VIOLATION
747 """
748
749 log.info('Setup attribute uniqueness plugin for "cn" attribute')
750 attruniq.add_unique_subtree(ACTIVE_DN)
751 attruniq.add_unique_subtree(STAGE_DN)
752 attruniq.enable_all_subtrees()
753 attruniq.enable()
754 topology_st.standalone.restart()
755
756 # Inserting sleep to prevent repeatedly occuring 'Server is busy' errors
757 sleep(5)
758
759 log.info("Verify both subtrees apply attruniqueness separately")
760 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
761 create_active_user(topology_st, uid=ACTIVE_USER_2_CN, cn=[ACTIVE_USER_1_CN, ACTIVE_USER_2_CN])
762 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
763 create_stage_user(topology_st, uid=STAGE_USER_2_CN, cn=[STAGE_USER_1_CN, STAGE_USER_2_CN])
764
765 log.info('Verify that uniqueness rule is enforced')
766 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
767 create_stage_user(topology_st, uid=STAGE_USER_2_CN, cn=[ACTIVE_USER_1_CN, STAGE_USER_2_CN])
768 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
769 create_active_user(topology_st, uid=ACTIVE_USER_2_CN, cn=[STAGE_USER_1_CN, ACTIVE_USER_2_CN])
770
771
772 def test_multiple_containers_mod(topology_st, attruniq, containers,
773 active_user_1, active_user_2, stage_user_1, stage_user_2):
774 """Test MOD operations with attribute uniqueness across multiple containers.
775
776 This test verifies that when multiple containers are configured for
777 attribute uniqueness without cross-subtree enforcement, modifications
778 are constrained within each container but allowed across containers.
779
780 :id: 29ad0e41-bb6f-4ec0-bf79-0d6a4402a5db
781 :setup: Standalone instance with containers and users in different containers
782 :steps:
783 1. Configure plugin for 'cn' attribute in both containers
784 2. Try to modify user in active container to match another active user (should fail)
785 3. Try to modify user in stage container to match another stage user (should fail)
786 4. Try to modify user in active container to match stage user (should succeed)
787 5. Try to modify user in stage container to match active user (should succeed)
788 :expectedresults:
789 1. Plugin configured successfully
790 2. Intra-container modification fails with CONSTRAINT_VIOLATION
791 3. Intra-container modification fails with CONSTRAINT_VIOLATION
792 4. Cross-container modification succeeds
793 5. Cross-container modification succeeds
794 """
795
796 log.info('Setup attribute uniqueness plugin for "cn" attribute')
797 attruniq.replace('uniqueness-subtrees', [ACTIVE_DN, STAGE_DN])
798 attruniq.enable()
799 topology_st.standalone.restart()
800
801 log.info('Verify that uniqueness is enforced in individual subtrees')
802 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
803 active_user_1.replace('cn', ACTIVE_USER_2_CN)
804 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
805 stage_user_1.replace('cn', STAGE_USER_2_CN)
806
807 log.info('Verify that uniqueness rule is not enforced across subtrees')
808 active_user_1.replace('cn', STAGE_USER_1_CN)
809 stage_user_1.replace('cn', ACTIVE_USER_1_CN)
810
811
812 def test_multiple_containers_mod_across_subtrees(topology_st, attruniq, containers,
813 active_user_1, active_user_2, stage_user_1, stage_user_2):
814 """Test MOD operations with attribute uniqueness across multiple containers with cross-subtree enforcement.
815
816 This test verifies that when the 'across-all-subtrees' option is enabled,
817 modification operations are constrained across all configured containers,
818 preventing duplicate values anywhere in the configured scope.
819
820 :id: f5272114-8869-4c61-96e2-0241500c47c2
821 :setup: Standalone instance with containers and users in different containers
822 :steps:
823 1. Configure plugin for 'cn' attribute in both containers with across-subtrees enabled
824 2. Try to modify user in active container to match another active user (should fail)
825 3. Try to modify user in stage container to match another stage user (should fail)
826 4. Try to modify user in active container to match stage user (should fail)
827 5. Try to modify user in stage container to match active user (should fail)
828 :expectedresults:
829 1. Plugin configured successfully with across-subtrees enabled
830 2. Intra-container modification fails with CONSTRAINT_VIOLATION
831 3. Intra-container modification fails with CONSTRAINT_VIOLATION
832 4. Cross-container modification fails with CONSTRAINT_VIOLATION
833 5. Cross-container modification fails with CONSTRAINT_VIOLATION
834 """
835
836 log.info('Setup attribute uniqueness plugin for "cn" attribute')
837 attruniq.add_unique_subtree(ACTIVE_DN)
838 attruniq.add_unique_subtree(STAGE_DN)
839 attruniq.enable_all_subtrees()
840 attruniq.enable()
841 topology_st.standalone.restart()
842
843 log.info('Verify that uniqueness is enforced in individual subtrees')
844 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
845 active_user_1.replace('cn', ACTIVE_USER_2_CN)
846 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
847 stage_user_1.replace('cn', STAGE_USER_2_CN)
848
849 log.info('Verify that uniqueness rule is enforced across subtrees')
850 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
851 active_user_1.replace('cn', STAGE_USER_1_CN)
852 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
853 stage_user_1.replace('cn', ACTIVE_USER_1_CN)
854
855
856 def test_multiple_containers_modrdn(topology_st, attruniq, containers,
857 active_user_1, active_user_2, stage_user_1, stage_user_2):
858 """Test MODRDN operations with attribute uniqueness across multiple containers.
859
860 This test verifies that when multiple containers are configured for
861 attribute uniqueness without cross-subtree enforcement, ModRDN operations
862 are constrained within each container but allowed across containers.
863
864 :id: 4b835bfe-d116-418a-beb9-943e23b7cd2a
865 :setup: Standalone instance with containers and users in different containers
866 :steps:
867 1. Configure plugin for 'cn' attribute in both containers
868 2. Try to rename user in active container to match another active user (should fail)
869 3. Try to rename user in stage container to match another stage user (should fail)
870 4. Try to rename user in active container to match stage user (should succeed)
871 5. Try to rename user in stage container to match active user (should succeed)
872 :expectedresults:
873 1. Plugin configured successfully
874 2. Intra-container ModRDN fails with CONSTRAINT_VIOLATION
875 3. Intra-container ModRDN fails with CONSTRAINT_VIOLATION
876 4. Cross-container ModRDN succeeds
877 5. Cross-container ModRDN succeeds
878 """
879
880 log.info('Setup attribute uniqueness plugin for "cn" attribute')
881 # Replace dummy subtree with actual test subtrees
882 attruniq.replace('uniqueness-subtrees', [ACTIVE_DN, STAGE_DN])
883 attruniq.enable()
884 topology_st.standalone.restart()
885
886 log.info('Verify that uniqueness is enforced in individual subtrees')
887 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
888 active_user_1.rename(f'cn={ACTIVE_USER_2_CN}', deloldrdn=False)
889 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
890 stage_user_1.rename(f'cn={STAGE_USER_2_CN}', deloldrdn=False)
891
892 log.info('Verify that uniqueness rule is not enforced across subtrees')
893 active_user_2.rename(f'cn={STAGE_USER_1_CN}', deloldrdn=False)
894 stage_user_2.rename(f'cn={ACTIVE_USER_1_CN}', deloldrdn=False)
895
896
897 def test_multiple_containers_modrdn_across_subtrees(topology_st, attruniq, containers,
898 active_user_1, active_user_2, stage_user_1, stage_user_2):
899 """Test MODRDN operations with attribute uniqueness across multiple containers with cross-subtree enforcement.
900
901 This test verifies that when the 'across-all-subtrees' option is enabled,
902 ModRDN operations are constrained across all configured containers,
903 preventing duplicate values anywhere in the configured scope.
904
905 :id: 8d2c4af9-1b45-4c72-9523-7f8b39c74e21
906 :setup: Standalone instance with containers and users in different containers
907 :steps:
908 1. Configure plugin for 'cn' attribute in both containers with across-subtrees enabled
909 2. Try to rename user in active container to match another active user (should fail)
910 3. Try to rename user in stage container to match another stage user (should fail)
911 4. Try to rename user in active container to match stage user (should fail)
912 5. Try to rename user in stage container to match active user (should fail)
913 :expectedresults:
914 1. Plugin configured successfully with across-subtrees enabled
915 2. Intra-container ModRDN fails with CONSTRAINT_VIOLATION
916 3. Intra-container ModRDN fails with CONSTRAINT_VIOLATION
917 4. Cross-container ModRDN fails with CONSTRAINT_VIOLATION
918 5. Cross-container ModRDN fails with CONSTRAINT_VIOLATION
919 """
920
921 log.info('Setup attribute uniqueness plugin for "cn" attribute')
922 attruniq.add_unique_subtree(ACTIVE_DN)
923 attruniq.add_unique_subtree(STAGE_DN)
924 attruniq.enable_all_subtrees()
925 attruniq.enable()
926 topology_st.standalone.restart()
927
928 log.info('Verify that uniqueness is enforced in individual subtrees')
929 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
930 active_user_1.rename(f'cn={ACTIVE_USER_2_CN}', deloldrdn=False)
931 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
932 stage_user_1.rename(f'cn={STAGE_USER_2_CN}', deloldrdn=False)
933
934 log.info('Verify that uniqueness rule is enforced across subtrees')
935 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
936 active_user_2.rename(f'cn={STAGE_USER_1_CN}', deloldrdn=False)
937 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
938 stage_user_2.rename(f'cn={ACTIVE_USER_1_CN}', deloldrdn=False)
939
940
941 def _config_file(topology_st, action='save'):
942 dse_ldif = topology_st.standalone.confdir + '/dse.ldif'
943 sav_file = topology_st.standalone.confdir + '/dse.ldif.ticket47823'
944 if action == 'save':
945 shutil.copy(dse_ldif, sav_file)
946 else:
947 shutil.copy(sav_file, dse_ldif)
948
949
950 def _pattern_errorlog(file, log_pattern):
951 try:
952 _pattern_errorlog.last_pos += 1
953 except AttributeError:
954 _pattern_errorlog.last_pos = 0
955
956 found = None
957 log.debug("_pattern_errorlog: start at offset %d" % _pattern_errorlog.last_pos)
958 file.seek(_pattern_errorlog.last_pos)
959
960 while True:
961 line = file.readline()
962 log.debug("_pattern_errorlog: [%d] %s" % (file.tell(), line))
963 found = log_pattern.search(line)
964 if ((line == '') or (found)):
965 break
966
967 log.debug("_pattern_errorlog: end at offset %d" % file.tell())
968 _pattern_errorlog.last_pos = file.tell()
969 return found
970
971
972 def test_invalid_config_missing_attr_name(topology_st):
973 """Test that invalid plugin configuration is properly detected when attribute name is missing.
974
975 This test verifies that the attribute uniqueness plugin correctly fails
976 to start when the required attribute name configuration is missing,
977 and that appropriate error messages are logged.
978
979 :id: 2ff3aa56-304e-41b0-9614-eeb481ef6c55
980 :setup: Standalone instance
981 :steps:
982 1. Save current configuration
983 2. Create plugin configuration without uniqueness attribute name
984 3. Enable the plugin
985 4. Try to restart the server (should fail)
986 5. Verify expected error message in logs
987 6. Restore configuration and verify server can restart
988 :expectedresults:
989 1. Configuration saved successfully
990 2. Invalid plugin configuration created
991 3. Plugin enabled successfully
992 4. Server restart fails with appropriate error
993 5. Error message "Attribute name not defined" found in logs
994 6. Server restarts successfully with restored configuration
995 """
996
997 _config_file(topology_st, action='save')
998
999 attruniq = AttributeUniquenessPlugin(topology_st.standalone, dn="cn=attruniq,cn=plugins,cn=config")
1000 attruniq.create(properties={
1001 'cn': 'attruniq',
1002 'uniqueness-subtrees': DEFAULT_SUFFIX
1003 })
1004 attruniq.enable()
1005
1006 topology_st.standalone.errorlog_file = open(topology_st.standalone.errlog, "r")
1007
1008 with pytest.raises(subprocess.CalledProcessError):
1009 log.info("Server is down as expected due to invalid config")
1010 topology_st.standalone.restart()
1011
1012 log.info("Check the expected error message")
1013 regex = re.compile("[A|a]ttribute name not defined")
1014 res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
1015 assert res
1016
1017 log.info("Restore the configuration and verify the server can be restarted")
1018 _config_file(topology_st, action='restore')
1019 topology_st.standalone.restart()
1020
1021
1022 def test_invalid_config_invalid_subtree(topology_st):
1023 """Test that invalid plugin configuration is properly detected when subtree is invalid.
1024
1025 This test verifies that the attribute uniqueness plugin correctly fails
1026 to start when an invalid subtree DN is specified in the configuration,
1027 and that appropriate error messages are logged.
1028
1029 :id: b7e51131-8542-4110-a90a-3e5c88f75355
1030 :setup: Standalone instance
1031 :steps:
1032 1. Save current configuration
1033 2. Create plugin configuration with invalid subtree DN
1034 3. Enable the plugin
1035 4. Try to restart the server (should fail)
1036 5. Verify expected error message in logs
1037 6. Restore configuration and verify server can restart
1038 :expectedresults:
1039 1. Configuration saved successfully
1040 2. Invalid plugin configuration created with bad subtree
1041 3. Plugin enabled successfully
1042 4. Server restart fails with appropriate error
1043 5. Error message "No valid subtree is defined" found in logs
1044 6. Server restarts successfully with restored configuration
1045 """
1046
1047 _config_file(topology_st, action='save')
1048
1049 attruniq = AttributeUniquenessPlugin(topology_st.standalone, dn="cn=attruniq,cn=plugins,cn=config")
1050 attruniq.create(properties={
1051 'cn': 'attruniq',
1052 'uniqueness-attribute-name': 'cn',
1053 'uniqueness-subtrees': 'invalid_subtree'
1054 })
1055 attruniq.enable()
1056
1057 topology_st.standalone.errorlog_file = open(topology_st.standalone.errlog, "r")
1058
1059 with pytest.raises(subprocess.CalledProcessError):
1060 log.info("Server is down as expected due to invalid config")
1061 topology_st.standalone.restart()
1062
1063 log.info("Check the expected error message")
1064 regex = re.compile("No valid subtree is defined")
1065 res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
1066 assert res
1067
1068 log.info("Restore the configuration and verify the server can be restarted")
1069 _config_file(topology_st, action='restore')
1070 topology_st.standalone.restart()