misc_test.py 15 KB

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