Debian Codegraph @

dirsrvtests/tests/suites/healthcheck/health_security_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2026 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 import pytest
11 import os
12 import subprocess
13 import distro
14 import time
15 from datetime import *
16 from lib389.config import Encryption
17 from lib389.utils import *
18 from lib389._constants import *
19 from lib389.cli_base import FakeArgs
20 from test389.topologies import topology_st
21 from lib389.cli_ctl.health import health_check_run
22 from lib389.paths import Paths
23
24 CMD_OUTPUT = 'No issues found.'
25 JSON_OUTPUT = '[]'
26
27 ds_paths = Paths()
28 libfaketime = pytest.importorskip('libfaketime')
29 libfaketime.reexec_if_needed()
30
31 log = logging.getLogger(__name__)
32
33
34 def run_healthcheck_and_flush_log(topology, instance, searched_code, json, searched_code2=None):
35 args = FakeArgs()
36 args.instance = instance.serverid
37 args.verbose = instance.verbose
38 args.list_errors = False
39 args.list_checks = False
40 args.check = ['config', 'encryption', 'tls', 'fschecks']
41 args.dry_run = False
42 args.exclude_check = []
43
44 if json:
45 log.info('Use healthcheck with --json option')
46 args.json = json
47 health_check_run(instance, topology.logcap.log, args)
48 assert topology.logcap.contains(searched_code)
49 log.info('Healthcheck returned searched code: %s' % searched_code)
50
51 if searched_code2 is not None:
52 assert topology.logcap.contains(searched_code2)
53 log.info('Healthcheck returned searched code: %s' % searched_code2)
54 else:
55 log.info('Use healthcheck without --json option')
56 args.json = json
57 health_check_run(instance, topology.logcap.log, args)
58 assert topology.logcap.contains(searched_code)
59 log.info('Healthcheck returned searched code: %s' % searched_code)
60
61 if searched_code2 is not None:
62 assert topology.logcap.contains(searched_code2)
63 log.info('Healthcheck returned searched code: %s' % searched_code2)
64
65 log.info('Clear the log')
66 topology.logcap.flush()
67
68
69 @pytest.mark.xfail(ds_is_older("1.4.1"), reason="Not implemented")
70 def test_healthcheck_insecure_pwd_hash_configured(topology_st):
71 """Check if HealthCheck returns DSCLE0002 code
72
73 :id: 6baf949c-a5eb-4f4e-83b4-8302e677758a
74 :setup: Standalone instance
75 :steps:
76 1. Create DS instance
77 2. Configure an insecure passwordStorageScheme (as SHA) for the instance
78 3. Use HealthCheck without --json option
79 4. Use HealthCheck with --json option
80 5. Set passwordStorageScheme and nsslapd-rootpwstoragescheme to PBKDF2_SHA512
81 6. Use HealthCheck without --json option
82 7. Use HealthCheck with --json option
83 :expectedresults:
84 1. Success
85 2. Success
86 3. Healthcheck reports DSCLE0002 code and related details
87 4. Healthcheck reports DSCLE0002 code and related details
88 5. Success
89 6. Healthcheck reports no issue found
90 7. Healthcheck reports no issue found
91 """
92
93 RET_CODE = 'DSCLE0002'
94
95 standalone = topology_st.standalone
96
97 log.info('Configure an insecure passwordStorageScheme (SHA)')
98 standalone.config.set('passwordStorageScheme', 'SHA')
99
100 standalone.stop()
101 try:
102 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=RET_CODE)
103 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=RET_CODE)
104 finally:
105 if not standalone.status():
106 standalone.start()
107 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=RET_CODE)
108 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=RET_CODE)
109
110
111 log.info('Set passwordStorageScheme and nsslapd-rootpwstoragescheme to PBKDF2-SHA512')
112 standalone.config.set('passwordStorageScheme', 'PBKDF2-SHA512')
113 standalone.config.set('nsslapd-rootpwstoragescheme', 'PBKDF2-SHA512')
114
115 standalone.stop()
116 try:
117 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=CMD_OUTPUT)
118 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=JSON_OUTPUT)
119 finally:
120 if not standalone.status():
121 standalone.start()
122 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=CMD_OUTPUT)
123 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=JSON_OUTPUT)
124
125
126 @pytest.mark.xfail(ds_is_older("1.4.1"), reason="Not implemented")
127 def test_healthcheck_min_allowed_tls_version_too_low(topology_st):
128 """Check if HealthCheck returns DSELE0001 code
129
130 :id: a4be3390-9508-4827-8f82-e4e21081caab
131 :setup: Standalone instance
132 :steps:
133 1. Create DS instance
134 2. Set the TLS minimum version to TLS1.0
135 3. Use HealthCheck without --json option
136 4. Use HealthCheck with --json option
137 5. Set the TLS minimum version to TLS1.2
138 6. Use HealthCheck without --json option
139 7. Use HealthCheck with --json option
140 :expectedresults:
141 1. Success
142 2. Success
143 3. Healthcheck reports DSELE0001 code and related details
144 4. Healthcheck reports DSELE0001 code and related details
145 5. Success
146 6. Healthcheck reports no issue found
147 7. Healthcheck reports no issue found
148 """
149
150 RET_CODE = 'DSELE0001'
151 HIGHER_VS = 'TLS1.2'
152 SMALL_VS = 'TLS1.0'
153 RHEL = 'Red Hat Enterprise Linux'
154
155 standalone = topology_st.standalone
156
157 standalone.enable_tls()
158
159 # We have to update-crypto-policies to LEGACY, otherwise we can't set TLS1.0
160 log.info('Updating crypto policies')
161 assert subprocess.check_call(['update-crypto-policies', '--set', 'LEGACY']) == 0
162
163 log.info('Set the TLS minimum version to TLS1.0')
164 enc = Encryption(standalone)
165 enc.replace('sslVersionMin', SMALL_VS)
166 standalone.restart()
167
168 standalone.stop()
169 try:
170 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=RET_CODE)
171 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=RET_CODE)
172 finally:
173 if not standalone.status():
174 standalone.start()
175 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=RET_CODE)
176 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=RET_CODE)
177
178 log.info('Set the TLS minimum version to TLS1.2')
179 enc.replace('sslVersionMin', HIGHER_VS)
180 standalone.restart()
181
182 standalone.stop()
183 try:
184 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=CMD_OUTPUT)
185 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=JSON_OUTPUT)
186 finally:
187 if not standalone.status():
188 standalone.start()
189 run_healthcheck_and_flush_log(topology_st, standalone, json=False, searched_code=CMD_OUTPUT)
190 run_healthcheck_and_flush_log(topology_st, standalone, json=True, searched_code=JSON_OUTPUT)
191
192 if RHEL in distro.linux_distribution():
193 log.info('Set crypto-policies back to DEFAULT')
194 assert subprocess.check_call(['update-crypto-policies', '--set', 'DEFAULT']) == 0
195
196
197 @pytest.mark.xfail(ds_is_older("1.4.1"), reason="Not implemented")
198 def test_healthcheck_resolvconf_bad_file_perm(topology_st):
199 """Check if HealthCheck returns DSPERMLE0001 code
200
201 :id: 8572b9e9-70e7-49e9-b745-864f6f2468a8
202 :setup: Standalone instance
203 :steps:
204 1. Create DS instance
205 2. Change the /etc/resolv.conf file permissions to 444
206 3. Use HealthCheck without --json option
207 4. Use HealthCheck with --json option
208 5. set /etc/resolv.conf permissions to 644
209 6. Use HealthCheck without --json option
210 7. Use HealthCheck with --json option
211 :expectedresults:
212 1. Success
213 2. Success
214 3. Healthcheck reports DSPERMLE0001 code and related details
215 4. Healthcheck reports DSPERMLE0001 code and related details
216 5. Success
217 6. Healthcheck reports no issue found
218 7. Healthcheck reports no issue found
219 """
220
221 RET_CODE = 'DSPERMLE0001'
222
223 standalone = topology_st.standalone
224
225 log.info('Change the /etc/resolv.conf file permissions to 444')
226 os.chmod('/etc/resolv.conf', 0o444)
227
228 standalone.stop()
229 try:
230 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
231 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
232 finally:
233 if not standalone.status():
234 standalone.start()
235 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
236 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
237
238 log.info('Change the /etc/resolv.conf file permissions to 644')
239 os.chmod('/etc/resolv.conf', 0o644)
240
241 standalone.stop()
242 try:
243 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
244 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
245 finally:
246 if not standalone.status():
247 standalone.start()
248 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
249 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
250
251
252 @pytest.mark.xfail(ds_is_older("1.4.1"), reason="Not implemented")
253 def test_healthcheck_pwdfile_bad_file_perm(topology_st):
254 """Check if HealthCheck returns DSPERMLE0002 code
255
256 :id: ec137d66-bad6-4eed-90bd-fc1d572bbe1f
257 :setup: Standalone instance
258 :steps:
259 1. Create DS instance
260 2. Change the /etc/dirsrv/slapd-xxx/pwdfile.txt permissions to 000
261 3. Use HealthCheck without --json option
262 4. Use HealthCheck with --json option
263 5. Change the /etc/dirsrv/slapd-xxx/pwdfile.txt permissions to 400
264 6. Use HealthCheck without --json option
265 7. Use HealthCheck with --json option
266 :expectedresults:
267 1. Success
268 2. Success
269 3. Healthcheck reports DSPERMLE0002 code and related details
270 4. Healthcheck reports DSPERMLE0002 code and related details
271 5. Success
272 6. Healthcheck reports no issue found
273 7. Healthcheck reports no issue found
274 """
275
276 RET_CODE = 'DSPERMLE0002'
277
278 standalone = topology_st.standalone
279 cert_dir = standalone.ds_paths.cert_dir
280
281 log.info('Change the /etc/dirsrv/slapd-{}/pwdfile.txt permissions to 000'.format(standalone.serverid))
282 os.chmod('{}/pwdfile.txt'.format(cert_dir), 0o000)
283
284 standalone.stop()
285 try:
286 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
287 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
288 finally:
289 if not standalone.status():
290 standalone.start()
291 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
292 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
293
294 log.info('Change the /etc/dirsrv/slapd-{}/pwdfile.txt permissions to 400'.format(standalone.serverid))
295 os.chmod('{}/pwdfile.txt'.format(cert_dir), 0o400)
296
297 standalone.stop()
298 try:
299 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
300 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
301 finally:
302 if not standalone.status():
303 standalone.start()
304 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
305 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
306
307
308 @pytest.mark.xfail(ds_is_older("1.4.1"), reason="Not implemented")
309 def test_healthcheck_certif_expiring_within_30d(topology_st):
310 """Check if HealthCheck returns DSCERTLE0001 code
311
312 :id: f30b8115-0fd3-4c1d-9f5a-383bea7ea869
313 :setup: Standalone instance
314 :steps:
315 1. Create DS instance
316 2. Use libfaketime to tell the process the date is within 30 days before certificate expiration
317 3. Use HealthCheck without --json option
318 4. Use HealthCheck with --json option
319 :expectedresults:
320 1. Success
321 2. Success
322 3. Healthcheck reports DSCERTLE0001 code and related details
323 4. Healthcheck reports DSCERTLE0001 code and related details
324 """
325
326 RET_CODE = 'DSCERTLE0001'
327
328 standalone = topology_st.standalone
329
330 standalone.enable_tls()
331
332 # Cert is valid two years from today, so we count the date that is within 30 days before certificate expiration
333 date_future = datetime.now() + timedelta(days=701)
334
335 with libfaketime.fake_time(date_future):
336 time.sleep(1)
337 standalone.stop()
338 try:
339 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
340 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
341 finally:
342 if not standalone.status():
343 standalone.start()
344 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
345 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
346
347 # Try again with real time just to make sure no issues were found
348 standalone.stop()
349 try:
350 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
351 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
352 finally:
353 if not standalone.status():
354 standalone.start()
355 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
356 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
357
358
359 @pytest.mark.xfail(ds_is_older("1.4.1"), reason="Not implemented")
360 def test_healthcheck_certif_expired(topology_st):
361 """Check if HealthCheck returns DSCERTLE0002 code
362
363 :id: ceff2c22-62c0-4fd9-b737-930a88458d68
364 :setup: Standalone instance
365 :steps:
366 1. Create DS instance
367 2. Use libfaketime to tell the process the date is after certificate expiration
368 3. Use HealthCheck without --json option
369 4. Use HealthCheck with --json option
370 :expectedresults:
371 1. Success
372 2. Success
373 3. Healthcheck reports DSCERTLE0002 code and related details
374 4. Healthcheck reports DSCERTLE0002 code and related details
375 """
376
377 RET_CODE = 'DSCERTLE0002'
378
379 standalone = topology_st.standalone
380
381 standalone.enable_tls()
382
383 # Cert is valid two years from today, so we count the date that is after expiration
384 date_future = datetime.now() + timedelta(days=731)
385
386 with libfaketime.fake_time(date_future):
387 time.sleep(1)
388 standalone.stop()
389 try:
390 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
391 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
392 finally:
393 if not standalone.status():
394 standalone.start()
395 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=False)
396 run_healthcheck_and_flush_log(topology_st, standalone, RET_CODE, json=True)
397
398 # Try again with real time just to make sure no issues were found
399 standalone.stop()
400 try:
401 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
402 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
403 finally:
404 if not standalone.status():
405 standalone.start()
406 run_healthcheck_and_flush_log(topology_st, standalone, CMD_OUTPUT, json=False)
407 run_healthcheck_and_flush_log(topology_st, standalone, JSON_OUTPUT, json=True)
408
409
410 if __name__ == '__main__':
411 # Run isolated
412 # -s for DEBUG mode
413 CURRENT_FILE = os.path.realpath(__file__)