misc_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. """
  2. # --- BEGIN COPYRIGHT BLOCK ---
  3. # Copyright (C) 2019 RED Hat, Inc.
  4. # All rights reserved.
  5. #
  6. # License: GPL (version 3 or any later version).
  7. # See LICENSE for details.
  8. # --- END COPYRIGHT BLOCK ----
  9. """
  10. import os
  11. import pytest
  12. from lib389._constants import DEFAULT_SUFFIX, PW_DM
  13. from lib389.idm.user import UserAccount, UserAccounts
  14. from lib389._mapped_object import DSLdapObject
  15. from lib389.idm.account import Accounts, Anonymous
  16. from lib389.idm.organizationalunit import OrganizationalUnit, OrganizationalUnits
  17. from lib389.idm.group import Group, Groups
  18. from lib389.topologies import topology_st as topo
  19. from lib389.idm.domain import Domain
  20. from lib389.plugins import ACLPlugin
  21. import ldap
  22. pytestmark = pytest.mark.tier1
  23. PEOPLE = "ou=PEOPLE,{}".format(DEFAULT_SUFFIX)
  24. DYNGROUP = "cn=DYNGROUP,{}".format(PEOPLE)
  25. CONTAINER_1_DELADD = "ou=Product Development,{}".format(DEFAULT_SUFFIX)
  26. CONTAINER_2_DELADD = "ou=Accounting,{}".format(DEFAULT_SUFFIX)
  27. @pytest.fixture(scope="function")
  28. def aci_of_user(request, topo):
  29. """
  30. :param request:
  31. :param topo:
  32. """
  33. aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
  34. def finofaci():
  35. """
  36. Removes and Restores ACIs after the test.
  37. """
  38. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  39. domain.remove_all('aci')
  40. for i in aci_list:
  41. domain.add("aci", i)
  42. request.addfinalizer(finofaci)
  43. @pytest.fixture(scope="function")
  44. def clean(request, topo):
  45. """
  46. :param request:
  47. :param topo:
  48. """
  49. ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX)
  50. try:
  51. for i in ['Product Development', 'Accounting']:
  52. ous.create(properties={'ou': i})
  53. except ldap.ALREADY_EXISTS as eoor_eoor:
  54. topo.standalone.log.info("Exception (expected): %s" % type(eoor_eoor).__name__)
  55. def fin():
  56. """
  57. Deletes entries after the test.
  58. """
  59. for scope_scope in [CONTAINER_1_DELADD, CONTAINER_2_DELADD, PEOPLE]:
  60. try:
  61. DSLdapObject(topo.standalone, scope_scope).delete()
  62. except ldap.ALREADY_EXISTS as eoor_eoor:
  63. topo.standalone.log.info("Exception (expected): %s" % type(eoor_eoor).__name__)
  64. request.addfinalizer(fin)
  65. def test_accept_aci_in_addition_to_acl(topo, clean, aci_of_user):
  66. """
  67. Misc Test 2 accept aci in addition to acl
  68. :id: 8e9408fa-7db8-11e8-adaa-8c16451d917b
  69. :setup: Standalone Instance
  70. :steps:
  71. 1. Add test entry
  72. 2. Add ACI
  73. 3. User should follow ACI role
  74. :expectedresults:
  75. 1. Entry should be added
  76. 2. Operation should succeed
  77. 3. Operation should succeed
  78. """
  79. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou=product development')
  80. user = uas.create_test_user()
  81. for i in [('mail', '[email protected]'), ('givenname', 'Anuj'), ('userPassword', PW_DM)]:
  82. user.set(i[0], i[1])
  83. aci_target = "(targetattr=givenname)"
  84. aci_allow = ('(version 3.0; acl "Name of the ACI"; deny (read, search, compare, write)')
  85. aci_subject = 'userdn="ldap:///anyone";)'
  86. Domain(topo.standalone, CONTAINER_1_DELADD).add("aci", aci_target + aci_allow + aci_subject)
  87. conn = Anonymous(topo.standalone).bind()
  88. # aci will block targetattr=givenname to anyone
  89. user = UserAccount(conn, user.dn)
  90. with pytest.raises(AssertionError):
  91. assert user.get_attr_val_utf8('givenname') == 'Anuj'
  92. # aci will allow targetattr=uid to anyone
  93. assert user.get_attr_val_utf8('uid') == 'test_user_1000'
  94. for i in uas.list():
  95. i.delete()
  96. @pytest.mark.bz334451
  97. def test_more_then_40_acl_will_crash_slapd(topo, clean, aci_of_user):
  98. """
  99. bug 334451 : more then 40 acl will crash slapd
  100. superseded by Bug 772778 - acl cache overflown problem with > 200 acis
  101. :id: 93a44c60-7db8-11e8-9439-8c16451d917b
  102. :setup: Standalone Instance
  103. :steps:
  104. 1. Add test entry
  105. 2. Add ACI
  106. 3. User should follow ACI role
  107. :expectedresults:
  108. 1. Entry should be added
  109. 2. Operation should succeed
  110. 3. Operation should succeed
  111. """
  112. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou=Accounting')
  113. user = uas.create_test_user()
  114. aci_target = '(target ="ldap:///{}")(targetattr !="userPassword")'.format(CONTAINER_1_DELADD)
  115. # more_then_40_acl_will not crash_slapd
  116. for i in range(40):
  117. aci_allow = '(version 3.0;acl "ACI_{}";allow (read, search, compare)'.format(i)
  118. aci_subject = 'userdn="ldap:///anyone";)'
  119. aci_body = aci_target + aci_allow + aci_subject
  120. Domain(topo.standalone, CONTAINER_1_DELADD).add("aci", aci_body)
  121. conn = Anonymous(topo.standalone).bind()
  122. assert UserAccount(conn, user.dn).get_attr_val_utf8('uid') == 'test_user_1000'
  123. for i in uas.list():
  124. i.delete()
  125. @pytest.mark.bz345643
  126. def test_search_access_should_not_include_read_access(topo, clean, aci_of_user):
  127. """
  128. bug 345643
  129. Misc Test 4 search access should not include read access
  130. :id: 98ab173e-7db8-11e8-a309-8c16451d917b
  131. :setup: Standalone Instance
  132. :steps:
  133. 1. Add test entry
  134. 2. Add ACI
  135. 3. User should follow ACI role
  136. :expectedresults:
  137. 1. Entry should be added
  138. 2. Operation should succeed
  139. 3. Operation should succeed
  140. """
  141. assert Domain(topo.standalone, DEFAULT_SUFFIX).present('aci')
  142. Domain(topo.standalone, DEFAULT_SUFFIX)\
  143. .add("aci", [f'(target ="ldap:///{DEFAULT_SUFFIX}")(targetattr !="userPassword")'
  144. '(version 3.0;acl "anonymous access";allow (search)'
  145. '(userdn = "ldap:///anyone");)',
  146. f'(target="ldap:///{DEFAULT_SUFFIX}") (targetattr = "*")(version 3.0; '
  147. 'acl "allow self write";allow(write) '
  148. 'userdn = "ldap:///self";)',
  149. f'(target="ldap:///{DEFAULT_SUFFIX}") (targetattr = "*")(version 3.0; '
  150. 'acl "Allow all admin group"; allow(all) groupdn = "ldap:///cn=Directory '
  151. 'Administrators, {}";)'])
  152. conn = Anonymous(topo.standalone).bind()
  153. # search_access_should_not_include_read_access
  154. suffix = Domain(conn, DEFAULT_SUFFIX)
  155. with pytest.raises(AssertionError):
  156. assert suffix.present('aci')
  157. def test_only_allow_some_targetattr(topo, clean, aci_of_user):
  158. """
  159. Misc Test 5 only allow some targetattr (1/2)
  160. :id: 9d27f048-7db8-11e8-a71c-8c16451d917b
  161. :setup: Standalone Instance
  162. :steps:
  163. 1. Add test entry
  164. 2. Add ACI
  165. 3. User should follow ACI role
  166. :expectedresults:
  167. 1. Entry should be added
  168. 2. Operation should succeed
  169. 3. Operation should succeed
  170. """
  171. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=None)
  172. for i in range(1, 3):
  173. user = uas.create_test_user(uid=i, gid=i)
  174. user.replace_many(('cn', 'Anuj1'), ('mail', '[email protected]'))
  175. Domain(topo.standalone, DEFAULT_SUFFIX).\
  176. replace("aci", '(target="ldap:///{}")(targetattr="mail||objectClass")'
  177. '(version 3.0; acl "Test";allow (read,search,compare) '
  178. '(userdn = "ldap:///anyone"); )'.format(DEFAULT_SUFFIX))
  179. conn = Anonymous(topo.standalone).bind()
  180. accounts = Accounts(conn, DEFAULT_SUFFIX)
  181. # aci will allow only mail targetattr
  182. assert len(accounts.filter('(mail=*)')) == 2
  183. # aci will allow only mail targetattr
  184. assert not accounts.filter('(cn=*)')
  185. # with root no , blockage
  186. assert len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(uid=*)')) == 2
  187. for i in uas.list():
  188. i.delete()
  189. def test_only_allow_some_targetattr_two(topo, clean, aci_of_user):
  190. """
  191. Misc Test 6 only allow some targetattr (2/2)"
  192. :id: a188239c-7db8-11e8-903e-8c16451d917b
  193. :setup: Standalone Instance
  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. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=None)
  204. for i in range(5):
  205. user = uas.create_test_user(uid=i, gid=i)
  206. user.replace_many(('mail', '[email protected]'),
  207. ('cn', 'Anuj'), ('userPassword', PW_DM))
  208. user1 = uas.create_test_user()
  209. user1.replace_many(('mail', '[email protected]'), ('userPassword', PW_DM))
  210. Domain(topo.standalone, DEFAULT_SUFFIX).\
  211. replace("aci", '(target="ldap:///{}") (targetattr="mail||objectClass")'
  212. '(targetfilter="cn=Anuj") (version 3.0; acl "$tet_thistest"; '
  213. 'allow (compare,read,search) '
  214. '(userdn = "ldap:///anyone"); )'.format(DEFAULT_SUFFIX))
  215. conn = UserAccount(topo.standalone, user.dn).bind(PW_DM)
  216. # aci will allow only mail targetattr but only for cn=Anuj
  217. account = Accounts(conn, DEFAULT_SUFFIX)
  218. assert len(account.filter('(mail=*)')) == 5
  219. assert not account.filter('(cn=*)')
  220. for i in account.filter('(mail=*)'):
  221. assert i.get_attr_val_utf8('mail') == '[email protected]'
  222. conn = Anonymous(topo.standalone).bind()
  223. # aci will allow only mail targetattr but only for cn=Anuj
  224. account = Accounts(conn, DEFAULT_SUFFIX)
  225. assert len(account.filter('(mail=*)')) == 5
  226. assert not account.filter('(cn=*)')
  227. for i in account.filter('(mail=*)'):
  228. assert i.get_attr_val_utf8('mail') == '[email protected]'
  229. # with root no blockage
  230. assert len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(mail=*)')) == 6
  231. for i in uas.list():
  232. i.delete()
  233. @pytest.mark.bz326000
  234. def test_memberurl_needs_to_be_normalized(topo, clean, aci_of_user):
  235. """
  236. Non-regression test for BUG 326000: MemberURL needs to be normalized
  237. :id: a5d172e6-7db8-11e8-aca7-8c16451d917b
  238. :setup: Standalone Instance
  239. :steps:
  240. 1. Add test entry
  241. 2. Add ACI
  242. 3. User should follow ACI role
  243. :expectedresults:
  244. 1. Entry should be added
  245. 2. Operation should succeed
  246. 3. Operation should succeed
  247. """
  248. ou_ou = OrganizationalUnit(topo.standalone, "ou=PEOPLE,{}".format(DEFAULT_SUFFIX))
  249. ou_ou.set('aci', '(targetattr= *)'
  250. '(version 3.0; acl "tester"; allow(all) '
  251. 'groupdn = "ldap:///cn =DYNGROUP,ou=PEOPLE, {}";)'.format(DEFAULT_SUFFIX))
  252. groups = Groups(topo.standalone, DEFAULT_SUFFIX, rdn='ou=PEOPLE')
  253. groups.create(properties={"cn": "DYNGROUP",
  254. "description": "DYNGROUP",
  255. 'objectClass': 'groupOfURLS',
  256. 'memberURL': "ldap:///ou=PEOPLE,{}??sub?"
  257. "(uid=test_user_2)".format(DEFAULT_SUFFIX)})
  258. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
  259. for demo1 in [(1, "Entry to test rights on."), (2, "Member of DYNGROUP")]:
  260. user = uas.create_test_user(uid=demo1[0], gid=demo1[0])
  261. user.replace_many(('description', demo1[1]), ('userPassword', PW_DM))
  262. ##with normal aci
  263. conn = UserAccount(topo.standalone, uas.list()[1].dn).bind(PW_DM)
  264. harry = UserAccount(conn, uas.list()[1].dn)
  265. harry.add('sn', 'FRED')
  266. ##with abnomal aci
  267. dygrp = Group(topo.standalone, DYNGROUP)
  268. dygrp.remove('memberurl', "ldap:///ou=PEOPLE,{}??sub?(uid=test_user_2)".format(DEFAULT_SUFFIX))
  269. dygrp.add('memberurl', "ldap:///ou=PEOPLE,{}??sub?(uid=tesT_UsEr_2)".format(DEFAULT_SUFFIX))
  270. harry.add('sn', 'Not FRED')
  271. for i in uas.list():
  272. i.delete()
  273. @pytest.mark.bz624370
  274. def test_greater_than_200_acls_can_be_created(topo, clean, aci_of_user):
  275. """
  276. Misc 10, check that greater than 200 ACLs can be created. Bug 624370
  277. :id: ac020252-7db8-11e8-8652-8c16451d917b
  278. :setup: Standalone Instance
  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. # greater_than_200_acls_can_be_created
  289. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
  290. for i in range(200):
  291. user = uas.create_test_user(uid=i, gid=i)
  292. user.set('aci', '(targetattr = "description")'
  293. '(version 3.0;acl "foo{}"; allow (read, search, compare)'
  294. '(userdn="ldap:///anyone");)'.format(i))
  295. assert user.\
  296. get_attr_val_utf8('aci') == '(targetattr = "description")' \
  297. '(version 3.0;acl "foo{}"; allow ' \
  298. '(read, search, compare)' \
  299. '(userdn="ldap:///anyone");)'.format(i)
  300. for i in uas.list():
  301. i.delete()
  302. @pytest.mark.bz624453
  303. def test_server_bahaves_properly_with_very_long_attribute_names(topo, clean, aci_of_user):
  304. """
  305. Make sure the server bahaves properly with very long attribute names. Bug 624453.
  306. :id: b0d31942-7db8-11e8-a833-8c16451d917b
  307. :setup: Standalone Instance
  308. :steps:
  309. 1. Add test entry
  310. 2. Add ACI
  311. 3. User should follow ACI role
  312. :expectedresults:
  313. 1. Entry should be added
  314. 2. Operation should succeed
  315. 3. Operation should succeed
  316. """
  317. users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
  318. users.create_test_user()
  319. users.list()[0].set('userpassword', PW_DM)
  320. user = UserAccount(topo.standalone, 'uid=test_user_1000,ou=People,{}'.format(DEFAULT_SUFFIX))
  321. with pytest.raises(ldap.INVALID_SYNTAX):
  322. user.add("aci", "a" * 9000)
  323. def test_do_bind_as_201_distinct_users(topo, clean, aci_of_user):
  324. """
  325. Do bind as 201 distinct users
  326. Increase the nsslapd-aclpb-max-selected-acls in cn=ACL Plugin,cn=plugins,cn=config
  327. Restart the server
  328. Do bind as 201 distinct users
  329. :id: c0060532-7db8-11e8-a124-8c16451d917b
  330. :setup: Standalone Instance
  331. :steps:
  332. 1. Add test entry
  333. 2. Add ACI
  334. 3. User should follow ACI role
  335. :expectedresults:
  336. 1. Entry should be added
  337. 2. Operation should succeed
  338. 3. Operation should succeed
  339. """
  340. uas = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
  341. for i in range(50):
  342. user = uas.create_test_user(uid=i, gid=i)
  343. user.set('userPassword', PW_DM)
  344. for i in range(len(uas.list())):
  345. uas.list()[i].bind(PW_DM)
  346. ACLPlugin(topo.standalone).replace("nsslapd-aclpb-max-selected-acls", '220')
  347. topo.standalone.restart()
  348. for i in range(len(uas.list())):
  349. uas.list()[i].bind(PW_DM)
  350. if __name__ == "__main__":
  351. CURRENT_FILE = os.path.realpath(__file__)
  352. pytest.main("-s -v %s" % CURRENT_FILE)