| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2022 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 pytest |
| 10 |
import os |
| 11 |
import ldap |
| 12 |
import time |
| 13 |
from lib389.backend import Backends |
| 14 |
from lib389.utils import ensure_str |
| 15 |
from test389.topologies import topology_st as topo |
| 16 |
from lib389._constants import * |
| 17 |
from lib389.plugins import MemberOfPlugin, ReferentialIntegrityPlugin |
| 18 |
from lib389.idm.user import UserAccount, UserAccounts |
| 19 |
from lib389.idm.group import Group, Groups |
| 20 |
from lib389.idm.nscontainer import nsContainers |
| 21 |
from lib389.idm.domain import Domain |
| 22 |
from lib389.idm.organizationalunit import OrganizationalUnit |
| 23 |
|
| 24 |
SUBTREE_1 = 'cn=sub1,%s' % SUFFIX |
| 25 |
SUBTREE_2 = 'cn=sub2,%s' % SUFFIX |
| 26 |
SUBTREE_3 = 'cn=sub3,%s' % SUFFIX |
| 27 |
|
| 28 |
def add_container(inst, dn, name): |
| 29 |
"""Creates container entry""" |
| 30 |
conts = nsContainers(inst, dn) |
| 31 |
cont = conts.create(properties={'cn': name}) |
| 32 |
return cont |
| 33 |
|
| 34 |
def add_member_and_group(server, cn, group_cn, subtree): |
| 35 |
users = UserAccounts(server, subtree, rdn=None) |
| 36 |
users.create(properties={'uid': f'test_{cn}', |
| 37 |
'cn': f'test_{cn}', |
| 38 |
'sn': f'test_{cn}', |
| 39 |
'description': 'member', |
| 40 |
'uidNumber': '1000', |
| 41 |
'gidNumber': '2000', |
| 42 |
'homeDirectory': '/home/testuser'}) |
| 43 |
group = Groups(server, subtree, rdn=None) |
| 44 |
group.create(properties={'cn': group_cn, |
| 45 |
'member': f'uid=test_{cn},{subtree}', |
| 46 |
'description': 'group'}) |
| 47 |
|
| 48 |
def check_membership(server, user_dn=None, group_dn=None, find_result=True): |
| 49 |
ent = server.getEntry(user_dn, ldap.SCOPE_BASE, "(objectclass=*)", ['memberof']) |
| 50 |
found = False |
| 51 |
if ent.hasAttr('memberof'): |
| 52 |
for val in ent.getValues('memberof'): |
| 53 |
if ensure_str(val) == group_dn: |
| 54 |
found = True |
| 55 |
break |
| 56 |
|
| 57 |
if find_result: |
| 58 |
assert found |
| 59 |
else: |
| 60 |
assert (not found) |
| 61 |
|
| 62 |
def test_multiple_scopes(topo): |
| 63 |
"""Specify memberOf works when multiple include scopes are defined |
| 64 |
|
| 65 |
:id: fbcd70cc-c83d-4c79-bd5b-2d8f017545ae |
| 66 |
:setup: Standalone Instance |
| 67 |
:steps: |
| 68 |
1. Set multiple include scopes |
| 69 |
2. Test members added to both scopes are correctly updated |
| 70 |
3. Test user outside of scope was not updated |
| 71 |
4. Set exclude scope |
| 72 |
5. Move user into excluded subtree and check the membership is correct |
| 73 |
:expectedresults: |
| 74 |
1. Success |
| 75 |
2. Success |
| 76 |
3. Success |
| 77 |
4. Success |
| 78 |
5. Success |
| 79 |
""" |
| 80 |
|
| 81 |
inst = topo.standalone |
| 82 |
|
| 83 |
EXCLUDED_SUBTREE = 'cn=exclude,%s' % SUFFIX |
| 84 |
# enable Referential Integrity plugin |
| 85 |
# to correctly process the 'member' attribute |
| 86 |
refint = ReferentialIntegrityPlugin(inst) |
| 87 |
refint.add_excludescope(EXCLUDED_SUBTREE) |
| 88 |
refint.enable() |
| 89 |
|
| 90 |
# configure plugin |
| 91 |
memberof = MemberOfPlugin(inst) |
| 92 |
memberof.enable() |
| 93 |
memberof.add('memberOfEntryScope', SUBTREE_1) |
| 94 |
memberof.add('memberOfEntryScope', SUBTREE_2) |
| 95 |
if (memberof.get_memberofdeferredupdate() and memberof.get_memberofdeferredupdate().lower() == "on"): |
| 96 |
delay = 3 |
| 97 |
else: |
| 98 |
delay = 0 |
| 99 |
inst.restart() |
| 100 |
|
| 101 |
# Add setup entries |
| 102 |
add_container(inst, SUFFIX, 'sub1') |
| 103 |
add_container(inst, SUFFIX, 'sub2') |
| 104 |
add_container(inst, SUFFIX, 'sub3') |
| 105 |
add_member_and_group(inst, 'm1', 'g1', SUBTREE_1) |
| 106 |
add_member_and_group(inst, 'm2', 'g2', SUBTREE_2) |
| 107 |
add_member_and_group(inst, 'm3', 'g3', SUBTREE_3) |
| 108 |
|
| 109 |
# Check users 1 and 2 were correctly updated |
| 110 |
check_membership(inst, f'uid=test_m1,{SUBTREE_1}', f'cn=g1,{SUBTREE_1}', True) |
| 111 |
check_membership(inst, f'uid=test_m2,{SUBTREE_2}', f'cn=g2,{SUBTREE_2}', True) |
| 112 |
|
| 113 |
# Check that user3, which is out of scope, was not updated |
| 114 |
check_membership(inst, f'uid=test_m3,{SUBTREE_3}', f'cn=g1,{SUBTREE_1}', False) |
| 115 |
check_membership(inst, f'uid=test_m3,{SUBTREE_3}', f'cn=g2,{SUBTREE_2}', False) |
| 116 |
check_membership(inst, f'uid=test_m3,{SUBTREE_3}', f'cn=g3,{SUBTREE_3}', False) |
| 117 |
|
| 118 |
# Set exclude scope |
| 119 |
EXCLUDED_USER = f"uid=test_m1,{EXCLUDED_SUBTREE}" |
| 120 |
INCLUDED_USER = f"uid=test_m1,{SUBTREE_1}" |
| 121 |
GROUP_DN = f'cn=g1,{SUBTREE_1}' |
| 122 |
|
| 123 |
add_container(inst, SUFFIX, 'exclude') |
| 124 |
memberof.add('memberOfEntryScopeExcludeSubtree', EXCLUDED_SUBTREE) |
| 125 |
|
| 126 |
# Move user to excluded scope |
| 127 |
user = UserAccount(topo.standalone, dn=INCLUDED_USER) |
| 128 |
user.rename("uid=test_m1", newsuperior=EXCLUDED_SUBTREE) |
| 129 |
|
| 130 |
time.sleep(delay) |
| 131 |
# Check memberOf and group are cleaned up |
| 132 |
check_membership(inst, EXCLUDED_USER, GROUP_DN, False) |
| 133 |
group = Group(topo.standalone, dn=GROUP_DN) |
| 134 |
assert not group.present("member", INCLUDED_USER) |
| 135 |
assert not group.present("member", EXCLUDED_USER) |
| 136 |
|
| 137 |
def test_memberof_scope_multiple_backends(topology_st): |
| 138 |
"""Test memberOf plugin correctly handles multiple backends with different scopes |
| 139 |
|
| 140 |
:id: 96419128-70a4-4a81-943e-8d1e9c92f241 |
| 141 |
:setup: Instance with multiple backends |
| 142 |
:steps: |
| 143 |
1. Create out of scope backend |
| 144 |
2. Create domain and OU's for out of scope backend |
| 145 |
3. Create groups and users in both backends |
| 146 |
4. Enable MO plugin and set scope to backend 1 |
| 147 |
5. Add users to groups in both backends |
| 148 |
6. Verify in scope user has memberOf attr value of in scope group dn |
| 149 |
7. Verify out scope user has an empty memberOf attr value |
| 150 |
:expectedresults: |
| 151 |
1. Success |
| 152 |
2. Success |
| 153 |
3. Success |
| 154 |
4. Success |
| 155 |
5. Success, trigger MO update |
| 156 |
6. Success |
| 157 |
7. Success |
| 158 |
""" |
| 159 |
inst = topology_st.standalone |
| 160 |
outscope_suffix = 'dc=other,dc=org' |
| 161 |
|
| 162 |
# Create a backend 2 |
| 163 |
backends = Backends(inst) |
| 164 |
outscope_be = backends.create(properties={ |
| 165 |
'cn': 'testBackend', |
| 166 |
'nsslapd-suffix': outscope_suffix |
| 167 |
}) |
| 168 |
|
| 169 |
# Create domain entry for backend 2 |
| 170 |
domain = Domain(inst, outscope_suffix) |
| 171 |
domain.create(properties={ |
| 172 |
'dc': 'other' |
| 173 |
}) |
| 174 |
|
| 175 |
# Create OUs for backend 2 |
| 176 |
people_ou = OrganizationalUnit(inst, f'ou=People,{outscope_suffix}') |
| 177 |
people_ou.create(properties={'ou': 'People'}) |
| 178 |
groups_ou = OrganizationalUnit(inst, f'ou=Groups,{outscope_suffix}') |
| 179 |
groups_ou.create(properties={'ou': 'Groups'}) |
| 180 |
|
| 181 |
# Create test groups and users |
| 182 |
inscope_users = UserAccounts(inst, 'dc=example,dc=com') |
| 183 |
inscope_groups = Groups(inst, 'dc=example,dc=com') |
| 184 |
inscope_user = inscope_users.create_test_user(uid=1000) |
| 185 |
inscope_group = inscope_groups.create(properties={'cn': 'testgroup1'}) # NO MEMBERS initially |
| 186 |
|
| 187 |
outscope_users = UserAccounts(inst, outscope_suffix) |
| 188 |
outscope_groups = Groups(inst, outscope_suffix) |
| 189 |
outscope_user = outscope_users.create_test_user(uid=2000) |
| 190 |
outscope_group = outscope_groups.create(properties={'cn': 'testgroup2'}) # NO MEMBERS initially |
| 191 |
|
| 192 |
# Configure memberof plugin with backend 1 scope |
| 193 |
memberof = MemberOfPlugin(inst) |
| 194 |
memberof.enable() |
| 195 |
memberof.replace('memberOfEntryScope', 'dc=example,dc=com') |
| 196 |
inst.restart() |
| 197 |
|
| 198 |
# Trigger memberof processing |
| 199 |
inscope_group.add('member', inscope_user.dn) |
| 200 |
outscope_group.add('member', outscope_user.dn) |
| 201 |
|
| 202 |
# Sleep for a bit |
| 203 |
time.sleep(2) |
| 204 |
|
| 205 |
# In scope user should have memberOf attribute |
| 206 |
inscope_user_entry = inscope_user.get_attrs_vals_utf8(['memberOf']) |
| 207 |
assert inscope_user_entry['memberOf'] == [inscope_group.dn] |
| 208 |
|
| 209 |
# Out of scope user should not have memberOf attribute |
| 210 |
outscope_user_entry = outscope_user.get_attrs_vals_utf8(['memberOf']) |
| 211 |
assert outscope_user_entry['memberOf'] == [] |
| 212 |
|
| 213 |
# Cleanup |
| 214 |
inscope_group.delete() |
| 215 |
inscope_user.delete() |
| 216 |
outscope_group.delete() |
| 217 |
outscope_user.delete() |
| 218 |
outscope_be.delete() |
| 219 |
|
| 220 |
if __name__ == '__main__': |
| 221 |
# Run isolated |
| 222 |
# -s for DEBUG mode |
| 223 |
CURRENT_FILE = os.path.realpath(__file__) |
| 224 |
pytest.main(["-s", CURRENT_FILE]) |