ticket47653MMR_test.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. '''
  10. Created on Nov 7, 2013
  11. @author: tbordaz
  12. '''
  13. import os
  14. import sys
  15. import time
  16. import ldap
  17. import logging
  18. import pytest
  19. from lib389 import DirSrv, Entry, tools
  20. from lib389.tools import DirSrvTools
  21. from lib389._constants import *
  22. from lib389.properties import *
  23. logging.getLogger(__name__).setLevel(logging.DEBUG)
  24. log = logging.getLogger(__name__)
  25. #
  26. # important part. We can deploy Master1 and Master2 on different versions
  27. #
  28. installation1_prefix = None
  29. installation2_prefix = None
  30. TEST_REPL_DN = "cn=test_repl, %s" % SUFFIX
  31. OC_NAME = 'OCticket47653'
  32. MUST = "(postalAddress $ postalCode)"
  33. MAY = "(member $ street)"
  34. OTHER_NAME = 'other_entry'
  35. MAX_OTHERS = 10
  36. BIND_NAME = 'bind_entry'
  37. BIND_DN = 'cn=%s, %s' % (BIND_NAME, SUFFIX)
  38. BIND_PW = 'password'
  39. ENTRY_NAME = 'test_entry'
  40. ENTRY_DN = 'cn=%s, %s' % (ENTRY_NAME, SUFFIX)
  41. ENTRY_OC = "top person %s" % OC_NAME
  42. def _oc_definition(oid_ext, name, must=None, may=None):
  43. oid = "1.2.3.4.5.6.7.8.9.10.%d" % oid_ext
  44. desc = 'To test ticket 47490'
  45. sup = 'person'
  46. if not must:
  47. must = MUST
  48. if not may:
  49. may = MAY
  50. new_oc = "( %s NAME '%s' DESC '%s' SUP %s AUXILIARY MUST %s MAY %s )" % (oid, name, desc, sup, must, may)
  51. return new_oc
  52. class TopologyMaster1Master2(object):
  53. def __init__(self, master1, master2):
  54. master1.open()
  55. self.master1 = master1
  56. master2.open()
  57. self.master2 = master2
  58. @pytest.fixture(scope="module")
  59. def topology(request):
  60. '''
  61. This fixture is used to create a replicated topology for the 'module'.
  62. The replicated topology is MASTER1 <-> Master2.
  63. '''
  64. global installation1_prefix
  65. global installation2_prefix
  66. # allocate master1 on a given deployement
  67. master1 = DirSrv(verbose=False)
  68. if installation1_prefix:
  69. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  70. # Args for the master1 instance
  71. args_instance[SER_HOST] = HOST_MASTER_1
  72. args_instance[SER_PORT] = PORT_MASTER_1
  73. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_1
  74. args_master = args_instance.copy()
  75. master1.allocate(args_master)
  76. # allocate master1 on a given deployement
  77. master2 = DirSrv(verbose=False)
  78. if installation2_prefix:
  79. args_instance[SER_DEPLOYED_DIR] = installation2_prefix
  80. # Args for the consumer instance
  81. args_instance[SER_HOST] = HOST_MASTER_2
  82. args_instance[SER_PORT] = PORT_MASTER_2
  83. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_2
  84. args_master = args_instance.copy()
  85. master2.allocate(args_master)
  86. # Get the status of the instance and restart it if it exists
  87. instance_master1 = master1.exists()
  88. instance_master2 = master2.exists()
  89. # Remove all the instances
  90. if instance_master1:
  91. master1.delete()
  92. if instance_master2:
  93. master2.delete()
  94. # Create the instances
  95. master1.create()
  96. master1.open()
  97. master2.create()
  98. master2.open()
  99. #
  100. # Now prepare the Master-Consumer topology
  101. #
  102. # First Enable replication
  103. master1.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER, replicaId=REPLICAID_MASTER_1)
  104. master2.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER, replicaId=REPLICAID_MASTER_2)
  105. # Initialize the supplier->consumer
  106. properties = {RA_NAME: r'meTo_$host:$port',
  107. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  108. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  109. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  110. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  111. repl_agreement = master1.agreement.create(suffix=SUFFIX, host=master2.host, port=master2.port, properties=properties)
  112. if not repl_agreement:
  113. log.fatal("Fail to create a replica agreement")
  114. sys.exit(1)
  115. log.debug("%s created" % repl_agreement)
  116. properties = {RA_NAME: r'meTo_$host:$port',
  117. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  118. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  119. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  120. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  121. master2.agreement.create(suffix=SUFFIX, host=master1.host, port=master1.port, properties=properties)
  122. master1.agreement.init(SUFFIX, HOST_MASTER_2, PORT_MASTER_2)
  123. master1.waitForReplInit(repl_agreement)
  124. # Check replication is working fine
  125. if master1.testReplication(DEFAULT_SUFFIX, master2):
  126. log.info('Replication is working.')
  127. else:
  128. log.fatal('Replication is not working.')
  129. assert False
  130. def fin():
  131. master1.delete()
  132. master2.delete()
  133. request.addfinalizer(fin)
  134. # Here we have two instances master and consumer
  135. # with replication working.
  136. return TopologyMaster1Master2(master1, master2)
  137. def test_ticket47653_init(topology):
  138. """
  139. It adds
  140. - Objectclass with MAY 'member'
  141. - an entry ('bind_entry') with which we bind to test the 'SELFDN' operation
  142. It deletes the anonymous aci
  143. """
  144. topology.master1.log.info("Add %s that allows 'member' attribute" % OC_NAME)
  145. new_oc = _oc_definition(2, OC_NAME, must=MUST, may=MAY)
  146. topology.master1.schema.add_schema('objectClasses', new_oc)
  147. # entry used to bind with
  148. topology.master1.log.info("Add %s" % BIND_DN)
  149. topology.master1.add_s(Entry((BIND_DN, {
  150. 'objectclass': "top person".split(),
  151. 'sn': BIND_NAME,
  152. 'cn': BIND_NAME,
  153. 'userpassword': BIND_PW})))
  154. # enable acl error logging
  155. mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', str(128 + 8192))] # ACL + REPL
  156. topology.master1.modify_s(DN_CONFIG, mod)
  157. topology.master2.modify_s(DN_CONFIG, mod)
  158. # remove all aci's and start with a clean slate
  159. mod = [(ldap.MOD_DELETE, 'aci', None)]
  160. topology.master1.modify_s(SUFFIX, mod)
  161. topology.master2.modify_s(SUFFIX, mod)
  162. # add dummy entries
  163. for cpt in range(MAX_OTHERS):
  164. name = "%s%d" % (OTHER_NAME, cpt)
  165. topology.master1.add_s(Entry(("cn=%s,%s" % (name, SUFFIX), {
  166. 'objectclass': "top person".split(),
  167. 'sn': name,
  168. 'cn': name})))
  169. def test_ticket47653_add(topology):
  170. '''
  171. This test ADD an entry on MASTER1 where 47653 is fixed. Then it checks that entry is replicated
  172. on MASTER2 (even if on MASTER2 47653 is NOT fixed). Then update on MASTER2 and check the update on MASTER1
  173. It checks that, bound as bind_entry,
  174. - we can not ADD an entry without the proper SELFDN aci.
  175. - with the proper ACI we can not ADD with 'member' attribute
  176. - with the proper ACI and 'member' it succeeds to ADD
  177. '''
  178. topology.master1.log.info("\n\n######################### ADD ######################\n")
  179. # bind as bind_entry
  180. topology.master1.log.info("Bind as %s" % BIND_DN)
  181. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  182. # Prepare the entry with multivalued members
  183. entry_with_members = Entry(ENTRY_DN)
  184. entry_with_members.setValues('objectclass', 'top', 'person', 'OCticket47653')
  185. entry_with_members.setValues('sn', ENTRY_NAME)
  186. entry_with_members.setValues('cn', ENTRY_NAME)
  187. entry_with_members.setValues('postalAddress', 'here')
  188. entry_with_members.setValues('postalCode', '1234')
  189. members = []
  190. for cpt in range(MAX_OTHERS):
  191. name = "%s%d" % (OTHER_NAME, cpt)
  192. members.append("cn=%s,%s" % (name, SUFFIX))
  193. members.append(BIND_DN)
  194. entry_with_members.setValues('member', members)
  195. # Prepare the entry with only one member value
  196. entry_with_member = Entry(ENTRY_DN)
  197. entry_with_member.setValues('objectclass', 'top', 'person', 'OCticket47653')
  198. entry_with_member.setValues('sn', ENTRY_NAME)
  199. entry_with_member.setValues('cn', ENTRY_NAME)
  200. entry_with_member.setValues('postalAddress', 'here')
  201. entry_with_member.setValues('postalCode', '1234')
  202. member = []
  203. member.append(BIND_DN)
  204. entry_with_member.setValues('member', member)
  205. # entry to add WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
  206. try:
  207. topology.master1.log.info("Try to add Add %s (aci is missing): %r" % (ENTRY_DN, entry_with_member))
  208. topology.master1.add_s(entry_with_member)
  209. except Exception as e:
  210. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  211. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  212. # Ok Now add the proper ACI
  213. topology.master1.log.info("Bind as %s and add the ADD SELFDN aci" % DN_DM)
  214. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  215. ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
  216. ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
  217. ACI_ALLOW = "(version 3.0; acl \"SelfDN add\"; allow (add)"
  218. ACI_SUBJECT = " userattr = \"member#selfDN\";)"
  219. ACI_BODY = ACI_TARGET + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
  220. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  221. topology.master1.modify_s(SUFFIX, mod)
  222. time.sleep(1)
  223. # bind as bind_entry
  224. topology.master1.log.info("Bind as %s" % BIND_DN)
  225. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  226. # entry to add WITHOUT member and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
  227. try:
  228. topology.master1.log.info("Try to add Add %s (member is missing)" % ENTRY_DN)
  229. topology.master1.add_s(Entry((ENTRY_DN, {
  230. 'objectclass': ENTRY_OC.split(),
  231. 'sn': ENTRY_NAME,
  232. 'cn': ENTRY_NAME,
  233. 'postalAddress': 'here',
  234. 'postalCode': '1234'})))
  235. except Exception as e:
  236. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  237. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  238. # entry to add WITH memberS and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
  239. # member should contain only one value
  240. try:
  241. topology.master1.log.info("Try to add Add %s (with several member values)" % ENTRY_DN)
  242. topology.master1.add_s(entry_with_members)
  243. except Exception as e:
  244. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  245. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  246. topology.master1.log.info("Try to add Add %s should be successful" % ENTRY_DN)
  247. try:
  248. topology.master1.add_s(entry_with_member)
  249. except ldap.LDAPError as e:
  250. topology.master1.log.info("Failed to add entry, error: " + e.message['desc'])
  251. assert False
  252. #
  253. # Now check the entry as been replicated
  254. #
  255. topology.master2.simple_bind_s(DN_DM, PASSWORD)
  256. topology.master1.log.info("Try to retrieve %s from Master2" % ENTRY_DN)
  257. loop = 0
  258. while loop <= 10:
  259. try:
  260. ent = topology.master2.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  261. break
  262. except ldap.NO_SUCH_OBJECT:
  263. time.sleep(1)
  264. loop += 1
  265. assert loop <= 10
  266. # Now update the entry on Master2 (as DM because 47653 is possibly not fixed on M2)
  267. topology.master1.log.info("Update %s on M2" % ENTRY_DN)
  268. mod = [(ldap.MOD_REPLACE, 'description', 'test_add')]
  269. topology.master2.modify_s(ENTRY_DN, mod)
  270. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  271. loop = 0
  272. while loop <= 10:
  273. try:
  274. ent = topology.master1.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  275. if ent.hasAttr('description') and (ent.getValue('description') == 'test_add'):
  276. break
  277. except ldap.NO_SUCH_OBJECT:
  278. time.sleep(1)
  279. loop += 1
  280. assert ent.getValue('description') == 'test_add'
  281. def test_ticket47653_modify(topology):
  282. '''
  283. This test MOD an entry on MASTER1 where 47653 is fixed. Then it checks that update is replicated
  284. on MASTER2 (even if on MASTER2 47653 is NOT fixed). Then update on MASTER2 (bound as BIND_DN).
  285. This update may fail whether or not 47653 is fixed on MASTER2
  286. It checks that, bound as bind_entry,
  287. - we can not modify an entry without the proper SELFDN aci.
  288. - adding the ACI, we can modify the entry
  289. '''
  290. # bind as bind_entry
  291. topology.master1.log.info("Bind as %s" % BIND_DN)
  292. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  293. topology.master1.log.info("\n\n######################### MODIFY ######################\n")
  294. # entry to modify WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
  295. try:
  296. topology.master1.log.info("Try to modify %s (aci is missing)" % ENTRY_DN)
  297. mod = [(ldap.MOD_REPLACE, 'postalCode', '9876')]
  298. topology.master1.modify_s(ENTRY_DN, mod)
  299. except Exception as e:
  300. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  301. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  302. # Ok Now add the proper ACI
  303. topology.master1.log.info("Bind as %s and add the WRITE SELFDN aci" % DN_DM)
  304. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  305. ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
  306. ACI_TARGETATTR = "(targetattr = *)"
  307. ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
  308. ACI_ALLOW = "(version 3.0; acl \"SelfDN write\"; allow (write)"
  309. ACI_SUBJECT = " userattr = \"member#selfDN\";)"
  310. ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
  311. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  312. topology.master1.modify_s(SUFFIX, mod)
  313. time.sleep(1)
  314. # bind as bind_entry
  315. topology.master1.log.info("M1: Bind as %s" % BIND_DN)
  316. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  317. # modify the entry and checks the value
  318. topology.master1.log.info("M1: Try to modify %s. It should succeeds" % ENTRY_DN)
  319. mod = [(ldap.MOD_REPLACE, 'postalCode', '1928')]
  320. topology.master1.modify_s(ENTRY_DN, mod)
  321. topology.master1.log.info("M1: Bind as %s" % DN_DM)
  322. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  323. topology.master1.log.info("M1: Check the update of %s" % ENTRY_DN)
  324. ents = topology.master1.search_s(ENTRY_DN, ldap.SCOPE_BASE, 'objectclass=*')
  325. assert len(ents) == 1
  326. assert ents[0].postalCode == '1928'
  327. # Now check the update has been replicated on M2
  328. topology.master1.log.info("M2: Bind as %s" % DN_DM)
  329. topology.master2.simple_bind_s(DN_DM, PASSWORD)
  330. topology.master1.log.info("M2: Try to retrieve %s" % ENTRY_DN)
  331. loop = 0
  332. while loop <= 10:
  333. try:
  334. ent = topology.master2.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  335. if ent.hasAttr('postalCode') and (ent.getValue('postalCode') == '1928'):
  336. break
  337. except ldap.NO_SUCH_OBJECT:
  338. time.sleep(1)
  339. loop += 1
  340. assert loop <= 10
  341. assert ent.getValue('postalCode') == '1928'
  342. # Now update the entry on Master2 bound as BIND_DN (update may fail if 47653 is not fixed on M2)
  343. topology.master1.log.info("M2: Update %s (bound as %s)" % (ENTRY_DN, BIND_DN))
  344. topology.master2.simple_bind_s(BIND_DN, PASSWORD)
  345. fail = False
  346. try:
  347. mod = [(ldap.MOD_REPLACE, 'postalCode', '1929')]
  348. topology.master2.modify_s(ENTRY_DN, mod)
  349. fail = False
  350. except ldap.INSUFFICIENT_ACCESS:
  351. topology.master1.log.info("M2: Exception (INSUFFICIENT_ACCESS): that is fine the bug is possibly not fixed on M2")
  352. fail = True
  353. except Exception as e:
  354. topology.master1.log.info("M2: Exception (not expected): %s" % type(e).__name__)
  355. assert 0
  356. if not fail:
  357. # Check the update has been replicaed on M1
  358. topology.master1.log.info("M1: Bind as %s" % DN_DM)
  359. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  360. topology.master1.log.info("M1: Check %s.postalCode=1929)" % (ENTRY_DN))
  361. loop = 0
  362. while loop <= 10:
  363. try:
  364. ent = topology.master1.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  365. if ent.hasAttr('postalCode') and (ent.getValue('postalCode') == '1929'):
  366. break
  367. except ldap.NO_SUCH_OBJECT:
  368. time.sleep(1)
  369. loop += 1
  370. assert ent.getValue('postalCode') == '1929'
  371. def test_ticket47653_final(topology):
  372. log.info('Testcase PASSED')
  373. def run_isolated():
  374. '''
  375. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  376. To run isolated without py.test, you need to
  377. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  378. - set the installation prefix
  379. - run this program
  380. '''
  381. global installation1_prefix
  382. global installation2_prefix
  383. installation1_prefix = None
  384. installation2_prefix = None
  385. topo = topology(True)
  386. test_ticket47653_init(topo)
  387. test_ticket47653_add(topo)
  388. test_ticket47653_modify(topo)
  389. test_ticket47653_final(topo)
  390. if __name__ == '__main__':
  391. run_isolated()