ticket47653MMR_test.py 18 KB

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