keywords_part2_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2019 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. This test script will test wrong/correct key value with ACIs.
  10. """
  11. import os
  12. import time
  13. from datetime import datetime
  14. import pytest
  15. from lib389._constants import DEFAULT_SUFFIX, PW_DM
  16. from lib389.idm.domain import Domain
  17. from lib389.idm.organizationalunit import OrganizationalUnit
  18. from lib389.idm.user import UserAccount
  19. import ldap
  20. pytestmark = pytest.mark.tier1
  21. KEYWORDS_OU_KEY = "ou=Keywords,{}".format(DEFAULT_SUFFIX)
  22. DAYOFWEEK_OU_KEY = "ou=Dayofweek,{}".format(KEYWORDS_OU_KEY)
  23. IP_OU_KEY = "ou=IP,{}".format(KEYWORDS_OU_KEY)
  24. TIMEOFDAY_OU_KEY = "ou=Timeofday,{}".format(KEYWORDS_OU_KEY)
  25. EVERYDAY_KEY = "uid=EVERYDAY_KEY,{}".format(DAYOFWEEK_OU_KEY)
  26. TODAY_KEY = "uid=TODAY_KEY,{}".format(DAYOFWEEK_OU_KEY)
  27. NODAY_KEY = "uid=NODAY_KEY,{}".format(DAYOFWEEK_OU_KEY)
  28. FULLIP_KEY = "uid=FULLIP_KEY,{}".format(IP_OU_KEY)
  29. NETSCAPEIP_KEY = "uid=NETSCAPEIP_KEY,{}".format(IP_OU_KEY)
  30. NOIP_KEY = "uid=NOIP_KEY,{}".format(IP_OU_KEY)
  31. FULLWORKER_KEY = "uid=FULLWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
  32. DAYWORKER_KEY = "uid=DAYWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
  33. NIGHTWORKER_KEY = "uid=NIGHTWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
  34. NOWORKER_KEY = "uid=NOWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
  35. def test_access_from_certain_network_only_ip(topo, add_user, aci_of_user):
  36. """
  37. User can access the data when connecting from certain network only as per the ACI.
  38. :id: 4ec38296-7ac5-11e8-9816-8c16451d917b
  39. :setup: Standalone Server
  40. :steps:
  41. 1. Add test entry
  42. 2. Add ACI
  43. 3. User should follow ACI role
  44. :expectedresults:
  45. 1. Entry should be added
  46. 2. Operation should succeed
  47. 3. Operation should succeed
  48. """
  49. # Turn access log buffering off to make less time consuming
  50. topo.standalone.config.set('nsslapd-accesslog-logbuffering', 'off')
  51. # Find the ip from ds logs , as we need to know the exact ip used by ds to run the instances.
  52. # Wait till Access Log is generated
  53. topo.standalone.restart()
  54. ip_ip = topo.standalone.ds_access_log.match('.* connection from ')[0].split()[-1]
  55. # Add ACI
  56. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  57. domain.add("aci", f'(target = "ldap:///{IP_OU_KEY}")(targetattr=*)(version 3.0; aci "IP aci"; '
  58. f'allow(all)userdn = "ldap:///{NETSCAPEIP_KEY}" and ip = "{ip_ip}" ;)')
  59. # create a new connection for the test
  60. conn = UserAccount(topo.standalone, NETSCAPEIP_KEY).bind(PW_DM)
  61. # Perform Operation
  62. org = OrganizationalUnit(conn, IP_OU_KEY)
  63. org.replace("seeAlso", "cn=1")
  64. # remove the aci
  65. domain.ensure_removed("aci", f'(target = "ldap:///{IP_OU_KEY}")(targetattr=*)(version 3.0; aci '
  66. f'"IP aci"; allow(all)userdn = "ldap:///{NETSCAPEIP_KEY}" and '
  67. f'ip = "{ip_ip}" ;)')
  68. # Now add aci with new ip
  69. domain.add("aci", f'(target = "ldap:///{IP_OU_KEY}")(targetattr=*)(version 3.0; aci "IP aci"; '
  70. f'allow(all)userdn = "ldap:///{NETSCAPEIP_KEY}" and ip = "100.1.1.1" ;)')
  71. # After changing the ip user cant access data
  72. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  73. org.replace("seeAlso", "cn=1")
  74. def test_connectin_from_an_unauthorized_network(topo, add_user, aci_of_user):
  75. """
  76. User cannot access the data when connectin from an unauthorized network as per the ACI.
  77. :id: 52d1ecce-7ac5-11e8-9ad9-8c16451d917b
  78. :setup: Standalone Server
  79. :steps:
  80. 1. Add test entry
  81. 2. Add ACI
  82. 3. User should follow ACI role
  83. :expectedresults:
  84. 1. Entry should be added
  85. 2. Operation should succeed
  86. 3. Operation should succeed
  87. """
  88. # Find the ip from ds logs , as we need to know the exact ip used by ds to run the instances.
  89. ip_ip = topo.standalone.ds_access_log.match('.* connection from ')[0].split()[-1]
  90. # Add ACI
  91. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  92. domain.add("aci", f'(target = "ldap:///{IP_OU_KEY}")'
  93. f'(targetattr=*)(version 3.0; aci "IP aci"; '
  94. f'allow(all) userdn = "ldap:///{NETSCAPEIP_KEY}" '
  95. f'and ip != "{ip_ip}" ;)')
  96. # create a new connection for the test
  97. conn = UserAccount(topo.standalone, NETSCAPEIP_KEY).bind(PW_DM)
  98. # Perform Operation
  99. org = OrganizationalUnit(conn, IP_OU_KEY)
  100. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  101. org.replace("seeAlso", "cn=1")
  102. # Remove the ACI
  103. domain.ensure_removed('aci', domain.get_attr_vals('aci')[-1])
  104. # Add new ACI
  105. domain.add('aci', f'(target = "ldap:///{IP_OU_KEY}")(targetattr=*)'
  106. f'(version 3.0; aci "IP aci"; allow(all) '
  107. f'userdn = "ldap:///{NETSCAPEIP_KEY}" and ip = "{ip_ip}" ;)')
  108. # now user can access data
  109. org.replace("seeAlso", "cn=1")
  110. def test_ip_keyword_test_noip_cannot(topo, add_user, aci_of_user):
  111. """
  112. User NoIP cannot assess the data as per the ACI.
  113. :id: 570bc7f6-7ac5-11e8-88c1-8c16451d917b
  114. :setup: Standalone Server
  115. :steps:
  116. 1. Add test entry
  117. 2. Add ACI
  118. 3. User should follow ACI role
  119. :expectedresults:
  120. 1. Entry should be added
  121. 2. Operation should succeed
  122. 3. Operation should succeed
  123. """
  124. # Add ACI
  125. Domain(topo.standalone,
  126. DEFAULT_SUFFIX).add("aci", f'(target ="ldap:///{IP_OU_KEY}")'
  127. f'(targetattr=*)(version 3.0; aci "IP aci"; allow(all) '
  128. f'userdn = "ldap:///{FULLIP_KEY}" and ip = "*" ;)')
  129. # Create a new connection for this test.
  130. conn = UserAccount(topo.standalone, NOIP_KEY).bind(PW_DM)
  131. # Perform Operation
  132. org = OrganizationalUnit(conn, IP_OU_KEY)
  133. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  134. org.replace("seeAlso", "cn=1")
  135. def test_user_can_access_the_data_at_any_time(topo, add_user, aci_of_user):
  136. """
  137. User can access the data at any time as per the ACI.
  138. :id: 5b4da91a-7ac5-11e8-bbda-8c16451d917b
  139. :setup: Standalone Server
  140. :steps:
  141. 1. Add test entry
  142. 2. Add ACI
  143. 3. User should follow ACI role
  144. :expectedresults:
  145. 1. Entry should be added
  146. 2. Operation should succeed
  147. 3. Operation should succeed
  148. """
  149. # Add ACI
  150. Domain(topo.standalone,
  151. DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")'
  152. f'(targetattr=*)(version 3.0; aci "Timeofday aci"; '
  153. f'allow(all) userdn ="ldap:///{FULLWORKER_KEY}" and '
  154. f'(timeofday >= "0000" and timeofday <= "2359") ;)')
  155. # Create a new connection for this test.
  156. conn = UserAccount(topo.standalone, FULLWORKER_KEY).bind(PW_DM)
  157. # Perform Operation
  158. org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
  159. org.replace("seeAlso", "cn=1")
  160. def test_user_can_access_the_data_only_in_the_morning(topo, add_user, aci_of_user):
  161. """
  162. User can access the data only in the morning as per the ACI.
  163. :id: 5f7d380c-7ac5-11e8-8124-8c16451d917b
  164. :setup: Standalone Server
  165. :steps:
  166. 1. Add test entry
  167. 2. Add ACI
  168. 3. User should follow ACI role
  169. :expectedresults:
  170. 1. Entry should be added
  171. 2. Operation should succeed
  172. 3. Operation should succeed
  173. """
  174. # Add ACI
  175. Domain(topo.standalone,
  176. DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")'
  177. f'(targetattr=*)(version 3.0; aci "Timeofday aci"; '
  178. f'allow(all) userdn = "ldap:///{DAYWORKER_KEY}" '
  179. f'and timeofday < "1200" ;)')
  180. # Create a new connection for this test.
  181. conn = UserAccount(topo.standalone, DAYWORKER_KEY).bind(PW_DM)
  182. # Perform Operation
  183. org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
  184. if datetime.now().hour >= 12:
  185. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  186. org.replace("seeAlso", "cn=1")
  187. else:
  188. org.replace("seeAlso", "cn=1")
  189. def test_user_can_access_the_data_only_in_the_afternoon(topo, add_user, aci_of_user):
  190. """
  191. User can access the data only in the afternoon as per the ACI.
  192. :id: 63eb5b1c-7ac5-11e8-bd46-8c16451d917b
  193. :setup: Standalone Server
  194. :steps:
  195. 1. Add test entry
  196. 2. Add ACI
  197. 3. User should follow ACI role
  198. :expectedresults:
  199. 1. Entry should be added
  200. 2. Operation should succeed
  201. 3. Operation should succeed
  202. """
  203. # Add ACI
  204. Domain(topo.standalone,
  205. DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")'
  206. f'(targetattr=*)(version 3.0; aci "Timeofday aci"; '
  207. f'allow(all) userdn = "ldap:///{NIGHTWORKER_KEY}" '
  208. f'and timeofday > \'1200\' ;)')
  209. # create a new connection for the test
  210. conn = UserAccount(topo.standalone, NIGHTWORKER_KEY).bind(PW_DM)
  211. # Perform Operation
  212. org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
  213. if datetime.now().hour < 12:
  214. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  215. org.replace("seeAlso", "cn=1")
  216. else:
  217. org.replace("seeAlso", "cn=1")
  218. def test_timeofday_keyword(topo, add_user, aci_of_user):
  219. """
  220. User NOWORKER_KEY can access the data as per the ACI after removing
  221. ACI it cant.
  222. :id: 681dd58e-7ac5-11e8-bed1-8c16451d917b
  223. :setup: Standalone Server
  224. :steps:
  225. 1. Add test entry
  226. 2. Add ACI
  227. 3. User should follow ACI role
  228. :expectedresults:
  229. 1. Entry should be added
  230. 2. Operation should succeed
  231. 3. Operation should succeed
  232. """
  233. now = time.strftime("%c")
  234. now_1 = "".join(now.split()[3].split(":"))[:4]
  235. # Add ACI
  236. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  237. domain.add("aci", f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")'
  238. f'(targetattr=*)(version 3.0; aci "Timeofday aci"; '
  239. f'allow(all) userdn = "ldap:///{NOWORKER_KEY}" '
  240. f'and timeofday = \'{now_1}\' ;)')
  241. # Create a new connection for this test.
  242. conn = UserAccount(topo.standalone, NOWORKER_KEY).bind(PW_DM)
  243. # Perform Operation
  244. org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
  245. org.replace("seeAlso", "cn=1")
  246. # Remove ACI
  247. aci = domain.get_attr_vals_utf8('aci')[-1]
  248. domain.ensure_removed('aci', aci)
  249. assert aci not in domain.get_attr_vals_utf8('aci')
  250. # after removing the ACI user cannot access the data
  251. time.sleep(1)
  252. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  253. org.replace("seeAlso", "cn=1")
  254. def test_dayofweek_keyword_test_everyday_can_access(topo, add_user, aci_of_user):
  255. """
  256. User can access the data EVERYDAY_KEY as per the ACI.
  257. :id: 6c5922ca-7ac5-11e8-8f01-8c16451d917b
  258. :setup: Standalone Server
  259. :steps:
  260. 1. Add test entry
  261. 2. Add ACI
  262. 3. User should follow ACI role
  263. :expectedresults:
  264. 1. Entry should be added
  265. 2. Operation should succeed
  266. 3. Operation should succeed
  267. """
  268. # Add ACI
  269. Domain(topo.standalone,
  270. DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{DAYOFWEEK_OU_KEY}")'
  271. f'(targetattr=*)(version 3.0; aci "Dayofweek aci"; '
  272. f'allow(all) userdn = "ldap:///{EVERYDAY_KEY}" and '
  273. f'dayofweek = "Sun, Mon, Tue, Wed, Thu, Fri, Sat" ;)')
  274. # Create a new connection for this test.
  275. conn = UserAccount(topo.standalone, EVERYDAY_KEY).bind(PW_DM)
  276. # Perform Operation
  277. org = OrganizationalUnit(conn, DAYOFWEEK_OU_KEY)
  278. org.replace("seeAlso", "cn=1")
  279. def test_dayofweek_keyword_today_can_access(topo, add_user, aci_of_user):
  280. """
  281. User can access the data one day per week as per the ACI.
  282. :id: 7131dc88-7ac5-11e8-acc2-8c16451d917b
  283. :setup: Standalone Server
  284. :steps:
  285. 1. Add test entry
  286. 2. Add ACI
  287. 3. User should follow ACI role
  288. :expectedresults:
  289. 1. Entry should be added
  290. 2. Operation should succeed
  291. 3. Operation should succeed
  292. """
  293. today_1 = time.strftime("%c").split()[0]
  294. # Add ACI
  295. Domain(topo.standalone,
  296. DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{DAYOFWEEK_OU_KEY}")'
  297. f'(targetattr=*)(version 3.0; aci "Dayofweek aci"; '
  298. f'allow(all) userdn = "ldap:///{TODAY_KEY}" '
  299. f'and dayofweek = \'{today_1}\' ;)')
  300. # Create a new connection for this test.
  301. conn = UserAccount(topo.standalone, TODAY_KEY).bind(PW_DM)
  302. # Perform Operation
  303. org = OrganizationalUnit(conn, DAYOFWEEK_OU_KEY)
  304. org.replace("seeAlso", "cn=1")
  305. def test_user_cannot_access_the_data_at_all(topo, add_user, aci_of_user):
  306. """
  307. User cannot access the data at all as per the ACI.
  308. :id: 75cdac5e-7ac5-11e8-968a-8c16451d917b
  309. :setup: Standalone Server
  310. :steps:
  311. 1. Add test entry
  312. 2. Add ACI
  313. 3. User should follow ACI role
  314. :expectedresults:
  315. 1. Entry should be added
  316. 2. Operation should succeed
  317. 3. Operation should succeed
  318. """
  319. # Add ACI
  320. Domain(topo.standalone,
  321. DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{DAYOFWEEK_OU_KEY}")'
  322. f'(targetattr=*)(version 3.0; aci "Dayofweek aci"; '
  323. f'allow(all) userdn = "ldap:///{TODAY_KEY}" '
  324. f'and dayofweek = "$NEW_DATE" ;)')
  325. # Create a new connection for this test.
  326. conn = UserAccount(topo.standalone, NODAY_KEY).bind(PW_DM)
  327. # Perform Operation
  328. org = OrganizationalUnit(conn, DAYOFWEEK_OU_KEY)
  329. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  330. org.replace("seeAlso", "cn=1")
  331. if __name__ == "__main__":
  332. CURRENT_FILE = os.path.realpath(__file__)
  333. pytest.main("-s -v %s" % CURRENT_FILE)