pwdPolicy_inherit_global_test.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. import subprocess
  11. import time
  12. import ldap
  13. import pytest
  14. from lib389 import Entry
  15. from lib389._constants import *
  16. from lib389.topologies import topology_st
  17. logging.getLogger(__name__).setLevel(logging.INFO)
  18. log = logging.getLogger(__name__)
  19. CONFIG_DN = 'cn=config'
  20. OU_PEOPLE = 'ou=People,' + DEFAULT_SUFFIX
  21. PWP_CONTAINER = 'nsPwPolicyContainer'
  22. PWP_CONTAINER_DN = 'cn=' + PWP_CONTAINER + ',' + OU_PEOPLE
  23. PWP_ENTRY_DN = 'cn=nsPwPolicyEntry,' + OU_PEOPLE
  24. PWP_CONTAINER_PEOPLE = 'cn="%s",%s' % (PWP_ENTRY_DN, PWP_CONTAINER_DN)
  25. PWP_TEMPLATE_ENTRY_DN = 'cn=nsPwTemplateEntry,' + OU_PEOPLE
  26. ATTR_INHERIT_GLOBAL = 'nsslapd-pwpolicy-inherit-global'
  27. ATTR_CHECK_SYNTAX = 'passwordCheckSyntax'
  28. BN = 'uid=buser,' + DEFAULT_SUFFIX
  29. TEMP_USER = 'cn=test{}'
  30. TEMP_USER_DN = '%s,%s' % (TEMP_USER, OU_PEOPLE)
  31. @pytest.fixture(scope="module")
  32. def test_user(topology_st, request):
  33. """User for binding operation"""
  34. log.info('Adding user {}'.format(BN))
  35. try:
  36. topology_st.standalone.add_s(Entry((BN,
  37. {'objectclass': ['top',
  38. 'person',
  39. 'organizationalPerson',
  40. 'inetOrgPerson'],
  41. 'cn': 'bind user',
  42. 'sn': 'bind user',
  43. 'userPassword': PASSWORD})))
  44. log.info('Adding an aci for the bind user')
  45. BN_ACI = '(targetattr="*")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///%s";)' % BN
  46. topology_st.standalone.modify_s(OU_PEOPLE, [(ldap.MOD_ADD, 'aci', BN_ACI)])
  47. except ldap.LDAPError as e:
  48. log.error('Failed to add user (%s): error (%s)' % (BN,
  49. e.message['desc']))
  50. raise e
  51. def fin():
  52. log.info('Deleting user {}'.format(BN))
  53. topology_st.standalone.delete_s(BN)
  54. topology_st.standalone.modify_s(OU_PEOPLE, [(ldap.MOD_DELETE, 'aci', BN_ACI)])
  55. request.addfinalizer(fin)
  56. @pytest.fixture(scope="module")
  57. def password_policy(topology_st, test_user):
  58. """Set global password policy.
  59. Then, set fine-grained subtree level password policy
  60. to ou=People with no password syntax.
  61. Note: do not touch nsslapd-pwpolicy-inherit-global -- off by default
  62. """
  63. log.info('Enable fine-grained policy')
  64. try:
  65. topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE,
  66. 'nsslapd-pwpolicy-local',
  67. 'on')])
  68. except ldap.LDAPError as e:
  69. log.error('Failed to set fine-grained policy: error {}'.format(
  70. e.message['desc']))
  71. raise e
  72. log.info('Create password policy for subtree {}'.format(OU_PEOPLE))
  73. try:
  74. subprocess.call(['%s/ns-newpwpolicy.pl' % topology_st.standalone.get_sbin_dir(),
  75. '-D', DN_DM, '-w', PASSWORD,
  76. '-p', str(PORT_STANDALONE), '-h', HOST_STANDALONE,
  77. '-S', OU_PEOPLE, '-Z', SERVERID_STANDALONE])
  78. except subprocess.CalledProcessError as e:
  79. log.error('Failed to create pw policy policy for {}: error {}'.format(
  80. OU_PEOPLE, e.message['desc']))
  81. raise e
  82. log.info('Add pwdpolicysubentry attribute to {}'.format(OU_PEOPLE))
  83. try:
  84. topology_st.standalone.modify_s(OU_PEOPLE, [(ldap.MOD_REPLACE,
  85. 'pwdpolicysubentry',
  86. PWP_CONTAINER_PEOPLE)])
  87. except ldap.LDAPError as e:
  88. log.error('Failed to pwdpolicysubentry pw policy ' \
  89. 'policy for {}: error {}'.format(OU_PEOPLE,
  90. e.message['desc']))
  91. raise e
  92. log.info("Set the default settings for the policy container.")
  93. topology_st.standalone.modify_s(PWP_CONTAINER_PEOPLE,
  94. [(ldap.MOD_REPLACE, 'passwordMustChange', 'off'),
  95. (ldap.MOD_REPLACE, 'passwordExp', 'off'),
  96. (ldap.MOD_REPLACE, 'passwordMinAge', '0'),
  97. (ldap.MOD_REPLACE, 'passwordChange', 'off'),
  98. (ldap.MOD_REPLACE, 'passwordStorageScheme', 'ssha')])
  99. check_attr_val(topology_st, CONFIG_DN, ATTR_INHERIT_GLOBAL, 'off')
  100. check_attr_val(topology_st, CONFIG_DN, ATTR_CHECK_SYNTAX, 'off')
  101. def check_attr_val(topology_st, dn, attr, expected):
  102. """Check that entry has the value"""
  103. try:
  104. centry = topology_st.standalone.search_s(dn, ldap.SCOPE_BASE, 'cn=*')
  105. assert centry[0], 'Failed to get %s' % dn
  106. val = centry[0].getValue(attr)
  107. assert val == expected, 'Default value of %s is not %s, but %s' % (
  108. attr, expected, val)
  109. log.info('Default value of %s is %s' % (attr, expected))
  110. except ldap.LDAPError as e:
  111. log.fatal('Failed to search ' + dn + ': ' + e.message['desc'])
  112. raise e
  113. @pytest.mark.parametrize('inherit_value,checksyntax_value',
  114. [('off', 'off'), ('on', 'off'), ('off', 'on')])
  115. def test_entry_has_no_restrictions(topology_st, password_policy, test_user,
  116. inherit_value, checksyntax_value):
  117. """Make sure an entry added to ou=people
  118. has no password syntax restrictions when:
  119. - 'passwordCheckSyntax' is 'off' for 'nsslapd-pwpolicy-inherit-global'
  120. equaled 'off' and 'on'
  121. - 'passwordCheckSyntax' is 'on' for 'nsslapd-pwpolicy-inherit-global'
  122. equaled 'off'
  123. :Feature: Password policy
  124. :Setup: Standalone instance, test user,
  125. password policy entries for a subtree
  126. :Steps: 1. Bind as test user
  127. 2. Set 'nsslapd-pwpolicy-inherit-global' and
  128. 'passwordCheckSyntax' accordingly:
  129. a) 'off' and 'off'
  130. b) 'on' and 'off'
  131. c) 'off' and 'on'
  132. 3. Try to add user with a short password
  133. :Assert: No exception should occure
  134. """
  135. log.info('Set {} to {}'.format(ATTR_INHERIT_GLOBAL, inherit_value))
  136. log.info('Set {} to {}'.format(ATTR_CHECK_SYNTAX, checksyntax_value))
  137. topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
  138. ATTR_INHERIT_GLOBAL, inherit_value)])
  139. topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
  140. ATTR_CHECK_SYNTAX, checksyntax_value)])
  141. # Wait a second for cn=config to apply
  142. time.sleep(1)
  143. check_attr_val(topology_st, CONFIG_DN, ATTR_INHERIT_GLOBAL, inherit_value)
  144. check_attr_val(topology_st, CONFIG_DN, ATTR_CHECK_SYNTAX, checksyntax_value)
  145. log.info('Bind as test user')
  146. topology_st.standalone.simple_bind_s(BN, PASSWORD)
  147. log.info('Make sure an entry added to ou=people has '
  148. 'no password syntax restrictions.')
  149. try:
  150. topology_st.standalone.add_s(Entry((TEMP_USER_DN.format('0'),
  151. {'objectclass': ['top',
  152. 'person',
  153. 'organizationalPerson',
  154. 'inetOrgPerson'],
  155. 'cn': TEMP_USER.format('0'),
  156. 'sn': TEMP_USER.format('0'),
  157. 'userPassword': 'short'})))
  158. except ldap.LDAPError as e:
  159. log.fatal('Failed to add cn=test0 with userPassword: short: ' +
  160. e.message['desc'])
  161. raise e
  162. finally:
  163. log.info('Bind as DM user')
  164. topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
  165. log.info('Remove {}'.format(TEMP_USER_DN.format('0')))
  166. try:
  167. topology_st.standalone.delete_s(TEMP_USER_DN.format('0'))
  168. except ldap.NO_SUCH_OBJECT as e:
  169. log.fatal('There is no {}, it is a problem'.format(TEMP_USER_DN.format('0')))
  170. raise e
  171. @pytest.mark.parametrize('container', [DN_CONFIG, PWP_CONTAINER_PEOPLE])
  172. def test_entry_has_restrictions(topology_st, password_policy, test_user, container):
  173. """Set 'nsslapd-pwpolicy-inherit-global: on'
  174. and 'passwordCheckSyntax: on'. Make sure that
  175. syntax rules work, if set them at both: cn=config and
  176. ou=people policy container.
  177. :Feature: Password policy
  178. :Setup: Standalone instance, test user,
  179. password policy entries for a subtree
  180. :Steps: 1. Bind as test user
  181. 2. Switch 'nsslapd-pwpolicy-inherit-global: on'
  182. 3. Switch 'passwordCheckSyntax: on'
  183. 4. Set 'passwordMinLength: 9' to:
  184. a) cn=config
  185. b) ou=people policy container
  186. 5. Try to add user with a short password (<9)
  187. 6. Try to add user with a long password (>9)
  188. :Assert: User should be rejected
  189. """
  190. log.info('Set {} to {}'.format(ATTR_INHERIT_GLOBAL, 'on'))
  191. log.info('Set {} to {}'.format(ATTR_CHECK_SYNTAX, 'on'))
  192. topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
  193. ATTR_INHERIT_GLOBAL, 'on')])
  194. topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
  195. ATTR_CHECK_SYNTAX, 'on')])
  196. topology_st.standalone.modify_s(container, [(ldap.MOD_REPLACE,
  197. 'passwordMinLength', '9')])
  198. # Wait a second for cn=config to apply
  199. time.sleep(1)
  200. check_attr_val(topology_st, CONFIG_DN, ATTR_INHERIT_GLOBAL, 'on')
  201. check_attr_val(topology_st, CONFIG_DN, ATTR_CHECK_SYNTAX, 'on')
  202. log.info('Bind as test user')
  203. topology_st.standalone.simple_bind_s(BN, PASSWORD)
  204. log.info('Try to add user with a short password (<9)')
  205. with pytest.raises(ldap.CONSTRAINT_VIOLATION):
  206. topology_st.standalone.add_s(Entry((TEMP_USER_DN.format('0'),
  207. {'objectclass': ['top',
  208. 'person',
  209. 'organizationalPerson',
  210. 'inetOrgPerson'],
  211. 'cn': TEMP_USER.format('0'),
  212. 'sn': TEMP_USER.format('0'),
  213. 'userPassword': 'short'})))
  214. log.info('Try to add user with a long password (>9)')
  215. try:
  216. topology_st.standalone.add_s(Entry((TEMP_USER_DN.format('1'),
  217. {'objectclass': ['top',
  218. 'person',
  219. 'organizationalPerson',
  220. 'inetOrgPerson'],
  221. 'cn': TEMP_USER.format('1'),
  222. 'sn': TEMP_USER.format('1'),
  223. 'userPassword': 'Reallylong1'})))
  224. except ldap.LDAPError as e:
  225. log.fatal('Failed to add cn=test1 with userPassword: short: '
  226. + e.message['desc'])
  227. raise e
  228. finally:
  229. log.info('Bind as DM user')
  230. topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
  231. log.info('Remove {}'.format(TEMP_USER_DN.format('0')))
  232. try:
  233. topology_st.standalone.delete_s(TEMP_USER_DN.format('0'))
  234. except ldap.NO_SUCH_OBJECT as e:
  235. log.info('There is no {}, it is okay'.format(TEMP_USER_DN.format('0')))
  236. try:
  237. topology_st.standalone.delete_s(TEMP_USER_DN.format('1'))
  238. except ldap.NO_SUCH_OBJECT as e:
  239. log.fatal('There is no {}, it is a problem'.format(TEMP_USER_DN.format('1')))
  240. raise e
  241. if __name__ == '__main__':
  242. # Run isolated
  243. # -s for DEBUG mode
  244. CURRENT_FILE = os.path.realpath(__file__)
  245. pytest.main("-s %s" % CURRENT_FILE)