Debian Codegraph @

dirsrvtests/tests/suites/password/pwdAdmin_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2016 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 os
10 import pytest
11 import time
12 from lib389.tasks import *
13 from lib389.utils import *
14 from test389.topologies import topology_st
15
16 from lib389.idm.user import UserAccounts
17 from lib389.idm.group import Groups
18 from lib389.idm.domain import Domain
19 from lib389._constants import SUFFIX, DEFAULT_SUFFIX
20
21 pytestmark = pytest.mark.tier1
22
23 logging.getLogger(__name__).setLevel(logging.DEBUG)
24 log = logging.getLogger(__name__)
25
26 CONFIG_DN = 'cn=config'
27 ADMIN_NAME = 'passwd_admin'
28 ADMIN_DN = 'cn=%s,%s' % (ADMIN_NAME, SUFFIX)
29 ADMIN2_NAME = 'passwd_admin2'
30 ADMIN2_DN = 'cn=%s,%s' % (ADMIN2_NAME, SUFFIX)
31 ADMIN_PWD = 'ntaheonusheoasuhoau_9'
32 ADMIN_GROUP_DN = 'cn=password admin group,%s' % (SUFFIX)
33 ENTRY_NAME = 'Joe Schmo'
34 ENTRY_DN = 'cn=%s,%s' % (ENTRY_NAME, SUFFIX)
35 INVALID_PWDS = ('2_Short', 'No_Number', 'N0Special', '{SSHA}bBy8UdtPZwu8uZna9QOYG3Pr41RpIRVDl8wddw==')
36
37
38 @pytest.fixture(scope="module")
39 def password_policy(topology_st):
40 """Set up password policy
41 Create a Password Admin entry;
42 Set up password policy attributes in config;
43 Add an aci to give everyone full access;
44 Test that the setup works
45 """
46
47 log.info('test_pwdAdmin_init: Creating Password Administrator entries...')
48
49 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
50 groups = Groups(topology_st.standalone, DEFAULT_SUFFIX)
51
52 # Add Password Admin 1
53 admin1_user = users.create(properties={
54 'uid': 'admin1',
55 'cn' : 'admin1',
56 'sn' : 'strator',
57 'uidNumber' : '1000',
58 'gidNumber' : '2000',
59 'homeDirectory' : '/home/admin1',
60 'userPassword': ADMIN_PWD
61 })
62
63 # Add Password Admin 2
64 admin2_user = users.create(properties={
65 'uid': 'admin2',
66 'cn' : 'admin2',
67 'sn' : 'strator',
68 'uidNumber' : '1000',
69 'gidNumber' : '2000',
70 'homeDirectory' : '/home/admin2',
71 'userPassword': ADMIN_PWD
72 })
73
74 # Add Password Admin Group
75 admin_group = groups.create(properties={
76 'cn': 'password admin group'
77 })
78
79 admin_group.add_member(admin1_user.dn)
80 admin_group.add_member(admin2_user.dn)
81
82 # Configure password policy
83 log.info('test_pwdAdmin_init: Configuring password policy...')
84
85 topology_st.standalone.config.replace_many(
86 ('nsslapd-pwpolicy-local', 'on'),
87 ('passwordCheckSyntax', 'on'),
88 ('passwordMinCategories', '1'),
89 ('passwordMinTokenLength', '2'),
90 ('passwordExp', 'on'),
91 ('passwordMinDigits', '1'),
92 ('passwordMinSpecials', '1'),
93 ('passwordHistory', 'on'),
94 ('passwordStorageScheme', 'clear'),
95 ('nsslapd-enable-upgrade-hash', 'off')
96 )
97
98 #
99 # Add an aci to allow everyone all access (just makes things easier)
100 #
101 log.info('Add aci to allow password admin to add/update entries...')
102
103 domain = Domain(topology_st.standalone, DEFAULT_SUFFIX)
104
105 ACI_TARGET = "(target = \"ldap:///%s\")" % SUFFIX
106 ACI_TARGETATTR = "(targetattr = *)"
107 ACI_ALLOW = "(version 3.0; acl \"Password Admin Access\"; allow (all) "
108 ACI_SUBJECT = "(userdn = \"ldap:///anyone\");)"
109 ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_ALLOW + ACI_SUBJECT
110
111 domain.add('aci', ACI_BODY)
112
113 #
114 # Bind as the future Password Admin
115 #
116 log.info('test_pwdAdmin_init: Bind as the Password Administrator (before activating)...')
117 admin_conn = admin1_user.bind(ADMIN_PWD)
118
119 #
120 # Setup our test entry, and test password policy is working
121 #
122
123 # Connect up an admin authed users connection.
124 admin_users = UserAccounts(admin_conn, DEFAULT_SUFFIX)
125
126 #
127 # Start by attempting to add an entry with an invalid password
128 #
129 log.info('test_pwdAdmin_init: Attempt to add entries with invalid passwords, these adds should fail...')
130 for passwd in INVALID_PWDS:
131 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
132 admin_users.create(properties={
133 'uid': 'example',
134 'cn' : 'example',
135 'sn' : 'example',
136 'uidNumber' : '1000',
137 'gidNumber' : '2000',
138 'homeDirectory' : '/home/example',
139 'userPassword': passwd
140 })
141
142 return (admin_group, admin1_user, admin2_user)
143
144
145 def test_pwdAdmin_bypass(topology_st, password_policy):
146 """Test that password administrators/root DN can
147 bypass password syntax/policy
148
149 :id: 743bfe33-a1f7-482b-8807-efeb7aa57348
150 :setup: Standalone instance, Password Admin entry,
151 Password policy configured as below:
152 nsslapd-pwpolicy-local: on
153 passwordCheckSyntax: on
154 passwordMinCategories: 1
155 passwordMinTokenLength: 2
156 passwordExp: on
157 passwordMinDigits: 1
158 passwordMinSpecials: 1
159 :steps:
160 1: Add users with invalid passwords
161 :expectedresults:
162 1: Users should be added successfully.
163 """
164
165 #
166 # Now activate a password administator, bind as root dn to do the config
167 # update, then rebind as the password admin
168 #
169 log.info('test_pwdAdmin: Activate the Password Administator...')
170
171 # Extract our fixture data.
172
173 (admin_group, admin1_user, admin2_user) = password_policy
174
175 # Set the password admin
176
177 topology_st.standalone.config.set('passwordAdminDN', admin1_user.dn)
178
179 #
180 # Get our test entry
181 #
182
183 admin_conn = admin1_user.bind(ADMIN_PWD)
184 admin_users = UserAccounts(admin_conn, DEFAULT_SUFFIX)
185
186 #
187 # Start adding entries with invalid passwords, delete the entry after each pass.
188 #
189 for passwd in INVALID_PWDS:
190 u1 = admin_users.create(properties={
191 'uid': 'example',
192 'cn' : 'example',
193 'sn' : 'example',
194 'uidNumber' : '1000',
195 'gidNumber' : '2000',
196 'homeDirectory' : '/home/example',
197 'userPassword': passwd
198 })
199 u1.delete()
200
201
202 def test_pwdAdmin_no_admin(topology_st, password_policy):
203 """Test that password administrators/root DN can
204 bypass password syntax/policy
205
206 :id: 74347798-7cc7-4ce7-ad5c-06387ffde02c
207 :setup: Standalone instance, Password Admin entry,
208 Password policy configured as below:
209 nsslapd-pwpolicy-local: on
210 passwordCheckSyntax: on
211 passwordMinCategories: 1
212 passwordMinTokenLength: 2
213 passwordExp: on
214 passwordMinDigits: 1
215 passwordMinSpecials: 1
216 :steps:
217 1: Create a user
218 2: Attempt to set passwords on the user that are invalid
219 :expectedresults:
220 1: Success
221 2: The passwords should NOT be set
222 """
223 (admin_group, admin1_user, admin2_user) = password_policy
224
225 # Remove password admin
226
227 # Can't use pytest.raises. because this may or may not exist
228 try:
229 topology_st.standalone.config.remove_all('passwordAdminDN')
230 except ldap.NO_SUCH_ATTRIBUTE:
231 pass
232
233 #
234 # Add the entry for the next round of testing (modify password)
235 #
236 admin_conn = admin1_user.bind(ADMIN_PWD)
237 admin_users = UserAccounts(admin_conn, DEFAULT_SUFFIX)
238
239 u2 = admin_users.create(properties={
240 'uid': 'example',
241 'cn' : 'example',
242 'sn' : 'example',
243 'uidNumber' : '1000',
244 'gidNumber' : '2000',
245 'homeDirectory' : '/home/example',
246 'userPassword': ADMIN_PWD
247 })
248
249 #
250 # Make invalid password updates that should fail
251 #
252 for passwd in INVALID_PWDS:
253 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
254 u2.replace('userPassword', passwd)
255
256
257 def test_pwdAdmin_modify(topology_st, password_policy):
258 """Test that password administrators/root DN can modify
259 passwords rather than adding them.
260
261 :id: 85326527-8eeb-401f-9d1b-4ef55dee45a4
262 :setup: Standalone instance, Password Admin entry,
263 Password policy configured as below:
264 nsslapd-pwpolicy-local: on
265 passwordCheckSyntax: on
266 passwordMinCategories: 1
267 passwordMinTokenLength: 2
268 passwordExp: on
269 passwordMinDigits: 1
270 passwordMinSpecials: 1
271 :steps:
272 1: Retrieve the user
273 2: Replace the password with invalid content
274 :expectedresults:
275 1: Success
276 2: The password should be set
277 """
278 (admin_group, admin1_user, admin2_user) = password_policy
279
280 # Update config - set the password admin
281 topology_st.standalone.config.set('passwordAdminDN', admin1_user.dn)
282
283 admin_conn = admin1_user.bind(ADMIN_PWD)
284 admin_users = UserAccounts(admin_conn, DEFAULT_SUFFIX)
285
286 u3 = admin_users.get('example')
287 #
288 # Make the same password updates, but this time they should succeed
289 #
290 for passwd in INVALID_PWDS:
291 u3.replace('userPassword', passwd)
292
293
294 def test_pwdAdmin_group(topology_st, password_policy):
295 """Test that password admin group can bypass policy.
296
297 :id: 4d62ae34-0f25-486e-b823-afd2b431e9b0
298 :setup: Standalone instance, Password Admin entry,
299 Password policy configured as below:
300 nsslapd-pwpolicy-local: on
301 passwordCheckSyntax: on
302 passwordMinCategories: 1
303 passwordMinTokenLength: 2
304 passwordExp: on
305 passwordMinDigits: 1
306 passwordMinSpecials: 1
307 :steps:
308 1: Add group to passwordadmin dn
309 2: Attempt to set invalid passwords.
310 :expectedresults:
311 1: Success.
312 2: Password should be set.
313 """
314 (admin_group, admin1_user, admin2_user) = password_policy
315
316 # Update config - set the password admin group
317 topology_st.standalone.config.set('passwordAdminDN', admin_group.dn)
318
319 # Bind as admin2, who is in the group.
320
321 admin2_conn = admin2_user.bind(ADMIN_PWD)
322 admin2_users = UserAccounts(admin2_conn, DEFAULT_SUFFIX)
323
324 u4 = admin2_users.get('example')
325
326 # Make some invalid password updates, but they should succeed
327 for passwd in INVALID_PWDS:
328 u4.replace('userPassword', passwd)
329
330
331 def test_pwdAdmin_config_validation(topology_st, password_policy):
332 """Check passwordAdminDN for valid and invalid values
333
334 :id: f7049482-41e8-438b-ae18-cdd2612c783a
335 :setup: Standalone instance, Password Admin entry,
336 Password policy configured as below:
337 nsslapd-pwpolicy-local: on
338 passwordCheckSyntax: on
339 passwordMinCategories: 1
340 passwordMinTokenLength: 1
341 passwordExp: on
342 passwordMinDigits: 1
343 passwordMinSpecials: 1
344 :steps:
345 1. Add multiple attributes - one already exists so just try and add the second one
346 2. Set passwordAdminDN attribute to an invalid value (ZZZZZ)
347 :expectedresults:
348 1. The operation should fail
349 2. The operation should fail
350 """
351
352 (admin_group, admin1_user, admin2_user) = password_policy
353 # Add multiple attributes - one already exists so just try and add the second one
354 topology_st.standalone.config.set('passwordAdminDN', admin_group.dn)
355 with pytest.raises(ldap.OBJECT_CLASS_VIOLATION):
356 topology_st.standalone.config.add('passwordAdminDN', admin1_user.dn)
357
358 # Attempt to set invalid DN
359 with pytest.raises(ldap.INVALID_SYNTAX):
360 topology_st.standalone.config.set('passwordAdminDN', 'zzzzzzzzzzzz')
361
362
363 def test_pwd_admin_config_test_skip_updates(topology_st, password_policy):
364 """Check passwordAdminDN does not update entry password state attributes
365
366 :id: 964f1430-795b-4f4d-85b2-abaffe66ddcb
367
368 :setup: Standalone instance
369 :steps:
370 1. Add test entry
371 2. Update password
372 3. Password history updated
373 4. Enable "skip info update"
374 5. Update password again
375 6. New password not in history
376 :expectedresults:
377 1. Success
378 2. Success
379 3. Success
380 4. Success
381 5. Success
382 6. Success
383 """
384
385 inst = topology_st.standalone
386 passwd_in_history = "Secret123"
387 password_not_in_history = "ShouldNotBeInHistory"
388 (admin_group, admin1_user, admin2_user) = password_policy
389
390 # Update config
391 inst.config.set('passwordAdminDN', admin_group.dn)
392
393 # Add test entry
394 admin_conn = admin1_user.bind(ADMIN_PWD)
395 admin_users = UserAccounts(admin_conn, DEFAULT_SUFFIX)
396 admin_users.create(properties={
397 'uid': 'skipInfoUpdate',
398 'cn': 'skipInfoUpdate',
399 'sn': 'skipInfoUpdate',
400 'uidNumber': '1001',
401 'gidNumber': '2002',
402 'homeDirectory': '/home/skipInfoUpdate',
403 'userPassword': "abdcefghijk"
404 })
405
406 # Update password to populate history
407 user = admin_users.get('skipInfoUpdate')
408 user.replace('userPassword', passwd_in_history)
409 user.replace('userPassword', passwd_in_history)
410 time.sleep(1)
411
412 # Check password history was updated
413 passwords = user.get_attr_vals_utf8('passwordHistory')
414 log.debug(f"passwords in history for {user.dn}: {str(passwords)}")
415 found = False
416 for passwd in passwords:
417 if passwd_in_history in passwd:
418 found = True
419 assert found
420
421 # Disable password state info updates
422 inst.config.set('passwordAdminSkipInfoUpdate', 'on')
423 time.sleep(1)
424
425 # Update password
426 user.replace('userPassword', password_not_in_history)
427 user.replace('userPassword', password_not_in_history)
428 time.sleep(1)
429
430 # Check it is not in password history
431 passwords = user.get_attr_vals_utf8('passwordHistory')
432 log.debug(f"Part 2: passwords in history for {user.dn}: {str(passwords)}")
433 found = False
434 for passwd in passwords:
435 if password_not_in_history in passwd:
436 found = True
437 assert not found
438
439
440 def test_pwd_admin_extended_op(topology_st, password_policy):
441 """Test RootDN can bypass passwsord policy via extended op password modify
442
443 :id: 687fec1d-1081-4012-a2c4-41b427b9e764
444
445 :setup: Standalone instance
446 :steps:
447 1. Add test entry
448 2. Run passwd_s (extended op) twice as root DN
449
450 :expectedresults:
451 1. Success
452 2. Success
453 """
454
455 inst = topology_st.standalone
456 inst.enable_tls()
457
458 users = UserAccounts(inst, DEFAULT_SUFFIX)
459 user = users.create(properties={
460 'uid': 'extop',
461 'cn': 'extop',
462 'sn': 'extop',
463 'uidNumber': '1000',
464 'gidNumber': '2000',
465 'homeDirectory': '/home/extop',
466 'userPassword': "password"
467 })
468 user.replace('userpassword', 'password2')
469
470 inst.passwd_s(user.dn, 'password2', 'password')
471 inst.passwd_s(user.dn, 'password', 'password2')
472
473 user.delete()
474
475
476 if __name__ == '__main__':
477 # Run isolated
478 # -s for DEBUG mode
479 CURRENT_FILE = os.path.realpath(__file__)
480 pytest.main("-s %s" % CURRENT_FILE)