Debian Codegraph @

dirsrvtests/tests/suites/clu/dsconf_dsctl_security_cli_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 """Test dsconf and dsctl security CLI subcommands using subprocess"""
10
11 import json
12 import logging
13 import os
14 import pytest
15 import subprocess
16 import tempfile
17 from test389.topologies import topology_st as topo
18
19 pytestmark = pytest.mark.tier1
20
21 log = logging.getLogger(__name__)
22
23 TEST_CERT_PEM = """-----BEGIN CERTIFICATE-----
24 MIIF2zCCA8OgAwIBAgIFAMl06zEwDQYJKoZIhvcNAQELBQAwZTELMAkGA1UEBhMC
25 QVUxEzARBgNVBAgTClF1ZWVuc2xhbmQxDjAMBgNVBAcTBTM4OWRzMRAwDgYDVQQK
26 Ewd0ZXN0aW5nMR8wHQYDVQQDExZzc2NhLjM4OWRzLmV4YW1wbGUuY29tMB4XDTI2
27 MDIyNTE1MDYzNFoXDTQ2MDIyNTE1MDYzNFowgZMxCzAJBgNVBAYTAkFVMRMwEQYD
28 VQQIEwpRdWVlbnNsYW5kMQ4wDAYDVQQHEwUzODlkczEQMA4GA1UEChMHdGVzdGlu
29 ZzEtMCsGA1UEKhMkMmU2YzNmMzUtMjhkMy00ZmU4LWEyYmUtM2ZmYTIzMWRmYjA3
30 MR4wHAYDVQQDExVsb2NhbGhvc3QubG9jYWxkb21haW4wggIiMA0GCSqGSIb3DQEB
31 AQUAA4ICDwAwggIKAoICAQCSVoeRTmQhxbJ9CJj6hPczhf7PzYVR/xdR8s0/Q5wL
32 kNJUyWfJfyVORe8u0X1zKLyBY4KX3IWIxkHi2vfLkFay7AzqETDlZzKXvrvpQB6G
33 TfcVXf912m53EqeJs7g9IwDZzmTK48okOSL9WpMIGmhSLzxoyQE8Fd5grnD9wwW4
34 rJuz/dFFzWuys2acErvxKVCLOFd7RX0itXVOZ7IdosiZoWNz8PjLZjihBM4/4cow
35 6SrK8IQhLUg5uT6VOftSag8kl1UkcyMySwfALNxoVIh8eyItp2yXGEjX3sPKEeeD
36 p8Pu/JrsYmdVJAjPhVUd0I8KydNGe0Iu0AMSWA0gylXY/5PDTMeeHtduXe945fBB
37 iXWMZM/SXLwRSt/+IE2QnSNCTlqX+QQkl5gQu2cxir/KR5lqnCqrxr74qFUf+WtX
38 LplXkKwwo/NtEcvWlKWBtHDQ3z9vqJgAWI/20ITXrySBDVKZCyCt5fH+J/o3gQlO
39 vNah1r/+S63T7dQHbCLg5uzG+zYnUNlOcXZpXvfXGsSHkSGEB1txY7jyoDg/j1M2
40 5yEZUHvY3NMdvEDh3s785TBEBm7ZgXnGVTHbGdJdW4X+hKPHtlmgGFJ6THBoYYj7
41 v88qivm1GYB1wJmKcTIlyLuAj6yvI4CgSKHAWNIf8yohp1BfKm3PHVW9DCRsDrR4
42 mwIDAQABo2MwYTALBgNVHQ8EBAMCBPAwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsG
43 AQUFBwMBMBEGCWCGSAGG+EIBAQQEAwIGwDAgBgNVHREEGTAXghVsb2NhbGhvc3Qu
44 bG9jYWxkb21haW4wDQYJKoZIhvcNAQELBQADggIBABwJZZcPRtw/UtWJXQb4KtSf
45 o6d1e6DMDz2O3r3l7PxU2ILY7I9VC1CeVl/mXbwMycwr24S5PcWpRNlU6BipDOlO
46 Ck2+yvflV/LRHn25zwXRIfjlVVb6BTn3QoKbtTU76g/nko4pDb2ksPixzuqcQUOC
47 HVCsY7caXQQKlH4o4OSCYaYX9qkJYVjA6yv3k39UNXaWzo1DI7ymjif0Lqr/eCV8
48 jIXLS/qAouPSZWib5k66sEH0RdyC1FsBVbpfdIuJrdSonE3KZhYlL+B93LhEAqDN
49 nzTkiTFrLlLQ583in7upQA9MQ5J3cW6WpWIUjRmUBpI/BQTkCzANucv9lVX//dDD
50 OZnM1ImTjwp2bTqxdZev0VW/rSMF5xKCd6tiTsmYozjcYDJ4BIgX5AiL/lu2NTVv
51 Rumt/wQCUq2R35XOtzD6h1JRBFS+F/9P/tJOZyf6Nd+/t6ugkeKd9GgVzkB+lF62
52 l8Wf5r4rV5fZgZqiAuq872jDDQMzK2kTUDE0yxQDSfoStVhO/3PoavqPLIDym6RK
53 HxMlKKIkrC1LnURE+tiMPGp7qrfBzahU+7EVnM+05C9fZn6BBd1g9HpdEbszOKP/
54 zom8MYe41Lq9vjFiYN+TkququhsFcs+i5YS2wkAT7F5a6NJxskP+Lf0EcBC+TC6k
55 dMsdEJ0KW0YniBYuPhvx
56 -----END CERTIFICATE-----"""
57
58
59 @pytest.fixture(scope="module")
60 def setup_tls(topo):
61 """Enable TLS on the instance"""
62 topo.standalone.enable_tls()
63 yield topo
64 # Cleanup is handled by the topology fixture
65
66
67 def run_cmd(cmd, env=None, check=True, stdin_input=None):
68 """Helper function to run a command and return output
69
70 :param cmd: Command list to run
71 :param env: Environment variables
72 :param check: Whether to check return code
73 :param stdin_input: String to send to stdin
74 :return: tuple of (returncode, stdout, stderr)
75 """
76 log.info(f'Running command: {" ".join(cmd)}')
77 proc = subprocess.Popen(
78 cmd,
79 stdout=subprocess.PIPE,
80 stderr=subprocess.PIPE,
81 stdin=subprocess.PIPE if stdin_input else None,
82 env=env
83 )
84 stdin_bytes = stdin_input.encode('utf-8') if stdin_input else None
85 stdout, stderr = proc.communicate(input=stdin_bytes)
86 stdout_str = stdout.decode('utf-8') if stdout else ''
87 stderr_str = stderr.decode('utf-8') if stderr else ''
88
89 log.info(f'Return code: {proc.returncode}')
90 if stdout_str:
91 log.info(f'STDOUT: {stdout_str}')
92 if stderr_str:
93 log.info(f'STDERR: {stderr_str}')
94
95 if check and proc.returncode != 0:
96 raise subprocess.CalledProcessError(
97 proc.returncode, cmd, stdout, stderr
98 )
99
100 return proc.returncode, stdout_str, stderr_str
101
102
103 def _create_temp_cert_file():
104 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.pem') as cert_file:
105 cert_file.write(TEST_CERT_PEM)
106 return cert_file.name
107
108
109 def test_dsconf_security_get(topo):
110 """Test dsconf security get command via subprocess
111
112 :id: a1a2a3a4-0001-0001-0001-000000000001
113 :setup: Standalone Instance
114 :steps:
115 1. Run dsconf security get command
116 2. Verify output contains security attributes
117 3. Run with --json flag
118 4. Verify JSON output is valid
119 :expectedresults:
120 1. Command succeeds
121 2. Output contains expected attributes
122 3. JSON command succeeds
123 4. JSON is valid and parseable
124 """
125 inst = topo.standalone
126 dsconf_cmd = [
127 'dsconf',
128 inst.serverid,
129 'security',
130 'get'
131 ]
132
133 # Test plain output
134 returncode, stdout, stderr = run_cmd(dsconf_cmd)
135 assert returncode == 0
136 assert 'nsslapd-security:' in stdout
137 assert 'nsslapd-secureport:' in stdout or 'nsslapd-securePort:' in stdout.lower()
138
139 # Test JSON output
140 dsconf_cmd_json = ['dsconf', inst.serverid, '--json', 'security', 'get']
141 returncode, stdout, stderr = run_cmd(dsconf_cmd_json)
142 assert returncode == 0
143
144 # Parse and validate JSON
145 result = json.loads(stdout)
146 assert 'type' in result
147 assert 'items' in result
148
149
150 def test_dsconf_security_set(topo):
151 """Test dsconf security set command via subprocess
152
153 :id: a1a2a3a4-0001-0001-0001-000000000002
154 :setup: Standalone Instance
155 :steps:
156 1. Set session timeout using dsconf
157 2. Verify setting with get command
158 3. Reset to original value
159 :expectedresults:
160 1. Set command succeeds
161 2. Value is correctly set
162 3. Reset succeeds
163 """
164 inst = topo.standalone
165 dsconf_base = [
166 'dsconf',
167 inst.serverid,
168 'security'
169 ]
170
171 # Get original value
172 cmd = ['dsconf', inst.serverid, '--json', 'security', 'get']
173 returncode, stdout, stderr = run_cmd(cmd)
174 original_data = json.loads(stdout)
175 original_timeout = original_data.get('items', {}).get('nsslapd-ssl-session-timeout', ['600'])[0]
176
177 try:
178 # Set new value
179 cmd = dsconf_base + ['set', '--session-timeout', '900']
180 returncode, stdout, stderr = run_cmd(cmd)
181 assert returncode == 0
182
183 # Verify new value
184 cmd = dsconf_base + ['get']
185 returncode, stdout, stderr = run_cmd(cmd)
186 assert '900' in stdout
187
188 finally:
189 # Reset to original
190 cmd = dsconf_base + ['set', '--session-timeout', original_timeout]
191 run_cmd(cmd, check=False)
192
193
194 def test_dsconf_security_enable_disable(setup_tls):
195 """Test dsconf security enable/disable commands via subprocess
196
197 :id: a1a2a3a4-0001-0001-0001-000000000003
198 :setup: Standalone Instance with TLS
199 :steps:
200 1. Enable security
201 2. Verify security is enabled
202 3. Disable security
203 4. Verify security is disabled
204 :expectedresults:
205 1. Enable command succeeds
206 2. Security is on
207 3. Disable command succeeds
208 4. Security is off
209 """
210 inst = setup_tls.standalone
211 dsconf_base = [
212 'dsconf',
213 inst.serverid,
214 'security'
215 ]
216
217 # Enable security
218 cmd = dsconf_base + ['enable']
219 returncode, stdout, stderr = run_cmd(cmd)
220 assert returncode == 0
221
222 # Verify enabled
223 cmd = dsconf_base + ['get']
224 returncode, stdout, stderr = run_cmd(cmd)
225 assert 'nsslapd-security: on' in stdout
226
227 # Disable security
228 cmd = dsconf_base + ['disable']
229 returncode, stdout, stderr = run_cmd(cmd)
230 assert returncode == 0
231
232 # Verify disabled
233 cmd = dsconf_base + ['get']
234 returncode, stdout, stderr = run_cmd(cmd)
235 assert 'nsslapd-security: off' in stdout
236
237
238 def test_dsconf_security_rsa_get(topo):
239 """Test dsconf security rsa get command via subprocess
240
241 :id: a1a2a3a4-0001-0001-0001-000000000004
242 :setup: Standalone Instance
243 :steps:
244 1. Run dsconf security rsa get command
245 2. Verify output contains RSA attributes
246 :expectedresults:
247 1. Command succeeds
248 2. Output contains RSA activation attribute
249 """
250 inst = topo.standalone
251 cmd = [
252 'dsconf',
253 inst.serverid,
254 'security',
255 'rsa',
256 'get'
257 ]
258
259 returncode, stdout, stderr = run_cmd(cmd)
260 assert returncode == 0
261 assert 'nssslactivation:' in stdout.lower() or 'nsSSLActivation:' in stdout
262
263
264 def test_dsconf_security_rsa_enable_disable(setup_tls):
265 """Test dsconf security rsa enable/disable via subprocess
266
267 :id: a1a2a3a4-0001-0001-0001-000000000005
268 :setup: Standalone Instance with TLS
269 :steps:
270 1. Enable RSA
271 2. Verify RSA is enabled
272 3. Disable RSA
273 4. Verify RSA is disabled
274 :expectedresults:
275 1. Enable succeeds
276 2. RSA is on
277 3. Disable succeeds
278 4. RSA is off
279 """
280 inst = setup_tls.standalone
281 dsconf_base = [
282 'dsconf',
283 inst.serverid,
284 'security',
285 'rsa'
286 ]
287
288 # Enable RSA
289 cmd = dsconf_base + ['enable']
290 returncode, stdout, stderr = run_cmd(cmd)
291 assert returncode == 0
292
293 # Verify enabled
294 cmd = dsconf_base + ['get']
295 returncode, stdout, stderr = run_cmd(cmd)
296 assert 'on' in stdout.lower()
297
298 # Disable RSA
299 cmd = dsconf_base + ['disable']
300 returncode, stdout, stderr = run_cmd(cmd)
301 assert returncode == 0
302
303 # Verify disabled
304 cmd = dsconf_base + ['get']
305 returncode, stdout, stderr = run_cmd(cmd)
306 assert 'off' in stdout.lower()
307
308
309 def test_dsconf_security_ciphers_list(setup_tls):
310 """Test dsconf security ciphers list command via subprocess
311
312 :id: a1a2a3a4-0001-0001-0001-000000000006
313 :setup: Standalone Instance with TLS
314 :steps:
315 1. List all ciphers
316 2. List enabled ciphers
317 3. List supported ciphers
318 4. List disabled ciphers
319 5. Test with --json flag
320 :expectedresults:
321 1. Command succeeds and shows ciphers
322 2. Command succeeds
323 3. Command succeeds
324 4. Command succeeds
325 5. JSON output is valid
326 """
327 inst = setup_tls.standalone
328 dsconf_base = [
329 'dsconf',
330 inst.serverid,
331 'security',
332 'ciphers',
333 'list'
334 ]
335
336 # List all ciphers
337 cmd = dsconf_base.copy()
338 returncode, stdout, stderr = run_cmd(cmd)
339 assert returncode == 0
340
341 # List enabled ciphers
342 cmd = dsconf_base + ['--enabled']
343 returncode, stdout, stderr = run_cmd(cmd)
344 assert returncode == 0
345
346 # List supported ciphers
347 cmd = dsconf_base + ['--supported']
348 returncode, stdout, stderr = run_cmd(cmd)
349 assert returncode == 0
350
351 # List disabled ciphers
352 cmd = dsconf_base + ['--disabled']
353 returncode, stdout, stderr = run_cmd(cmd)
354 assert returncode == 0
355
356 # Test JSON output
357 cmd = ['dsconf', inst.serverid, '--json', 'security', 'ciphers', 'list']
358 returncode, stdout, stderr = run_cmd(cmd)
359 assert returncode == 0
360 # Validate JSON
361 result = json.loads(stdout)
362 assert isinstance(result, (dict, list))
363
364
365 def test_dsconf_security_ciphers_get(topo):
366 """Test dsconf security ciphers get command via subprocess
367
368 :id: a1a2a3a4-0001-0001-0001-000000000007
369 :setup: Standalone Instance
370 :steps:
371 1. Run dsconf security ciphers get command
372 2. Verify output is returned
373 :expectedresults:
374 1. Command succeeds
375 2. Output contains cipher information
376 """
377 inst = topo.standalone
378 cmd = [
379 'dsconf',
380 inst.serverid,
381 'security',
382 'ciphers',
383 'get'
384 ]
385
386 returncode, stdout, stderr = run_cmd(cmd)
387 assert returncode == 0
388 # Output should contain something (cipher list or <undefined>)
389 assert len(stdout) > 0
390
391
392 def test_dsconf_security_ciphers_enable_disable(setup_tls):
393 """Test dsconf security ciphers enable/disable via subprocess
394
395 :id: a1a2a3a4-0001-0001-0001-000000000008
396 :setup: Standalone Instance with TLS
397 :steps:
398 1. Enable a specific cipher
399 2. Disable a specific cipher
400 :expectedresults:
401 1. Enable command succeeds
402 2. Disable command succeeds
403 """
404 inst = setup_tls.standalone
405 dsconf_base = [
406 'dsconf',
407 inst.serverid,
408 'security',
409 'ciphers'
410 ]
411
412 # Enable a cipher
413 cmd = dsconf_base + ['enable', 'TLS_AES_128_GCM_SHA256']
414 returncode, stdout, stderr = run_cmd(cmd, check=False)
415 # May fail if cipher is already enabled or not available, that's ok
416
417 # Disable a cipher
418 cmd = dsconf_base + ['disable', 'TLS_AES_128_GCM_SHA256']
419 returncode, stdout, stderr = run_cmd(cmd, check=False)
420 # May fail if cipher is already disabled, that's ok
421
422
423 def test_dsconf_security_certificate_list(setup_tls):
424 """Test dsconf security certificate list command via subprocess
425
426 :id: a1a2a3a4-0001-0001-0001-000000000009
427 :setup: Standalone Instance with TLS
428 :steps:
429 1. List certificates
430 2. Verify Server-Cert is in list
431 3. Test with --json flag
432 :expectedresults:
433 1. Command succeeds
434 2. Server-Cert is present
435 3. JSON output is valid
436 """
437 inst = setup_tls.standalone
438 cmd = [
439 'dsconf',
440 inst.serverid,
441 'security',
442 'certificate',
443 'list'
444 ]
445
446 # Test plain output
447 returncode, stdout, stderr = run_cmd(cmd)
448 assert returncode == 0
449 assert 'Server-Cert' in stdout
450
451 # Test JSON output
452 cmd_json = ['dsconf', inst.serverid, '--json', 'security', 'certificate', 'list']
453 returncode, stdout, stderr = run_cmd(cmd_json)
454 assert returncode == 0
455 result = json.loads(stdout)
456 assert isinstance(result, (dict, list))
457
458
459 def test_dsconf_security_certificate_get(setup_tls):
460 """Test dsconf security certificate get command via subprocess
461
462 :id: a1a2a3a4-0001-0001-0001-000000000010
463 :setup: Standalone Instance with TLS
464 :steps:
465 1. Get Server-Cert details
466 2. Verify output contains certificate information
467 :expectedresults:
468 1. Command succeeds
469 2. Output shows certificate details
470 """
471 inst = setup_tls.standalone
472 cmd = [
473 'dsconf',
474 inst.serverid,
475 'security',
476 'certificate',
477 'get',
478 'Server-Cert'
479 ]
480
481 returncode, stdout, stderr = run_cmd(cmd)
482 assert returncode == 0
483 assert 'Server-Cert' in stdout or 'Certificate Name:' in stdout
484
485
486 def test_dsconf_security_certificate_add_delete(setup_tls):
487 """Test dsconf security certificate add and delete via subprocess
488
489 :id: a1a2a3a4-0001-0001-0001-000000000011
490 :setup: Standalone Instance with TLS
491 :steps:
492 1. Create a test certificate PEM file
493 2. Add certificate via dsconf
494 3. Verify certificate was added
495 4. Delete certificate
496 5. Verify certificate was deleted
497 :expectedresults:
498 1. PEM file created
499 2. Add command succeeds
500 3. Certificate is in list
501 4. Delete command succeeds
502 5. Certificate is not in list
503 """
504 inst = setup_tls.standalone
505 dsconf_base = [
506 'dsconf',
507 inst.serverid,
508 'security',
509 'certificate'
510 ]
511
512 # Create test certificate
513 pem_content = """-----BEGIN CERTIFICATE-----
514 MIIFZjCCA06gAwIBAgIFAL18JOowDQYJKoZIhvcNAQELBQAwZTELMAkGA1UEBhMC
515 QVUxEzARBgNVBAgTClF1ZWVuc2xhbmQxDjAMBgNVBAcTBTM4OWRzMRAwDgYDVQQK
516 Ewd0ZXN0aW5nMR8wHQYDVQQDExZzc2NhLjM4OWRzLmV4YW1wbGUuY29tMB4XDTIy
517 MTAyNTE5NDU0M1oXDTI0MTAyNTE5NDU0M1owZTELMAkGA1UEBhMCQVUxEzARBgNV
518 BAgTClF1ZWVuc2xhbmQxDjAMBgNVBAcTBTM4OWRzMRAwDgYDVQQKEwd0ZXN0aW5n
519 MR8wHQYDVQQDExZzc2NhLjM4OWRzLmV4YW1wbGUuY29tMIICIjANBgkqhkiG9w0B
520 AQEFAAOCAg8AMIICCgKCAgEA5E+pd7+8lBsbTKdjHgkSLi2Z5T5G9T+3wziDHhsz
521 F0nG+IOu5yYVkoj/bMxR3sNNlbDLk5ATyNAfytW3cAUZ3NLqm6bmEZdUjD6YycVk
522 AvrfY3zVVE9Debfw6JI3ml8JlC3t8dqn2KT7dmSjvr9zPS95HU+RepjzAqJAKY3B
523 27v0cMetUnxG4pqc7zqnSZJXVP/OXMKSNpujHnK8HyjT8tUJIYQ0YvU2JPJpz3fC
524 BJrmzgO2xYLgLPu6abhP6PQ6uUU+d4j36lG4J/4OiMY0Lr+mnaBAaD3ULPtN5eZh
525 fjQ9d+Sh89xHz92icWhkn8c7IHNEZNtMHNTNJiNbWKuU9HpBWNjWHJoxSxXn4Emr
526 DSfG+lq2UU2m9m+XrDK/7t0W/zC3S+zwcyqM8SJAiZnGEi85058wB0BB1HnnAfFX
527 gel3uZFhnR4d86O/vO5VUqg5Ko795DPzPa3SU4rR36U3nUF7g5WhEAmYNCj683D3
528 DJDPJeCZmis7xtYB5K6Wu6SnFDxBEfhcWSsamWM286KntOiUtqQEzDy4OpZEUsgq
529 s7uqQSl/dfGdY9hCpXMYhlvMfVv3aIoM5zPuXN2cE1QkTnE1pyo8gZqnPLFZnwc9
530 FT+Wjpy0EmsAM/5AIed5h+JgJ304P+wkyjf7APUZyUwf4UJN6aro6N8W23F7dAu5
531 uJ0CAwEAAaMdMBswDAYDVR0TBAUwAwEB/zALBgNVHQ8EBAMCAgQwDQYJKoZIhvcN
532 AQELBQADggIBADFlVdDp1gTF/gl5ysZoo4/4BBSpLx5JDeyPE6BityZ/lwHvBUq3
533 VzmIsU6kxAJfk0p9S7G4LgHIC/DVsLTIE5do6tUdyrawvcaanbYn9ScNoFVQ0GDS
534 C6Ir8PftEvc6bpI4hjkv4goK9fTq5Jtv4LSuRfxFEwoLO+WN8a65IFWlaHJl/Erp
535 9fzP+JKDo8zeh4qnMkaTSCBmLWa8kErrV462RU+qZktf/V4/gWg6k5Vp+82xNk7f
536 9/Mrg9KshNux7A4YCd8LgLEeCgsigi4N6zcfjQB0Rh5u9kXu/hzOjh379ki/vqju
537 i+MTVH97LMB47uR1LEl0VvhWSjID0ePUtbPHCJwOsxWyxBCJY6V7A9nj52uXMGuX
538 xghssZTFvRK6Bb1OiPNYRGqmuymm8rcSFdsY5yemkxJ6kfn40JIRCmVFwqaqu7MC
539 nxyaWAKpRHKM5IyeVZHkFzL9jR/2tVBbjfCAl6YSwM759VcOsw2SGMQKpGIPEBTa
540 1NBdlG45aWJBx5jBdVfOskLjxmBjosByJJHRLtrUBvg66ZBsx1k0c9XjsKmC59JP
541 AzI8zYp/TY/6T5igxM+CSx98DsJFccPBZFFJX+YYRL7DFN38Yb7jMgIUXYHS28Gc
542 1c8kz7ylcQB8lKgCgpcBCH5ZSnLVAnH3uqCygxSTgTo+jgJklKc0xFuR
543 -----END CERTIFICATE-----"""
544
545 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.pem') as pem_file:
546 pem_file.write(pem_content)
547 pem_path = pem_file.name
548
549 try:
550 # Add certificate
551 cmd = dsconf_base + ['add', '--file', pem_path, '--name', 'TEST_CLI_CERT']
552 returncode, stdout, stderr = run_cmd(cmd, check=False)
553 # May fail if cert already exists, continue anyway
554
555 # Verify it was added
556 cmd = dsconf_base + ['list']
557 returncode, stdout, stderr = run_cmd(cmd)
558 # Certificate should be in list (or may have failed to add)
559
560 # Delete certificate
561 cmd = dsconf_base + ['del', 'TEST_CLI_CERT']
562 returncode, stdout, stderr = run_cmd(cmd, check=False)
563 # May fail if cert doesn't exist, that's ok
564
565 finally:
566 if os.path.exists(pem_path):
567 os.unlink(pem_path)
568
569
570 def test_dsconf_security_ca_certificate_list(setup_tls):
571 """Test dsconf security ca-certificate list via subprocess
572
573 :id: a1a2a3a4-0001-0001-0001-000000000012
574 :setup: Standalone Instance with TLS
575 :steps:
576 1. List CA certificates
577 2. Verify command succeeds
578 :expectedresults:
579 1. Command succeeds
580 2. Output is returned
581 """
582 inst = setup_tls.standalone
583 cmd = [
584 'dsconf',
585 inst.serverid,
586 'security',
587 'ca-certificate',
588 'list'
589 ]
590
591 returncode, stdout, stderr = run_cmd(cmd)
592 assert returncode == 0
593
594
595 def test_dsctl_tls_show_cert(setup_tls):
596 """Test dsctl tls show-certs command via subprocess
597
598 :id: b1b2b3b4-0002-0002-0002-000000000001
599 :setup: Standalone Instance with TLS
600 :steps:
601 1. Run dsctl tls show-certs
602 2. Verify output shows certificates
603 :expectedresults:
604 1. Command succeeds
605 2. Output contains certificate information
606 """
607 inst = setup_tls.standalone
608 cmd = [
609 'dsctl',
610 inst.serverid,
611 'tls',
612 'show-cert',
613 'Server-Cert'
614 ]
615
616 returncode, stdout, stderr = run_cmd(cmd)
617 assert returncode == 0
618
619
620 def test_dsctl_tls_list_client_ca(setup_tls):
621 """Test dsctl tls list-client-cas command via subprocess
622
623 :id: b1b2b3b4-0002-0002-0002-000000000003
624 :setup: Standalone Instance with TLS
625 :steps:
626 1. Run dsctl tls list-client-cas
627 2. Verify command executes
628 :expectedresults:
629 1. Command succeeds
630 2. Output is returned
631 """
632 inst = setup_tls.standalone
633 cmd = [
634 'dsctl',
635 inst.serverid,
636 'tls',
637 'list-client-ca'
638 ]
639
640 returncode, stdout, stderr = run_cmd(cmd)
641 assert returncode == 0
642 assert 'Self-Signed-CA' in stdout
643
644
645 def test_dsctl_tls_export_cert(setup_tls):
646 """Test dsctl tls export-cert command via subprocess
647
648 :id: b1b2b3b4-0002-0002-0002-000000000004
649 :setup: Standalone Instance with TLS
650 :steps:
651 1. Export Server-Cert to PEM file
652 2. Verify file is created
653 3. Verify file contains certificate
654 :expectedresults:
655 1. Command succeeds
656 2. File exists
657 3. File contains PEM certificate
658 """
659 inst = setup_tls.standalone
660
661 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.pem') as export_file:
662 export_path = export_file.name
663
664 try:
665 cmd = [
666 'dsctl',
667 inst.serverid,
668 'tls',
669 'export-cert',
670 '--output-file',
671 export_path,
672 'Server-Cert',
673 ]
674
675 returncode, stdout, stderr = run_cmd(cmd)
676 assert returncode == 0
677
678 # Verify file exists and has content
679 assert os.path.exists(export_path)
680 assert os.path.getsize(export_path) > 0
681
682 # Verify it's a PEM certificate
683 with open(export_path, 'r') as f:
684 content = f.read()
685 assert '-----BEGIN CERTIFICATE-----' in content
686
687 finally:
688 if os.path.exists(export_path):
689 os.unlink(export_path)
690
691
692 def test_dsctl_tls_generate_server_cert_csr(topo):
693 """Test dsctl tls generate-server-cert-csr via subprocess
694
695 :id: b1b2b3b4-0002-0002-0002-000000000005
696 :setup: Standalone Instance
697 :steps:
698 1. Generate a CSR with subject
699 2. Verify dsctl does not crash
700 :expectedresults:
701 1. Command succeeds
702 2. Command return code is 0 or 1
703 """
704 inst = topo.standalone
705 cmd = [
706 'dsctl',
707 inst.serverid,
708 'tls',
709 'generate-server-cert-csr',
710 '-s', 'CN=test.example.com,O=Example,C=US'
711 ]
712
713 returncode, stdout, stderr = run_cmd(cmd, check=False)
714 # Command may succeed or fail depending on system configuration
715 # Just verify it doesn't crash
716 assert returncode in [0, 1] # 0 = success, 1 = expected error
717
718
719 def test_dsctl_security_options(topo):
720 """Test dsctl with various security-related options via subprocess
721
722 :id: b1b2b3b4-0002-0002-0002-000000000006
723 :setup: Standalone Instance
724 :steps:
725 1. Run dsctl status command
726 2. Verify command works
727 :expectedresults:
728 1. Command succeeds
729 2. Output shows instance status
730 """
731 inst = topo.standalone
732 cmd = [
733 'dsctl',
734 inst.serverid,
735 'status'
736 ]
737
738 returncode, stdout, stderr = run_cmd(cmd)
739 assert returncode == 0
740 assert 'Instance' in stdout or inst.serverid in stdout
741
742
743 def test_dsconf_security_disable_plaintext_port(topo):
744 """Test dsconf security disable_plain_port via subprocess
745
746 :id: a1a2a3a4-0001-0001-0001-000000000013
747 :setup: Standalone Instance
748 :steps:
749 1. Disable plaintext port
750 2. Verify port is set to 0
751 3. Restore original port
752 :expectedresults:
753 1. Command succeeds
754 2. Port is disabled
755 3. Restore succeeds
756 """
757 inst = topo.standalone
758 dsconf_base = [
759 'dsconf',
760 inst.serverid
761 ]
762
763 # Get original port
764 original_port = inst.port
765
766 try:
767 # Disable plaintext port
768 cmd = dsconf_base + ['security', 'disable_plain_port']
769 returncode, stdout, stderr = run_cmd(cmd, stdin_input='Yes I am sure\n')
770 assert returncode == 0
771
772 # Verify port is 0
773 assert inst.config.get_attr_val_utf8('nsslapd-port') == "0"
774
775 finally:
776 # Restore original port
777 inst.config.set('nsslapd-port', str(original_port))
778
779
780 def test_dsconf_security_certificate_set_trust_flags(setup_tls):
781 """Test dsconf security certificate set-trust-flags via subprocess
782
783 :id: a1a2a3a4-0001-0001-0001-000000000014
784 :setup: Standalone Instance with TLS
785 :steps:
786 1. Set trust flags on Self-Signed-CA
787 2. Verify command succeeds
788 :expectedresults:
789 1. Command succeeds
790 2. No errors returned
791 """
792 inst = setup_tls.standalone
793 cmd = [
794 'dsconf',
795 inst.serverid,
796 'security',
797 'certificate',
798 'set-trust-flags',
799 'Self-Signed-CA',
800 '--flags',
801 'CT,,'
802 ]
803
804 returncode, stdout, stderr = run_cmd(cmd)
805 assert returncode == 0
806
807
808 def test_dsconf_help_security(topo):
809 """Test dsconf security help commands via subprocess
810
811 :id: a1a2a3a4-0001-0001-0001-000000000015
812 :setup: Standalone Instance
813 :steps:
814 1. Run dsconf security --help
815 2. Run dsconf security rsa --help
816 3. Run dsconf security ciphers --help
817 4. Run dsconf security certificate --help
818 :expectedresults:
819 1. Help text is displayed
820 2. RSA help is displayed
821 3. Ciphers help is displayed
822 4. Certificate help is displayed
823 """
824 inst = topo.standalone
825 dsconf_base = [
826 'dsconf',
827 inst.serverid,
828 'security'
829 ]
830
831 # Security help
832 cmd = dsconf_base + ['--help']
833 returncode, stdout, stderr = run_cmd(cmd)
834 assert returncode == 0
835 assert 'usage:' in stdout.lower() or 'security' in stdout.lower()
836
837 # RSA help
838 cmd = dsconf_base + ['rsa', '--help']
839 returncode, stdout, stderr = run_cmd(cmd)
840 assert returncode == 0
841
842 # Ciphers help
843 cmd = dsconf_base + ['ciphers', '--help']
844 returncode, stdout, stderr = run_cmd(cmd)
845 assert returncode == 0
846
847 # Certificate help
848 cmd = dsconf_base + ['certificate', '--help']
849 returncode, stdout, stderr = run_cmd(cmd)
850 assert returncode == 0
851
852
853 def test_dsctl_help_tls(topo):
854 """Test dsctl tls help command via subprocess
855
856 :id: b1b2b3b4-0002-0002-0002-000000000007
857 :setup: Standalone Instance
858 :steps:
859 1. Run dsctl tls --help
860 :expectedresults:
861 1. Help text is displayed
862 """
863 inst = topo.standalone
864 cmd = [
865 'dsctl',
866 inst.serverid,
867 'tls',
868 '--help'
869 ]
870
871 returncode, stdout, stderr = run_cmd(cmd)
872 assert returncode == 0
873 assert 'usage:' in stdout.lower() or 'tls' in stdout.lower()
874
875
876 def test_dsconf_certificate_invalid_files(setup_tls):
877 """Test dsconf with invalid/malformed certificate files via subprocess
878
879 :id: a1a2a3a4-0001-0001-0001-000000000016
880 :setup: Standalone Instance with TLS
881 :steps:
882 1. Try to import corrupted PEM certificate
883 2. Try to import truncated certificate
884 3. Try to import invalid PKCS#12 file
885 4. Verify all commands fail with appropriate errors
886 :expectedresults:
887 1. Corrupted PEM import fails
888 2. Truncated cert import fails
889 3. Invalid PKCS#12 import fails
890 4. Error messages are meaningful (not just error codes)
891 """
892 inst = setup_tls.standalone
893 dsconf_base = [
894 'dsconf',
895 inst.serverid,
896 'security',
897 'certificate'
898 ]
899
900 # Test 1: Corrupted PEM certificate
901 corrupted_pem = """-----BEGIN CERTIFICATE-----
902 CORRUPTED DATA HERE NOT VALID BASE64!@#$%^&*
903 THIS IS NOT A REAL CERTIFICATE
904 -----END CERTIFICATE-----"""
905
906 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.pem') as f:
907 f.write(corrupted_pem)
908 corrupted_pem_path = f.name
909
910 try:
911 cmd = dsconf_base + ['add', '--file', corrupted_pem_path, '--name', 'CORRUPTED_CERT']
912 returncode, stdout, stderr = run_cmd(cmd, check=False)
913 assert returncode != 0
914 # Should have error text, not just error code
915 assert '255' not in stderr or 'base64' in stderr.lower() or 'invalid' in stderr.lower()
916 finally:
917 if os.path.exists(corrupted_pem_path):
918 os.unlink(corrupted_pem_path)
919
920 # Test 2: Truncated certificate (incomplete PEM)
921 truncated_pem = """-----BEGIN CERTIFICATE-----
922 MIIFZjCCA06gAwIBAgIFAL18JOowDQYJKoZIhvcNAQELBQAwZTELMAkGA1UEBhMC
923 QVUxEzARBgNVBAgTClF1ZWVuc2xhbmQxDjAMBgNVBAcTBTM4OWRzMRAwDgYDVQQK
924 """ # Intentionally truncated, no END CERTIFICATE
925
926 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.pem') as f:
927 f.write(truncated_pem)
928 truncated_pem_path = f.name
929
930 try:
931 cmd = dsconf_base + ['add', '--file', truncated_pem_path, '--name', 'TRUNCATED_CERT']
932 returncode, stdout, stderr = run_cmd(cmd, check=False)
933 assert returncode != 0
934 finally:
935 if os.path.exists(truncated_pem_path):
936 os.unlink(truncated_pem_path)
937
938 # Test 3: Invalid PKCS#12 file (just random data)
939 with tempfile.NamedTemporaryFile(mode='wb', delete=False, suffix='.p12') as f:
940 f.write(b'NOT A VALID PKCS12 FILE')
941 invalid_p12_path = f.name
942
943 try:
944 cmd = dsconf_base + ['add', '--file', invalid_p12_path, '--name', 'INVALID_P12']
945 returncode, stdout, stderr = run_cmd(cmd, check=False)
946 assert returncode != 0
947 finally:
948 if os.path.exists(invalid_p12_path):
949 os.unlink(invalid_p12_path)
950
951
952 def test_dsconf_certificate_overwrite_scenarios(setup_tls):
953 """Test certificate overwrite with and without --force flag via subprocess
954
955 :id: a1a2a3a4-0001-0001-0001-000000000017
956 :setup: Standalone Instance with TLS
957 :steps:
958 1. Add a test certificate
959 2. Try to add same certificate without --force (should fail)
960 3. Add same certificate with --force (should succeed)
961 4. Clean up
962 :expectedresults:
963 1. Initial add succeeds
964 2. Duplicate add without --force fails
965 3. Duplicate add with --force succeeds
966 4. Cleanup succeeds
967 """
968 inst = setup_tls.standalone
969 dsconf_base = [
970 'dsconf',
971 inst.serverid,
972 'security',
973 'certificate'
974 ]
975
976 # Valid test certificate
977 pem_content = """-----BEGIN CERTIFICATE-----
978 MIIFZjCCA06gAwIBAgIFAL18JOowDQYJKoZIhvcNAQELBQAwZTELMAkGA1UEBhMC
979 QVUxEzARBgNVBAgTClF1ZWVuc2xhbmQxDjAMBgNVBAcTBTM4OWRzMRAwDgYDVQQK
980 Ewd0ZXN0aW5nMR8wHQYDVQQDExZzc2NhLjM4OWRzLmV4YW1wbGUuY29tMB4XDTIy
981 MTAyNTE5NDU0M1oXDTI0MTAyNTE5NDU0M1owZTELMAkGA1UEBhMCQVUxEzARBgNV
982 BAgTClF1ZWVuc2xhbmQxDjAMBgNVBAcTBTM4OWRzMRAwDgYDVQQKEwd0ZXN0aW5n
983 MR8wHQYDVQQDExZzc2NhLjM4OWRzLmV4YW1wbGUuY29tMIICIjANBgkqhkiG9w0B
984 AQEFAAOCAg8AMIICCgKCAgEA5E+pd7+8lBsbTKdjHgkSLi2Z5T5G9T+3wziDHhsz
985 F0nG+IOu5yYVkoj/bMxR3sNNlbDLk5ATyNAfytW3cAUZ3NLqm6bmEZdUjD6YycVk
986 AvrfY3zVVE9Debfw6JI3ml8JlC3t8dqn2KT7dmSjvr9zPS95HU+RepjzAqJAKY3B
987 27v0cMetUnxG4pqc7zqnSZJXVP/OXMKSNpujHnK8HyjT8tUJIYQ0YvU2JPJpz3fC
988 BJrmzgO2xYLgLPu6abhP6PQ6uUU+d4j36lG4J/4OiMY0Lr+mnaBAaD3ULPtN5eZh
989 fjQ9d+Sh89xHz92icWhkn8c7IHNEZNtMHNTNJiNbWKuU9HpBWNjWHJoxSxXn4Emr
990 DSfG+lq2UU2m9m+XrDK/7t0W/zC3S+zwcyqM8SJAiZnGEi85058wB0BB1HnnAfFX
991 gel3uZFhnR4d86O/vO5VUqg5Ko795DPzPa3SU4rR36U3nUF7g5WhEAmYNCj683D3
992 DJDPJeCZmis7xtYB5K6Wu6SnFDxBEfhcWSsamWM286KntOiUtqQEzDy4OpZEUsgq
993 s7uqQSl/dfGdY9hCpXMYhlvMfVv3aIoM5zPuXN2cE1QkTnE1pyo8gZqnPLFZnwc9
994 FT+Wjpy0EmsAM/5AIed5h+JgJ304P+wkyjf7APUZyUwf4UJN6aro6N8W23F7dAu5
995 uJ0CAwEAAaMdMBswDAYDVR0TBAUwAwEB/zALBgNVHQ8EBAMCAgQwDQYJKoZIhvcN
996 AQELBQADggIBADFlVdDp1gTF/gl5ysZoo4/4BBSpLx5JDeyPE6BityZ/lwHvBUq3
997 VzmIsU6kxAJfk0p9S7G4LgHIC/DVsLTIE5do6tUdyrawvcaanbYn9ScNoFVQ0GDS
998 C6Ir8PftEvc6bpI4hjkv4goK9fTq5Jtv4LSuRfxFEwoLO+WN8a65IFWlaHJl/Erp
999 9fzP+JKDo8zeh4qnMkaTSCBmLWa8kErrV462RU+qZktf/V4/gWg6k5Vp+82xNk7f
1000 9/Mrg9KshNux7A4YCd8LgLEeCgsigi4N6zcfjQB0Rh5u9kXu/hzOjh379ki/vqju
1001 i+MTVH97LMB47uR1LEl0VvhWSjID0ePUtbPHCJwOsxWyxBCJY6V7A9nj52uXMGuX
1002 xghssZTFvRK6Bb1OiPNYRGqmuymm8rcSFdsY5yemkxJ6kfn40JIRCmVFwqaqu7MC
1003 nxyaWAKpRHKM5IyeVZHkFzL9jR/2tVBbjfCAl6YSwM759VcOsw2SGMQKpGIPEBTa
1004 1NBdlG45aWJBx5jBdVfOskLjxmBjosByJJHRLtrUBvg66ZBsx1k0c9XjsKmC59JP
1005 AzI8zYp/TY/6T5igxM+CSx98DsJFccPBZFFJX+YYRL7DFN38Yb7jMgIUXYHS28Gc
1006 1c8kz7ylcQB8lKgCgpcBCH5ZSnLVAnH3uqCygxSTgTo+jgJklKc0xFuR
1007 -----END CERTIFICATE-----"""
1008
1009 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.pem') as f:
1010 f.write(pem_content)
1011 pem_path = f.name
1012
1013 cert_name = 'TEST_OVERWRITE_CERT'
1014
1015 try:
1016 # Initial add
1017 cmd = dsconf_base + ['add', '--file', pem_path, '--name', cert_name]
1018 returncode, stdout, stderr = run_cmd(cmd, check=False)
1019
1020 if returncode == 0:
1021 # Try to add again without --force (should fail)
1022 cmd = dsconf_base + ['add', '--file', pem_path, '--name', cert_name]
1023 returncode, stdout, stderr = run_cmd(cmd, check=False)
1024 # Should fail because cert already exists
1025 assert returncode != 0 or 'already exists' in stderr.lower()
1026
1027 # Try to add with --force flag (if supported)
1028 cmd = dsconf_base + ['add', '--file', pem_path, '--name', cert_name, '--force']
1029 returncode, stdout, stderr = run_cmd(cmd, check=False)
1030 # Command may succeed with force or may not support --force flag
1031
1032 finally:
1033 # Cleanup
1034 cmd = dsconf_base + ['del', cert_name]
1035 run_cmd(cmd, check=False)
1036 if os.path.exists(pem_path):
1037 os.unlink(pem_path)
1038
1039
1040 def test_dsconf_json_text_output_consistency(setup_tls):
1041 """Test JSON vs text output consistency for list/get commands via subprocess
1042
1043 :id: a1a2a3a4-0001-0001-0001-000000000018
1044 :setup: Standalone Instance with TLS
1045 :steps:
1046 1. Get security config in text and JSON format
1047 2. Compare that essential data is in both
1048 3. List certificates in text and JSON format
1049 4. Compare that certificates appear in both
1050 5. Get certificate details in text and JSON format
1051 6. Verify consistency
1052 :expectedresults:
1053 1. Both formats return data
1054 2. Essential security attributes appear in both
1055 3. Both formats return certificates
1056 4. Same certificates appear in both
1057 5. Both formats return details
1058 6. Data is consistent between formats
1059 """
1060 inst = setup_tls.standalone
1061
1062 # Test 1: Security get - text vs JSON
1063 cmd_text = ['dsconf', inst.serverid, 'security', 'get']
1064 returncode, stdout_text, stderr = run_cmd(cmd_text)
1065 assert returncode == 0
1066
1067 cmd_json = ['dsconf', inst.serverid, '--json', 'security', 'get']
1068 returncode, stdout_json, stderr = run_cmd(cmd_json)
1069 assert returncode == 0
1070 json_data = json.loads(stdout_json)
1071 assert 'type' in json_data or 'items' in json_data or 'attrs' in json_data
1072
1073 # Test 2: Certificate list - text vs JSON
1074 cmd_text = ['dsconf', inst.serverid, 'security', 'certificate', 'list']
1075 returncode, stdout_text, stderr = run_cmd(cmd_text)
1076 assert returncode == 0
1077
1078 cmd_json = ['dsconf', inst.serverid, '--json', 'security', 'certificate', 'list']
1079 returncode, stdout_json, stderr = run_cmd(cmd_json)
1080 assert returncode == 0
1081 json_data = json.loads(stdout_json)
1082 assert isinstance(json_data, (dict, list))
1083
1084 # Verify Server-Cert appears in both
1085 assert 'Server-Cert' in stdout_text
1086 json_str = json.dumps(json_data)
1087 assert 'Server-Cert' in json_str
1088
1089 # Test 3: Certificate get - text vs JSON
1090 cmd_text = ['dsconf', inst.serverid, 'security', 'certificate', 'get', 'Server-Cert']
1091 returncode, stdout_text, stderr = run_cmd(cmd_text)
1092 assert returncode == 0
1093
1094 cmd_json = ['dsconf', inst.serverid, '--json', 'security', 'certificate', 'get', 'Server-Cert']
1095 returncode, stdout_json, stderr = run_cmd(cmd_json, check=False)
1096 # JSON format may not be supported for this command, check if it succeeds
1097 if returncode == 0:
1098 json_data = json.loads(stdout_json)
1099 assert isinstance(json_data, dict)
1100
1101
1102 def test_dsctl_tls_error_messages(topo):
1103 """Test that dsctl tls commands return meaningful error messages via subprocess
1104
1105 :id: b1b2b3b4-0002-0002-0002-000000000008
1106 :setup: Standalone Instance
1107 :steps:
1108 1. Try to generate CSR with malformed subject
1109 2. Try to remove non-existent certificate
1110 3. Try to export non-existent certificate
1111 4. Verify error messages are meaningful
1112 :expectedresults:
1113 1. CSR generation fails with meaningful error
1114 2. Remove fails with meaningful error
1115 3. Export fails with meaningful error
1116 4. No bare error codes like '255' in output
1117 """
1118 inst = topo.standalone
1119
1120 # Test 1: Malformed subject in CSR generation
1121 cmd = [
1122 'dsctl',
1123 inst.serverid,
1124 'tls',
1125 'generate-server-cert-csr',
1126 '-s', 'INVALID_SUBJECT'
1127 ]
1128 returncode, stdout, stderr = run_cmd(cmd, check=False)
1129 assert returncode != 0
1130 # Should have error text, not just '255'
1131 combined_output = stdout + stderr
1132 if '255' in combined_output:
1133 assert 'improperly formatted' in combined_output.lower() or 'invalid' in combined_output.lower()
1134
1135 # Test 2: Remove non-existent certificate
1136 cmd = [
1137 'dsctl',
1138 inst.serverid,
1139 'tls',
1140 'remove-cert',
1141 'NONEXISTENT_CERT_12345'
1142 ]
1143 returncode, stdout, stderr = run_cmd(cmd, check=False)
1144 assert returncode != 0
1145 combined_output = stdout + stderr
1146 if '255' in combined_output:
1147 assert 'not found' in combined_output.lower() or 'could not find' in combined_output.lower()
1148
1149 # Test 3: Export non-existent certificate
1150 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.pem') as f:
1151 export_path = f.name
1152
1153 try:
1154 cmd = [
1155 'dsctl',
1156 inst.serverid,
1157 'tls',
1158 'export-cert',
1159 '--output-file',
1160 export_path,
1161 'NONEXISTENT_CERT_12345'
1162 ]
1163 returncode, stdout, stderr = run_cmd(cmd, check=False)
1164 assert returncode != 0
1165 combined_output = stdout + stderr
1166 if '255' in combined_output:
1167 assert 'not found' in combined_output.lower() or 'could not find' in combined_output.lower()
1168 finally:
1169 if os.path.exists(export_path):
1170 os.unlink(export_path)
1171
1172
1173 def test_dsconf_certificate_backend_consistency(setup_tls):
1174 """Test backend consistency between DynamicCerts and NssSsl via subprocess
1175
1176 :id: a1a2a3a4-0001-0001-0001-000000000019
1177 :setup: Standalone Instance with TLS
1178 :steps:
1179 1. List certificates using dsconf
1180 2. Verify Server-Cert appears (from NssSsl backend)
1181 3. Check if dynamic certificates backend is accessible
1182 4. Verify consistency of certificate data
1183 :expectedresults:
1184 1. Certificate list succeeds
1185 2. Server-Cert is present
1186 3. Backend is accessible
1187 4. Data is consistent
1188 """
1189 inst = setup_tls.standalone
1190
1191 # List certificates
1192 cmd = ['dsconf', inst.serverid, 'security', 'certificate', 'list']
1193 returncode, stdout, stderr = run_cmd(cmd)
1194 assert returncode == 0
1195 assert 'Server-Cert' in stdout
1196
1197 # Get certificate details
1198 cmd = ['dsconf', inst.serverid, 'security', 'certificate', 'get', 'Server-Cert']
1199 returncode, stdout, stderr = run_cmd(cmd)
1200 assert returncode == 0
1201
1202 # Verify we get meaningful certificate information
1203 assert 'Server-Cert' in stdout or 'Certificate' in stdout
1204
1205 # List CA certificates
1206 cmd = ['dsconf', inst.serverid, 'security', 'ca-certificate', 'list']
1207 returncode, stdout, stderr = run_cmd(cmd)
1208 assert returncode == 0
1209
1210 # The output should be consistent whether accessed via NssSsl or DynamicCerts backend
1211
1212
1213 def test_dsctl_tls_pkcs12_password_scenarios(setup_tls):
1214 """Test PKCS#12 import with various password scenarios via subprocess
1215
1216 :id: b1b2b3b4-0002-0002-0002-000000000009
1217 :setup: Standalone Instance with TLS
1218 :steps:
1219 1. Try to import PKCS#12 with wrong password
1220 2. Try to import PKCS#12 with missing password
1221 3. Try to import PKCS#12 with empty password when one is required
1222 4. Verify appropriate error messages
1223 :expectedresults:
1224 1. Wrong password fails with meaningful error
1225 2. Missing password fails with meaningful error
1226 3. Empty password fails with meaningful error
1227 4. Error messages indicate password issues, not bare error codes
1228 """
1229 inst = setup_tls.standalone
1230
1231 # Create a dummy PKCS#12-like file (not a real one, just for testing error handling)
1232 with tempfile.NamedTemporaryFile(mode='wb', delete=False, suffix='.p12') as f:
1233 # Write some bytes that look like PKCS#12 header but aren't valid
1234 f.write(b'\x30\x82') # ASN.1 SEQUENCE tag
1235 f.write(b'NOT_A_VALID_PKCS12_FILE')
1236 p12_path = f.name
1237
1238 try:
1239 # Test 1: Try import with wrong password
1240 cmd = [
1241 'dsctl',
1242 inst.serverid,
1243 'tls',
1244 'import-server-key-cert',
1245 p12_path,
1246 p12_path # Using same file as both key and cert for testing
1247 ]
1248 returncode, stdout, stderr = run_cmd(cmd, check=False)
1249 assert returncode != 0
1250 combined_output = stdout + stderr
1251 # Should get error about private key or file format, not bare '255'
1252 if '255' in combined_output:
1253 assert ('private key' in combined_output.lower() or
1254 'password' in combined_output.lower() or
1255 'pkcs' in combined_output.lower() or
1256 'invalid' in combined_output.lower())
1257
1258 finally:
1259 if os.path.exists(p12_path):
1260 os.unlink(p12_path)
1261
1262
1263 def test_dsconf_dynamic_certificates_attributes(setup_tls):
1264 """Test dynamic certificates attributes and timestamp handling via subprocess
1265
1266 :id: a1a2a3a4-0001-0001-0001-000000000020
1267 :setup: Standalone Instance with TLS
1268 :steps:
1269 1. List certificates in JSON format
1270 2. Check for dynamic certificate attributes
1271 3. Verify timestamp format if present
1272 4. Check certificate metadata (subject, issuer, etc.)
1273 :expectedresults:
1274 1. Certificate list returns valid JSON
1275 2. Expected attributes are present
1276 3. Timestamps are properly formatted
1277 4. Metadata is accessible and valid
1278 """
1279 inst = setup_tls.standalone
1280
1281 # Get certificate list in JSON format
1282 cmd = ['dsconf', inst.serverid, '--json', 'security', 'certificate', 'list']
1283 returncode, stdout, stderr = run_cmd(cmd)
1284 assert returncode == 0
1285
1286 json_data = json.loads(stdout)
1287 assert isinstance(json_data, (dict, list))
1288
1289 # Get details of a specific certificate in JSON if supported
1290 cmd = ['dsconf', inst.serverid, '--json', 'security', 'certificate', 'get', 'Server-Cert']
1291 returncode, stdout, stderr = run_cmd(cmd, check=False)
1292
1293 if returncode == 0:
1294 json_data = json.loads(stdout)
1295
1296 # Check for common certificate attributes
1297 # The exact structure depends on whether DynamicCerts or NssSsl backend is used
1298 json_str = json.dumps(json_data).lower()
1299
1300 # Look for certificate-related attributes (case-insensitive)
1301 # These might be in different formats depending on the backend
1302 assert ('subject' in json_str or
1303 'issuer' in json_str or
1304 'cert' in json_str or
1305 'nickname' in json_str)
1306
1307
1308 def test_dsconf_security_multiple_operations(setup_tls):
1309 """Test multiple security operations in sequence via subprocess
1310
1311 :id: a1a2a3a4-0001-0001-0001-000000000021
1312 :setup: Standalone Instance with TLS
1313 :steps:
1314 1. Disable security
1315 2. Enable security
1316 3. Disable RSA
1317 4. Enable RSA
1318 5. Get security config after each change
1319 6. Verify state changes are reflected
1320 :expectedresults:
1321 1. Disable succeeds
1322 2. Enable succeeds
1323 3. RSA disable succeeds
1324 4. RSA enable succeeds
1325 5. Each get returns current state
1326 6. Changes are properly reflected
1327 """
1328 inst = setup_tls.standalone
1329 dsconf_base = ['dsconf', inst.serverid, 'security']
1330
1331 # Enable security
1332 cmd = dsconf_base + ['enable']
1333 returncode, stdout, stderr = run_cmd(cmd)
1334 assert returncode == 0
1335
1336 # Verify enabled
1337 cmd = dsconf_base + ['get']
1338 returncode, stdout, stderr = run_cmd(cmd)
1339 assert 'nsslapd-security: on' in stdout
1340
1341 # Disable security
1342 cmd = dsconf_base + ['disable']
1343 returncode, stdout, stderr = run_cmd(cmd)
1344 assert returncode == 0
1345
1346 # Verify disabled
1347 cmd = dsconf_base + ['get']
1348 returncode, stdout, stderr = run_cmd(cmd)
1349 assert 'nsslapd-security: off' in stdout
1350
1351 # Re-enable for RSA tests
1352 cmd = dsconf_base + ['enable']
1353 returncode, stdout, stderr = run_cmd(cmd)
1354 assert returncode == 0
1355
1356 # Enable RSA
1357 cmd = dsconf_base + ['rsa', 'enable']
1358 returncode, stdout, stderr = run_cmd(cmd)
1359 assert returncode == 0
1360
1361 # Verify RSA is on
1362 cmd = dsconf_base + ['rsa', 'get']
1363 returncode, stdout, stderr = run_cmd(cmd)
1364 assert 'on' in stdout.lower()
1365
1366 # Disable RSA
1367 cmd = dsconf_base + ['rsa', 'disable']
1368 returncode, stdout, stderr = run_cmd(cmd)
1369 assert returncode == 0
1370
1371 # Verify RSA is off
1372 cmd = dsconf_base + ['rsa', 'get']
1373 returncode, stdout, stderr = run_cmd(cmd)
1374 assert 'off' in stdout.lower()
1375
1376
1377 def test_dsconf_security_encryption_module_create(setup_tls):
1378 """Test dsconf security encryption-module add via subprocess
1379
1380 :id: 75d93730-8212-4a67-b6ee-f7f57436a311
1381 :setup: Standalone Instance with TLS
1382 :steps:
1383 1. Add a new encryption module with required and optional arguments
1384 2. Verify module exists and is activated
1385 :expectedresults:
1386 1. Add command succeeds
1387 2. Module details can be fetched and contain expected values
1388 """
1389 inst = setup_tls.standalone
1390 module_name = f'CLI_ENC_MODULE'
1391
1392 add_cmd = [
1393 'dsconf', inst.serverid, 'security', 'encryption-module', 'add', module_name,
1394 '--cert-nickname', 'Second-Cert',
1395 '--activated',
1396 '--token', 'internal (software)',
1397 '--server-key-extract-file', f'/tmp/{module_name}.key',
1398 '--server-cert-extract-file', f'/tmp/{module_name}.crt',
1399 ]
1400
1401 try:
1402 returncode, stdout, stderr = run_cmd(add_cmd)
1403 assert returncode == 0
1404
1405 get_cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'get', module_name]
1406 returncode, stdout, stderr = run_cmd(get_cmd)
1407 assert returncode == 0
1408 assert module_name in stdout
1409 assert 'nssslactivation: on' in stdout.lower()
1410 assert 'serverkeyextractfile: /tmp/cli_enc_module.key' in stdout.lower()
1411 assert 'servercertextractfile: /tmp/cli_enc_module.crt' in stdout.lower()
1412 assert 'nssslpersonalityssl: second-cert' in stdout.lower()
1413 assert 'nsssltoken: internal (software)' in stdout.lower()
1414 finally:
1415 cleanup_cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'delete', module_name]
1416 run_cmd(cleanup_cmd, check=False)
1417
1418
1419 def test_dsconf_security_encryption_module_edit(setup_tls):
1420 """Test dsconf security encryption-module edit via subprocess
1421
1422 :id: d6b7169d-23ee-489e-a90c-872ef35f73db
1423 :setup: Standalone Instance with TLS
1424 :steps:
1425 1. Add a new encryption module
1426 2. Edit the module activation/token/extract file options
1427 3. Verify updated values
1428 :expectedresults:
1429 1. Add succeeds
1430 2. Edit succeeds
1431 3. Updated values are visible in module output
1432 """
1433 inst = setup_tls.standalone
1434 module_name = 'CLI_ENC_MODULE'
1435 token_value = 'Internal (Software) Token'
1436
1437 add_cmd = [
1438 'dsconf', inst.serverid, 'security', 'encryption-module', 'add', module_name,
1439 '--cert-nickname', 'Second-Cert',
1440 '--activated',
1441 ]
1442 edit_cmd = [
1443 'dsconf', inst.serverid, 'security', 'encryption-module', 'edit', module_name,
1444 '--cert-nickname', 'Second-Cert2',
1445 '--deactivate',
1446 '--token', token_value,
1447 '--server-key-extract-file', f'/tmp/{module_name}.edited.key',
1448 '--server-cert-extract-file', f'/tmp/{module_name}.edited.crt',
1449 ]
1450
1451 try:
1452 returncode, stdout, stderr = run_cmd(add_cmd)
1453 assert returncode == 0
1454
1455 returncode, stdout, stderr = run_cmd(edit_cmd)
1456 assert returncode == 0
1457
1458 get_cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'get', module_name]
1459 returncode, stdout, stderr = run_cmd(get_cmd)
1460 assert returncode == 0
1461 assert 'nssslactivation: off' in stdout.lower()
1462 assert 'serverkeyextractfile: /tmp/cli_enc_module.edited.key' in stdout.lower()
1463 assert 'servercertextractfile: /tmp/cli_enc_module.edited.crt' in stdout.lower()
1464 assert 'nssslpersonalityssl: second-cert2' in stdout.lower()
1465 assert 'nsssltoken: internal (software) token' in stdout.lower()
1466 finally:
1467 cleanup_cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'delete', module_name]
1468 run_cmd(cleanup_cmd, check=False)
1469
1470
1471 def test_dsconf_security_encryption_module_delete(setup_tls):
1472 """Test dsconf security encryption-module delete via subprocess
1473
1474 :id: be36928b-09b7-4c02-8431-4d3440ea979f
1475 :setup: Standalone Instance with TLS
1476 :steps:
1477 1. Add a new encryption module
1478 2. Delete the module
1479 3. Verify module no longer appears in list
1480 :expectedresults:
1481 1. Add succeeds
1482 2. Delete succeeds
1483 3. Module is not listed anymore
1484 """
1485 inst = setup_tls.standalone
1486 module_name = f'CLI_ENC_MODULE'
1487
1488 add_cmd = [
1489 'dsconf', inst.serverid, 'security', 'encryption-module', 'add', module_name,
1490 '--cert-nickname', 'Second-Cert',
1491 ]
1492 del_cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'delete', module_name]
1493 list_cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'list', '--just-names']
1494
1495 run_cmd(add_cmd)
1496 returncode, stdout, stderr = run_cmd(del_cmd)
1497 assert returncode == 0
1498
1499 returncode, stdout, stderr = run_cmd(list_cmd)
1500 assert returncode == 0
1501 assert module_name not in stdout
1502
1503
1504 def test_dsconf_security_encryption_module_list_all_args(setup_tls):
1505 """Test dsconf security encryption-module list with all list arguments via subprocess
1506
1507 :id: 7aa7ac1a-bf95-4902-a650-258be832817f
1508 :setup: Standalone Instance with TLS
1509 :steps:
1510 1. Create a temporary certificate file and import it as Second-Cert
1511 2. Add a temporary encryption module that references Second-Cert
1512 3. Run encryption-module list with no args
1513 4. Run encryption-module list with --just-names
1514 5. Run JSON output list
1515 6. Run JSON output list with --just-names
1516 :expectedresults:
1517 1. Certificate import succeeds (or already exists)
1518 2. Module add succeeds
1519 3. Default list succeeds and contains module details
1520 4. --just-names succeeds and contains the module name
1521 5. JSON list succeeds and is valid JSON
1522 6. JSON + --just-names succeeds and includes module name
1523 """
1524 inst = setup_tls.standalone
1525 module_name = f'CLI_ENC_MODULE'
1526
1527 try:
1528 run_cmd([
1529 'dsconf', inst.serverid, 'security', 'encryption-module', 'add', module_name,
1530 '--cert-nickname', 'Second-Cert',
1531 '--activated'
1532 ])
1533
1534 cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'list']
1535 returncode, stdout, stderr = run_cmd(cmd)
1536 assert returncode == 0
1537 assert module_name in stdout
1538
1539 cmd = ['dsconf', inst.serverid, 'security', 'encryption-module', 'list', '--just-names']
1540 returncode, stdout, stderr = run_cmd(cmd)
1541 assert returncode == 0
1542 assert module_name in stdout
1543
1544 cmd = ['dsconf', inst.serverid, '--json', 'security', 'encryption-module', 'list']
1545 returncode, stdout, stderr = run_cmd(cmd)
1546 assert returncode == 0
1547 data = json.loads(stdout)
1548 assert isinstance(data, dict)
1549 assert 'items' in data
1550
1551 cmd = ['dsconf', inst.serverid, '--json', 'security', 'encryption-module', 'list', '--just-names']
1552 returncode, stdout, stderr = run_cmd(cmd)
1553 assert returncode == 0
1554 data = json.loads(stdout)
1555 assert isinstance(data, dict)
1556 assert module_name in json.dumps(data)
1557 finally:
1558 run_cmd(['dsconf', inst.serverid, 'security', 'encryption-module', 'delete', module_name], check=False)
1559
1560
1561 def test_dsconf_security_certificate_import_second_cert(setup_tls):
1562 """Test dsconf security certificate add/get with Second-Cert via subprocess
1563
1564 :id: b18f0744-d1ce-41b4-98a0-254e9c290285
1565 :setup: Standalone Instance with TLS
1566 :steps:
1567 1. Create a temp PEM certificate file
1568 2. Import the certificate using dsconf with name Second-Cert
1569 3. Verify it can be retrieved with dsconf security certificate get
1570 :expectedresults:
1571 1. Temp certificate file is created
1572 2. Import succeeds
1573 3. Certificate details are returned for Second-Cert
1574 """
1575 inst = setup_tls.standalone
1576 cert_path = _create_temp_cert_file()
1577 cert_name = 'Second-Cert'
1578 try:
1579 add_cmd = [
1580 'dsconf', inst.serverid, 'security', 'certificate', 'add',
1581 '--file', cert_path,
1582 '--name', cert_name,
1583 '--do-it'
1584 ]
1585 returncode, stdout, stderr = run_cmd(add_cmd, check=False)
1586 # If cert already exists in this environment, continue with validation
1587 assert returncode in [0, 1]
1588
1589 get_cmd = ['dsconf', inst.serverid, 'security', 'certificate', 'get', cert_name]
1590 returncode, stdout, stderr = run_cmd(get_cmd)
1591 assert returncode == 0
1592 assert 'Certificate Name: Second-Cert' in stdout
1593 finally:
1594 run_cmd(['dsconf', inst.serverid, 'security', 'certificate', 'del', cert_name], check=False)
1595 if os.path.exists(cert_path):
1596 os.unlink(cert_path)
1597
1598 def test_dsctl_tls_import_cert_with_unique_nickname(setup_tls):
1599 """Test dsctl tls import-server-cert with fixed nickname via subprocess
1600
1601 :id: 281dfc03-42c6-4c65-a313-a677c0691bcf
1602 :setup: Standalone Instance with TLS
1603 :steps:
1604 1. Export Second-Cert to a temp PEM file
1605 2. Import the PEM as a server certificate with nickname Second-Cert
1606 3. Verify show-cert returns details for Second-Cert
1607 :expectedresults:
1608 1. Export succeeds
1609 2. Import succeeds
1610 3. show-cert succeeds for Second-Cert
1611 """
1612 inst = setup_tls.standalone
1613 unique_nick = 'Second-Cert'
1614
1615 cert_path = _create_temp_cert_file()
1616
1617 try:
1618 import_cmd = ['dsctl', inst.serverid, 'tls', 'import-server-cert', cert_path, '--nickname', unique_nick]
1619 returncode, stdout, stderr = run_cmd(import_cmd)
1620 assert returncode == 0
1621
1622 show_cmd = ['dsctl', inst.serverid, 'tls', 'show-cert', unique_nick]
1623 returncode, stdout, stderr = run_cmd(show_cmd)
1624 assert returncode == 0
1625 assert len(stdout.strip()) > 0
1626 finally:
1627 run_cmd(['dsctl', inst.serverid, 'tls', 'remove-cert', unique_nick], check=False)
1628 if os.path.exists(cert_path):
1629 os.unlink(cert_path)
1630
1631
1632 def test_dsctl_tls_generate_csr_with_unique_nickname(setup_tls):
1633 """Test dsctl tls generate-server-cert-csr with unique nickname via subprocess
1634
1635 :id: 37900e94-56a9-446a-9de7-c02dc30e03e5
1636 :setup: Standalone Instance with TLS
1637 :steps:
1638 1. Generate CSR using --nickname and a valid subject
1639 2. Verify expected CSR file path exists
1640 3. Remove generated CSR file
1641 :expectedresults:
1642 1. Command succeeds
1643 2. CSR file is created
1644 3. Cleanup succeeds
1645 """
1646 inst = setup_tls.standalone
1647 unique_nick = 'Second-Cert'
1648 csr_path = os.path.join(inst.get_cert_dir(), f'{unique_nick}.csr')
1649
1650 cmd = [
1651 'dsctl',
1652 inst.serverid,
1653 'tls',
1654 'generate-server-cert-csr',
1655 '--nickname', unique_nick,
1656 '-s', f'CN={unique_nick},O=Example,C=US'
1657 ]
1658 try:
1659 returncode, stdout, stderr = run_cmd(cmd)
1660 assert returncode == 0
1661 assert os.path.exists(csr_path)
1662 with open(csr_path, 'r') as csr_file:
1663 content = csr_file.read()
1664 if unique_nick not in content:
1665 log.error(f"CSR file {csr_path} does not contain the nickname {unique_nick}")
1666 assert False
1667
1668 finally:
1669 if os.path.exists(csr_path):
1670 os.unlink(csr_path)
1671
1672
1673 if __name__ == '__main__':
1674 # Run isolated
1675 # -s for DEBUG mode
1676 CURRENT_FILE = os.path.realpath(__file__)
1677 pytest.main(["-s", "-v", CURRENT_FILE])