regression_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # Copyright (C) 2017 Red Hat, Inc.
  2. # All rights reserved.
  3. #
  4. # License: GPL (version 3 or any later version).
  5. # See LICENSE for details.
  6. # --- END COPYRIGHT BLOCK ---
  7. #
  8. import pytest
  9. import time
  10. from lib389._constants import PASSWORD, DN_DM, DEFAULT_SUFFIX
  11. from lib389._constants import SUFFIX, PASSWORD, DN_DM, DN_CONFIG, PLUGIN_RETRO_CHANGELOG, DEFAULT_SUFFIX, DEFAULT_CHANGELOG_DB, DEFAULT_BENAME
  12. from lib389 import Entry
  13. from lib389.topologies import topology_m1 as topo_master
  14. from lib389.idm.user import UserAccounts
  15. from lib389.utils import ldap, os, logging, ensure_bytes, ds_is_newer, ds_supports_new_changelog
  16. from lib389.topologies import topology_st as topo
  17. from lib389.idm.organizationalunit import OrganizationalUnits
  18. pytestmark = pytest.mark.tier1
  19. DEBUGGING = os.getenv("DEBUGGING", default=False)
  20. if DEBUGGING:
  21. logging.getLogger(__name__).setLevel(logging.DEBUG)
  22. else:
  23. logging.getLogger(__name__).setLevel(logging.INFO)
  24. log = logging.getLogger(__name__)
  25. user_data = {'cn': 'CNpwtest1', 'sn': 'SNpwtest1', 'uid': 'UIDpwtest1', 'mail': '[email protected]',
  26. 'givenname': 'GNpwtest1'}
  27. TEST_PASSWORDS = list(user_data.values())
  28. # Add substring/token values of "CNpwtest1"
  29. TEST_PASSWORDS += ['CNpwtest1ZZZZ', 'ZZZZZCNpwtest1',
  30. 'ZCNpwtest1', 'CNpwtest1Z', 'ZCNpwtest1Z',
  31. 'ZZCNpwtest1', 'CNpwtest1ZZ', 'ZZCNpwtest1ZZ',
  32. 'ZZZCNpwtest1', 'CNpwtest1ZZZ', 'ZZZCNpwtest1ZZZ',
  33. 'ZZZZZZCNpwtest1ZZZZZZZZ']
  34. TEST_PASSWORDS2 = (
  35. 'CN12pwtest31', 'SN3pwtest231', 'UID1pwtest123', '[email protected]', '2GN1pwtest123', 'People123')
  36. def _check_unhashed_userpw(inst, user_dn, is_present=False):
  37. """Check if unhashed#user#password attribute is present or not in the changelog"""
  38. unhashed_pwd_attribute = 'unhashed#user#password'
  39. if ds_supports_new_changelog():
  40. dbscanOut = inst.dbscan(DEFAULT_BENAME, 'replication_changelog')
  41. else:
  42. changelog_dbdir = os.path.join(os.path.dirname(inst.dbdir), DEFAULT_CHANGELOG_DB)
  43. for dbfile in os.listdir(changelog_dbdir):
  44. if dbfile.endswith('.db'):
  45. changelog_dbfile = os.path.join(changelog_dbdir, dbfile)
  46. log.info('Changelog dbfile file exist: {}'.format(changelog_dbfile))
  47. log.info('Running dbscan -f to check {} attr'.format(unhashed_pwd_attribute))
  48. dbscanOut = inst.dbscan(DEFAULT_CHANGELOG_DB, changelog_dbfile)
  49. for entry in dbscanOut.split(b'dbid: '):
  50. if ensure_bytes('operation: modify') in entry and ensure_bytes(user_dn) in entry and ensure_bytes('userPassword') in entry:
  51. if is_present:
  52. assert ensure_bytes(unhashed_pwd_attribute) in entry
  53. else:
  54. assert ensure_bytes(unhashed_pwd_attribute) not in entry
  55. @pytest.fixture(scope="module")
  56. def passw_policy(topo, request):
  57. """Configure password policy with PasswordCheckSyntax attribute set to on"""
  58. log.info('Configure Pwpolicy with PasswordCheckSyntax and nsslapd-pwpolicy-local set to on')
  59. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  60. topo.standalone.config.set('PasswordExp', 'on')
  61. topo.standalone.config.set('PasswordCheckSyntax', 'off')
  62. topo.standalone.config.set('nsslapd-pwpolicy-local', 'on')
  63. subtree = 'ou=people,{}'.format(DEFAULT_SUFFIX)
  64. log.info('Configure subtree password policy for {}'.format(subtree))
  65. topo.standalone.subtreePwdPolicy(subtree, {'passwordchange': b'on',
  66. 'passwordCheckSyntax': b'on',
  67. 'passwordLockout': b'on',
  68. 'passwordResetFailureCount': b'3',
  69. 'passwordLockoutDuration': b'3',
  70. 'passwordMaxFailure': b'2'})
  71. time.sleep(1)
  72. def fin():
  73. log.info('Reset pwpolicy configuration settings')
  74. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  75. topo.standalone.config.set('PasswordExp', 'off')
  76. topo.standalone.config.set('PasswordCheckSyntax', 'off')
  77. topo.standalone.config.set('nsslapd-pwpolicy-local', 'off')
  78. request.addfinalizer(fin)
  79. @pytest.fixture(scope="module")
  80. def create_user(topo, request):
  81. """Add test users using UserAccounts"""
  82. log.info('Adding user-uid={},ou=people,{}'.format(user_data['uid'], DEFAULT_SUFFIX))
  83. users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
  84. user_properties = {
  85. 'uidNumber': '1001',
  86. 'gidNumber': '2001',
  87. 'cn': 'pwtest1',
  88. 'userpassword': PASSWORD,
  89. 'homeDirectory': '/home/pwtest1'}
  90. user_properties.update(user_data)
  91. tuser = users.create(properties=user_properties)
  92. def fin():
  93. log.info('Deleting user-{}'.format(tuser.dn))
  94. tuser.delete()
  95. request.addfinalizer(fin)
  96. return tuser
  97. def test_pwp_local_unlock(topo, passw_policy, create_user):
  98. """Test subtree policies use the same global default for passwordUnlock
  99. :id: 741a8417-5f65-4012-b9ed-87987ce3ca1b
  100. :setup: Standalone instance
  101. :steps:
  102. 1. Test user can bind
  103. 2. Bind with bad passwords to lockout account, and verify account is locked
  104. 3. Wait for lockout interval, and bind with valid password
  105. :expectedresults:
  106. 1. Bind successful
  107. 2. Entry is locked
  108. 3. Entry can bind with correct password
  109. """
  110. # Add aci so users can change their own password
  111. USER_ACI = '(targetattr="userpassword")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///self";)'
  112. ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX)
  113. ou = ous.get('people')
  114. ou.add('aci', USER_ACI)
  115. log.info("Verify user can bind...")
  116. create_user.bind(PASSWORD)
  117. log.info('Test passwordUnlock default - user should be able to reset password after lockout')
  118. for i in range(0, 2):
  119. try:
  120. create_user.bind("bad-password")
  121. except ldap.INVALID_CREDENTIALS:
  122. # expected
  123. pass
  124. except ldap.LDAPError as e:
  125. log.fatal("Got unexpected failure: " + str(e))
  126. raise e
  127. log.info('Verify account is locked')
  128. with pytest.raises(ldap.CONSTRAINT_VIOLATION):
  129. create_user.bind(PASSWORD)
  130. log.info('Wait for lockout duration...')
  131. time.sleep(4)
  132. log.info('Check if user can now bind with correct password')
  133. create_user.bind(PASSWORD)
  134. @pytest.mark.bz1465600
  135. @pytest.mark.parametrize("user_pasw", TEST_PASSWORDS)
  136. def test_trivial_passw_check(topo, passw_policy, create_user, user_pasw):
  137. """PasswordCheckSyntax attribute fails to validate cn, sn, uid, givenname, ou and mail attributes
  138. :id: bf9fe1ef-56cb-46a3-a6f8-5530398a06dc
  139. :parametrized: yes
  140. :setup: Standalone instance.
  141. :steps:
  142. 1. Configure local password policy with PasswordCheckSyntax set to on.
  143. 2. Add users with cn, sn, uid, givenname, mail and userPassword attributes.
  144. 3. Configure subtree password policy for ou=people subtree.
  145. 4. Reset userPassword with trivial values like cn, sn, uid, givenname, ou and mail attributes.
  146. :expectedresults:
  147. 1. Enabling PasswordCheckSyntax should PASS.
  148. 2. Add users should PASS.
  149. 3. Configure subtree password policy should PASS.
  150. 4. Resetting userPassword to cn, sn, uid and mail should be rejected.
  151. """
  152. create_user.rebind(PASSWORD)
  153. log.info('Replace userPassword attribute with {}'.format(user_pasw))
  154. with pytest.raises(ldap.CONSTRAINT_VIOLATION) as excinfo:
  155. create_user.reset_password(user_pasw)
  156. log.fatal('Failed: Userpassword with {} is accepted'.format(user_pasw))
  157. assert 'password based off of user entry' in str(excinfo.value)
  158. # reset password
  159. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  160. create_user.set('userPassword', PASSWORD)
  161. @pytest.mark.parametrize("user_pasw", TEST_PASSWORDS)
  162. def test_global_vs_local(topo, passw_policy, create_user, user_pasw):
  163. """Passwords rejected if its similar to uid, cn, sn, givenname, ou and mail attributes
  164. :id: dfd6cf5d-8bcd-4895-a691-a43ad9ec1be8
  165. :parametrized: yes
  166. :setup: Standalone instance
  167. :steps:
  168. 1. Configure global password policy with PasswordCheckSyntax set to off
  169. 2. Add users with cn, sn, uid, mail, givenname and userPassword attributes
  170. 3. Replace userPassword similar to cn, sn, uid, givenname, ou and mail attributes
  171. :expectedresults:
  172. 1. Disabling the local policy should PASS.
  173. 2. Add users should PASS.
  174. 3. Resetting userPasswords similar to cn, sn, uid, givenname, ou and mail attributes should PASS.
  175. """
  176. log.info('Configure Pwpolicy with PasswordCheckSyntax and nsslapd-pwpolicy-local set to off')
  177. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  178. topo.standalone.config.set('nsslapd-pwpolicy-local', 'off')
  179. create_user.rebind(PASSWORD)
  180. log.info('Replace userPassword attribute with {}'.format(user_pasw))
  181. create_user.reset_password(user_pasw)
  182. # reset password
  183. create_user.set('userPassword', PASSWORD)
  184. @pytest.mark.ds49789
  185. def test_unhashed_pw_switch(topo_master):
  186. """Check that nsslapd-unhashed-pw-switch works corrently
  187. :id: e5aba180-d174-424d-92b0-14fe7bb0b92a
  188. :setup: Master Instance
  189. :steps:
  190. 1. A Master is created, enable retrocl (not used here)
  191. 2. Create a set of users
  192. 3. update userpassword of user1 and check that unhashed#user#password is not logged (default)
  193. 4. udpate userpassword of user2 and check that unhashed#user#password is not logged ('nolog')
  194. 5. udpate userpassword of user3 and check that unhashed#user#password is logged ('on')
  195. :expectedresults:
  196. 1. Success
  197. 2. Success
  198. 3. Success (unhashed#user#password is not logged in the replication changelog)
  199. 4. Success (unhashed#user#password is not logged in the replication changelog)
  200. 5. Success (unhashed#user#password is logged in the replication changelog)
  201. """
  202. MAX_USERS = 10
  203. PEOPLE_DN = ("ou=people," + DEFAULT_SUFFIX)
  204. inst = topo_master.ms["master1"]
  205. inst.modify_s("cn=Retro Changelog Plugin,cn=plugins,cn=config",
  206. [(ldap.MOD_REPLACE, 'nsslapd-changelogmaxage', b'2m'),
  207. (ldap.MOD_REPLACE, 'nsslapd-changelog-trim-interval', b"5s"),
  208. (ldap.MOD_REPLACE, 'nsslapd-logAccess', b'on')])
  209. inst.config.loglevel(vals=[256 + 4], service='access')
  210. inst.restart()
  211. # If you need any test suite initialization,
  212. # please, write additional fixture for that (including finalizer).
  213. # Topology for suites are predefined in lib389/topologies.py.
  214. # enable dynamic plugins, memberof and retro cl plugin
  215. #
  216. log.info('Enable plugins...')
  217. try:
  218. inst.modify_s(DN_CONFIG,
  219. [(ldap.MOD_REPLACE,
  220. 'nsslapd-dynamic-plugins',
  221. b'on')])
  222. except ldap.LDAPError as e:
  223. ldap.error('Failed to enable dynamic plugins! ' + e.message['desc'])
  224. assert False
  225. #topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  226. inst.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  227. #topology_st.standalone.modify_s("cn=changelog,cn=ldbm database,cn=plugins,cn=config", [(ldap.MOD_REPLACE, 'nsslapd-cachememsize', str(100000))])
  228. inst.restart()
  229. log.info('create users and group...')
  230. for idx in range(1, MAX_USERS):
  231. try:
  232. USER_DN = ("uid=member%d,%s" % (idx, PEOPLE_DN))
  233. inst.add_s(Entry((USER_DN,
  234. {'objectclass': 'top extensibleObject'.split(),
  235. 'uid': 'member%d' % (idx)})))
  236. except ldap.LDAPError as e:
  237. log.fatal('Failed to add user (%s): error %s' % (USER_DN, e.message['desc']))
  238. assert False
  239. # Check default is that unhashed#user#password is not logged on 1.4.1.6+
  240. user = "uid=member1,%s" % (PEOPLE_DN)
  241. inst.modify_s(user, [(ldap.MOD_REPLACE,
  242. 'userpassword',
  243. PASSWORD.encode())])
  244. inst.stop()
  245. if ds_is_newer('1.4.1.6'):
  246. _check_unhashed_userpw(inst, user, is_present=False)
  247. else:
  248. _check_unhashed_userpw(inst, user, is_present=True)
  249. # Check with nolog that unhashed#user#password is not logged
  250. inst.modify_s(DN_CONFIG,
  251. [(ldap.MOD_REPLACE,
  252. 'nsslapd-unhashed-pw-switch',
  253. b'nolog')])
  254. inst.restart()
  255. user = "uid=member2,%s" % (PEOPLE_DN)
  256. inst.modify_s(user, [(ldap.MOD_REPLACE,
  257. 'userpassword',
  258. PASSWORD.encode())])
  259. inst.stop()
  260. _check_unhashed_userpw(inst, user, is_present=False)
  261. # Check with value 'on' that unhashed#user#password is logged
  262. inst.modify_s(DN_CONFIG,
  263. [(ldap.MOD_REPLACE,
  264. 'nsslapd-unhashed-pw-switch',
  265. b'on')])
  266. inst.restart()
  267. user = "uid=member3,%s" % (PEOPLE_DN)
  268. inst.modify_s(user, [(ldap.MOD_REPLACE,
  269. 'userpassword',
  270. PASSWORD.encode())])
  271. inst.stop()
  272. _check_unhashed_userpw(inst, user, is_present=True)
  273. if DEBUGGING:
  274. # Add debugging steps(if any)...
  275. pass
  276. if __name__ == '__main__':
  277. # Run isolated
  278. # -s for DEBUG mode
  279. CURRENT_FILE = os.path.realpath(__file__)
  280. pytest.main(["-s", CURRENT_FILE])