rootdn_plugin_test.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. import socket
  16. from lib389 import DirSrv, Entry, tools, tasks
  17. from lib389.tools import DirSrvTools
  18. from lib389._constants import *
  19. from lib389.properties import *
  20. from lib389.tasks import *
  21. logging.getLogger(__name__).setLevel(logging.DEBUG)
  22. log = logging.getLogger(__name__)
  23. installation1_prefix = None
  24. PLUGIN_DN = 'cn=' + PLUGIN_ROOTDN_ACCESS + ',cn=plugins,cn=config'
  25. USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
  26. class TopologyStandalone(object):
  27. def __init__(self, standalone):
  28. standalone.open()
  29. self.standalone = standalone
  30. @pytest.fixture(scope="module")
  31. def topology(request):
  32. global installation1_prefix
  33. if installation1_prefix:
  34. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  35. # Creating standalone instance ...
  36. standalone = DirSrv(verbose=False)
  37. args_instance[SER_HOST] = HOST_STANDALONE
  38. args_instance[SER_PORT] = PORT_STANDALONE
  39. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  40. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  41. args_standalone = args_instance.copy()
  42. standalone.allocate(args_standalone)
  43. instance_standalone = standalone.exists()
  44. if instance_standalone:
  45. standalone.delete()
  46. standalone.create()
  47. standalone.open()
  48. def fin():
  49. standalone.delete()
  50. request.addfinalizer(fin)
  51. return TopologyStandalone(standalone)
  52. def test_rootdn_init(topology):
  53. '''
  54. Initialize our setup to test the ROot DN Access Control Plugin
  55. Test the following access control type:
  56. - Allowed IP address *
  57. - Denied IP address *
  58. - Specific time window
  59. - Days allowed access
  60. - Allowed host *
  61. - Denied host *
  62. * means mulitple valued
  63. '''
  64. log.info('Initializing root DN test suite...')
  65. #
  66. # Set an aci so we can modify the plugin after we deny the Root DN
  67. #
  68. ACI = ('(target ="ldap:///cn=config")(targetattr = "*")(version 3.0' +
  69. ';acl "all access";allow (all)(userdn="ldap:///anyone");)')
  70. try:
  71. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_ADD, 'aci', ACI)])
  72. except ldap.LDAPError as e:
  73. log.fatal('test_rootdn_init: Failed to add aci to config: error ' +
  74. e.message['desc'])
  75. assert False
  76. #
  77. # Create a user to modify the config
  78. #
  79. try:
  80. topology.standalone.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
  81. 'uid': 'user1',
  82. 'userpassword': PASSWORD})))
  83. except ldap.LDAPError as e:
  84. log.fatal('test_rootdn_init: Failed to add test user ' + USER1_DN + ': error ' +
  85. e.message['desc'])
  86. assert False
  87. #
  88. # Enable dynamic plugins
  89. #
  90. try:
  91. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', 'on')])
  92. except ldap.LDAPError as e:
  93. log.fatal('test_rootdn_init: Failed to set dynamic plugins: error ' + e.message['desc'])
  94. assert False
  95. #
  96. # Enable the plugin (aftewr enabling dynamic plugins)
  97. #
  98. topology.standalone.plugins.enable(PLUGIN_ROOTDN_ACCESS)
  99. log.info('test_rootdn_init: Initialized root DN test suite.')
  100. def test_rootdn_access_specific_time(topology):
  101. '''
  102. Test binding inside and outside of a specific time
  103. '''
  104. log.info('Running test_rootdn_access_specific_time...')
  105. # Get the current time, and bump it ahead twohours
  106. current_hour = time.strftime("%H")
  107. if int(current_hour) > 12:
  108. open_time = '0200'
  109. close_time = '0400'
  110. else:
  111. open_time = '1600'
  112. close_time = '1800'
  113. try:
  114. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD, 'rootdn-open-time', open_time),
  115. (ldap.MOD_ADD, 'rootdn-close-time', close_time)])
  116. except ldap.LDAPError as e:
  117. log.fatal('test_rootdn_access_specific_time: Failed to set (blocking) open/close times: error ' +
  118. e.message['desc'])
  119. assert False
  120. #
  121. # Bind as Root DN - should fail
  122. #
  123. try:
  124. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  125. succeeded = True
  126. except ldap.LDAPError as e:
  127. succeeded = False
  128. if succeeded:
  129. log.fatal('test_rootdn_access_specific_time: Root DN was incorrectly able to bind')
  130. assert False
  131. #
  132. # Set config to allow the entire day
  133. #
  134. try:
  135. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  136. except ldap.LDAPError as e:
  137. log.fatal('test_rootdn_access_specific_time: test_rootdn: failed to bind as user1')
  138. assert False
  139. try:
  140. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', '0000'),
  141. (ldap.MOD_REPLACE, 'rootdn-close-time', '2359')])
  142. except ldap.LDAPError as e:
  143. log.fatal('test_rootdn_access_specific_time: Failed to set (open) open/close times: error ' +
  144. e.message['desc'])
  145. assert False
  146. try:
  147. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  148. except ldap.LDAPError as e:
  149. log.fatal('test_rootdn_access_specific_time: Root DN bind failed unexpectedly failed: error ' +
  150. e.message['desc'])
  151. assert False
  152. #
  153. # Cleanup - undo the changes we made so the next test has a clean slate
  154. #
  155. try:
  156. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, 'rootdn-open-time', None),
  157. (ldap.MOD_DELETE, 'rootdn-close-time', None)])
  158. except ldap.LDAPError as e:
  159. log.fatal('test_rootdn_access_specific_time: Failed to delete open and close time: error ' +
  160. e.message['desc'])
  161. assert False
  162. try:
  163. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  164. except ldap.LDAPError as e:
  165. log.fatal('test_rootdn_access_specific_time: Root DN bind failed unexpectedly failed: error ' +
  166. e.message['desc'])
  167. assert False
  168. log.info('test_rootdn_access_specific_time: PASSED')
  169. def test_rootdn_access_day_of_week(topology):
  170. '''
  171. Test the days of week feature
  172. '''
  173. log.info('Running test_rootdn_access_day_of_week...')
  174. days = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')
  175. day = int(time.strftime("%w", time.gmtime()))
  176. if day == 6:
  177. # Handle the roll over from Saturday into Sunday
  178. deny_days = days[1] + ', ' + days[2]
  179. allow_days = days[6] + ',' + days[0]
  180. elif day > 3:
  181. deny_days = days[0] + ', ' + days[1]
  182. allow_days = days[day] + ',' + days[day - 1]
  183. else:
  184. deny_days = days[4] + ',' + days[5]
  185. allow_days = days[day] + ',' + days[day + 1]
  186. log.info('Today: ' + days[day])
  187. log.info('Allowed days: ' + allow_days)
  188. log.info('Deny days: ' + deny_days)
  189. #
  190. # Set the deny days
  191. #
  192. try:
  193. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-days-allowed',
  194. deny_days)])
  195. except ldap.LDAPError as e:
  196. log.fatal('test_rootdn_access_day_of_week: Failed to set the deny days: error ' +
  197. e.message['desc'])
  198. assert False
  199. #
  200. # Bind as Root DN - should fail
  201. #
  202. try:
  203. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  204. succeeded = True
  205. except ldap.LDAPError as e:
  206. succeeded = False
  207. if succeeded:
  208. log.fatal('test_rootdn_access_day_of_week: Root DN was incorrectly able to bind')
  209. assert False
  210. #
  211. # Set the allow days
  212. #
  213. try:
  214. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  215. except ldap.LDAPError as e:
  216. log.fatal('test_rootdn_access_day_of_week: : failed to bind as user1')
  217. assert False
  218. try:
  219. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-days-allowed',
  220. allow_days)])
  221. except ldap.LDAPError as e:
  222. log.fatal('test_rootdn_access_day_of_week: Failed to set the deny days: error ' +
  223. e.message['desc'])
  224. assert False
  225. try:
  226. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  227. except ldap.LDAPError as e:
  228. log.fatal('test_rootdn_access_day_of_week: Root DN bind failed unexpectedly failed: error ' +
  229. e.message['desc'])
  230. assert False
  231. #
  232. # Cleanup - undo the changes we made so the next test has a clean slate
  233. #
  234. try:
  235. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, 'rootdn-days-allowed', None)])
  236. except ldap.LDAPError as e:
  237. log.fatal('test_rootdn_access_day_of_week: Failed to set rootDN plugin config: error ' +
  238. e.message['desc'])
  239. assert False
  240. try:
  241. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  242. except ldap.LDAPError as e:
  243. log.fatal('test_rootdn_access_day_of_week: Root DN bind failed unexpectedly failed: error ' +
  244. e.message['desc'])
  245. assert False
  246. log.info('test_rootdn_access_day_of_week: PASSED')
  247. def test_rootdn_access_denied_ip(topology):
  248. '''
  249. Test denied IP feature - we can just test denying 127.0.01
  250. '''
  251. log.info('Running test_rootdn_access_denied_ip...')
  252. try:
  253. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE,
  254. 'rootdn-deny-ip',
  255. '127.0.0.1'),
  256. (ldap.MOD_ADD,
  257. 'rootdn-deny-ip',
  258. '::1')])
  259. except ldap.LDAPError as e:
  260. log.fatal('test_rootdn_access_denied_ip: Failed to set rootDN plugin config: error ' +
  261. e.message['desc'])
  262. assert False
  263. #
  264. # Bind as Root DN - should fail
  265. #
  266. try:
  267. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  268. succeeded = True
  269. except ldap.LDAPError as e:
  270. succeeded = False
  271. if succeeded:
  272. log.fatal('test_rootdn_access_denied_ip: Root DN was incorrectly able to bind')
  273. assert False
  274. #
  275. # Change the denied IP so root DN succeeds
  276. #
  277. try:
  278. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  279. except ldap.LDAPError as e:
  280. log.fatal('test_rootdn_access_denied_ip: : failed to bind as user1')
  281. assert False
  282. try:
  283. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-deny-ip', '255.255.255.255')])
  284. except ldap.LDAPError as e:
  285. log.fatal('test_rootdn_access_denied_ip: Failed to set rootDN plugin config: error ' +
  286. e.message['desc'])
  287. assert False
  288. try:
  289. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  290. except ldap.LDAPError as e:
  291. log.fatal('test_rootdn_access_denied_ip: Root DN bind failed unexpectedly failed: error ' +
  292. e.message['desc'])
  293. assert False
  294. #
  295. # Cleanup - undo the changes we made so the next test has a clean slate
  296. #
  297. try:
  298. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, 'rootdn-deny-ip', None)])
  299. except ldap.LDAPError as e:
  300. log.fatal('test_rootdn_access_denied_ip: Failed to set rootDN plugin config: error ' +
  301. e.message['desc'])
  302. assert False
  303. try:
  304. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  305. except ldap.LDAPError as e:
  306. log.fatal('test_rootdn_access_denied_ip: Root DN bind failed unexpectedly failed: error ' +
  307. e.message['desc'])
  308. assert False
  309. log.info('test_rootdn_access_denied_ip: PASSED')
  310. def test_rootdn_access_denied_host(topology):
  311. '''
  312. Test denied Host feature - we can just test denying localhost
  313. '''
  314. log.info('Running test_rootdn_access_denied_host...')
  315. hostname = socket.gethostname()
  316. try:
  317. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
  318. 'rootdn-deny-host',
  319. hostname)])
  320. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
  321. 'rootdn-deny-host',
  322. 'localhost')])
  323. except ldap.LDAPError as e:
  324. log.fatal('test_rootdn_access_denied_host: Failed to set deny host: error ' +
  325. e.message['desc'])
  326. assert False
  327. #
  328. # Bind as Root DN - should fail
  329. #
  330. try:
  331. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  332. succeeded = True
  333. except ldap.LDAPError as e:
  334. succeeded = False
  335. if succeeded:
  336. log.fatal('test_rootdn_access_denied_host: Root DN was incorrectly able to bind')
  337. assert False
  338. #
  339. # Change the denied host so root DN succeeds
  340. #
  341. try:
  342. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  343. except ldap.LDAPError as e:
  344. log.fatal('test_rootdn_access_denied_host: : failed to bind as user1')
  345. assert False
  346. try:
  347. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-deny-host', 'i.dont.exist.com')])
  348. except ldap.LDAPError as e:
  349. log.fatal('test_rootdn_access_denied_host: Failed to set rootDN plugin config: error ' +
  350. e.message['desc'])
  351. assert False
  352. try:
  353. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  354. except ldap.LDAPError as e:
  355. log.fatal('test_rootdn_access_denied_host: Root DN bind failed unexpectedly failed: error ' +
  356. e.message['desc'])
  357. assert False
  358. #
  359. # Cleanup - undo the changes we made so the next test has a clean slate
  360. #
  361. try:
  362. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, 'rootdn-deny-host', None)])
  363. except ldap.LDAPError as e:
  364. log.fatal('test_rootdn_access_denied_host: Failed to set rootDN plugin config: error ' +
  365. e.message['desc'])
  366. assert False
  367. try:
  368. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  369. except ldap.LDAPError as e:
  370. log.fatal('test_rootdn_access_denied_host: Root DN bind failed unexpectedly failed: error ' +
  371. e.message['desc'])
  372. assert False
  373. log.info('test_rootdn_access_denied_host: PASSED')
  374. def test_rootdn_access_allowed_ip(topology):
  375. '''
  376. Test allowed ip feature
  377. '''
  378. log.info('Running test_rootdn_access_allowed_ip...')
  379. #
  380. # Set allowed host to an unknown host - blocks the Root DN
  381. #
  382. try:
  383. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-allow-ip', '255.255.255.255')])
  384. except ldap.LDAPError as e:
  385. log.fatal('test_rootdn_access_allowed_ip: Failed to set allowed host: error ' +
  386. e.message['desc'])
  387. assert False
  388. #
  389. # Bind as Root DN - should fail
  390. #
  391. try:
  392. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  393. succeeded = True
  394. except ldap.LDAPError as e:
  395. succeeded = False
  396. if succeeded:
  397. log.fatal('test_rootdn_access_allowed_ip: Root DN was incorrectly able to bind')
  398. assert False
  399. #
  400. # Allow localhost
  401. #
  402. try:
  403. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  404. except ldap.LDAPError as e:
  405. log.fatal('test_rootdn_access_allowed_ip: : failed to bind as user1')
  406. assert False
  407. try:
  408. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-allow-ip', '127.0.0.1'),
  409. (ldap.MOD_ADD, 'rootdn-allow-ip', '::1')])
  410. except ldap.LDAPError as e:
  411. log.fatal('test_rootdn_access_allowed_ip: Failed to set allowed host: error ' +
  412. e.message['desc'])
  413. assert False
  414. try:
  415. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  416. except ldap.LDAPError as e:
  417. log.fatal('test_rootdn_access_allowed_ip: Root DN bind failed unexpectedly failed: error ' +
  418. e.message['desc'])
  419. assert False
  420. #
  421. # Cleanup - undo everything we did so the next test has a clean slate
  422. #
  423. try:
  424. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, 'rootdn-allow-ip', None)])
  425. except ldap.LDAPError as e:
  426. log.fatal('test_rootdn_access_allowed_ip: Failed to delete(rootdn-allow-ip): error ' +
  427. e.message['desc'])
  428. assert False
  429. try:
  430. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  431. except ldap.LDAPError as e:
  432. log.fatal('test_rootdn_access_allowed_ip: Root DN bind failed unexpectedly failed: error ' +
  433. e.message['desc'])
  434. assert False
  435. log.info('test_rootdn_access_allowed_ip: PASSED')
  436. def test_rootdn_access_allowed_host(topology):
  437. '''
  438. Test allowed ip feature
  439. '''
  440. log.info('Running test_rootdn_access_allowed_host...')
  441. #
  442. # Set allowed host to an unknown host - blocks the Root DN
  443. #
  444. try:
  445. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-allow-host', 'i.dont.exist.com')])
  446. except ldap.LDAPError as e:
  447. log.fatal('test_rootdn_access_allowed_host: Failed to set allowed host: error ' +
  448. e.message['desc'])
  449. assert False
  450. #
  451. # Bind as Root DN - should fail
  452. #
  453. try:
  454. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  455. succeeded = True
  456. except ldap.LDAPError as e:
  457. succeeded = False
  458. if succeeded:
  459. log.fatal('test_rootdn_access_allowed_host: Root DN was incorrectly able to bind')
  460. assert False
  461. #
  462. # Allow localhost
  463. #
  464. try:
  465. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  466. except ldap.LDAPError as e:
  467. log.fatal('test_rootdn_access_allowed_host: : failed to bind as user1')
  468. assert False
  469. hostname = socket.gethostname()
  470. try:
  471. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
  472. 'rootdn-allow-host',
  473. 'localhost')])
  474. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
  475. 'rootdn-allow-host',
  476. hostname)])
  477. except ldap.LDAPError as e:
  478. log.fatal('test_rootdn_access_allowed_host: Failed to set allowed host: error ' +
  479. e.message['desc'])
  480. assert False
  481. try:
  482. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  483. except ldap.LDAPError as e:
  484. log.fatal('test_rootdn_access_allowed_host: Root DN bind failed unexpectedly failed: error ' +
  485. e.message['desc'])
  486. assert False
  487. #
  488. # Cleanup - undo everything we did so the next test has a clean slate
  489. #
  490. try:
  491. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_DELETE, 'rootdn-allow-host', None)])
  492. except ldap.LDAPError as e:
  493. log.fatal('test_rootdn_access_allowed_host: Failed to delete(rootdn-allow-host): error ' +
  494. e.message['desc'])
  495. assert False
  496. try:
  497. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  498. except ldap.LDAPError as e:
  499. log.fatal('test_rootdn_access_allowed_host: Root DN bind failed unexpectedly failed: error ' +
  500. e.message['desc'])
  501. assert False
  502. log.info('test_rootdn_access_allowed_host: PASSED')
  503. def test_rootdn_config_validate(topology):
  504. '''
  505. Test configuration validation
  506. test single valued attributes: rootdn-open-time,
  507. rootdn-close-time,
  508. rootdn-days-allowed
  509. '''
  510. log.info('Running test_rootdn_config_validate...')
  511. #
  512. # Test rootdn-open-time
  513. #
  514. try:
  515. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', '0000')])
  516. log.fatal('test_rootdn_config_validate: Incorrectly allowed to just add "rootdn-open-time" ')
  517. assert False
  518. except ldap.LDAPError:
  519. pass
  520. try:
  521. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD, 'rootdn-open-time', '0000'),
  522. (ldap.MOD_ADD, 'rootdn-open-time', '0001')])
  523. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add multiple "rootdn-open-time"')
  524. assert False
  525. except ldap.LDAPError:
  526. pass
  527. try:
  528. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', '-1'),
  529. (ldap.MOD_REPLACE, 'rootdn-close-time', '0000')])
  530. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-open-time: -1"')
  531. assert False
  532. except ldap.LDAPError:
  533. pass
  534. try:
  535. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', '2400'),
  536. (ldap.MOD_REPLACE, 'rootdn-close-time', '0000')])
  537. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-open-time: 2400"')
  538. assert False
  539. except ldap.LDAPError:
  540. pass
  541. try:
  542. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', 'aaaaa'),
  543. (ldap.MOD_REPLACE, 'rootdn-close-time', '0000')])
  544. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-open-time: aaaaa"')
  545. assert False
  546. except ldap.LDAPError:
  547. pass
  548. #
  549. # Test rootdn-close-time
  550. #
  551. try:
  552. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-close-time', '0000')])
  553. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add just "rootdn-close-time"')
  554. assert False
  555. except ldap.LDAPError:
  556. pass
  557. try:
  558. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD, 'rootdn-close-time', '0000'),
  559. (ldap.MOD_ADD, 'rootdn-close-time', '0001')])
  560. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add multiple "rootdn-open-time"')
  561. assert False
  562. except ldap.LDAPError:
  563. pass
  564. try:
  565. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', '0000'),
  566. (ldap.MOD_REPLACE, 'rootdn-close-time', '-1')])
  567. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-close-time: -1"')
  568. assert False
  569. except ldap.LDAPError:
  570. pass
  571. try:
  572. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', '0000'),
  573. (ldap.MOD_REPLACE, 'rootdn-close-time', '2400')])
  574. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-close-time: 2400"')
  575. assert False
  576. except ldap.LDAPError:
  577. pass
  578. try:
  579. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-open-time', '0000'),
  580. (ldap.MOD_REPLACE, 'rootdn-close-time', 'aaaaa')])
  581. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-close-time: aaaaa"')
  582. assert False
  583. except ldap.LDAPError:
  584. pass
  585. #
  586. # Test days allowed
  587. #
  588. try:
  589. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD, 'rootdn-days-allowed', 'Mon'),
  590. (ldap.MOD_ADD, 'rootdn-days-allowed', 'Tue')])
  591. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add two "rootdn-days-allowed"')
  592. assert False
  593. except ldap.LDAPError:
  594. pass
  595. try:
  596. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-days-allowed', 'Mon1')])
  597. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-days-allowed: Mon1"')
  598. assert False
  599. except ldap.LDAPError:
  600. pass
  601. try:
  602. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-days-allowed', 'Tue, Mon1')])
  603. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-days-allowed: Tue, Mon1"')
  604. assert False
  605. except ldap.LDAPError:
  606. pass
  607. try:
  608. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-days-allowed', 'm111m')])
  609. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-days-allowed: 111"')
  610. assert False
  611. except ldap.LDAPError:
  612. pass
  613. try:
  614. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-days-allowed', 'Gur')])
  615. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-days-allowed: Gur"')
  616. assert False
  617. except ldap.LDAPError:
  618. pass
  619. #
  620. # Test allow ips
  621. #
  622. try:
  623. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-allow-ip', '12.12.Z.12')])
  624. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-allow-ip: 12.12.Z.12"')
  625. assert False
  626. except ldap.LDAPError:
  627. pass
  628. #
  629. # Test deny ips
  630. #
  631. try:
  632. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-deny-ip', '12.12.Z.12')])
  633. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-deny-ip: 12.12.Z.12"')
  634. assert False
  635. except ldap.LDAPError:
  636. pass
  637. #
  638. # Test allow hosts
  639. #
  640. try:
  641. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-allow-host', 'host._.com')])
  642. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-allow-host: host._.com"')
  643. assert False
  644. except ldap.LDAPError:
  645. pass
  646. #
  647. # Test deny hosts
  648. #
  649. try:
  650. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'rootdn-deny-host', 'host.####.com')])
  651. log.fatal('test_rootdn_config_validate: Incorrectly allowed to add invalid "rootdn-deny-host: host.####.com"')
  652. assert False
  653. except ldap.LDAPError:
  654. pass
  655. log.info('test_rootdn_config_validate: PASSED')
  656. if __name__ == '__main__':
  657. # Run isolated
  658. # -s for DEBUG mode
  659. CURRENT_FILE = os.path.realpath(__file__)
  660. pytest.main("-s %s" % CURRENT_FILE)