1
0

ticket47950_test.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. from lib389 import DirSrv, Entry, tools, tasks
  16. from lib389.tools import DirSrvTools
  17. from lib389._constants import *
  18. from lib389.properties import *
  19. from lib389.tasks import *
  20. log = logging.getLogger(__name__)
  21. USER1_DN = "uid=user1,%s" % DEFAULT_SUFFIX
  22. USER2_DN = "uid=user2,%s" % DEFAULT_SUFFIX
  23. class TopologyStandalone(object):
  24. def __init__(self, standalone):
  25. standalone.open()
  26. self.standalone = standalone
  27. @pytest.fixture(scope="module")
  28. def topology(request):
  29. '''
  30. This fixture is used to standalone topology for the 'module'.
  31. '''
  32. standalone = DirSrv(verbose=False)
  33. # Args for the standalone instance
  34. args_instance[SER_HOST] = HOST_STANDALONE
  35. args_instance[SER_PORT] = PORT_STANDALONE
  36. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  37. args_standalone = args_instance.copy()
  38. standalone.allocate(args_standalone)
  39. # Get the status of the instance and restart it if it exists
  40. instance_standalone = standalone.exists()
  41. # Remove the instance
  42. if instance_standalone:
  43. standalone.delete()
  44. # Create the instance
  45. standalone.create()
  46. # Used to retrieve configuration information (dbdir, confdir...)
  47. standalone.open()
  48. def fin():
  49. standalone.delete()
  50. request.addfinalizer(fin)
  51. # Here we have standalone instance up and running
  52. return TopologyStandalone(standalone)
  53. def test_ticket47950(topology):
  54. """
  55. Testing nsslapd-plugin-binddn-tracking does not cause issues around
  56. access control and reconfiguring replication/repl agmt.
  57. """
  58. log.info('Testing Ticket 47950 - Testing nsslapd-plugin-binddn-tracking')
  59. #
  60. # Turn on bind dn tracking
  61. #
  62. try:
  63. topology.standalone.modify_s("cn=config", [(ldap.MOD_REPLACE, 'nsslapd-plugin-binddn-tracking', 'on')])
  64. log.info('nsslapd-plugin-binddn-tracking enabled.')
  65. except ldap.LDAPError as e:
  66. log.error('Failed to enable bind dn tracking: ' + e.message['desc'])
  67. assert False
  68. #
  69. # Add two users
  70. #
  71. try:
  72. topology.standalone.add_s(Entry((USER1_DN, {
  73. 'objectclass': "top person inetuser".split(),
  74. 'userpassword': "password",
  75. 'sn': "1",
  76. 'cn': "user 1"})))
  77. log.info('Added test user %s' % USER1_DN)
  78. except ldap.LDAPError as e:
  79. log.error('Failed to add %s: %s' % (USER1_DN, e.message['desc']))
  80. assert False
  81. try:
  82. topology.standalone.add_s(Entry((USER2_DN, {
  83. 'objectclass': "top person inetuser".split(),
  84. 'sn': "2",
  85. 'cn': "user 2"})))
  86. log.info('Added test user %s' % USER2_DN)
  87. except ldap.LDAPError as e:
  88. log.error('Failed to add user1: ' + e.message['desc'])
  89. assert False
  90. #
  91. # Add an aci
  92. #
  93. try:
  94. acival = '(targetattr ="cn")(version 3.0;acl "Test bind dn tracking"' + \
  95. ';allow (all) (userdn = "ldap:///%s");)' % USER1_DN
  96. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', acival)])
  97. log.info('Added aci')
  98. except ldap.LDAPError as e:
  99. log.error('Failed to add aci: ' + e.message['desc'])
  100. assert False
  101. #
  102. # Make modification as user
  103. #
  104. try:
  105. topology.standalone.simple_bind_s(USER1_DN, "password")
  106. log.info('Bind as user %s successful' % USER1_DN)
  107. except ldap.LDAPError as e:
  108. log.error('Failed to bind as user1: ' + e.message['desc'])
  109. assert False
  110. try:
  111. topology.standalone.modify_s(USER2_DN, [(ldap.MOD_REPLACE, 'cn', 'new value')])
  112. log.info('%s successfully modified user %s' % (USER1_DN, USER2_DN))
  113. except ldap.LDAPError as e:
  114. log.error('Failed to update user2: ' + e.message['desc'])
  115. assert False
  116. #
  117. # Setup replica and create a repl agmt
  118. #
  119. try:
  120. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  121. log.info('Bind as %s successful' % DN_DM)
  122. except ldap.LDAPError as e:
  123. log.error('Failed to bind as rootDN: ' + e.message['desc'])
  124. assert False
  125. try:
  126. topology.standalone.replica.enableReplication(suffix=DEFAULT_SUFFIX, role=REPLICAROLE_MASTER,
  127. replicaId=REPLICAID_MASTER_1)
  128. log.info('Successfully enabled replication.')
  129. except ValueError:
  130. log.error('Failed to enable replication')
  131. assert False
  132. properties = {RA_NAME: r'test plugin internal bind dn',
  133. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  134. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  135. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  136. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  137. try:
  138. repl_agreement = topology.standalone.agreement.create(suffix=DEFAULT_SUFFIX, host="127.0.0.1",
  139. port="7777", properties=properties)
  140. log.info('Successfully created replication agreement')
  141. except InvalidArgumentError as e:
  142. log.error('Failed to create replication agreement: ' + e.message['desc'])
  143. assert False
  144. #
  145. # modify replica
  146. #
  147. try:
  148. properties = {REPLICA_ID: "7"}
  149. topology.standalone.replica.setProperties(DEFAULT_SUFFIX, None, None, properties)
  150. log.info('Successfully modified replica')
  151. except ldap.LDAPError as e:
  152. log.error('Failed to update replica config: ' + e.message['desc'])
  153. assert False
  154. #
  155. # modify repl agmt
  156. #
  157. try:
  158. properties = {RA_CONSUMER_PORT: "8888"}
  159. topology.standalone.agreement.setProperties(None, repl_agreement, None, properties)
  160. log.info('Successfully modified replication agreement')
  161. except ValueError:
  162. log.error('Failed to update replica agreement: ' + repl_agreement)
  163. assert False
  164. if __name__ == '__main__':
  165. # Run isolated
  166. # -s for DEBUG mode
  167. CURRENT_FILE = os.path.realpath(__file__)
  168. pytest.main("-s %s" % CURRENT_FILE)