ticket47833_test.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. from lib389.utils import *
  21. SCOPE_IN_CN = 'in'
  22. SCOPE_OUT_CN = 'out'
  23. SCOPE_IN_DN = 'cn=%s,%s' % (SCOPE_IN_CN, SUFFIX)
  24. SCOPE_OUT_DN = 'cn=%s,%s' % (SCOPE_OUT_CN, SUFFIX)
  25. PROVISIONING_CN = "provisioning"
  26. PROVISIONING_DN = "cn=%s,%s" % (PROVISIONING_CN, SCOPE_IN_DN)
  27. ACTIVE_CN = "accounts"
  28. STAGE_CN = "staged users"
  29. DELETE_CN = "deleted users"
  30. ACTIVE_DN = "cn=%s,%s" % (ACTIVE_CN, SCOPE_IN_DN)
  31. STAGE_DN = "cn=%s,%s" % (STAGE_CN, PROVISIONING_DN)
  32. DELETE_DN = "cn=%s,%s" % (DELETE_CN, PROVISIONING_DN)
  33. STAGE_USER_CN = "stage guy"
  34. STAGE_USER_DN = "cn=%s,%s" % (STAGE_USER_CN, STAGE_DN)
  35. ACTIVE_USER_CN = "active guy"
  36. ACTIVE_USER_DN = "cn=%s,%s" % (ACTIVE_USER_CN, ACTIVE_DN)
  37. OUT_USER_CN = "out guy"
  38. OUT_USER_DN = "cn=%s,%s" % (OUT_USER_CN, SCOPE_OUT_DN)
  39. STAGE_GROUP_CN = "stage group"
  40. STAGE_GROUP_DN = "cn=%s,%s" % (STAGE_GROUP_CN, STAGE_DN)
  41. ACTIVE_GROUP_CN = "active group"
  42. ACTIVE_GROUP_DN = "cn=%s,%s" % (ACTIVE_GROUP_CN, ACTIVE_DN)
  43. OUT_GROUP_CN = "out group"
  44. OUT_GROUP_DN = "cn=%s,%s" % (OUT_GROUP_CN, SCOPE_OUT_DN)
  45. logging.getLogger(__name__).setLevel(logging.DEBUG)
  46. log = logging.getLogger(__name__)
  47. installation1_prefix = None
  48. class TopologyStandalone(object):
  49. def __init__(self, standalone):
  50. standalone.open()
  51. self.standalone = standalone
  52. @pytest.fixture(scope="module")
  53. def topology(request):
  54. global installation1_prefix
  55. if installation1_prefix:
  56. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  57. # Creating standalone instance ...
  58. standalone = DirSrv(verbose=False)
  59. if installation1_prefix:
  60. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  61. args_instance[SER_HOST] = HOST_STANDALONE
  62. args_instance[SER_PORT] = PORT_STANDALONE
  63. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  64. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  65. args_standalone = args_instance.copy()
  66. standalone.allocate(args_standalone)
  67. instance_standalone = standalone.exists()
  68. if instance_standalone:
  69. standalone.delete()
  70. standalone.create()
  71. standalone.open()
  72. def fin():
  73. standalone.delete()
  74. request.addfinalizer(fin)
  75. return TopologyStandalone(standalone)
  76. def _header(topology, label):
  77. topology.standalone.log.info("\n\n###############################################")
  78. topology.standalone.log.info("#######")
  79. topology.standalone.log.info("####### %s" % label)
  80. topology.standalone.log.info("#######")
  81. topology.standalone.log.info("###############################################")
  82. def _add_user(topology, type='active'):
  83. if type == 'active':
  84. topology.standalone.add_s(Entry((ACTIVE_USER_DN, {
  85. 'objectclass': "top person inetuser".split(),
  86. 'sn': ACTIVE_USER_CN,
  87. 'cn': ACTIVE_USER_CN})))
  88. elif type == 'stage':
  89. topology.standalone.add_s(Entry((STAGE_USER_DN, {
  90. 'objectclass': "top person inetuser".split(),
  91. 'sn': STAGE_USER_CN,
  92. 'cn': STAGE_USER_CN})))
  93. else:
  94. topology.standalone.add_s(Entry((OUT_USER_DN, {
  95. 'objectclass': "top person inetuser".split(),
  96. 'sn': OUT_USER_CN,
  97. 'cn': OUT_USER_CN})))
  98. def _find_memberof(topology, user_dn=None, group_dn=None, find_result=True):
  99. assert(topology)
  100. assert(user_dn)
  101. assert(group_dn)
  102. ent = topology.standalone.getEntry(user_dn, ldap.SCOPE_BASE, "(objectclass=*)", ['memberof'])
  103. found = False
  104. if ent.hasAttr('memberof'):
  105. for val in ent.getValues('memberof'):
  106. topology.standalone.log.info("!!!!!!! %s: memberof->%s" % (user_dn, val))
  107. if val == group_dn:
  108. found = True
  109. break
  110. if find_result:
  111. assert(found)
  112. else:
  113. assert(not found)
  114. def _find_member(topology, user_dn=None, group_dn=None, find_result=True):
  115. assert(topology)
  116. assert(user_dn)
  117. assert(group_dn)
  118. ent = topology.standalone.getEntry(group_dn, ldap.SCOPE_BASE, "(objectclass=*)", ['member'])
  119. found = False
  120. if ent.hasAttr('member'):
  121. for val in ent.getValues('member'):
  122. topology.standalone.log.info("!!!!!!! %s: member ->%s" % (group_dn, val))
  123. if val == user_dn:
  124. found = True
  125. break
  126. if find_result:
  127. assert(found)
  128. else:
  129. assert(not found)
  130. def _modrdn_entry(topology=None, entry_dn=None, new_rdn=None, del_old=0, new_superior=None):
  131. assert topology != None
  132. assert entry_dn != None
  133. assert new_rdn != None
  134. topology.standalone.log.info("\n\n######################### MODRDN %s ######################\n" % new_rdn)
  135. if new_superior:
  136. topology.standalone.rename_s(entry_dn, new_rdn, newsuperior=new_superior, delold=del_old)
  137. else:
  138. topology.standalone.rename_s(entry_dn, new_rdn, delold=del_old)
  139. def _check_memberof(topology=None, action=None, user_dn=None, group_dn=None, find_result=None):
  140. assert(topology)
  141. assert(user_dn)
  142. assert(group_dn)
  143. if action == ldap.MOD_ADD:
  144. txt = 'add'
  145. elif action == ldap.MOD_DELETE:
  146. txt = 'delete'
  147. else:
  148. txt = 'replace'
  149. topology.standalone.log.info('\n%s entry %s' % (txt, user_dn))
  150. topology.standalone.log.info('to group %s' % group_dn)
  151. topology.standalone.modify_s(group_dn, [(action, 'member', user_dn)])
  152. time.sleep(1)
  153. _find_memberof(topology, user_dn=user_dn, group_dn=group_dn, find_result=find_result)
  154. def test_ticket47829_init(topology):
  155. topology.standalone.add_s(Entry((SCOPE_IN_DN, {
  156. 'objectclass': "top nscontainer".split(),
  157. 'cn': SCOPE_IN_DN})))
  158. topology.standalone.add_s(Entry((SCOPE_OUT_DN, {
  159. 'objectclass': "top nscontainer".split(),
  160. 'cn': SCOPE_OUT_DN})))
  161. topology.standalone.add_s(Entry((PROVISIONING_DN, {
  162. 'objectclass': "top nscontainer".split(),
  163. 'cn': PROVISIONING_CN})))
  164. topology.standalone.add_s(Entry((ACTIVE_DN, {
  165. 'objectclass': "top nscontainer".split(),
  166. 'cn': ACTIVE_CN})))
  167. topology.standalone.add_s(Entry((STAGE_DN, {
  168. 'objectclass': "top nscontainer".split(),
  169. 'cn': STAGE_DN})))
  170. topology.standalone.add_s(Entry((DELETE_DN, {
  171. 'objectclass': "top nscontainer".split(),
  172. 'cn': DELETE_CN})))
  173. # add groups
  174. topology.standalone.add_s(Entry((ACTIVE_GROUP_DN, {
  175. 'objectclass': "top groupOfNames".split(),
  176. 'cn': ACTIVE_GROUP_CN})))
  177. topology.standalone.add_s(Entry((STAGE_GROUP_DN, {
  178. 'objectclass': "top groupOfNames".split(),
  179. 'cn': STAGE_GROUP_CN})))
  180. topology.standalone.add_s(Entry((OUT_GROUP_DN, {
  181. 'objectclass': "top groupOfNames".split(),
  182. 'cn': OUT_GROUP_CN})))
  183. # add users
  184. _add_user(topology, 'active')
  185. _add_user(topology, 'stage')
  186. _add_user(topology, 'out')
  187. # enable memberof of with scope account
  188. topology.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  189. dn = "cn=%s,%s" % (PLUGIN_MEMBER_OF, DN_PLUGIN)
  190. topology.standalone.modify_s(dn, [(ldap.MOD_REPLACE, 'memberOfEntryScope', ACTIVE_DN)])
  191. topology.standalone.restart(timeout=10)
  192. def test_ticket47829_mod_stage_user_modrdn_stage_user_1(topology):
  193. _header(topology, 'add an Stage user to a Active group. Then move Stage user to Stage')
  194. old_stage_user_dn = STAGE_USER_DN
  195. old_stage_user_rdn = "cn=%s" % STAGE_USER_CN
  196. new_stage_user_rdn = "cn=x%s" % STAGE_USER_CN
  197. new_stage_user_dn = "%s,%s" % (new_stage_user_rdn, STAGE_DN)
  198. # add Stage user to active group
  199. _check_memberof(topology, action=ldap.MOD_ADD, user_dn=old_stage_user_dn, group_dn=ACTIVE_GROUP_DN, find_result=False)
  200. _find_member (topology, user_dn=old_stage_user_dn, group_dn=ACTIVE_GROUP_DN, find_result=True)
  201. # move the Stage entry to Stage, expect no 'member' and 'memberof'
  202. _modrdn_entry (topology, entry_dn=old_stage_user_dn, new_rdn=new_stage_user_rdn, new_superior=STAGE_DN)
  203. _find_memberof(topology, user_dn=new_stage_user_dn, group_dn=ACTIVE_GROUP_DN, find_result=False)
  204. _find_member (topology, user_dn=new_stage_user_dn, group_dn=ACTIVE_GROUP_DN, find_result=False)
  205. def test_ticket47833_final(topology):
  206. log.info('Testcase PASSED')
  207. def run_isolated():
  208. global installation1_prefix
  209. installation1_prefix = None
  210. topo = topology(True)
  211. test_ticket47829_init(topo)
  212. test_ticket47829_mod_stage_user_modrdn_stage_user_1(topo)
  213. test_ticket47833_final(topo)
  214. if __name__ == '__main__':
  215. run_isolated()