rootdn_plugin_test.py 31 KB

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