| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2022 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 pytest |
| 11 |
import os |
| 12 |
import time |
| 13 |
import ldap |
| 14 |
from lib389._constants import * |
| 15 |
from lib389.idm.user import UserAccounts |
| 16 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 17 |
from test389.topologies import topology_st as topo |
| 18 |
from lib389.idm.directorymanager import DirectoryManager |
| 19 |
|
| 20 |
pytestmark = pytest.mark.tier1 |
| 21 |
|
| 22 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 23 |
if DEBUGGING: |
| 24 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 25 |
else: |
| 26 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 27 |
log = logging.getLogger(__name__) |
| 28 |
|
| 29 |
USER_ACI = '(targetattr="userpassword")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)' |
| 30 |
TOKEN = 'test_user123' |
| 31 |
|
| 32 |
user_properties = { |
| 33 |
'uid': 'Test_user123', |
| 34 |
'cn': 'test_user123', |
| 35 |
'sn': 'test_user123', |
| 36 |
'uidNumber': '1001', |
| 37 |
'gidNumber': '2001', |
| 38 |
'userpassword': PASSWORD, |
| 39 |
'description': 'userdesc', |
| 40 |
'homeDirectory': '/home/{}'.format('test_user')} |
| 41 |
|
| 42 |
|
| 43 |
def pwd_setup(topo): |
| 44 |
ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX) |
| 45 |
ou = ous.get('people') |
| 46 |
ou.add('aci', USER_ACI) |
| 47 |
|
| 48 |
topo.standalone.config.replace_many(('passwordCheckSyntax', 'on'), |
| 49 |
('passwordMinLength', '4'), |
| 50 |
('passwordMinCategories', '1')) |
| 51 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 52 |
return users.create(properties=user_properties) |
| 53 |
|
| 54 |
|
| 55 |
def test_token_lengths(topo): |
| 56 |
"""Test that password token length is enforced for various lengths including |
| 57 |
the same length as the attribute being checked by the policy. |
| 58 |
|
| 59 |
:id: dae9d916-2a03-4707-b454-9e901d295b13 |
| 60 |
:setup: Standalone instance |
| 61 |
:steps: |
| 62 |
1. Create user, setup global password policy |
| 63 |
2. Bind as user, change password to 'Abcd012+' |
| 64 |
3. Bind as user with 'Abcd012+', attempt changes to 'user', 'us123', 'Tuse!1234', 'Tuse!0987', 'Tabc!1234' |
| 65 |
4. For each passwordMinTokenLength 4, 6, 10: change settings, rebind as user, attempt password with token of that length from TOKEN |
| 66 |
5. Cleanup - delete user |
| 67 |
:expectedresults: |
| 68 |
1. User created, password policy enabled and set |
| 69 |
2. Success |
| 70 |
3. All attempts fail with CONSTRAINT_VIOLATION |
| 71 |
4. All attempts fail with CONSTRAINT_VIOLATION |
| 72 |
5. User successfully deleted |
| 73 |
""" |
| 74 |
user = pwd_setup(topo) |
| 75 |
dm = DirectoryManager(topo.standalone) |
| 76 |
|
| 77 |
try: |
| 78 |
# Verify that the user can change their password |
| 79 |
user.rebind(PASSWORD) |
| 80 |
user.replace('userpassword', 'Abcd012+') |
| 81 |
|
| 82 |
# Verify that the default password policy is enforced |
| 83 |
user.rebind('Abcd012+') |
| 84 |
for new_password in ['user', 'us123', 'Tuse!1234', 'Tuse!0987', 'Tabc!1234']: |
| 85 |
log.info(f"Testing password {new_password}") |
| 86 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 87 |
user.replace('userpassword', new_password) |
| 88 |
|
| 89 |
# Verify that the password policy is enforced for different token lengths |
| 90 |
for length in ['4', '6', '10']: |
| 91 |
dm.rebind(PASSWORD) |
| 92 |
topo.standalone.config.set('passwordMinTokenLength', length) |
| 93 |
user.rebind('Abcd012+') |
| 94 |
time.sleep(1) |
| 95 |
|
| 96 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 97 |
passwd = TOKEN[:int(length)] |
| 98 |
log.info("Testing password len {} token ({})".format(length, passwd)) |
| 99 |
user.replace('userpassword', passwd) |
| 100 |
|
| 101 |
finally: |
| 102 |
# Cleanup |
| 103 |
user.delete() |
| 104 |
|
| 105 |
|
| 106 |
if __name__ == '__main__': |
| 107 |
# Run isolated |
| 108 |
# -s for DEBUG mode |
| 109 |
CURRENT_FILE = os.path.realpath(__file__) |
| 110 |
pytest.main("-s %s" % CURRENT_FILE) |
| 111 |
|