globalgroup_test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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, UserAccounts
  11. from lib389.idm.group import UniqueGroup, UniqueGroups
  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. ACLGROUP_OU_GLOBAL = "ou=ACLGroup,{}".format(DEFAULT_SUFFIX)
  17. NESTEDGROUP_OU_GLOBAL = "ou=nestedgroup, {}".format(DEFAULT_SUFFIX)
  18. TESTING_OU_GLOBAL = "ou=Product Testing,{}".format(DEFAULT_SUFFIX)
  19. DEEPUSER_GLOBAL = "uid=DEEPUSER_GLOBAL, {}".format(NESTEDGROUP_OU_GLOBAL)
  20. DEEPUSER1_GLOBAL = "uid=DEEPUSER1_GLOBAL, {}".format(NESTEDGROUP_OU_GLOBAL)
  21. DEEPUSER2_GLOBAL = "uid=DEEPUSER2_GLOBAL, {}".format(NESTEDGROUP_OU_GLOBAL)
  22. DEEPUSER3_GLOBAL = "uid=DEEPUSER3_GLOBAL, {}".format(NESTEDGROUP_OU_GLOBAL)
  23. DEEPGROUPSCRATCHENTRY_GLOBAL = "uid=scratchEntry,{}".format(NESTEDGROUP_OU_GLOBAL)
  24. GROUPDNATTRSCRATCHENTRY_GLOBAL = "uid=GROUPDNATTRSCRATCHENTRY_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  25. GROUPDNATTRCHILDSCRATCHENTRY_GLOBAL = "uid=c1,{}".format(GROUPDNATTRSCRATCHENTRY_GLOBAL)
  26. NEWCHILDSCRATCHENTRY_GLOBAL = "uid=newChild,{}".format(NESTEDGROUP_OU_GLOBAL)
  27. BIG_GLOBAL = "cn=BIG_GLOBAL Group,{}".format(DEFAULT_SUFFIX)
  28. ALLGROUPS_GLOBAL = "cn=ALLGROUPS_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  29. GROUPA_GLOBAL = "cn=GROUPA_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  30. GROUPB_GLOBAL = "cn=GROUPB_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  31. GROUPC_GLOBAL = "cn=GROUPC_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  32. GROUPD_GLOBAL = "cn=GROUPD_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  33. GROUPE_GLOBAL = "cn=GROUPE_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  34. GROUPF_GLOBAL = "cn=GROUPF_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  35. GROUPG_GLOBAL = "cn=GROUPG_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  36. GROUPH_GLOBAL = "cn=GROUPH_GLOBAL,{}".format(NESTEDGROUP_OU_GLOBAL)
  37. CONTAINER_1_DELADD = "ou=Product Development,{}".format(DEFAULT_SUFFIX)
  38. CONTAINER_2_DELADD = "ou=Accounting,{}".format(DEFAULT_SUFFIX)
  39. @pytest.fixture(scope="function")
  40. def aci_of_user(request, topo):
  41. aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
  42. def finofaci():
  43. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  44. domain.set('aci', None)
  45. for i in aci_list:
  46. domain.add("aci", i)
  47. request.addfinalizer(finofaci)
  48. @pytest.fixture(scope="module")
  49. def test_user(request, topo):
  50. for demo in ['Product Development', 'Accounting', 'Product Testing', 'nestedgroup', 'ACLGroup']:
  51. OrganizationalUnit(topo.standalone, "ou={},{}".format(demo, DEFAULT_SUFFIX)).create(properties={'ou': demo})
  52. user = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou=Accounting')
  53. for demo1 in ['Ted Morris', 'David Miller']:
  54. user.create(properties= {
  55. 'uid': demo1,
  56. 'cn': demo1,
  57. 'sn': 'user',
  58. 'uidNumber': '1000',
  59. 'gidNumber': '2000',
  60. 'homeDirectory': '/home/' + demo1,
  61. 'userPassword': PW_DM
  62. })
  63. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX, 'ou=nestedgroup')
  64. for demo1 in ['DEEPUSER_GLOBAL', 'scratchEntry', 'DEEPUSER2_GLOBAL', 'DEEPUSER1_GLOBAL',
  65. 'DEEPUSER3_GLOBAL', 'GROUPDNATTRSCRATCHENTRY_GLOBAL', 'newChild']:
  66. uas.create(properties={
  67. 'uid': demo1,
  68. 'cn': demo1,
  69. 'sn': 'user',
  70. 'uidNumber': '1000',
  71. 'gidNumber': '2000',
  72. 'homeDirectory': '/home/' + demo1,
  73. 'userPassword': PW_DM
  74. })
  75. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX, 'uid=GROUPDNATTRSCRATCHENTRY_GLOBAL,ou=nestedgroup')
  76. for demo1 in ['c1', 'CHILD1_GLOBAL']:
  77. uas.create(properties={
  78. 'uid': demo1,
  79. 'cn': demo1,
  80. 'sn': 'user',
  81. 'uidNumber': '1000',
  82. 'gidNumber': '2000',
  83. 'homeDirectory': '/home/' + demo1,
  84. 'userPassword': PW_DM
  85. })
  86. grp = UniqueGroups(topo.standalone, DEFAULT_SUFFIX, rdn='ou=nestedgroup')
  87. for i in [('ALLGROUPS_GLOBAL', GROUPA_GLOBAL), ('GROUPA_GLOBAL', GROUPB_GLOBAL), ('GROUPB_GLOBAL', GROUPC_GLOBAL),
  88. ('GROUPC_GLOBAL', GROUPD_GLOBAL), ('GROUPD_GLOBAL', GROUPE_GLOBAL), ('GROUPE_GLOBAL', GROUPF_GLOBAL),
  89. ('GROUPF_GLOBAL', GROUPG_GLOBAL), ('GROUPG_GLOBAL', GROUPH_GLOBAL), ('GROUPH_GLOBAL', DEEPUSER_GLOBAL)]:
  90. grp.create(properties={'cn': i[0],
  91. 'ou': 'groups',
  92. 'uniquemember': i[1]
  93. })
  94. grp = UniqueGroup(topo.standalone, 'cn=BIG_GLOBAL Group,{}'.format(DEFAULT_SUFFIX))
  95. grp.create(properties={'cn': 'BIG_GLOBAL Group',
  96. 'ou': 'groups',
  97. 'uniquemember': ["uid=Ted Morris,ou=Accounting,{}".format(DEFAULT_SUFFIX),
  98. "uid=David Miller,ou=Accounting,{}".format(DEFAULT_SUFFIX),]
  99. })
  100. def test_caching_changes(topo, aci_of_user, test_user):
  101. """
  102. Add user and then test deny
  103. :id: 26ed2dc2-783f-11e8-b1a5-8c16451d917b
  104. :setup: server
  105. :steps:
  106. 1. Add test entry
  107. 2. Take a count of users using DN_DM
  108. 3. Add test user
  109. 4. add aci
  110. 5. test should fullfil the aci rules
  111. :expectedresults:
  112. 1. Entry should be added
  113. 2. Operation should succeed
  114. 3. Operation should succeed
  115. 4. Operation should succeed
  116. 5. Operation should succeed
  117. """
  118. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr="roomnumber")(version 3.0; acl "ACLGroup"; deny ( read, search ) userdn = "ldap:///all" ;)')
  119. user = UserAccounts(topo.standalone, DEFAULT_SUFFIX, "ou=AclGroup").create_test_user()
  120. user.set('roomnumber', '3445')
  121. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  122. # targetattr="roomnumber" will be denied access
  123. user = UserAccount(conn, 'uid=test_user_1000,ou=ACLGroup,dc=example,dc=com')
  124. with pytest.raises(AssertionError):
  125. assert user.get_attr_val_utf8('roomNumber')
  126. UserAccount(topo.standalone, 'uid=test_user_1000,ou=ACLGroup,dc=example,dc=com').delete()
  127. def test_deny_group_member_all_rights_to_user(topo, aci_of_user, test_user):
  128. """
  129. Try deleting user while no access
  130. :id: 0da68a4c-7840-11e8-98c2-8c16451d917b
  131. :setup: server
  132. :steps:
  133. 1. Add test entry
  134. 2. Take a count of users using DN_DM
  135. 3. delete test user
  136. 4. add aci
  137. 5. test should fullfil the aci rules
  138. :expectedresults:
  139. 1. Entry should be added
  140. 2. Operation should succeed
  141. 3. Operation should succeed
  142. 4. Operation should succeed
  143. 5. Operation should succeed
  144. """
  145. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr="*")(version 3.0; acl "ACLGroup"; deny (all) groupdn = "ldap:///{}" ;)'.format(BIG_GLOBAL))
  146. conn = UserAccount(topo.standalone, "uid=Ted Morris, ou=Accounting, {}".format(DEFAULT_SUFFIX)).bind(PW_DM)
  147. # group BIG_GLOBAL will have no access
  148. user = UserAccount(conn, DEEPUSER3_GLOBAL)
  149. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  150. user.delete()
  151. def test_deny_group_member_all_rights_to_group_members(topo, aci_of_user, test_user):
  152. """
  153. Deny group member all rights
  154. :id: 2d4ff70c-7840-11e8-8472-8c16451d917b
  155. :setup: server
  156. :steps:
  157. 1. Add test entry
  158. 2. Take a count of users using DN_DM
  159. 3. Add test user
  160. 4. add aci
  161. 5. test should fullfil the aci rules
  162. :expectedresults:
  163. 1. Entry should be added
  164. 2. Operation should succeed
  165. 3. Operation should succeed
  166. 4. Operation should succeed
  167. 5. Operation should succeed
  168. """
  169. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr="*")(version 3.0; acl "ACLGroup"; deny (all) groupdn = "ldap:///{}" ;)'.format(BIG_GLOBAL))
  170. UserAccounts(topo.standalone, DEFAULT_SUFFIX, "ou=AclGroup").create_test_user()
  171. conn = UserAccount(topo.standalone, "uid=Ted Morris, ou=Accounting, {}".format(DEFAULT_SUFFIX)).bind(PW_DM)
  172. # group BIG_GLOBAL no access
  173. user = UserAccount(conn, 'uid=test_user_1000,ou=ACLGroup,dc=example,dc=com')
  174. with pytest.raises(IndexError):
  175. user.get_attr_val_utf8('uid')
  176. UserAccount(topo.standalone, 'uid=test_user_1000,ou=ACLGroup,dc=example,dc=com').delete()
  177. def test_deeply_nested_groups_aci_denial(topo, test_user, aci_of_user):
  178. """
  179. Test deeply nested groups (1)
  180. This aci will not allow search or modify to a user too deep to be detected.
  181. :id: 3d98229c-7840-11e8-9f55-8c16451d917b
  182. :setup: server
  183. :steps:
  184. 1. Add test entry
  185. 2. Take a count of users using DN_DM
  186. 3. Add test user
  187. 4. add aci
  188. 5. test should fullfil the aci rules
  189. :expectedresults:
  190. 1. Entry should be added
  191. 2. Operation should succeed
  192. 3. Operation should succeed
  193. 4. Operation should succeed
  194. 5. Operation should succeed
  195. """
  196. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr="*")(version 3.0; acl "ACLGroup"; allow (all) groupdn = "ldap:///{}" ;)'.format(ALLGROUPS_GLOBAL))
  197. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  198. # ALLGROUPS_GLOBAL have all access
  199. assert UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL).get_attr_val_utf8('uid') == 'scratchEntry'
  200. user = UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  201. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  202. user.delete()
  203. def test_deeply_nested_groups_aci_denial_two(topo, test_user, aci_of_user):
  204. """
  205. Test deeply nested groups (2)
  206. This aci will allow search and modify
  207. :id: 4ef6348e-7840-11e8-a70c-8c16451d917b
  208. :setup: server
  209. :steps:
  210. 1. Add test entry
  211. 2. Take a count of users using DN_DM
  212. 3. Add test user
  213. 4. add aci
  214. 5. test should fullfil the aci rules
  215. :expectedresults:
  216. 1. Entry should be added
  217. 2. Operation should succeed
  218. 3. Operation should succeed
  219. 4. Operation should succeed
  220. 5. Operation should succeed
  221. """
  222. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr="*")(version 3.0; acl "ACLGroup"; allow (all) groupdn = "ldap:///{}" ;)'.format(GROUPE_GLOBAL))
  223. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  224. # GROUPE_GLOBAL have all access
  225. user = UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  226. user.add("sn", "Fred")
  227. user.remove("sn", "Fred")
  228. def test_deeply_nested_groups_aci_allow(topo, test_user, aci_of_user):
  229. """
  230. Test deeply nested groups (3)
  231. This aci will allow search and modify
  232. :id: 8d338210-7840-11e8-8584-8c16451d917b
  233. :setup: server
  234. :steps:
  235. 1. Add test entry
  236. 2. Take a count of users using DN_DM
  237. 3. Add test user
  238. 4. add aci
  239. 5. test should fullfil the aci rules
  240. :expectedresults:
  241. 1. Entry should be added
  242. 2. Operation should succeed
  243. 3. Operation should succeed
  244. 4. Operation should succeed
  245. 5. Operation should succeed
  246. """
  247. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ['(targetattr="*")(version 3.0; acl "ACLGroup"; allow (all) groupdn = "ldap:///{}" ;)'.format(ALLGROUPS_GLOBAL), '(targetattr="*")(version 3.0; acl "ACLGroup"; allow (all) groupdn = "ldap:///{}" ;)'.format(GROUPE_GLOBAL)])
  248. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  249. # test deeply nested groups
  250. user = UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  251. user.add("sn", "Fred")
  252. user.remove("sn", "Fred")
  253. def test_deeply_nested_groups_aci_allow_two(topo, test_user, aci_of_user):
  254. """
  255. This aci will not allow search or modify to a user too deep to be detected.
  256. :id: 8d3459c4-7840-11e8-8ed8-8c16451d917b
  257. :setup: server
  258. :steps:
  259. 1. Add test entry
  260. 2. Take a count of users using DN_DM
  261. 3. Add test user
  262. 4. add aci
  263. 5. test should fullfil the aci rules
  264. :expectedresults:
  265. 1. Entry should be added
  266. 2. Operation should succeed
  267. 3. Operation should succeed
  268. 4. Operation should succeed
  269. 5. Operation should succeed
  270. """
  271. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr="*")(version 3.0; acl "ACLGroup"; allow (all) groupdn = "ldap:///{}" ;)'.format(ALLGROUPS_GLOBAL))
  272. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  273. # This aci should not allow search or modify to a user too deep to be detected.
  274. user = UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  275. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  276. user.add("sn", "Fred")
  277. assert user.get_attr_val_utf8('uid') == 'scratchEntry'
  278. def test_undefined_in_group_eval(topo, test_user, aci_of_user):
  279. """
  280. This aci will not allow access .
  281. :id: f1605e16-7840-11e8-b954-8c16451d917b
  282. :setup: server
  283. :steps:
  284. 1. Add test entry
  285. 2. Take a count of users using DN_DM
  286. 3. Add test user
  287. 4. add aci
  288. 5. test should fullfil the aci rules
  289. :expectedresults:
  290. 1. Entry should be added
  291. 2. Operation should succeed
  292. 3. Operation should succeed
  293. 4. Operation should succeed
  294. 5. Operation should succeed
  295. """
  296. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr="*")(version 3.0; acl "ACLGroup"; allow (all) groupdn != "ldap:///{}" ;)'.format(ALLGROUPS_GLOBAL))
  297. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  298. # This aci should NOT allow access
  299. user = UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  300. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  301. user.add("sn", "Fred")
  302. assert user.get_attr_val_utf8('uid') == 'scratchEntry'
  303. def test_undefined_in_group_eval_two(topo, test_user, aci_of_user):
  304. """
  305. This aci will allow access
  306. :id: fcfbcce2-7840-11e8-ba77-8c16451d917b
  307. :setup: server
  308. :steps:
  309. 1. Add test entry
  310. 2. Take a count of users using DN_DM
  311. 3. Add test user
  312. 4. add aci
  313. 5. test should fullfil the aci rules
  314. :expectedresults:
  315. 1. Entry should be added
  316. 2. Operation should succeed
  317. 3. Operation should succeed
  318. 4. Operation should succeed
  319. 5. Operation should succeed
  320. """
  321. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr=*)(version 3.0; aci "tester"; allow(all) groupdn = "ldap:///{}\ || ldap:///{}";)'.format(ALLGROUPS_GLOBAL, GROUPG_GLOBAL))
  322. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  323. user = UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  324. # This aci should allow access
  325. user.add("sn", "Fred")
  326. assert UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL).get_attr_val_utf8('uid') == 'scratchEntry'
  327. user.remove("sn", "Fred")
  328. def test_undefined_in_group_eval_three(topo, test_user, aci_of_user):
  329. """
  330. This aci will allow access
  331. :id: 04943dcc-7841-11e8-8c46-8c16451d917b
  332. :setup: server
  333. :steps:
  334. 1. Add test entry
  335. 2. Take a count of users using DN_DM
  336. 3. Add test user
  337. 4. add aci
  338. 5. test should fullfil the aci rules
  339. :expectedresults:
  340. 1. Entry should be added
  341. 2. Operation should succeed
  342. 3. Operation should succeed
  343. 4. Operation should succeed
  344. 5. Operation should succeed
  345. """
  346. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr=*)(version 3.0; aci "tester"; allow(all) groupdn = "ldap:///{}\ || ldap:///{}";)'.format(GROUPG_GLOBAL, ALLGROUPS_GLOBAL))
  347. conn = UserAccount(topo.standalone, DEEPUSER_GLOBAL).bind(PW_DM)
  348. user = Domain(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  349. # test UNDEFINED in group
  350. user.add("sn", "Fred")
  351. assert UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL).get_attr_val_utf8('uid') == 'scratchEntry'
  352. user.remove("sn", "Fred")
  353. def test_undefined_in_group_eval_four(topo, test_user, aci_of_user):
  354. """
  355. This aci will not allow access
  356. :id: 0b03d10e-7841-11e8-9341-8c16451d917b
  357. :setup: server
  358. :steps:
  359. 1. Add test entry
  360. 2. Take a count of users using DN_DM
  361. 3. Add test user
  362. 4. add aci
  363. 5. test should fullfil the aci rules
  364. :expectedresults:
  365. 1. Entry should be added
  366. 2. Operation should succeed
  367. 3. Operation should succeed
  368. 4. Operation should succeed
  369. 5. Operation should succeed
  370. """
  371. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(targetattr=*)(version 3.0; aci "tester"; allow(all) groupdn != "ldap:///{}\ || ldap:///{}";)'.format(ALLGROUPS_GLOBAL, GROUPG_GLOBAL))
  372. conn = UserAccount(topo.standalone, DEEPUSER1_GLOBAL).bind(PW_DM)
  373. # test UNDEFINED in group
  374. user = UserAccount(conn, DEEPGROUPSCRATCHENTRY_GLOBAL)
  375. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  376. user.add("sn", "Fred")
  377. assert user.get_attr_val_utf8('uid') == 'scratchEntry'
  378. if __name__ == "__main__":
  379. CURRENT_FILE = os.path.realpath(__file__)
  380. pytest.main("-s -v %s" % CURRENT_FILE)