syntax_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2020 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 os
  9. import pytest
  10. from lib389._constants import DEFAULT_SUFFIX
  11. from lib389.idm.domain import Domain
  12. from lib389.topologies import topology_st as topo
  13. from lib389.utils import ds_is_older
  14. import ldap
  15. pytestmark = pytest.mark.tier1
  16. INVALID = [('test_targattrfilters_1',
  17. f'(targattrfilters ="add=title:title=fred),del=cn:(cn!=harry)")'
  18. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  19. ('test_targattrfilters_2',
  20. f'(targattrfilters ="add=:(title=fred),del=cn:(cn!=harry)")'
  21. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  22. ('test_targattrfilters_3',
  23. f'(targattrfilters ="add=:(title=fred),del=cn:(cn!=harry))'
  24. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  25. ('test_targattrfilters_4',
  26. f'(targattrfilters ="add=title:(title=fred),=cn:(cn!=harry")'
  27. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  28. ('test_targattrfilters_5',
  29. f'(targattrfilters ="add=title:(|(title=fred)(cn=harry)),del=cn:(cn=harry)")'
  30. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  31. ('test_targattrfilters_6',
  32. f'(targattrfilters ="add=title:(|(title=fred)(title=harry)),del=cn:(title=harry)")'
  33. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  34. ('test_targattrfilters_7',
  35. f'(targattrfilters ="add=title:(cn=architect), '
  36. f'del=title:(title=architect) && l:(l=cn=Meylan,dc=example,dc=com")")'
  37. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  38. ('test_targattrfilters_8',
  39. f'(targattrfilters ="add=title:(cn=architect)")'
  40. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  41. ('test_targattrfilters_9',
  42. f'(targattrfilters ="add=title:(cn=arch*)")'
  43. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  44. ('test_targattrfilters_10',
  45. f'(targattrfilters ="add=title:(cn >= 1)")'
  46. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  47. ('test_targattrfilters_11',
  48. f'(targattrfilters ="add=title:(cn <= 1)")'
  49. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  50. ('test_targattrfilters_12',
  51. f'(targattrfilters ="add=title:(cn ~= 1)")'
  52. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  53. ('test_targattrfilters_13',
  54. f'(targattrfilters ="add=title:(!(cn ~= 1))")'
  55. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  56. ('test_targattrfilters_14',
  57. f'(targattrfilters ="add=title:(&(cn=fred)(cn ~= 1))")'
  58. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  59. ('test_targattrfilters_15',
  60. f'(targattrfilters ="add=title:(|(cn=fred)(cn ~= 1))")'
  61. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  62. ('test_targattrfilters_16',
  63. f'(targattrfilters ="add=title:(&(|(title=fred)(title=harry))(cn ~= 1))")'
  64. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  65. ('test_targattrfilters_17',
  66. f'\(targattrfilters ="add=title:(&(|(&(title=harry)(title=fred))'
  67. f'(title=harry))(title ~= 1))")'
  68. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  69. ('test_targattrfilters_19',
  70. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  71. f'(targetattr="*")'
  72. f'(version 3.0; acl "Name of the ACI"; deny(write)gropdn="ldap:///anyone";)'),
  73. ('test_targattrfilters_21',
  74. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  75. f'(targetattr="*")'
  76. f'(version 3.0; acl "Name of the ACI"; deny(rite)userdn="ldap:///anyone";)'),
  77. ('test_targattrfilters_22',
  78. f'(targt = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  79. f'(targetattr="*")'
  80. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  81. ('test_targattrfilters_23',
  82. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  83. f'(targetattr="*")'
  84. f'(version 3.0; acl "Name of the ACI"; absolute (all)userdn="ldap:///anyone";)'),
  85. ('test_Missing_acl_mispel',
  86. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  87. f'(targetattr="*")'
  88. f'(version 3.0; alc "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  89. ('test_Missing_acl_string',
  90. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  91. f'(targetattr="*")'
  92. f'(version 3.0; "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  93. ('test_Wrong_version_string',
  94. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  95. f'(targetattr="*")'
  96. f'(version 2.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  97. ('test_Missing_version_string',
  98. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  99. f'(targetattr="*")'
  100. f'(; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  101. ('test_Authenticate_statement',
  102. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  103. f'(targetattr != "uid")'
  104. f'(targetattr="*")(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  105. f'userdn="ldap:///anyone";)'),
  106. ('test_Multiple_targets',
  107. f'(target = ldap:///ou=Product Development,{DEFAULT_SUFFIX})'
  108. f'(target = ldap:///ou=Product Testing,{DEFAULT_SUFFIX})(targetattr="*")'
  109. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  110. ('test_Target_set_to_self',
  111. f'(target = ldap:///self)(targetattr="*")'
  112. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  113. ('test_target_set_with_ldap_instead_of_ldap',
  114. f'(target = ldap:\\\{DEFAULT_SUFFIX})(targetattr="*")'
  115. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  116. ('test_target_set_with_more_than_three',
  117. f'(target = ldap:////{DEFAULT_SUFFIX})(targetattr="*")'
  118. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  119. ('test_target_set_with_less_than_three',
  120. f'(target = ldap://{DEFAULT_SUFFIX})(targetattr="*")'
  121. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  122. ('test_bind_rule_set_with_less_than_three',
  123. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr="*")'
  124. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:/anyone";)'),
  125. ('test_Use_semicolon_instead_of_comma_in_permission',
  126. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr="*")'
  127. f'(version 3.0; acl "Name of the ACI"; deny '
  128. f'(read; search; compare; write)userdn="ldap:///anyone";)'),
  129. ('test_Use_double_equal_instead_of_equal_in_the_target',
  130. f'(target == ldap:///{DEFAULT_SUFFIX})(targetattr="*")'
  131. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  132. ('test_use_double_equal_instead_of_equal_in_user_and_group_access',
  133. f'(target = ldap:///{DEFAULT_SUFFIX})'
  134. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  135. f'userdn == "ldap:///anyone";)'),
  136. ('test_donot_cote_the_name_of_the_aci',
  137. f'(target = ldap:///{DEFAULT_SUFFIX})'
  138. f'(version 3.0; acl Name of the ACI ; deny absolute (all)userdn = "ldap:///anyone";)'),
  139. ('test_extra_parentheses_case_1',
  140. f'( )(target = ldap:///{DEFAULT_SUFFIX}) (targetattr="*")'
  141. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone";)'),
  142. ('test_extra_parentheses_case_2',
  143. f'(((((target = ldap:///{DEFAULT_SUFFIX})(targetattr="*")'
  144. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  145. f'userdn == "ldap:///anyone";)'),
  146. ('test_extra_parentheses_case_3',
  147. f'(((target = ldap:///{DEFAULT_SUFFIX}) (targetattr="*")'
  148. f'(version 3.0; acl "Name of the ACI"; deny absolute '
  149. f'(all)userdn = "ldap:///anyone";)))'),
  150. ('test_no_semicolon_at_the_end_of_the_aci',
  151. f'(target = ldap:///{DEFAULT_SUFFIX}) (targetattr="*")'
  152. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone")'),
  153. ('test_a_character_different_of_a_semicolon_at_the_end_of_the_aci',
  154. f'(target = ldap:///{DEFAULT_SUFFIX}) (targetattr="*")'
  155. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone"%)'),
  156. ('test_bad_filter',
  157. f'(target = ldap:///{DEFAULT_SUFFIX}) '
  158. f'(targetattr="cn |&| sn |(|) uid")'
  159. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone";)'),
  160. ('test_Use_double_equal_instead_of_equal_in_the_targattrfilters',
  161. f'(target = ldap:///{DEFAULT_SUFFIX})(targattrfilters== "add=title:(title=architect)")'
  162. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  163. ('test_Use_double_equal_instead_of_equal_inside_the_targattrfilters',
  164. f'(target = ldap:///{DEFAULT_SUFFIX})(targattrfilters="add==title:(title==architect)")'
  165. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),]
  166. FAILED = [('test_targattrfilters_18',
  167. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  168. f'(targetattr="*")'
  169. f'(version 3.0; acl "Name of the ACI"; deny(write)userdn="ldap:///{"123" * 300}";)'),
  170. ('test_targattrfilters_20',
  171. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  172. f'(targetattr="*")'
  173. f'(version 3.0; acl "Name of the ACI"; deny(write)userdns="ldap:///anyone";)'),
  174. ('test_bind_rule_set_with_more_than_three',
  175. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr="*")'
  176. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  177. f'userdn="ldap:////////anyone";)'),
  178. ('test_Use_double_equal_instead_of_equal_in_the_targetattr',
  179. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr==*)'
  180. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  181. ('test_Use_double_equal_instead_of_equal_in_the_targetfilter',
  182. f'(target = ldap:///{DEFAULT_SUFFIX})(targetfilter==*)'
  183. f'(version 3.0; acl "Name of the ACI"; deny absolute '
  184. f'(all)userdn="ldap:///anyone";)'), ]
  185. @pytest.mark.xfail(reason='https://bugzilla.redhat.com/show_bug.cgi?id=1691473')
  186. @pytest.mark.parametrize("real_value", [a[1] for a in FAILED],
  187. ids=[a[0] for a in FAILED])
  188. def test_aci_invalid_syntax_fail(topo, real_value):
  189. """
  190. Try to set wrong ACI syntax.
  191. :id: 83c40784-fff5-49c8-9535-7064c9c19e7e
  192. :parametrized: yes
  193. :setup: Standalone Instance
  194. :steps:
  195. 1. Create ACI
  196. 2. Try to setup the ACI with Instance
  197. :expectedresults:
  198. 1. It should pass
  199. 2. It should not pass
  200. """
  201. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  202. with pytest.raises(ldap.INVALID_SYNTAX):
  203. domain.add("aci", real_value)
  204. @pytest.mark.parametrize("real_value", [a[1] for a in INVALID],
  205. ids=[a[0] for a in INVALID])
  206. def test_aci_invalid_syntax(topo, real_value):
  207. """
  208. Try to set wrong ACI syntax.
  209. :id: e8bf20b6-48be-4574-8300-056e42a0f0a8
  210. :parametrized: yes
  211. :setup: Standalone Instance
  212. :steps:
  213. 1. Create ACI
  214. 2. Try to setup the ACI with Instance
  215. :expectedresults:
  216. 1. It should pass
  217. 2. It should not pass
  218. """
  219. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  220. with pytest.raises(ldap.INVALID_SYNTAX):
  221. domain.add("aci", real_value)
  222. def test_target_set_above_the_entry_test(topo):
  223. """
  224. Try to set wrong ACI syntax.
  225. :id: d544d09a-6ed1-11e8-8872-8c16451d917b
  226. :setup: Standalone Instance
  227. :steps:
  228. 1. Create ACI
  229. 2. Try to setup the ACI with Instance
  230. :expectedresults:
  231. 1. It should pass
  232. 2. It should not pass
  233. """
  234. domain = Domain(topo.standalone, "ou=People,{}".format(DEFAULT_SUFFIX))
  235. with pytest.raises(ldap.INVALID_SYNTAX):
  236. domain.add("aci", f'(target = ldap:///{DEFAULT_SUFFIX})'
  237. f'(targetattr="*")(version 3.0; acl "Name of the ACI"; deny absolute '
  238. f'(all)userdn="ldap:///anyone";)')
  239. if __name__ == "__main__":
  240. CURRENT_FILE = os.path.realpath(__file__)
  241. pytest.main("-s -v %s" % CURRENT_FILE)