Debian Codegraph @

dirsrvtests/tests/suites/password/pwd_upgrade_on_bind_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2019 William Brown <[email protected]>
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 ldap
10 import pytest
11 from lib389.utils import *
12 from test389.topologies import topology_st
13 from lib389.idm.user import UserAccounts
14 from lib389._constants import (DEFAULT_SUFFIX, DN_CONFIG, PASSWORD, DN_DM)
15
16 pytestmark = pytest.mark.tier1
17
18 CONFIG_ATTR = 'passwordSendExpiringTime'
19 USER_DN = 'uid=tuser,ou=people,{}'.format(DEFAULT_SUFFIX)
20 USER_RDN = 'tuser'
21 USER_PASSWD = 'secret123'
22 USER_ACI = '(targetattr="userpassword")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)'
23
24 @pytest.fixture
25 def add_user(topology_st, request):
26 """Adds a user for binding"""
27
28 log.info('Add the user')
29
30 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
31 user = users.create(properties={
32 'uid': USER_RDN,
33 'cn': USER_RDN,
34 'sn': USER_RDN,
35 'uidNumber': '3000',
36 'gidNumber': '4000',
37 'homeDirectory': '/home/user',
38 'description': 'd_e_s_c',
39 'userPassword': USER_PASSWD
40 })
41
42 def fin():
43 """Removes the user entry"""
44
45 log.info('Remove the user entry')
46 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
47 user.delete()
48
49 request.addfinalizer(fin)
50
51 @pytest.fixture
52 def global_policy(topology_st, request):
53 """Sets the required global
54 password policy attributes under
55 cn=config entry
56 """
57
58 attrs = {'passwordExp': '',
59 'passwordMaxAge': '',
60 'passwordWarning': '',
61 CONFIG_ATTR: ''}
62
63 log.info('Get the default values')
64 entry = topology_st.standalone.getEntry(DN_CONFIG, ldap.SCOPE_BASE,
65 '(objectClass=*)', attrs.keys())
66
67 for key in attrs.keys():
68 attrs[key] = entry.getValue(key)
69
70 log.info('Set the new values')
71 topology_st.standalone.config.replace_many(('passwordExp', 'on'),
72 ('passwordMaxAge', '172800'),
73 ('passwordWarning', '86400'),
74 (CONFIG_ATTR, 'on'))
75
76 def fin():
77 """Resets the defaults"""
78
79 log.info('Reset the defaults')
80 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
81 for key in attrs.keys():
82 topology_st.standalone.config.replace(key, attrs[key])
83
84 request.addfinalizer(fin)
85 # A short sleep is required after the modifying password policy or cn=config
86 time.sleep(0.5)
87
88 def test_password_hash_on_upgrade(topology_st, global_policy, add_user):
89 """If a legacy password hash is present, assert that on a correct bind
90 the hash is "upgraded" to the latest-and-greatest hash format on the
91 server.
92
93 Assert also that password FAILURE does not alter the password.
94 Assert that the password expiration date, history, etc is not modified
95 as password hash upgrade on bind should be invisible to the user.
96
97
98 :id: 42cf99e6-454d-46f5-8f1c-8bb699864a07
99 :setup: Single instance
100 :steps: 1. Set a password hash in SSHA256, and hash to pbkdf2 statically
101 2. Get initial passwordExpirationtime
102 3. Test a faulty bind
103 4. Assert the PW is SSHA256
104 5. Test a correct bind
105 6. Assert the PW is PBKDF2
106 7. Assert the passwordExpirationtime hasnt changed after upgrade on bind
107 :expectedresults:
108 1. Successfully set the values
109 2. Successfully get the passwordExpirationtime
110 3. The bind fails
111 4. The PW is SSHA256
112 5. The bind succeeds
113 6. The PW is PBKDF2udo
114 7. pwd expiration time hasnt been modifed
115
116 """
117 # Make sure the server is set to pkbdf
118 topology_st.standalone.config.set('passwordStorageScheme', 'PBKDF2_SHA256')
119 topology_st.standalone.config.set('nsslapd-allow-hashed-passwords', 'on')
120 topology_st.standalone.config.set('nsslapd-enable-upgrade-hash', 'on')
121
122 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
123 user = users.get(USER_RDN)
124
125 # Static version of "password" in SSHA256.
126 user.set('userPassword', "{SSHA256}9eliEQgjfc4Fcj1IXZtc/ne1GRF+OIjz/NfSTX4f7HByGMQrWHLMLA==")
127 ts1 = user.get_attr_val_utf8('passwordExpirationTime')
128
129 # Attempt to bind with incorrect password.
130 with pytest.raises(ldap.INVALID_CREDENTIALS):
131 badconn = user.bind('badpassword')
132
133 # Check the pw is SSHA256
134 up = user.get_attr_val_utf8('userPassword')
135 assert up.startswith('{SSHA256}')
136
137 # Bind with correct, trigger update on bind
138 time.sleep(1)
139 conn = user.bind(PASSWORD)
140
141 # Check the pw is now PBKDF2!
142 up = user.get_attr_val_utf8('userPassword')
143 assert up.startswith('{PBKDF2_SHA256}')
144
145 # Verify passwordExpirationtime has not been reset ater hash upgrade
146 ts2 = user.get_attr_val_utf8('passwordExpirationTime')
147 assert ts1 == ts2
148
149 def test_password_hash_on_upgrade_clearcrypt(topology_st):
150 """In some deploymentes, some passwords MAY be in clear or crypt which have
151 specific possible application integrations allowing the read value to be
152 processed by other entities. We avoid upgrading these two, to prevent
153 breaking these integrations.
154
155 :id: 27712492-a4bf-4ea9-977b-b4850ddfb628
156 :setup: Single instance
157 :steps: 1. Set a password hash in CLEAR, and hash to pbkdf2 statically
158 2. Test a correct bind
159 3. Assert the PW is CLEAR
160 4. Set the password to CRYPT
161 5. Test a correct bind
162 6. Assert the PW is CLEAR
163 :expectedresults:
164 1. Successfully set the values
165 2. The bind succeeds
166 3. The PW is CLEAR
167 4. The set succeeds
168 5. The bind succeeds
169 6. The PW is CRYPT
170 """
171 # Make sure the server is set to pkbdf
172 topology_st.standalone.config.set('nsslapd-allow-hashed-passwords', 'on')
173 topology_st.standalone.config.set('nsslapd-enable-upgrade-hash', 'on')
174
175 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
176 user = users.create_test_user(1001)
177
178 topology_st.standalone.config.set('passwordStorageScheme', 'CLEAR')
179 user.set('userPassword', "password")
180 topology_st.standalone.config.set('passwordStorageScheme', 'PBKDF2_SHA256')
181
182 conn = user.bind(PASSWORD)
183 up = user.get_attr_val_utf8('userPassword')
184 assert up.startswith('password')
185
186 user.set('userPassword', "{crypt}I0S3Ry62CSoFg")
187 conn = user.bind(PASSWORD)
188 up = user.get_attr_val_utf8('userPassword')
189 assert up.startswith('{crypt}')
190
191 def test_password_hash_on_upgrade_disable(topology_st):
192 """If a legacy password hash is present, assert that on a correct bind
193 the hash is "upgraded" to the latest-and-greatest hash format on the
194 server. But some people may not like this, so test that we can disable
195 the feature too!
196
197 :id: ed315145-a3d1-4f17-b04c-73d3638e7ade
198 :setup: Single instance
199 :steps: 1. Set a password hash in SSHA256, and hash to pbkdf2 statically
200 2. Test a faulty bind
201 3. Assert the PW is SSHA256
202 4. Test a correct bind
203 5. Assert the PW is SSHA256
204 :expectedresults:
205 1. Successfully set the values
206 2. The bind fails
207 3. The PW is SSHA256
208 4. The bind succeeds
209 5. The PW is SSHA256
210 """
211 # Make sure the server is set to pkbdf
212 topology_st.standalone.config.set('passwordStorageScheme', 'PBKDF2_SHA256')
213 topology_st.standalone.config.set('nsslapd-allow-hashed-passwords', 'on')
214 topology_st.standalone.config.set('nsslapd-enable-upgrade-hash', 'off')
215
216 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
217 user = users.create_test_user(1002)
218 # Static version of "password" in SSHA256.
219 user.set('userPassword', "{SSHA256}9eliEQgjfc4Fcj1IXZtc/ne1GRF+OIjz/NfSTX4f7HByGMQrWHLMLA==")
220 # Attempt to bind with incorrect password.
221 with pytest.raises(ldap.INVALID_CREDENTIALS):
222 badconn = user.bind('badpassword')
223 # Check the pw is SSHA256
224 up = user.get_attr_val_utf8('userPassword')
225 assert up.startswith('{SSHA256}')
226
227 # Bind with correct.
228 conn = user.bind(PASSWORD)
229 # Check the pw is NOT upgraded!
230 up = user.get_attr_val_utf8('userPassword')
231 assert up.startswith('{SSHA256}')