ticket48784_test.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2016 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 shlex
  13. import subprocess
  14. import ldap
  15. import logging
  16. import pytest
  17. import base64
  18. from lib389 import DirSrv, Entry, tools, tasks
  19. from lib389.tools import DirSrvTools
  20. from lib389._constants import *
  21. from lib389.properties import *
  22. from lib389.tasks import *
  23. from lib389.utils import *
  24. logging.getLogger(__name__).setLevel(logging.DEBUG)
  25. log = logging.getLogger(__name__)
  26. installation1_prefix = None
  27. CONFIG_DN = 'cn=config'
  28. ENCRYPTION_DN = 'cn=encryption,%s' % CONFIG_DN
  29. RSA = 'RSA'
  30. RSA_DN = 'cn=%s,%s' % (RSA, ENCRYPTION_DN)
  31. ISSUER = 'cn=CAcert'
  32. CACERT = 'CAcertificate'
  33. M1SERVERCERT = 'Server-Cert1'
  34. M2SERVERCERT = 'Server-Cert2'
  35. M1LDAPSPORT = '41636'
  36. M2LDAPSPORT = '42636'
  37. class TopologyReplication(object):
  38. def __init__(self, master1, master2):
  39. master1.open()
  40. self.master1 = master1
  41. master2.open()
  42. self.master2 = master2
  43. @pytest.fixture(scope="module")
  44. def topology(request):
  45. global installation1_prefix
  46. if installation1_prefix:
  47. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  48. # Creating master 1...
  49. master1 = DirSrv(verbose=False)
  50. if installation1_prefix:
  51. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  52. args_instance[SER_HOST] = HOST_MASTER_1
  53. args_instance[SER_PORT] = PORT_MASTER_1
  54. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_1
  55. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  56. args_master = args_instance.copy()
  57. master1.allocate(args_master)
  58. instance_master1 = master1.exists()
  59. if instance_master1:
  60. master1.delete()
  61. master1.create()
  62. master1.open()
  63. master1.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER, replicaId=REPLICAID_MASTER_1)
  64. # Creating master 2...
  65. master2 = DirSrv(verbose=False)
  66. if installation1_prefix:
  67. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  68. args_instance[SER_HOST] = HOST_MASTER_2
  69. args_instance[SER_PORT] = PORT_MASTER_2
  70. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_2
  71. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  72. args_master = args_instance.copy()
  73. master2.allocate(args_master)
  74. instance_master2 = master2.exists()
  75. if instance_master2:
  76. master2.delete()
  77. master2.create()
  78. master2.open()
  79. master2.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER, replicaId=REPLICAID_MASTER_2)
  80. #
  81. # Create all the agreements
  82. #
  83. # Creating agreement from master 1 to master 2
  84. properties = {RA_NAME: r'meTo_%s:%s' % (master2.host, master2.port),
  85. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  86. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  87. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  88. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  89. global m1_m2_agmt
  90. m1_m2_agmt = master1.agreement.create(suffix=SUFFIX, host=master2.host, port=master2.port, properties=properties)
  91. if not m1_m2_agmt:
  92. log.fatal("Fail to create a master -> master replica agreement")
  93. sys.exit(1)
  94. log.debug("%s created" % m1_m2_agmt)
  95. # Creating agreement from master 2 to master 1
  96. properties = {RA_NAME: r'meTo_%s:%s' % (master1.host, master1.port),
  97. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  98. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  99. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  100. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  101. global m2_m1_agmt
  102. m2_m1_agmt = master2.agreement.create(suffix=SUFFIX, host=master1.host, port=master1.port, properties=properties)
  103. if not m2_m1_agmt:
  104. log.fatal("Fail to create a master -> master replica agreement")
  105. sys.exit(1)
  106. log.debug("%s created" % m2_m1_agmt)
  107. # Allow the replicas to get situated with the new agreements...
  108. time.sleep(2)
  109. global M1SUBJECT
  110. M1SUBJECT = 'CN=%s,OU=389 Directory Server' % (master1.host)
  111. global M2SUBJECT
  112. M2SUBJECT = 'CN=%s,OU=390 Directory Server' % (master2.host)
  113. #
  114. # Initialize all the agreements
  115. #
  116. master1.agreement.init(SUFFIX, HOST_MASTER_2, PORT_MASTER_2)
  117. master1.waitForReplInit(m1_m2_agmt)
  118. # Check replication is working...
  119. if master1.testReplication(DEFAULT_SUFFIX, master2):
  120. log.info('Replication is working.')
  121. else:
  122. log.fatal('Replication is not working.')
  123. assert False
  124. # Delete each instance in the end
  125. def fin():
  126. master1.delete()
  127. master2.delete()
  128. request.addfinalizer(fin)
  129. return TopologyReplication(master1, master2)
  130. @pytest.fixture(scope="module")
  131. def add_entry(server, name, rdntmpl, start, num):
  132. log.info("\n######################### Adding %d entries to %s ######################" % (num, name))
  133. for i in range(num):
  134. ii = start + i
  135. dn = '%s%d,%s' % (rdntmpl, ii, DEFAULT_SUFFIX)
  136. try:
  137. server.add_s(Entry((dn, {'objectclass': 'top person extensibleObject'.split(),
  138. 'uid': '%s%d' % (rdntmpl, ii),
  139. 'cn': '%s user%d' % (name, ii),
  140. 'sn': 'user%d' % (ii)})))
  141. except ldap.LDAPError as e:
  142. log.error('Failed to add %s ' % dn + e.message['desc'])
  143. assert False
  144. def enable_ssl(server, ldapsport, mycert):
  145. log.info("\n######################### Enabling SSL LDAPSPORT %s ######################\n" % ldapsport)
  146. server.simple_bind_s(DN_DM, PASSWORD)
  147. server.modify_s(ENCRYPTION_DN, [(ldap.MOD_REPLACE, 'nsSSL3', 'off'),
  148. (ldap.MOD_REPLACE, 'nsTLS1', 'on'),
  149. (ldap.MOD_REPLACE, 'nsSSLClientAuth', 'allowed'),
  150. (ldap.MOD_REPLACE, 'nsSSL3Ciphers', '+all')])
  151. server.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'nsslapd-security', 'on'),
  152. (ldap.MOD_REPLACE, 'nsslapd-ssl-check-hostname', 'off'),
  153. (ldap.MOD_REPLACE, 'nsslapd-secureport', ldapsport)])
  154. server.add_s(Entry((RSA_DN, {'objectclass': "top nsEncryptionModule".split(),
  155. 'cn': RSA,
  156. 'nsSSLPersonalitySSL': mycert,
  157. 'nsSSLToken': 'internal (software)',
  158. 'nsSSLActivation': 'on'})))
  159. time.sleep(1)
  160. def doAndPrintIt(cmdline, filename):
  161. proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  162. if filename is None:
  163. log.info(" OUT:")
  164. else:
  165. log.info(" OUT: %s" % filename)
  166. fd = open(filename, "w")
  167. while True:
  168. l = proc.stdout.readline()
  169. if l == "":
  170. break
  171. if filename is None:
  172. log.info(" %s" % l)
  173. else:
  174. fd.write(l)
  175. log.info(" ERR:")
  176. while True:
  177. l = proc.stderr.readline()
  178. if l == "" or l == "\n":
  179. break
  180. log.info(" <%s>" % l)
  181. assert False
  182. if filename is not None:
  183. fd.close()
  184. time.sleep(1)
  185. def create_keys_certs(topology):
  186. log.info("\n######################### Creating SSL Keys and Certs ######################\n")
  187. global m1confdir
  188. m1confdir = topology.master1.confdir
  189. global m2confdir
  190. m2confdir = topology.master2.confdir
  191. log.info("##### shutdown master1")
  192. topology.master1.stop(timeout=10)
  193. log.info("##### Creating a password file")
  194. pwdfile = '%s/pwdfile.txt' % (m1confdir)
  195. os.system('rm -f %s' % pwdfile)
  196. opasswd = os.popen("(ps -ef ; w ) | sha1sum | awk '{print $1}'", "r")
  197. passwd = opasswd.readline()
  198. pwdfd = open(pwdfile, "w")
  199. pwdfd.write(passwd)
  200. pwdfd.close()
  201. time.sleep(1)
  202. log.info("##### create the pin file")
  203. m1pinfile = '%s/pin.txt' % (m1confdir)
  204. m2pinfile = '%s/pin.txt' % (m2confdir)
  205. os.system('rm -f %s' % m1pinfile)
  206. os.system('rm -f %s' % m2pinfile)
  207. pintxt = 'Internal (Software) Token:%s' % passwd
  208. pinfd = open(m1pinfile, "w")
  209. pinfd.write(pintxt)
  210. pinfd.close()
  211. os.system('chmod 400 %s' % m1pinfile)
  212. log.info("##### Creating a noise file")
  213. noisefile = '%s/noise.txt' % (m1confdir)
  214. noise = os.popen("(w ; ps -ef ; date ) | sha1sum | awk '{print $1}'", "r")
  215. noisewdfd = open(noisefile, "w")
  216. noisewdfd.write(noise.readline())
  217. noisewdfd.close()
  218. time.sleep(1)
  219. cmdline = ['certutil', '-N', '-d', m1confdir, '-f', pwdfile]
  220. log.info("##### Create key3.db and cert8.db database (master1): %s" % cmdline)
  221. doAndPrintIt(cmdline, None)
  222. cmdline = ['certutil', '-G', '-d', m1confdir, '-z', noisefile, '-f', pwdfile]
  223. log.info("##### Creating encryption key for CA (master1): %s" % cmdline)
  224. #os.system('certutil -G -d %s -z %s -f %s' % (m1confdir, noisefile, pwdfile))
  225. doAndPrintIt(cmdline, None)
  226. time.sleep(2)
  227. log.info("##### Creating self-signed CA certificate (master1) -- nickname %s" % CACERT)
  228. os.system('( echo y ; echo ; echo y ) | certutil -S -n "%s" -s "%s" -x -t "CT,," -m 1000 -v 120 -d %s -z %s -f %s -2' % (CACERT, ISSUER, m1confdir, noisefile, pwdfile))
  229. global M1SUBJECT
  230. cmdline = ['certutil', '-S', '-n', M1SERVERCERT, '-s', M1SUBJECT, '-c', CACERT, '-t', ',,', '-m', '1001', '-v', '120', '-d', m1confdir, '-z', noisefile, '-f', pwdfile]
  231. log.info("##### Creating Server certificate -- nickname %s: %s" % (M1SERVERCERT, cmdline))
  232. doAndPrintIt(cmdline, None)
  233. time.sleep(2)
  234. global M2SUBJECT
  235. cmdline = ['certutil', '-S', '-n', M2SERVERCERT, '-s', M2SUBJECT, '-c', CACERT, '-t', ',,', '-m', '1002', '-v', '120', '-d', m1confdir, '-z', noisefile, '-f', pwdfile]
  236. log.info("##### Creating Server certificate -- nickname %s: %s" % (M2SERVERCERT, cmdline))
  237. doAndPrintIt(cmdline, None)
  238. time.sleep(2)
  239. log.info("##### start master1")
  240. topology.master1.start(timeout=10)
  241. log.info("##### enable SSL in master1 with all ciphers")
  242. enable_ssl(topology.master1, M1LDAPSPORT, M1SERVERCERT)
  243. cmdline = ['certutil', '-L', '-d', m1confdir]
  244. log.info("##### Check the cert db: %s" % cmdline)
  245. doAndPrintIt(cmdline, None)
  246. log.info("##### stop master[12]")
  247. topology.master1.stop(timeout=10)
  248. topology.master2.stop(timeout=10)
  249. global mytmp
  250. mytmp = '/tmp'
  251. m2pk12file = '%s/%s.pk12' % (mytmp, M2SERVERCERT)
  252. cmd = 'pk12util -o %s -n "%s" -d %s -w %s -k %s' % (m2pk12file, M2SERVERCERT, m1confdir, pwdfile, pwdfile)
  253. log.info("##### Extract PK12 file for master2: %s" % cmd)
  254. os.system(cmd)
  255. time.sleep(1)
  256. log.info("##### Check PK12 files")
  257. if os.path.isfile(m2pk12file):
  258. log.info('%s is successfully extracted.' % m2pk12file)
  259. else:
  260. log.fatal('%s was not extracted.' % m2pk12file)
  261. assert False
  262. log.info("##### Initialize Cert DB for master2")
  263. cmdline = ['certutil', '-N', '-d', m2confdir, '-f', pwdfile]
  264. log.info("##### Create key3.db and cert8.db database (master2): %s" % cmdline)
  265. doAndPrintIt(cmdline, None)
  266. log.info("##### Import certs to master2")
  267. log.info('Importing %s' % CACERT)
  268. cacert = '%s%s.pem' % (mytmp, CACERT)
  269. cmdline = ['certutil', '-L', '-n', CACERT, '-d', m1confdir, '-a']
  270. doAndPrintIt(cmdline, cacert)
  271. os.system('certutil -A -n "%s" -t "CT,," -f %s -d %s -a -i %s' % (CACERT, pwdfile, m2confdir, cacert))
  272. cmd = 'pk12util -i %s -n "%s" -d %s -w %s -k %s' % (m2pk12file, M2SERVERCERT, m2confdir, pwdfile, pwdfile)
  273. log.info('##### Importing %s to master2: %s' % (M2SERVERCERT, cmd))
  274. os.system(cmd)
  275. log.info('copy %s to %s' % (m1pinfile, m2pinfile))
  276. os.system('cp %s %s' % (m1pinfile, m2pinfile))
  277. os.system('chmod 400 %s' % m2pinfile)
  278. time.sleep(1)
  279. log.info("##### start master2")
  280. topology.master2.start(timeout=10)
  281. log.info("##### enable SSL in master2 with all ciphers")
  282. enable_ssl(topology.master2, M2LDAPSPORT, M2SERVERCERT)
  283. log.info("##### restart master2")
  284. topology.master2.restart(timeout=30)
  285. log.info("##### restart master1")
  286. topology.master1.restart(timeout=30)
  287. log.info("\n######################### Creating SSL Keys and Certs Done ######################\n")
  288. def config_tls_agreements(topology):
  289. log.info("######################### Configure SSL/TLS agreements ######################")
  290. log.info("######################## master1 <-- startTLS -> master2 #####################")
  291. log.info("##### Update the agreement of master1")
  292. global m1_m2_agmt
  293. topology.master1.modify_s(m1_m2_agmt, [(ldap.MOD_REPLACE, 'nsDS5ReplicaTransportInfo', 'TLS')])
  294. log.info("##### Update the agreement of master2")
  295. global m2_m1_agmt
  296. topology.master2.modify_s(m2_m1_agmt, [(ldap.MOD_REPLACE, 'nsDS5ReplicaTransportInfo', 'TLS')])
  297. time.sleep(1)
  298. topology.master1.restart(10)
  299. topology.master2.restart(10)
  300. log.info("\n######################### Configure SSL/TLS agreements Done ######################\n")
  301. def set_ssl_Version(server, name, version):
  302. log.info("\n######################### Set %s on %s ######################\n" %
  303. (version, name))
  304. server.simple_bind_s(DN_DM, PASSWORD)
  305. if version.startswith('SSL'):
  306. server.modify_s(ENCRYPTION_DN, [(ldap.MOD_REPLACE, 'nsSSL3', 'on'),
  307. (ldap.MOD_REPLACE, 'nsTLS1', 'off'),
  308. (ldap.MOD_REPLACE, 'sslVersionMin', 'SSL3'),
  309. (ldap.MOD_REPLACE, 'sslVersionMax', 'SSL3')])
  310. elif version.startswith('TLS'):
  311. server.modify_s(ENCRYPTION_DN, [(ldap.MOD_REPLACE, 'nsSSL3', 'off'),
  312. (ldap.MOD_REPLACE, 'nsTLS1', 'on'),
  313. (ldap.MOD_REPLACE, 'sslVersionMin', version),
  314. (ldap.MOD_REPLACE, 'sslVersionMax', version)])
  315. else:
  316. log.info("Invalid version %s", version)
  317. assert False
  318. def test_ticket48784(topology):
  319. """
  320. Set up 2way MMR:
  321. master_1 <----- startTLS -----> master_2
  322. Make sure the replication is working.
  323. Then, stop the servers and set only SSLv3 on master_1 while TLS1.2 on master_2
  324. Replication is supposed to fail.
  325. """
  326. log.info("Ticket 48784 - Allow usage of OpenLDAP libraries that don't use NSS for crypto")
  327. create_keys_certs(topology)
  328. config_tls_agreements(topology)
  329. add_entry(topology.master1, 'master1', 'uid=m1user', 0, 5)
  330. add_entry(topology.master2, 'master2', 'uid=m2user', 0, 5)
  331. time.sleep(10)
  332. log.info('##### Searching for entries on master1...')
  333. entries = topology.master1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)')
  334. assert 10 == len(entries)
  335. log.info('##### Searching for entries on master2...')
  336. entries = topology.master2.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)')
  337. assert 10 == len(entries)
  338. log.info("##### openldap client just accepts sslVersionMin not Max.")
  339. set_ssl_Version(topology.master1, 'master1', 'SSL3')
  340. set_ssl_Version(topology.master2, 'master2', 'TLS1.2')
  341. log.info("##### restart master[12]")
  342. topology.master1.restart(timeout=10)
  343. topology.master2.restart(timeout=10)
  344. log.info("##### replication from master_1 to master_2 should be ok.")
  345. add_entry(topology.master1, 'master1', 'uid=m1user', 10, 1)
  346. log.info("##### replication from master_2 to master_1 should fail.")
  347. add_entry(topology.master2, 'master2', 'uid=m2user', 10, 1)
  348. time.sleep(10)
  349. log.info('##### Searching for entries on master1...')
  350. entries = topology.master1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)')
  351. assert 11 == len(entries) # This is supposed to be "1" less than master 2's entry count
  352. log.info('##### Searching for entries on master2...')
  353. entries = topology.master2.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)')
  354. assert 12 == len(entries)
  355. log.info("Ticket 48784 - PASSED")
  356. if __name__ == '__main__':
  357. # Run isolated
  358. # -s for DEBUG mode
  359. CURRENT_FILE = os.path.realpath(__file__)
  360. pytest.main("-s %s" % CURRENT_FILE)