| 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 |
|
| 10 |
""" |
| 11 |
This file contains the test for password storage scheme |
| 12 |
""" |
| 13 |
|
| 14 |
import os |
| 15 |
import subprocess |
| 16 |
import shutil |
| 17 |
import pytest |
| 18 |
|
| 19 |
from test389.topologies import topology_st as topo |
| 20 |
from lib389.idm.user import UserAccounts, UserAccount |
| 21 |
from lib389._constants import DEFAULT_SUFFIX, DN_DM, PASSWORD, ErrorLog |
| 22 |
from lib389.config import Config |
| 23 |
from lib389.password_plugins import ( |
| 24 |
SSHA512Plugin, |
| 25 |
PBKDF2SHA1Plugin, |
| 26 |
PBKDF2SHA256Plugin, |
| 27 |
PBKDF2SHA512Plugin |
| 28 |
) |
| 29 |
from lib389.utils import ds_is_older |
| 30 |
|
| 31 |
pytestmark = pytest.mark.tier1 |
| 32 |
|
| 33 |
PBKDF2_NUM_ITERATIONS_DEFAULT = 100000 |
| 34 |
|
| 35 |
PBKDF2_SCHEMES = [ |
| 36 |
('PBKDF2-SHA1', PBKDF2SHA1Plugin, PBKDF2_NUM_ITERATIONS_DEFAULT), |
| 37 |
('PBKDF2-SHA256', PBKDF2SHA256Plugin, PBKDF2_NUM_ITERATIONS_DEFAULT), |
| 38 |
('PBKDF2-SHA512', PBKDF2SHA512Plugin, PBKDF2_NUM_ITERATIONS_DEFAULT) |
| 39 |
] |
| 40 |
|
| 41 |
|
| 42 |
@pytest.fixture(scope="function") |
| 43 |
def new_user(request, topo): |
| 44 |
"""Fixture to create and clean up a test user for each test""" |
| 45 |
# Generate unique user ID based on test name |
| 46 |
uid = f'new_user_{request.node.name[:20]}' |
| 47 |
|
| 48 |
# Create user |
| 49 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 50 |
user = users.create(properties={ |
| 51 |
'uid': uid, |
| 52 |
'cn': 'Test User', |
| 53 |
'sn': 'User', |
| 54 |
'uidNumber': '1000', |
| 55 |
'gidNumber': '2000', |
| 56 |
'homeDirectory': f'/home/{uid}' |
| 57 |
}) |
| 58 |
|
| 59 |
def fin(): |
| 60 |
try: |
| 61 |
# Ensure we're bound as DM before cleanup |
| 62 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 63 |
if user.exists(): |
| 64 |
user.delete() |
| 65 |
except Exception as e: |
| 66 |
log.error(f"Error during user cleanup: {e}") |
| 67 |
|
| 68 |
request.addfinalizer(fin) |
| 69 |
return user |
| 70 |
|
| 71 |
|
| 72 |
def user_config(topo, field_value): |
| 73 |
""" |
| 74 |
Will set storage schema and create user. |
| 75 |
""" |
| 76 |
Config(topo.standalone).replace("passwordStorageScheme", field_value) |
| 77 |
user = UserAccounts(topo.standalone, DEFAULT_SUFFIX).create_test_user() |
| 78 |
user.set('userpassword', 'ItsMeAnuj') |
| 79 |
return user |
| 80 |
|
| 81 |
|
| 82 |
LIST_FOR_PARAMETERIZATION = ["CRYPT", "SHA", "SSHA", "SHA256", "SSHA256", |
| 83 |
"SHA384", "SSHA384", "SHA512", "SSHA512", "MD5", "PBKDF2_SHA256"] |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.parametrize("value", LIST_FOR_PARAMETERIZATION, ids=LIST_FOR_PARAMETERIZATION) |
| 87 |
def test_check_password_scheme(topo, value): |
| 88 |
"""Check all password scheme. |
| 89 |
|
| 90 |
:id: 196bccfc-33a6-11ea-a2a5-8c16451d917b |
| 91 |
:parametrized: yes |
| 92 |
:setup: Standalone |
| 93 |
:steps: |
| 94 |
1. Change password scheme and create user with password. |
| 95 |
2. check password scheme is set . |
| 96 |
3. Delete user |
| 97 |
:expectedresults: |
| 98 |
1. Pass |
| 99 |
2. Pass |
| 100 |
3. Pass |
| 101 |
""" |
| 102 |
user = user_config(topo, value) |
| 103 |
assert '{' + f'{value.lower()}' + '}' in \ |
| 104 |
UserAccount(topo.standalone, user.dn).get_attr_val_utf8('userpassword').lower() |
| 105 |
user.delete() |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize('scheme_name,plugin_class,default_rounds', PBKDF2_SCHEMES) |
| 109 |
def test_pbkdf2_default_rounds(topo, new_user, scheme_name, plugin_class, default_rounds): |
| 110 |
"""Test PBKDF2 schemes with default iteration rounds. |
| 111 |
|
| 112 |
:id: bd58cd76-14f9-4d54-9793-ee7bba8e5369 |
| 113 |
:parametrized: yes |
| 114 |
:setup: Standalone |
| 115 |
:steps: |
| 116 |
1. Remove any existing rounds configuration |
| 117 |
2. Verify default rounds are used |
| 118 |
3. Set password and verify hash format |
| 119 |
4. Test authentication |
| 120 |
:expectedresults: |
| 121 |
1. Pass |
| 122 |
2. Pass |
| 123 |
3. Pass |
| 124 |
4. Pass |
| 125 |
""" |
| 126 |
try: |
| 127 |
# Flush logs |
| 128 |
topo.standalone.restart() |
| 129 |
topo.standalone.config.loglevel((ErrorLog.DEFAULT, ErrorLog.PLUGIN)) |
| 130 |
topo.standalone.deleteErrorLogs() |
| 131 |
|
| 132 |
plugin = plugin_class(topo.standalone) |
| 133 |
plugin.remove_all('nsslapd-pwdpbkdf2numiterations') |
| 134 |
topo.standalone.restart() |
| 135 |
|
| 136 |
topo.standalone.config.replace('passwordStorageScheme', scheme_name) |
| 137 |
|
| 138 |
current_rounds = plugin.get_rounds() |
| 139 |
assert current_rounds == default_rounds, \ |
| 140 |
f"Expected default {default_rounds} rounds, got {current_rounds}" |
| 141 |
|
| 142 |
new_user.set('userPassword', 'Secret123') |
| 143 |
pwd_hash = new_user.get_attr_val_utf8('userPassword') |
| 144 |
assert pwd_hash.startswith('{' + scheme_name.upper() + '}') |
| 145 |
assert str(default_rounds) in pwd_hash |
| 146 |
|
| 147 |
topo.standalone.simple_bind_s(new_user.dn, 'Secret123') |
| 148 |
|
| 149 |
assert topo.standalone.searchErrorsLog( |
| 150 |
f'{scheme_name} - Number of iterations set to {default_rounds} from default' |
| 151 |
) |
| 152 |
finally: |
| 153 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 154 |
|
| 155 |
|
| 156 |
@pytest.mark.parametrize('scheme_name,plugin_class,default_rounds', PBKDF2_SCHEMES) |
| 157 |
def test_pbkdf2_rounds_reset(topo, new_user, scheme_name, plugin_class, default_rounds): |
| 158 |
"""Test PBKDF2 schemes rounds reset to defaults. |
| 159 |
|
| 160 |
:id: 59bf95c5-6a07-4db1-81eb-d59b54436826 |
| 161 |
:parametrized: yes |
| 162 |
:setup: Standalone |
| 163 |
:steps: |
| 164 |
1. Set custom rounds for PBKDF2 plugin |
| 165 |
2. Verify custom rounds are used |
| 166 |
3. Remove rounds configuration |
| 167 |
4. Verify defaults are restored |
| 168 |
5. Test password operations with default rounds |
| 169 |
:expectedresults: |
| 170 |
1. Pass |
| 171 |
2. Pass |
| 172 |
3. Pass |
| 173 |
4. Pass |
| 174 |
5. Pass |
| 175 |
""" |
| 176 |
try: |
| 177 |
# Flush logs |
| 178 |
topo.standalone.restart() |
| 179 |
topo.standalone.config.loglevel((ErrorLog.DEFAULT, ErrorLog.PLUGIN)) |
| 180 |
topo.standalone.deleteErrorLogs() |
| 181 |
|
| 182 |
test_rounds = 25000 |
| 183 |
plugin = plugin_class(topo.standalone) |
| 184 |
plugin.set_rounds(test_rounds) |
| 185 |
topo.standalone.restart() |
| 186 |
|
| 187 |
current_rounds = plugin.get_rounds() |
| 188 |
assert current_rounds == test_rounds, \ |
| 189 |
f"Expected {test_rounds} rounds, got {current_rounds}" |
| 190 |
|
| 191 |
plugin.remove_all('nsslapd-pwdpbkdf2numiterations') |
| 192 |
topo.standalone.restart() |
| 193 |
|
| 194 |
current_rounds = plugin.get_rounds() |
| 195 |
assert current_rounds == default_rounds, \ |
| 196 |
f"Expected default {default_rounds} rounds after reset, got {current_rounds}" |
| 197 |
|
| 198 |
topo.standalone.config.replace('passwordStorageScheme', scheme_name) |
| 199 |
|
| 200 |
new_user.set('userPassword', 'Secret123') |
| 201 |
pwd_hash = new_user.get_attr_val_utf8('userPassword') |
| 202 |
assert pwd_hash.startswith('{' + scheme_name.upper() + '}') |
| 203 |
assert str(default_rounds) in pwd_hash |
| 204 |
|
| 205 |
topo.standalone.simple_bind_s(new_user.dn, 'Secret123') |
| 206 |
|
| 207 |
assert topo.standalone.searchErrorsLog( |
| 208 |
f'{scheme_name} - Number of iterations set to {default_rounds} from default' |
| 209 |
) |
| 210 |
finally: |
| 211 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 212 |
|
| 213 |
|
| 214 |
@pytest.mark.parametrize('scheme_name,plugin_class,_', PBKDF2_SCHEMES) |
| 215 |
@pytest.mark.parametrize('rounds', [10000, 20000, 50000]) |
| 216 |
def test_pbkdf2_custom_rounds(topo, new_user, scheme_name, plugin_class, _, rounds): |
| 217 |
"""Test PBKDF2 schemes with custom iteration rounds. |
| 218 |
|
| 219 |
:id: 6bec6542-ed8d-4a0e-89d6-e047757767c2 |
| 220 |
:parametrized: yes |
| 221 |
:setup: Standalone |
| 222 |
:steps: |
| 223 |
1. Set custom rounds for PBKDF2 plugin |
| 224 |
2. Verify rounds are set correctly |
| 225 |
3. Set password and verify hash format |
| 226 |
4. Test authentication |
| 227 |
5. Verify rounds in password hash |
| 228 |
:expectedresults: |
| 229 |
1. Pass |
| 230 |
2. Pass |
| 231 |
3. Pass |
| 232 |
4. Pass |
| 233 |
5. Pass |
| 234 |
""" |
| 235 |
try: |
| 236 |
# Flush logs |
| 237 |
topo.standalone.restart() |
| 238 |
topo.standalone.config.loglevel((ErrorLog.DEFAULT, ErrorLog.PLUGIN)) |
| 239 |
topo.standalone.deleteErrorLogs() |
| 240 |
|
| 241 |
plugin = plugin_class(topo.standalone) |
| 242 |
plugin.set_rounds(rounds) |
| 243 |
topo.standalone.restart() |
| 244 |
|
| 245 |
current_rounds = plugin.get_rounds() |
| 246 |
assert current_rounds == rounds, \ |
| 247 |
f"Expected {rounds} rounds, got {current_rounds}" |
| 248 |
|
| 249 |
topo.standalone.config.replace('passwordStorageScheme', scheme_name) |
| 250 |
|
| 251 |
new_user.set('userPassword', 'Secret123') |
| 252 |
pwd_hash = new_user.get_attr_val_utf8('userPassword') |
| 253 |
assert pwd_hash.startswith('{' + scheme_name.upper() + '}') |
| 254 |
assert str(rounds) in pwd_hash |
| 255 |
|
| 256 |
topo.standalone.simple_bind_s(new_user.dn, 'Secret123') |
| 257 |
|
| 258 |
assert topo.standalone.searchErrorsLog( |
| 259 |
f'{scheme_name} - Number of iterations set' |
| 260 |
) |
| 261 |
finally: |
| 262 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 263 |
|
| 264 |
|
| 265 |
@pytest.mark.parametrize('scheme_name,plugin_class,_', PBKDF2_SCHEMES) |
| 266 |
def test_pbkdf2_invalid_rounds(topo, scheme_name, plugin_class, _): |
| 267 |
"""Test PBKDF2 schemes with invalid iteration rounds. |
| 268 |
|
| 269 |
:id: 4e5b4f37-c97b-4f58-b5c5-726495d9fa4e |
| 270 |
:parametrized: yes |
| 271 |
:setup: Standalone |
| 272 |
:steps: |
| 273 |
1. Try to set invalid rounds (too low and too high) |
| 274 |
2. Verify appropriate errors are raised |
| 275 |
3. Verify original rounds are maintained |
| 276 |
:expectedresults: |
| 277 |
1. Pass |
| 278 |
2. Pass |
| 279 |
3. Pass |
| 280 |
""" |
| 281 |
# Flush logs |
| 282 |
topo.standalone.restart() |
| 283 |
topo.standalone.config.loglevel((ErrorLog.DEFAULT, ErrorLog.PLUGIN)) |
| 284 |
topo.standalone.deleteErrorLogs() |
| 285 |
|
| 286 |
plugin = plugin_class(topo.standalone) |
| 287 |
plugin.enable() |
| 288 |
|
| 289 |
original_rounds = plugin.get_rounds() |
| 290 |
|
| 291 |
with pytest.raises(ValueError) as excinfo: |
| 292 |
plugin.set_rounds(5000) |
| 293 |
assert "rounds must be between 10,000 and 10,000,000" in str(excinfo.value) |
| 294 |
|
| 295 |
with pytest.raises(ValueError) as excinfo: |
| 296 |
plugin.set_rounds(20000000) |
| 297 |
assert "rounds must be between 10,000 and 10,000,000" in str(excinfo.value) |
| 298 |
|
| 299 |
current_rounds = plugin.get_rounds() |
| 300 |
assert current_rounds == original_rounds, \ |
| 301 |
f"Rounds changed from {original_rounds} to {current_rounds}" |
| 302 |
|
| 303 |
|
| 304 |
@pytest.mark.parametrize('scheme_name,plugin_class,_', PBKDF2_SCHEMES) |
| 305 |
def test_pbkdf2_rounds_persistence(topo, new_user, scheme_name, plugin_class, _): |
| 306 |
"""Test PBKDF2 rounds persistence across server restarts. |
| 307 |
|
| 308 |
:id: b15de1ae-53ac-429f-991b-cea5e6a7b383 |
| 309 |
:parametrized: yes |
| 310 |
:setup: Standalone |
| 311 |
:steps: |
| 312 |
1. Set custom rounds for PBKDF2 plugin |
| 313 |
2. Restart server |
| 314 |
3. Verify rounds are maintained |
| 315 |
4. Set password and verify hash |
| 316 |
5. Test authentication |
| 317 |
:expectedresults: |
| 318 |
1. Pass |
| 319 |
2. Pass |
| 320 |
3. Pass |
| 321 |
4. Pass |
| 322 |
5. Pass |
| 323 |
""" |
| 324 |
try: |
| 325 |
# Flush logs |
| 326 |
topo.standalone.restart() |
| 327 |
topo.standalone.config.loglevel((ErrorLog.DEFAULT, ErrorLog.PLUGIN)) |
| 328 |
topo.standalone.deleteErrorLogs() |
| 329 |
|
| 330 |
test_rounds = 15000 |
| 331 |
plugin = plugin_class(topo.standalone) |
| 332 |
plugin.set_rounds(test_rounds) |
| 333 |
topo.standalone.restart() |
| 334 |
|
| 335 |
current_rounds = plugin.get_rounds() |
| 336 |
assert current_rounds == test_rounds, \ |
| 337 |
f"Expected {test_rounds} rounds after restart, got {current_rounds}" |
| 338 |
|
| 339 |
topo.standalone.config.replace('passwordStorageScheme', scheme_name) |
| 340 |
|
| 341 |
new_user.set('userPassword', 'Secret123') |
| 342 |
pwd_hash = new_user.get_attr_val_utf8('userPassword') |
| 343 |
assert str(test_rounds) in pwd_hash |
| 344 |
|
| 345 |
topo.standalone.simple_bind_s(new_user.dn, 'Secret123') |
| 346 |
finally: |
| 347 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 348 |
|
| 349 |
|
| 350 |
def test_clear_scheme(topo): |
| 351 |
"""Check clear password scheme. |
| 352 |
|
| 353 |
:id: 2420aadc-33a6-11ea-b59a-8c16451d917b |
| 354 |
:setup: Standalone |
| 355 |
:steps: |
| 356 |
1. Change password scheme and create user with password. |
| 357 |
2. check password scheme is set . |
| 358 |
3. Delete user |
| 359 |
:expectedresults: |
| 360 |
1. Pass |
| 361 |
2. Pass |
| 362 |
3. Pass |
| 363 |
""" |
| 364 |
user = user_config(topo, "CLEAR") |
| 365 |
assert "ItsMeAnuj" in UserAccount(topo.standalone, user.dn).get_attr_val_utf8('userpassword') |
| 366 |
user.delete() |
| 367 |
|
| 368 |
|
| 369 |
def test_check_two_scheme(topo): |
| 370 |
"""Check password scheme SHA and CRYPT |
| 371 |
|
| 372 |
:id: 2b677f1e-33a6-11ea-a371-8c16451d917b |
| 373 |
:setup: Standalone |
| 374 |
:steps: |
| 375 |
1. Change password scheme and create user with password. |
| 376 |
2. check password scheme is set . |
| 377 |
3. Delete user |
| 378 |
:expectedresults: |
| 379 |
1. Pass |
| 380 |
2. Pass |
| 381 |
3. Pass |
| 382 |
""" |
| 383 |
for schema, value in [("nsslapd-rootpwstoragescheme", "SHA"), |
| 384 |
("passwordStorageScheme", "CRYPT")]: |
| 385 |
Config(topo.standalone).replace(schema, value) |
| 386 |
topo.standalone.restart() |
| 387 |
user = UserAccounts(topo.standalone, DEFAULT_SUFFIX).create_test_user() |
| 388 |
user.set('userpassword', 'ItsMeAnuj') |
| 389 |
assert '{' + f'{"CRYPT".lower()}' + '}' \ |
| 390 |
in UserAccount(topo.standalone, user.dn).get_attr_val_utf8('userpassword').lower() |
| 391 |
user.delete() |
| 392 |
|
| 393 |
@pytest.mark.skipif(ds_is_older('1.4'), reason="Not implemented") |
| 394 |
def test_check_pbkdf2_sha512(topo): |
| 395 |
"""Check password scheme PBKDF2-SHA512 is restored after deletion |
| 396 |
|
| 397 |
:id: 31612e7e-33a6-11ea-a750-8c16451d917b |
| 398 |
:setup: Standalone |
| 399 |
:steps: |
| 400 |
1. Try to delete PBKDF2-SHA512. |
| 401 |
2. Should not deleted PBKDF2-SHA512 and server should up. |
| 402 |
:expectedresults: |
| 403 |
1. Pass |
| 404 |
2. Pass |
| 405 |
""" |
| 406 |
value = 'PBKDF2-SHA512' |
| 407 |
user = user_config(topo, value) |
| 408 |
assert '{' + f'{value.lower()}' + '}' in \ |
| 409 |
UserAccount(topo.standalone, user.dn).get_attr_val_utf8('userpassword').lower() |
| 410 |
plg = PBKDF2SHA512Plugin(topo.standalone) |
| 411 |
plg._protected = False |
| 412 |
plg.delete() |
| 413 |
topo.standalone.restart() |
| 414 |
assert Config(topo.standalone).get_attr_val_utf8('passwordStorageScheme') == value |
| 415 |
assert topo.standalone.status() |
| 416 |
user.delete() |
| 417 |
|
| 418 |
|
| 419 |
def test_check_ssha512(topo): |
| 420 |
"""Check password scheme SSHA512. |
| 421 |
|
| 422 |
:id: 9db023d2-33a1-11ea-b68c-8c16451d917b |
| 423 |
:setup: Standalone |
| 424 |
:steps: |
| 425 |
1. Try to delete SSHA512Plugin. |
| 426 |
2. Should deleted SSHA512Plugin and server should not up. |
| 427 |
3. Restore dse file to recover |
| 428 |
:expectedresults: |
| 429 |
1. Pass |
| 430 |
2. Pass |
| 431 |
3. Pass |
| 432 |
""" |
| 433 |
value = 'SSHA512' |
| 434 |
config_dir = topo.standalone.get_config_dir() |
| 435 |
user = user_config(topo, value) |
| 436 |
assert '{' + f'{value.lower()}' + '}' in \ |
| 437 |
UserAccount(topo.standalone, user.dn).get_attr_val_utf8('userpassword').lower() |
| 438 |
plg = SSHA512Plugin(topo.standalone) |
| 439 |
plg._protected = False |
| 440 |
plg.delete() |
| 441 |
with pytest.raises(subprocess.CalledProcessError): |
| 442 |
topo.standalone.restart() |
| 443 |
shutil.copy(config_dir + '/dse.ldif.startOK', config_dir + '/dse.ldif') |
| 444 |
topo.standalone.restart() |
| 445 |
user.delete() |
| 446 |
|
| 447 |
|
| 448 |
if __name__ == "__main__": |
| 449 |
CURRENT_FILE = os.path.realpath(__file__) |
| 450 |
pytest.main("-s -v %s" % CURRENT_FILE) |