| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2020 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 ldap |
| 11 |
import logging |
| 12 |
import pytest |
| 13 |
import time |
| 14 |
from lib389._constants import DEFAULT_SUFFIX, DEFAULT_BENAME |
| 15 |
from lib389.config import Config |
| 16 |
from lib389.plugins import USNPlugin, MemberOfPlugin |
| 17 |
from lib389.idm.group import Groups |
| 18 |
from lib389.idm.user import UserAccounts |
| 19 |
from lib389.idm.organizationalunit import OrganizationalUnit |
| 20 |
from lib389.tombstone import Tombstones |
| 21 |
from lib389.rootdse import RootDSE |
| 22 |
from test389.topologies import topology_st, topology_m2 |
| 23 |
|
| 24 |
pytestmark = pytest.mark.tier1 |
| 25 |
|
| 26 |
log = logging.getLogger(__name__) |
| 27 |
|
| 28 |
USER_NUM = 10 |
| 29 |
GROUP_NUM = 3 |
| 30 |
|
| 31 |
|
| 32 |
def check_entryusn_no_duplicates(entryusn_list): |
| 33 |
"""Check that all values in the list are unique""" |
| 34 |
|
| 35 |
if len(entryusn_list) > len(set(entryusn_list)): |
| 36 |
raise AssertionError(f"EntryUSN values have duplicates, please, check logs") |
| 37 |
|
| 38 |
|
| 39 |
def check_lastusn_after_restart(inst): |
| 40 |
"""Check that last usn is the same after restart""" |
| 41 |
|
| 42 |
root_dse = RootDSE(inst) |
| 43 |
last_usn_before = root_dse.get_attr_val_int("lastusn;userroot") |
| 44 |
inst.restart() |
| 45 |
last_usn_after = root_dse.get_attr_val_int("lastusn;userroot") |
| 46 |
assert last_usn_after == last_usn_before |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture(scope="module") |
| 50 |
def setup(topology_st, request): |
| 51 |
""" |
| 52 |
Enable USN plug-in |
| 53 |
Enable MEMBEROF plugin |
| 54 |
Add test entries |
| 55 |
""" |
| 56 |
|
| 57 |
inst = topology_st.standalone |
| 58 |
|
| 59 |
log.info("Enable the USN plugin...") |
| 60 |
plugin = USNPlugin(inst) |
| 61 |
plugin.enable() |
| 62 |
|
| 63 |
log.info("Enable the MEMBEROF plugin...") |
| 64 |
plugin = MemberOfPlugin(inst) |
| 65 |
plugin.enable() |
| 66 |
|
| 67 |
inst.restart() |
| 68 |
|
| 69 |
users_list = [] |
| 70 |
log.info("Adding test entries...") |
| 71 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 72 |
for id in range(USER_NUM): |
| 73 |
user = users.create_test_user(uid=id) |
| 74 |
users_list.append(user) |
| 75 |
|
| 76 |
groups_list = [] |
| 77 |
log.info("Adding test groups...") |
| 78 |
groups = Groups(inst, DEFAULT_SUFFIX) |
| 79 |
for id in range(GROUP_NUM): |
| 80 |
group = groups.create(properties={'cn': f'test_group{id}'}) |
| 81 |
groups_list.append(group) |
| 82 |
|
| 83 |
def fin(): |
| 84 |
for user in users_list: |
| 85 |
try: |
| 86 |
user.delete() |
| 87 |
except ldap.NO_SUCH_OBJECT: |
| 88 |
pass |
| 89 |
for group in groups_list: |
| 90 |
try: |
| 91 |
group.delete() |
| 92 |
except ldap.NO_SUCH_OBJECT: |
| 93 |
pass |
| 94 |
request.addfinalizer(fin) |
| 95 |
|
| 96 |
return {"users": users_list, |
| 97 |
"groups": groups_list} |
| 98 |
|
| 99 |
|
| 100 |
def test_entryusn_no_duplicates(topology_st, setup): |
| 101 |
"""Verify that entryUSN is not duplicated after memberOf operation |
| 102 |
|
| 103 |
:id: 1a7d382d-1214-4d56-b9c2-9c4ed57d1683 |
| 104 |
:setup: Standalone instance, Groups and Users, USN and memberOf are enabled |
| 105 |
:steps: |
| 106 |
1. Add a member to group 1 |
| 107 |
2. Add a member to group 1 and 2 |
| 108 |
3. Check that entryUSNs are different |
| 109 |
4. Check that lastusn before and after a restart are the same |
| 110 |
:expectedresults: |
| 111 |
1. Success |
| 112 |
2. Success |
| 113 |
3. Success |
| 114 |
4. Success |
| 115 |
""" |
| 116 |
|
| 117 |
inst = topology_st.standalone |
| 118 |
plugin = MemberOfPlugin(inst) |
| 119 |
if (plugin.get_memberofdeferredupdate() and plugin.get_memberofdeferredupdate().lower() == "on"): |
| 120 |
delay = 3 |
| 121 |
else: |
| 122 |
delay = 0 |
| 123 |
config = Config(inst) |
| 124 |
config.replace('nsslapd-accesslog-level', '260') # Internal op |
| 125 |
config.replace('nsslapd-errorlog-level', '65536') |
| 126 |
config.replace('nsslapd-plugin-logging', 'on') |
| 127 |
entryusn_list = [] |
| 128 |
|
| 129 |
users = setup["users"] |
| 130 |
groups = setup["groups"] |
| 131 |
|
| 132 |
groups[0].replace('member', users[0].dn) |
| 133 |
time.sleep(delay) |
| 134 |
entryusn_list.append(users[0].get_attr_val_int('entryusn')) |
| 135 |
log.info(f"{users[0].dn}_1: {entryusn_list[-1:]}") |
| 136 |
entryusn_list.append(groups[0].get_attr_val_int('entryusn')) |
| 137 |
log.info(f"{groups[0].dn}_1: {entryusn_list[-1:]}") |
| 138 |
check_entryusn_no_duplicates(entryusn_list) |
| 139 |
|
| 140 |
groups[1].replace('member', [users[0].dn, users[1].dn]) |
| 141 |
time.sleep(delay) |
| 142 |
entryusn_list.append(users[0].get_attr_val_int('entryusn')) |
| 143 |
log.info(f"{users[0].dn}_2: {entryusn_list[-1:]}") |
| 144 |
entryusn_list.append(users[1].get_attr_val_int('entryusn')) |
| 145 |
log.info(f"{users[1].dn}_2: {entryusn_list[-1:]}") |
| 146 |
entryusn_list.append(groups[1].get_attr_val_int('entryusn')) |
| 147 |
log.info(f"{groups[1].dn}_2: {entryusn_list[-1:]}") |
| 148 |
check_entryusn_no_duplicates(entryusn_list) |
| 149 |
|
| 150 |
check_lastusn_after_restart(inst) |
| 151 |
|
| 152 |
|
| 153 |
def test_entryusn_is_same_after_failure(topology_st, setup): |
| 154 |
"""Verify that entryUSN is the same after failed operation |
| 155 |
|
| 156 |
:id: 1f227533-370a-48c1-b920-9b3b0bcfc32e |
| 157 |
:setup: Standalone instance, Groups and Users, USN and memberOf are enabled |
| 158 |
:steps: |
| 159 |
1. Get current group's entryUSN value |
| 160 |
2. Try to modify the group with an invalid syntax |
| 161 |
3. Get new group's entryUSN value and compare with old |
| 162 |
4. Check that lastusn before and after a restart are the same |
| 163 |
:expectedresults: |
| 164 |
1. Success |
| 165 |
2. Invalid Syntax error |
| 166 |
3. Should be the same |
| 167 |
4. Success |
| 168 |
""" |
| 169 |
|
| 170 |
inst = topology_st.standalone |
| 171 |
users = setup["users"] |
| 172 |
|
| 173 |
# We need this update so we get the latest USN pointed to our entry |
| 174 |
users[0].replace('description', 'update') |
| 175 |
|
| 176 |
entryusn_before = users[0].get_attr_val_int('entryusn') |
| 177 |
users[0].replace('description', 'update') |
| 178 |
try: |
| 179 |
users[0].replace('uid', 'invalid update') |
| 180 |
except ldap.NOT_ALLOWED_ON_RDN: |
| 181 |
pass |
| 182 |
users[0].replace('description', 'second update') |
| 183 |
entryusn_after = users[0].get_attr_val_int('entryusn') |
| 184 |
|
| 185 |
# entryUSN should be OLD + 2 (only two user updates) |
| 186 |
assert entryusn_after == (entryusn_before + 2) |
| 187 |
|
| 188 |
check_lastusn_after_restart(inst) |
| 189 |
|
| 190 |
|
| 191 |
def test_entryusn_after_repl_delete(topology_m2): |
| 192 |
"""Verify that entryUSN is incremented on 1 after delete operation which creates a tombstone |
| 193 |
|
| 194 |
:id: 1704cf65-41bc-4347-bdaf-20fc2431b218 |
| 195 |
:setup: An instance with replication, Users, USN enabled |
| 196 |
:steps: |
| 197 |
1. Try to delete a user |
| 198 |
2. Check the tombstone has the incremented USN |
| 199 |
3. Try to delete ou=People with users |
| 200 |
4. Check the entry has a not incremented entryUSN |
| 201 |
:expectedresults: |
| 202 |
1. Success |
| 203 |
2. Success |
| 204 |
3. Should fail with Not Allowed On Non-leaf error |
| 205 |
4. Success |
| 206 |
""" |
| 207 |
|
| 208 |
inst = topology_m2.ms["supplier1"] |
| 209 |
plugin = USNPlugin(inst) |
| 210 |
plugin.enable() |
| 211 |
inst.restart() |
| 212 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 213 |
|
| 214 |
try: |
| 215 |
user_1 = users.create_test_user() |
| 216 |
user_rdn = user_1.rdn |
| 217 |
tombstones = Tombstones(inst, DEFAULT_SUFFIX) |
| 218 |
|
| 219 |
user_1.replace('description', 'update_ts') |
| 220 |
user_usn = user_1.get_attr_val_int('entryusn') |
| 221 |
|
| 222 |
user_1.delete() |
| 223 |
time.sleep(1) # Gives a little time for tombstone creation to complete |
| 224 |
|
| 225 |
ts = tombstones.get(user_rdn) |
| 226 |
ts_usn = ts.get_attr_val_int('entryusn') |
| 227 |
|
| 228 |
assert (user_usn + 1) == ts_usn |
| 229 |
|
| 230 |
user_1 = users.create_test_user() |
| 231 |
org = OrganizationalUnit(inst, f"ou=People,{DEFAULT_SUFFIX}") |
| 232 |
org.replace('description', 'update_ts') |
| 233 |
ou_usn_before = org.get_attr_val_int('entryusn') |
| 234 |
try: |
| 235 |
org.delete() |
| 236 |
except ldap.NOT_ALLOWED_ON_NONLEAF: |
| 237 |
pass |
| 238 |
ou_usn_after = org.get_attr_val_int('entryusn') |
| 239 |
assert ou_usn_before == ou_usn_after |
| 240 |
|
| 241 |
finally: |
| 242 |
try: |
| 243 |
user_1.delete() |
| 244 |
except ldap.NO_SUCH_OBJECT: |
| 245 |
pass |
| 246 |
|
| 247 |
|
| 248 |
def test_entryusn_tombstone_index(topology_st, setup): |
| 249 |
"""Verify deleted entries with USN (tombstones) are correctly handled in indexes |
| 250 |
|
| 251 |
:id: 95b2c769-f00e-43f4-af63-074c07547478 |
| 252 |
:setup: Standalone instance with USN plugin enabled and test users |
| 253 |
:steps: |
| 254 |
1. Create two test users for index verification |
| 255 |
2. Delete first user and check it is not in the cn index via dbscan |
| 256 |
3. Reindex cn offline with db2index |
| 257 |
4. Check deleted entry is still not in the cn index |
| 258 |
5. Delete second user and check tombstone exists via Tombstones class |
| 259 |
6. Reindex objectclass offline with db2index |
| 260 |
7. Check tombstone still exists after reindex |
| 261 |
:expectedresults: |
| 262 |
1. Users are created |
| 263 |
2. Deleted entry should not appear in cn index |
| 264 |
3. Reindexing succeeds |
| 265 |
4. Deleted entry should still not appear in cn index |
| 266 |
5. Tombstone should exist |
| 267 |
6. Reindexing succeeds |
| 268 |
7. Tombstone should still exist |
| 269 |
""" |
| 270 |
|
| 271 |
inst = topology_st.standalone |
| 272 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 273 |
|
| 274 |
log.info('Creating test users for index verification') |
| 275 |
cn_user = users.create_test_user(uid=1100) |
| 276 |
oc_user = users.create_test_user(uid=1101) |
| 277 |
cn_user_cn = cn_user.get_attr_val_utf8('cn') |
| 278 |
oc_user_uid = oc_user.get_attr_val_utf8('uid') |
| 279 |
|
| 280 |
log.info(f'Deleting user {cn_user_cn} for cn index verification') |
| 281 |
cn_user.delete() |
| 282 |
|
| 283 |
log.info('Checking deleted entry is not in the cn index') |
| 284 |
dbscan_output = inst.dbscan(bename=DEFAULT_BENAME, index='cn').decode('utf-8', errors='replace') |
| 285 |
assert cn_user_cn not in dbscan_output |
| 286 |
|
| 287 |
log.info('Reindexing cn offline') |
| 288 |
inst.stop() |
| 289 |
assert inst.db2index(bename=DEFAULT_BENAME, attrs=['cn']) |
| 290 |
inst.start() |
| 291 |
|
| 292 |
log.info('Verifying deleted entry is still not in cn index after reindex') |
| 293 |
dbscan_output = inst.dbscan(bename=DEFAULT_BENAME, index='cn').decode('utf-8', errors='replace') |
| 294 |
assert cn_user_cn not in dbscan_output |
| 295 |
|
| 296 |
log.info(f'Deleting user {oc_user_uid} for tombstone verification') |
| 297 |
oc_user.delete() |
| 298 |
|
| 299 |
log.info('Checking tombstone exists in the directory') |
| 300 |
tombstones = Tombstones(inst, DEFAULT_SUFFIX) |
| 301 |
ts_list = tombstones.filter(f'(uid={oc_user_uid})') |
| 302 |
assert len(ts_list) == 1 |
| 303 |
|
| 304 |
log.info('Reindexing objectclass offline') |
| 305 |
inst.stop() |
| 306 |
assert inst.db2index(bename=DEFAULT_BENAME, attrs=['objectclass']) |
| 307 |
inst.start() |
| 308 |
|
| 309 |
log.info('Verifying tombstone still exists after reindex') |
| 310 |
ts_list = tombstones.filter(f'(uid={oc_user_uid})') |
| 311 |
assert len(ts_list) == 1 |
| 312 |
|
| 313 |
|
| 314 |
if __name__ == '__main__': |
| 315 |
CURRENT_FILE = os.path.realpath(__file__) |
| 316 |
pytest.main(["-s", CURRENT_FILE]) |