| 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 time |
| 11 |
import ldap |
| 12 |
import pytest |
| 13 |
import subprocess |
| 14 |
import logging |
| 15 |
|
| 16 |
from lib389._constants import DEFAULT_SUFFIX, DN_DM, PASSWORD, DN_CONFIG |
| 17 |
from test389.topologies import topology_st |
| 18 |
from lib389.idm.user import UserAccounts |
| 19 |
from lib389.idm.domain import Domain |
| 20 |
from lib389.pwpolicy import PwPolicyManager |
| 21 |
|
| 22 |
pytestmark = pytest.mark.tier1 |
| 23 |
|
| 24 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 25 |
if DEBUGGING: |
| 26 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 27 |
else: |
| 28 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 29 |
log = logging.getLogger(__name__) |
| 30 |
|
| 31 |
OU_DN = f"ou=People,{DEFAULT_SUFFIX}" |
| 32 |
USER_ACI = '(targetattr="userpassword || passwordHistory")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)' |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture(autouse=True, scope="function") |
| 36 |
def restore_global_policy(topology_st, request): |
| 37 |
"""Snapshot and restore global password policy around each test in this file.""" |
| 38 |
inst = topology_st.standalone |
| 39 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 40 |
|
| 41 |
attrs = [ |
| 42 |
'nsslapd-pwpolicy-local', |
| 43 |
'nsslapd-pwpolicy-inherit-global', |
| 44 |
'passwordHistory', |
| 45 |
'passwordInHistory', |
| 46 |
'passwordChange', |
| 47 |
] |
| 48 |
|
| 49 |
entry = inst.getEntry(DN_CONFIG, ldap.SCOPE_BASE, '(objectClass=*)', attrs) |
| 50 |
saved = {attr: entry.getValue(attr) for attr in attrs} |
| 51 |
|
| 52 |
def fin(): |
| 53 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 54 |
for attr, value in saved.items(): |
| 55 |
inst.config.replace(attr, value) |
| 56 |
|
| 57 |
request.addfinalizer(fin) |
| 58 |
|
| 59 |
|
| 60 |
@pytest.fixture(scope="function") |
| 61 |
def setup_entries(topology_st, request): |
| 62 |
"""Create test OU and user, and install an ACI for self password changes.""" |
| 63 |
|
| 64 |
inst = topology_st.standalone |
| 65 |
|
| 66 |
suffix = Domain(inst, DEFAULT_SUFFIX) |
| 67 |
suffix.add('aci', USER_ACI) |
| 68 |
|
| 69 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 70 |
try: |
| 71 |
user = users.create_test_user(uid=1) |
| 72 |
except ldap.ALREADY_EXISTS: |
| 73 |
user = users.get("test_user_1") |
| 74 |
|
| 75 |
def fin(): |
| 76 |
pwp = PwPolicyManager(inst) |
| 77 |
try: |
| 78 |
pwp.delete_local_policy(OU_DN) |
| 79 |
except Exception as e: |
| 80 |
if "No password policy" in str(e): |
| 81 |
pass |
| 82 |
else: |
| 83 |
raise e |
| 84 |
try: |
| 85 |
pwp.delete_local_policy(user.dn) |
| 86 |
except Exception as e: |
| 87 |
if "No password policy" in str(e): |
| 88 |
pass |
| 89 |
else: |
| 90 |
raise e |
| 91 |
suffix.remove('aci', USER_ACI) |
| 92 |
request.addfinalizer(fin) |
| 93 |
|
| 94 |
return user |
| 95 |
|
| 96 |
|
| 97 |
def set_user_password(inst, user, new_password, bind_as_user_password=None, expect_violation=False): |
| 98 |
if bind_as_user_password is not None: |
| 99 |
user.rebind(bind_as_user_password) |
| 100 |
try: |
| 101 |
user.reset_password(new_password) |
| 102 |
if expect_violation: |
| 103 |
pytest.fail("Password change unexpectedly succeeded") |
| 104 |
except ldap.CONSTRAINT_VIOLATION: |
| 105 |
if not expect_violation: |
| 106 |
pytest.fail("Password change unexpectedly rejected with CONSTRAINT_VIOLATION") |
| 107 |
finally: |
| 108 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 109 |
time.sleep(1) |
| 110 |
|
| 111 |
|
| 112 |
def set_global_history(inst, enabled: bool, count: int, inherit_global: str = 'on'): |
| 113 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 114 |
inst.config.replace('nsslapd-pwpolicy-local', 'on') |
| 115 |
inst.config.replace('nsslapd-pwpolicy-inherit-global', inherit_global) |
| 116 |
inst.config.replace('passwordHistory', 'on' if enabled else 'off') |
| 117 |
inst.config.replace('passwordInHistory', str(count)) |
| 118 |
inst.config.replace('passwordChange', 'on') |
| 119 |
time.sleep(1) |
| 120 |
|
| 121 |
|
| 122 |
def ensure_local_subtree_policy(inst, count: int, track_update_time: str = 'on'): |
| 123 |
pwp = PwPolicyManager(inst) |
| 124 |
pwp.create_subtree_policy(OU_DN, { |
| 125 |
'passwordChange': 'on', |
| 126 |
'passwordHistory': 'on', |
| 127 |
'passwordInHistory': str(count), |
| 128 |
'passwordTrackUpdateTime': track_update_time, |
| 129 |
}) |
| 130 |
time.sleep(1) |
| 131 |
|
| 132 |
|
| 133 |
def set_local_history_via_cli(inst, count: int): |
| 134 |
sbin_dir = inst.get_sbin_dir() |
| 135 |
inst_name = inst.serverid |
| 136 |
cmd = [f"{sbin_dir}/dsconf", inst_name, "localpwp", "set", f"--pwdhistorycount={count}", OU_DN] |
| 137 |
rc = subprocess.call(cmd) |
| 138 |
assert rc == 0, f"dsconf command failed rc={rc}: {' '.join(cmd)}" |
| 139 |
time.sleep(1) |
| 140 |
|
| 141 |
|
| 142 |
def test_global_history_only_enforced(topology_st, setup_entries): |
| 143 |
"""Global-only history enforcement with count 2 |
| 144 |
|
| 145 |
:id: 3d8cf35b-4a33-4587-9814-ebe18b7a1f92 |
| 146 |
:setup: Standalone instance, test OU and user, ACI for self password changes |
| 147 |
:steps: |
| 148 |
1. Remove local policies |
| 149 |
2. Set global policy: passwordHistory=on, passwordInHistory=2 |
| 150 |
3. Set password to Alpha1, then change to Alpha2 and Alpha3 as the user |
| 151 |
4. Attempt to change to Alpha1 and Alpha2 |
| 152 |
5. Attempt to change to Alpha4 |
| 153 |
:expectedresults: |
| 154 |
1. Success |
| 155 |
2. Success |
| 156 |
3. Success |
| 157 |
4. Changes to Welcome1 and Welcome2 are rejected with CONSTRAINT_VIOLATION |
| 158 |
5. Change to Welcome4 is accepted |
| 159 |
""" |
| 160 |
inst = topology_st.standalone |
| 161 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 162 |
|
| 163 |
set_global_history(inst, enabled=True, count=2) |
| 164 |
|
| 165 |
user = setup_entries |
| 166 |
user.reset_password('Alpha1') |
| 167 |
set_user_password(inst, user, 'Alpha2', bind_as_user_password='Alpha1') |
| 168 |
set_user_password(inst, user, 'Alpha3', bind_as_user_password='Alpha2') |
| 169 |
|
| 170 |
# Within last 2 |
| 171 |
set_user_password(inst, user, 'Alpha2', bind_as_user_password='Alpha3', expect_violation=True) |
| 172 |
set_user_password(inst, user, 'Alpha1', bind_as_user_password='Alpha3', expect_violation=True) |
| 173 |
|
| 174 |
# New password should be allowed |
| 175 |
set_user_password(inst, user, 'Alpha4', bind_as_user_password='Alpha3', expect_violation=False) |
| 176 |
|
| 177 |
|
| 178 |
def test_local_overrides_global_history(topology_st, setup_entries): |
| 179 |
"""Local subtree policy (history=3) overrides global (history=1) |
| 180 |
|
| 181 |
:id: 97c22f56-5ea6-40c1-8d8c-1cece3bf46fd |
| 182 |
:setup: Standalone instance, test OU and user |
| 183 |
:steps: |
| 184 |
1. Set global policy passwordInHistory=1 |
| 185 |
2. Create local subtree policy on the OU with passwordInHistory=3 |
| 186 |
3. Set password to Bravo1, then change to Bravo2 and Bravo3 as the user |
| 187 |
4. Attempt to change to Bravo1 |
| 188 |
5. Attempt to change to Bravo5 |
| 189 |
:expectedresults: |
| 190 |
1. Success |
| 191 |
2. Success |
| 192 |
3. Success |
| 193 |
4. Change to Welcome1 is rejected (local policy wins) |
| 194 |
5. Change to Welcome5 is accepted |
| 195 |
""" |
| 196 |
inst = topology_st.standalone |
| 197 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 198 |
|
| 199 |
set_global_history(inst, enabled=True, count=1, inherit_global='on') |
| 200 |
|
| 201 |
ensure_local_subtree_policy(inst, count=3) |
| 202 |
|
| 203 |
user = setup_entries |
| 204 |
user.reset_password('Bravo1') |
| 205 |
set_user_password(inst, user, 'Bravo2', bind_as_user_password='Bravo1') |
| 206 |
set_user_password(inst, user, 'Bravo3', bind_as_user_password='Bravo2') |
| 207 |
|
| 208 |
# Third prior should be rejected under local policy count=3 |
| 209 |
set_user_password(inst, user, 'Bravo1', bind_as_user_password='Bravo3', expect_violation=True) |
| 210 |
|
| 211 |
# New password allowed |
| 212 |
set_user_password(inst, user, 'Bravo5', bind_as_user_password='Bravo3', expect_violation=False) |
| 213 |
|
| 214 |
|
| 215 |
def test_change_local_history_via_cli_affects_enforcement(topology_st, setup_entries): |
| 216 |
"""Changing local policy via CLI is enforced immediately |
| 217 |
|
| 218 |
:id: 5a6d0d14-4009-4bad-86e1-cde5000c43dc |
| 219 |
:setup: Standalone instance, test OU and user, dsconf available |
| 220 |
:steps: |
| 221 |
1. Ensure local subtree policy passwordInHistory=3 |
| 222 |
2. Set password to Charlie1, then change to Charlie2 and Charlie3 as the user |
| 223 |
3. Attempt to change to Charlie1 (within last 3) |
| 224 |
4. Run: dsconf <inst> localpwp set --pwdhistorycount=1 "ou=product testing,<suffix>" |
| 225 |
5. Attempt to change to Charlie1 again |
| 226 |
:expectedresults: |
| 227 |
1. Success |
| 228 |
2. Success |
| 229 |
3. Change to Welcome1 is rejected |
| 230 |
4. CLI command succeeds |
| 231 |
5. Change to Welcome1 now succeeds (only last 1 is disallowed) |
| 232 |
""" |
| 233 |
inst = topology_st.standalone |
| 234 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 235 |
|
| 236 |
ensure_local_subtree_policy(inst, count=3) |
| 237 |
|
| 238 |
user = setup_entries |
| 239 |
user.reset_password('Charlie1') |
| 240 |
set_user_password(inst, user, 'Charlie2', bind_as_user_password='Charlie1', expect_violation=False) |
| 241 |
set_user_password(inst, user, 'Charlie3', bind_as_user_password='Charlie2', expect_violation=False) |
| 242 |
|
| 243 |
# With count=3, Welcome1 is within history |
| 244 |
set_user_password(inst, user, 'Charlie1', bind_as_user_password='Charlie3', expect_violation=True) |
| 245 |
|
| 246 |
# Reduce local count to 1 via CLI to exercise CLI mapping and updated code |
| 247 |
set_local_history_via_cli(inst, count=1) |
| 248 |
|
| 249 |
# Now Welcome1 should be allowed |
| 250 |
set_user_password(inst, user, 'Charlie1', bind_as_user_password='Charlie3', expect_violation=False) |
| 251 |
|
| 252 |
|
| 253 |
def test_history_local_only_enforced(topology_st, setup_entries): |
| 254 |
"""Local-only history enforcement with count 3 |
| 255 |
|
| 256 |
:id: af6ff34d-ac94-4108-a7b6-2b589c960154 |
| 257 |
:setup: Standalone instance, test OU and user |
| 258 |
:steps: |
| 259 |
1. Disable global password history (passwordHistory=off, passwordInHistory=0, inherit off) |
| 260 |
2. Ensure local subtree policy with passwordInHistory=3 |
| 261 |
3. Set password to Delta1, then change to Delta2 and Delta3 as the user |
| 262 |
4. Attempt to change to Delta1 |
| 263 |
5. Attempt to change to Delta5 |
| 264 |
6. Change once more to Delta6, then change to Delta1 |
| 265 |
:expectedresults: |
| 266 |
1. Success |
| 267 |
2. Success |
| 268 |
3. Success |
| 269 |
4. Change to Welcome1 is rejected (within last 3) |
| 270 |
5. Change to Welcome5 is accepted |
| 271 |
6. Welcome1 is now older than the last 3 and is accepted |
| 272 |
""" |
| 273 |
inst = topology_st.standalone |
| 274 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 275 |
|
| 276 |
set_global_history(inst, enabled=False, count=0, inherit_global='off') |
| 277 |
|
| 278 |
ensure_local_subtree_policy(inst, count=3) |
| 279 |
|
| 280 |
user = setup_entries |
| 281 |
user.reset_password('Delta1') |
| 282 |
set_user_password(inst, user, 'Delta2', bind_as_user_password='Delta1') |
| 283 |
set_user_password(inst, user, 'Delta3', bind_as_user_password='Delta2') |
| 284 |
|
| 285 |
# Within last 2 |
| 286 |
set_user_password(inst, user, 'Delta1', bind_as_user_password='Delta3', expect_violation=True) |
| 287 |
|
| 288 |
# New password allowed |
| 289 |
set_user_password(inst, user, 'Delta5', bind_as_user_password='Delta3', expect_violation=False) |
| 290 |
|
| 291 |
# Now Welcome1 is older than last 2 after one more change |
| 292 |
set_user_password(inst, user, 'Delta6', bind_as_user_password='Delta5', expect_violation=False) |
| 293 |
set_user_password(inst, user, 'Delta1', bind_as_user_password='Delta6', expect_violation=False) |
| 294 |
|
| 295 |
|
| 296 |
def test_user_policy_detection_and_enforcement(topology_st, setup_entries): |
| 297 |
"""User local policy is detected and enforced; removal falls back to global policy |
| 298 |
|
| 299 |
:id: 2213126a-1f47-468c-8337-0d2ee5d2d585 |
| 300 |
:setup: Standalone instance, test OU and user |
| 301 |
:steps: |
| 302 |
1. Set global policy passwordInHistory=1 |
| 303 |
2. Create a user local password policy on the user with passwordInHistory=3 |
| 304 |
3. Verify is_user_policy(USER_DN) is True |
| 305 |
4. Set password to Echo1, then change to Echo2 and Echo3 as the user |
| 306 |
5. Attempt to change to Echo1 (within last 3) |
| 307 |
6. Delete the user local policy |
| 308 |
7. Verify is_user_policy(USER_DN) is False |
| 309 |
8. Attempt to change to Echo1 again (now only last 1 disallowed by global) |
| 310 |
:expectedresults: |
| 311 |
1. Success |
| 312 |
2. Success |
| 313 |
3. is_user_policy returns True |
| 314 |
4. Success |
| 315 |
5. Change to Welcome1 is rejected |
| 316 |
6. Success |
| 317 |
7. is_user_policy returns False |
| 318 |
8. Change to Welcome1 succeeds (two back is allowed by global=1) |
| 319 |
""" |
| 320 |
inst = topology_st.standalone |
| 321 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 322 |
|
| 323 |
set_global_history(inst, enabled=True, count=1, inherit_global='on') |
| 324 |
|
| 325 |
pwp = PwPolicyManager(inst) |
| 326 |
user = setup_entries |
| 327 |
pwp.create_user_policy(user.dn, { |
| 328 |
'passwordChange': 'on', |
| 329 |
'passwordHistory': 'on', |
| 330 |
'passwordInHistory': '3', |
| 331 |
}) |
| 332 |
|
| 333 |
assert pwp.is_user_policy(user.dn) is True |
| 334 |
|
| 335 |
user.reset_password('Echo1') |
| 336 |
set_user_password(inst, user, 'Echo2', bind_as_user_password='Echo1', expect_violation=False) |
| 337 |
set_user_password(inst, user, 'Echo3', bind_as_user_password='Echo2', expect_violation=False) |
| 338 |
set_user_password(inst, user, 'Echo1', bind_as_user_password='Echo3', expect_violation=True) |
| 339 |
|
| 340 |
pwp.delete_local_policy(user.dn) |
| 341 |
assert pwp.is_user_policy(user.dn) is False |
| 342 |
|
| 343 |
# With only global=1, Echo1 (two back) is allowed |
| 344 |
set_user_password(inst, user, 'Echo1', bind_as_user_password='Echo3', expect_violation=False) |
| 345 |
|
| 346 |
|
| 347 |
if __name__ == '__main__': |
| 348 |
# Run isolated |
| 349 |
# -s for DEBUG mode |
| 350 |
CURRENT_FILE = os.path.realpath(__file__) |
| 351 |
pytest.main("-s %s" % CURRENT_FILE) |