| 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 os |
| 10 |
import logging |
| 11 |
import ldap |
| 12 |
import pytest |
| 13 |
import time |
| 14 |
import pwd, grp |
| 15 |
from lib389._constants import DEFAULT_SUFFIX |
| 16 |
from lib389.idm.user import UserAccounts |
| 17 |
from lib389.replica import ReplicationManager, ReplicaRole |
| 18 |
from lib389.schema import Schema |
| 19 |
from test389.topologies import topology_st, create_topology |
| 20 |
|
| 21 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 22 |
log = logging.getLogger(__name__) |
| 23 |
|
| 24 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 25 |
|
| 26 |
|
| 27 |
# Redefine some fixtures so we can use them with function scope |
| 28 |
@pytest.fixture(scope="function") |
| 29 |
def topology(request): |
| 30 |
"""Create Replication Deployment with two suppliers""" |
| 31 |
|
| 32 |
topology = create_topology({ReplicaRole.SUPPLIER: 2}) |
| 33 |
|
| 34 |
def fin(): |
| 35 |
if DEBUGGING: |
| 36 |
[inst.stop() for inst in topology] |
| 37 |
else: |
| 38 |
[inst.delete() for inst in topology] |
| 39 |
request.addfinalizer(fin) |
| 40 |
|
| 41 |
return topology |
| 42 |
|
| 43 |
|
| 44 |
def trigger_update(topology, user_rdn, num): |
| 45 |
"""Trigger an update on the supplier to start a replication |
| 46 |
session and a schema push. |
| 47 |
""" |
| 48 |
users_m1 = UserAccounts(topology.ms["supplier1"], DEFAULT_SUFFIX) |
| 49 |
user = users_m1.get(user_rdn) |
| 50 |
user.replace('telephonenumber', str(num)) |
| 51 |
|
| 52 |
# Wait for the update to be replicated |
| 53 |
users_m2 = UserAccounts(topology.ms["supplier2"], DEFAULT_SUFFIX) |
| 54 |
for _ in range(30): |
| 55 |
try: |
| 56 |
user = users_m2.get(user_rdn) |
| 57 |
val = user.get_attr_val_int('telephonenumber') |
| 58 |
if val == num: |
| 59 |
log.debug(f"Replication successful for {user_rdn} with value {num}.") |
| 60 |
return |
| 61 |
time.sleep(1) |
| 62 |
log.debug(f"Waiting for replication: received {val}, expected {num}.") |
| 63 |
except ldap.NO_SUCH_OBJECT: |
| 64 |
log.debug("User not found, retrying...") |
| 65 |
time.sleep(1) |
| 66 |
|
| 67 |
|
| 68 |
def validate_schema_changes(topology, attr_name, rule_to_verify, m1_not_present=False, m2_not_present=False): |
| 69 |
"""Validates a schema change after replication and restart.""" |
| 70 |
|
| 71 |
m1 = topology.ms["supplier1"] |
| 72 |
m2 = topology.ms["supplier2"] |
| 73 |
schema_m1 = Schema(m1) |
| 74 |
schema_m2 = Schema(m2) |
| 75 |
|
| 76 |
# Verify schema replication |
| 77 |
assert m1.schema.get_schema_csn() == m2.schema.get_schema_csn(), "Schema CSN mismatch after replication." |
| 78 |
assert f'{rule_to_verify}Match' not in schema_m1.query_attributetype(attr_name)[0] if m1_not_present else \ |
| 79 |
f'{rule_to_verify}Match' in schema_m1.query_attributetype(attr_name)[0], "Custom schema incorrectly replicated to m1." |
| 80 |
assert f'{rule_to_verify}Match' not in schema_m2.query_attributetype(attr_name)[0] if m2_not_present else \ |
| 81 |
f'{rule_to_verify}Match' in schema_m2.query_attributetype(attr_name)[0], "Custom schema incorrectly replicated to m2." |
| 82 |
|
| 83 |
# Test that the custom schema is present in schema 99user.ldif in server's schema dir |
| 84 |
schema_filename = (m2.schemadir + "/99user.ldif") |
| 85 |
with open(schema_filename, 'r') as f: |
| 86 |
assert f'{rule_to_verify}SubstringsMatch' not in f.read() if m2_not_present else \ |
| 87 |
f'{rule_to_verify}SubstringsMatch' in f.read(), "Custom schema incorrectly replicated to 99user.ldif." |
| 88 |
|
| 89 |
|
| 90 |
def test_subset_syntax_accepted(topology): |
| 91 |
"""Test that a superset syntax is rejected when modifying the schema. |
| 92 |
|
| 93 |
:id: 067de984-dea7-4701-805d-d4eca1a35d14 |
| 94 |
:setup: Two supplier replication topology |
| 95 |
:steps: |
| 96 |
1. Create a test user on the first supplier. |
| 97 |
2. Create testSn attribute type with IA5String syntax |
| 98 |
3. Verify it's replicated to the second supplier |
| 99 |
4. Change the syntax to Directory String |
| 100 |
5. Verify it's replicated to the second supplier |
| 101 |
:expectedresults: |
| 102 |
1. Success |
| 103 |
2. Success |
| 104 |
3. Success |
| 105 |
4. Success |
| 106 |
5. Success |
| 107 |
""" |
| 108 |
|
| 109 |
replication_manager = ReplicationManager(DEFAULT_SUFFIX) |
| 110 |
m1 = topology.ms["supplier1"] |
| 111 |
m2 = topology.ms["supplier2"] |
| 112 |
|
| 113 |
# Create a test user on m1 |
| 114 |
users_m1 = UserAccounts(m1, DEFAULT_SUFFIX) |
| 115 |
test_user = users_m1.create_test_user() |
| 116 |
|
| 117 |
# Retrieve and modify the schema on m1 |
| 118 |
schema_m1 = Schema(m1) |
| 119 |
surname_attr_type = schema_m1.query_attributetype('surname')[0] |
| 120 |
assert 'caseIgnoreMatch' in schema_m1.query_attributetype('surname')[0], "Standard schema not found in m1." |
| 121 |
|
| 122 |
surname_attr_type = surname_attr_type.replace('sn', 'testSn') |
| 123 |
surname_attr_type = surname_attr_type.replace('surName', 'testSurname') |
| 124 |
surname_attr_type = surname_attr_type.replace('2.5.4.4', '1.3.6.1.4.1.131313.9999.1') |
| 125 |
surname_attr_type = surname_attr_type.replace('RFC 4519', 'user defined') |
| 126 |
surname_attr_type = surname_attr_type.replace('caseIgnoreMatch', 'caseIgnoreIA5Match') |
| 127 |
surname_attr_type = surname_attr_type.replace('caseIgnoreSubstringsMatch', 'caseIgnoreIA5SubstringsMatch') |
| 128 |
surname_attr_type = surname_attr_type.replace('1.3.6.1.4.1.1466.115.121.1.15', '1.3.6.1.4.1.1466.115.121.1.26') |
| 129 |
schema_m1.add('attributetypes', surname_attr_type) |
| 130 |
|
| 131 |
# Trigger replication and wait for completion |
| 132 |
trigger_update(topology, test_user.rdn, 1) |
| 133 |
replication_manager.wait_for_replication(m1, m2) |
| 134 |
|
| 135 |
validate_schema_changes(topology, 'testSurname', 'caseIgnoreIA5') |
| 136 |
|
| 137 |
# Restart servers to ensure persistence of changes |
| 138 |
m1.restart() |
| 139 |
m2.restart() |
| 140 |
|
| 141 |
validate_schema_changes(topology, 'testSurname', 'caseIgnoreIA5') |
| 142 |
|
| 143 |
# Change to a different matching rule and Directory String syntax on supplier1 |
| 144 |
m2.stop() |
| 145 |
surname_attr_type = schema_m1.query_attributetype('testSurname')[0] |
| 146 |
assert 'caseIgnoreIA5Match' in schema_m1.query_attributetype('testSurname')[0], "Standard schema not found in m1." |
| 147 |
schema_m1.remove_attributetype('testSurname') |
| 148 |
|
| 149 |
surname_attr_type = surname_attr_type.replace('caseIgnoreIA5Match', 'caseIgnoreMatch') |
| 150 |
surname_attr_type = surname_attr_type.replace('caseIgnoreIA5SubstringsMatch', 'caseIgnoreSubstringsMatch') |
| 151 |
surname_attr_type = surname_attr_type.replace('1.3.6.1.4.1.1466.115.121.1.26', '1.3.6.1.4.1.1466.115.121.1.15') |
| 152 |
schema_m1.add('attributetypes', surname_attr_type) |
| 153 |
m2.start() |
| 154 |
|
| 155 |
# Trigger replication and wait for completion |
| 156 |
trigger_update(topology, test_user.rdn, 2) |
| 157 |
replication_manager.wait_for_replication(m1, m2) |
| 158 |
|
| 159 |
validate_schema_changes(topology, 'testSurname', 'caseIgnore') |
| 160 |
|
| 161 |
# Restart servers to ensure persistence of changes |
| 162 |
m1.restart() |
| 163 |
m2.restart() |
| 164 |
|
| 165 |
validate_schema_changes(topology, 'testSurname', 'caseIgnore') |
| 166 |
|
| 167 |
|
| 168 |
def test_standard_schema_superset_change_correctly_rejected(topology): |
| 169 |
"""Test if custom schema modifications are properly rejected when |
| 170 |
changing to a superset syntax. |
| 171 |
|
| 172 |
:id: ea8a32b0-9901-4ec9-8ed4-dda551a3eb43 |
| 173 |
:setup: Two supplier replication topology |
| 174 |
:steps: |
| 175 |
1. Create a test user on the first supplier. |
| 176 |
2. Retrieve and modify the schema on the first supplier, changing 'surname' attribute's syntax to a Directory String subset |
| 177 |
3. Trigger replication and wait for completion. |
| 178 |
4. Verify schema replication was rejected |
| 179 |
5. Restart both suppliers. |
| 180 |
6. Verify schema replication was rejected |
| 181 |
:expectedresults: |
| 182 |
1. Success |
| 183 |
2. Success |
| 184 |
3. Success |
| 185 |
4. Success |
| 186 |
5. Success |
| 187 |
6. Success |
| 188 |
""" |
| 189 |
replication_manager = ReplicationManager(DEFAULT_SUFFIX) |
| 190 |
m1 = topology.ms["supplier1"] |
| 191 |
m2 = topology.ms["supplier2"] |
| 192 |
|
| 193 |
# Create a test user on m1 |
| 194 |
users_m1 = UserAccounts(m1, DEFAULT_SUFFIX) |
| 195 |
test_user = users_m1.create_test_user() |
| 196 |
|
| 197 |
# Retrieve and modify the schema on m1 |
| 198 |
schema_m1 = Schema(m1) |
| 199 |
schema_m2 = Schema(m2) |
| 200 |
surname_attr_type = schema_m1.query_attributetype('surname')[0] |
| 201 |
assert 'caseIgnoreMatch' in schema_m1.query_attributetype('surname')[0], "Standard schema not found in m1." |
| 202 |
|
| 203 |
surname_attr_type = surname_attr_type.replace('caseIgnoreMatch', 'caseIgnoreIA5Match') |
| 204 |
surname_attr_type = surname_attr_type.replace('caseIgnoreSubstringsMatch', 'caseIgnoreIA5SubstringsMatch') |
| 205 |
surname_attr_type = surname_attr_type.replace('1.3.6.1.4.1.1466.115.121.1.15', '1.3.6.1.4.1.1466.115.121.1.26') |
| 206 |
schema_m1.add('attributetypes', surname_attr_type) |
| 207 |
|
| 208 |
# Trigger replication and wait for completion |
| 209 |
trigger_update(topology, test_user.rdn, 1) |
| 210 |
replication_manager.wait_for_replication(m1, m2) |
| 211 |
|
| 212 |
validate_schema_changes(topology, 'surname', 'caseIgnoreIA5', m1_not_present=True, m2_not_present=True) |
| 213 |
|
| 214 |
# Restart servers to ensure persistence of changes |
| 215 |
m1.restart() |
| 216 |
m2.restart() |
| 217 |
|
| 218 |
validate_schema_changes(topology, 'surname', 'caseIgnoreIA5', m1_not_present=True, m2_not_present=True) |
| 219 |
|
| 220 |
|
| 221 |
def test_custom_standard_schema_stays_local(topology): |
| 222 |
"""Test if custom standard schema defined in 98test.ldif on one supplier |
| 223 |
is not replicated to the other supplier. |
| 224 |
|
| 225 |
:id: b166334d-ed71-4b96-84e1-c376aedc355c |
| 226 |
:setup: Two supplier replication topology |
| 227 |
:steps: |
| 228 |
1. Create a test user on the first supplier. |
| 229 |
2. Create a custom standard schema in 98test.ldif on the first supplier. |
| 230 |
3. Trigger replication and wait for completion. |
| 231 |
4. Verify it was not replicated to the second supplier. |
| 232 |
5. Restart both suppliers. |
| 233 |
6. Verify it was not replicated to the second supplier. |
| 234 |
:expectedresults: |
| 235 |
1. Success |
| 236 |
2. Success |
| 237 |
3. Success |
| 238 |
4. Success |
| 239 |
5. Success |
| 240 |
6. Success |
| 241 |
""" |
| 242 |
replication_manager = ReplicationManager(DEFAULT_SUFFIX) |
| 243 |
m1 = topology.ms["supplier1"] |
| 244 |
m2 = topology.ms["supplier2"] |
| 245 |
|
| 246 |
# Create a test user on m1 |
| 247 |
users_m1 = UserAccounts(m1, DEFAULT_SUFFIX) |
| 248 |
test_user = users_m1.create_test_user() |
| 249 |
file_path = os.path.join(m1.schemadir, "98test.ldif") |
| 250 |
m1.stop() |
| 251 |
with open(file_path, 'a') as f: |
| 252 |
f.write("""dn: cn=schema |
| 253 |
attributeTypes: ( 2.5.4.4 NAME ( 'sn' 'surName' ) SUP name \ |
| 254 |
EQUALITY caseIgnoreIA5Match SUBSTR caseIgnoreIA5SubstringsMatch \ |
| 255 |
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 X-ORIGIN 'user defined' )""") |
| 256 |
|
| 257 |
# Adjust the permissions to 600: |
| 258 |
os.chmod(file_path, 0o600) |
| 259 |
dirsrv_uid = pwd.getpwnam('dirsrv').pw_uid |
| 260 |
dirsrv_gid = grp.getgrnam('dirsrv').gr_gid |
| 261 |
os.chown(file_path, dirsrv_uid, dirsrv_gid) |
| 262 |
|
| 263 |
m1.start() |
| 264 |
|
| 265 |
# Retrieve and modify the schema on m1 |
| 266 |
schema_m1 = Schema(m1) |
| 267 |
schema_m2 = Schema(m2) |
| 268 |
|
| 269 |
# Trigger replication and wait for completion |
| 270 |
trigger_update(topology, test_user.rdn, 1) |
| 271 |
replication_manager.wait_for_replication(m1, m2) |
| 272 |
|
| 273 |
validate_schema_changes(topology, 'surname', 'caseIgnoreIA5', m2_not_present=True) |
| 274 |
|
| 275 |
# Restart servers to ensure persistence of changes |
| 276 |
m1.restart() |
| 277 |
m2.restart() |
| 278 |
|
| 279 |
validate_schema_changes(topology, 'surname', 'caseIgnoreIA5', m2_not_present=True) |
| 280 |
|
| 281 |
|
| 282 |
def test_custom_schema_persistent_after_restart_locally(topology_st): |
| 283 |
"""Test if custom schema modifications persist after server restarts on standalone instance. |
| 284 |
|
| 285 |
:id: e662d3e1-9d0f-45fe-b9f9-912f847602bc |
| 286 |
:setup: Two supplier replication topology |
| 287 |
:steps: |
| 288 |
1. Create a test user on the first supplier. |
| 289 |
2. Retrieve and modify the schema on the first supplier, changing 'surname' attribute's matching rule. |
| 290 |
3. Restart both suppliers. |
| 291 |
4. Verify schema is persisted after restart. |
| 292 |
:expectedresults: |
| 293 |
1. Success |
| 294 |
2. Success |
| 295 |
3. Success |
| 296 |
4. Success |
| 297 |
""" |
| 298 |
st = topology_st.standalone |
| 299 |
|
| 300 |
# Create a test user on m1 |
| 301 |
users_st = UserAccounts(st, DEFAULT_SUFFIX) |
| 302 |
test_user = users_st.create_test_user() |
| 303 |
|
| 304 |
# Retrieve and modify the schema on st |
| 305 |
schema_st = Schema(st) |
| 306 |
surname_attr_type = schema_st.query_attributetype('surname')[0] |
| 307 |
assert 'caseIgnoreMatch' in schema_st.query_attributetype('surname')[0], "Standard schema not found in st." |
| 308 |
|
| 309 |
surname_attr_type = surname_attr_type.replace('caseIgnoreMatch', 'caseIgnoreIA5Match') |
| 310 |
surname_attr_type = surname_attr_type.replace('caseIgnoreSubstringsMatch', 'caseIgnoreIA5SubstringsMatch') |
| 311 |
surname_attr_type = surname_attr_type.replace('1.3.6.1.4.1.1466.115.121.1.15', '1.3.6.1.4.1.1466.115.121.1.26') |
| 312 |
schema_st.add('attributetypes', surname_attr_type) |
| 313 |
|
| 314 |
test_user.replace('telephonenumber', "1") |
| 315 |
|
| 316 |
# Verify schema |
| 317 |
assert 'caseIgnoreIA5Match' in schema_st.query_attributetype('surname')[0], "Custom schema not found in st." |
| 318 |
|
| 319 |
# Restart servers to ensure persistence of changes |
| 320 |
st.restart() |
| 321 |
|
| 322 |
# Verify schema persistence post-restart |
| 323 |
assert 'caseIgnoreIA5Match' in schema_st.query_attributetype('surname')[0], "Custom schema not persisted in st." |
| 324 |
|
| 325 |
# Test that the custom schema is present in schema 99user.ldif in server's schema dir |
| 326 |
schema_filename = (st.schemadir + "/99user.ldif") |
| 327 |
with open(schema_filename, 'r') as f: |
| 328 |
assert 'caseIgnoreIA5SubstringsMatch' in f.read(), "Custom schema not found in 99user.ldif." |