| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2023 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 os |
| 10 |
import pytest |
| 11 |
import time |
| 12 |
import ldap |
| 13 |
import logging |
| 14 |
from test389.topologies import topology_st as topo |
| 15 |
from lib389.idm.user import UserAccounts, UserAccount |
| 16 |
from lib389.idm.account import Account |
| 17 |
from lib389._constants import DEFAULT_SUFFIX |
| 18 |
from lib389.idm.group import Groups |
| 19 |
|
| 20 |
from ..clu.dbmon_test import _set_dbsizes |
| 21 |
from lib389.config import Config |
| 22 |
from lib389.idm.organizationalunit import OrganizationalUnits, OrganizationalUnit |
| 23 |
from lib389.plugins import MEPTemplates, MEPConfigs, ManagedEntriesPlugin, MEPTemplate |
| 24 |
from lib389.idm.nscontainer import nsContainers |
| 25 |
from lib389.idm.domain import Domain |
| 26 |
from lib389.utils import get_default_db_lib |
| 27 |
|
| 28 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 29 |
log = logging.getLogger(__name__) |
| 30 |
|
| 31 |
pytestmark = pytest.mark.tier1 |
| 32 |
USER_PASSWORD = 'password' |
| 33 |
|
| 34 |
|
| 35 |
def _create_ou(inst, name): |
| 36 |
OrganizationalUnits(inst, DEFAULT_SUFFIX).create(properties={'ou': name}) |
| 37 |
|
| 38 |
|
| 39 |
def _create_template(inst, name): |
| 40 |
templates = MEPTemplates(inst, DEFAULT_SUFFIX) |
| 41 |
temp = templates.create(properties={ |
| 42 |
'cn': f"{name}_template", |
| 43 |
'mepRDNAttr': 'cn', |
| 44 |
'mepStaticAttr': ['objectclass: posixGroup', 'objectclass: top', f'description: {name}'], |
| 45 |
'mepMappedAttr': ['cn: $uid', 'memberUid: $uid', 'gidNumber: $uidNumber'] |
| 46 |
}) |
| 47 |
return temp.dn |
| 48 |
|
| 49 |
|
| 50 |
def _config_plugin(inst, name, template): |
| 51 |
configs = MEPConfigs(inst) |
| 52 |
configs.create(properties={ |
| 53 |
'cn': f"{name}_config", |
| 54 |
'originScope': f"ou=People,{DEFAULT_SUFFIX}", |
| 55 |
'originFilter': 'objectClass=posixAccount', |
| 56 |
'managedBase': f"ou={name},{DEFAULT_SUFFIX}", |
| 57 |
'managedTemplate': template}) |
| 58 |
|
| 59 |
|
| 60 |
def _create_user(inst, name): |
| 61 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 62 |
users.create(properties={ |
| 63 |
'uid': name, |
| 64 |
'cn': name, |
| 65 |
'sn': name, |
| 66 |
'uidNumber': '100', |
| 67 |
'gidNumber': '100', |
| 68 |
'homeDirectory': f"/home/{name}", |
| 69 |
}) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.xfail(reason='https://github.com/389ds/389-ds-base/issues/1870') |
| 73 |
def test_overlapping_scope(topo): |
| 74 |
"""Test overlapping scopes in Managed Entries |
| 75 |
|
| 76 |
:id: 7038ab53-89c8-4fce-897a-76d42ec85063 |
| 77 |
:setup: Standalone Instance |
| 78 |
:steps: |
| 79 |
1. Make the two subtrees for targets (Create organizational units `oua`, and `oub`). |
| 80 |
2. Add the template A (`oua`). |
| 81 |
3. Enable the plugin A (`oua`). |
| 82 |
4. Add the template B (`oub`). |
| 83 |
5. Enable the plugin B (`oub`). |
| 84 |
6. Add a user (`user1`). |
| 85 |
7. Search for the user to ensure it's present in both A and B. |
| 86 |
:expectedresults: |
| 87 |
1. Success |
| 88 |
2. Success |
| 89 |
3. Success |
| 90 |
4. Success |
| 91 |
5. Success |
| 92 |
6. Success |
| 93 |
7. Success |
| 94 |
""" |
| 95 |
|
| 96 |
inst = topo.standalone |
| 97 |
|
| 98 |
log.info("Creating organizational units") |
| 99 |
_create_ou(inst, 'oua') |
| 100 |
_create_ou(inst, 'oub') |
| 101 |
|
| 102 |
log.info("Creating template for 'oua'") |
| 103 |
dn = _create_template(inst, 'oua') |
| 104 |
|
| 105 |
log.info("Configuring plugin for 'oua'") |
| 106 |
_config_plugin(inst, 'oua', dn) |
| 107 |
|
| 108 |
log.info("Creating template for 'oub'") |
| 109 |
dn = _create_template(inst, 'oub') |
| 110 |
|
| 111 |
log.info("Configuring plugin for 'oub'") |
| 112 |
_config_plugin(inst, 'oub', dn) |
| 113 |
|
| 114 |
log.info("Creating user 'user1'") |
| 115 |
_create_user(inst, 'user1') |
| 116 |
|
| 117 |
log.info("Searching for user in both A and B") |
| 118 |
results = inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(&(cn=user1)(objectClass=posixGroup))', ['cn']) |
| 119 |
|
| 120 |
log.info(f"Found {len(results)} results for user search") |
| 121 |
assert(len(results) == 2) |
| 122 |
|
| 123 |
|
| 124 |
@pytest.fixture(scope="module") |
| 125 |
def _create_inital(topo): |
| 126 |
""" |
| 127 |
Will create entries for this module |
| 128 |
""" |
| 129 |
meps = MEPTemplates(topo.standalone, DEFAULT_SUFFIX) |
| 130 |
mep_template1 = meps.create( |
| 131 |
properties={'cn': 'UPG Template', 'mepRDNAttr': 'cn', 'mepStaticAttr': 'objectclass: posixGroup', |
| 132 |
'mepMappedAttr': 'cn: $uid|gidNumber: $gidNumber|description: User private group for $uid'.split( |
| 133 |
'|')}) |
| 134 |
conf_mep = MEPConfigs(topo.standalone) |
| 135 |
conf_mep.create(properties={'cn': 'UPG Definition1', 'originScope': f'cn=Users,{DEFAULT_SUFFIX}', |
| 136 |
'originFilter': 'objectclass=posixaccount', 'managedBase': f'cn=Groups,{DEFAULT_SUFFIX}', |
| 137 |
'managedTemplate': mep_template1.dn}) |
| 138 |
container = nsContainers(topo.standalone, DEFAULT_SUFFIX) |
| 139 |
for cn in ['Users', 'Groups']: |
| 140 |
container.create(properties={'cn': cn}) |
| 141 |
|
| 142 |
|
| 143 |
def test_managed_entry_betxn(topo): |
| 144 |
"""Test if failure to create a managed entry rolls back the transaction. |
| 145 |
|
| 146 |
:id: 7aa74994-f89b-11ec-9821-98fa9ba19b65 |
| 147 |
:setup: Standalone Instance |
| 148 |
:customerscenario: True |
| 149 |
:steps: |
| 150 |
1. Check that plugin active if not activate it |
| 151 |
2. Create a Template entry |
| 152 |
3. Create a definition entry |
| 153 |
4. Attempt to create a user |
| 154 |
5. Verify that transaction is aborted and user not created |
| 155 |
:expectedresults: |
| 156 |
1. Success |
| 157 |
2. Success |
| 158 |
3. Success |
| 159 |
4. Success |
| 160 |
5. Success |
| 161 |
""" |
| 162 |
|
| 163 |
log.info("Make sure the plugin is active") |
| 164 |
me_plugn = ManagedEntriesPlugin(topo.standalone) |
| 165 |
log.info("Stopping and starting the Managed Entry plugin.") |
| 166 |
me_plugn.disable() |
| 167 |
me_plugn.enable() |
| 168 |
assert me_plugn.status() |
| 169 |
log.info("Plugin Restarted.") |
| 170 |
log.info("Adding organization units") |
| 171 |
ous1 = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX) |
| 172 |
ou1_people = ous1.create(properties={'ou': 'tst_people'}) |
| 173 |
ou1_groups = ous1.create(properties={'ou': 'tst_groups'}) |
| 174 |
|
| 175 |
log.info("Create the template entry") |
| 176 |
mep_templates1 = MEPTemplates(topo.standalone, DEFAULT_SUFFIX) |
| 177 |
mep_temp1 = mep_templates1.create(properties={ |
| 178 |
'cn': 'MEP template1', |
| 179 |
'mepRDNAttr': 'cn', |
| 180 |
'mepStaticAttr': 'objectclass: groupOfNames|objectclass: extensibleObject'.split('|'), |
| 181 |
'mepMappedAttr': 'cn: $cn|uid: $cn|gidNumber: $uidNumber'.split('|') |
| 182 |
}) |
| 183 |
conf_mep = MEPConfigs(topo.standalone) |
| 184 |
log.info("Create definition entry.") |
| 185 |
conf_mep.create(properties={ |
| 186 |
'cn': 'cn=config', |
| 187 |
'originScope': ou1_people.dn, |
| 188 |
'originFilter': 'objectclass=posixAccount', |
| 189 |
'managedBase': ou1_groups.dn, |
| 190 |
'managedTemplate': mep_temp1.dn}) |
| 191 |
topo.standalone.restart() |
| 192 |
log.info("Attempt to add a user that doenst fit the template.") |
| 193 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou={}'.format(ou1_people.rdn)) |
| 194 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 195 |
mgd_entry = users.create(properties={ |
| 196 |
'uid': 'test_uid', |
| 197 |
'cn': 'test_uid', |
| 198 |
'sn': 'test_sn', |
| 199 |
}) |
| 200 |
mgd_entry.delete() |
| 201 |
log.info("Cleaning up") |
| 202 |
|
| 203 |
|
| 204 |
def test_binddn_tracking(topo, _create_inital): |
| 205 |
"""Test Managed Entries basic functionality |
| 206 |
|
| 207 |
:id: ea2ddfd4-aaec-11ea-8416-8c16451d917b |
| 208 |
:setup: Standalone Instance |
| 209 |
:steps: |
| 210 |
1. Set nsslapd-plugin-binddn-tracking attribute under cn=config |
| 211 |
2. Add user |
| 212 |
3. Managed Entry Plugin runs against managed entries upon any update without validating |
| 213 |
4. verify creation of User Private Group with its time stamp value |
| 214 |
5. Modify the SN attribute which is not mapped with managed entry |
| 215 |
6. run ModRDN operation and check the User Private group |
| 216 |
7. Check the time stamp of UPG should be changed now |
| 217 |
8. Check the creatorsname should be user dn and internalCreatorsname should be plugin name |
| 218 |
9. Check if a managed group entry was created |
| 219 |
:expectedresults: |
| 220 |
1. Success |
| 221 |
2. Success |
| 222 |
3. Success |
| 223 |
4. Success |
| 224 |
5. Success |
| 225 |
6. Success |
| 226 |
7. Success |
| 227 |
8. Success |
| 228 |
9. Success |
| 229 |
""" |
| 230 |
config = Config(topo.standalone) |
| 231 |
# set nsslapd-plugin-binddn-tracking attribute under cn=config |
| 232 |
config.replace('nsslapd-plugin-binddn-tracking', 'on') |
| 233 |
# Add user |
| 234 |
user = UserAccounts(topo.standalone, f'cn=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 235 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=test_user_1000,cn=Groups,{DEFAULT_SUFFIX}' |
| 236 |
entry = Account(topo.standalone, f'cn=test_user_1000,cn=Groups,{DEFAULT_SUFFIX}') |
| 237 |
# Managed Entry Plugin runs against managed entries upon any update without validating |
| 238 |
# verify creation of User Private Group with its time stamp value |
| 239 |
stamp1 = entry.get_attr_val_utf8('modifyTimestamp') |
| 240 |
user.replace('sn', 'NewSN_modified') |
| 241 |
stamp2 = entry.get_attr_val_utf8('modifyTimestamp') |
| 242 |
# Modify the SN attribute which is not mapped with managed entry |
| 243 |
# Check the time stamp of UPG should not be changed |
| 244 |
assert stamp1 == stamp2 |
| 245 |
time.sleep(1) |
| 246 |
# run ModRDN operation and check the User Private group |
| 247 |
user.rename(new_rdn='uid=UserNewRDN', newsuperior='cn=Users,dc=example,dc=com') |
| 248 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=UserNewRDN,cn=Groups,{DEFAULT_SUFFIX}' |
| 249 |
entry = Account(topo.standalone, f'cn=UserNewRDN,cn=Groups,{DEFAULT_SUFFIX}') |
| 250 |
stamp3 = entry.get_attr_val_utf8('modifyTimestamp') |
| 251 |
# Check the time stamp of UPG should be changed now |
| 252 |
assert stamp2 != stamp3 |
| 253 |
time.sleep(1) |
| 254 |
user.replace('gidNumber', '1') |
| 255 |
stamp4 = entry.get_attr_val_utf8('modifyTimestamp') |
| 256 |
assert stamp4 != stamp3 |
| 257 |
# Check the creatorsname should be user dn and internalCreatorsname should be plugin name |
| 258 |
assert entry.get_attr_val_utf8('creatorsname') == 'cn=directory manager' |
| 259 |
assert entry.get_attr_val_utf8('internalCreatorsname') == 'cn=Managed Entries,cn=plugins,cn=config' |
| 260 |
assert entry.get_attr_val_utf8('modifiersname') == 'cn=directory manager' |
| 261 |
user.delete() |
| 262 |
config.replace('nsslapd-plugin-binddn-tracking', 'off') |
| 263 |
|
| 264 |
|
| 265 |
class WithObjectClass(Account): |
| 266 |
def __init__(self, instance, dn=None): |
| 267 |
super(WithObjectClass, self).__init__(instance, dn) |
| 268 |
self._rdn_attribute = 'uid' |
| 269 |
self._create_objectclasses = ['top', 'person', 'inetorgperson'] |
| 270 |
|
| 271 |
|
| 272 |
def test_mentry01(topo, _create_inital): |
| 273 |
"""Test Managed Entries basic functionality |
| 274 |
|
| 275 |
:id: 863678bb-9383-42cf-b2a8-8763f4908650 |
| 276 |
:setup: Standalone Instance |
| 277 |
:steps: |
| 278 |
1. Check the plug-in status |
| 279 |
2. Add Template and definition entry |
| 280 |
3. Add our org units |
| 281 |
4. Add users with PosixAccount ObjectClass and verify creation of User Private Group |
| 282 |
5. Disable the plug-in and check the status |
| 283 |
6. Enable the plug-in and check the status the plug-in is disabled and creation of UPG should fail |
| 284 |
7. Add users with PosixAccount ObjectClass and verify creation of User Private Group |
| 285 |
8. Run ModRDN with deleteoldrdn and a leading plus in uid (e.g. uid=+newname), and verify |
| 286 |
uid/mepManagedEntry updates are correct |
| 287 |
9. Add users, run ModRDN operation and check the User Private group |
| 288 |
10. Add users, run LDAPMODIFY to change the gidNumber and check the User Private group |
| 289 |
11. Checking whether creation of User Private group fails for existing group entry |
| 290 |
12. Checking whether adding of posixAccount objectClass to existing user creates UPG |
| 291 |
13. Running ModRDN operation and checking the user private groups mepManagedBy attribute |
| 292 |
14. Deleting mepManagedBy attribute and running ModRDN operation to check if it creates a new UPG |
| 293 |
15. Change the RDN of template entry, DSA Unwilling to perform error expected |
| 294 |
16. Change the RDN of cn=Users to cn=TestUsers and check UPG are deleted |
| 295 |
:expectedresults: |
| 296 |
1. Success |
| 297 |
2. Success |
| 298 |
3. Success |
| 299 |
4. Success |
| 300 |
5. Success |
| 301 |
6. Success |
| 302 |
7. Success |
| 303 |
8. Success |
| 304 |
9. Success |
| 305 |
10. Success |
| 306 |
11. Success |
| 307 |
12. Success |
| 308 |
13. Success |
| 309 |
14. Success |
| 310 |
15. Fail(Unwilling to perform ) |
| 311 |
16. Success |
| 312 |
""" |
| 313 |
# Check the plug-in status |
| 314 |
mana = ManagedEntriesPlugin(topo.standalone) |
| 315 |
assert mana.status() |
| 316 |
|
| 317 |
# Add Template and definition entry |
| 318 |
org1 = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX).create(properties={'ou': 'Users'}) |
| 319 |
org2 = OrganizationalUnit(topo.standalone, f'ou=Groups,{DEFAULT_SUFFIX}') |
| 320 |
meps = MEPTemplates(topo.standalone, DEFAULT_SUFFIX) |
| 321 |
mep_template1 = meps.create(properties={ |
| 322 |
'cn': 'UPG Template1', |
| 323 |
'mepRDNAttr': 'cn', |
| 324 |
'mepStaticAttr': 'objectclass: posixGroup', |
| 325 |
'mepMappedAttr': 'cn: $uid|gidNumber: $gidNumber|description: User private group for $uid'.split('|')}) |
| 326 |
conf_mep = MEPConfigs(topo.standalone) |
| 327 |
conf_mep.create(properties={ |
| 328 |
'cn': 'UPG Definition2', |
| 329 |
'originScope': org1.dn, |
| 330 |
'originFilter': 'objectclass=posixaccount', |
| 331 |
'managedBase': org2.dn, |
| 332 |
'managedTemplate': mep_template1.dn}) |
| 333 |
|
| 334 |
# Add users with PosixAccount ObjectClass and verify creation of User Private Group |
| 335 |
user = UserAccounts(topo.standalone, f'ou=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 336 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=test_user_1000,ou=Groups,{DEFAULT_SUFFIX}' |
| 337 |
|
| 338 |
# Disable the plug-in and check the status |
| 339 |
mana.disable() |
| 340 |
user.delete() |
| 341 |
topo.standalone.restart() |
| 342 |
|
| 343 |
# Add users with PosixAccount ObjectClass when the plug-in is disabled and creation of UPG should fail |
| 344 |
user = UserAccounts(topo.standalone, f'ou=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 345 |
assert not user.get_attr_val_utf8('mepManagedEntry') |
| 346 |
|
| 347 |
# Enable the plug-in and check the status |
| 348 |
mana.enable() |
| 349 |
user.delete() |
| 350 |
topo.standalone.restart() |
| 351 |
|
| 352 |
# Add users with PosixAccount ObjectClass and verify creation of User Private Group |
| 353 |
user = UserAccounts(topo.standalone, f'ou=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 354 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=test_user_1000,ou=Groups,{DEFAULT_SUFFIX}' |
| 355 |
|
| 356 |
# Run ModRDN operation with deleteoldrdn and a leading plus in the new uid |
| 357 |
user.rename(new_rdn=r'uid=\+newname', deloldrdn=True) |
| 358 |
assert 'test_user_1000' not in user.get_attr_val_utf8('uid') |
| 359 |
assert '+newname' in user.get_attr_val_utf8('uid') |
| 360 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=\\2Bnewname,ou=Groups,{DEFAULT_SUFFIX}' |
| 361 |
|
| 362 |
# Add users, run ModRDN operation and check the User Private group |
| 363 |
# Add users, run LDAPMODIFY to change the gidNumber and check the User Private group |
| 364 |
user.rename(new_rdn='uid=UserNewRDN', newsuperior='ou=Users,dc=example,dc=com') |
| 365 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=UserNewRDN,ou=Groups,{DEFAULT_SUFFIX}' |
| 366 |
user.replace('gidNumber', '20209') |
| 367 |
entry = Account(topo.standalone, f'cn=UserNewRDN,ou=Groups,{DEFAULT_SUFFIX}') |
| 368 |
assert entry.get_attr_val_utf8('gidNumber') == '20209' |
| 369 |
user.replace_many(('sn', 'new_modified_sn'), ('gidNumber', '31309')) |
| 370 |
assert entry.get_attr_val_utf8('gidNumber') == '31309' |
| 371 |
user.delete() |
| 372 |
|
| 373 |
# Checking whether creation of User Private group fails for existing group entry |
| 374 |
Groups(topo.standalone, f'ou=Groups,{DEFAULT_SUFFIX}', rdn=None).create(properties={'cn': 'MENTRY_14'}) |
| 375 |
user = UserAccounts(topo.standalone, f'ou=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 376 |
with pytest.raises(ldap.NO_SUCH_OBJECT): |
| 377 |
entry.status() |
| 378 |
user.delete() |
| 379 |
|
| 380 |
# Checking whether adding of posixAccount objectClass to existing user creates UPG |
| 381 |
# Add Users without posixAccount objectClass |
| 382 |
users = WithObjectClass(topo.standalone, f'uid=test_test, ou=Users,{DEFAULT_SUFFIX}') |
| 383 |
user_properties1 = {'uid': 'test_test', 'cn': 'test', 'sn': 'test', 'mail': '[email protected]', 'telephoneNumber': '123'} |
| 384 |
user = users.create(properties=user_properties1) |
| 385 |
assert not user.get_attr_val_utf8('mepManagedEntry') |
| 386 |
|
| 387 |
# Add posixAccount objectClass |
| 388 |
user.replace_many(('objectclass', ['top', 'person', 'inetorgperson', 'posixAccount']), |
| 389 |
('homeDirectory', '/home/ok'), |
| 390 |
('uidNumber', '61603'), ('gidNumber', '61603')) |
| 391 |
assert not user.get_attr_val_utf8('mepManagedEntry') |
| 392 |
user = UserAccounts(topo.standalone, f'ou=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 393 |
entry = Account(topo.standalone, 'cn=test_user_1000,ou=Groups,dc=example,dc=com') |
| 394 |
|
| 395 |
# Add inetuser objectClass |
| 396 |
user.replace_many( |
| 397 |
('objectclass', ['top', 'account', 'posixaccount', 'inetOrgPerson', |
| 398 |
'organizationalPerson', 'nsMemberOf', 'nsAccount', |
| 399 |
'person', 'mepOriginEntry', 'inetuser']), |
| 400 |
('memberOf', entry.dn)) |
| 401 |
assert entry.status() |
| 402 |
user.delete() |
| 403 |
user = UserAccounts(topo.standalone, f'ou=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 404 |
entry = Account(topo.standalone, 'cn=test_user_1000,ou=Groups,dc=example,dc=com') |
| 405 |
|
| 406 |
# Add groupofNames objectClass |
| 407 |
user.replace_many( |
| 408 |
('objectclass', ['top', 'account', 'posixaccount', 'inetOrgPerson', |
| 409 |
'organizationalPerson', 'nsMemberOf', 'nsAccount', |
| 410 |
'person', 'mepOriginEntry', 'groupofNames']), |
| 411 |
('memberOf', user.dn)) |
| 412 |
assert entry.status() |
| 413 |
|
| 414 |
# Running ModRDN operation and checking the user private groups mepManagedBy |
| 415 |
# attribute was also reset because the modrdn on the origin will do a modrdn |
| 416 |
# on checkManagedEntry to match the new rdn value of the origin entry |
| 417 |
checkManagedEntry = UserAccounts(topo.standalone, f'ou=Groups,{DEFAULT_SUFFIX}', rdn=None) |
| 418 |
check_entry = checkManagedEntry.create(properties={ |
| 419 |
'objectclass': ['top', 'extensibleObject'], |
| 420 |
'uid': 'CheckModRDN', |
| 421 |
'uidNumber': '12', |
| 422 |
'gidNumber': '12', |
| 423 |
'homeDirectory': '/home', |
| 424 |
'sn': 'tmp', |
| 425 |
'cn': 'tmp', |
| 426 |
}) |
| 427 |
user.replace('mepManagedEntry', check_entry.dn) |
| 428 |
user.rename(new_rdn='uid=UserNewRDN', newsuperior='ou=Users,dc=example,dc=com') |
| 429 |
assert user.get_attr_val_utf8_l('mepManagedEntry') == f'cn=UserNewRDN,ou=Groups,{DEFAULT_SUFFIX}'.lower() |
| 430 |
|
| 431 |
# Deleting mepManagedBy attribute and running ModRDN operation to check if it creates a new UPG |
| 432 |
user.remove('mepManagedEntry', f'cn=UserNewRDN,ou=Groups,{DEFAULT_SUFFIX}') |
| 433 |
user.rename(new_rdn='uid=UserNewRDN1', newsuperior='ou=Users,dc=example,dc=com') |
| 434 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=UserNewRDN1,ou=Groups,{DEFAULT_SUFFIX}' |
| 435 |
|
| 436 |
# Change the RDN of template entry, DSA Unwilling to perform error expected |
| 437 |
mep = MEPTemplate(topo.standalone, f'cn=UPG Template,{DEFAULT_SUFFIX}') |
| 438 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 439 |
mep.rename(new_rdn='cn=UPG Template2', newsuperior='dc=example,dc=com') |
| 440 |
|
| 441 |
# Change the RDN of cn=Users to cn=TestUsers and check UPG are deleted |
| 442 |
before = user.get_attr_val_utf8('mepManagedEntry') |
| 443 |
user.rename(new_rdn='uid=Anuj', newsuperior='ou=Users,dc=example,dc=com') |
| 444 |
assert user.get_attr_val_utf8('mepManagedEntry') != before |
| 445 |
|
| 446 |
|
| 447 |
def test_managed_entry_removal(topo): |
| 448 |
"""Check that we can't remove managed entry manually |
| 449 |
|
| 450 |
:id: cf9c5be5-97ef-46fc-b199-8346acf4c296 |
| 451 |
:setup: Standalone Instance |
| 452 |
:steps: |
| 453 |
1. Enable the plugin |
| 454 |
2. Restart the instance |
| 455 |
3. Add our org units |
| 456 |
4. Set up config entry and template entry for the org units |
| 457 |
5. Add an entry that meets the MEP scope |
| 458 |
6. Check if a managed group entry was created |
| 459 |
7. Try to remove the entry while bound as Admin (non-DM) |
| 460 |
8. Remove the entry while bound as DM |
| 461 |
9. Check that the managing entry can be deleted too |
| 462 |
:expectedresults: |
| 463 |
1. Success |
| 464 |
2. Success |
| 465 |
3. Success |
| 466 |
4. Success |
| 467 |
5. Success |
| 468 |
6. Success |
| 469 |
7. Should fail |
| 470 |
8. Success |
| 471 |
9. Success |
| 472 |
""" |
| 473 |
|
| 474 |
inst = topo.standalone |
| 475 |
|
| 476 |
# Add ACI so we can test that non-DM user can't delete managed entry |
| 477 |
domain = Domain(inst, DEFAULT_SUFFIX) |
| 478 |
ACI_TARGET = f"(target = \"ldap:///{DEFAULT_SUFFIX}\")" |
| 479 |
ACI_TARGETATTR = "(targetattr = *)" |
| 480 |
ACI_ALLOW = "(version 3.0; acl \"Admin Access\"; allow (all) " |
| 481 |
ACI_SUBJECT = "(userdn = \"ldap:///anyone\");)" |
| 482 |
ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_ALLOW + ACI_SUBJECT |
| 483 |
domain.add('aci', ACI_BODY) |
| 484 |
|
| 485 |
# stop the plugin, and start it |
| 486 |
plugin = ManagedEntriesPlugin(inst) |
| 487 |
plugin.disable() |
| 488 |
plugin.enable() |
| 489 |
|
| 490 |
# Add our org units |
| 491 |
ous = OrganizationalUnits(inst, DEFAULT_SUFFIX) |
| 492 |
ou_people = ous.create(properties={'ou': 'managed_people'}) |
| 493 |
ou_groups = ous.create(properties={'ou': 'managed_groups'}) |
| 494 |
|
| 495 |
mep_templates = MEPTemplates(inst, DEFAULT_SUFFIX) |
| 496 |
mep_template1 = mep_templates.create(properties={ |
| 497 |
'cn': 'MEP template', |
| 498 |
'mepRDNAttr': 'cn', |
| 499 |
'mepStaticAttr': 'objectclass: groupOfNames|objectclass: extensibleObject'.split('|'), |
| 500 |
'mepMappedAttr': 'cn: $cn|uid: $cn|gidNumber: $uidNumber'.split('|') |
| 501 |
}) |
| 502 |
mep_configs = MEPConfigs(inst) |
| 503 |
mep_configs.create(properties={'cn': 'config', |
| 504 |
'originScope': ou_people.dn, |
| 505 |
'originFilter': 'objectclass=posixAccount', |
| 506 |
'managedBase': ou_groups.dn, |
| 507 |
'managedTemplate': mep_template1.dn}) |
| 508 |
inst.restart() |
| 509 |
|
| 510 |
# Add an entry that meets the MEP scope |
| 511 |
test_users_m1 = UserAccounts(inst, DEFAULT_SUFFIX, rdn='ou={}'.format(ou_people.rdn)) |
| 512 |
managing_entry = test_users_m1.create_test_user(1001) |
| 513 |
managing_entry.reset_password(USER_PASSWORD) |
| 514 |
user_bound_conn = managing_entry.bind(USER_PASSWORD) |
| 515 |
|
| 516 |
# Get the managed entry |
| 517 |
managed_groups = Groups(inst, ou_groups.dn, rdn=None) |
| 518 |
managed_entry = managed_groups.get(managing_entry.rdn) |
| 519 |
|
| 520 |
# Check that the managed entry was created |
| 521 |
assert managed_entry.exists() |
| 522 |
|
| 523 |
# Try to remove the entry while bound as Admin (non-DM) |
| 524 |
managed_groups_user_conn = Groups(user_bound_conn, ou_groups.dn, rdn=None) |
| 525 |
managed_entry_user_conn = managed_groups_user_conn.get(managed_entry.rdn) |
| 526 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 527 |
managed_entry_user_conn.delete() |
| 528 |
assert managed_entry_user_conn.exists() |
| 529 |
|
| 530 |
# Remove the entry while bound as DM |
| 531 |
managed_entry.delete() |
| 532 |
assert not managed_entry.exists() |
| 533 |
|
| 534 |
# Check that the managing entry can be deleted too |
| 535 |
managing_entry.delete() |
| 536 |
assert not managing_entry.exists() |
| 537 |
|
| 538 |
|
| 539 |
MAX_ACCOUNTS = 2 |
| 540 |
|
| 541 |
|
| 542 |
@pytest.mark.skipif( |
| 543 |
get_default_db_lib() == "mdb", |
| 544 |
reason="nsslapd-db-page-size is BDB-specific; ticket 47976 scenario uses BDB", |
| 545 |
) |
| 546 |
def test_managed_entry_delete_after_reimport(topo, _create_inital): |
| 547 |
"""User delete must not hang after reimport with large page size. |
| 548 |
|
| 549 |
With MEP (posixAccount -> posixGroup), reimporting the DB with a very large page size |
| 550 |
can place a user and its managed group on the same page. Deleting the user then |
| 551 |
triggers MEP post-op to delete the group causing a deadlock. |
| 552 |
|
| 553 |
:id: a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d |
| 554 |
:setup: Standalone Instance |
| 555 |
:steps: |
| 556 |
1. Create two users under cn=Users (MEP creates private groups in cn=Groups) |
| 557 |
2. Set BDB page size and reimport the DB (user + group on same page) |
| 558 |
3. Delete each user with short timeout |
| 559 |
4. Verify users are deleted |
| 560 |
:expectedresults: |
| 561 |
1. Success |
| 562 |
2. Success |
| 563 |
3. Should not hang |
| 564 |
4. Success |
| 565 |
""" |
| 566 |
inst = topo.standalone |
| 567 |
|
| 568 |
users_ou = f"cn=Users,{DEFAULT_SUFFIX}" |
| 569 |
|
| 570 |
# Create users (MEP creates private groups in cn=Groups) |
| 571 |
users = UserAccounts(inst, DEFAULT_SUFFIX, rdn="cn=Users") |
| 572 |
for i in range(MAX_ACCOUNTS): |
| 573 |
users.create( |
| 574 |
rdn=f"uid=user{i}", |
| 575 |
properties={ |
| 576 |
"uid": f"user{i}", |
| 577 |
"cn": f"user{i}", |
| 578 |
"sn": f"user{i}", |
| 579 |
"uidNumber": "1", |
| 580 |
"gidNumber": "1", |
| 581 |
"homeDirectory": f"/home/user{i}", |
| 582 |
}, |
| 583 |
) |
| 584 |
|
| 585 |
# Set large BDB page size and reimport DB so user + managed group can end up on same page |
| 586 |
_set_dbsizes(inst, 64 * 1024, 80960) |
| 587 |
|
| 588 |
# Delete users with short timeout; must not hang |
| 589 |
orig_timeout = inst.get_option(ldap.OPT_TIMEOUT) |
| 590 |
inst.set_option(ldap.OPT_TIMEOUT, 5) |
| 591 |
|
| 592 |
try: |
| 593 |
for i in range(MAX_ACCOUNTS): |
| 594 |
name = f"user{i}" |
| 595 |
user_dn = f"uid={name},{users_ou}" |
| 596 |
user = UserAccount(inst, user_dn) |
| 597 |
try: |
| 598 |
user.delete() |
| 599 |
except ldap.TIMEOUT: |
| 600 |
log.fatal("Timeout on delete") |
| 601 |
pytest.fail("Delete hung") |
| 602 |
|
| 603 |
# Verify users are gone |
| 604 |
for i in range(MAX_ACCOUNTS): |
| 605 |
name = f"user{i}" |
| 606 |
user_dn = f"uid={name},{users_ou}" |
| 607 |
user = UserAccount(inst, user_dn) |
| 608 |
assert not user.exists(), f"Expected {user_dn} to be deleted" |
| 609 |
log.info(f"{name} was correctly deleted") |
| 610 |
finally: |
| 611 |
inst.set_option(ldap.OPT_TIMEOUT, orig_timeout) |
| 612 |
|
| 613 |
|
| 614 |
if __name__ == '__main__': |
| 615 |
# Run isolated |
| 616 |
# -s for DEBUG mode |
| 617 |
CURRENT_FILE = os.path.realpath(__file__) |
| 618 |
pytest.main("-s %s" % CURRENT_FILE) |