| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2020 William Brown <[email protected]> |
| 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 ldap |
| 11 |
import os |
| 12 |
import time |
| 13 |
import threading |
| 14 |
from ldap.syncrepl import SyncreplConsumer |
| 15 |
from ldap.ldapobject import ReconnectLDAPObject |
| 16 |
import pytest |
| 17 |
from lib389 import DirSrv |
| 18 |
from lib389.idm.organizationalunit import OrganizationalUnits, OrganizationalUnit |
| 19 |
from lib389.idm.user import nsUserAccounts, UserAccounts |
| 20 |
from lib389.idm.group import Groups |
| 21 |
from test389.topologies import topology_st as topology |
| 22 |
from test389.topologies import topology_m2 as topo_m2 |
| 23 |
from lib389.paths import Paths |
| 24 |
from lib389.utils import ds_is_older, is_fips, ensure_bytes |
| 25 |
from lib389.plugins import RetroChangelogPlugin, ContentSyncPlugin, AutoMembershipPlugin, MemberOfPlugin, MemberOfSharedConfig, AutoMembershipDefinitions, MEPTemplates, MEPConfigs, ManagedEntriesPlugin, MEPTemplate |
| 26 |
from lib389._constants import * |
| 27 |
|
| 28 |
from . import ISyncRepl, syncstate_assert |
| 29 |
|
| 30 |
default_paths = Paths() |
| 31 |
pytestmark = pytest.mark.tier1 |
| 32 |
|
| 33 |
log = logging.getLogger(__name__) |
| 34 |
|
| 35 |
@pytest.fixture(scope="function") |
| 36 |
def init_sync_repl_plugins(topology, request): |
| 37 |
"""Prepare test environment (retroCL/sync_repl/ |
| 38 |
automember/memberof) and cleanup at the end of the test |
| 39 |
1.: enable retroCL |
| 40 |
2.: configure retroCL to log nsuniqueid as targetUniqueId |
| 41 |
3.: enable content_sync plugin |
| 42 |
4.: enable automember |
| 43 |
5.: create (2) groups. Few groups can help to reproduce the concurrent updates problem. |
| 44 |
6.: configure automember to provision those groups with 'member' |
| 45 |
7.: enable and configure memberof plugin |
| 46 |
8.: enable plugin log level |
| 47 |
9.: restart the server |
| 48 |
""" |
| 49 |
inst = topology[0] |
| 50 |
inst.restart() |
| 51 |
|
| 52 |
# Enable/configure retroCL |
| 53 |
plugin = RetroChangelogPlugin(inst) |
| 54 |
plugin.disable() |
| 55 |
plugin.enable() |
| 56 |
plugin.set('nsslapd-attribute', 'nsuniqueid:targetuniqueid') |
| 57 |
|
| 58 |
# Enable sync plugin |
| 59 |
plugin = ContentSyncPlugin(inst) |
| 60 |
plugin.enable() |
| 61 |
|
| 62 |
# Enable automember |
| 63 |
plugin = AutoMembershipPlugin(inst) |
| 64 |
plugin.disable() |
| 65 |
plugin.enable() |
| 66 |
|
| 67 |
# Add the automember group |
| 68 |
groups = Groups(inst, DEFAULT_SUFFIX) |
| 69 |
group = [] |
| 70 |
for i in range(1,5): |
| 71 |
group.append(groups.create(properties={'cn': 'group%d' % i})) |
| 72 |
|
| 73 |
# Add the automember config entry |
| 74 |
am_configs = AutoMembershipDefinitions(inst) |
| 75 |
am_configs_cleanup = [] |
| 76 |
for g in group: |
| 77 |
am_config = am_configs.create(properties={'cn': 'config %s' % g.get_attr_val_utf8('cn'), |
| 78 |
'autoMemberScope': DEFAULT_SUFFIX, |
| 79 |
'autoMemberFilter': 'uid=*', |
| 80 |
'autoMemberDefaultGroup': g.dn, |
| 81 |
'autoMemberGroupingAttr': 'member:dn'}) |
| 82 |
am_configs_cleanup.append(am_config) |
| 83 |
|
| 84 |
# Enable and configure memberof plugin |
| 85 |
plugin = MemberOfPlugin(inst) |
| 86 |
plugin.disable() |
| 87 |
plugin.enable() |
| 88 |
|
| 89 |
plugin.replace_groupattr('member') |
| 90 |
|
| 91 |
memberof_config = MemberOfSharedConfig(inst, 'cn=memberOf config,{}'.format(DEFAULT_SUFFIX)) |
| 92 |
try: |
| 93 |
memberof_config.create(properties={'cn': 'memberOf config', |
| 94 |
'memberOfGroupAttr': 'member', |
| 95 |
'memberOfAttr': 'memberof'}) |
| 96 |
except ldap.ALREADY_EXISTS: |
| 97 |
pass |
| 98 |
|
| 99 |
# Enable plugin log level (usefull for debug) |
| 100 |
inst.setLogLevel(65536) |
| 101 |
inst.restart() |
| 102 |
|
| 103 |
def fin(): |
| 104 |
inst.restart() |
| 105 |
for am_config in am_configs_cleanup: |
| 106 |
am_config.delete() |
| 107 |
for g in group: |
| 108 |
try: |
| 109 |
g.delete() |
| 110 |
except: |
| 111 |
pass |
| 112 |
request.addfinalizer(fin) |
| 113 |
|
| 114 |
#unstable or unstatus tests, skipped for now |
| 115 |
#it fails, let's say 1 time out of 10, while decoding asn1 response |
| 116 |
@pytest.mark.flaky(max_runs=2, min_passes=1) |
| 117 |
@pytest.mark.skipif(ldap.__version__ < '3.3.1', |
| 118 |
reason="python ldap versions less that 3.3.1 have bugs in sync repl that will cause this to fail!") |
| 119 |
def test_syncrepl_basic(topology): |
| 120 |
""" Test basic functionality of the SyncRepl interface |
| 121 |
|
| 122 |
:id: f9fea826-8ae2-412a-8e88-b8e0ba939b06 |
| 123 |
|
| 124 |
:setup: Standalone instance |
| 125 |
|
| 126 |
:steps: |
| 127 |
1. Enable Retro Changelog |
| 128 |
2. Enable Syncrepl |
| 129 |
3. Run the syncstate test to check refresh, add, delete, mod. |
| 130 |
|
| 131 |
:expectedresults: |
| 132 |
1. Success |
| 133 |
1. Success |
| 134 |
1. Success |
| 135 |
""" |
| 136 |
st = topology.standalone |
| 137 |
# Enable RetroChangelog. |
| 138 |
rcl = RetroChangelogPlugin(st) |
| 139 |
rcl.enable() |
| 140 |
# Set the default targetid |
| 141 |
rcl.replace('nsslapd-attribute', 'nsuniqueid:targetUniqueId') |
| 142 |
# Enable sync repl |
| 143 |
csp = ContentSyncPlugin(st) |
| 144 |
csp.enable() |
| 145 |
# Restart DS |
| 146 |
st.restart() |
| 147 |
# Setup the syncer |
| 148 |
sync = ISyncRepl(st) |
| 149 |
# Run the checks |
| 150 |
syncstate_assert(st, sync) |
| 151 |
|
| 152 |
class TestSyncer(ReconnectLDAPObject, SyncreplConsumer): |
| 153 |
def __init__(self, *args, **kwargs): |
| 154 |
self.cookie = None |
| 155 |
self.cookies = [] |
| 156 |
ldap.ldapobject.ReconnectLDAPObject.__init__(self, *args, **kwargs) |
| 157 |
|
| 158 |
def syncrepl_set_cookie(self, cookie): |
| 159 |
# extract the changenumber from the cookie |
| 160 |
self.cookie = cookie |
| 161 |
self.cookies.append(cookie.split('#')[2]) |
| 162 |
log.info("XXXX Set cookie: %s" % cookie) |
| 163 |
|
| 164 |
def syncrepl_get_cookie(self): |
| 165 |
log.info("XXXX Get cookie: %s" % self.cookie) |
| 166 |
return self.cookie |
| 167 |
|
| 168 |
def syncrepl_present(self, uuids, refreshDeletes=False): |
| 169 |
log.info("XXXX syncrepl_present uuids %s %s" % ( uuids, refreshDeletes)) |
| 170 |
|
| 171 |
def syncrepl_delete(self, uuids): |
| 172 |
log.info("XXXX syncrepl_delete uuids %s" % uuids) |
| 173 |
|
| 174 |
def syncrepl_entry(self, dn, attrs, uuid): |
| 175 |
log.info("XXXX syncrepl_entry dn %s" % dn) |
| 176 |
|
| 177 |
def syncrepl_refreshdone(self): |
| 178 |
log.info("XXXX syncrepl_refreshdone") |
| 179 |
|
| 180 |
def get_cookies(self): |
| 181 |
return self.cookies |
| 182 |
|
| 183 |
class Sync_persist(threading.Thread, ReconnectLDAPObject, SyncreplConsumer): |
| 184 |
# This runs a sync_repl client in background |
| 185 |
# it registers a result that contain a list of the change numbers (from the cookie) |
| 186 |
# that are list as they are received |
| 187 |
def __init__(self, inst): |
| 188 |
threading.Thread.__init__(self) |
| 189 |
self.daemon = True |
| 190 |
self.inst = inst |
| 191 |
self.cookie = None |
| 192 |
self.conn = inst.clone({SER_ROOT_DN: 'cn=directory manager', SER_ROOT_PW: 'password'}) |
| 193 |
self.filterstr = '(|(objectClass=groupofnames)(objectClass=person))' |
| 194 |
self.attrs = [ |
| 195 |
'objectclass', |
| 196 |
'cn', |
| 197 |
'displayname', |
| 198 |
'gidnumber', |
| 199 |
'givenname', |
| 200 |
'homedirectory', |
| 201 |
'mail', |
| 202 |
'member', |
| 203 |
'memberof', |
| 204 |
'sn', |
| 205 |
'uid', |
| 206 |
'uidnumber', |
| 207 |
] |
| 208 |
self.conn.open() |
| 209 |
self.result = [] |
| 210 |
|
| 211 |
def get_result(self): |
| 212 |
# used to return the cookies list to the requestor |
| 213 |
return self.result |
| 214 |
|
| 215 |
def run(self): |
| 216 |
"""Start a sync repl client""" |
| 217 |
ldap_connection = TestSyncer(self.inst.toLDAPURL()) |
| 218 |
ldap_connection.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND) |
| 219 |
ldap_connection.set_option(ldap.OPT_X_TLS_CACERTFILE, os.path.join(self.inst.get_config_dir(), "ca.crt")) |
| 220 |
if is_fips(): |
| 221 |
ldap_connection.set_option(ldap.OPT_X_TLS_PROTOCOL_MIN, ldap.OPT_X_TLS_PROTOCOL_TLS1_2) |
| 222 |
ldap_connection.set_option(ldap.OPT_X_TLS_NEWCTX, 0) |
| 223 |
|
| 224 |
ldap_connection.simple_bind_s('cn=directory manager', 'password') |
| 225 |
ldap_search = ldap_connection.syncrepl_search( |
| 226 |
"dc=example,dc=com", |
| 227 |
ldap.SCOPE_SUBTREE, |
| 228 |
mode='refreshAndPersist', |
| 229 |
attrlist=self.attrs, |
| 230 |
filterstr=self.filterstr, |
| 231 |
cookie=None |
| 232 |
) |
| 233 |
|
| 234 |
try: |
| 235 |
while ldap_connection.syncrepl_poll(all=1, msgid=ldap_search): |
| 236 |
pass |
| 237 |
except (ldap.SERVER_DOWN, ldap.CONNECT_ERROR) as e: |
| 238 |
print('syncrepl_poll: LDAP error (%s)', e) |
| 239 |
self.result = ldap_connection.get_cookies() |
| 240 |
log.info("ZZZ result = %s" % self.result) |
| 241 |
ldap_connection.unbind() |
| 242 |
|
| 243 |
def test_sync_repl_mep(topology, request): |
| 244 |
"""Test sync repl with MEP plugin that triggers several |
| 245 |
updates on the same entry |
| 246 |
|
| 247 |
:id: d9515930-293e-42da-9835-9f255fa6111b |
| 248 |
:setup: Standalone Instance |
| 249 |
:steps: |
| 250 |
1. enable retro/sync_repl/mep |
| 251 |
2. Add mep Template and definition entry |
| 252 |
3. start sync_repl client |
| 253 |
4. Add users with PosixAccount ObjectClass (mep will update it several times) |
| 254 |
5. Check that the received cookie are progressing |
| 255 |
:expectedresults: |
| 256 |
1. Success |
| 257 |
2. Success |
| 258 |
3. Success |
| 259 |
4. Success |
| 260 |
5. Success |
| 261 |
""" |
| 262 |
inst = topology[0] |
| 263 |
inst.enable_tls() |
| 264 |
|
| 265 |
# Enable/configure retroCL |
| 266 |
plugin = RetroChangelogPlugin(inst) |
| 267 |
plugin.disable() |
| 268 |
plugin.enable() |
| 269 |
plugin.set('nsslapd-attribute', 'nsuniqueid:targetuniqueid') |
| 270 |
|
| 271 |
# Enable sync plugin |
| 272 |
plugin = ContentSyncPlugin(inst) |
| 273 |
plugin.enable() |
| 274 |
|
| 275 |
# Check the plug-in status |
| 276 |
mana = ManagedEntriesPlugin(inst) |
| 277 |
plugin.enable() |
| 278 |
|
| 279 |
# Add Template and definition entry |
| 280 |
org1 = OrganizationalUnits(inst, DEFAULT_SUFFIX).create(properties={'ou': 'Users'}) |
| 281 |
org2 = OrganizationalUnit(inst, f'ou=Groups,{DEFAULT_SUFFIX}') |
| 282 |
meps = MEPTemplates(inst, DEFAULT_SUFFIX) |
| 283 |
mep_template1 = meps.create(properties={ |
| 284 |
'cn': 'UPG Template1', |
| 285 |
'mepRDNAttr': 'cn', |
| 286 |
'mepStaticAttr': 'objectclass: posixGroup', |
| 287 |
'mepMappedAttr': 'cn: $uid|gidNumber: $gidNumber|description: User private group for $uid'.split('|')}) |
| 288 |
conf_mep = MEPConfigs(inst) |
| 289 |
mep_config = conf_mep.create(properties={ |
| 290 |
'cn': 'UPG Definition2', |
| 291 |
'originScope': org1.dn, |
| 292 |
'originFilter': 'objectclass=posixaccount', |
| 293 |
'managedBase': org2.dn, |
| 294 |
'managedTemplate': mep_template1.dn}) |
| 295 |
|
| 296 |
# Enable plugin log level (usefull for debug) |
| 297 |
inst.setLogLevel(65536) |
| 298 |
inst.restart() |
| 299 |
|
| 300 |
# create a sync repl client and wait 5 seconds to be sure it is running |
| 301 |
sync_repl = Sync_persist(inst) |
| 302 |
sync_repl.start() |
| 303 |
time.sleep(5) |
| 304 |
|
| 305 |
# Add users with PosixAccount ObjectClass and verify creation of User Private Group |
| 306 |
user = UserAccounts(inst, f'ou=Users,{DEFAULT_SUFFIX}', rdn=None).create_test_user() |
| 307 |
assert user.get_attr_val_utf8('mepManagedEntry') == f'cn=test_user_1000,ou=Groups,{DEFAULT_SUFFIX}' |
| 308 |
|
| 309 |
# stop the server to get the sync_repl result set (exit from while loop). |
| 310 |
# Only way I found to acheive that. |
| 311 |
# and wait a bit to let sync_repl thread time to set its result before fetching it. |
| 312 |
inst.stop() |
| 313 |
time.sleep(10) |
| 314 |
cookies = sync_repl.get_result() |
| 315 |
|
| 316 |
# checking that the cookie are in increasing and in an acceptable range (0..1000) |
| 317 |
assert len(cookies) > 0 |
| 318 |
prev = -1 |
| 319 |
for cookie in cookies: |
| 320 |
log.info('Check cookie %s' % cookie) |
| 321 |
|
| 322 |
assert int(cookie) >= 0 |
| 323 |
assert int(cookie) < 1000 |
| 324 |
assert int(cookie) > prev |
| 325 |
prev = int(cookie) |
| 326 |
sync_repl.join() |
| 327 |
log.info('test_sync_repl_map: PASS\n') |
| 328 |
|
| 329 |
def test_sync_repl_cookie(topology, init_sync_repl_plugins, request): |
| 330 |
"""Test sync_repl cookie are progressing is an increasing order |
| 331 |
when there are nested updates |
| 332 |
|
| 333 |
:id: d7fbde25-5702-46ac-b38e-169d7a68e97c |
| 334 |
:setup: Standalone Instance |
| 335 |
:steps: |
| 336 |
1.: initialization/cleanup done by init_sync_repl_plugins fixture |
| 337 |
2.: create a thread dedicated to run a sync repl client |
| 338 |
3.: Create (9) users that will generate nested updates (automember/memberof) |
| 339 |
4.: stop sync repl client and collect the list of cookie.change_no |
| 340 |
5.: check that cookies.change_no are in increasing order |
| 341 |
:expectedresults: |
| 342 |
1.: succeeds |
| 343 |
2.: succeeds |
| 344 |
3.: succeeds |
| 345 |
4.: succeeds |
| 346 |
5.: succeeds |
| 347 |
""" |
| 348 |
inst = topology[0] |
| 349 |
inst.enable_tls() |
| 350 |
|
| 351 |
# create a sync repl client and wait 5 seconds to be sure it is running |
| 352 |
sync_repl = Sync_persist(inst) |
| 353 |
sync_repl.start() |
| 354 |
time.sleep(5) |
| 355 |
|
| 356 |
# create users, that automember/memberof will generate nested updates |
| 357 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 358 |
users_set = [] |
| 359 |
for i in range(10001, 10010): |
| 360 |
users_set.append(users.create_test_user(uid=i)) |
| 361 |
|
| 362 |
# stop the server to get the sync_repl result set (exit from while loop). |
| 363 |
# Only way I found to acheive that. |
| 364 |
# and wait a bit to let sync_repl thread time to set its result before fetching it. |
| 365 |
inst.stop() |
| 366 |
time.sleep(10) |
| 367 |
cookies = sync_repl.get_result() |
| 368 |
|
| 369 |
# checking that the cookie are in increasing and in an acceptable range (0..1000) |
| 370 |
assert len(cookies) > 0 |
| 371 |
prev = -1 |
| 372 |
for cookie in cookies: |
| 373 |
log.info('Check cookie %s' % cookie) |
| 374 |
|
| 375 |
assert int(cookie) >= 0 |
| 376 |
assert int(cookie) < 1000 |
| 377 |
assert int(cookie) > prev |
| 378 |
prev = int(cookie) |
| 379 |
sync_repl.join() |
| 380 |
log.info('test_sync_repl_cookie: PASS\n') |
| 381 |
|
| 382 |
def fin(): |
| 383 |
inst.restart() |
| 384 |
for user in users_set: |
| 385 |
try: |
| 386 |
user.delete() |
| 387 |
except: |
| 388 |
pass |
| 389 |
|
| 390 |
request.addfinalizer(fin) |
| 391 |
|
| 392 |
return |
| 393 |
|
| 394 |
def test_sync_repl_cookie_add_del(topology, init_sync_repl_plugins, request): |
| 395 |
"""Test sync_repl cookie are progressing is an increasing order |
| 396 |
when there add and del |
| 397 |
|
| 398 |
:id: 83e11038-6ed0-4a5b-ac77-e44887ab11e3 |
| 399 |
:setup: Standalone Instance |
| 400 |
:steps: |
| 401 |
1.: initialization/cleanup done by init_sync_repl_plugins fixture |
| 402 |
2.: create a thread dedicated to run a sync repl client |
| 403 |
3.: Create (3) users that will generate nested updates (automember/memberof) |
| 404 |
4.: Delete (3) users |
| 405 |
5.: stop sync repl client and collect the list of cookie.change_no |
| 406 |
6.: check that cookies.change_no are in increasing order |
| 407 |
:expectedresults: |
| 408 |
1.: succeeds |
| 409 |
2.: succeeds |
| 410 |
3.: succeeds |
| 411 |
4.: succeeds |
| 412 |
5.: succeeds |
| 413 |
6.: succeeds |
| 414 |
""" |
| 415 |
inst = topology[0] |
| 416 |
inst.enable_tls() |
| 417 |
|
| 418 |
# create a sync repl client and wait 5 seconds to be sure it is running |
| 419 |
sync_repl = Sync_persist(inst) |
| 420 |
sync_repl.start() |
| 421 |
time.sleep(5) |
| 422 |
# create users, that automember/memberof will generate nested updates |
| 423 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 424 |
users_set = [] |
| 425 |
for i in range(10001, 10004): |
| 426 |
users_set.append(users.create_test_user(uid=i)) |
| 427 |
|
| 428 |
time.sleep(10) |
| 429 |
# delete users, that automember/memberof will generate nested updates |
| 430 |
for user in users_set: |
| 431 |
user.delete() |
| 432 |
# stop the server to get the sync_repl result set (exit from while loop). |
| 433 |
# Only way I found to acheive that. |
| 434 |
# and wait a bit to let sync_repl thread time to set its result before fetching it. |
| 435 |
inst.stop() |
| 436 |
cookies = sync_repl.get_result() |
| 437 |
# checking that the cookie are in increasing and in an acceptable range (0..1000) |
| 438 |
assert len(cookies) > 0 |
| 439 |
prev = -1 |
| 440 |
for cookie in cookies: |
| 441 |
log.info('Check cookie %s' % cookie) |
| 442 |
|
| 443 |
assert int(cookie) >= 0 |
| 444 |
assert int(cookie) < 1000 |
| 445 |
assert int(cookie) > prev |
| 446 |
prev = int(cookie) |
| 447 |
sync_repl.join() |
| 448 |
log.info('test_sync_repl_cookie_add_del: PASS\n') |
| 449 |
|
| 450 |
def fin(): |
| 451 |
pass |
| 452 |
|
| 453 |
request.addfinalizer(fin) |
| 454 |
|
| 455 |
return |
| 456 |
|
| 457 |
def test_sync_repl_cookie_with_failure(topology, init_sync_repl_plugins, request): |
| 458 |
"""Test sync_repl cookie are progressing is the right order |
| 459 |
when there is a failure in nested updates |
| 460 |
|
| 461 |
:id: e0103448-170e-4080-8f22-c34606447ce2 |
| 462 |
:setup: Standalone Instance |
| 463 |
:steps: |
| 464 |
1. initialization/cleanup done by init_sync_repl_plugins fixture |
| 465 |
2. update group2 so that it will not accept 'member' attribute (set by memberof) |
| 466 |
3. create a thread dedicated to run a sync repl client |
| 467 |
4. Create a group that will be the only update received by sync repl client |
| 468 |
5. Create (9) users that will generate nested updates (automember/memberof). Creation will fail because 'member' attribute is not allowed in group2 |
| 469 |
6. stop sync repl client and collect the list of cookie.change_no |
| 470 |
7. check that the list of cookie.change_no contains only the group 'step 11' |
| 471 |
:expectedresults: |
| 472 |
1. succeeds |
| 473 |
2. succeeds |
| 474 |
3. succeeds |
| 475 |
4. succeeds |
| 476 |
5. Fails (expected) |
| 477 |
6. succeeds |
| 478 |
7. succeeds |
| 479 |
""" |
| 480 |
inst = topology[0] |
| 481 |
|
| 482 |
# Set group2 as a groupOfUniqueNames so that automember will fail to update that group |
| 483 |
# This will trigger a failure in internal MOD and a failure to add member |
| 484 |
group2 = Groups(inst, DEFAULT_SUFFIX).get('group2') |
| 485 |
group2.replace('objectclass', 'groupOfUniqueNames') |
| 486 |
|
| 487 |
|
| 488 |
# create a sync repl client and wait 5 seconds to be sure it is running |
| 489 |
sync_repl = Sync_persist(inst) |
| 490 |
sync_repl.start() |
| 491 |
time.sleep(5) |
| 492 |
|
| 493 |
# Add a test group just to check that sync_repl receives that SyncControlInfo cookie |
| 494 |
groups = Groups(inst, DEFAULT_SUFFIX) |
| 495 |
testgroup = groups.create(properties={'cn': 'group%d' % 10}) |
| 496 |
|
| 497 |
# create users, that automember/memberof will generate nested updates |
| 498 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 499 |
users_set = [] |
| 500 |
for i in range(1000,1010): |
| 501 |
try: |
| 502 |
users_set.append(users.create_test_user(uid=i)) |
| 503 |
# Automember should fail to add uid=1000 in group2 |
| 504 |
assert(False) |
| 505 |
except ldap.UNWILLING_TO_PERFORM: |
| 506 |
pass |
| 507 |
|
| 508 |
# stop the server to get the sync_repl result set (exit from while loop). |
| 509 |
# Only way I found to acheive that. |
| 510 |
# and wait a bit to let sync_repl thread time to set its result before fetching it. |
| 511 |
inst.stop() |
| 512 |
time.sleep(10) |
| 513 |
cookies = sync_repl.get_result() |
| 514 |
|
| 515 |
# checking that the cookie list contains only two entries |
| 516 |
# the one from the SyncInfo/RefreshDelete that indicates the end of the refresh |
| 517 |
# the the one from SyncStateControl related to the only updated entry (group10) |
| 518 |
assert len(cookies) == 2 |
| 519 |
prev = -1 |
| 520 |
for cookie in cookies: |
| 521 |
log.info('Check cookie %s' % cookie) |
| 522 |
|
| 523 |
assert int(cookie) >= 0 |
| 524 |
assert int(cookie) < 1000 |
| 525 |
assert int(cookie) > prev |
| 526 |
prev = int(cookie) |
| 527 |
sync_repl.join() |
| 528 |
log.info('test_sync_repl_cookie_with_failure: PASS\n') |
| 529 |
|
| 530 |
def fin(): |
| 531 |
inst.restart() |
| 532 |
for user in users_set: |
| 533 |
try: |
| 534 |
user.delete() |
| 535 |
except: |
| 536 |
pass |
| 537 |
testgroup.delete() |
| 538 |
|
| 539 |
request.addfinalizer(fin) |
| 540 |
|
| 541 |
def test_sync_repl_cenotaph(topo_m2, request): |
| 542 |
"""Test the creation of a cenotaph while a |
| 543 |
sync repl client is running |
| 544 |
|
| 545 |
:id: 8ca1724a-cf42-4880-bf0f-be451f9bd3b4 |
| 546 |
:setup: MMR with 2 suppliers |
| 547 |
:steps: |
| 548 |
1. Enable retroCL/content_sync |
| 549 |
2. Run a sync repl client |
| 550 |
3. create users |
| 551 |
4. do a MODRDN of a user entry => creation of cenotaph |
| 552 |
5. stop sync repl client |
| 553 |
:expectedresults: |
| 554 |
1. Should succeeds |
| 555 |
2. Should succeeds |
| 556 |
3. Should succeeds |
| 557 |
4. Should succeeds |
| 558 |
5. Should succeeds |
| 559 |
""" |
| 560 |
m1 = topo_m2.ms["supplier1"] |
| 561 |
m1.enable_tls() |
| 562 |
# Enable/configure retroCL |
| 563 |
plugin = RetroChangelogPlugin(m1) |
| 564 |
plugin.disable() |
| 565 |
plugin.enable() |
| 566 |
plugin.set('nsslapd-attribute', 'nsuniqueid:targetuniqueid') |
| 567 |
|
| 568 |
# Enable sync plugin |
| 569 |
plugin = ContentSyncPlugin(m1) |
| 570 |
plugin.enable() |
| 571 |
# Restart DS |
| 572 |
m1.restart() |
| 573 |
|
| 574 |
# create a sync repl client and wait 5 seconds to be sure it is running |
| 575 |
sync_repl = Sync_persist(m1) |
| 576 |
sync_repl.start() |
| 577 |
time.sleep(5) |
| 578 |
|
| 579 |
# create users |
| 580 |
users = UserAccounts(m1, DEFAULT_SUFFIX) |
| 581 |
users_set = [] |
| 582 |
for i in range(10001, 10003): |
| 583 |
users_set.append(users.create_test_user(uid=i)) |
| 584 |
|
| 585 |
# rename the entry that would trigger the creation of a cenotaph |
| 586 |
users_set[0].rename("uid=foo") |
| 587 |
|
| 588 |
# stop the server to get the sync_repl result set (exit from while loop). |
| 589 |
# Only way I found to acheive that. |
| 590 |
# and wait a bit to let sync_repl thread time to set its result before fetching it. |
| 591 |
m1.stop() |
| 592 |
time.sleep(2) |
| 593 |
|
| 594 |
def fin(): |
| 595 |
m1.restart() |
| 596 |
for user in users_set: |
| 597 |
try: |
| 598 |
user.delete() |
| 599 |
except: |
| 600 |
pass |
| 601 |
|
| 602 |
request.addfinalizer(fin) |
| 603 |
|
| 604 |
def test_sync_repl_dynamic_plugin(topology, request): |
| 605 |
"""Test sync_repl with dynamic plugin |
| 606 |
|
| 607 |
:id: d4f84913-c18a-459f-8525-110f610ca9e6 |
| 608 |
:setup: install a standalone instance |
| 609 |
:steps: |
| 610 |
1. reset instance to standard (no retroCL, no sync_repl, no dynamic plugin) |
| 611 |
2. Enable dynamic plugin |
| 612 |
3. Enable retroCL/content_sync |
| 613 |
4. Establish a sync_repl req |
| 614 |
:expectedresults: |
| 615 |
1. Should succeeds |
| 616 |
2. Should succeeds |
| 617 |
3. Should succeeds |
| 618 |
4. Should succeeds |
| 619 |
""" |
| 620 |
topology.standalone.enable_tls() |
| 621 |
# Reset the instance in a default config |
| 622 |
# Disable content sync plugin |
| 623 |
topology.standalone.plugins.disable(name=PLUGIN_REPL_SYNC) |
| 624 |
|
| 625 |
# Disable retro changelog |
| 626 |
topology.standalone.plugins.disable(name=PLUGIN_RETRO_CHANGELOG) |
| 627 |
|
| 628 |
# Disable dynamic plugins |
| 629 |
topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', b'off')]) |
| 630 |
topology.standalone.restart() |
| 631 |
|
| 632 |
# Now start the test |
| 633 |
# Enable dynamic plugins |
| 634 |
try: |
| 635 |
topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', b'on')]) |
| 636 |
except ldap.LDAPError as e: |
| 637 |
log.error('Failed to enable dynamic plugin! {}'.format(e.args[0]['desc'])) |
| 638 |
assert False |
| 639 |
|
| 640 |
# Enable retro changelog |
| 641 |
topology.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG) |
| 642 |
|
| 643 |
# Enbale content sync plugin |
| 644 |
topology.standalone.plugins.enable(name=PLUGIN_REPL_SYNC) |
| 645 |
|
| 646 |
# create a sync repl client and wait 5 seconds to be sure it is running |
| 647 |
sync_repl = Sync_persist(topology.standalone) |
| 648 |
sync_repl.start() |
| 649 |
time.sleep(5) |
| 650 |
|
| 651 |
# create users |
| 652 |
users = UserAccounts(topology.standalone, DEFAULT_SUFFIX) |
| 653 |
users_set = [] |
| 654 |
for i in range(10001, 10004): |
| 655 |
users_set.append(users.create_test_user(uid=i)) |
| 656 |
|
| 657 |
time.sleep(10) |
| 658 |
# delete users, that automember/memberof will generate nested updates |
| 659 |
for user in users_set: |
| 660 |
user.delete() |
| 661 |
# stop the server to get the sync_repl result set (exit from while loop). |
| 662 |
# Only way I found to acheive that. |
| 663 |
# and wait a bit to let sync_repl thread time to set its result before fetching it. |
| 664 |
topology.standalone.stop() |
| 665 |
sync_repl.get_result() |
| 666 |
sync_repl.join() |
| 667 |
log.info('test_sync_repl_dynamic_plugin: PASS\n') |
| 668 |
|
| 669 |
# Success |
| 670 |
log.info('Test complete') |
| 671 |
|
| 672 |
def test_sync_repl_invalid_cookie(topology, request): |
| 673 |
"""Test sync_repl with invalid cookie |
| 674 |
|
| 675 |
:id: 8fa4a8f8-acf4-42a5-90f1-6ba1d8080e46 |
| 676 |
:setup: install a standalone instance |
| 677 |
:steps: |
| 678 |
1. reset instance to standard (no retroCL, no sync_repl, no dynamic plugin) |
| 679 |
2. Enable retroCL/content_sync |
| 680 |
3. Establish a sync_repl connection |
| 681 |
4. Tests servers results to search with invalid cookie |
| 682 |
5. Add/delete an user entry to check the server is up and running |
| 683 |
:expectedresults: |
| 684 |
1. Should succeeds |
| 685 |
2. Should succeeds |
| 686 |
3. Should succeeds |
| 687 |
4. Should succeeds |
| 688 |
5. Should succeeds |
| 689 |
""" |
| 690 |
|
| 691 |
# Reset the instance in a default config |
| 692 |
# Disable content sync plugin |
| 693 |
topology.standalone.restart() |
| 694 |
topology.standalone.plugins.disable(name=PLUGIN_REPL_SYNC) |
| 695 |
|
| 696 |
# Disable retro changelog |
| 697 |
topology.standalone.plugins.disable(name=PLUGIN_RETRO_CHANGELOG) |
| 698 |
|
| 699 |
# Disable dynamic plugins |
| 700 |
topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', b'off')]) |
| 701 |
topology.standalone.restart() |
| 702 |
|
| 703 |
# Enable retro changelog |
| 704 |
topology.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG) |
| 705 |
|
| 706 |
# Enbale content sync plugin |
| 707 |
topology.standalone.plugins.enable(name=PLUGIN_REPL_SYNC) |
| 708 |
topology.standalone.restart() |
| 709 |
|
| 710 |
# Setup the syncer |
| 711 |
sync = ISyncRepl(topology.standalone) |
| 712 |
|
| 713 |
# Test invalid cookies |
| 714 |
cookies = ('#', '##', 'a#a#a', 'a#a#1', 'foo') |
| 715 |
for invalid_cookie in cookies: |
| 716 |
log.info('Testing cookie: %s' % invalid_cookie) |
| 717 |
try: |
| 718 |
ldap_search = sync.syncrepl_search(base=DEFAULT_SUFFIX, |
| 719 |
scope=ldap.SCOPE_SUBTREE, |
| 720 |
attrlist=['objectclass', 'cn', 'homedirectory', 'sn','uid'], |
| 721 |
filterstr='(|(objectClass=groupofnames)(objectClass=person))', |
| 722 |
mode='refreshOnly', |
| 723 |
cookie=invalid_cookie) |
| 724 |
poll_result = sync.syncrepl_poll(all=1) |
| 725 |
|
| 726 |
log.fatal('Invalid cookie accepted!') |
| 727 |
assert False |
| 728 |
except Exception as e: |
| 729 |
log.info('Invalid cookie correctly rejected: {}'.format(e.args[0]['info'])) |
| 730 |
pass |
| 731 |
|
| 732 |
# check that the server is still up and running |
| 733 |
users = UserAccounts(topology.standalone, DEFAULT_SUFFIX) |
| 734 |
user = users.create_test_user(uid=1000) |
| 735 |
|
| 736 |
# Success |
| 737 |
log.info('Test complete') |
| 738 |
|
| 739 |
def fin(): |
| 740 |
topology.standalone.restart() |
| 741 |
try: |
| 742 |
user.delete() |
| 743 |
except: |
| 744 |
pass |
| 745 |
|
| 746 |
request.addfinalizer(fin) |
| 747 |
|
| 748 |
|
| 749 |
def test_sync_repl_non_root_persist(topology, request): |
| 750 |
"""Test sync_repl persistent search as a non-root user triggers |
| 751 |
proper ACL evaluation without causing OPERATIONS_ERROR. |
| 752 |
|
| 753 |
:id: 97f4be3d-f8c3-45cf-8295-ed58ee7bd371 |
| 754 |
:setup: Standalone Instance |
| 755 |
:steps: |
| 756 |
1. Enable RetroChangelog and ContentSync plugins |
| 757 |
2. Create a test user and set its password |
| 758 |
3. Add an ACI granting the test user read access |
| 759 |
4. Run a syncrepl persistent search bound as the test user |
| 760 |
5. Add entries while the persistent search is active |
| 761 |
6. Stop the server to end the sync thread |
| 762 |
7. Check that the sync client received results without error |
| 763 |
8. Check the error log for 'Missing aclpb' messages |
| 764 |
:expectedresults: |
| 765 |
1. Success |
| 766 |
2. Success |
| 767 |
3. Success |
| 768 |
4. Success - no OPERATIONS_ERROR |
| 769 |
5. Success |
| 770 |
6. Success |
| 771 |
7. The sync client should have received cookies |
| 772 |
8. No 'Missing aclpb' errors in the log |
| 773 |
""" |
| 774 |
inst = topology.standalone |
| 775 |
inst.restart() |
| 776 |
inst.enable_tls() |
| 777 |
|
| 778 |
# Enable RetroChangelog |
| 779 |
rcl = RetroChangelogPlugin(inst) |
| 780 |
rcl.disable() |
| 781 |
rcl.enable() |
| 782 |
rcl.set('nsslapd-attribute', 'nsuniqueid:targetUniqueId') |
| 783 |
|
| 784 |
# Enable Content Sync plugin |
| 785 |
csp = ContentSyncPlugin(inst) |
| 786 |
csp.enable() |
| 787 |
|
| 788 |
inst.restart() |
| 789 |
|
| 790 |
# Create the syncrepl user |
| 791 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 792 |
sync_user = users.create_test_user(uid=29999) |
| 793 |
sync_user.set('userPassword', 'sync_password') |
| 794 |
SYNC_USER_DN = sync_user.dn |
| 795 |
|
| 796 |
# Add ACI granting the sync user read access to the suffix |
| 797 |
ACI = ( |
| 798 |
'(targetattr="*")(version 3.0; acl "Sync user read access";' |
| 799 |
' allow (read, search, compare)' |
| 800 |
' userdn = "ldap:///%s";)' % SYNC_USER_DN |
| 801 |
) |
| 802 |
inst.modify_s(DEFAULT_SUFFIX, |
| 803 |
[(ldap.MOD_ADD, 'aci', ensure_bytes(ACI))]) |
| 804 |
|
| 805 |
# Start a syncrepl persistent search as the non-root user |
| 806 |
# Using the Sync_persist-like pattern but with non-root bind |
| 807 |
sync_error = [None] # mutable container to capture errors from thread |
| 808 |
sync_cookies = [] |
| 809 |
|
| 810 |
class NonRootSyncer(ReconnectLDAPObject, SyncreplConsumer): |
| 811 |
def __init__(self, *args, **kwargs): |
| 812 |
self.cookie = None |
| 813 |
self.cookies = [] |
| 814 |
ldap.ldapobject.ReconnectLDAPObject.__init__(self, *args, **kwargs) |
| 815 |
|
| 816 |
def syncrepl_set_cookie(self, cookie): |
| 817 |
self.cookie = cookie |
| 818 |
self.cookies.append(cookie.split('#')[2]) |
| 819 |
|
| 820 |
def syncrepl_get_cookie(self): |
| 821 |
return self.cookie |
| 822 |
|
| 823 |
def syncrepl_present(self, uuids, refreshDeletes=False): |
| 824 |
pass |
| 825 |
|
| 826 |
def syncrepl_delete(self, uuids): |
| 827 |
pass |
| 828 |
|
| 829 |
def syncrepl_entry(self, dn, attrs, uuid): |
| 830 |
log.info("NonRootSyncer received entry: %s" % dn) |
| 831 |
|
| 832 |
def syncrepl_refreshdone(self): |
| 833 |
log.info("NonRootSyncer refresh done") |
| 834 |
|
| 835 |
def get_cookies(self): |
| 836 |
return self.cookies |
| 837 |
|
| 838 |
def sync_thread_func(): |
| 839 |
ldap_conn = None |
| 840 |
try: |
| 841 |
ldap_conn = NonRootSyncer(inst.toLDAPURL()) |
| 842 |
ldap_conn.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND) |
| 843 |
ldap_conn.set_option(ldap.OPT_X_TLS_CACERTFILE, |
| 844 |
os.path.join(inst.get_config_dir(), "ca.crt")) |
| 845 |
if is_fips(): |
| 846 |
ldap_conn.set_option(ldap.OPT_X_TLS_PROTOCOL_MIN, |
| 847 |
ldap.OPT_X_TLS_PROTOCOL_TLS1_2) |
| 848 |
ldap_conn.set_option(ldap.OPT_X_TLS_NEWCTX, 0) |
| 849 |
|
| 850 |
# Bind as the non-root user |
| 851 |
ldap_conn.simple_bind_s(SYNC_USER_DN, 'sync_password') |
| 852 |
|
| 853 |
ldap_search = ldap_conn.syncrepl_search( |
| 854 |
DEFAULT_SUFFIX, |
| 855 |
ldap.SCOPE_SUBTREE, |
| 856 |
mode='refreshAndPersist', |
| 857 |
attrlist=['objectclass', 'cn', 'uid', 'sn'], |
| 858 |
filterstr='(objectClass=person)', |
| 859 |
cookie=None |
| 860 |
) |
| 861 |
|
| 862 |
while ldap_conn.syncrepl_poll(all=1, msgid=ldap_search): |
| 863 |
pass |
| 864 |
except ldap.OPERATIONS_ERROR as e: |
| 865 |
log.error("SyncRepl got OPERATIONS_ERROR: %s" % e) |
| 866 |
sync_error[0] = e |
| 867 |
except (ldap.SERVER_DOWN, ldap.CONNECT_ERROR): |
| 868 |
pass |
| 869 |
finally: |
| 870 |
if ldap_conn: |
| 871 |
sync_cookies.extend(ldap_conn.get_cookies()) |
| 872 |
|
| 873 |
t = threading.Thread(target=sync_thread_func) |
| 874 |
t.daemon = True |
| 875 |
t.start() |
| 876 |
time.sleep(5) |
| 877 |
|
| 878 |
# Add entries while persistent search is active (as directory manager) |
| 879 |
test_users = [] |
| 880 |
for i in range(20001, 20004): |
| 881 |
test_users.append(users.create_test_user(uid=i)) |
| 882 |
time.sleep(5) |
| 883 |
|
| 884 |
# Stop the server to end the sync thread cleanly |
| 885 |
inst.stop() |
| 886 |
t.join(timeout=30) |
| 887 |
|
| 888 |
# Verify no OPERATIONS_ERROR was raised |
| 889 |
assert sync_error[0] is None, \ |
| 890 |
"SyncRepl persistent search as non-root user got OPERATIONS_ERROR: %s" % sync_error[0] |
| 891 |
|
| 892 |
# Verify the sync client received some cookies (entries were synced) |
| 893 |
assert len(sync_cookies) > 0, \ |
| 894 |
"SyncRepl persistent search as non-root user received no cookies" |
| 895 |
|
| 896 |
# Restart and check error log for the specific aclpb error |
| 897 |
inst.start() |
| 898 |
assert not inst.searchErrorsLog('Missing aclpb'), \ |
| 899 |
"Found 'Missing aclpb' errors in the error log" |
| 900 |
|
| 901 |
log.info('test_sync_repl_non_root_persist: PASS') |
| 902 |
|
| 903 |
def fin(): |
| 904 |
inst.restart() |
| 905 |
try: |
| 906 |
inst.modify_s(DEFAULT_SUFFIX, |
| 907 |
[(ldap.MOD_DELETE, 'aci', ensure_bytes(ACI))]) |
| 908 |
except: |
| 909 |
pass |
| 910 |
for user in test_users: |
| 911 |
try: |
| 912 |
user.delete() |
| 913 |
except: |
| 914 |
pass |
| 915 |
try: |
| 916 |
sync_user.delete() |
| 917 |
except: |
| 918 |
pass |
| 919 |
|
| 920 |
request.addfinalizer(fin) |