| 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 os |
| 11 |
import ldap |
| 12 |
import time |
| 13 |
import pytest |
| 14 |
from lib389._constants import DEFAULT_SUFFIX |
| 15 |
from test389.topologies import topology_m2 |
| 16 |
from lib389.idm.user import UserAccounts |
| 17 |
from lib389.replica import ReplicationManager |
| 18 |
from lib389._mapped_object import DSLdapObject |
| 19 |
from lib389.schema import Schema, OBJECT_MODEL_PARAMS |
| 20 |
from ldap.schema.models import ObjectClass |
| 21 |
|
| 22 |
pytestmark = pytest.mark.tier2 |
| 23 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 24 |
log = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
def wait_for_attr_value(instance, dn, attr, value=None, timeout=30): |
| 28 |
"""Wait for an attribute to have a specific value or be absent.""" |
| 29 |
|
| 30 |
for _ in range(timeout): |
| 31 |
try: |
| 32 |
entry_obj = DSLdapObject(instance, dn) |
| 33 |
if value is None: |
| 34 |
if not entry_obj.present(attr): |
| 35 |
return True |
| 36 |
else: |
| 37 |
if entry_obj.present(attr, value): |
| 38 |
return True |
| 39 |
except (ldap.NO_SUCH_OBJECT, ldap.SERVER_DOWN, ldap.CONNECT_ERROR): |
| 40 |
pass # Expected during server restart |
| 41 |
except Exception as e: |
| 42 |
log.debug(f"Error checking {dn}: {e}") |
| 43 |
|
| 44 |
time.sleep(1) |
| 45 |
|
| 46 |
if value is None: |
| 47 |
raise AssertionError(f"Timeout: attribute '{attr}' still present in {dn} after {timeout}s") |
| 48 |
else: |
| 49 |
raise AssertionError(f"Timeout: attribute '{attr}' does not have value '{value}' in {dn} after {timeout}s") |
| 50 |
|
| 51 |
def _add_oc(instance, oid, name): |
| 52 |
"""Add an objectClass""" |
| 53 |
schema = Schema(instance) |
| 54 |
params = OBJECT_MODEL_PARAMS[ObjectClass].copy() |
| 55 |
params.update({ |
| 56 |
'names': (name,), |
| 57 |
'oid': oid, |
| 58 |
'desc': 'To test schema update policies', |
| 59 |
'must': ('postalAddress', 'postalCode'), |
| 60 |
'may': ('member', 'street'), |
| 61 |
'sup': ('person',), |
| 62 |
}) |
| 63 |
schema.add_objectclass(params) |
| 64 |
|
| 65 |
|
| 66 |
@pytest.fixture |
| 67 |
def temporary_oc2(topology_m2): |
| 68 |
"""Fixture to create and automatically clean up OC2UpdatePolicy.""" |
| 69 |
supplier1 = topology_m2.ms["supplier1"] |
| 70 |
supplier2 = topology_m2.ms["supplier2"] |
| 71 |
name = "OC2UpdatePolicy" |
| 72 |
|
| 73 |
log.info(f"Adding temporary objectclass: {name}") |
| 74 |
_add_oc(supplier1, "1.2.3.4.5.6.7.8.9.10.3", name) |
| 75 |
|
| 76 |
yield name |
| 77 |
|
| 78 |
log.info(f"Cleaning up temporary objectclass: {name}") |
| 79 |
for s in [supplier1, supplier2]: |
| 80 |
try: |
| 81 |
schema = Schema(s) |
| 82 |
schema.remove_objectclass(name) |
| 83 |
except (ldap.NO_SUCH_OBJECT, ValueError): |
| 84 |
pass |
| 85 |
|
| 86 |
|
| 87 |
@pytest.fixture(scope="function") |
| 88 |
def setup_test_env(request, topology_m2): |
| 89 |
"""Initialize the test environment""" |
| 90 |
supplier1 = topology_m2.ms["supplier1"] |
| 91 |
supplier2 = topology_m2.ms["supplier2"] |
| 92 |
|
| 93 |
log.info("Add OCUpdatePolicy that allows 'member' attribute") |
| 94 |
_add_oc(supplier1, "1.2.3.4.5.6.7.8.9.10.2", "OCUpdatePolicy") |
| 95 |
|
| 96 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 97 |
repl.wait_for_replication(supplier1, supplier2) |
| 98 |
repl.wait_for_replication(supplier2, supplier1) |
| 99 |
|
| 100 |
users = UserAccounts(supplier1, DEFAULT_SUFFIX) |
| 101 |
users.create_test_user(uid=1000) |
| 102 |
|
| 103 |
config = supplier1.config |
| 104 |
config.replace_many( |
| 105 |
('nsslapd-errorlog-level', str(128 + 8192)) |
| 106 |
) |
| 107 |
|
| 108 |
config2 = supplier2.config |
| 109 |
config2.replace_many( |
| 110 |
('nsslapd-errorlog-level', str(128 + 8192)) |
| 111 |
) |
| 112 |
|
| 113 |
for i in range(10): |
| 114 |
users.create_test_user(uid=2000 + i) |
| 115 |
|
| 116 |
log.info("Create main test entry") |
| 117 |
members = [f"uid=test_user_{2000 + i},ou=People,{DEFAULT_SUFFIX}" for i in range(10)] |
| 118 |
members.append(f"uid=test_user_1000,ou=People,{DEFAULT_SUFFIX}") |
| 119 |
users.create(properties={ |
| 120 |
'uid': 'test_entry', |
| 121 |
'cn': 'test_entry', |
| 122 |
'sn': 'test_entry', |
| 123 |
'objectClass': ['top', 'person', 'organizationalPerson', |
| 124 |
'inetOrgPerson', 'posixAccount', 'account', 'OCUpdatePolicy'], |
| 125 |
'postalAddress': 'here', |
| 126 |
'postalCode': '1234', |
| 127 |
'member': members, |
| 128 |
'uidNumber': '3000', |
| 129 |
'gidNumber': '3000', |
| 130 |
'homeDirectory': '/home/test_entry' |
| 131 |
}) |
| 132 |
|
| 133 |
def fin(): |
| 134 |
users = UserAccounts(supplier1, DEFAULT_SUFFIX) |
| 135 |
for user in users.list(): |
| 136 |
user.delete() |
| 137 |
|
| 138 |
config.replace('nsslapd-errorlog-level', '0') |
| 139 |
config2.replace('nsslapd-errorlog-level', '0') |
| 140 |
|
| 141 |
schema = Schema(supplier1) |
| 142 |
try: |
| 143 |
schema.remove_objectclass("OCUpdatePolicy") |
| 144 |
except (ldap.NO_SUCH_OBJECT, ValueError): |
| 145 |
pass |
| 146 |
|
| 147 |
request.addfinalizer(fin) |
| 148 |
|
| 149 |
|
| 150 |
def test_schema_update_policy_allow(topology_m2, setup_test_env): |
| 151 |
"""Test that schema updates are allowed and replicated when no reject policy is set |
| 152 |
|
| 153 |
:id: c8e3d2e4-5b7a-4a9d-9f2e-8a5b3c4d6e7f |
| 154 |
:setup: Two supplier replication setup with test entries |
| 155 |
:steps: |
| 156 |
1. Add an entry with custom objectclass on supplier1 |
| 157 |
2. Check entry is replicated to supplier2 |
| 158 |
3. Update entry on supplier2 |
| 159 |
4. Check update is replicated back to supplier1 |
| 160 |
:expectedresults: |
| 161 |
1. Entry should be added successfully |
| 162 |
2. Entry should be replicated with schema |
| 163 |
3. Update should succeed |
| 164 |
4. Update should be replicated |
| 165 |
""" |
| 166 |
supplier1 = topology_m2.ms["supplier1"] |
| 167 |
supplier2 = topology_m2.ms["supplier2"] |
| 168 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 169 |
|
| 170 |
log.info("Check entry was replicated to supplier2") |
| 171 |
repl.wait_for_replication(supplier1, supplier2) |
| 172 |
users = UserAccounts(supplier1, DEFAULT_SUFFIX) |
| 173 |
users2 = UserAccounts(supplier2, DEFAULT_SUFFIX) |
| 174 |
entry = users2.get('test_entry') |
| 175 |
assert entry |
| 176 |
|
| 177 |
log.info("Update entry on supplier2") |
| 178 |
entry.replace('description', 'test_add') |
| 179 |
repl.wait_for_replication(supplier2, supplier1) |
| 180 |
entry = users.get('test_entry') |
| 181 |
assert entry.get_attr_val_utf8('description') == 'test_add' |
| 182 |
|
| 183 |
|
| 184 |
def test_schema_update_policy_reject(topology_m2, setup_test_env, temporary_oc2): |
| 185 |
"""Test that schema updates can be rejected based on policy |
| 186 |
|
| 187 |
:id: d9f4e3b5-6c8b-5b0e-0f3a-9b6c5d8e9g0 |
| 188 |
:setup: Two supplier replication setup with test entries |
| 189 |
:steps: |
| 190 |
1. Configure supplier1 to reject schema updates containing OC_NAME |
| 191 |
2. Add a new objectclass on supplier1 |
| 192 |
3. Update an entry to trigger schema push |
| 193 |
4. Verify schema was not pushed to supplier2 |
| 194 |
5. Remove reject policy |
| 195 |
6. Update entry again |
| 196 |
7. Verify schema is now pushed to supplier2 |
| 197 |
:expectedresults: |
| 198 |
1. Policy should be configured |
| 199 |
2. New objectclass should be added |
| 200 |
3. Update should trigger replication |
| 201 |
4. Schema should not be pushed due to policy |
| 202 |
5. Policy should be removed |
| 203 |
6. Update should trigger replication |
| 204 |
7. Schema should now be pushed |
| 205 |
""" |
| 206 |
supplier1 = topology_m2.ms["supplier1"] |
| 207 |
supplier2 = topology_m2.ms["supplier2"] |
| 208 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 209 |
|
| 210 |
log.info("Configure supplier to reject schema updates for OCUpdatePolicy") |
| 211 |
policy_dn = f"cn=supplierUpdatePolicy,cn=replSchema,{supplier1.config.dn}" |
| 212 |
policy_entry = DSLdapObject(supplier1, policy_dn) |
| 213 |
policy_entry.add('schemaUpdateObjectclassReject', 'OCUpdatePolicy') |
| 214 |
supplier1.restart() |
| 215 |
wait_for_attr_value(supplier1, policy_dn, 'schemaUpdateObjectclassReject', 'OCUpdatePolicy') |
| 216 |
|
| 217 |
log.info("Verify OC2UpdatePolicy is in supplier1") |
| 218 |
schema = Schema(supplier1) |
| 219 |
schema_attrs = schema.get_objectclasses() |
| 220 |
assert any('oc2updatepolicy' in (name.lower() for name in oc.names) for oc in schema_attrs) |
| 221 |
|
| 222 |
log.info("Update entry on supplier1 to trigger schema push") |
| 223 |
users = UserAccounts(supplier1, DEFAULT_SUFFIX) |
| 224 |
test_user = users.get('test_entry') |
| 225 |
test_user.replace('description', 'test_reject') |
| 226 |
|
| 227 |
log.info("Check update was replicated") |
| 228 |
repl.wait_for_replication(supplier1, supplier2) |
| 229 |
users2 = UserAccounts(supplier2, DEFAULT_SUFFIX) |
| 230 |
entry = users2.get('test_entry') |
| 231 |
assert entry.get_attr_val_utf8('description') == 'test_reject' |
| 232 |
|
| 233 |
log.info("Verify OC2UpdatePolicy was NOT pushed to supplier2") |
| 234 |
schema_attrs = supplier2.schema.get_objectclasses() |
| 235 |
assert not any('oc2updatepolicy' in (name.lower() for name in oc.names) for oc in schema_attrs) |
| 236 |
|
| 237 |
log.info("Remove reject policy") |
| 238 |
policy_entry.remove('schemaUpdateObjectclassReject', 'OCUpdatePolicy') |
| 239 |
supplier1.restart() |
| 240 |
wait_for_attr_value(supplier1, policy_dn, 'schemaUpdateObjectclassReject', None) |
| 241 |
|
| 242 |
log.info("Update entry again to trigger schema push") |
| 243 |
test_user.replace('description', 'test_no_more_reject') |
| 244 |
|
| 245 |
log.info("Check update was replicated") |
| 246 |
repl.wait_for_replication(supplier1, supplier2) |
| 247 |
entry = users2.get('test_entry') |
| 248 |
assert entry.get_attr_val_utf8('description') == 'test_no_more_reject' |
| 249 |
|
| 250 |
log.info("Verify OC2UpdatePolicy is now in supplier2") |
| 251 |
schema_attrs = supplier2.schema.get_objectclasses() |
| 252 |
assert any('oc2updatepolicy' in (name.lower() for name in oc.names) for oc in schema_attrs) |
| 253 |
|
| 254 |
|
| 255 |
if __name__ == '__main__': |
| 256 |
# Run isolated |
| 257 |
# -s for DEBUG mode |
| 258 |
CURRENT_FILE = os.path.realpath(__file__) |
| 259 |
pytest.main(["-s", CURRENT_FILE]) |