valueacl_part2_test.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. import pytest, os, ldap
  9. from lib389._constants import DEFAULT_SUFFIX, PW_DM
  10. from lib389.idm.user import UserAccount
  11. from lib389.idm.account import Anonymous
  12. from lib389.idm.organizationalunit import OrganizationalUnit
  13. from lib389.topologies import topology_st as topo
  14. from lib389.idm.domain import Domain
  15. pytestmark = pytest.mark.tier1
  16. CONTAINER_1_DELADD = "ou=Product Development,{}".format(DEFAULT_SUFFIX)
  17. CONTAINER_2_DELADD = "ou=Accounting,{}".format(DEFAULT_SUFFIX)
  18. USER_DELADD = "cn=Jeff Vedder,{}".format(CONTAINER_1_DELADD)
  19. USER_WITH_ACI_DELADD = "cn=Sam Carter,{}".format(CONTAINER_2_DELADD)
  20. FRED = "cn=FRED,ou=Accounting,{}".format(DEFAULT_SUFFIX)
  21. HARRY = "cn=HARRY,ou=Accounting,{}".format(DEFAULT_SUFFIX)
  22. KIRSTENVAUGHAN = "cn=Kirsten Vaughan,ou=Human Resources,{}".format(DEFAULT_SUFFIX)
  23. HUMAN_OU_GLOBAL = "ou=Human Resources,{}".format(DEFAULT_SUFFIX)
  24. @pytest.fixture(scope="function")
  25. def aci_of_user(request, topo):
  26. aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
  27. def finofaci():
  28. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  29. domain.set('aci', None)
  30. for i in aci_list:
  31. domain.add("aci", i)
  32. request.addfinalizer(finofaci)
  33. @pytest.fixture(scope="function")
  34. def _add_user(request, topo):
  35. for i in ["Product Development", 'Accounting', "Human Resources"]:
  36. ou = OrganizationalUnit(topo.standalone, "ou={},{}".format(i, DEFAULT_SUFFIX))
  37. ou.create(properties={'ou': i})
  38. properties = {
  39. 'uid': 'Jeff Vedder',
  40. 'cn': 'Jeff Vedder',
  41. 'sn': 'user',
  42. 'uidNumber': '1000',
  43. 'gidNumber': '2000',
  44. 'homeDirectory': '/home/' + 'JeffVedder',
  45. 'userPassword': 'password'
  46. }
  47. user = UserAccount(topo.standalone, 'cn=Jeff Vedder,{}'.format(CONTAINER_1_DELADD))
  48. user.create(properties=properties)
  49. user.set('secretary', 'cn=Arpitoo Borah, o=Red Hat, c=As')
  50. user.set('mail', '[email protected]')
  51. properties = {
  52. 'uid': 'Sam Carter',
  53. 'cn': 'Sam Carter',
  54. 'sn': 'user',
  55. 'uidNumber': '1000',
  56. 'gidNumber': '2000',
  57. 'homeDirectory': '/home/' + 'SamCarter',
  58. 'userPassword': 'password'
  59. }
  60. user = UserAccount(topo.standalone, 'cn=Sam Carter,{}'.format(CONTAINER_2_DELADD))
  61. user.create(properties=properties)
  62. properties = {
  63. 'uid': 'Kirsten Vaughan',
  64. 'cn': 'Kirsten Vaughan',
  65. 'sn': 'Kirsten Vaughan',
  66. 'uidNumber': '1000',
  67. 'gidNumber': '2000',
  68. 'homeDirectory': '/home/' + 'KirstenVaughan',
  69. 'userPassword': 'password'
  70. }
  71. user = UserAccount(topo.standalone, 'cn=Kirsten Vaughan, ou=Human Resources,{}'.format(DEFAULT_SUFFIX))
  72. user.create(properties=properties)
  73. properties = {
  74. 'uid': 'HARRY',
  75. 'cn': 'HARRY',
  76. 'sn': 'HARRY',
  77. 'uidNumber': '1000',
  78. 'gidNumber': '2000',
  79. 'homeDirectory': '/home/' + 'HARRY',
  80. 'userPassword': 'password'
  81. }
  82. user = UserAccount(topo.standalone, 'cn=HARRY, ou=Accounting,{}'.format(DEFAULT_SUFFIX))
  83. user.create(properties=properties)
  84. def fin():
  85. for DN in [USER_DELADD, USER_WITH_ACI_DELADD, FRED, HARRY, KIRSTENVAUGHAN,
  86. HUMAN_OU_GLOBAL, CONTAINER_2_DELADD,CONTAINER_1_DELADD]:
  87. ua = UserAccount(topo.standalone, DN)
  88. try:
  89. ua.delete()
  90. except:
  91. pass
  92. request.addfinalizer(fin)
  93. def test_we_can_search_as_expected(topo, _add_user, aci_of_user):
  94. """
  95. Testing the targattrfilters keyword that allows access control based on the value of the attributes being added (or deleted))
  96. Test that we can search as expected
  97. :id: e845dbba-7aa9-11e8-8988-8c16451d917b
  98. :setup: server
  99. :steps:
  100. 1. Add test entry
  101. 2. Add ACI
  102. 3. User should follow ACI role
  103. :expectedresults:
  104. 1. Entry should be added
  105. 2. Operation should succeed
  106. 3. Operation should succeed
  107. """
  108. ACI_BODY = '(target="ldap:///cn=*,ou=Product Development, {}")' \
  109. '(targetfilter="cn=Jeff*")(targetattr="secretary || objectclass || mail")' \
  110. '(targattrfilters = "add=title:(title=arch*)")(version 3.0; acl "$tet_thistest"; ' \
  111. 'allow (write,read,search,compare) (userdn = "ldap:///anyone") ;)'.format(DEFAULT_SUFFIX)
  112. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  113. conn = Anonymous(topo.standalone).bind()
  114. # aci will allow secretary , mail , objectclass
  115. user = UserAccount(conn, USER_DELADD)
  116. assert user.get_attr_vals('secretary')
  117. assert user.get_attr_vals('mail')
  118. assert user.get_attr_vals('objectclass')
  119. def test_we_can_mod_title_as_expected(topo, _add_user, aci_of_user):
  120. """
  121. Testing the targattrfilters keyword that allows access control based on the
  122. value of the attributes being added (or deleted))
  123. "Valueacl Test $tet_thistest Test search will work with targattrfilters present."
  124. :id: f8c1ea88-7aa9-11e8-a55c-8c16451d917b
  125. :setup: server
  126. :steps:
  127. 1. Add test entry
  128. 2. Add ACI
  129. 3. User should follow ACI role
  130. :expectedresults:
  131. 1. Entry should be added
  132. 2. Operation should succeed
  133. 3. Operation should succeed
  134. """
  135. ACI_BODY = '(target="ldap:///cn=*,ou=Product Development, {}")' \
  136. '(targetfilter="cn=Jeff*")(targetattr="secretary || objectclass || mail")' \
  137. '(targattrfilters = "add=title:(title=arch*)")(version 3.0; acl "$tet_thistest"; ' \
  138. 'allow (write,read,search,compare) (userdn = "ldap:///anyone") ;)'.format(DEFAULT_SUFFIX)
  139. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  140. # aci will not allow 'title', 'topdog'
  141. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  142. user = UserAccount(conn, USER_DELADD)
  143. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  144. user.add('title', 'topdog')
  145. def test_modify_with_multiple_filters(topo, _add_user, aci_of_user):
  146. """
  147. Testing the targattrfilters keyword that allows access control based on the
  148. value of the attributes being added (or deleted))
  149. "Valueacl Test $tet_thistest Allowed by multiple."
  150. :id: fd9d223e-7aa9-11e8-a83b-8c16451d917b
  151. :setup: server
  152. :steps:
  153. 1. Add test entry
  154. 2. Add ACI
  155. 3. User should follow ACI role
  156. :expectedresults:
  157. 1. Entry should be added
  158. 2. Operation should succeed
  159. 3. Operation should succeed
  160. """
  161. ACI_BODY = '(targattrfilters = "add=title:(title=architect) && secretary:' \
  162. '(secretary=cn=Meylan,{}), del=title:(title=architect) && secretary:' \
  163. '(secretary=cn=Meylan,{})")(version 3.0; acl "$tet_thistest"; allow (write) ' \
  164. '(userdn = "ldap:///anyone") ;)'.format(
  165. DEFAULT_SUFFIX, DEFAULT_SUFFIX
  166. )
  167. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  168. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  169. # aci will allow title some attribute only
  170. user = UserAccount(conn, USER_DELADD)
  171. user.add("title", "architect")
  172. assert user.get_attr_val('title')
  173. user.add("secretary", "cn=Meylan,dc=example,dc=com")
  174. assert user.get_attr_val('secretary')
  175. def test_denied_by_multiple_filters(topo, _add_user, aci_of_user):
  176. """
  177. Testing the targattrfilters keyword that allows access control based on the value of the
  178. attributes being added (or deleted))
  179. "Valueacl Test $tet_thistest Denied by multiple filters."
  180. :id: 034c6c62-7aaa-11e8-8634-8c16451d917b
  181. :setup: server
  182. :steps:
  183. 1. Add test entry
  184. 2. Add ACI
  185. 3. User should follow ACI role
  186. :expectedresults:
  187. 1. Entry should be added
  188. 2. Operation should succeed
  189. 3. Operation should succeed
  190. """
  191. ACI_BODY = '(targattrfilters = "add=title:(title=architect) && secretary:' \
  192. '(secretary=cn=Meylan,{}), del=title:(title=architect) && secretary:' \
  193. '(secretary=cn=Meylan,{})")(version 3.0; acl "$tet_thistest"; allow (write) ' \
  194. '(userdn = "ldap:///anyone") ;)'.format(DEFAULT_SUFFIX, DEFAULT_SUFFIX)
  195. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  196. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  197. # aci will allow title some attribute only
  198. user = UserAccount(conn, USER_DELADD)
  199. user.add("title", "architect")
  200. assert user.get_attr_val('title')
  201. user.add("secretary", "cn=Meylan,dc=example,dc=com")
  202. assert user.get_attr_val('secretary')
  203. # aci will allow title some attribute only
  204. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  205. user.add("secretary", "cn=Grenoble,dc=example,dc=com")
  206. def test_allowed_add_one_attribute(topo, _add_user, aci_of_user):
  207. """
  208. Testing the targattrfilters keyword that allows access control based on the value of the
  209. attributes being added (or deleted))
  210. "Valueacl Test $tet_thistest Allowed add one attribute (in presence of multiple filters)"
  211. :id: 086c7f0c-7aaa-11e8-b69f-8c16451d917b
  212. :setup: server
  213. :steps:
  214. 1. Add test entry
  215. 2. Add ACI
  216. 3. User should follow ACI role
  217. :expectedresults:
  218. 1. Entry should be added
  219. 2. Operation should succeed
  220. 3. Operation should succeed
  221. """
  222. ACI_BODY = '(targattrfilters = "add=title:(title=architect) && secretary:(secretary=cn=Meylan, {}), ' \
  223. 'del=title:(title=architect) && secretary:(secretary=cn=Meylan, {})")(version 3.0; acl "$tet_thistest"; ' \
  224. 'allow (write) (userdn = "ldap:///{}") ;)'.format(
  225. DEFAULT_SUFFIX, DEFAULT_SUFFIX, USER_WITH_ACI_DELADD)
  226. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  227. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  228. user = UserAccount(conn, USER_DELADD)
  229. # aci will allow add ad delete
  230. user.add('title', 'architect')
  231. assert user.get_attr_val('title')
  232. user.remove('title', 'architect')
  233. def test_cannot_add_an_entry_with_attribute_values_we_are_not_allowed_add(
  234. topo, _add_user, aci_of_user
  235. ):
  236. """
  237. Testing the targattrfilters keyword that allows access control based on the value of the
  238. attributes being added (or deleted))
  239. "Valueacl Test $tet_thistest Test not allowed add an entry"
  240. :id: 0d0effee-7aaa-11e8-b673-8c16451d917b
  241. :setup: server
  242. :steps:
  243. 1. Add test entry
  244. 2. Add ACI
  245. 3. User should follow ACI role
  246. :expectedresults:
  247. 1. Entry should be added
  248. 2. Operation should succeed
  249. 3. Operation should succeed
  250. """
  251. ACI_BODY = '(targattrfilters = "add=title:(|(title=engineer)(title=cool dude)(title=scum)) ' \
  252. '&& secretary:(secretary=cn=Meylan, {}), del=title:(|(title=engineer)(title=cool dude)' \
  253. '(title=scum))")(version 3.0; aci "$tet_thistest"; allow (add) userdn = "ldap:///{}";)'.format(
  254. DEFAULT_SUFFIX, DEFAULT_SUFFIX)
  255. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  256. properties = {
  257. 'uid': 'FRED',
  258. 'cn': 'FRED',
  259. 'sn': 'user',
  260. 'uidNumber': '1000',
  261. 'gidNumber': '2000',
  262. 'homeDirectory': '/home/' + 'FRED'
  263. }
  264. user = UserAccount(topo.standalone, 'cn=FRED,ou=Accounting,{}'.format(DEFAULT_SUFFIX))
  265. user.create(properties=properties)
  266. user.set('title', ['anuj', 'kumar', 'borah'])
  267. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  268. # aci will not allow adding objectclass
  269. user = UserAccount(conn, USER_WITH_ACI_DELADD)
  270. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  271. user.add("objectclass", "person")
  272. def test_on_modrdn(topo, _add_user, aci_of_user):
  273. """
  274. Testing the targattrfilters keyword that allows access control based on the value of the
  275. attributes being added (or deleted))
  276. Test that valuacls kick in for modrdn operation.
  277. :id: 12985dde-7aaa-11e8-abde-8c16451d917b
  278. :setup: server
  279. :steps:
  280. 1. Add test entry
  281. 2. Add ACI
  282. 3. User should follow ACI role
  283. :expectedresults:
  284. 1. Entry should be added
  285. 2. Operation should succeed
  286. 3. Operation should succeed
  287. """
  288. ACI_BODY = '(target="ldap:///cn=*,ou=Accounting,{}")(targattrfilters = "add=cn:(|(cn=engineer)), ' \
  289. 'del=title:(|(title=engineer)(title=cool dude)(title=scum))")(version 3.0; aci "$tet_thistest"; ' \
  290. 'allow (write) userdn = "ldap:///{}";)'.format(DEFAULT_SUFFIX, USER_WITH_ACI_DELADD)
  291. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  292. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  293. # modrdn_s is not allowed with ou=OU1
  294. useraccount = UserAccount(conn, FRED)
  295. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  296. useraccount.rename("ou=OU1")
  297. def test_on_modrdn_allow(topo, _add_user, aci_of_user):
  298. """
  299. Testing the targattrfilters keyword that allows access control based on the value of the attributes being
  300. added (or deleted))
  301. "Valueacl Test $tet_thistest Test modrdn still works (2)"
  302. :id: 17720562-7aaa-11e8-82ee-8c16451d917b
  303. :setup: server
  304. :steps:
  305. 1. Add test entry
  306. 2. Add ACI
  307. 3. User should follow ACI role
  308. :expectedresults:
  309. 1. Entry should be added
  310. 2. Operation should succeed
  311. 3. Operation should succeed
  312. """
  313. ACI_BODY = '(target="ldap:///{}")(targattrfilters = "add=cn:((cn=engineer)), del=cn:((cn=jonny))")' \
  314. '(version 3.0; aci "$tet_thistest"; allow (write) ' \
  315. 'userdn = "ldap:///{}";)'.format(DEFAULT_SUFFIX, USER_WITH_ACI_DELADD)
  316. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
  317. properties = {
  318. 'uid': 'jonny',
  319. 'cn': 'jonny',
  320. 'sn': 'user',
  321. 'uidNumber': '1000',
  322. 'gidNumber': '2000',
  323. 'homeDirectory': '/home/' + 'jonny'
  324. }
  325. user = UserAccount(topo.standalone, 'cn=jonny,{}'.format(DEFAULT_SUFFIX))
  326. user.create(properties=properties)
  327. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  328. # aci will allow modrdn_s on cn=engineer
  329. useraccount = UserAccount(conn, "cn=jonny,{}".format(DEFAULT_SUFFIX))
  330. useraccount.rename("cn=engineer")
  331. assert useraccount.dn == 'cn=engineer,dc=example,dc=com'
  332. @pytest.mark.bz979515
  333. def test_targattrfilters_keyword(topo):
  334. """
  335. Testing the targattrfilters keyword that allows access control based on the value
  336. of the attributes being added (or deleted))
  337. "Bug #979515 - ACLs inoperative in some search scenarios [rhel-6.5]"
  338. "Bug #979516 is a clone for DS8.2 on RHEL5.9"
  339. "Bug #979514 is a clone for RHEL6.4 zStream errata"
  340. :id: 23f9e9d0-7aaa-11e8-b16b-8c16451d917b
  341. :setup: server
  342. :steps:
  343. 1. Add test entry
  344. 2. Add ACI
  345. 3. User should follow ACI role
  346. :expectedresults:
  347. 1. Entry should be added
  348. 2. Operation should succeed
  349. 3. Operation should succeed
  350. """
  351. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  352. domain.set('aci', None)
  353. ou = OrganizationalUnit(topo.standalone, 'ou=bug979515,{}'.format(DEFAULT_SUFFIX))
  354. ou.create(properties={'ou': 'bug979515'})
  355. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", '(target="ldap:///ou=bug979515,{}") '
  356. '(targetattr= "uid") ( version 3.0; acl "read other subscriber"; allow (compare, read, search) '
  357. 'userdn="ldap:///uid=*,ou=bug979515,{}" ; )'.format(DEFAULT_SUFFIX, DEFAULT_SUFFIX))
  358. properties = {
  359. 'uid': 'acientryusr1',
  360. 'cn': 'acientryusr1',
  361. 'sn': 'user',
  362. 'uidNumber': '1000',
  363. 'gidNumber': '2000',
  364. 'homeDirectory': '/home/' + 'acientryusr1'
  365. }
  366. user = UserAccount(topo.standalone, 'cn=acientryusr1,ou=bug979515,{}'.format(DEFAULT_SUFFIX))
  367. user.create(properties=properties)
  368. user.set('telephoneNumber', '99972566596')
  369. user.set('mail', '[email protected]')
  370. user.set("userPassword", "password")
  371. properties = {
  372. 'uid': 'newaciphoneusr1',
  373. 'cn': 'newaciphoneusr1',
  374. 'sn': 'user',
  375. 'uidNumber': '1000',
  376. 'gidNumber': '2000',
  377. 'homeDirectory': '/home/' + 'newaciphoneusr1'
  378. }
  379. user = UserAccount(topo.standalone, 'cn=newaciphoneusr1,ou=bug979515,{}'.format(DEFAULT_SUFFIX))
  380. user.create(properties=properties)
  381. user.set('telephoneNumber', '99972566596')
  382. user.set('mail', '[email protected]')
  383. conn = UserAccount(topo.standalone, "cn=acientryusr1,ou=bug979515,{}".format(DEFAULT_SUFFIX)).bind(PW_DM)
  384. # Testing the targattrfilters keyword that allows access control based on the value of the attributes being added (or deleted))
  385. user = UserAccount(conn, "cn=acientryusr1,ou=bug979515,{}".format(DEFAULT_SUFFIX))
  386. with pytest.raises(IndexError):
  387. user.get_attr_vals('mail')
  388. user.get_attr_vals('telephoneNumber')
  389. user.get_attr_vals('cn')
  390. user = UserAccount(topo.standalone, "cn=acientryusr1,ou=bug979515,{}".format(DEFAULT_SUFFIX))
  391. user.get_attr_vals('mail')
  392. user.get_attr_vals('telephoneNumber')
  393. user.get_attr_vals('cn')
  394. if __name__ == '__main__':
  395. CURRENT_FILE = os.path.realpath(__file__)
  396. pytest.main("-s -v %s" % CURRENT_FILE)