| 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 ldap |
| 13 |
import time |
| 14 |
from ldap.controls.ppolicy import PasswordPolicyControl |
| 15 |
from test389.topologies import topology_st as topo |
| 16 |
from lib389.idm.user import UserAccounts |
| 17 |
from lib389._constants import (DN_DM, PASSWORD, DEFAULT_SUFFIX) |
| 18 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 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_DN = 'uid=test entry,ou=people,dc=example,dc=com' |
| 30 |
USER_PW = b'password123' |
| 31 |
USER_ACI = '(targetattr="userpassword")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)' |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture |
| 35 |
def init_user(topo, request): |
| 36 |
"""Initialize a user - Delete and re-add test user |
| 37 |
""" |
| 38 |
try: |
| 39 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 40 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 41 |
user = users.get('test entry') |
| 42 |
user.delete() |
| 43 |
except ldap.NO_SUCH_OBJECT: |
| 44 |
pass |
| 45 |
except ldap.LDAPError as e: |
| 46 |
log.error("Failed to delete user, error: {}".format(e.message['desc'])) |
| 47 |
assert False |
| 48 |
|
| 49 |
user_data = {'uid': 'test entry', |
| 50 |
'cn': 'test entry', |
| 51 |
'sn': 'test entry', |
| 52 |
'uidNumber': '3000', |
| 53 |
'gidNumber': '4000', |
| 54 |
'homeDirectory': '/home/test_entry', |
| 55 |
'userPassword': USER_PW} |
| 56 |
users.create(properties=user_data) |
| 57 |
|
| 58 |
|
| 59 |
def change_passwd(topo): |
| 60 |
"""Reset users password as the user, then re-bind as Directory Manager |
| 61 |
""" |
| 62 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 63 |
user = users.get('test entry') |
| 64 |
user.rebind(USER_PW) |
| 65 |
user.reset_password(USER_PW) |
| 66 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 67 |
|
| 68 |
|
| 69 |
def bind_and_get_control(topo, err=0): |
| 70 |
"""Bind as the user, and return any controls |
| 71 |
""" |
| 72 |
res_type = res_data = res_msgid = res_ctrls = None |
| 73 |
result_id = '' |
| 74 |
|
| 75 |
try: |
| 76 |
result_id = topo.standalone.simple_bind(USER_DN, USER_PW, |
| 77 |
serverctrls=[PasswordPolicyControl()]) |
| 78 |
res_type, res_data, res_msgid, res_ctrls = topo.standalone.result3(result_id) |
| 79 |
if err: |
| 80 |
log.fatal('Expected an error, but bind succeeded') |
| 81 |
assert False |
| 82 |
except ldap.LDAPError as e: |
| 83 |
if err: |
| 84 |
log.debug('Got expected error: {}'.format(str(e))) |
| 85 |
pass |
| 86 |
else: |
| 87 |
log.fatal('Did not expect an error: {}'.format(str(e))) |
| 88 |
assert False |
| 89 |
|
| 90 |
if DEBUGGING and res_ctrls and len(res_ctrls) > 0: |
| 91 |
for ctl in res_ctrls: |
| 92 |
if ctl.timeBeforeExpiration: |
| 93 |
log.debug('control time before expiration: {}'.format(ctl.timeBeforeExpiration)) |
| 94 |
if ctl.graceAuthNsRemaining: |
| 95 |
log.debug('control grace login remaining: {}'.format(ctl.graceAuthNsRemaining)) |
| 96 |
if ctl.error is not None and ctl.error >= 0: |
| 97 |
log.debug('control error: {}'.format(ctl.error)) |
| 98 |
|
| 99 |
topo.standalone.simple_bind_s(DN_DM, PASSWORD) |
| 100 |
return res_ctrls |
| 101 |
|
| 102 |
|
| 103 |
def test_pwd_must_change(topo, init_user): |
| 104 |
"""Test for expiration control when password must be changed because an |
| 105 |
admin reset the password |
| 106 |
|
| 107 |
:id: a3d99be5-0b69-410d-b72f-04eda8821a56 |
| 108 |
:setup: Standalone instance, a user for testing |
| 109 |
:steps: |
| 110 |
1. Configure password policy and reset password as admin |
| 111 |
2. Bind, and check for expired control withthe proper error code "2" |
| 112 |
:expectedresults: |
| 113 |
1. Config update succeeds, adn the password is reset |
| 114 |
2. The EXPIRED control is returned, and we the expected error code "2" |
| 115 |
""" |
| 116 |
|
| 117 |
log.info('Configure password policy with paswordMustChange set to "on"') |
| 118 |
topo.standalone.config.set('passwordExp', 'on') |
| 119 |
topo.standalone.config.set('passwordMaxAge', '200') |
| 120 |
topo.standalone.config.set('passwordGraceLimit', '0') |
| 121 |
topo.standalone.config.set('passwordWarning', '199') |
| 122 |
topo.standalone.config.set('passwordMustChange', 'on') |
| 123 |
|
| 124 |
ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX) |
| 125 |
ou = ous.get('people') |
| 126 |
ou.add('aci', USER_ACI) |
| 127 |
|
| 128 |
log.info('Reset userpassword as Directory Manager') |
| 129 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 130 |
user = users.get('test entry') |
| 131 |
user.reset_password(USER_PW) |
| 132 |
|
| 133 |
log.info('Bind should return ctrl with error code 2 (changeAfterReset)') |
| 134 |
time.sleep(2) |
| 135 |
ctrls = bind_and_get_control(topo) |
| 136 |
if ctrls and len(ctrls) > 0: |
| 137 |
if ctrls[0].error is None: |
| 138 |
log.fatal("Response ctrl error code not set") |
| 139 |
assert False |
| 140 |
elif ctrls[0].error != 2: |
| 141 |
log.fatal("Got unexpected error code: {}".format(ctrls[0].error)) |
| 142 |
assert False |
| 143 |
else: |
| 144 |
log.fatal("We did not get a response ctrl") |
| 145 |
assert False |
| 146 |
|
| 147 |
|
| 148 |
def test_pwd_expired_grace_limit(topo, init_user): |
| 149 |
"""Test for expiration control when password is expired, but there are |
| 150 |
remaining grace logins |
| 151 |
|
| 152 |
:id: a3d99be5-0b69-410d-b72f-04eda8821a51 |
| 153 |
:setup: Standalone instance, a user for testing |
| 154 |
:steps: |
| 155 |
1. Configure password policy and reset password,adn allow it to expire |
| 156 |
2. Bind, and check for expired control, and grace limit |
| 157 |
3. Bind again, consuming the last grace login, control should be returned |
| 158 |
4. Bind again, it should fail, and no control returned |
| 159 |
:expectedresults: |
| 160 |
1. Config update and password reset are successful |
| 161 |
2. The EXPIRED control is returned, and we get the expected number |
| 162 |
of grace logins in the control |
| 163 |
3. The response control has the expected value for grace logins |
| 164 |
4. The bind fails with error 49, and no contorl is returned |
| 165 |
""" |
| 166 |
|
| 167 |
log.info('Configure password policy with grace limit set tot 2') |
| 168 |
topo.standalone.config.set('passwordExp', 'on') |
| 169 |
topo.standalone.config.set('passwordMaxAge', '5') |
| 170 |
topo.standalone.config.set('passwordGraceLimit', '2') |
| 171 |
|
| 172 |
log.info('Change password and wait for it to expire') |
| 173 |
change_passwd(topo) |
| 174 |
time.sleep(6) |
| 175 |
|
| 176 |
log.info('Bind and use up one grace login (only one left)') |
| 177 |
ctrls = bind_and_get_control(topo) |
| 178 |
if ctrls is None or len(ctrls) == 0: |
| 179 |
log.fatal('Did not get EXPIRED control in resposne') |
| 180 |
assert False |
| 181 |
else: |
| 182 |
if int(ctrls[0].graceAuthNsRemaining) != 1: |
| 183 |
log.fatal('Got unexpected value for grace logins: {}'.format(ctrls[0].graceAuthNsRemaining)) |
| 184 |
assert False |
| 185 |
|
| 186 |
log.info('Use up last grace login, should get control') |
| 187 |
ctrls = bind_and_get_control(topo) |
| 188 |
if ctrls is None or len(ctrls) == 0: |
| 189 |
log.fatal('Did not get control in response') |
| 190 |
assert False |
| 191 |
|
| 192 |
log.info('No grace login available, bind should fail, and no control should be returned') |
| 193 |
ctrls = bind_and_get_control(topo, err=49) |
| 194 |
if ctrls and len(ctrls) > 0: |
| 195 |
log.fatal('Incorrectly got control in response') |
| 196 |
assert False |
| 197 |
|
| 198 |
|
| 199 |
def test_pwd_expiring_with_warning(topo, init_user): |
| 200 |
"""Test expiring control response before and after warning is sent |
| 201 |
|
| 202 |
:id: 3594431f-e681-4a04-8edb-33ad2d9dad5b |
| 203 |
:setup: Standalone instance, a user for testing |
| 204 |
:steps: |
| 205 |
1. Configure password policy, and reset password |
| 206 |
2. Check for EXPIRING control, and the "time to expire" |
| 207 |
3. Bind again, as a warning has now been sent, and check the "time to expire" |
| 208 |
:expectedresults: |
| 209 |
1. Configuration update and password reset are successful |
| 210 |
2. Get the EXPIRING control, and the expected "time to expire" values |
| 211 |
3. Get the EXPIRING control, and the expected "time to expire" values |
| 212 |
""" |
| 213 |
|
| 214 |
log.info('Configure password policy') |
| 215 |
topo.standalone.config.set('passwordExp', 'on') |
| 216 |
topo.standalone.config.set('passwordMaxAge', '50') |
| 217 |
topo.standalone.config.set('passwordWarning', '50') |
| 218 |
|
| 219 |
log.info('Change password and get controls') |
| 220 |
change_passwd(topo) |
| 221 |
ctrls = bind_and_get_control(topo) |
| 222 |
if ctrls is None or len(ctrls) == 0: |
| 223 |
log.fatal('Did not get EXPIRING control in response') |
| 224 |
assert False |
| 225 |
|
| 226 |
if int(ctrls[0].timeBeforeExpiration) < 50: |
| 227 |
log.fatal('Got unexpected value for timeBeforeExpiration: {}'.format(ctrls[0].timeBeforeExpiration)) |
| 228 |
assert False |
| 229 |
|
| 230 |
log.info('Warning has been sent, try the bind again, and recheck the expiring time') |
| 231 |
time.sleep(5) |
| 232 |
ctrls = bind_and_get_control(topo) |
| 233 |
if ctrls is None or len(ctrls) == 0: |
| 234 |
log.fatal('Did not get EXPIRING control in resposne') |
| 235 |
assert False |
| 236 |
|
| 237 |
if int(ctrls[0].timeBeforeExpiration) > 50: |
| 238 |
log.fatal('Got unexpected value for timeBeforeExpiration: {}'.format(ctrls[0].timeBeforeExpiration)) |
| 239 |
assert False |
| 240 |
|
| 241 |
|
| 242 |
def test_pwd_expiring_with_no_warning(topo, init_user): |
| 243 |
"""Test expiring control response when no warning is sent |
| 244 |
|
| 245 |
:id: a3d99be5-0b69-410d-b72f-04eda8821a54 |
| 246 |
:setup: Standalone instance, a user for testing |
| 247 |
:steps: |
| 248 |
1. Configure password policy, and reset password |
| 249 |
2. Bind, and check that no controls are returned |
| 250 |
3. Set passwordSendExpiringTime to "on", bind, and check that the |
| 251 |
EXPIRING control is returned |
| 252 |
:expectedresults: |
| 253 |
1. Configuration update and passwordreset are successful |
| 254 |
2. No control is returned from bind |
| 255 |
3. A control is returned after setting "passwordSendExpiringTime" |
| 256 |
""" |
| 257 |
|
| 258 |
log.info('Configure password policy') |
| 259 |
topo.standalone.config.set('passwordExp', 'on') |
| 260 |
topo.standalone.config.set('passwordMaxAge', '50') |
| 261 |
topo.standalone.config.set('passwordWarning', '5') |
| 262 |
|
| 263 |
log.info('When the warning is less than the max age, we never send expiring control response') |
| 264 |
change_passwd(topo) |
| 265 |
ctrls = bind_and_get_control(topo) |
| 266 |
if len(ctrls) > 0: |
| 267 |
log.fatal('Incorrectly got a response control: {}'.format(ctrls)) |
| 268 |
assert False |
| 269 |
|
| 270 |
log.info('Turn on sending expiring control regardless of warning') |
| 271 |
topo.standalone.config.set('passwordSendExpiringTime', 'on') |
| 272 |
|
| 273 |
ctrls = bind_and_get_control(topo) |
| 274 |
if ctrls is None or len(ctrls) == 0: |
| 275 |
log.fatal('Did not get EXPIRED control in response') |
| 276 |
assert False |
| 277 |
|
| 278 |
if int(ctrls[0].timeBeforeExpiration) < 49: |
| 279 |
log.fatal('Got unexpected value for time before expiration: {}'.format(ctrls[0].timeBeforeExpiration)) |
| 280 |
assert False |
| 281 |
|
| 282 |
log.info('Check expiring time again') |
| 283 |
time.sleep(6) |
| 284 |
ctrls = bind_and_get_control(topo) |
| 285 |
if ctrls is None or len(ctrls) == 0: |
| 286 |
log.fatal('Did not get EXPIRED control in resposne') |
| 287 |
assert False |
| 288 |
|
| 289 |
if int(ctrls[0].timeBeforeExpiration) > 51: |
| 290 |
log.fatal('Got unexpected value for time before expiration: {}'.format(ctrls[0].timeBeforeExpiration)) |
| 291 |
assert False |
| 292 |
|
| 293 |
log.info('Turn off sending expiring control (restore the default setting)') |
| 294 |
topo.standalone.config.set('passwordSendExpiringTime', 'off') |
| 295 |
|
| 296 |
if __name__ == '__main__': |
| 297 |
# Run isolated |
| 298 |
# -s for DEBUG mode |
| 299 |
CURRENT_FILE = os.path.realpath(__file__) |
| 300 |
pytest.main("-s %s" % CURRENT_FILE) |