ticket47970_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 ldap.sasl
  14. import logging
  15. import pytest
  16. from lib389 import DirSrv, Entry, tools, tasks
  17. from lib389.tools import DirSrvTools
  18. from lib389._constants import *
  19. from lib389.properties import *
  20. from lib389.tasks import *
  21. log = logging.getLogger(__name__)
  22. USER1_DN = "uid=user1,%s" % DEFAULT_SUFFIX
  23. USER2_DN = "uid=user2,%s" % DEFAULT_SUFFIX
  24. class TopologyStandalone(object):
  25. def __init__(self, standalone):
  26. standalone.open()
  27. self.standalone = standalone
  28. @pytest.fixture(scope="module")
  29. def topology(request):
  30. '''
  31. This fixture is used to standalone topology for the 'module'.
  32. '''
  33. standalone = DirSrv(verbose=False)
  34. # Args for the standalone instance
  35. args_instance[SER_HOST] = HOST_STANDALONE
  36. args_instance[SER_PORT] = PORT_STANDALONE
  37. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  38. args_standalone = args_instance.copy()
  39. standalone.allocate(args_standalone)
  40. # Get the status of the instance and restart it if it exists
  41. instance_standalone = standalone.exists()
  42. # Remove the instance
  43. if instance_standalone:
  44. standalone.delete()
  45. # Create the instance
  46. standalone.create()
  47. # Used to retrieve configuration information (dbdir, confdir...)
  48. standalone.open()
  49. def fin():
  50. standalone.delete()
  51. request.addfinalizer(fin)
  52. # Here we have standalone instance up and running
  53. return TopologyStandalone(standalone)
  54. def test_ticket47970(topology):
  55. """
  56. Testing that a failed SASL bind does not trigger account lockout -
  57. which would attempt to update the passwordRetryCount on the root dse entry
  58. """
  59. log.info('Testing Ticket 47970 - Testing that a failed SASL bind does not trigger account lockout')
  60. #
  61. # Enable account lockout
  62. #
  63. try:
  64. topology.standalone.modify_s("cn=config", [(ldap.MOD_REPLACE, 'passwordLockout', 'on')])
  65. log.info('account lockout enabled.')
  66. except ldap.LDAPError as e:
  67. log.error('Failed to enable account lockout: ' + e.message['desc'])
  68. assert False
  69. try:
  70. topology.standalone.modify_s("cn=config", [(ldap.MOD_REPLACE, 'passwordMaxFailure', '5')])
  71. log.info('passwordMaxFailure set.')
  72. except ldap.LDAPError as e:
  73. log.error('Failed to to set passwordMaxFailure: ' + e.message['desc'])
  74. assert False
  75. #
  76. # Perform SASL bind that should fail
  77. #
  78. failed_as_expected = False
  79. try:
  80. user_name = "mark"
  81. pw = "secret"
  82. auth_tokens = ldap.sasl.digest_md5(user_name, pw)
  83. topology.standalone.sasl_interactive_bind_s("", auth_tokens)
  84. except ldap.INVALID_CREDENTIALS as e:
  85. log.info("SASL Bind failed as expected")
  86. failed_as_expected = True
  87. if not failed_as_expected:
  88. log.error("SASL bind unexpectedly succeeded!")
  89. assert False
  90. #
  91. # Check that passwordRetryCount was not set on the root dse entry
  92. #
  93. try:
  94. entry = topology.standalone.search_s("", ldap.SCOPE_BASE,
  95. "passwordRetryCount=*",
  96. ['passwordRetryCount'])
  97. except ldap.LDAPError as e:
  98. log.error('Failed to search Root DSE entry: ' + e.message['desc'])
  99. assert False
  100. if entry:
  101. log.error('Root DSE was incorrectly updated')
  102. assert False
  103. # We passed
  104. log.info('Root DSE was correctly not updated')
  105. if __name__ == '__main__':
  106. # Run isolated
  107. # -s for DEBUG mode
  108. CURRENT_FILE = os.path.realpath(__file__)
  109. pytest.main("-s %s" % CURRENT_FILE)