Debian Codegraph @

dirsrvtests/tests/suites/acl/deladd_test.py

raw
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
9 """
10 Importing necessary Modules.
11 """
12
13 import os
14 import pytest
15
16 from lib389._constants import DEFAULT_SUFFIX, PW_DM
17 from lib389.idm.user import UserAccount, UserAccounts
18 from lib389.idm.group import Groups
19 from lib389.idm.organizationalunit import OrganizationalUnit, OrganizationalUnits
20 from test389.topologies import topology_st as topo
21 from lib389.idm.domain import Domain
22
23 import ldap
24
25 pytestmark = pytest.mark.tier1
26
27
28 USER_WITH_ACI_DELADD = 'uid=test_user_1000,ou=People,dc=example,dc=com'
29 USER_DELADD = 'uid=test_user_1,ou=Accounting,dc=example,dc=com'
30
31
32 @pytest.fixture(scope="function")
33 def _aci_of_user(request, topo):
34 """
35 Removes and Restores ACIs after the test.
36 """
37 aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
38
39 def finofaci():
40 """
41 Removes and Restores ACIs after the test.
42 """
43 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
44 domain.remove_all('aci')
45 for i in aci_list:
46 domain.add("aci", i)
47
48 request.addfinalizer(finofaci)
49
50
51 @pytest.fixture(scope="function")
52 def _add_user(request, topo):
53 """
54 This function will create user for the test and in the end entries will be deleted .
55 """
56
57 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
58 user = users.create_test_user()
59 user.set("userPassword", PW_DM)
60
61 ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX)
62 ous.create(properties={'ou':'Accounting'})
63
64 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn='ou=Accounting')
65 for i in range(1, 3):
66 user = users.create_test_user(uid=i, gid=i)
67 user.set("userPassword", PW_DM)
68
69 def fin():
70 """
71 Deletes entries after the test.
72 """
73 users1 = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=None)
74 for dn_dn in users1.list():
75 dn_dn.delete()
76
77 groups = Groups(topo.standalone, DEFAULT_SUFFIX)
78 for dn_dn in groups.list():
79 dn_dn.delete()
80
81 ou_ou = OrganizationalUnit(topo.standalone, f'ou=Accounting,{DEFAULT_SUFFIX}')
82 ou_ou.delete()
83
84 request.addfinalizer(fin)
85
86
87 def test_allow_delete_access_to_groupdn(topo, _add_user, _aci_of_user):
88
89 """Test allow delete access to groupdn
90
91 :id: 7cf15992-68ad-11e8-85af-54e1ad30572c
92 :setup: topo.standalone
93 :steps:
94 1. Add test entry
95 2. Add ACI that allows groupdn to delete
96 3. Delete something using test USER_DELADD
97 4. Remove ACI
98 :expectedresults:
99 1. Entry should be added
100 2. ACI should be added
101 3. Delete operation should succeed
102 4. Delete operation for ACI should succeed
103 """
104 # Create Group and add member
105 groups = Groups(topo.standalone, DEFAULT_SUFFIX)
106 group = groups.create(properties={"cn": "group1",
107 "description": "testgroup"})
108 group.add_member(USER_WITH_ACI_DELADD)
109
110 # set aci
111 aci_target = f'(targetattr="*")'
112 aci_allow = f'(version 3.0; acl "All rights for {group.dn}"; allow (delete) '
113 aci_subject = f'groupdn="ldap:///{group.dn}";)'
114
115 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", (aci_target + aci_allow + aci_subject))
116
117 # create connection with USER_WITH_ACI_DELADD
118 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
119
120 # Perform delete operation
121 for i in [USER_DELADD, USER_WITH_ACI_DELADD]:
122 UserAccount(conn, i).delete()
123
124
125 def test_allow_add_access_to_anyone(topo, _add_user, _aci_of_user):
126
127 """Test to allow add access to anyone
128
129 :id: 5ca31cc4-68e0-11e8-8666-8c16451d917b
130 :setup: topo.standalone
131 :steps:
132 1. Add test entry
133 2. Add ACI that allows groupdn to add
134 3. Add something using test USER_DELADD
135 4. Remove ACI
136 :expectedresults:
137 1. Entry should be added
138 2. ACI should be added
139 3. Add operation should succeed
140 4. Delete operation for ACI should succeed
141 """
142 # set aci
143 aci_target = f'(targetattr="*")'
144 aci_allow = f'(version 3.0; acl "All rights for anyone"; allow (add) '
145 aci_subject = f'userdn="ldap:///anyone";)'
146 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", (aci_target + aci_allow + aci_subject))
147
148 # create connection with USER_WITH_ACI_DELADD
149 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
150
151 # Perform add operation
152 users = UserAccounts(conn, DEFAULT_SUFFIX, rdn='ou=Accounting')
153 user = users.create_test_user(gid=3, uid=3)
154 assert user.exists()
155
156 users = UserAccounts(conn, DEFAULT_SUFFIX)
157 user = users.create_test_user(gid=3, uid=3)
158 assert user.exists()
159
160
161 def test_allow_delete_access_to_anyone(topo, _add_user, _aci_of_user):
162
163 """Test to allow delete access to anyone
164
165 :id: f5447c7e-68e1-11e8-84c4-8c16451d917b
166 :setup: server
167 :steps:
168 1. Add test entry
169 2. Add ACI that allows groupdn to delete some userdn
170 3. Delete something using test USER_DELADD
171 4. Remove ACI
172 :expectedresults:
173 1. Entry should be added
174 2. ACI should be added
175 3. Operation should succeed
176 4. Delete operation for ACI should succeed
177 """
178 # set aci
179 aci_target = f'(targetattr="*")'
180 aci_allow = f'(version 3.0; acl "All rights for anyone"; allow (delete) '
181 aci_subject = f'userdn="ldap:///anyone";)'
182
183 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", (aci_target + aci_allow + aci_subject))
184
185 # create connection with USER_WITH_ACI_DELADD
186 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
187
188 # Perform delete operation
189 UserAccount(conn, USER_DELADD).delete()
190
191
192 def test_allow_delete_access_not_to_userdn(topo, _add_user, _aci_of_user):
193
194 """Test to Allow delete access to != userdn
195
196 :id: 00637f6e-68e3-11e8-92a3-8c16451d917b
197 :setup: server
198 :steps:
199 1. Add test entry
200 2. Add ACI that allows userdn not to delete some userdn
201 3. Delete something using test USER_DELADD
202 4. Remove ACI
203 :expectedresults:
204 1. Entry should be added
205 2. ACI should be added
206 3. Operation should not succeed
207 4. Delete operation for ACI should succeed
208 """
209 # set aci
210 aci_target = f'(targetattr="*")'
211 aci_allow = f'(version 3.0; acl "All rights for %s"; allow (delete) ' % USER_DELADD
212 aci_subject = f'userdn!="ldap:///{USER_WITH_ACI_DELADD}";)'
213
214 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", (aci_target + aci_allow + aci_subject))
215
216 # create connection with USER_WITH_ACI_DELADD
217 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
218
219 # Perform delete operation
220 user = UserAccount(conn, USER_DELADD)
221 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
222 user.delete()
223
224
225 def test_allow_delete_access_not_to_group(topo, _add_user, _aci_of_user):
226
227 """Test to Allow delete access to != groupdn
228
229 :id: f58fc8b0-68e5-11e8-9313-8c16451d917b
230 :setup: server
231 :steps:
232 1. Add test entry
233 2. Add ACI that allows groupdn not to delete some userdn
234 3. Delete something using test USER_DELADD belong to test group
235 4. Remove ACI
236 :expectedresults:
237 1. Entry should be added
238 2. ACI should be added
239 3. Operation should not succeed
240 4. Delete operation for ACI should succeed
241 """
242 # Create group
243 groups = Groups(topo.standalone, DEFAULT_SUFFIX)
244 group = groups.create(properties={"cn": "group1",
245 "description": "testgroup"})
246 group.add_member(USER_WITH_ACI_DELADD)
247
248 # set aci
249 aci_target = f'(targetattr="*")'
250 aci_allow = f'(version 3.0; acl "All rights for {group.dn}"; allow (delete)'
251 aci_subject = f'groupdn!="ldap:///{group.dn}";)'
252
253 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", (aci_target + aci_allow + aci_subject))
254
255 # create connection with USER_WITH_ACI_DELADD
256 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
257 user = UserAccount(conn, USER_DELADD)
258
259 # Perform delete operation
260 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
261 user.delete()
262
263
264 def test_allow_add_access_to_parent(topo, _add_user, _aci_of_user):
265
266 """Test to Allow add privilege to parent
267
268 :id: 9f099845-9dbc-412f-bdb9-19a5ea729694
269 :setup: server
270 :steps:
271 1. Add test entry
272 2. Add ACI that Allow add privilege to parent
273 3. Add something using test USER_DELADD
274 4. Remove ACI
275 :expectedresults:
276 1. Entry should be added
277 2. ACI should be added
278 3. Operation should succeed
279 4. Delete operation for ACI should succeed
280 """
281 # set aci
282 aci_target = f'(targetattr="*")'
283 aci_allow = f'(version 3.0; acl "All rights for parent"; allow (add) '
284 aci_subject = f'userdn="ldap:///parent";)'
285
286 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", (aci_target + aci_allow + aci_subject))
287
288 # create connection with USER_WITH_ACI_DELADD
289 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
290
291 # Perform Allow add privilege to parent
292 users = UserAccounts(conn, DEFAULT_SUFFIX, rdn='uid=test_user_1000, ou=people')
293 user = users.create_test_user(gid=1, uid=1)
294 assert user.exists()
295
296 # Delete created user
297 UserAccounts(topo.standalone, DEFAULT_SUFFIX).get('test_user_1').delete()
298
299
300 def test_allow_delete_access_to_parent(topo, _add_user, _aci_of_user):
301
302 """Test to Allow delete access to parent
303
304 :id: 2dd7f624-68e7-11e8-8591-8c16451d917b
305 :setup: server
306 :steps:
307 1. Add test entry
308 2. Add ACI that Allow delete privilege to parent
309 3. Delete something using test USER_DELADD
310 4. Remove ACI
311 :expectedresults:
312 1. Entry should be added
313 2. ACI should be added
314 3. Operation should succeed
315 4. Delete operation for ACI should succeed
316 """
317 # set aci
318 aci_target = f'(targetattr="*")'
319 aci_allow = f'(version 3.0; acl "All rights for parent"; allow (add,delete) '
320 aci_subject = f'userdn="ldap:///parent";)'
321
322 Domain(topo.standalone, DEFAULT_SUFFIX).add("aci", (aci_target + aci_allow + aci_subject))
323
324 # create connection with USER_WITH_ACI_DELADD
325 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
326
327 # Create a user with parent 'uid=test_user_1000, ou=people, {}'.format(DEFAULT_SUFFIX)
328 users = UserAccounts(conn, DEFAULT_SUFFIX, rdn='uid=test_user_1000, ou=people')
329 new_user = users.create_test_user(gid=1, uid=1)
330 assert new_user.exists()
331
332 # Perform Allow delete access to parent
333 new_user.delete()
334
335
336 def test_allow_delete_access_to_dynamic_group(topo, _add_user, _aci_of_user, request):
337
338 """Test to Allow delete access to dynamic group
339
340 :id: 14ffa452-68ed-11e8-a60d-8c16451d917b
341 :setup: server
342 :steps:
343 1. Add test entry
344 2. Add ACI that Allow delete privilege to dynamic group
345 3. Delete something using test USER_DELADD
346 4. Remove ACI
347 :expectedresults:
348 1. Entry should be added
349 2. ACI should be added
350 3. Operation should succeed
351 4. Delete operation for ACI should succeed
352 """
353 # Create dynamic group
354 groups = Groups(topo.standalone, DEFAULT_SUFFIX)
355 group = groups.create(properties={"cn": "group1",
356 "description": "testgroup"})
357
358 group.add("objectclass", "groupOfURLs")
359 group.add("memberURL",
360 f"ldap:///dc=example,dc=com??sub?(&(objectclass=person)(uid=test_user_1000))")
361
362 # Set ACI
363 Domain(topo.standalone, DEFAULT_SUFFIX).\
364 add("aci", f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr="*")'
365 f'(version 3.0; acl "{request.node.name}"; '
366 f'allow (delete) (groupdn = "ldap:///{group.dn}"); )')
367
368 # create connection with USER_WITH_ACI_DELADD
369 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
370
371 # Perform Allow delete access to dynamic group
372 UserAccount(conn, USER_DELADD).delete()
373
374
375 def test_allow_delete_access_to_dynamic_group_uid(topo, _add_user, _aci_of_user, request):
376
377 """Test to Allow delete access to dynamic group
378
379 :id: 010a4f20-752a-4173-b763-f520c7a85b82
380 :setup: server
381 :steps:
382 1. Add test entry
383 2. Add ACI that Allow delete privilege to dynamic group
384 3. Delete something using test USER_DELADD
385 4. Remove ACI
386 :expectedresults:
387 1. Entry should be added
388 2. ACI should be added
389 3. Operation should succeed
390 4. Delete operation for ACI should succeed
391 """
392 # Create dynamic group
393 groups = Groups(topo.standalone, DEFAULT_SUFFIX)
394 group = groups.create(properties={"cn": "group1",
395 "description": "testgroup"})
396
397 group.add("objectclass", "groupOfURLs")
398 group.add("memberURL",
399 f'ldap:///{DEFAULT_SUFFIX}??sub?(&(objectclass=person)(cn=test_user_1000))')
400
401 # Set ACI
402 Domain(topo.standalone, DEFAULT_SUFFIX).\
403 add("aci", f'(target = ldap:///{DEFAULT_SUFFIX})'
404 f'(targetattr="uid")(version 3.0; acl "{request.node.name}"; '
405 f'allow (delete) (groupdn = "ldap:///{group.dn}"); )')
406
407 # create connection with USER_WITH_ACI_DELADD
408 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
409
410 # Perform Allow delete access to dynamic group
411 UserAccount(conn, USER_DELADD).delete()
412
413
414 def test_allow_delete_access_not_to_dynamic_group(topo, _add_user, _aci_of_user, request):
415
416 """Test to Allow delete access to != dynamic group
417
418 :id: 9ecb139d-bca8-428e-9044-fd89db5a3d14
419 :setup: server
420 :steps:
421 1. Add test entry
422 2. Add ACI that delete access to != dynamic group
423 3. Delete something using test USER_DELADD
424 4. Remove ACI
425 :expectedresults:
426 1. Entry should be added
427 2. ACI should be added
428 3. Operation should not succeed
429 4. Delete operation for ACI should succeed
430 """
431 # Create dynamic group
432 groups = Groups(topo.standalone, DEFAULT_SUFFIX)
433 group = groups.create(properties={"cn": "group1",
434 "description": "testgroup"})
435 group.add("objectclass", "groupOfURLs")
436 group.add("memberURL",
437 f'ldap:///{DEFAULT_SUFFIX}??sub?(&(objectclass=person)(cn=test_user_1000))')
438
439 # Set ACI
440 Domain(topo.standalone, DEFAULT_SUFFIX).\
441 add("aci", f'(target = ldap:///{DEFAULT_SUFFIX})'
442 f'(targetattr="*")(version 3.0; acl "{request.node.name}"; '
443 f'allow (delete) (groupdn != "ldap:///{group.dn}"); )')
444
445 # create connection with USER_WITH_ACI_DELADD
446 conn = UserAccount(topo.standalone, USER_WITH_ACI_DELADD).bind(PW_DM)
447 user = UserAccount(conn, USER_DELADD)
448
449 # Perform Allow delete access to != dynamic group
450 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
451 user.delete()
452
453
454 if __name__ == "__main__":
455 CURRENT_FILE = os.path.realpath(__file__)
456 pytest.main("-s -v %s" % CURRENT_FILE)