Debian Codegraph @

dirsrvtests/tests/suites/password/pwdPolicy_warning_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2020 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 pytest
10 import subprocess
11 from ldap.controls.ppolicy import PasswordPolicyControl
12 from lib389.tasks import *
13 from lib389.utils import *
14 from test389.topologies import topology_st
15 from lib389.idm.user import UserAccounts
16 from lib389.idm.organizationalunit import OrganizationalUnits
17 from lib389._constants import (DEFAULT_SUFFIX, DN_CONFIG, PASSWORD, DN_DM)
18 from lib389.config import Config
19 import datetime as dt
20
21 pytestmark = pytest.mark.tier1
22
23 CONFIG_ATTR = 'passwordSendExpiringTime'
24 USER_DN = 'uid=tuser,ou=people,{}'.format(DEFAULT_SUFFIX)
25 USER_RDN = 'tuser'
26 USER_PASSWD = 'secret123'
27 USER_ACI = '(targetattr="userpassword")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)'
28
29 logging.getLogger(__name__).setLevel(logging.INFO)
30 log = logging.getLogger(__name__)
31
32
33 @pytest.fixture
34 def global_policy(topology_st, request):
35 """Sets the required global
36 password policy attributes under
37 cn=config entry
38 """
39
40 attrs = {'passwordExp': '',
41 'passwordMaxAge': '',
42 'passwordWarning': '',
43 CONFIG_ATTR: ''}
44
45 log.info('Get the default values')
46 entry = topology_st.standalone.getEntry(DN_CONFIG, ldap.SCOPE_BASE,
47 '(objectClass=*)', attrs.keys())
48
49 for key in attrs.keys():
50 attrs[key] = entry.getValue(key)
51
52 log.info('Set the new values')
53 topology_st.standalone.config.replace_many(('passwordExp', 'on'),
54 ('passwordMaxAge', '172800'),
55 ('passwordWarning', '86400'),
56 (CONFIG_ATTR, 'on'))
57
58 def fin():
59 """Resets the defaults"""
60
61 log.info('Reset the defaults')
62 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
63 for key in attrs.keys():
64 topology_st.standalone.config.replace(key, attrs[key])
65
66 request.addfinalizer(fin)
67 # A short sleep is required after the modifying password policy or cn=config
68 time.sleep(0.5)
69
70
71 @pytest.fixture
72 def global_policy_default(topology_st, request):
73 """Sets the required global password policy
74 attributes for testing the default behavior
75 of password expiry warning time
76 """
77
78 attrs = {'passwordExp': '',
79 'passwordMaxAge': '',
80 'passwordWarning': '',
81 CONFIG_ATTR: ''}
82
83 log.info('Get the default values')
84 entry = topology_st.standalone.getEntry(DN_CONFIG, ldap.SCOPE_BASE,
85 '(objectClass=*)', attrs.keys())
86 for key in attrs.keys():
87 attrs[key] = entry.getValue(key)
88
89 log.info('Set the new values')
90 topology_st.standalone.config.replace_many(
91 ('passwordExp', 'on'),
92 ('passwordMaxAge', '8640000'),
93 ('passwordWarning', '86400'),
94 (CONFIG_ATTR, 'off'))
95
96 def fin():
97 """Resets the defaults"""
98 log.info('Reset the defaults')
99 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
100 for key in attrs.keys():
101 topology_st.standalone.config.replace(key, attrs[key])
102
103 request.addfinalizer(fin)
104 # A short sleep is required after modifying password policy or cn=config
105 time.sleep(0.5)
106
107
108 @pytest.fixture
109 def add_user(topology_st, request):
110 """Adds a user for binding"""
111
112 log.info('Add the user')
113
114 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
115 user = users.create(properties={
116 'uid': USER_RDN,
117 'cn': USER_RDN,
118 'sn': USER_RDN,
119 'uidNumber': '3000',
120 'gidNumber': '4000',
121 'homeDirectory': '/home/user',
122 'description': 'd_e_s_c',
123 'userPassword': USER_PASSWD
124 })
125
126 def fin():
127 """Removes the user entry"""
128
129 log.info('Remove the user entry')
130 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
131 user.delete()
132
133 request.addfinalizer(fin)
134
135
136 @pytest.fixture
137 def local_policy(topology_st, add_user):
138 """Sets fine grained policy for user entry"""
139
140 log.info("Setting fine grained policy for user ({})".format(USER_DN))
141
142 subprocess.call(['%s/dsconf' % topology_st.standalone.get_sbin_dir(),
143 'slapd-standalone1',
144 'localpwp',
145 'adduser',
146 USER_DN])
147 # A short sleep is required after modifying password policy
148 time.sleep(0.5)
149
150
151 def get_password_warning(topology_st):
152 """Gets the password expiry warning time for the user"""
153
154 res_type = res_data = res_msgid = res_ctrls = None
155 result_id = ''
156
157 log.info('Bind with the user and request the password expiry warning time')
158
159 result_id = topology_st.standalone.simple_bind(USER_DN, USER_PASSWD,
160 serverctrls=[PasswordPolicyControl()])
161 res_type, res_data, res_msgid, res_ctrls = \
162 topology_st.standalone.result3(result_id)
163 # Return the control
164 return res_ctrls
165
166
167 def set_conf_attr(topology_st, attr, val):
168 """Sets the value of a given attribute under cn=config"""
169
170 log.info("Setting {} to {}".format(attr, val))
171 topology_st.standalone.config.set(attr, val)
172 # A short sleep is required after modifying cn=config
173 time.sleep(0.5)
174
175
176 def get_conf_attr(topology_st, attr):
177 """Gets the value of a given attribute under cn=config entry
178 """
179 return topology_st.standalone.config.get_attr_val_utf8(attr)
180
181
182 @pytest.mark.parametrize("value", (' ', 'junk123', 'on', 'off'))
183 def test_different_values(topology_st, value):
184 """Try to set passwordSendExpiringTime attribute
185 to various values both valid and invalid
186
187 :id: 3e6d79fb-b4c8-4860-897e-5b207815a75d
188 :parametrized: yes
189 :setup: Standalone instance
190 :steps:
191 1. Try to set passwordSendExpiringTime to 'on' and 'off'
192 under cn=config entry
193 2. Try to set passwordSendExpiringTime to ' ' and 'junk123'
194 under cn=config entry
195 3. Run the search command to check the
196 value of passwordSendExpiringTime attribute
197 :expectedresults:
198 1. Valid values should be accepted and saved
199 2. Should be rejected with an OPERATIONS_ERROR
200 3. The attribute should be changed for valid values
201 and unchanged for invalid
202 """
203
204 log.info('Get the default value')
205 defval = get_conf_attr(topology_st, CONFIG_ATTR)
206
207 if value not in ('on', 'off'):
208 log.info('An invalid value is being tested')
209 with pytest.raises(ldap.OPERATIONS_ERROR):
210 set_conf_attr(topology_st, CONFIG_ATTR, value)
211
212 log.info('Now check the value is unchanged')
213 assert get_conf_attr(topology_st, CONFIG_ATTR) == defval
214
215 log.info("Invalid value {} was rejected correctly".format(value))
216 else:
217 log.info('A valid value is being tested')
218 set_conf_attr(topology_st, CONFIG_ATTR, value)
219
220 log.info('Now check that the value has been changed')
221 assert str(get_conf_attr(topology_st, CONFIG_ATTR)) == value
222
223 log.info("{} is now set to {}".format(CONFIG_ATTR, value))
224
225 log.info('Set passwordSendExpiringTime back to the default value')
226 set_conf_attr(topology_st, CONFIG_ATTR, defval)
227
228
229 def test_expiry_time(topology_st, global_policy, add_user):
230 """Test whether the password expiry warning
231 time for a user is returned appropriately
232
233 :id: 7adfd395-9b25-4cc0-9b71-14710dc1a28c
234 :setup: Standalone instance, a user entry,
235 Global password policy configured as below:
236 passwordExp: on
237 passwordMaxAge: 172800
238 passwordWarning: 86400
239 passwordSendExpiringTime: on
240 :steps:
241 1. Bind as the normal user
242 2. Request password policy control for the user
243 3. Bind as DM
244 :expectedresults:
245 1. Bind should be successful
246 2. The password expiry warning time for the user should be returned
247 3. Bind should be successful
248 """
249
250 res_ctrls = None
251
252 ous = OrganizationalUnits(topology_st.standalone, DEFAULT_SUFFIX)
253 ou = ous.get('people')
254 ou.add('aci', USER_ACI)
255
256 log.info('Get the password expiry warning time')
257 log.info("Binding with ({}) and requesting the password expiry warning time"
258 .format(USER_DN))
259 res_ctrls = get_password_warning(topology_st)
260
261 log.info('Check whether the time is returned')
262 assert res_ctrls
263
264 log.info("user's password will expire in {:d} seconds"
265 .format(res_ctrls[0].timeBeforeExpiration))
266
267 log.info("Rebinding as DM")
268 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
269
270
271 @pytest.mark.parametrize("attr,val", [(CONFIG_ATTR, 'off'),
272 ('passwordWarning', '3600')])
273 def test_password_warning(topology_st, global_policy, add_user, attr, val):
274 """Test password expiry warning time by setting passwordSendExpiringTime to off
275 and setting passwordWarning to a short value
276
277 :id: 39f54b3c-8c80-43ca-856a-174d81c56ce8
278 :parametrized: yes
279 :setup: Standalone instance, a test user,
280 Global password policy configured as below:
281 passwordExp: on
282 passwordMaxAge: 172800
283 passwordWarning: 86400
284 passwordSendExpiringTime: on
285 :steps:
286 1. Set passwordSendExpiringTime attribute to off or
287 to on and passwordWarning to a small value (3600)
288 2. Bind as the normal user
289 3. Request the password expiry warning time
290 4. Bind as DM
291 :expectedresults:
292 1. passwordSendExpiringTime and passwordWarning are set successfully
293 2. Bind should be successful
294 3. Password expiry warning time should be returned for the small value
295 and should not be returned when passwordSendExpiringTime is off
296 4. Bind should be successful
297 """
298
299 log.info('Set configuration parameter')
300 set_conf_attr(topology_st, attr, val)
301
302 log.info("Binding with ({}) and requesting password expiry warning time"
303 .format(USER_DN))
304 res_ctrls = get_password_warning(topology_st)
305
306 log.info('Check the state of the control')
307 if not res_ctrls:
308 log.info("Password Expiry warning time is not returned as {} is set to {}"
309 .format(attr, val))
310 else:
311 log.info("({}) password will expire in {:d} seconds"
312 .format(USER_DN, res_ctrls[0].timeBeforeExpiration))
313
314 log.info("Rebinding as DM")
315 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
316
317
318 def test_with_different_password_states(topology_st, global_policy, add_user):
319 """Test the control with different password states
320
321 :id: d297fb1a-661f-4d52-bb43-2a2a340b8b0e
322 :setup: Standalone instance, a user entry,
323 Global password policy configured as below:
324 passwordExp: on
325 passwordMaxAge: 172800
326 passwordWarning: 86400
327 passwordSendExpiringTime: on
328 :steps:
329 1. Expire user's password by changing
330 passwordExpirationTime timestamp
331 2. Try to bind to the server with the user entry
332 3. Revert back user's passwordExpirationTime
333 4. Try to bind with the user entry and request
334 the control
335 5. Bind as DM
336 :expectedresults:
337 1. Operation should be successful
338 2. Operation should fail because of Invalid Credentials
339 3. passwordExpirationTime is successfully changed
340 4. Bind should be successful and the password expiry
341 warning time should be returned
342 5. Bind should be successful
343 """
344
345 res_ctrls = None
346
347 log.info("Expire user's password by changing passwordExpirationTime timestamp")
348 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
349 user = users.get(USER_RDN)
350 old_ts = user.get_attr_val_utf8('passwordExpirationTime')
351 log.info("Old passwordExpirationTime: {}".format(old_ts))
352
353 new_ts = (dt.datetime.strptime(old_ts, '%Y%m%d%H%M%SZ') - dt.timedelta(31)).strftime('%Y%m%d%H%M%SZ')
354 log.info("New passwordExpirationTime: {}".format(new_ts))
355 user.replace('passwordExpirationTime', new_ts)
356
357 log.info("Attempting to bind with user {} and retrive the password expiry warning time".format(USER_DN))
358 with pytest.raises(ldap.INVALID_CREDENTIALS) as ex:
359 res_ctrls = get_password_warning(topology_st)
360
361 log.info("Bind Failed, error: {}".format(str(ex)))
362
363 log.info("Rebinding as DM")
364 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
365
366 log.info("Reverting back user's passwordExpirationTime")
367
368 user.replace('passwordExpirationTime', old_ts)
369
370 log.info("Rebinding with {} and retrieving the password expiry warning time".format(USER_DN))
371 res_ctrls = get_password_warning(topology_st)
372
373 log.info('Check that the control is returned')
374 assert res_ctrls
375
376 log.info("user's password will expire in {:d} seconds"
377 .format(res_ctrls[0].timeBeforeExpiration))
378
379 log.info("Rebinding as DM")
380 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
381
382
383 def test_default_behavior(topology_st, global_policy_default, add_user):
384 """Test the default behavior of password expiry warning time
385
386 :id: c47fa824-ee08-4b78-885f-bca4c42bb655
387 :setup: Standalone instance, a user entry,
388 Global password policy configured as below:
389 passwordExp: on
390 passwordMaxAge: 8640000
391 passwordWarning: 86400
392 passwordSendExpiringTime: off
393 :steps:
394 1. Bind as the normal user
395 2. Request the control for the user
396 3. Bind as DM
397 :expectedresults:
398 1. Bind should be successful
399 2. No control should be returned
400 3. Bind should be successful
401 """
402
403 res_ctrls = None
404
405 log.info("Binding with {} and requesting the password expiry warning time"
406 .format(USER_DN))
407 res_ctrls = get_password_warning(topology_st)
408
409 log.info('Check that no control is returned')
410 assert not res_ctrls
411
412 log.info("Rebinding as DM")
413 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
414
415
416 def test_when_maxage_and_warning_are_the_same(topology_st, global_policy_default, add_user):
417 """Test the warning expiry when passwordMaxAge and
418 passwordWarning are set to the same value.
419
420 :id: e57a1b1c-96fc-11e7-a91b-28d244694824
421 :setup: Standalone instance, a user entry,
422 Global password policy configured as below:
423 passwordExp: on
424 passwordMaxAge: 86400
425 passwordWarning: 86400
426 passwordSendExpiringTime: off
427 :steps:
428 1. Bind as the normal user
429 2. Change user's password to reset its password expiration time
430 3. Request the control for the user
431 4. Bind as DM
432 :expectedresults:
433 1. Bind should be successful
434 2. Password should be changed and password's expiration time reset
435 3. Password expiry warning time should be returned by the
436 server since passwordMaxAge and passwordWarning are set
437 to the same value
438 4. Bind should be successful
439 """
440
441 log.info('Set the new values')
442 topology_st.standalone.config.set('passwordMaxAge', '86400')
443 res_ctrls = None
444
445 log.info("First change user's password to reset its password expiration time")
446 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
447 user = users.get(USER_RDN)
448 user.rebind(USER_PASSWD)
449 user.reset_password(USER_PASSWD)
450
451 time.sleep(2)
452 log.info("Binding with {} and requesting the password expiry warning time"
453 .format(USER_DN))
454 res_ctrls = get_password_warning(topology_st)
455
456 log.info('Check that control is returned even'
457 'if passwordSendExpiringTime is set to off')
458 assert res_ctrls
459
460 log.info("user's password will expire in {:d} seconds".format(res_ctrls[0].timeBeforeExpiration))
461
462 log.info("Rebinding as DM")
463 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
464
465
466 def test_with_local_policy(topology_st, global_policy, local_policy):
467 """Test the attribute with fine grained policy set for the user
468
469 :id: ab7d9f86-8cfe-48c3-8baa-739e599f006a
470 :setup: Standalone instance, a user entry,
471 Global password policy configured as below:
472 passwordExp: on
473 passwordMaxAge: 172800
474 passwordWarning: 86400
475 passwordSendExpiringTime: on
476 Fine grained password policy for the user using: dsconf INST localpwp
477 :steps:
478 1. Bind as the normal user
479 2. Request the control for the user
480 3. Bind as DM
481 :expectedresults:
482 1. Bind should be successful
483 2. Password expiry warning time should not be returned for the user
484 3. Bind should be successful
485 """
486
487 res_ctrls = None
488
489 log.info("Attempting to get password expiry warning time for user {}".format(USER_DN))
490 res_ctrls = get_password_warning(topology_st)
491
492 log.info('Check that the control is not returned')
493 assert not res_ctrls
494
495 log.info("Password expiry warning time is not returned")
496
497 log.info("Rebinding as DM")
498 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
499
500
501 def test_search_shadowWarning_when_passwordWarning_is_lower(topology_st, global_policy):
502 """Test if value shadowWarning is present with global password policy
503 when passwordWarning is set with lower value.
504
505 :id: c1e82de6-1aa3-42c3-844a-9720172158a3
506 :setup: Standalone Instance
507 :steps:
508 1. Bind as Directory Manager
509 2. Set global password policy
510 3. Add test user to instance.
511 4. Modify passwordWarning to have smaller value than 86400
512 5. Bind as the new user
513 6. Search for shadowWarning attribute
514 7. Rebind as Directory Manager
515 :expectedresults:
516 1. Binding should be successful
517 2. Setting password policy should be successful
518 3. Adding test user should be successful
519 4. Modifying passwordWarning should be successful
520 5. Binding should be successful
521 6. Attribute shadowWarning should be found
522 7. Binding should be successful
523 """
524
525 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
526
527 log.info("Bind as %s" % DN_DM)
528 assert topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
529
530 log.info("Creating test user")
531 testuser = users.create_test_user(1004)
532 testuser.add('objectclass', 'shadowAccount')
533 testuser.set('userPassword', USER_PASSWD)
534
535 log.info("Setting passwordWarning to smaller value than 86400")
536 assert topology_st.standalone.config.set('passwordWarning', '86399')
537
538 log.info("Bind as test user")
539 assert topology_st.standalone.simple_bind_s(testuser.dn, USER_PASSWD)
540
541 log.info("Check if attribute shadowWarning is present")
542 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
543 assert testuser.present('shadowWarning')
544
545
546 def test_password_expire_works(topology_st):
547 """Regression test for bug624080. If passwordMaxAge is set to a
548 value and a new user is added, if the passwordMaxAge is changed
549 to a shorter expiration time and the new users password
550 is then changed ..... the passwordExpirationTime for the
551 new user should be changed too. There was a bug in DS 6.2
552 where the expirationtime remained unchanged.
553
554 :id: 1ead6052-4636-11ea-b5af-8c16451d917b
555 :setup: Standalone
556 :steps:
557 1. Set the Global password policy and a passwordMaxAge to 5 days
558 2. Add the new user
559 3. Check the users password expiration time now
560 4. Decrease global passwordMaxAge to 2 days
561 5. Modify the users password
562 6. Modify the user one more time to make sur etime has been reset
563 7. turn off the password policy
564 :expectedresults:
565 1. Success
566 2. Success
567 3. Success
568 4. Success
569 5. Success
570 6. Success
571 7. Success
572 """
573 config = Config(topology_st.standalone)
574 config.replace_many(('passwordMaxAge', '432000'),
575 ('passwordExp', 'on'))
576 user = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX, rdn=None).create_test_user()
577 user.set('userPassword', 'anuj')
578 time.sleep(0.5)
579 expire_time = user.get_attr_val_utf8('passwordExpirationTime')
580 config.replace('passwordMaxAge', '172800')
581 user.set('userPassword', 'borah')
582 time.sleep(0.5)
583 expire_time2 = user.get_attr_val_utf8('passwordExpirationTime')
584 config.replace('passwordMaxAge', '604800')
585 user.set('userPassword', 'anujagaiin')
586 time.sleep(0.5)
587 expire_time3 = user.get_attr_val_utf8('passwordExpirationTime')
588 assert expire_time != expire_time2 != expire_time3
589 config.replace('passwordExp', 'off')
590
591
592 if __name__ == '__main__':
593 # Run isolated
594 # -s for DEBUG mode
595 CURRENT_FILE = os.path.realpath(__file__)
596 pytest.main("-s %s" % CURRENT_FILE)