| 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 time |
| 13 |
import ldap |
| 14 |
from lib389._constants import * |
| 15 |
from test389.topologies import topology_st as topo |
| 16 |
from lib389.utils import ensure_str |
| 17 |
from lib389.plugins import MemberOfPlugin |
| 18 |
from lib389.idm.user import UserAccounts, UserAccount |
| 19 |
from lib389.idm.group import Group, Groups, UniqueGroup, UniqueGroups |
| 20 |
|
| 21 |
log = logging.getLogger(__name__) |
| 22 |
|
| 23 |
# Constants |
| 24 |
PLUGIN_MEMBER_OF = 'MemberOf Plugin' |
| 25 |
MEMBEROF_PLUGIN_DN = ('cn=' + PLUGIN_MEMBER_OF + ',cn=plugins,cn=config') |
| 26 |
PLUGIN_ENABLED = 'nsslapd-pluginEnabled' |
| 27 |
|
| 28 |
# Test data |
| 29 |
USER1_DN = 'uid=testuser1,ou=people,' + DEFAULT_SUFFIX |
| 30 |
USER2_DN = 'uid=testuser2,ou=people,' + DEFAULT_SUFFIX |
| 31 |
USER3_DN = 'uid=testuser3,ou=people,' + DEFAULT_SUFFIX |
| 32 |
UNIQGROUP1_DN = 'cn=testugroup1,ou=groups,' + DEFAULT_SUFFIX |
| 33 |
UNIQGROUP2_DN = 'cn=testugroup2,ou=groups,' + DEFAULT_SUFFIX |
| 34 |
GROUP1_DN = 'cn=testgroup1,ou=groups,' + DEFAULT_SUFFIX |
| 35 |
GROUP2_DN = 'cn=testgroup2,ou=groups,' + DEFAULT_SUFFIX |
| 36 |
GROUP3_DN = 'cn=testgroup3,ou=groups,' + DEFAULT_SUFFIX |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture(scope="function") |
| 40 |
def users_and_groups(request, topo): |
| 41 |
""" |
| 42 |
Add users and groups to the default suffix |
| 43 |
""" |
| 44 |
|
| 45 |
log.info('Adding users and groups') |
| 46 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 47 |
for user_name in ["testuser1", "testuser2", "testuser3"]: |
| 48 |
users.create(properties={ |
| 49 |
'uid': user_name, |
| 50 |
'cn': user_name, |
| 51 |
'sn': user_name, |
| 52 |
'uidNumber': '1234', |
| 53 |
'gidNumber': '1234', |
| 54 |
'homeDirectory': '/home/test', |
| 55 |
}) |
| 56 |
|
| 57 |
groups = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 58 |
for group_name in ["testgroup1", "testgroup2", "testgroup3"]: |
| 59 |
groups.create(properties={'cn': group_name}) |
| 60 |
|
| 61 |
uniquegroups = UniqueGroups(topo.standalone, DEFAULT_SUFFIX) |
| 62 |
uniquegroups.create(properties={'cn': 'testugroup1'}) |
| 63 |
uniquegroups.create(properties={'cn': 'testugroup2'}) |
| 64 |
|
| 65 |
def fin(): |
| 66 |
log.info('Cleanup') |
| 67 |
UserAccount(topo.standalone, USER1_DN).delete() |
| 68 |
UserAccount(topo.standalone, USER2_DN).delete() |
| 69 |
UserAccount(topo.standalone, USER3_DN).delete() |
| 70 |
Group(topo.standalone, GROUP1_DN).delete() |
| 71 |
Group(topo.standalone, GROUP2_DN).delete() |
| 72 |
Group(topo.standalone, GROUP3_DN).delete() |
| 73 |
UniqueGroup(topo.standalone, UNIQGROUP1_DN).delete() |
| 74 |
UniqueGroup(topo.standalone, UNIQGROUP2_DN).delete() |
| 75 |
|
| 76 |
request.addfinalizer(fin) |
| 77 |
|
| 78 |
|
| 79 |
def _memberof_checking_delay(inst): |
| 80 |
"""Get appropriate delay for memberof checking based on plugin configuration""" |
| 81 |
memberof = MemberOfPlugin(inst) |
| 82 |
if (memberof.get_memberofdeferredupdate() and memberof.get_memberofdeferredupdate().lower() == "on"): |
| 83 |
# In case of deferred update then a safe delay to let the deferred thread processing is 3 sec |
| 84 |
delay = 3 |
| 85 |
else: |
| 86 |
# Else it is the same TXN, no reason to wait |
| 87 |
delay = 0 |
| 88 |
return delay |
| 89 |
|
| 90 |
|
| 91 |
def _check_memberof(inst, member, group): |
| 92 |
"""Check if member has memberof attribute pointing to group""" |
| 93 |
log.info("Lookup memberof from %s" % member) |
| 94 |
entry = inst.getEntry(ensure_str(member), ldap.SCOPE_BASE, '(objectclass=*)', ['memberof']) |
| 95 |
if not entry.hasAttr('memberof'): |
| 96 |
return False |
| 97 |
|
| 98 |
found = False |
| 99 |
for val in entry.getValues('memberof'): |
| 100 |
log.info("memberof: %s" % ensure_str(val)) |
| 101 |
if ensure_str(group.lower()) == ensure_str(val.lower()): |
| 102 |
found = True |
| 103 |
log.info("--> membership verified") |
| 104 |
break |
| 105 |
return found |
| 106 |
|
| 107 |
|
| 108 |
def test_user_to_group_membership(topo, users_and_groups): |
| 109 |
"""Test that including and excluding groups works as expected with users |
| 110 |
|
| 111 |
:id: 1561e082-b2cf-4863-b3b3-6af2398c545d |
| 112 |
:setup: Standalone Instance |
| 113 |
:steps: |
| 114 |
1. Enable and configure memberof plugin |
| 115 |
2. Add users to various groups |
| 116 |
3. Check that users have, or do not have the memberof attribute |
| 117 |
:expectedresults: |
| 118 |
1. Plugin should be enabled successfully |
| 119 |
2. Success |
| 120 |
3. Success |
| 121 |
""" |
| 122 |
|
| 123 |
inst = topo.standalone |
| 124 |
|
| 125 |
# Enable memberof plugin |
| 126 |
log.info("Enable MemberOf plugin") |
| 127 |
memberof = MemberOfPlugin(inst) |
| 128 |
memberof.enable() |
| 129 |
memberof.remove_all_specific_group_filters() |
| 130 |
memberof.remove_all_exclude_specific_group_filters() |
| 131 |
memberof.add_specific_group_filter("(entrydn=" + GROUP1_DN + ")") |
| 132 |
memberof.add_exclude_specific_group_filter("(entrydn=" + GROUP3_DN + ")") |
| 133 |
inst.restart() |
| 134 |
|
| 135 |
# Add user to group |
| 136 |
log.info("Add user to group 1") |
| 137 |
group1 = Group(inst, GROUP1_DN) |
| 138 |
group1.add_member(USER1_DN) |
| 139 |
group2 = Group(inst, GROUP2_DN) |
| 140 |
group2.add_member(USER2_DN) |
| 141 |
group3 = Group(inst, GROUP3_DN) |
| 142 |
group3.add_member(USER3_DN) |
| 143 |
|
| 144 |
# Wait for memberof processing |
| 145 |
delay = _memberof_checking_delay(inst) |
| 146 |
if delay > 0: |
| 147 |
time.sleep(delay) |
| 148 |
|
| 149 |
# Check memberof attribute |
| 150 |
log.info("Check memberof attribute") |
| 151 |
assert _check_memberof(inst, USER1_DN, GROUP1_DN) |
| 152 |
assert not _check_memberof(inst, USER2_DN, GROUP2_DN) |
| 153 |
assert not _check_memberof(inst, USER3_DN, GROUP3_DN) |
| 154 |
|
| 155 |
|
| 156 |
def test_group_to_group_membership(topo, users_and_groups): |
| 157 |
"""Test that including and excluding groups works as expected with nested |
| 158 |
groups |
| 159 |
|
| 160 |
:id: 1561e082-b2cf-4863-b3b3-6af2398c545e |
| 161 |
:setup: Standalone Instance |
| 162 |
:steps: |
| 163 |
1. Enable and configure memberof plugin |
| 164 |
2. Add groups to other groups |
| 165 |
3. Check that users have, or do not have the memberof attribute |
| 166 |
:expectedresults: |
| 167 |
1. Plugin should be enabled successfully |
| 168 |
2. Success |
| 169 |
3. Success |
| 170 |
""" |
| 171 |
|
| 172 |
inst = topo.standalone |
| 173 |
|
| 174 |
# Enable memberof plugin |
| 175 |
log.info("Enable MemberOf plugin") |
| 176 |
memberof = MemberOfPlugin(inst) |
| 177 |
memberof.enable() |
| 178 |
memberof.remove_all_specific_group_filters() |
| 179 |
memberof.remove_all_exclude_specific_group_filters() |
| 180 |
memberof.add_exclude_specific_group_filter("(entrydn=" + GROUP3_DN + ")") |
| 181 |
inst.restart() |
| 182 |
|
| 183 |
# Add user to group |
| 184 |
log.info("Add user to group 1") |
| 185 |
group1 = Group(inst, GROUP1_DN) |
| 186 |
group1.add_member(USER1_DN) |
| 187 |
group2 = Group(inst, GROUP2_DN) |
| 188 |
group2.add_member(GROUP2_DN) |
| 189 |
group2.add_member(GROUP3_DN) |
| 190 |
group3 = Group(inst, GROUP3_DN) |
| 191 |
group3.add_member(GROUP2_DN) |
| 192 |
|
| 193 |
# _check_memberof (member, group) |
| 194 |
assert _check_memberof(inst, USER1_DN, GROUP1_DN) |
| 195 |
assert not _check_memberof(inst, GROUP3_DN, GROUP2_DN) |
| 196 |
assert not _check_memberof(inst, GROUP2_DN, GROUP3_DN) |
| 197 |
|
| 198 |
|
| 199 |
def test_group_oc_membership(topo, users_and_groups): |
| 200 |
"""Test that including and excluding groups works as expected with custom |
| 201 |
objectclasses. The specific objectclasses are used to distinguish if entries |
| 202 |
are "groups" or "not groups" |
| 203 |
|
| 204 |
:id: 42714d25-be6d-4aa0-831d-0f4cf91fac86 |
| 205 |
:setup: Standalone Instance |
| 206 |
:steps: |
| 207 |
1. Enable and configure memberof plugin (exclude groups only and |
| 208 |
objectclass groupofuniquenames) |
| 209 |
2. Add user to an excluded group but the group has objectclass groupofnames |
| 210 |
3. Add user to an excluded group but the group has objectclass groupofuniquenames |
| 211 |
4. Memberof config remove exclude rules and add include ones |
| 212 |
5. Add user to a group that is in the include list |
| 213 |
6. Add group to a group that is not in the include list |
| 214 |
7. Add group that is not in the include list to a group that is in the include list |
| 215 |
8. Add group with unknown objectclass to a group that is in the include list |
| 216 |
9. Add group with known objectclass to a group that is in the include list |
| 217 |
:expectedresults: |
| 218 |
1. Plugin should be enabled successfully |
| 219 |
2. Membership is not updated |
| 220 |
3. Membership is not updated |
| 221 |
4. Success |
| 222 |
5. Success |
| 223 |
6. Membership is not updated |
| 224 |
7. Membership is not updated |
| 225 |
8. Success |
| 226 |
9. Membership is not updated |
| 227 |
""" |
| 228 |
|
| 229 |
inst = topo.standalone |
| 230 |
|
| 231 |
# Enable memberof plugin |
| 232 |
log.info("Enable MemberOf plugin") |
| 233 |
memberof = MemberOfPlugin(inst) |
| 234 |
memberof.enable() |
| 235 |
memberof.add_groupattr('uniquemember') |
| 236 |
memberof.remove_all_specific_group_filters() |
| 237 |
memberof.remove_all_exclude_specific_group_filters() |
| 238 |
memberof.add_exclude_specific_group_filter("(entrydn=" + UNIQGROUP1_DN + ")") |
| 239 |
memberof.add_exclude_specific_group_filter("(entrydn=" + GROUP1_DN + ")") |
| 240 |
memberof.add_specific_group_oc("groupofuniquenames") |
| 241 |
inst.restart() |
| 242 |
|
| 243 |
# Add user to an excluded group but the group has objectclass groupofnames |
| 244 |
# but since we are excluding specifc DN's we don't care about the entry's |
| 245 |
# objectclasses |
| 246 |
log.info("Add user to group") |
| 247 |
group = Group(inst, GROUP1_DN) |
| 248 |
group.add_member(USER1_DN) |
| 249 |
assert not _check_memberof(inst, USER1_DN, GROUP1_DN) |
| 250 |
|
| 251 |
# Add user to an excluded group that does match specifc group objectclass |
| 252 |
unique_group = UniqueGroup(inst, UNIQGROUP1_DN) |
| 253 |
unique_group.add_member(USER1_DN) |
| 254 |
assert not _check_memberof(inst, USER1_DN, UNIQGROUP1_DN) |
| 255 |
|
| 256 |
# |
| 257 |
# Switch over to just allowing specific groups |
| 258 |
# |
| 259 |
memberof.remove_all_exclude_specific_group_filters() |
| 260 |
memberof.add_specific_group_filter("(entrydn=" + UNIQGROUP1_DN + ")") |
| 261 |
memberof.add_specific_group_filter("(entrydn=" + GROUP1_DN + ")") |
| 262 |
time.sleep(1) |
| 263 |
# Update group that should allow the update |
| 264 |
unique_group.add_member(USER2_DN) |
| 265 |
assert _check_memberof(inst, USER2_DN, UNIQGROUP1_DN) |
| 266 |
|
| 267 |
# Update a valid "group" but that is not in the specified list |
| 268 |
unique_group2 = UniqueGroup(inst, UNIQGROUP2_DN) |
| 269 |
unique_group2.add_member(USER3_DN) |
| 270 |
assert not _check_memberof(inst, USER3_DN, GROUP1_DN) |
| 271 |
|
| 272 |
# uniquegroup2 should not be listed as memberof uniquegroup1 since its not |
| 273 |
# in the allowed group list |
| 274 |
unique_group.add_member(UNIQGROUP2_DN) |
| 275 |
assert not _check_memberof(inst, UNIQGROUP2_DN, UNIQGROUP1_DN) |
| 276 |
|
| 277 |
# uniquegroup1 is an allowed group, and group2 is not seen a group because |
| 278 |
# of the specific group objectclass setting. So the MO update should be |
| 279 |
# allowed in this case |
| 280 |
unique_group.add_member(GROUP2_DN) |
| 281 |
assert _check_memberof(inst, GROUP2_DN, UNIQGROUP1_DN) |
| 282 |
|
| 283 |
# Now add groupOfNames oc to the MO config, and try adding a group as a |
| 284 |
# member that is not in the specific group list |
| 285 |
memberof.add_specific_group_oc("groupofnames") |
| 286 |
unique_group.add_member(GROUP3_DN) |
| 287 |
assert not _check_memberof(inst, GROUP3_DN, UNIQGROUP1_DN) |
| 288 |
|
| 289 |
|
| 290 |
if __name__ == '__main__': |
| 291 |
# Run isolated |
| 292 |
# -s for DEBUG mode |
| 293 |
CURRENT_FILE = os.path.realpath(__file__) |
| 294 |
pytest.main(["-s", CURRENT_FILE]) |