| 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 ldap |
| 10 |
import logging |
| 11 |
import pytest |
| 12 |
import os |
| 13 |
import time |
| 14 |
from lib389._constants import DEFAULT_SUFFIX, PASSWORD |
| 15 |
from test389.topologies import topology_st as topo |
| 16 |
from lib389.pwpolicy import PwPolicyManager |
| 17 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 18 |
from lib389.idm.user import UserAccount, UserAccounts, TEST_USER_PROPERTIES |
| 19 |
from lib389.idm.domain import Domain |
| 20 |
|
| 21 |
log = logging.getLogger(__name__) |
| 22 |
|
| 23 |
LOCAL_RDN = 'ou=People' |
| 24 |
GLOBAL_RDN = 'ou=Global' |
| 25 |
LOCAL_BIND_DN ="uid=local_user,ou=people," + DEFAULT_SUFFIX |
| 26 |
GLOBAL_BIND_DN ="uid=global_user,ou=global," + DEFAULT_SUFFIX |
| 27 |
|
| 28 |
|
| 29 |
def create_entries(inst): |
| 30 |
# Create local user |
| 31 |
users = UserAccounts(inst, DEFAULT_SUFFIX, rdn=LOCAL_RDN) |
| 32 |
user_props = TEST_USER_PROPERTIES.copy() |
| 33 |
user_props.update({'uid': 'local_user', 'cn': 'local_user', 'userpassword': PASSWORD}) |
| 34 |
users.create(properties=user_props) |
| 35 |
|
| 36 |
# Create new OU |
| 37 |
ou_global = OrganizationalUnits(inst, DEFAULT_SUFFIX).create(properties={'ou': 'Global'}) |
| 38 |
|
| 39 |
# Create global user |
| 40 |
users = UserAccounts(inst, DEFAULT_SUFFIX, rdn=GLOBAL_RDN) |
| 41 |
user_props = TEST_USER_PROPERTIES.copy() |
| 42 |
user_props.update({'uid': 'global_user', 'cn': 'global_user', 'userpassword': PASSWORD}) |
| 43 |
users.create(properties=user_props) |
| 44 |
|
| 45 |
# Add aci |
| 46 |
aci = '(targetattr="*")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)' |
| 47 |
suffix = Domain(inst, DEFAULT_SUFFIX) |
| 48 |
suffix.add('aci', aci) |
| 49 |
|
| 50 |
|
| 51 |
def create_policies(inst): |
| 52 |
# Configure subtree policy |
| 53 |
pwp = PwPolicyManager(inst) |
| 54 |
subtree_policy_props = { |
| 55 |
'passwordCheckSyntax': 'on', |
| 56 |
'passwordMinLength': '6', |
| 57 |
'passwordChange': 'on', |
| 58 |
'passwordLockout': 'on', |
| 59 |
'passwordMaxFailure': '2', |
| 60 |
} |
| 61 |
pwp.create_subtree_policy(f'{LOCAL_RDN},{DEFAULT_SUFFIX}', subtree_policy_props) |
| 62 |
|
| 63 |
# Configure global policy |
| 64 |
inst.config.replace('nsslapd-pwpolicy-local', 'on') |
| 65 |
inst.config.replace('passwordCheckSyntax', 'on') |
| 66 |
inst.config.replace('passwordMinLength', '8') |
| 67 |
inst.config.replace('passwordChange', 'on') |
| 68 |
inst.config.replace('passwordLockout', 'on') |
| 69 |
inst.config.replace('passwordMaxFailure', '5') |
| 70 |
time.sleep(1) |
| 71 |
|
| 72 |
|
| 73 |
def test_debug_logging(topo): |
| 74 |
"""Enable password policy logging |
| 75 |
|
| 76 |
:id: cc152c65-94e0-4716-a77c-abdd2deec00d |
| 77 |
:setup: Standalone Instance |
| 78 |
:steps: |
| 79 |
1. Set password policy logging level |
| 80 |
2. Add database entries |
| 81 |
3. Configure local and global policies |
| 82 |
4. Test syntax checking on local policy |
| 83 |
5. Test syntax checking on global policy |
| 84 |
6. Test account lockout on local policy |
| 85 |
7. Test account lockout on global policy |
| 86 |
|
| 87 |
:expectedresults: |
| 88 |
1. Success |
| 89 |
2. Success |
| 90 |
3. Success |
| 91 |
4. Success |
| 92 |
5. Success |
| 93 |
6. Success |
| 94 |
7. Success |
| 95 |
""" |
| 96 |
|
| 97 |
inst = topo.standalone |
| 98 |
|
| 99 |
# Enable password policy debug logging |
| 100 |
inst.config.replace('nsslapd-errorlog-level', '1048576') |
| 101 |
|
| 102 |
# Create entries and password policies |
| 103 |
create_entries(inst) |
| 104 |
create_policies(inst) |
| 105 |
|
| 106 |
# Setup bind connections |
| 107 |
|
| 108 |
local_conn = UserAccounts(inst, DEFAULT_SUFFIX, rdn=LOCAL_RDN).get('local_user').bind(PASSWORD) |
| 109 |
local_user = UserAccount(local_conn, LOCAL_BIND_DN) |
| 110 |
global_conn = UserAccounts(inst, DEFAULT_SUFFIX, rdn=GLOBAL_RDN).get('global_user').bind(PASSWORD) |
| 111 |
global_user = UserAccount(global_conn, GLOBAL_BIND_DN) |
| 112 |
|
| 113 |
# Test syntax checking on local policy |
| 114 |
passwd_val = "passw" # len 5 which is less than configured 6 |
| 115 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 116 |
local_user.replace('userpassword', passwd_val) |
| 117 |
time.sleep(1) |
| 118 |
|
| 119 |
err_msg = "PWDPOLICY_DEBUG - Invalid password syntax - password must be at least 6 characters long: Entry " + \ |
| 120 |
"\\(uid=local_user,ou=people,dc=example,dc=com\\) Policy \\(cn=" |
| 121 |
assert inst.searchErrorsLog(err_msg) |
| 122 |
|
| 123 |
# Test syntax checking on global policy |
| 124 |
passwd_val = "passwod" # len 7 which is less than configured 8 |
| 125 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 126 |
global_user.replace('userpassword', passwd_val) |
| 127 |
time.sleep(1) |
| 128 |
|
| 129 |
err_msg = "PWDPOLICY_DEBUG - Invalid password syntax - password must be at least 8 characters long: Entry " + \ |
| 130 |
"\\(uid=global_user,ou=global,dc=example,dc=com\\) Policy \\(Global\\)" |
| 131 |
assert inst.searchErrorsLog(err_msg) |
| 132 |
|
| 133 |
# Test account lock is logging for local policy |
| 134 |
for i in range(2): |
| 135 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 136 |
local_user.bind("bad") |
| 137 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 138 |
local_user.bind("bad") |
| 139 |
|
| 140 |
err_msg = "PWDPOLICY_DEBUG - Account is locked: Entry " + \ |
| 141 |
"\\(uid=local_user,ou=people,dc=example,dc=com\\) Policy \\(cn=" |
| 142 |
assert inst.searchErrorsLog(err_msg) |
| 143 |
|
| 144 |
# Test account lock is logging for global policy |
| 145 |
for i in range(5): |
| 146 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 147 |
global_user.bind("bad") |
| 148 |
with pytest.raises(ldap.CONSTRAINT_VIOLATION): |
| 149 |
global_user.bind("bad") |
| 150 |
|
| 151 |
err_msg = "PWDPOLICY_DEBUG - Account is locked: Entry " + \ |
| 152 |
"\\(uid=global_user,ou=global,dc=example,dc=com\\) Policy \\(Global\\)" |
| 153 |
assert inst.searchErrorsLog(err_msg) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == '__main__': |
| 157 |
# Run isolated |
| 158 |
# -s for DEBUG mode |
| 159 |
CURRENT_FILE = os.path.realpath(__file__) |
| 160 |
pytest.main(["-s", CURRENT_FILE]) |