Debian Codegraph @

dirsrvtests/tests/suites/password/password_TPR_policy_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2021 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 os
11 import pytest
12 from lib389.utils import *
13 from test389.topologies import topology_st as topo
14 from test389.topologies import topology_m2c2 as topo_m2c2
15 from lib389.idm.user import UserAccounts, UserAccount
16 from lib389._constants import DEFAULT_SUFFIX, DN_DM
17 from lib389.config import Config
18 from lib389.idm.account import Accounts
19 from lib389.idm.organizationalunit import OrganizationalUnits, OrganizationalUnit
20 from lib389.idm.directorymanager import DirectoryManager
21 from lib389.pwpolicy import PwPolicyManager
22 from lib389.replica import Replicas, ReplicationManager
23 from lib389.dseldif import *
24 import time
25 import ldap
26
27 pytestmark = pytest.mark.tier1
28
29 logging.getLogger(__name__).setLevel(logging.DEBUG)
30 log = logging.getLogger(__name__)
31
32 DN_CONFIG = 'cn=config'
33 TEST_ENTRY_NAME = 'mmrepl_test'
34 TEST_ENTRY_DN = 'uid={},{}'.format(TEST_ENTRY_NAME, DEFAULT_SUFFIX)
35 NEW_SUFFIX_NAME = 'test_repl'
36 NEW_SUFFIX = 'o={}'.format(NEW_SUFFIX_NAME)
37 NEW_BACKEND = 'repl_base'
38 PASSWORD = 'password'
39 NEW_PASSWORD = 'changed_pass'
40 USER1_PASS = 'jdoe1_password'
41 USER2_PASS = 'jdoe2_password'
42
43
44 def get_agreement(agmts, consumer):
45 log.info('Get agreement towards consumer among the agreemment list')
46 for agmt in agmts.list():
47 if (agmt.get_attr_val_utf8('nsDS5ReplicaPort') == str(consumer.port) and
48 agmt.get_attr_val_utf8('nsDS5ReplicaHost') == consumer.host):
49 return agmt
50 return None
51
52
53 def _create_user(topo, uid, cn, sn, givenname, userpassword, gid, ou):
54 user = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=ou).create(properties={
55 'uid': uid,
56 'cn': cn,
57 'sn': sn,
58 'ou': ou,
59 'givenname': givenname,
60 'mail': f'{uid}@example.com',
61 'homeDirectory': f'/home/{uid}',
62 'uidNumber': '1000',
63 'gidNumber': gid,
64 'userpassword': userpassword,
65 })
66 log.info('Creating user {} with UID: {}'.format(givenname, uid))
67 return user
68
69
70 @pytest.fixture(scope="function")
71 def _add_user(request, topo):
72 for uid, cn, sn, givenname, userpassword, gid, ou in [
73 ('jdoe1', 'John Doe1', 'jdoe1', 'Johnny', USER1_PASS, '10001', 'ou=People'),
74 ('jdoe2', 'Jane Doe2', 'jdoe2', 'Janie', USER2_PASS, '10002', 'ou=People'),
75 ]:
76 user = _create_user(topo, uid, cn, sn, givenname, userpassword, gid, ou)
77 instance = f'ou=People,{DEFAULT_SUFFIX}'
78
79 def fin():
80
81 for user1 in UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=None).list():
82 user1.delete()
83
84 request.addfinalizer(fin)
85
86
87 def change_pwp_parameter(topo, pwp, operation, to_do):
88 """
89 Will change password policy parameter
90 """
91 pwp1 = PwPolicyManager(topo.standalone)
92 user = pwp1.get_pwpolicy_entry(f'{pwp},{DEFAULT_SUFFIX}')
93 user.replace(operation, to_do)
94
95
96 @pytest.fixture(scope="function")
97 def set_global_TPR_policies(request, topo):
98 """Sets the required global password policy attributes under
99 cn=config entry
100 """
101
102 attrs = {'passwordMustChange': '',
103 'passwordTPRMaxUse': '',
104 'passwordTPRDelayExpireAt': '',
105 'passwordTPRDelayValidFrom': '',
106 }
107 log.info('Get the default values')
108 entry = topo.standalone.getEntry(DN_CONFIG, ldap.SCOPE_BASE, '(objectClass=*)', attrs.keys())
109 for key in attrs.keys():
110 attrs[key] = entry.getValue(key)
111 log.info('Set the Global password policy passwordMustChange on, passwordTPRMaxUse 3')
112 log.info('passwordTPRDelayExpireAt 600, passwordTPRDelayValidFrom 6')
113 topo.standalone.config.replace_many(('passwordMustChange', 'on'),
114 ('passwordTPRMaxUse', '3'),
115 ('passwordTPRDelayExpireAt', '600'),
116 ('passwordTPRDelayValidFrom', '6'))
117
118 def fin():
119 """Resets the defaults"""
120
121 log.info('Reset the defaults')
122 topo.standalone.open()
123 for key in attrs.keys():
124 topo.standalone.config.replace(key, attrs[key])
125
126 request.addfinalizer(fin)
127 # A short sleep is required after the modifying password policy or cn=config
128 time.sleep(0.5)
129
130
131 def _create_local_pwp(topo, instance):
132 """
133 For a subtree entry create a local policy
134 """
135
136 policy_props = {}
137 pwp = PwPolicyManager(topo.standalone)
138 pwadm_locpol = pwp.create_subtree_policy(instance, policy_props)
139 for attribute, value in [
140 ('pwdmustchange', 'on'),
141 ('passwordTPRMaxUse', '3'),
142 ('passwordTPRDelayExpireAt', '1800'),
143 ('passwordTPRDelayValidFrom', '5'),
144 ]:
145 pwadm_locpol.add(attribute, value)
146 log.info('Creating local policies for subtree {}'.format(instance))
147 return pwadm_locpol
148
149
150 def test_only_user_can_reset_TPR(topo, _add_user, set_global_TPR_policies):
151 """ One Time password with expiration
152
153 :id: 07838d5e-db43-11eb-85e5-fa163ead4114
154 :customerscenario: True
155 :setup: Standalone
156 :steps:
157 1. Create DS Instance
158 2. Create 2 users with appropriate password
159 3. Create Global TPR policy enable passwordMustChange: on
160 4. Trigger Temporary password and reset user1 password
161 5. Bind as user#2 and attempt to Reset user#1 password as user#2
162 6. Verify admin can reset users#1,2 passwords
163
164 :expectedresults:
165 1. Success
166 2. Success
167 3. Success
168 4. Success
169 5. Fail(ldap.INSUFFICIENT_ACCESS)
170 6. Success
171
172 """
173 log.info('Creating 2 users with appropriate password')
174 user1 = UserAccount(topo.standalone, f'uid=jdoe1,ou=People,{DEFAULT_SUFFIX}')
175 user2 = UserAccount(topo.standalone, f'uid=jdoe2,ou=People,{DEFAULT_SUFFIX}')
176 log.info('Setting Local policies...')
177 conn_user2 = user2.bind(USER2_PASS)
178
179 UserAccount(conn_user2, user2.dn).replace('userpassword', 'reset_pass')
180 log.info('Attempting to change user#1 password as user#2 ')
181
182 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
183 UserAccount(conn_user2, user1.dn).replace('userpassword', 'reset_pass')
184
185
186 def test_local_TPR_supercedes_global_TPR(topo, _add_user, set_global_TPR_policies):
187 """ One Time password with expiration
188
189 :id: beb2dac4-e116-11eb-a85e-98fa9ba19b65
190 :customerscenario: True
191 :setup: Standalone
192 :steps:
193 1. Create DS Instance
194 2. Create user with appropriate password
195 3. Configure the Global Password policies with passwordTPRMaxUse 5
196 4. Configure different local password policy for passwordTPRMaxUse 3
197 5. Trigger TPR by resetting the user password above
198 6. Attempt an ldap search with an incorrect bind password for user above
199 7. Repeat as many times as set by attribute passwordTPRMaxUse
200 8. Should lock the account after value is set in the local passwordTPRMaxUse is reached
201 9. Try to search with the correct password account will be locked.
202
203 :expectedresults:
204 1. Success
205 2. Success
206 3. Fail(ldap.INSUFFICIENT_ACCESS)
207 4. Success
208 5. Success
209 6. Success
210 7. Success
211 8. Success
212 9. Success
213
214 """
215
216 user1 = UserAccount(topo.standalone, f'uid=jdoe1,ou=People,{DEFAULT_SUFFIX}')
217 user2 = UserAccount(topo.standalone, f'uid=jdoe2,ou=People,{DEFAULT_SUFFIX}')
218 log.info('Setting local password Temporary password reset policies')
219
220 log.info('Setting Global TPR policy attributes')
221 Config(topo.standalone).replace('passwordMustChange', 'on')
222 Config(topo.standalone).replace('passwordTPRMaxUse', '5')
223 Config(topo.standalone).replace('passwordTPRDelayExpireAt', '600')
224 Config(topo.standalone).replace('passwordTPRDelayValidFrom', '6')
225 log.info('Resetting {} password to trigger TPR policy'.format(user1))
226 user1.replace('userpassword', 'not_allowed_change')
227 count = 0
228
229 while count < 4:
230 if count == 4:
231 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
232 user2.bind('badbadbad')
233 else:
234 with pytest.raises(ldap.INVALID_CREDENTIALS):
235 count += 1
236 user2.bind('badbadbad')
237
238
239 def test_once_TPR_reset_old_passwd_invalid(topo, _add_user, set_global_TPR_policies):
240 """ Verify that once a password has been reset it cannot be reused
241
242 :id: f3ea4f00-e89c-11eb-b81d-98fa9ba19b65
243 :customerscenario: True
244 :setup: Standalone
245 :steps:
246 1. Create DS Instance
247 2. Create user jdoe1 with appropriate password
248 3. Configure the Global Password policies enable passwordMustChange
249 4. Trigger TPR by resetting the user jdoe1 password above
250 5. Attempt to login with the old password
251 6. Login as jdoe1 with the correct password and update the new password
252
253
254 :expectedresults:
255 1. Success
256 2. Success
257 3. Success
258 4. Success
259 5. Fail(ldap.CONSTRAINT_VIOLATION)
260 6. Success
261
262 """
263 new_password = 'test_password'
264 log.info('Creating user jdoe1 with appropriate password')
265 user1 = UserAccount(topo.standalone, f'uid=jdoe1,ou=People,{DEFAULT_SUFFIX}')
266 user1.replace('userpassword', new_password)
267 log.info('Making sure the Global Policy passwordTPRDelayValidFrom is short')
268 config = Config(topo.standalone)
269 config.replace_many(
270 ('passwordLockout', 'off'),
271 ('passwordMaxFailure', '3'),
272 ('passwordLegacyPolicy', 'off'),
273 ('passwordTPRDelayValidFrom', '-1'),
274 ('nsslapd-pwpolicy-local', 'on'), )
275
276 log.info(' Attempting to bind as {} with the old password {}'.format(user1, USER1_PASS))
277 time.sleep(.5)
278 with pytest.raises(ldap.INVALID_CREDENTIALS):
279 user1.bind(USER1_PASS)
280 log.info('Login as jdoe1 with the correct reset password')
281 time.sleep(.5)
282 user1.rebind(new_password)
283
284
285 def test_reset_pwd_before_passwordTPRDelayValidFrom(topo, _add_user, set_global_TPR_policies):
286 """ Verify that user cannot reset pwd
287 before passwordTPRDelayValidFrom value elapses
288
289 :id: 22987082-e8ae-11eb-a992-98fa9ba19b65
290 :customerscenario: True
291 :setup: Standalone
292 :steps:
293 1. Create DS Instance
294 2. Create user jdoe2 with appropriate password
295 3. Configure the Global Password policies disable passwordTPRDelayValidFrom to -1
296 4. Trigger TPR by resetting the user jdoe1 password above
297 5. Attempt to bind and rebind immediately
298 6. Set passwordTPRDelayValidFrom - 5secs elapses and bind rebind before 5 secs elapses
299 7. Wait for the passwordTPRDelayValidFrom value to elapse and try to reset passwd
300
301 :expectedresults:
302 1. Success
303 2. Success
304 3. Success
305 4. Success
306 5. Success
307 6. Fail(ldap.LDAP_CONSTRAINT_VIOLATION)
308 7. Success
309
310
311 """
312 user2 = UserAccount(topo.standalone, f'uid=jdoe2,ou=People,{DEFAULT_SUFFIX}')
313 log.info('Creating user {} with appropriate password'.format(user2))
314 log.info('Disabling TPR policy passwordTPRDelayValidFrom')
315 topo.standalone.config.replace_many(('passwordMustChange', 'on'),
316 ('passwordTPRDelayValidFrom', '10'))
317 log.info('Triggering TPR and binding immediately after')
318 user2.replace('userpassword', 'new_password')
319 time.sleep(.5)
320 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
321 user2.bind('new_password')
322 time.sleep(.5)
323 topo.standalone.config.replace_many(('passwordMustChange', 'on'),
324 ('passwordTPRDelayValidFrom', '-1'))
325 log.info('Triggering TPR and binding immediately after with passwordTPRDelayValidFrom set to -1')
326 user2.replace('userpassword', 'new_password1')
327 time.sleep(.5)
328 user2.rebind('new_password1')
329
330
331 def test_admin_resets_pwd_TPR_attrs_reset(topo, _add_user, set_global_TPR_policies):
332 """Test When the ‘userpassword’ is updated (update_pw_info) by an administrator
333 and it exists a TPR policy, then the server flags that the entry has a
334 TPR password with ‘pwdTPRReset: TRUE’, ‘pwdTPRExpTime’ and ‘pwdTPRUseCount’.
335
336 :id: e6a84dc0-f142-11eb-8c96-fa163e1f582c
337 :customerscenario: True
338 :setup: Standalone
339 :steps:
340 1. Create DS Instance
341 2. Create user jdoe2 with appropriate password
342 3. Configure the Global Password policies enable
343 4. Trigger TPR by resetting the user jdoe1 password above
344 5. Reset the users password ‘userpassword’
345 6. Check that ‘pwdTPRExpTime’ and ‘pwdTPRUseCount’ are updated
346 :expectedresults:
347 1. Success
348 2. Success
349 3. Success
350 4. Success
351 5. Success
352 6. Success
353
354 """
355
356 user1 = UserAccount(topo.standalone, f'uid=jdoe1,ou=People,{DEFAULT_SUFFIX}')
357 log.info('Logging current time')
358 start_time = time.mktime(time.gmtime())
359 log.info('Verifying the Global policy are set and attributes are all set to "None"')
360 for tpr_attrib in ['pwdTPRReset', 'pwdTPRExpTime', 'pwdTPRUseCount']:
361 assert user1.get_attr_val_utf8(tpr_attrib) is None
362 config = Config(topo.standalone)
363 config.replace_many(('pwdmustchange', 'on'),
364 ('passwordTPRMaxUse', '3'),
365 ('passwordTPRDelayExpireAt', '1800'),
366 ('passwordTPRDelayValidFrom', '1'))
367 assert user1.get_attr_val_utf8('pwdTPRExpTime') is None
368 log.info('Triggering TPR as Admin')
369 user1.replace('userpassword', 'new_password')
370 time.sleep(1)
371 log.info('Checking that pwdTPRReset, pwdTPRExpTime, pwdTPRUseCount are reset.')
372 assert user1.get_attr_val_utf8('pwdTPRReset') == 'TRUE'
373 assert user1.get_attr_val_utf8('pwdTPRExpTime') is None
374 assert user1.get_attr_val_utf8('pwdTPRUseCount') is '0'
375
376
377 def test_user_resets_pwd_TPR_attrs_reset(topo, _add_user, set_global_TPR_policies):
378 """Test once password is reset attributes are set to FALSE
379
380 :id: 6614068a-ee7d-11eb-b1a3-98fa9ba19b65
381 :customerscenario: True
382 :setup: Standalone
383 :steps:
384 1. Create DS Instance
385 2. Create user jdoe2 with appropriate password
386 3. Configure the Global Password policies and set passwordMustChange on
387 4. Trigger TPR by resetting the user jdoe1 password above
388 5. Reset the users password ‘userpassword’
389 6. Check that pwdTPRReset, pwdTPRUseCount, pwdTPRValidFrom, pwdTPRExpireAt are RESET
390 :expectedresults:
391 1. Success
392 2. Success
393 3. Success
394 4. Success
395 5. Success
396 6. Success
397
398 """
399 user1 = UserAccount(topo.standalone, f'uid=jdoe1,ou=People,{DEFAULT_SUFFIX}')
400 log.info('Logging current time')
401 start_time = time.mktime(time.gmtime())
402 log.info('Verifying the Global policy are set and attributes are all set to "None"')
403 for tpr_attrib in ['pwdTPRReset', 'pwdTPRUseCount', 'pwdTPRValidFrom', 'pwdTPRExpireAt']:
404 assert user1.get_attr_val_utf8(tpr_attrib) is None
405 config = Config(topo.standalone)
406 config.replace_many(('pwdmustchange', 'on'),
407 ('passwordTPRMaxUse', '3'),
408 ('passwordTPRDelayExpireAt', '1800'),
409 ('passwordTPRDelayValidFrom', '1'))
410 assert user1.get_attr_val_utf8('pwdTPRReset') is None
411 log.info('Triggering TPR check that pwdTPRReset, pwdTPRUseCount, pwdTPRValidFrom, pwdTPRExpireAt are set')
412 user1.replace('userpassword', 'new_password')
413 time.sleep(3)
414 assert user1.get_attr_val_utf8('pwdTPRReset') == 'TRUE'
415 assert user1.get_attr_val_utf8('pwdTPRUseCount') == '0'
416 assert gentime_to_posix_time(user1.get_attr_val_utf8('pwdTPRValidFrom')) > start_time
417 assert gentime_to_posix_time(user1.get_attr_val_utf8('pwdTPRExpireAt')) > start_time
418 conn = user1.rebind('new_password')
419 user1.replace('userpassword', 'extra_new_pass')
420 log.info('Checking that pwdTPRReset, pwdTPRUseCount, pwdTPRValidFrom, pwdTPRExpireAt are reset to None')
421 time.sleep(3)
422 assert user1.get_attr_val_utf8('pwdTPRReset') is None
423 assert user1.get_attr_val_utf8('pwdTPRUseCount') is None
424 assert (user1.get_attr_val_utf8('pwdTPRValidFrom')) is None
425 assert (user1.get_attr_val_utf8('pwdTPRExpireAt')) is None
426 log.info('Verified that attributes are reset after password is reset')
427
428
429 def test_TPR_replication_entry(topo_m2c2):
430 """Test once password is reset attributes are set to FALSE
431
432 :id: f8b98042-ff07-11eb-b938-98fa9ba19b65
433 :customerscenario: True
434 :setup: Replicated 2 Suppliers 2 Consumers
435 :steps:
436 1. Create Replicated Topology with 2 suppliers and 2 consumers
437 2. Create users on each replica
438 3. Verify that 'pwdTPRReset', 'pwdTPRUseCount', 'pwdTPRValidFrom', 'pwdTPRExpireAt' are set to None
439 4. Configure the Global Password policies and set passwordMustChange on supplier1
440 5. Trigger TPR by resetting the users password above
441 6. Reset the users password ‘userpassword’
442 7. Check that pwdTPRReset, pwdTPRUseCount, pwdTPRValidFrom, pwdTPRExpireAt are updated on every replica
443 :expectedresults:
444 1. Success
445 2. Success
446 3. Success
447 4. Success
448 5. Success
449 6. Success
450 7. Success
451
452 """
453 repl_list = ['supplier1', 'supplier2', 'consumer1', 'consumer2']
454 users_list = ['user_supplier1', 'user_supplier2', 'user_consumer1', 'user_consumer2']
455 uid = 'jdoe_repl'
456 cn = 'John Doe1 Repl'
457 sn = 'jdoe1_repl'
458 givenname = 'Johnny Replica'
459 userpassword = 'replica_pass'
460 gid = '10001'
461 ou = 'ou=People'
462 user_obj_list = []
463
464 for repl in repl_list:
465 for user in users_list:
466 obj_user = UserAccounts(topo_m2c2.ms[repl], DEFAULT_SUFFIX, rdn=ou).create(properties={
467 'uid': f'{uid}{user}',
468 'cn': cn,
469 'sn': sn,
470 'ou': ou,
471 'givenname': givenname,
472 'mail': f'{repl}{user}@example.com',
473 'homeDirectory': f'/home/{uid}{user}',
474 'uidNumber': '1000',
475 'gidNumber': gid,
476 'userpassword': userpassword,
477 })
478 user_obj_list.append(obj_user)
479 log.info('Creating user {} with UID: {} for {}'.format(givenname, uid, repl))
480 break
481 log.info("Created the following objects {}".format(user_obj_list))
482
483 start_time = time.mktime(time.gmtime())
484 log.info('Verifying the Global policy are set and attributes are all set to "None"')
485 tpr_attrib_list = ['pwdTPRReset', 'pwdTPRUseCount', 'pwdTPRValidFrom', 'pwdTPRExpireAt']
486 for tpr_attrib in tpr_attrib_list:
487 assert user_obj_list[0].get_attr_val_utf8(tpr_attrib) is None
488 assert user_obj_list[1].get_attr_val_utf8(tpr_attrib) is None
489 assert user_obj_list[2].get_attr_val_utf8(tpr_attrib) is None
490 assert user_obj_list[3].get_attr_val_utf8(tpr_attrib) is None
491
492 topo_m2c2.ms["supplier1"].config.replace_many(('passwordMustChange', 'on'),
493 ('passwordTPRMaxUse', '3'),
494 ('passwordTPRDelayExpireAt', '600'),
495 ('passwordTPRDelayValidFrom', '1'))
496 for user in user_obj_list:
497 user.replace('userpassword', 'changed_pass')
498 log.info('Triggering TPR by resetting password for entry {}'.format(user))
499 time.sleep(3)
500 log.info("Checking that Global passwordTPRMaxUse is in effect.")
501 count = 0
502 while count < 3:
503 if count == 3:
504 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
505 user.bind('password_fails')
506 else:
507 with pytest.raises(ldap.INVALID_CREDENTIALS):
508 count += 1
509 user.bind('password_fails')
510
511 for user in user_obj_list:
512 assert user.get_attr_val_utf8('pwdTPRReset') == 'TRUE'
513 assert user.get_attr_val_utf8('pwdTPRUseCount') == '3'
514 assert gentime_to_posix_time(user.get_attr_val_utf8('pwdTPRValidFrom')) > start_time
515 assert gentime_to_posix_time(user.get_attr_val_utf8('pwdTPRExpireAt')) > start_time
516 log.info('Checking TPR attributes are replicated for {}.'.format(user))
517
518
519 if __name__ == "__main__":
520 CURRENT_FILE = os.path.realpath(__file__)
521 pytest.main("-s -v %s" % CURRENT_FILE)