Debian Codegraph @

dirsrvtests/tests/suites/acl/keywords_part2_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
10 """
11 This test script will test wrong/correct key value with ACIs.
12 """
13 import ldap
14 import os
15 import pytest
16 import socket
17 import time
18 from datetime import datetime
19 from lib389._constants import DEFAULT_SUFFIX, PW_DM
20 from lib389.idm.domain import Domain
21 from lib389.idm.organizationalunit import OrganizationalUnit
22 from lib389.idm.user import UserAccount
23 from lib389.utils import *
24
25 pytestmark = pytest.mark.tier1
26
27 KEYWORDS_OU_KEY = "ou=Keywords,{}".format(DEFAULT_SUFFIX)
28 DAYOFWEEK_OU_KEY = "ou=Dayofweek,{}".format(KEYWORDS_OU_KEY)
29 IP_OU_KEY = "ou=IP,{}".format(KEYWORDS_OU_KEY)
30 TIMEOFDAY_OU_KEY = "ou=Timeofday,{}".format(KEYWORDS_OU_KEY)
31 EVERYDAY_KEY = "uid=EVERYDAY_KEY,{}".format(DAYOFWEEK_OU_KEY)
32 TODAY_KEY = "uid=TODAY_KEY,{}".format(DAYOFWEEK_OU_KEY)
33 NODAY_KEY = "uid=NODAY_KEY,{}".format(DAYOFWEEK_OU_KEY)
34 FULLIP_KEY = "uid=FULLIP_KEY,{}".format(IP_OU_KEY)
35 NETSCAPEIP_KEY = "uid=NETSCAPEIP_KEY,{}".format(IP_OU_KEY)
36 NOIP_KEY = "uid=NOIP_KEY,{}".format(IP_OU_KEY)
37 FULLWORKER_KEY = "uid=FULLWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
38 DAYWORKER_KEY = "uid=DAYWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
39 NIGHTWORKER_KEY = "uid=NIGHTWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
40 NOWORKER_KEY = "uid=NOWORKER_KEY,{}".format(TIMEOFDAY_OU_KEY)
41
42
43 @pytest.mark.skipif(os.getuid()!=0, reason="pytest non run by root user")
44 def test_access_from_certain_network_only_ip(topo, add_user, aci_of_user, request):
45 """
46 User can access the data when connecting from certain network only as per the ACI.
47
48 :id: 4ec38296-7ac5-11e8-9816-8c16451d917b
49 :customerscenario: True
50 :setup: Standalone Server
51 :steps:
52 1. Add test entry
53 2. Add ACI
54 3. User should follow ACI role
55 :expectedresults:
56 1. Entry should be added
57 2. Operation should succeed
58 3. Operation should succeed
59 """
60 # Turn access log buffering off to make less time consuming
61 topo.standalone.config.set('nsslapd-accesslog-logbuffering', 'off')
62
63 # Find the ip from ds logs , as we need to know the exact ip used by ds to run the instances.
64 # Wait till Access Log is generated
65 topo.standalone.restart()
66
67 old_hostname = socket.gethostname()
68 socket.sethostname('localhost')
69 hostname = socket.gethostname()
70 IP = socket.gethostbyname(hostname)
71
72 # Add ACI
73 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
74 domain.add("aci", f'(target = "ldap:///{IP_OU_KEY}")(targetattr=\"*\")(version 3.0; aci "IP aci"; '
75 f'allow(all)userdn = "ldap:///{NETSCAPEIP_KEY}" and (ip = "127.0.0.1" or ip = "::1" or ip = "{IP}") ;)')
76
77 # create a new connection for the test
78 new_uri = topo.standalone.ldapuri.replace(old_hostname, hostname)
79 topo.standalone.ldapuri = new_uri
80 conn = UserAccount(topo.standalone, NETSCAPEIP_KEY).bind(PW_DM)
81
82 # Perform Operation
83 topo.standalone.config.set('nsslapd-errorlog-level', '128')
84 org = OrganizationalUnit(conn, IP_OU_KEY)
85 topo.standalone.host = hostname
86 org.replace("seeAlso", "cn=1")
87
88 # remove the aci
89 domain.ensure_removed("aci", f'(target = "ldap:///{IP_OU_KEY}")(targetattr=\"*\")(version 3.0; aci '
90 f'"IP aci"; allow(all)userdn = "ldap:///{NETSCAPEIP_KEY}" and '
91 f'(ip = "127.0.0.1" or ip = "::1" or ip = "{IP}") ;)')
92 # Now add aci with new ip
93 domain.add("aci", f'(target = "ldap:///{IP_OU_KEY}")(targetattr="*")(version 3.0; aci "IP aci"; '
94 f'allow(all)userdn = "ldap:///{NETSCAPEIP_KEY}" and ip = "100.1.1.1" ;)')
95
96 # After changing the ip user cant access data
97 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
98 org.replace("seeAlso", "cn=1")
99
100 def fin():
101 log.info('Setting the hostname back to orginal')
102 socket.sethostname(old_hostname)
103
104 request.addfinalizer(fin)
105
106
107 @pytest.mark.skipif(os.getuid()!=0, reason="pytest non run by root user")
108 def test_connection_from_an_unauthorized_network(topo, add_user, aci_of_user, request):
109 """
110 User cannot access the data when connectin from an unauthorized network as per the ACI.
111
112 :id: 52d1ecce-7ac5-11e8-9ad9-8c16451d917b
113 :customerscenario: True
114 :setup: Standalone Server
115 :steps:
116 1. Add test entry
117 2. Add ACI
118 3. User should follow ACI role
119 :expectedresults:
120 1. Entry should be added
121 2. Operation should succeed
122 3. Operation should succeed
123 """
124 old_hostname = socket.gethostname()
125 socket.sethostname('localhost')
126 hostname = socket.gethostname()
127
128 # Add ACI
129 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
130 domain.add("aci", f'(target = "ldap:///{IP_OU_KEY}")'
131 f'(targetattr="*")(version 3.0; aci "IP aci"; '
132 f'allow(all) userdn = "ldap:///{NETSCAPEIP_KEY}" '
133 f'and (ip != "127.0.0.1" and ip != "::1") ;)')
134
135 # create a new connection for the test
136 new_uri = topo.standalone.ldapuri.replace(old_hostname, hostname)
137 topo.standalone.ldapuri = new_uri
138 conn = UserAccount(topo.standalone, NETSCAPEIP_KEY).bind(PW_DM)
139
140 # Perform Operation
141 topo.standalone.config.set('nsslapd-errorlog-level', '128')
142 org = OrganizationalUnit(conn, IP_OU_KEY)
143 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
144 org.replace("seeAlso", "cn=1")
145
146 # Remove the ACI
147 domain.ensure_removed('aci', domain.get_attr_vals('aci')[-1])
148 # Add new ACI
149 domain.add('aci', f'(target = "ldap:///{IP_OU_KEY}")(targetattr="*")'
150 f'(version 3.0; aci "IP aci"; allow(all) '
151 f'userdn = "ldap:///{NETSCAPEIP_KEY}" and (ip = "127.0.0.1" or ip = "::1") ;)')
152 time.sleep(1)
153
154 # now user can access data
155 org.replace("seeAlso", "cn=1")
156
157 def fin():
158 log.info('Setting the hostname back to orginal')
159 socket.sethostname(old_hostname)
160
161 request.addfinalizer(fin)
162
163
164 def test_ip_keyword_test_noip_cannot(topo, add_user, aci_of_user):
165 """
166 User NoIP cannot assess the data as per the ACI.
167
168 :id: 570bc7f6-7ac5-11e8-88c1-8c16451d917b
169 :customerscenario: True
170 :setup: Standalone Server
171 :steps:
172 1. Add test entry
173 2. Add ACI
174 3. User should follow ACI role
175 :expectedresults:
176 1. Entry should be added
177 2. Operation should succeed
178 3. Operation should succeed
179 """
180 # Add ACI
181 Domain(topo.standalone,
182 DEFAULT_SUFFIX).add("aci", f'(target ="ldap:///{IP_OU_KEY}")'
183 f'(targetattr="*")(version 3.0; aci "IP aci"; allow(all) '
184 f'userdn = "ldap:///{FULLIP_KEY}" and ip = "*" ;)')
185
186 # Create a new connection for this test.
187 conn = UserAccount(topo.standalone, NOIP_KEY).bind(PW_DM)
188 # Perform Operation
189 org = OrganizationalUnit(conn, IP_OU_KEY)
190 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
191 org.replace("seeAlso", "cn=1")
192
193
194 def test_user_can_access_the_data_at_any_time(topo, add_user, aci_of_user):
195 """
196 User can access the data at any time as per the ACI.
197
198 :id: 5b4da91a-7ac5-11e8-bbda-8c16451d917b
199 :customerscenario: True
200 :setup: Standalone Server
201 :steps:
202 1. Add test entry
203 2. Add ACI
204 3. User should follow ACI role
205 :expectedresults:
206 1. Entry should be added
207 2. Operation should succeed
208 3. Operation should succeed
209 """
210 # Add ACI
211 Domain(topo.standalone,
212 DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")'
213 f'(targetattr="*")(version 3.0; aci "Timeofday aci"; '
214 f'allow(all) userdn ="ldap:///{FULLWORKER_KEY}" and '
215 f'(timeofday >= "0000" and timeofday <= "2359") ;)')
216
217 # Create a new connection for this test.
218 conn = UserAccount(topo.standalone, FULLWORKER_KEY).bind(PW_DM)
219 # Perform Operation
220 org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
221 org.replace("seeAlso", "cn=1")
222
223
224 def test_user_can_access_the_data_only_in_the_morning(topo, add_user, aci_of_user):
225 """
226 User can access the data only in the morning as per the ACI.
227
228 :id: 5f7d380c-7ac5-11e8-8124-8c16451d917b
229 :customerscenario: True
230 :setup: Standalone Server
231 :steps:
232 1. Add test entry
233 2. Add ACI
234 3. User should follow ACI role
235 :expectedresults:
236 1. Entry should be added
237 2. Operation should succeed
238 3. Operation should succeed
239 """
240 # Add ACI
241 Domain(topo.standalone,
242 DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")'
243 f'(targetattr="*")(version 3.0; aci "Timeofday aci"; '
244 f'allow(all) userdn = "ldap:///{DAYWORKER_KEY}" '
245 f'and timeofday < "1200" ;)')
246
247 # Create a new connection for this test.
248 conn = UserAccount(topo.standalone, DAYWORKER_KEY).bind(PW_DM)
249 # Perform Operation
250 org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
251 if datetime.now().hour >= 12:
252 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
253 org.replace("seeAlso", "cn=1")
254 else:
255 org.replace("seeAlso", "cn=1")
256
257
258 def test_user_can_access_the_data_only_in_the_afternoon(topo, add_user, aci_of_user):
259 """
260 User can access the data only in the afternoon as per the ACI.
261
262 :id: 63eb5b1c-7ac5-11e8-bd46-8c16451d917b
263 :customerscenario: True
264 :setup: Standalone Server
265 :steps:
266 1. Add test entry
267 2. Add ACI
268 3. User should follow ACI role
269 :expectedresults:
270 1. Entry should be added
271 2. Operation should succeed
272 3. Operation should succeed
273 """
274 # Add ACI
275 Domain(topo.standalone,
276 DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")'
277 f'(targetattr="*")(version 3.0; aci "Timeofday aci"; '
278 f'allow(all) userdn = "ldap:///{NIGHTWORKER_KEY}" '
279 f'and timeofday > \'1200\' ;)')
280
281 # create a new connection for the test
282 conn = UserAccount(topo.standalone, NIGHTWORKER_KEY).bind(PW_DM)
283 # Perform Operation
284 org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
285 if datetime.now().hour < 12:
286 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
287 org.replace("seeAlso", "cn=1")
288 else:
289 org.replace("seeAlso", "cn=1")
290
291
292 def test_timeofday_keyword(topo, add_user, aci_of_user):
293 """
294 User NOWORKER_KEY can access the data as per the ACI after removing
295 ACI it cant.
296
297 :id: 681dd58e-7ac5-11e8-bed1-8c16451d917b
298 :customerscenario: True
299 :setup: Standalone Server + an user entry
300 :steps:
301 1. Compute current time (in minute)
302 2. Redo the 4 next steps until current time (in minute) has not changed
303 3. In 2. loop: Set previous time to current time
304 4. In 2. loop: Set the ACI
305 5. In 2. loop: Perform operation and record if access is allowed or not
306 6. In 2. loop: Compute again the current time
307 7. Check that access was allowed on last operation
308 8. Remove the ACI
309 9. Check that access is denied
310
311 :expectedresults:
312 1. Success
313 2. Success
314 3. Success
315 4. Success
316 5. Success
317 6. Success
318 7. last operation should have been allowed
319 8. Success
320 9. Operation should fail with ldap.INSUFFICIENT_ACCESS
321 """
322 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
323 aci = domain.get_attr_vals_utf8('aci')
324 # Create a new connection for this test.
325 conn = UserAccount(topo.standalone, NOWORKER_KEY).bind(PW_DM)
326 org = OrganizationalUnit(conn, TIMEOFDAY_OU_KEY)
327 now_1 = None
328 now_2 = "".join(time.strftime("%c").split()[3].split(":"))[:4]
329 # Perform the test in loop to avoid timing issues
330 while now_1 != now_2:
331 now_1 = now_2
332 # Set ACI
333 new_aci = f'(target = "ldap:///{TIMEOFDAY_OU_KEY}")' \
334 '(targetattr="*")(version 3.0; aci "Timeofday aci"; ' \
335 f'allow(all) userdn = "ldap:///{NOWORKER_KEY}" ' \
336 f'and timeofday = \'{now_1}\' ;)'
337 domain.replace("aci", [*aci, new_aci])
338 # Perform Operation
339 try:
340 org.replace("seeAlso", "cn=1")
341 access_allowed = True
342 except ldap.INSUFFICIENT_ACCESS:
343 access_allowed = False
344 # Check that time has not changed
345 now_2 = "".join(time.strftime("%c").split()[3].split(":"))[:4]
346 # Check that previous operation was successfull
347 assert access_allowed
348 # Remove ACI
349 domain.replace('aci', aci)
350 assert new_aci not in domain.get_attr_vals_utf8('aci')
351 # after removing the ACI user cannot access the data
352 time.sleep(1)
353 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
354 org.replace("seeAlso", "cn=1")
355
356
357 def test_dayofweek_keyword_test_everyday_can_access(topo, add_user, aci_of_user):
358 """
359 User can access the data EVERYDAY_KEY as per the ACI.
360
361 :id: 6c5922ca-7ac5-11e8-8f01-8c16451d917b
362 :customerscenario: True
363 :setup: Standalone Server
364 :steps:
365 1. Add test entry
366 2. Add ACI
367 3. User should follow ACI role
368 :expectedresults:
369 1. Entry should be added
370 2. Operation should succeed
371 3. Operation should succeed
372 """
373 # Add ACI
374 Domain(topo.standalone,
375 DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{DAYOFWEEK_OU_KEY}")'
376 f'(targetattr="*")(version 3.0; aci "Dayofweek aci"; '
377 f'allow(all) userdn = "ldap:///{EVERYDAY_KEY}" and '
378 f'dayofweek = "Sun, Mon, Tue, Wed, Thu, Fri, Sat" ;)')
379
380 # Create a new connection for this test.
381 conn = UserAccount(topo.standalone, EVERYDAY_KEY).bind(PW_DM)
382 # Perform Operation
383 org = OrganizationalUnit(conn, DAYOFWEEK_OU_KEY)
384 org.replace("seeAlso", "cn=1")
385
386
387 def test_dayofweek_keyword_today_can_access(topo, add_user, aci_of_user):
388 """
389 User can access the data one day per week as per the ACI.
390
391 :id: 7131dc88-7ac5-11e8-acc2-8c16451d917b
392 :customerscenario: True
393 :setup: Standalone Server + an user entry
394 :steps:
395 1. Compute current date
396 2. Redo the 4 next steps until current date has not changed
397 3. In 2. loop: Set previous time to current date
398 4. In 2. loop: Set the ACI
399 5. In 2. loop: Perform operation and record if access is allowed or not
400 6. In 2. loop: Compute again the current date
401 7. Check that access was allowed on last operation
402 :expectedresults:
403 1. Success
404 2. Success
405 3. Success
406 4. Success
407 5. Success
408 6. Success
409 7. last operation should have been allowed
410 """
411 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
412 aci = domain.get_attr_vals_utf8('aci')
413 # Create a new connection for this test.
414 conn = UserAccount(topo.standalone, TODAY_KEY).bind(PW_DM)
415 # Perform Operation
416 org = OrganizationalUnit(conn, DAYOFWEEK_OU_KEY)
417 today_1 = None
418 today_2 = time.strftime("%c").split()[0]
419 while today_1 != today_2:
420 # Set ACI
421 today_1 = today_2
422 new_aci = f'(target = "ldap:///{DAYOFWEEK_OU_KEY}")' \
423 '(targetattr="*")(version 3.0; aci "Dayofweek aci"; ' \
424 f'allow(all) userdn = "ldap:///{TODAY_KEY}" ' \
425 f'and dayofweek = \'{today_1}\' ;)'
426 domain.replace("aci", [*aci, new_aci])
427 try:
428 org.replace("seeAlso", "cn=1")
429 access_allowed = True
430 except ldap.INSUFFICIENT_ACCESS:
431 access_allowed = False
432 # Check that time has not changed
433 today_2 = time.strftime("%c").split()[0]
434 assert access_allowed
435
436
437 def test_user_cannot_access_the_data_at_all(topo, add_user, aci_of_user):
438 """
439 User cannot access the data at all as per the ACI.
440
441 :id: 75cdac5e-7ac5-11e8-968a-8c16451d917b
442 :customerscenario: True
443 :setup: Standalone Server
444 :steps:
445 1. Add test entry
446 2. Add ACI
447 3. User should follow ACI role
448 :expectedresults:
449 1. Entry should be added
450 2. Operation should succeed
451 3. Operation should succeed
452 """
453 # Add ACI
454 Domain(topo.standalone,
455 DEFAULT_SUFFIX).add("aci", f'(target = "ldap:///{DAYOFWEEK_OU_KEY}")'
456 f'(targetattr="*")(version 3.0; aci "Dayofweek aci"; '
457 f'allow(all) userdn = "ldap:///{TODAY_KEY}" '
458 f'and dayofweek = "$NEW_DATE" ;)')
459
460 # Create a new connection for this test.
461 conn = UserAccount(topo.standalone, NODAY_KEY).bind(PW_DM)
462 # Perform Operation
463 org = OrganizationalUnit(conn, DAYOFWEEK_OU_KEY)
464 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
465 org.replace("seeAlso", "cn=1")
466
467
468 if __name__ == "__main__":
469 CURRENT_FILE = os.path.realpath(__file__)
470 pytest.main("-s -v %s" % CURRENT_FILE)