Debian Codegraph @

dirsrvtests/tests/suites/password/password_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 logging
10
11 import pytest
12 from lib389.tasks import *
13 from test389.topologies import topology_st
14 from lib389._constants import PASSWORD, DEFAULT_SUFFIX
15
16 from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES
17
18 pytestmark = pytest.mark.tier1
19
20 logging.getLogger(__name__).setLevel(logging.DEBUG)
21 log = logging.getLogger(__name__)
22
23
24 def test_password_delete_specific_password(topology_st):
25 """Delete a specific userPassword, and make sure
26 it is actually deleted from the entry
27
28 :id: 800f432a-52ab-4661-ac66-a2bdd9b984d6
29 :setup: Standalone instance
30 :steps:
31 1. Add a user with userPassword attribute in cleartext
32 2. Delete the added value of userPassword attribute
33 3. Check if the userPassword attribute is deleted
34 4. Delete the user
35 :expectedresults:
36 1. The user with userPassword in cleartext should be added successfully
37 2. Operation should be successful
38 3. UserPassword should be deleted
39 4. The user should be successfully deleted
40 """
41
42 log.info('Running test_password_delete_specific_password...')
43
44 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
45
46 user = users.create(properties=TEST_USER_PROPERTIES)
47
48 #
49 # Add a test user with a password
50 #
51 user.set('userpassword', PASSWORD)
52
53 #
54 # Delete the exact password
55 #
56 user.remove('userpassword', PASSWORD)
57
58 #
59 # Check the password is actually deleted
60 #
61 assert not user.present('userPassword')
62
63 log.info('test_password_delete_specific_password: PASSED')
64
65 @pytest.fixture(scope="function")
66 def pbkdf2_sha512_scheme(request, topology_st):
67 """Set default password storage scheme to PBKDF2-SHA512"""
68
69 inst = topology_st.standalone
70 default_scheme = inst.config.get_attr_val_utf8('passwordStorageScheme')
71 inst.config.set('passwordStorageScheme', 'PBKDF2-SHA512')
72
73 def fin():
74 inst.config.set('passwordStorageScheme', default_scheme)
75
76 request.addfinalizer(fin)
77
78 @pytest.fixture(scope="function")
79 def crypt_scheme(request, topology_st):
80 """Set default password storage scheme to CRYPT"""
81
82 inst = topology_st.standalone
83 default_scheme = inst.config.get_attr_val_utf8('passwordStorageScheme')
84 inst.config.set('passwordStorageScheme', 'CRYPT')
85
86 def fin():
87 inst.config.set('passwordStorageScheme', default_scheme)
88
89 request.addfinalizer(fin)
90
91
92 def test_password_modify_non_utf8(topology_st, pbkdf2_sha512_scheme):
93 """Attempt a modify of the userPassword attribute with
94 an invalid non utf8 value
95
96 :id: a31af9d5-d665-42b9-8d6e-fea3d0837d36
97 :setup: Standalone instance
98 :steps:
99 1. Add a user if it doesnt exist and set its password
100 2. Verify password with a bind
101 3. Modify userPassword attr with invalid value
102 4. Attempt a bind with invalid password value
103 5. Verify original password with a bind
104 :expectedresults:
105 1. The user with userPassword should be added successfully
106 2. Operation should be successful
107 3. Server returns ldap.UNWILLING_TO_PERFORM
108 4. Server returns ldap.INVALID_CREDENTIALS
109 5. Operation should be successful
110 """
111
112 log.info('Running test_password_modify_non_utf8...')
113
114 # Create user and set password
115 standalone = topology_st.standalone
116 users = UserAccounts(standalone, DEFAULT_SUFFIX)
117 if not users.exists(TEST_USER_PROPERTIES['uid'][0]):
118 user = users.create(properties=TEST_USER_PROPERTIES)
119 else:
120 user = users.get(TEST_USER_PROPERTIES['uid'][0])
121 user.set('userpassword', PASSWORD)
122
123 # Verify password
124 try:
125 user.bind(PASSWORD)
126 except ldap.LDAPError as e:
127 log.fatal('Failed to bind as {}, error: '.format(user.dn) + e.args[0]['desc'])
128 assert False
129
130 # Modify userPassword with an invalid value
131 password = b'tes\x82t-password' # A non UTF-8 encoded password
132 with pytest.raises(ldap.UNWILLING_TO_PERFORM):
133 user.replace('userpassword', password)
134
135 # Verify a bind fails with invalid pasword
136 with pytest.raises(ldap.INVALID_CREDENTIALS):
137 user.bind(password)
138
139 # Verify we can still bind with original password
140 try:
141 user.bind(PASSWORD)
142 except ldap.LDAPError as e:
143 log.fatal('Failed to bind as {}, error: '.format(user.dn) + e.args[0]['desc'])
144 assert False
145
146 log.info('test_password_modify_non_utf8: PASSED')
147
148
149 @pytest.mark.parametrize('no_upgrade_hash, expected_hash',
150 [('CRYPT,CLEAR', '{crypt}'),
151 ('CrYpT,CLEAR', '{crypt}'),
152 ('PBKDF2-SHA512', '{PBKDF2-SHA512}'),
153 ('clear', '{PBKDF2-SHA512}')])
154 def test_pwd_scheme_no_upgrade_on_bind(topology_st, crypt_scheme, request, no_upgrade_hash, expected_hash):
155 """Check that password is/is_not updated on bind weither
156 the current hash is in nsslapd-scheme-list-no-upgrade-hash
157
158 :id: b4d2c525-a239-4ca6-a168-5126da7abedd
159 :parametrized: yes
160 :setup: Standalone instance
161 :steps:
162 1. Create a user with userpassword stored as CRYPT
163 2. Make sure configured stored hash is set to PBKDF2-SHA512
164 3. Make sure that the configuration skips update on bind for
165 CRYPT,CLEAR original hashes and set it to no_upgrade_hash value
166 4. Bind and check the hash password starts with expected_hash
167 :expectedresults:
168 1. The user with userPassword should be added successfully
169 2. Operation should be successful
170 3. Operation should be successful
171 4. After bind, the password has the expected hash
172 """
173
174 log.info('test_pwd_scheme_crypt_upgraded_on_bind...')
175
176 # Create user and set password with CRYPT
177 standalone = topology_st.standalone
178 users = UserAccounts(standalone, DEFAULT_SUFFIX)
179 if not users.exists(TEST_USER_PROPERTIES['uid'][0]):
180 user = users.create(properties=TEST_USER_PROPERTIES)
181 else:
182 user = users.get(TEST_USER_PROPERTIES['uid'][0])
183
184 # Check that the password is stored as CRYPT
185 user.set('userpassword', PASSWORD)
186 value_crypt = user.get_attr_val_utf8('userpassword')
187 assert value_crypt.startswith('{crypt}')
188
189 # change the default password Scheme to PBKDF2-SHA512
190 standalone.config.set('passwordStorageScheme', 'PBKDF2-SHA512')
191
192 # Check that default no upgrade hash is CRYPT,CLEAR
193 # and set it to no_upgrade_hash parameter value
194 original_no_upgrade_hash = standalone.config.get_attr_val_utf8('nsslapd-scheme-list-no-upgrade-hash')
195 assert original_no_upgrade_hash.lower() == "CRYPT,CLEAR".lower()
196 standalone.config.set('nsslapd-scheme-list-no-upgrade-hash', no_upgrade_hash)
197 new_no_upgrade_hash = standalone.config.get_attr_val_utf8('nsslapd-scheme-list-no-upgrade-hash')
198 assert new_no_upgrade_hash.lower() == no_upgrade_hash.lower()
199
200 # Authenticate user and check its password
201 # hash starts with expected_hash
202 user.bind(PASSWORD)
203 standalone.simple_bind_s(DN_DM, PASSWORD)
204 pwd_hashed_value = user.get_attr_val_utf8('userpassword')
205 assert pwd_hashed_value.startswith(expected_hash)
206
207 log.info('test_pwd_scheme_upgrade_on_bind PASSED')
208
209 def fin():
210 user.delete()
211 standalone.config.set('passwordStorageScheme', 'PBKDF2-SHA512')
212 standalone.config.set('nsslapd-scheme-list-no-upgrade-hash', 'CRYPT,CLEAR')
213
214 request.addfinalizer(fin)
215
216 if __name__ == '__main__':
217 # Run isolated
218 # -s for DEBUG mode
219 CURRENT_FILE = os.path.realpath(__file__)
220 pytest.main("-s %s" % CURRENT_FILE)