ticket47653MMR_test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. # clear the tmp directory
  131. master1.clearTmpDir(__file__)
  132. # Here we have two instances master and consumer
  133. # with replication working.
  134. return TopologyMaster1Master2(master1, master2)
  135. def test_ticket47653_init(topology):
  136. """
  137. It adds
  138. - Objectclass with MAY 'member'
  139. - an entry ('bind_entry') with which we bind to test the 'SELFDN' operation
  140. It deletes the anonymous aci
  141. """
  142. topology.master1.log.info("Add %s that allows 'member' attribute" % OC_NAME)
  143. new_oc = _oc_definition(2, OC_NAME, must=MUST, may=MAY)
  144. topology.master1.schema.add_schema('objectClasses', new_oc)
  145. # entry used to bind with
  146. topology.master1.log.info("Add %s" % BIND_DN)
  147. topology.master1.add_s(Entry((BIND_DN, {
  148. 'objectclass': "top person".split(),
  149. 'sn': BIND_NAME,
  150. 'cn': BIND_NAME,
  151. 'userpassword': BIND_PW})))
  152. # enable acl error logging
  153. mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', str(128 + 8192))] # ACL + REPL
  154. topology.master1.modify_s(DN_CONFIG, mod)
  155. topology.master2.modify_s(DN_CONFIG, mod)
  156. # get read of anonymous ACI for use 'read-search' aci in SEARCH test
  157. ACI_ANONYMOUS = "(targetattr!=\"userPassword\")(version 3.0; acl \"Enable anonymous access\"; allow (read, search, compare) userdn=\"ldap:///anyone\";)"
  158. mod = [(ldap.MOD_DELETE, 'aci', ACI_ANONYMOUS)]
  159. topology.master1.modify_s(SUFFIX, mod)
  160. topology.master2.modify_s(SUFFIX, mod)
  161. # add dummy entries
  162. for cpt in range(MAX_OTHERS):
  163. name = "%s%d" % (OTHER_NAME, cpt)
  164. topology.master1.add_s(Entry(("cn=%s,%s" % (name, SUFFIX), {
  165. 'objectclass': "top person".split(),
  166. 'sn': name,
  167. 'cn': name})))
  168. def test_ticket47653_add(topology):
  169. '''
  170. This test ADD an entry on MASTER1 where 47653 is fixed. Then it checks that entry is replicated
  171. on MASTER2 (even if on MASTER2 47653 is NOT fixed). Then update on MASTER2 and check the update on MASTER1
  172. It checks that, bound as bind_entry,
  173. - we can not ADD an entry without the proper SELFDN aci.
  174. - with the proper ACI we can not ADD with 'member' attribute
  175. - with the proper ACI and 'member' it succeeds to ADD
  176. '''
  177. topology.master1.log.info("\n\n######################### ADD ######################\n")
  178. # bind as bind_entry
  179. topology.master1.log.info("Bind as %s" % BIND_DN)
  180. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  181. # Prepare the entry with multivalued members
  182. entry_with_members = Entry(ENTRY_DN)
  183. entry_with_members.setValues('objectclass', 'top', 'person', 'OCticket47653')
  184. entry_with_members.setValues('sn', ENTRY_NAME)
  185. entry_with_members.setValues('cn', ENTRY_NAME)
  186. entry_with_members.setValues('postalAddress', 'here')
  187. entry_with_members.setValues('postalCode', '1234')
  188. members = []
  189. for cpt in range(MAX_OTHERS):
  190. name = "%s%d" % (OTHER_NAME, cpt)
  191. members.append("cn=%s,%s" % (name, SUFFIX))
  192. members.append(BIND_DN)
  193. entry_with_members.setValues('member', members)
  194. # Prepare the entry with only one member value
  195. entry_with_member = Entry(ENTRY_DN)
  196. entry_with_member.setValues('objectclass', 'top', 'person', 'OCticket47653')
  197. entry_with_member.setValues('sn', ENTRY_NAME)
  198. entry_with_member.setValues('cn', ENTRY_NAME)
  199. entry_with_member.setValues('postalAddress', 'here')
  200. entry_with_member.setValues('postalCode', '1234')
  201. member = []
  202. member.append(BIND_DN)
  203. entry_with_member.setValues('member', member)
  204. # entry to add WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
  205. try:
  206. topology.master1.log.info("Try to add Add %s (aci is missing): %r" % (ENTRY_DN, entry_with_member))
  207. topology.master1.add_s(entry_with_member)
  208. except Exception as e:
  209. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  210. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  211. # Ok Now add the proper ACI
  212. topology.master1.log.info("Bind as %s and add the ADD SELFDN aci" % DN_DM)
  213. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  214. ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
  215. ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
  216. ACI_ALLOW = "(version 3.0; acl \"SelfDN add\"; allow (add)"
  217. ACI_SUBJECT = " userattr = \"member#selfDN\";)"
  218. ACI_BODY = ACI_TARGET + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
  219. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  220. topology.master1.modify_s(SUFFIX, mod)
  221. time.sleep(1)
  222. # bind as bind_entry
  223. topology.master1.log.info("Bind as %s" % BIND_DN)
  224. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  225. # entry to add WITHOUT member and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
  226. try:
  227. topology.master1.log.info("Try to add Add %s (member is missing)" % ENTRY_DN)
  228. topology.master1.add_s(Entry((ENTRY_DN, {
  229. 'objectclass': ENTRY_OC.split(),
  230. 'sn': ENTRY_NAME,
  231. 'cn': ENTRY_NAME,
  232. 'postalAddress': 'here',
  233. 'postalCode': '1234'})))
  234. except Exception as e:
  235. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  236. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  237. # entry to add WITH memberS and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
  238. # member should contain only one value
  239. try:
  240. topology.master1.log.info("Try to add Add %s (with several member values)" % ENTRY_DN)
  241. topology.master1.add_s(entry_with_members)
  242. except Exception as e:
  243. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  244. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  245. topology.master1.log.info("Try to add Add %s should be successful" % ENTRY_DN)
  246. try:
  247. topology.master1.add_s(entry_with_member)
  248. except ldap.LDAPError as e:
  249. topology.master1.log.info("Failed to add entry, error: " + e.message['desc'])
  250. assert False
  251. #
  252. # Now check the entry as been replicated
  253. #
  254. topology.master2.simple_bind_s(DN_DM, PASSWORD)
  255. topology.master1.log.info("Try to retrieve %s from Master2" % ENTRY_DN)
  256. loop = 0
  257. while loop <= 10:
  258. try:
  259. ent = topology.master2.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  260. break
  261. except ldap.NO_SUCH_OBJECT:
  262. time.sleep(1)
  263. loop += 1
  264. assert loop <= 10
  265. # Now update the entry on Master2 (as DM because 47653 is possibly not fixed on M2)
  266. topology.master1.log.info("Update %s on M2" % ENTRY_DN)
  267. mod = [(ldap.MOD_REPLACE, 'description', 'test_add')]
  268. topology.master2.modify_s(ENTRY_DN, mod)
  269. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  270. loop = 0
  271. while loop <= 10:
  272. try:
  273. ent = topology.master1.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  274. if ent.hasAttr('description') and (ent.getValue('description') == 'test_add'):
  275. break
  276. except ldap.NO_SUCH_OBJECT:
  277. time.sleep(1)
  278. loop += 1
  279. assert ent.getValue('description') == 'test_add'
  280. def test_ticket47653_modify(topology):
  281. '''
  282. This test MOD an entry on MASTER1 where 47653 is fixed. Then it checks that update is replicated
  283. on MASTER2 (even if on MASTER2 47653 is NOT fixed). Then update on MASTER2 (bound as BIND_DN).
  284. This update may fail whether or not 47653 is fixed on MASTER2
  285. It checks that, bound as bind_entry,
  286. - we can not modify an entry without the proper SELFDN aci.
  287. - adding the ACI, we can modify the entry
  288. '''
  289. # bind as bind_entry
  290. topology.master1.log.info("Bind as %s" % BIND_DN)
  291. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  292. topology.master1.log.info("\n\n######################### MODIFY ######################\n")
  293. # entry to modify WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
  294. try:
  295. topology.master1.log.info("Try to modify %s (aci is missing)" % ENTRY_DN)
  296. mod = [(ldap.MOD_REPLACE, 'postalCode', '9876')]
  297. topology.master1.modify_s(ENTRY_DN, mod)
  298. except Exception as e:
  299. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  300. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  301. # Ok Now add the proper ACI
  302. topology.master1.log.info("Bind as %s and add the WRITE SELFDN aci" % DN_DM)
  303. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  304. ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
  305. ACI_TARGETATTR = "(targetattr = *)"
  306. ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
  307. ACI_ALLOW = "(version 3.0; acl \"SelfDN write\"; allow (write)"
  308. ACI_SUBJECT = " userattr = \"member#selfDN\";)"
  309. ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
  310. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  311. topology.master1.modify_s(SUFFIX, mod)
  312. time.sleep(1)
  313. # bind as bind_entry
  314. topology.master1.log.info("M1: Bind as %s" % BIND_DN)
  315. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  316. # modify the entry and checks the value
  317. topology.master1.log.info("M1: Try to modify %s. It should succeeds" % ENTRY_DN)
  318. mod = [(ldap.MOD_REPLACE, 'postalCode', '1928')]
  319. topology.master1.modify_s(ENTRY_DN, mod)
  320. topology.master1.log.info("M1: Bind as %s" % DN_DM)
  321. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  322. topology.master1.log.info("M1: Check the update of %s" % ENTRY_DN)
  323. ents = topology.master1.search_s(ENTRY_DN, ldap.SCOPE_BASE, 'objectclass=*')
  324. assert len(ents) == 1
  325. assert ents[0].postalCode == '1928'
  326. # Now check the update has been replicated on M2
  327. topology.master1.log.info("M2: Bind as %s" % DN_DM)
  328. topology.master2.simple_bind_s(DN_DM, PASSWORD)
  329. topology.master1.log.info("M2: Try to retrieve %s" % ENTRY_DN)
  330. loop = 0
  331. while loop <= 10:
  332. try:
  333. ent = topology.master2.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  334. if ent.hasAttr('postalCode') and (ent.getValue('postalCode') == '1928'):
  335. break
  336. except ldap.NO_SUCH_OBJECT:
  337. time.sleep(1)
  338. loop += 1
  339. assert loop <= 10
  340. assert ent.getValue('postalCode') == '1928'
  341. # Now update the entry on Master2 bound as BIND_DN (update may fail if 47653 is not fixed on M2)
  342. topology.master1.log.info("M2: Update %s (bound as %s)" % (ENTRY_DN, BIND_DN))
  343. topology.master2.simple_bind_s(BIND_DN, PASSWORD)
  344. fail = False
  345. try:
  346. mod = [(ldap.MOD_REPLACE, 'postalCode', '1929')]
  347. topology.master2.modify_s(ENTRY_DN, mod)
  348. fail = False
  349. except ldap.INSUFFICIENT_ACCESS:
  350. topology.master1.log.info("M2: Exception (INSUFFICIENT_ACCESS): that is fine the bug is possibly not fixed on M2")
  351. fail = True
  352. except Exception as e:
  353. topology.master1.log.info("M2: Exception (not expected): %s" % type(e).__name__)
  354. assert 0
  355. if not fail:
  356. # Check the update has been replicaed on M1
  357. topology.master1.log.info("M1: Bind as %s" % DN_DM)
  358. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  359. topology.master1.log.info("M1: Check %s.postalCode=1929)" % (ENTRY_DN))
  360. loop = 0
  361. while loop <= 10:
  362. try:
  363. ent = topology.master1.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  364. if ent.hasAttr('postalCode') and (ent.getValue('postalCode') == '1929'):
  365. break
  366. except ldap.NO_SUCH_OBJECT:
  367. time.sleep(1)
  368. loop += 1
  369. assert ent.getValue('postalCode') == '1929'
  370. def test_ticket47653_final(topology):
  371. topology.master1.delete()
  372. topology.master2.delete()
  373. log.info('Testcase PASSED')
  374. def run_isolated():
  375. '''
  376. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  377. To run isolated without py.test, you need to
  378. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  379. - set the installation prefix
  380. - run this program
  381. '''
  382. global installation1_prefix
  383. global installation2_prefix
  384. installation1_prefix = None
  385. installation2_prefix = None
  386. topo = topology(True)
  387. test_ticket47653_init(topo)
  388. test_ticket47653_add(topo)
  389. test_ticket47653_modify(topo)
  390. test_ticket47653_final(topo)
  391. if __name__ == '__main__':
  392. run_isolated()