| 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 logging |
| 10 |
import pytest |
| 11 |
import os |
| 12 |
import ldap |
| 13 |
from lib389._constants import DEFAULT_SUFFIX |
| 14 |
from test389.topologies import topology_st as topo |
| 15 |
from lib389.idm.user import UserAccounts |
| 16 |
from lib389.idm.group import Groups |
| 17 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 18 |
from lib389.config import LDBMConfig |
| 19 |
|
| 20 |
|
| 21 |
pytestmark = pytest.mark.tier1 |
| 22 |
|
| 23 |
log = logging.getLogger(__name__) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(scope="function") |
| 27 |
def dynamic_lists_setup(topo, request): |
| 28 |
log.info("Creating test users") |
| 29 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 30 |
user_dns = [] |
| 31 |
num_users = 5 |
| 32 |
|
| 33 |
for i in range(1, num_users + 1): |
| 34 |
user = users.create(properties={ |
| 35 |
'uid': f'testuser{i}', |
| 36 |
'cn': f'testuser{i}', |
| 37 |
'sn': f'User{i}', |
| 38 |
'uidNumber': f'{1000 + i}', |
| 39 |
'gidNumber': f'{1000 + i}', |
| 40 |
'homeDirectory': f'/home/testuser{i}', |
| 41 |
}) |
| 42 |
user_dns.append(user.dn) |
| 43 |
log.info(f"Created user: {user.dn}") |
| 44 |
|
| 45 |
def fin(): |
| 46 |
for user in users.list(): |
| 47 |
user.delete() |
| 48 |
request.addfinalizer(fin) |
| 49 |
|
| 50 |
return user_dns |
| 51 |
|
| 52 |
|
| 53 |
def test_dynamic_lists_memberurl_all_users(topo, dynamic_lists_setup): |
| 54 |
"""Test dynamic lists plugin with memberURL filter (uid=testuser*) to list all users |
| 55 |
|
| 56 |
:id: 6913fc98-fb22-4f21-817d-4cc19be1f1cd |
| 57 |
:setup: Standalone Instance |
| 58 |
:steps: |
| 59 |
1. Configure dynamic lists with objectclass, url_attr, and list_attr |
| 60 |
2. Create a group with memberURL attribute containing LDAP URL with filter (uid=testuser*) |
| 61 |
3. Search for the group and verify all users are listed as members |
| 62 |
4. Search using member as a filter and see if the group is returned |
| 63 |
:expectedresults: |
| 64 |
1. Success |
| 65 |
2. Group should be created with memberURL successfully |
| 66 |
3. All users should be listed as members of the group |
| 67 |
4. Group should be returned when searching using member as a filter |
| 68 |
""" |
| 69 |
|
| 70 |
inst = topo.standalone |
| 71 |
|
| 72 |
user_dns = dynamic_lists_setup |
| 73 |
|
| 74 |
# Step 1: Configure dynamic lists with objectclass, url_attr, and list_attr |
| 75 |
ldbmconfig = LDBMConfig(inst) |
| 76 |
ldbmconfig.replace('nsslapd-dynamic-lists-enabled', b'on') |
| 77 |
ldbmconfig.replace('nsslapd-dynamic-lists-attr', b'member') |
| 78 |
ldbmconfig.replace('nsslapd-dynamic-lists-oc', b'groupOfUrls') |
| 79 |
ldbmconfig.replace('nsslapd-dynamic-lists-url-attr', b'memberURL') |
| 80 |
|
| 81 |
# Step 2: Create a group with memberURL attribute |
| 82 |
log.info("Creating group with memberURL") |
| 83 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 84 |
group = groups.create(properties={ |
| 85 |
'cn': 'test_dynamic_group', |
| 86 |
'objectClass': ['groupOfUrls','groupOfNames'], |
| 87 |
'memberURL': f'ldap:///ou=people,{DEFAULT_SUFFIX}??sub?(uid=testuser*)', |
| 88 |
}) |
| 89 |
group_dn = group.dn |
| 90 |
log.info(f"Created group: {group_dn} with memberURL: ldap:///{DEFAULT_SUFFIX}??sub?(uid=*)") |
| 91 |
|
| 92 |
# Step 3: Search for the group to trigger the plugin and verify all users |
| 93 |
# are listed as members. The plugin processes memberURL during search |
| 94 |
# operations and dynamically adds member attributes |
| 95 |
log.info("Searching for group to trigger dynamic lists plugin") |
| 96 |
members = group.list_members() |
| 97 |
log.info(f"Found {len(members)} members in group") |
| 98 |
log.info(f"Members: {members}") |
| 99 |
|
| 100 |
# Verify all users are members |
| 101 |
assert len(members) == 5, f"Expected {5} members, got {len(members)}. Members: {members}" |
| 102 |
for user_dn in user_dns: |
| 103 |
assert user_dn in members, f"User {user_dn} should be a member of the group. Members: {members}" |
| 104 |
|
| 105 |
# Step 4: Now search using member as a filter and see if the group is returned |
| 106 |
results = groups.filter(f'(member={members[2]})') |
| 107 |
assert len(results) == 1, f"Expected 1 group, got {len(results)}. Results: {results}" |
| 108 |
assert results[0].dn == group_dn, (f"Group {results[0].dn} should be returned when searching " |
| 109 |
"using member as a filter. Results: {results}") |
| 110 |
|
| 111 |
log.info("Test completed successfully - all users are listed as members " |
| 112 |
"and the group is returned when searching using member as a filter") |
| 113 |
|
| 114 |
|
| 115 |
def test_dynamic_lists_non_dn_attribute(topo, dynamic_lists_setup): |
| 116 |
"""Test dynamic lists plugin with memberURL filter (uid=testuser*) to list |
| 117 |
all users homeDirectory attribute |
| 118 |
|
| 119 |
:id: c44ebc24-4fd5-45f7-9699-b481527821f0 |
| 120 |
:setup: Standalone Instance |
| 121 |
:steps: |
| 122 |
1. Configure dynamic lists with objectclass, url_attr, and list_attr |
| 123 |
2. Create a group with memberURL attribute containing LDAP URL with |
| 124 |
filter (uid=testuser*) and requested attribute (homeDirectory) |
| 125 |
3. Search for the group and verify all users homeDirectory attribute |
| 126 |
are listed in the entry |
| 127 |
:expectedresults: |
| 128 |
1. Success |
| 129 |
2. Group should be created with memberURL successfully |
| 130 |
3. All users homeDirectory attribute should be listed in the entry |
| 131 |
""" |
| 132 |
|
| 133 |
inst = topo.standalone |
| 134 |
|
| 135 |
# Step 1: Configure dynamic lists with objectclass, url_attr, and list_attr |
| 136 |
ldbmconfig = LDBMConfig(inst) |
| 137 |
ldbmconfig.replace('nsslapd-dynamic-lists-enabled', b'on') |
| 138 |
ldbmconfig.replace('nsslapd-dynamic-lists-attr', b'member') |
| 139 |
ldbmconfig.replace('nsslapd-dynamic-lists-oc', b'groupOfUrls') |
| 140 |
ldbmconfig.replace('nsslapd-dynamic-lists-url-attr', b'memberURL') |
| 141 |
|
| 142 |
# Step 2: Create a group with memberURL attribute |
| 143 |
log.info("Creating group with memberURL") |
| 144 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 145 |
group = groups.create(properties={ |
| 146 |
'cn': 'test_dynamic_group_2', |
| 147 |
'objectClass': 'groupOfUrls', |
| 148 |
'memberURL': f'ldap:///{DEFAULT_SUFFIX}?homeDirectory?sub?(uid=testuser*)', |
| 149 |
}) |
| 150 |
group_dn = group.dn |
| 151 |
log.info(f"Created group: {group_dn} with memberURL: ldap:///{DEFAULT_SUFFIX}?" |
| 152 |
"homeDirectory?sub?(uid=testuser*)") |
| 153 |
|
| 154 |
# Step 3: Search for the group to trigger the plugin and verify all |
| 155 |
# homeDirectories are listed in the entry |
| 156 |
values = group.get_attr_vals_utf8_l('homeDirectory') |
| 157 |
assert len(values) == 5, f"Expected {5} values, got {len(values)}. Values: {values}" |
| 158 |
for value in ['/home/testuser1', '/home/testuser2', '/home/testuser3', |
| 159 |
'/home/testuser4', '/home/testuser5']: |
| 160 |
assert value in values, f"Value {value} should be listed in the group. Values: {values}" |
| 161 |
|
| 162 |
|
| 163 |
def test_dynamic_lists_invalid_values(topo): |
| 164 |
"""Test dynamic lists plugin with memberURL filter (uid=testuser*) to list |
| 165 |
all users homeDirectory attribute |
| 166 |
|
| 167 |
:id: bbe98cdc-2255-45af-a40d-442d3995a388 |
| 168 |
:setup: Standalone Instance |
| 169 |
:steps: |
| 170 |
1. Configure dynamic lists with objectclass, url_attr, and list_attr |
| 171 |
2. Configure dynamic lists with non-existing objectclass |
| 172 |
3. Configure dynamic lists with non-existing url_attr |
| 173 |
4. Configure dynamic lists with non-existing list_attr |
| 174 |
5. Configure dynamic lists with non-dn syntax attribute for list-attr |
| 175 |
:expectedresults: |
| 176 |
1. Plugin should be enabled successfully |
| 177 |
2. Plugin should not be configured successfully |
| 178 |
3. Plugin should not be configured successfully |
| 179 |
4. Plugin should not be configured successfully |
| 180 |
5. Plugin should not be configured successfully |
| 181 |
6. Plugin should not be configured successfully |
| 182 |
""" |
| 183 |
|
| 184 |
inst = topo.standalone |
| 185 |
|
| 186 |
# Step 1: Configure dynamic lists with objectclass, url_attr, and list_attr |
| 187 |
ldbmconfig = LDBMConfig(inst) |
| 188 |
ldbmconfig.replace('nsslapd-dynamic-lists-enabled', b'on') |
| 189 |
ldbmconfig.replace('nsslapd-dynamic-lists-attr', b'member') |
| 190 |
ldbmconfig.replace('nsslapd-dynamic-lists-oc', b'groupOfUrls') |
| 191 |
ldbmconfig.replace('nsslapd-dynamic-lists-url-attr', b'memberURL') |
| 192 |
|
| 193 |
# Step 2: Configure dynamic lists with non-existing objectclass |
| 194 |
log.info("Configuring dynamic lists plugin") |
| 195 |
with pytest.raises(ldap.LDAPError) as e: |
| 196 |
ldbmconfig.replace('nsslapd-dynamic-lists-oc', b'doesNotExist') |
| 197 |
|
| 198 |
# Step 3: Configure dynamic lists with non-existing url_attr |
| 199 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 200 |
ldbmconfig.replace('nsslapd-dynamic-lists-url-attr', b'doesNotExist') |
| 201 |
|
| 202 |
# Step 4: Configure dynamic lists with non-existing list_attr |
| 203 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 204 |
ldbmconfig.replace('nsslapd-dynamic-lists-attr', b'doesNotExist') |
| 205 |
|
| 206 |
# Step 5: Configure dynamic lists with non-dn syntax attribute for list-attr |
| 207 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 208 |
ldbmconfig.replace('nsslapd-dynamic-lists-attr', b'cn') |
| 209 |
|
| 210 |
# Step 6: Configure dynamic lists with where url_attr and list_attr are the same |
| 211 |
ldbmconfig.replace('nsslapd-dynamic-lists-attr', b'uniquemember') |
| 212 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 213 |
ldbmconfig.replace('nsslapd-dynamic-lists-url-attr', b'uniquemember') |
| 214 |
|
| 215 |
|
| 216 |
def test_dynamic_lists_multiple_memberurl(topo, dynamic_lists_setup): |
| 217 |
"""Test dynamic lists plugin with multiple memberURL attributes pointing to |
| 218 |
different organizational units |
| 219 |
|
| 220 |
:id: a1b2c3d4-e5f6-7890-abcd-ef1234567890 |
| 221 |
:setup: Standalone Instance |
| 222 |
:steps: |
| 223 |
1. Configure dynamic lists with objectclass, url_attr, and list_attr |
| 224 |
2. Create a new organizational unit with one entry under it |
| 225 |
3. Create a group (demo_group) with memberURL attribute pointing to DEFAULT_SUFFIX |
| 226 |
4. Add an additional memberURL attribute to demo_group pointing to the new OU |
| 227 |
with the same filter as the first memberURL |
| 228 |
5. Search for the group and verify all users from both locations are listed as members |
| 229 |
6. Search for the group again and verify all users from both locations are listed as members |
| 230 |
:expectedresults: |
| 231 |
1. Success |
| 232 |
2. Organizational unit and entry should be created successfully |
| 233 |
3. Group should be created with first memberURL successfully |
| 234 |
4. Second memberURL should be added successfully |
| 235 |
5. All users from both locations should be listed as members of the group |
| 236 |
6. All users from both locations should be listed as members of the group |
| 237 |
""" |
| 238 |
|
| 239 |
inst = topo.standalone |
| 240 |
user_dns = dynamic_lists_setup |
| 241 |
|
| 242 |
# Step 1: Configure dynamic lists with objectclass, url_attr, and list_attr |
| 243 |
ldbmconfig = LDBMConfig(inst) |
| 244 |
ldbmconfig.replace('nsslapd-dynamic-lists-enabled', b'on') |
| 245 |
ldbmconfig.replace('nsslapd-dynamic-lists-attr', b'member') |
| 246 |
ldbmconfig.replace('nsslapd-dynamic-lists-oc', b'groupOfUrls') |
| 247 |
ldbmconfig.replace('nsslapd-dynamic-lists-url-attr', b'memberURL') |
| 248 |
|
| 249 |
# Step 2: Create a new organizational unit with one entry under it |
| 250 |
log.info("Creating new organizational unit") |
| 251 |
ous = OrganizationalUnits(inst, DEFAULT_SUFFIX) |
| 252 |
test_ou = ous.create(properties={ |
| 253 |
'ou': 'TestOU', |
| 254 |
'description': 'Test Organizational Unit for dynamic lists', |
| 255 |
}) |
| 256 |
ou_dn = test_ou.dn |
| 257 |
log.info(f"Created organizational unit: {ou_dn}") |
| 258 |
|
| 259 |
# Create a user entry under the new OU |
| 260 |
log.info("Creating user entry under the new OU") |
| 261 |
ou_users = UserAccounts(inst, DEFAULT_SUFFIX, rdn="ou=TestOU") |
| 262 |
ou_user = ou_users.create(properties={ |
| 263 |
'uid': 'testuser6', |
| 264 |
'cn': 'testuser6', |
| 265 |
'sn': 'User6', |
| 266 |
'uidNumber': '1006', |
| 267 |
'gidNumber': '1006', |
| 268 |
'homeDirectory': '/home/testuser6', |
| 269 |
}) |
| 270 |
ou_user_dn = ou_user.dn |
| 271 |
log.info(f"Created user under OU: {ou_user_dn}") |
| 272 |
|
| 273 |
# Step 3: Create a group (demo_group) with memberURL attribute pointing to DEFAULT_SUFFIX |
| 274 |
log.info("Creating demo_group with first memberURL") |
| 275 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 276 |
demo_group = groups.create(properties={ |
| 277 |
'cn': 'demo_group_multiple', |
| 278 |
'objectClass': 'groupOfUrls', |
| 279 |
'memberURL': f'ldap:///ou=people,{DEFAULT_SUFFIX}??sub?(uid=testuser*)', |
| 280 |
}) |
| 281 |
log.info(f"Created demo_group: {demo_group.dn} with memberURL: ldap:///{DEFAULT_SUFFIX}??sub?(uid=testuser*)") |
| 282 |
|
| 283 |
# Step 4: Add an additional memberURL attribute to demo_group pointing to the new OU |
| 284 |
# with the same filter as the first memberURL |
| 285 |
log.info("Adding second memberURL to demo_group pointing to new OU") |
| 286 |
second_memberurl = f'ldap:///{ou_dn}??sub?(uid=testuser*)' |
| 287 |
demo_group.add('memberURL', second_memberurl) |
| 288 |
log.info(f"Added second memberURL: {second_memberurl}") |
| 289 |
|
| 290 |
# Step 5: Search for the group to trigger the plugin and verify all users |
| 291 |
# from both locations are listed as members |
| 292 |
log.info("Searching for group to trigger dynamic lists plugin") |
| 293 |
members = demo_group.list_members() |
| 294 |
log.info(f"Found {len(members)} members in group") |
| 295 |
log.info(f"Members: {members}") |
| 296 |
|
| 297 |
# Verify all users from DEFAULT_SUFFIX are members |
| 298 |
assert len(members) == 6, f"Expected {6} members, got {len(members)}. Members: {members}" |
| 299 |
|
| 300 |
# Verify all users from DEFAULT_SUFFIX are members |
| 301 |
for user_dn in user_dns: |
| 302 |
assert user_dn in members, f"User {user_dn} should be a member of the group. Members: {members}" |
| 303 |
|
| 304 |
# Verify the user from the new OU is a member |
| 305 |
assert ou_user_dn in members, f"User {ou_user_dn} should be a member of the group. Members: {members}" |
| 306 |
|
| 307 |
# Now test that multiple seaches still return the same number of members |
| 308 |
members = demo_group.list_members() |
| 309 |
log.info(f"Found {len(members)} members in group") |
| 310 |
log.info(f"Members: {members}") |
| 311 |
assert len(members) == 6, f"Expected {6} members, got {len(members)}. Members: {members}" |
| 312 |
|
| 313 |
log.info("Test completed successfully - all users from both locations are listed as members") |
| 314 |
|
| 315 |
|
| 316 |
if __name__ == '__main__': |
| 317 |
# Run isolated |
| 318 |
# -s for DEBUG mode |
| 319 |
CURRENT_FILE = os.path.realpath(__file__) |
| 320 |
pytest.main(["-s", CURRENT_FILE]) |