ticket47714_test.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2015 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 sys
  11. import time
  12. import ldap
  13. import logging
  14. import pytest
  15. import shutil
  16. from lib389 import DirSrv, Entry, tools
  17. from lib389.tools import DirSrvTools
  18. from lib389._constants import *
  19. from lib389.properties import *
  20. log = logging.getLogger(__name__)
  21. ACCT_POLICY_CONFIG_DN = ('cn=config,cn=%s,cn=plugins,cn=config' %
  22. PLUGIN_ACCT_POLICY)
  23. ACCT_POLICY_DN = 'cn=Account Inactivation Policy,%s' % SUFFIX
  24. INACTIVITY_LIMIT = '9'
  25. SEARCHFILTER = '(objectclass=*)'
  26. TEST_USER = 'ticket47714user'
  27. TEST_USER_DN = 'uid=%s,%s' % (TEST_USER, SUFFIX)
  28. TEST_USER_PW = '%s' % TEST_USER
  29. class TopologyStandalone(object):
  30. def __init__(self, standalone):
  31. standalone.open()
  32. self.standalone = standalone
  33. @pytest.fixture(scope="module")
  34. def topology(request):
  35. '''
  36. This fixture is used to standalone topology for the 'module'.
  37. '''
  38. standalone = DirSrv(verbose=False)
  39. # Args for the standalone instance
  40. args_instance[SER_HOST] = HOST_STANDALONE
  41. args_instance[SER_PORT] = PORT_STANDALONE
  42. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  43. args_standalone = args_instance.copy()
  44. standalone.allocate(args_standalone)
  45. # Get the status of the instance and restart it if it exists
  46. instance_standalone = standalone.exists()
  47. # Remove the instance
  48. if instance_standalone:
  49. standalone.delete()
  50. # Create the instance
  51. standalone.create()
  52. # Used to retrieve configuration information (dbdir, confdir...)
  53. standalone.open()
  54. def fin():
  55. standalone.delete()
  56. request.addfinalizer(fin)
  57. # Here we have standalone instance up and running
  58. return TopologyStandalone(standalone)
  59. def _header(topology, label):
  60. topology.standalone.log.info("\n\n###############################################")
  61. topology.standalone.log.info("#######")
  62. topology.standalone.log.info("####### %s" % label)
  63. topology.standalone.log.info("#######")
  64. topology.standalone.log.info("###############################################")
  65. def test_ticket47714_init(topology):
  66. """
  67. 1. Add account policy entry to the DB
  68. 2. Add a test user to the DB
  69. """
  70. _header(topology, 'Testing Ticket 47714 - [RFE] Update lastLoginTime also in Account Policy plugin if account lockout is based on passwordExpirationTime.')
  71. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  72. log.info("\n######################### Adding Account Policy entry: %s ######################\n" % ACCT_POLICY_DN)
  73. topology.standalone.add_s(Entry((ACCT_POLICY_DN, {'objectclass': "top ldapsubentry extensibleObject accountpolicy".split(),
  74. 'accountInactivityLimit': INACTIVITY_LIMIT})))
  75. log.info("\n######################### Adding Test User entry: %s ######################\n" % TEST_USER_DN)
  76. topology.standalone.add_s(Entry((TEST_USER_DN, {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
  77. 'cn': TEST_USER,
  78. 'sn': TEST_USER,
  79. 'givenname': TEST_USER,
  80. 'userPassword': TEST_USER_PW,
  81. 'acctPolicySubentry': ACCT_POLICY_DN})))
  82. def test_ticket47714_run_0(topology):
  83. """
  84. Check this change has no inpact to the existing functionality.
  85. 1. Set account policy config without the new attr alwaysRecordLoginAttr
  86. 2. Bind as a test user
  87. 3. Bind as the test user again and check the lastLoginTime is updated
  88. 4. Waint longer than the accountInactivityLimit time and bind as the test user,
  89. which should fail with CONSTANT_VIOLATION.
  90. """
  91. _header(topology, 'Account Policy - No new attr alwaysRecordLoginAttr in config')
  92. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  93. # Modify Account Policy config entry
  94. topology.standalone.modify_s(ACCT_POLICY_CONFIG_DN, [(ldap.MOD_REPLACE, 'alwaysrecordlogin', 'yes'),
  95. (ldap.MOD_REPLACE, 'stateattrname', 'lastLoginTime'),
  96. (ldap.MOD_REPLACE, 'altstateattrname', 'createTimestamp'),
  97. (ldap.MOD_REPLACE, 'specattrname', 'acctPolicySubentry'),
  98. (ldap.MOD_REPLACE, 'limitattrname', 'accountInactivityLimit')])
  99. # Enable the plugins
  100. topology.standalone.plugins.enable(name=PLUGIN_ACCT_POLICY)
  101. topology.standalone.restart(timeout=120)
  102. log.info("\n######################### Bind as %s ######################\n" % TEST_USER_DN)
  103. try:
  104. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  105. except ldap.CONSTRAINT_VIOLATION as e:
  106. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  107. time.sleep(2)
  108. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  109. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  110. lastLoginTime0 = entry[0].lastLoginTime
  111. log.info("\n######################### Bind as %s again ######################\n" % TEST_USER_DN)
  112. try:
  113. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  114. except ldap.CONSTRAINT_VIOLATION as e:
  115. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  116. time.sleep(2)
  117. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  118. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  119. lastLoginTime1 = entry[0].lastLoginTime
  120. log.info("First lastLoginTime: %s, Second lastLoginTime: %s" % (lastLoginTime0, lastLoginTime1))
  121. assert lastLoginTime0 < lastLoginTime1
  122. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  123. entry = topology.standalone.search_s(ACCT_POLICY_DN, ldap.SCOPE_BASE, SEARCHFILTER)
  124. log.info("\n######################### %s ######################\n" % ACCT_POLICY_CONFIG_DN)
  125. log.info("accountInactivityLimit: %s" % entry[0].accountInactivityLimit)
  126. log.info("\n######################### %s DONE ######################\n" % ACCT_POLICY_CONFIG_DN)
  127. log.info("\n######################### Bind as %s again to fail ######################\n" % TEST_USER_DN)
  128. try:
  129. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  130. except ldap.CONSTRAINT_VIOLATION as e:
  131. log.info('CONSTRAINT VIOLATION ' + e.message['desc'])
  132. log.info("%s was successfully inactivated." % TEST_USER_DN)
  133. pass
  134. def test_ticket47714_run_1(topology):
  135. """
  136. Verify a new config attr alwaysRecordLoginAttr
  137. 1. Set account policy config with the new attr alwaysRecordLoginAttr: lastLoginTime
  138. Note: bogus attr is set to stateattrname.
  139. altstateattrname type value is used for checking whether the account is idle or not.
  140. 2. Bind as a test user
  141. 3. Bind as the test user again and check the alwaysRecordLoginAttr: lastLoginTime is updated
  142. """
  143. _header(topology, 'Account Policy - With new attr alwaysRecordLoginAttr in config')
  144. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  145. topology.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_DELETE, 'lastLoginTime', None)])
  146. # Modify Account Policy config entry
  147. topology.standalone.modify_s(ACCT_POLICY_CONFIG_DN, [(ldap.MOD_REPLACE, 'alwaysrecordlogin', 'yes'),
  148. (ldap.MOD_REPLACE, 'stateattrname', 'bogus'),
  149. (ldap.MOD_REPLACE, 'altstateattrname', 'modifyTimestamp'),
  150. (ldap.MOD_REPLACE, 'alwaysRecordLoginAttr', 'lastLoginTime'),
  151. (ldap.MOD_REPLACE, 'specattrname', 'acctPolicySubentry'),
  152. (ldap.MOD_REPLACE, 'limitattrname', 'accountInactivityLimit')])
  153. # Enable the plugins
  154. topology.standalone.plugins.enable(name=PLUGIN_ACCT_POLICY)
  155. topology.standalone.restart(timeout=120)
  156. log.info("\n######################### Bind as %s ######################\n" % TEST_USER_DN)
  157. try:
  158. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  159. except ldap.CONSTRAINT_VIOLATION as e:
  160. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  161. time.sleep(1)
  162. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  163. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  164. lastLoginTime0 = entry[0].lastLoginTime
  165. log.info("\n######################### Bind as %s again ######################\n" % TEST_USER_DN)
  166. try:
  167. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  168. except ldap.CONSTRAINT_VIOLATION as e:
  169. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  170. time.sleep(1)
  171. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  172. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  173. lastLoginTime1 = entry[0].lastLoginTime
  174. log.info("First lastLoginTime: %s, Second lastLoginTime: %s" % (lastLoginTime0, lastLoginTime1))
  175. assert lastLoginTime0 < lastLoginTime1
  176. topology.standalone.log.info("ticket47714 was successfully verified.")
  177. if __name__ == '__main__':
  178. # Run isolated
  179. # -s for DEBUG mode
  180. CURRENT_FILE = os.path.realpath(__file__)
  181. pytest.main("-s %s" % CURRENT_FILE)