modrdn_test.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.group import Group, UniqueGroup
  13. from lib389.idm.organizationalunit import OrganizationalUnit, OrganizationalUnits
  14. from lib389.topologies import topology_st as topo
  15. from lib389.idm.domain import Domain
  16. pytestmark = pytest.mark.tier1
  17. CONTAINER_1_DELADD = "ou=Product Development,{}".format(DEFAULT_SUFFIX)
  18. CONTAINER_2_DELADD = "ou=Accounting,{}".format(DEFAULT_SUFFIX)
  19. USER_DELADD = "cn=Jeff Vedder,{}".format(CONTAINER_1_DELADD)
  20. USER_WITH_ACI_DELADD = "cn=Sam Carter,{}".format(CONTAINER_2_DELADD)
  21. DYNAMIC_MODRDN = "cn=Test DYNAMIC_MODRDN Group 70, {}".format(DEFAULT_SUFFIX)
  22. SAM_DAMMY_MODRDN = "cn=Sam Carter1,ou=Accounting,{}".format(DEFAULT_SUFFIX)
  23. TRAC340_MODRDN = "cn=TRAC340_MODRDN,{}".format(DEFAULT_SUFFIX)
  24. NEWENTRY9_MODRDN = "cn=NEWENTRY9_MODRDN,{}".format("ou=People,{}".format(DEFAULT_SUFFIX))
  25. OU0_OU_MODRDN = "ou=OU0,{}".format(DEFAULT_SUFFIX)
  26. OU2_OU_MODRDN = "ou=OU2,{}".format(DEFAULT_SUFFIX)
  27. @pytest.fixture(scope="function")
  28. def aci_of_user(request, topo):
  29. aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
  30. def finofaci():
  31. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  32. domain.set('aci', None)
  33. for i in aci_list:
  34. domain.add("aci", i)
  35. request.addfinalizer(finofaci)
  36. @pytest.fixture(scope="function")
  37. def _add_user(request, topo):
  38. ou = OrganizationalUnit(topo.standalone, 'ou=Product Development,{}'.format(DEFAULT_SUFFIX))
  39. ou.create(properties={'ou': 'Product Development'})
  40. ou = OrganizationalUnit(topo.standalone, 'ou=Accounting,{}'.format(DEFAULT_SUFFIX))
  41. ou.create(properties={'ou': 'Accounting'})
  42. groups = Group(topo.standalone, DYNAMIC_MODRDN)
  43. group_properties = {"cn": "Test DYNAMIC_MODRDN Group 70",
  44. "objectclass": ["top", 'groupofURLs'],
  45. 'memberURL': 'ldap:///{}??base?(cn=*)'.format(USER_WITH_ACI_DELADD)}
  46. groups.create(properties=group_properties)
  47. properties = {
  48. 'uid': 'Jeff Vedder',
  49. 'cn': 'Jeff Vedder',
  50. 'sn': 'user',
  51. 'uidNumber': '1000',
  52. 'gidNumber': '2000',
  53. 'homeDirectory': '/home/' + 'JeffVedder',
  54. 'userPassword': PW_DM
  55. }
  56. user = UserAccount(topo.standalone, 'cn=Jeff Vedder,ou=Product Development,{}'.format(DEFAULT_SUFFIX))
  57. user.create(properties=properties)
  58. properties = {
  59. 'uid': 'Sam Carter',
  60. 'cn': 'Sam Carter',
  61. 'sn': 'user',
  62. 'uidNumber': '1000',
  63. 'gidNumber': '2000',
  64. 'homeDirectory': '/home/' + 'SamCarter',
  65. 'userPassword': PW_DM
  66. }
  67. user = UserAccount(topo.standalone, 'cn=Sam Carter,ou=Accounting,{}'.format(DEFAULT_SUFFIX))
  68. user.create(properties=properties)
  69. def fin():
  70. for DN in [USER_DELADD,USER_WITH_ACI_DELADD,DYNAMIC_MODRDN,CONTAINER_2_DELADD,CONTAINER_1_DELADD]:
  71. UserAccount(topo.standalone, DN).delete()
  72. request.addfinalizer(fin)
  73. def test_allow_write_privilege_to_anyone(topo, _add_user, aci_of_user):
  74. """
  75. Modrdn Test 1 Allow write privilege to anyone
  76. :id: 4406f12e-7932-11e8-9dea-8c16451d917b
  77. :setup: server
  78. :steps:
  79. 1. Add test entry
  80. 2. Add ACI
  81. 3. User should follow ACI role
  82. :expectedresults:
  83. 1. Entry should be added
  84. 2. Operation should succeed
  85. 3. Operation should succeed
  86. """
  87. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",
  88. '(target ="ldap:///{}")(targetattr=*)(version 3.0;acl "$tet_thistest";allow '
  89. '(write) (userdn = "ldap:///anyone");)'.format(DEFAULT_SUFFIX))
  90. conn = Anonymous(topo.standalone).bind()
  91. # Allow write privilege to anyone
  92. useraccount = UserAccount(conn, USER_WITH_ACI_DELADD)
  93. useraccount.rename("cn=Jeff Vedder")
  94. assert 'cn=Jeff Vedder,ou=Accounting,dc=example,dc=com' == useraccount.dn
  95. useraccount = UserAccount(conn, "cn=Jeff Vedder,ou=Accounting,dc=example,dc=com")
  96. useraccount.rename("cn=Sam Carter")
  97. assert 'cn=Sam Carter,ou=Accounting,dc=example,dc=com' == useraccount.dn
  98. def test_allow_write_privilege_to_dynamic_group_with_scope_set_to_base_in_ldap_url(
  99. topo, _add_user, aci_of_user
  100. ):
  101. """
  102. Modrdn Test 2 Allow write privilege to DYNAMIC_MODRDN group with scope set to base in LDAP URL
  103. :id: 4c0f8c00-7932-11e8-8398-8c16451d917b
  104. :setup: server
  105. :steps:
  106. 1. Add test entry
  107. 2. Add ACI
  108. 3. User should follow ACI role
  109. :expectedresults:
  110. 1. Entry should be added
  111. 2. Operation should succeed
  112. 3. Operation should succeed
  113. """
  114. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(target = ldap:///{})(targetattr=*)(version 3.0; acl "$tet_thistest"; allow(all)(groupdn = "ldap:///{}"); )'.format(DEFAULT_SUFFIX, DYNAMIC_MODRDN))
  115. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  116. # Allow write privilege to DYNAMIC_MODRDN group with scope set to base in LDAP URL
  117. useraccount = UserAccount(conn, USER_DELADD)
  118. useraccount.rename("cn=Jeffbo Vedder")
  119. assert 'cn=Jeffbo Vedder,ou=Product Development,dc=example,dc=com' == useraccount.dn
  120. useraccount = UserAccount(conn, "cn=Jeffbo Vedder,{}".format(CONTAINER_1_DELADD))
  121. useraccount.rename("cn=Jeff Vedder")
  122. assert 'cn=Jeff Vedder,ou=Product Development,dc=example,dc=com' == useraccount.dn
  123. def test_write_access_to_naming_atributes(topo, _add_user, aci_of_user):
  124. """
  125. Test for write access to naming atributes (1)
  126. Test that check for add writes to the new naming attr
  127. :id: 532fc630-7932-11e8-8924-8c16451d917b
  128. :setup: server
  129. :steps:
  130. 1. Add test entry
  131. 2. Add ACI
  132. 3. User should follow ACI role
  133. :expectedresults:
  134. 1. Entry should be added
  135. 2. Operation should succeed
  136. 3. Operation should succeed
  137. """
  138. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", '(target ="ldap:///{}")(targetattr != "uid")(version 3.0;acl "$tet_thistest";allow (write) (userdn = "ldap:///anyone");)'.format(DEFAULT_SUFFIX))
  139. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  140. #Test for write access to naming atributes
  141. useraccount = UserAccount(conn, USER_WITH_ACI_DELADD)
  142. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  143. useraccount.rename("uid=Jeffbo Vedder")
  144. def test_write_access_to_naming_atributes_two(topo, _add_user, aci_of_user):
  145. """
  146. Test for write access to naming atributes (2)
  147. :id: 5a2077d2-7932-11e8-9e7b-8c16451d917b
  148. :setup: server
  149. :steps:
  150. 1. Add test entry
  151. 2. Add ACI
  152. 3. User should follow ACI role
  153. 4. Now try to modrdn it to cn, won't work if request deleteoldrdn.
  154. :expectedresults:
  155. 1. Entry should be added
  156. 2. Operation should succeed
  157. 3. Operation should succeed
  158. 4. Operation should not succeed
  159. """
  160. Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", '(target ="ldap:///{}")(targetattr != "uid")(version 3.0;acl "$tet_thistest";allow (write) (userdn = "ldap:///anyone");)'.format(DEFAULT_SUFFIX))
  161. properties = {
  162. 'uid': 'Sam Carter1',
  163. 'cn': 'Sam Carter1',
  164. 'sn': 'user',
  165. 'uidNumber': '1000',
  166. 'gidNumber': '2000',
  167. 'homeDirectory': '/home/' + 'SamCarter1'
  168. }
  169. user = UserAccount(topo.standalone, 'cn=Sam Carter1,ou=Accounting,{}'.format(DEFAULT_SUFFIX))
  170. user.create(properties=properties)
  171. user.set("userPassword", "password")
  172. conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
  173. # Test for write access to naming atributes
  174. useraccount = UserAccount(conn, SAM_DAMMY_MODRDN)
  175. with pytest.raises(ldap.INSUFFICIENT_ACCESS):
  176. useraccount.rename("uid=Jeffbo Vedder")
  177. UserAccount(topo.standalone, SAM_DAMMY_MODRDN).delete()
  178. @pytest.mark.bz950351
  179. def test_access_aci_list_contains_any_deny_rule(topo, _add_user, aci_of_user):
  180. """
  181. Testing bug #950351: RHDS denies MODRDN access if ACI list contains any DENY rule
  182. Bug description: If you create a deny ACI for some or more attributes there is incorrect behaviour
  183. as you cannot rename the entry anymore
  184. :id: 62cbbb8a-7932-11e8-96a7-8c16451d917b
  185. :setup: server
  186. :steps:
  187. 1. Add test entry
  188. 2. Adding a new ou ou=People to $BASEDN
  189. 3. Adding a user NEWENTRY9_MODRDN to ou=People,$BASEDN
  190. 4. Adding an allow rule for NEWENTRY9_MODRDN and for others an aci deny rule
  191. :expectedresults:
  192. 1. Entry should be added
  193. 2. Operation should succeed
  194. 3. Operation should succeed
  195. 4. Operation should succeed
  196. """
  197. properties = {
  198. 'uid': 'NEWENTRY9_MODRDN',
  199. 'cn': 'NEWENTRY9_MODRDN_People',
  200. 'sn': 'user',
  201. 'uidNumber': '1000',
  202. 'gidNumber': '2000',
  203. 'homeDirectory': '/home/' + 'NEWENTRY9_MODRDN'
  204. }
  205. user = UserAccount(topo.standalone, 'cn=NEWENTRY9_MODRDN,ou=People,{}'.format(DEFAULT_SUFFIX))
  206. user.create(properties=properties)
  207. user.set("userPassword", "password")
  208. user.set("telephoneNumber", "989898191")
  209. user.set("mail", "[email protected]")
  210. user.set("givenName", "givenName")
  211. user.set("uid", "NEWENTRY9_MODRDN")
  212. OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX).get('People').add("aci", ['(targetattr = "*") '
  213. '(version 3.0;acl "admin";allow (all)(userdn = "ldap:///{}");)'.format(NEWENTRY9_MODRDN),
  214. '(targetattr = "mail") (version 3.0;acl "deny_mail";deny (write)(userdn = "ldap:///anyone");)',
  215. '(targetattr = "uid") (version 3.0;acl "allow uid";allow (write)(userdn = "ldap:///{}");)'.format(NEWENTRY9_MODRDN)])
  216. UserAccount(topo.standalone, NEWENTRY9_MODRDN).replace("userpassword", "Anuj")
  217. useraccount = UserAccount(topo.standalone, NEWENTRY9_MODRDN)
  218. useraccount.rename("uid=newrdnchnged")
  219. assert 'uid=newrdnchnged,ou=People,dc=example,dc=com' == useraccount.dn
  220. def test_renaming_target_entry(topo, _add_user, aci_of_user):
  221. """
  222. Test for renaming target entry
  223. :id: 6be1d33a-7932-11e8-9115-8c16451d917b
  224. :setup: server
  225. :steps:
  226. 1. Add test entry
  227. 2. Create a test user entry
  228. 3.Create a new ou entry with an aci
  229. 4. Make sure uid=$MYUID has the access
  230. 5. Rename ou=OU0 to ou=OU1
  231. 6. Create another ou=OU2
  232. 7. Move ou=OU1 under ou=OU2
  233. 8. Make sure uid=$MYUID still has the access
  234. :expectedresults:
  235. 1. Entry should be added
  236. 2. Operation should succeed
  237. 3. Operation should succeed
  238. 4. Operation should succeed
  239. 5. Operation should succeed
  240. 6. Operation should succeed
  241. 7. Operation should succeed
  242. 8. Operation should succeed
  243. """
  244. properties = {
  245. 'uid': 'TRAC340_MODRDN',
  246. 'cn': 'TRAC340_MODRDN',
  247. 'sn': 'user',
  248. 'uidNumber': '1000',
  249. 'gidNumber': '2000',
  250. 'homeDirectory': '/home/' + 'TRAC340_MODRDN'
  251. }
  252. user = UserAccount(topo.standalone, 'cn=TRAC340_MODRDN,{}'.format(DEFAULT_SUFFIX))
  253. user.create(properties=properties)
  254. user.set("userPassword", "password")
  255. ou = OrganizationalUnit(topo.standalone, 'ou=OU0,{}'.format(DEFAULT_SUFFIX))
  256. ou.create(properties={'ou': 'OU0'})
  257. ou.set('aci', '(targetattr=*)(version 3.0; acl "$MYUID";allow(read, search, compare) userdn = "ldap:///{}";)'.format(TRAC340_MODRDN))
  258. conn = UserAccount(topo.standalone, TRAC340_MODRDN).bind(PW_DM)
  259. assert OrganizationalUnits(conn, DEFAULT_SUFFIX).get('OU0')
  260. # Test for renaming target entry
  261. OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX).get('OU0').rename("ou=OU1")
  262. assert OrganizationalUnits(conn, DEFAULT_SUFFIX).get('OU1')
  263. ou = OrganizationalUnit(topo.standalone, 'ou=OU2,{}'.format(DEFAULT_SUFFIX))
  264. ou.create(properties={'ou': 'OU2'})
  265. # Test for renaming target entry
  266. OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX).get('OU1').rename("ou=OU1", newsuperior=OU2_OU_MODRDN)
  267. assert OrganizationalUnits(conn, DEFAULT_SUFFIX).get('OU1')
  268. if __name__ == "__main__":
  269. CURRENT_FILE = os.path.realpath(__file__)
  270. pytest.main("-s -v %s" % CURRENT_FILE)