| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2019 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 |
"""Test DNA plugin functionality""" |
| 10 |
|
| 11 |
import logging |
| 12 |
import pytest |
| 13 |
import os |
| 14 |
import time |
| 15 |
import ldap |
| 16 |
import ldapurl |
| 17 |
from lib389._constants import DEFAULT_SUFFIX |
| 18 |
from lib389.plugins import DNAPlugin, DNAPluginSharedConfigs, DNAPluginConfigs |
| 19 |
from lib389.dseldif import DSEldif |
| 20 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 21 |
from lib389.idm.user import UserAccounts, UserAccount |
| 22 |
from test389.topologies import topology_st |
| 23 |
|
| 24 |
pytestmark = pytest.mark.tier1 |
| 25 |
|
| 26 |
log = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
def test_dnatype_only_valid(topology_st): |
| 30 |
"""Test that DNA plugin only accepts valid attributes for "dnaType" |
| 31 |
|
| 32 |
:id: 0878ecff-5fdc-47d7-8c8f-edf4556f9746 |
| 33 |
:setup: Standalone Instance |
| 34 |
:steps: |
| 35 |
1. Create a use entry |
| 36 |
2. Create DNA shared config entry container |
| 37 |
3. Create DNA shared config entry |
| 38 |
4. Add DNA plugin config entry |
| 39 |
5. Enable DNA plugin |
| 40 |
6. Restart the instance |
| 41 |
7. Replace dnaType with invalid value |
| 42 |
:expectedresults: |
| 43 |
1. Successful |
| 44 |
2. Successful |
| 45 |
3. Successful |
| 46 |
4. Successful |
| 47 |
5. Successful |
| 48 |
6. Successful |
| 49 |
7. Unwilling to perform exception should be raised |
| 50 |
""" |
| 51 |
|
| 52 |
inst = topology_st.standalone |
| 53 |
plugin = DNAPlugin(inst) |
| 54 |
|
| 55 |
log.info("Creating an entry...") |
| 56 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 57 |
users.create_test_user(uid=1) |
| 58 |
|
| 59 |
log.info("Creating \"ou=ranges\"...") |
| 60 |
ous = OrganizationalUnits(inst, DEFAULT_SUFFIX) |
| 61 |
ou_ranges = ous.create(properties={'ou': 'ranges'}) |
| 62 |
ou_people = ous.get("People") |
| 63 |
|
| 64 |
log.info("Creating DNA shared config entry...") |
| 65 |
shared_configs = DNAPluginSharedConfigs(inst, ou_ranges.dn) |
| 66 |
shared_configs.create(properties={'dnaHostname': str(inst.host), |
| 67 |
'dnaPortNum': str(inst.port), |
| 68 |
'dnaRemainingValues': '9501'}) |
| 69 |
|
| 70 |
log.info("Add dna plugin config entry...") |
| 71 |
configs = DNAPluginConfigs(inst, plugin.dn) |
| 72 |
config = configs.create(properties={'cn': 'dna config', |
| 73 |
'dnaType': 'description', |
| 74 |
'dnaMaxValue': '10000', |
| 75 |
'dnaMagicRegen': '0', |
| 76 |
'dnaFilter': '(objectclass=top)', |
| 77 |
'dnaScope': ou_people.dn, |
| 78 |
'dnaNextValue': '500', |
| 79 |
'dnaSharedCfgDN': ou_ranges.dn}) |
| 80 |
|
| 81 |
log.info("Enable the DNA plugin...") |
| 82 |
plugin.enable() |
| 83 |
|
| 84 |
log.info("Restarting the server...") |
| 85 |
inst.restart() |
| 86 |
|
| 87 |
log.info("Apply an invalid attribute to the DNA config(dnaType: foo)...") |
| 88 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 89 |
config.replace('dnaType', 'foo') |
| 90 |
|
| 91 |
|
| 92 |
def test_dna_exclude_scope_functionality(topology_st): |
| 93 |
"""Test DNA plugin dnaExcludeScope functionality |
| 94 |
|
| 95 |
:id: a0603f09-7484-42ef-8468-41f16a51c321 |
| 96 |
:setup: Standalone instance with DNA plugin configured |
| 97 |
:steps: |
| 98 |
1. Test basic DNA functionality (magic regen -1 becomes assigned number) |
| 99 |
2. Test DNA preserves explicitly set values |
| 100 |
3. Test DNA in staging OU without exclude scope |
| 101 |
4. Add staging OU to dnaExcludeScope |
| 102 |
5. Verify DNA works in main suffix but not in excluded staging OU |
| 103 |
6. Test invalid DN handling in dnaExcludeScope |
| 104 |
:expectedresults: |
| 105 |
1. DNA replaces magic regen value -1 with assigned numbers |
| 106 |
2. DNA preserves explicitly set non-magic values |
| 107 |
3. DNA works in all areas when no exclude scope is set |
| 108 |
4. dnaExcludeScope is configured successfully |
| 109 |
5. DNA skips excluded staging OU but works elsewhere |
| 110 |
6. Invalid DNs are rejected with INVALID_SYNTAX error |
| 111 |
""" |
| 112 |
inst = topology_st.standalone |
| 113 |
plugin = DNAPlugin(inst) |
| 114 |
|
| 115 |
plugin.enable() |
| 116 |
|
| 117 |
ous = OrganizationalUnits(inst, DEFAULT_SUFFIX) |
| 118 |
staging_ou = ous.create(properties={'ou': 'staging'}) |
| 119 |
|
| 120 |
configs = DNAPluginConfigs(inst, plugin.dn) |
| 121 |
dna_config = configs.create(properties={ |
| 122 |
'cn': 'exclude scope config', |
| 123 |
'dnaType': 'employeeNumber', |
| 124 |
'dnaNextValue': '1000', |
| 125 |
'dnaMaxValue': '2000', |
| 126 |
'dnaMagicRegen': '-1', |
| 127 |
'dnaFilter': '(&(objectClass=person)(objectClass=organizationalPerson)(objectClass=inetOrgPerson))', |
| 128 |
'dnaScope': DEFAULT_SUFFIX |
| 129 |
}) |
| 130 |
|
| 131 |
inst.restart() |
| 132 |
|
| 133 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 134 |
|
| 135 |
log.info("Test basic DNA functionality (magic regen -1 becomes assigned number)") |
| 136 |
user1 = users.create_test_user() |
| 137 |
user1.replace('employeeNumber', '-1') |
| 138 |
assigned_num = user1.get_attr_val_utf8('employeeNumber') |
| 139 |
assert assigned_num != '-1', f"DNA should replace magic value -1 with real number, got {assigned_num}" |
| 140 |
assert int(assigned_num) >= 1000, f"Assigned number {assigned_num} should be in configured range" |
| 141 |
user1.delete() |
| 142 |
|
| 143 |
log.info("Test DNA preserves explicitly set values") |
| 144 |
user2 = users.create_test_user() |
| 145 |
user2.replace('employeeNumber', '500') |
| 146 |
preserved_num = user2.get_attr_val_utf8('employeeNumber') |
| 147 |
assert preserved_num == '500', f"DNA should preserve explicit value 500, got {preserved_num}" |
| 148 |
user2.delete() |
| 149 |
|
| 150 |
log.info("Test DNA in staging OU without exclude scope") |
| 151 |
user3 = UserAccount(inst, dn=f'cn=staging_user3,{staging_ou.dn}') |
| 152 |
user3.create(properties={ |
| 153 |
'cn': 'staging_user3', |
| 154 |
'sn': 'user3', |
| 155 |
'uid': 'staging_user3', |
| 156 |
'uidNumber': '3000', |
| 157 |
'gidNumber': '3000', |
| 158 |
'homeDirectory': '/home/staging_user3', |
| 159 |
'employeeNumber': '-1' |
| 160 |
}) |
| 161 |
assigned_num3 = user3.get_attr_val_utf8('employeeNumber') |
| 162 |
assert assigned_num3 != '-1', f"DNA should work in staging OU when not excluded, got {assigned_num3}" |
| 163 |
user3.delete() |
| 164 |
|
| 165 |
log.info("Add staging OU to dnaExcludeScope") |
| 166 |
dna_config.add('dnaExcludeScope', staging_ou.dn) |
| 167 |
|
| 168 |
log.info("Verify DNA works in main suffix but not in excluded staging OU") |
| 169 |
user4 = users.create_test_user() |
| 170 |
user4.replace('employeeNumber', '-1') |
| 171 |
assigned_num4 = user4.get_attr_val_utf8('employeeNumber') |
| 172 |
assert assigned_num4 != '-1', f"DNA should still work in main suffix, got {assigned_num4}" |
| 173 |
|
| 174 |
user5 = UserAccount(inst, dn=f'cn=staging_user5,{staging_ou.dn}') |
| 175 |
user5.create(properties={ |
| 176 |
'cn': 'staging_user5', |
| 177 |
'sn': 'user5', |
| 178 |
'uid': 'staging_user5', |
| 179 |
'uidNumber': '3001', |
| 180 |
'gidNumber': '3001', |
| 181 |
'homeDirectory': '/home/staging_user5', |
| 182 |
'employeeNumber': '-1' |
| 183 |
}) |
| 184 |
excluded_num5 = user5.get_attr_val_utf8('employeeNumber') |
| 185 |
assert excluded_num5 == '-1', f"DNA should skip excluded staging OU, magic value should remain -1, got {excluded_num5}" |
| 186 |
|
| 187 |
user4.delete() |
| 188 |
user5.delete() |
| 189 |
|
| 190 |
log.info("Test invalid DN handling in dnaExcludeScope") |
| 191 |
invalid_dn = f"invalidDN,{DEFAULT_SUFFIX}" |
| 192 |
with pytest.raises(ldap.INVALID_SYNTAX): |
| 193 |
dna_config.add('dnaExcludeScope', invalid_dn) |
| 194 |
|
| 195 |
|
| 196 |
def test_dna_no_shared_config_when_isolated(topology_st): |
| 197 |
"""Test that DNA plugin does not create shared config entries when |
| 198 |
the server is isolated (port=0 and security=off). |
| 199 |
|
| 200 |
:id: e0ad2f90-bf94-46c0-8caf-46f1448cb181 |
| 201 |
:setup: Standalone Instance |
| 202 |
:steps: |
| 203 |
1. Configure DNA plugin with shared config using unique names |
| 204 |
2. Enable plugin, restart, and verify shared config entry |
| 205 |
3. Stop instance, set nsslapd-port=0 and nsslapd-security=off offline |
| 206 |
4. Start instance in isolated mode, connect via LDAPI |
| 207 |
5. Wait for DNA event and verify no dnaPortNum=0 entry was created |
| 208 |
and original shared config entry with correct port persists |
| 209 |
6. Stop instance, restore port and security to original values offline |
| 210 |
7. Start normally and verify shared config entry is recreated |
| 211 |
:expectedresults: |
| 212 |
1. Successful |
| 213 |
2. Shared config entry exists with the instance port |
| 214 |
3. dse.ldif modified successfully |
| 215 |
4. Instance starts in isolated mode |
| 216 |
5. No dnaPortNum=0 entry exists, original entry persists |
| 217 |
6. dse.ldif restored to original values |
| 218 |
7. Shared config entry with correct port exists |
| 219 |
""" |
| 220 |
|
| 221 |
inst = topology_st.standalone |
| 222 |
plugin = DNAPlugin(inst) |
| 223 |
original_port = str(inst.port) |
| 224 |
original_security = inst.config.get_attr_val_utf8('nsslapd-security') or 'off' |
| 225 |
|
| 226 |
log.info("Creating \"ou=isolated-ranges\" for shared config...") |
| 227 |
ous = OrganizationalUnits(inst, DEFAULT_SUFFIX) |
| 228 |
ou_ranges = ous.create(properties={'ou': 'isolated-ranges'}) |
| 229 |
ou_people = ous.get("People") |
| 230 |
|
| 231 |
log.info("Add DNA plugin config entry with shared config...") |
| 232 |
configs = DNAPluginConfigs(inst, plugin.dn) |
| 233 |
dna_config = configs.create(properties={ |
| 234 |
'cn': 'dna isolated config', |
| 235 |
'dnaType': 'description', |
| 236 |
'dnaMaxValue': '10000', |
| 237 |
'dnaMagicRegen': '0', |
| 238 |
'dnaFilter': '(objectclass=top)', |
| 239 |
'dnaScope': ou_people.dn, |
| 240 |
'dnaNextValue': '500', |
| 241 |
'dnaSharedCfgDN': ou_ranges.dn}) |
| 242 |
|
| 243 |
log.info("Enable the DNA plugin and restart...") |
| 244 |
plugin.enable() |
| 245 |
inst.restart() |
| 246 |
|
| 247 |
log.info("Waiting for DNA plugin shared config event to fire...") |
| 248 |
time.sleep(35) |
| 249 |
|
| 250 |
log.info("Verify shared config entry exists with correct port...") |
| 251 |
shared_configs = DNAPluginSharedConfigs(inst, ou_ranges.dn) |
| 252 |
entries = shared_configs.list() |
| 253 |
valid_entry = None |
| 254 |
for entry in entries: |
| 255 |
if entry.get_attr_val_utf8('dnaPortNum') == original_port: |
| 256 |
valid_entry = entry |
| 257 |
break |
| 258 |
assert valid_entry is not None, \ |
| 259 |
f"Expected shared config entry with port {original_port}" |
| 260 |
|
| 261 |
try: |
| 262 |
log.info("Stopping instance to simulate isolated mode...") |
| 263 |
inst.stop() |
| 264 |
|
| 265 |
log.info("Editing dse.ldif: nsslapd-port=0, nsslapd-security=off...") |
| 266 |
dse = DSEldif(inst) |
| 267 |
dse.replace('cn=config', 'nsslapd-port', '0') |
| 268 |
dse.replace('cn=config', 'nsslapd-security', 'off') |
| 269 |
|
| 270 |
log.info("Starting instance in isolated mode (LDAPI only)...") |
| 271 |
inst.start(post_open=False) |
| 272 |
|
| 273 |
ldapi_uri = f"ldapi://{ldapurl.ldapUrlEscape(inst.ds_paths.ldapi)}" |
| 274 |
log.info(f"Connecting via LDAPI: {ldapi_uri}") |
| 275 |
inst.open(uri=ldapi_uri) |
| 276 |
|
| 277 |
log.info("Waiting for DNA plugin shared config event...") |
| 278 |
time.sleep(35) |
| 279 |
|
| 280 |
log.info("Checking that no dnaPortNum=0 shared config entry was created...") |
| 281 |
shared_configs = DNAPluginSharedConfigs(inst, ou_ranges.dn) |
| 282 |
entries = shared_configs.list() |
| 283 |
for entry in entries: |
| 284 |
entry_port = entry.get_attr_val_utf8('dnaPortNum') |
| 285 |
assert entry_port != '0', \ |
| 286 |
f"DNA plugin should not create shared config with port 0 " \ |
| 287 |
f"when server is isolated, but found: {entry.dn}" |
| 288 |
|
| 289 |
log.info("Verify original shared config entry persists during isolated mode...") |
| 290 |
original_persists = any( |
| 291 |
e.get_attr_val_utf8('dnaPortNum') == original_port for e in entries |
| 292 |
) |
| 293 |
assert original_persists, \ |
| 294 |
f"Original shared config entry with port {original_port} " \ |
| 295 |
f"should persist during isolated mode" |
| 296 |
finally: |
| 297 |
log.info("Restoring instance to normal state...") |
| 298 |
if inst.status(): |
| 299 |
inst.stop() |
| 300 |
|
| 301 |
dse = DSEldif(inst) |
| 302 |
dse.replace('cn=config', 'nsslapd-port', original_port) |
| 303 |
dse.replace('cn=config', 'nsslapd-security', original_security) |
| 304 |
inst.start() |
| 305 |
|
| 306 |
time.sleep(35) |
| 307 |
|
| 308 |
log.info("Verify shared config entry is recreated with correct port...") |
| 309 |
shared_configs = DNAPluginSharedConfigs(inst, ou_ranges.dn) |
| 310 |
entries = shared_configs.list() |
| 311 |
valid_found = False |
| 312 |
for entry in entries: |
| 313 |
if entry.get_attr_val_utf8('dnaPortNum') == original_port: |
| 314 |
valid_found = True |
| 315 |
break |
| 316 |
assert valid_found, \ |
| 317 |
f"Shared config entry with port {original_port} should exist after restore" |
| 318 |
|
| 319 |
|
| 320 |
if __name__ == '__main__': |
| 321 |
# Run isolated |
| 322 |
# -s for DEBUG mode |
| 323 |
CURRENT_FILE = os.path.realpath(__file__) |
| 324 |
pytest.main(["-s", CURRENT_FILE]) |