Debian Codegraph @

dirsrvtests/tests/suites/acl/search_real_test.py

raw
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
9 import pytest, os, ldap
10 from lib389._constants import DEFAULT_SUFFIX, PW_DM, ErrorLog
11 from lib389.idm.user import UserAccount, UserAccounts
12 from lib389.idm.account import Accounts
13 from lib389.idm.organizationalunit import OrganizationalUnit
14 from lib389.idm.group import Groups
15 from test389.topologies import topology_st as topo
16 from lib389.idm.domain import Domain
17 from lib389.idm.posixgroup import PosixGroups
18
19 pytestmark = pytest.mark.tier1
20
21 CONTAINER_1_DELADD = "ou=Product Development,{}".format(DEFAULT_SUFFIX)
22 CONTAINER_2_DELADD = "ou=Accounting,{}".format(DEFAULT_SUFFIX)
23 USER_ANUJ = "uid=Anuj Borah,{}".format(CONTAINER_1_DELADD)
24 USER_ANANDA = "uid=Ananda Borah,{}".format(CONTAINER_2_DELADD)
25
26
27 @pytest.fixture(scope="function")
28 def aci_of_user(request, topo):
29 # Add anonymous access aci
30 ACI_TARGET = "(targetattr != \"userpassword\")(target = \"ldap:///%s\")" % (DEFAULT_SUFFIX)
31 ACI_ALLOW = "(version 3.0; acl \"Anonymous Read access\"; allow (read,search,compare)"
32 ACI_SUBJECT = "(userdn=\"ldap:///anyone\");)"
33 ANON_ACI = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
34 suffix = Domain(topo.standalone, DEFAULT_SUFFIX)
35 try:
36 suffix.add('aci', ANON_ACI)
37 except ldap.TYPE_OR_VALUE_EXISTS:
38 pass
39
40 aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
41
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
48 request.addfinalizer(finofaci)
49
50
51 @pytest.fixture(scope="module")
52 def add_test_user(request, topo):
53 topo.standalone.config.loglevel((ErrorLog.ACL_SUMMARY,))
54
55 for i in ['Product Development', 'Accounting']:
56 OrganizationalUnit(topo.standalone, "ou={},{}".format(i, DEFAULT_SUFFIX)).create(properties={'ou': i})
57
58 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou=Product Development')
59 users.create(properties={
60 'uid': 'Anuj Borah',
61 'cn': 'Anuj Borah',
62 'sn': 'user',
63 'uidNumber': '1000',
64 'gidNumber': '2000',
65 'homeDirectory': '/home/' + 'AnujBorah',
66 'userPassword': PW_DM
67 })
68
69 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou=Accounting')
70 users.create(properties={
71 'uid': 'Ananda Borah',
72 'cn': 'Ananda Borah',
73 'sn': 'user',
74 'uidNumber': '1000',
75 'gidNumber': '2000',
76 'homeDirectory': '/home/' + 'AnandaBorah',
77 'userPassword': PW_DM
78 })
79
80
81 def test_deny_all_access_with_target_set(topo, add_test_user, aci_of_user):
82 """Test that Deny all access with target set
83
84 :id: 0550e680-6e0e-11e8-82f4-8c16451d917b
85 :setup: Standalone Instance
86 :steps:
87 1. Add Entry
88 2. Add ACI
89 3. Bind with test USER_ANUJ
90 4. Try search
91 5. Delete Entry,test USER_ANUJ, ACI
92 :expectedresults:
93 1. Operation should success
94 2. Operation should success
95 3. Operation should success
96 4. Operation should Fail
97 5. Operation should success
98 """
99 ACI_TARGET = '(target = ldap:///{})(targetattr="*")'.format(USER_ANANDA)
100 ACI_ALLOW = '(version 3.0; acl "Name of the ACI"; deny absolute (all)'
101 ACI_SUBJECT = 'userdn="ldap:///anyone";)'
102 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
103 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
104 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
105 # aci will block all for all usrs
106 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=Ananda*)'))
107 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
108 # aci will block all for all usrs
109 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=Ananda*)'))
110 # with root there is no aci blockage
111 assert 1 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(cn=Ananda*)'))
112
113
114 def test_deny_all_access_to_a_target_with_wild_card(topo, add_test_user, aci_of_user):
115 """Search Test 2 Deny all access to a target with wild card
116
117 :id: 1c370f98-6e11-11e8-9f10-8c16451d917b
118 :setup: Standalone Instance
119 :steps:
120 1. Add Entry
121 2. Add ACI
122 3. Bind with test USER_ANUJ
123 4. Try search
124 5. Delete Entry,test USER_ANUJ, ACI
125 :expectedresults:
126 1. Operation should success
127 2. Operation should success
128 3. Operation should success
129 4. Operation should Fail
130 5. Operation should success
131 """
132 ACI_TARGET = '(target = ldap:///uid=Ananda*, ou=*,{})(targetattr="*")'.format(
133 DEFAULT_SUFFIX
134 )
135 ACI_ALLOW = '(version 3.0; acl "Name of the ACI"; deny absolute (all)'
136 ACI_SUBJECT = 'userdn="ldap:///anyone";)'
137 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
138 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
139 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
140 # aci will block (cn=Sam*) for all usrs
141 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=Ananda*)'))
142 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
143 # aci will block (cn=Sam*) for all usrs
144 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=Ananda*)'))
145 # with root there is no aci blockage
146 assert 1 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(cn=Ananda*)'))
147
148
149 def test_deny_all_access_without_a_target_set(topo, add_test_user, aci_of_user):
150 """Search Test 3 Deny all access without a target set
151
152 :id: 2dbeb36a-6e11-11e8-ab9f-8c16451d917b
153 :setup: Standalone Instance
154 :steps:
155 1. Add Entry
156 2. Add ACI
157 3. Bind with test USER_ANUJ
158 4. Try search
159 5. Delete Entry,test USER_ANUJ, ACI
160 :expectedresults:
161 1. Operation should success
162 2. Operation should success
163 3. Operation should success
164 4. Operation should Fail
165 5. Operation should success
166 """
167 ACI_TARGET = '(targetattr="*")'
168 ACI_ALLOW = '(version 3.0; acl "Name of the ACI"; deny absolute (all)'
169 ACI_SUBJECT = 'userdn="ldap:///anyone";)'
170 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
171 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
172 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
173 # aci will block all for all usrs
174 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(ou=Accounting)'))
175 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
176 # aci will block all for all usrs
177 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(ou=Accounting)'))
178 # with root there is no aci blockage
179 assert 1 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(ou=Accounting)'))
180
181
182 def test_deny_read_search_and_compare_access_with_target_and_targetattr_set(
183 topo, add_test_user, aci_of_user
184 ):
185 """Search Test 4 Deny read, search and compare access with target and targetattr set
186
187 :id: 3f4a87e4-6e11-11e8-a09f-8c16451d917b
188 :setup: Standalone Instance
189 :steps:
190 1. Add Entry
191 2. Add ACI
192 3. Bind with test USER_ANUJ
193 4. Try search
194 5. Delete Entry,test USER_ANUJ, ACI
195 :expectedresults:
196 1. Operation should success
197 2. Operation should success
198 3. Operation should success
199 4. Operation should Fail
200 5. Operation should success
201 """
202 ACI_TARGET = '(target = ldap:///{})(targetattr="*")'.format(CONTAINER_2_DELADD)
203 ACI_ALLOW = '(version 3.0; acl "Name of the ACI"; deny absolute (all)'
204 ACI_SUBJECT = 'userdn="ldap:///anyone";)'
205 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
206 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
207 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
208 # aci will block all for all usrs
209 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(ou=Accounting)'))
210 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
211 # aci will block all for all usrs
212 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(ou=Accounting)'))
213 # with root there is no aci blockage
214 assert 1 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(ou=Accounting)'))
215
216
217 def test_deny_read_access_to_multiple_groupdns(topo, add_test_user, aci_of_user):
218 """Search Test 6 Deny read access to multiple groupdn's
219
220 :id: 8f3ba440-6e11-11e8-8b20-8c16451d917b
221 :setup: Standalone Instance
222 :steps:
223 1. Add Entry
224 2. Add ACI
225 3. Bind with test USER_ANUJ
226 4. Try search
227 5. Delete Entry,test USER_ANUJ, ACI
228 :expectedresults:
229 1. Operation should success
230 2. Operation should success
231 3. Operation should success
232 4. Operation should Fail
233 5. Operation should success
234 """
235 groups = Groups(topo.standalone, DEFAULT_SUFFIX)
236 group = groups.create(properties={"cn": "group1",
237 "description": "testgroup"
238 })
239 group.add_member(USER_ANANDA)
240
241 posix_groups = PosixGroups(topo.standalone, DEFAULT_SUFFIX)
242 posix_group = posix_groups.create(properties={
243 "cn": "group2",
244 "description": "testgroup2",
245 "gidNumber": "2000",
246 })
247 posix_group.add_member(USER_ANUJ)
248
249 ACI_TARGET = '(targetattr="*")'
250 ACI_ALLOW = '(version 3.0; acl "All rights for cn=group1,ou=Groups,{}"; deny(read)'.format(DEFAULT_SUFFIX)
251 ACI_SUBJECT = 'groupdn="ldap:///cn=group1,ou=Groups,{}||ldap:///cn=group2,ou=Groups,{}";)'.format(DEFAULT_SUFFIX, DEFAULT_SUFFIX)
252 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
253 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
254 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
255 # aci will block 'groupdn="ldap:///cn=group1,ou=Groups,dc=example,dc=com||ldap:///cn=group2,ou=Groups,dc=example,dc=com";)
256 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=*)'))
257 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
258 # aci will block 'groupdn="ldap:///cn=group1,ou=Groups,dc=example,dc=com||ldap:///cn=group2,ou=Groups,dc=example,dc=com";)
259 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=*)'))
260 # with root there is no aci blockage
261 assert 5 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(cn=*)'))
262 group = groups.get("group1")
263 group.delete()
264 posix_groups.get("group2")
265 posix_group.delete()
266
267
268 def test_deny_all_access_to_userdnattr(topo, add_test_user, aci_of_user):
269 """Search Test 7 Deny all access to userdnattr"
270
271 :id: ae482494-6e11-11e8-ae33-8c16451d917b
272 :setup: Standalone Instance
273 :steps:
274 1. Add Entry
275 2. Add ACI
276 3. Bind with test USER_ANUJ
277 4. Try search
278 5. Delete Entry,test USER_ANUJ, ACI
279 :expectedresults:
280 1. Operation should success
281 2. Operation should success
282 3. Operation should success
283 4. Operation should Fail
284 5. Operation should success
285 """
286 UserAccount(topo.standalone, USER_ANUJ).add('manager', USER_ANANDA)
287 ACI_TARGET = '(target = ldap:///{})(targetattr="*")'.format(DEFAULT_SUFFIX)
288 ACI_ALLOW = '(version 3.0; acl "Name of the ACI"; deny absolute (all)'
289 ACI_SUBJECT = 'userdnattr="manager";)'
290 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
291 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
292 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
293 # aci will block only 'userdnattr="manager"
294 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=Anuj Borah)'))
295 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
296 # aci will block only 'userdnattr="manager"
297 assert 1 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=Anuj Borah)'))
298 # with root there is no aci blockage
299 assert 1 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(cn=Anuj Borah)'))
300 UserAccount(topo.standalone, USER_ANUJ).remove('manager', USER_ANANDA)
301
302
303 def test_deny_all_access_with__target_set(topo, add_test_user, aci_of_user, request):
304 """Search Test 8 Deny all access with != target set
305
306 :id: bc00aed0-6e11-11e8-be66-8c16451d917b
307 :setup: Standalone Instance
308 :steps:
309 1. Add Entry
310 2. Add ACI
311 3. Bind with test USER_ANUJ
312 4. Try search
313 5. Delete Entry,test USER_ANUJ, ACI
314 :expectedresults:
315 1. Operation should success
316 2. Operation should success
317 3. Operation should success
318 4. Operation should Fail
319 5. Operation should success
320 """
321 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci",'(target != "ldap:///{}")(targetattr = "*")'
322 '(version 3.0; acl "{}"; deny absolute (all) (userdn = "ldap:///anyone") ;)'.format(USER_ANANDA, request.node.name))
323 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
324 # aci will not block USER_ANANDA will block others
325 assert 1 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=*)'))
326 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
327 # aci will not block USER_ANANDA will block others
328 assert 1 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=*)'))
329 # with root there is no aci blockage
330 assert 4 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(cn=*)'))
331
332
333 def test_deny_all_access_with__targetattr_set(topo, add_test_user, aci_of_user):
334 """Search Test 9 Deny all access with != targetattr set
335
336 :id: d2d73b2e-6e11-11e8-ad3d-8c16451d917b
337 :setup: Standalone Instance
338 :steps:
339 1. Add Entry
340 2. Add ACI
341 3. Bind with test USER_ANUJ
342 4. Try search
343 5. Delete Entry,test USER_ANUJ, ACI
344 :expectedresults:
345 1. Operation should success
346 2. Operation should success
347 3. Operation should success
348 4. Operation should Fail
349 5. Operation should success
350 """
351 testusers = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
352 user = testusers.create(properties={
353 'uid': 'Anuj',
354 'cn': 'Anuj',
355 'sn': 'user',
356 'uidNumber': '1000',
357 'gidNumber': '2000',
358 'homeDirectory': '/home/' + 'Anuj',
359 'userPassword': PW_DM
360 })
361
362 ACI_TARGET = '(targetattr != "uid||Objectclass")'
363 ACI_ALLOW = '(version 3.0; acl "Name of the ACI"; deny absolute (all)'
364 ACI_SUBJECT = 'userdn="ldap:///anyone";)'
365 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
366 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
367 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
368 # aci will allow only uid=*
369 assert 4 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(uid=*)'))
370 # aci will allow only uid=*
371 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=*)'))
372 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
373 # aci will allow only uid=*
374 assert 4 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(uid=*)'))
375 # aci will allow only uid=*
376 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(cn=*)'))
377 # with root there is no aci blockage
378 assert 4 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(uid=*)'))
379 # with root there is no aci blockage
380 assert 5 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(cn=*)'))
381 user.delete()
382
383
384 def test_deny_all_access_with_targetattr_set(topo, add_test_user, aci_of_user):
385 """Search Test 10 Deny all access with targetattr set
386
387 :id: e1602ff2-6e11-11e8-8e55-8c16451d917b
388 :setup: Standalone Instance
389 :steps:
390 1. Add Entry
391 2. Add ACI
392 3. Bind with test USER_ANUJ
393 4. Try search
394 5. Delete Entry,test USER_ANUJ, ACI
395 :expectedresults:
396 1. Operation should success
397 2. Operation should success
398 3. Operation should success
399 4. Operation should Fail
400 5. Operation should success
401 """
402 testuser = UserAccount(topo.standalone, "cn=Anuj12,ou=People,{}".format(DEFAULT_SUFFIX))
403 testuser.create(properties={
404 'uid': 'Anuj12',
405 'cn': 'Anuj12',
406 'sn': 'user',
407 'uidNumber': '1000',
408 'gidNumber': '2000',
409 'homeDirectory': '/home/' + 'Anuj12'
410 })
411
412 ACI_TARGET = '(targetattr="uid")'
413 ACI_ALLOW = '(version 3.0; acl "Name of the ACI"; deny absolute (all)'
414 ACI_SUBJECT = 'userdn="ldap:///anyone";)'
415 ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
416 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", ACI_BODY)
417 conn = UserAccount(topo.standalone, USER_ANANDA).bind(PW_DM)
418 # aci will block only uid=*
419 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(uid=*)'))
420 conn = UserAccount(topo.standalone, USER_ANUJ).bind(PW_DM)
421 # aci will block only uid=*
422 assert 0 == len(Accounts(conn, DEFAULT_SUFFIX).filter('(uid=*)'))
423 # with root there is no aci blockage
424 assert 4 == len(Accounts(topo.standalone, DEFAULT_SUFFIX).filter('(uid=*)'))
425 testuser.delete()
426
427
428 if __name__ == "__main__":
429 CURRENT_FILE = os.path.realpath(__file__)
430 pytest.main("-s -v %s" % CURRENT_FILE)