Debian Codegraph @

dirsrvtests/tests/suites/setup_ds/dscreate_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2021 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 import sys
9 import pytest
10 import subprocess
11 import logging
12 import grp
13 import pwd
14 import re
15 from tempfile import TemporaryDirectory
16 from lib389 import DirSrv
17 from lib389.cli_base import LogCapture
18 from lib389.instance.setup import SetupDs
19 from lib389.instance.remove import remove_ds_instance
20 from lib389.instance.options import General2Base, Slapd2Base
21 from lib389._constants import *
22 from lib389.utils import ds_is_older, selinux_label_file, ensure_list_str, ensure_str
23 from shutil import rmtree
24
25 pytestmark = [pytest.mark.tier0,
26 pytest.mark.skipif(ds_is_older('1.4.1.2'), reason="Needs a compatible systemd unit, see PR#50213")]
27
28 DEBUGGING = os.getenv('DEBUGGING', False)
29 if DEBUGGING:
30 logging.getLogger(__name__).setLevel(logging.DEBUG)
31 else:
32 logging.getLogger(__name__).setLevel(logging.INFO)
33 log = logging.getLogger(__name__)
34
35 INSTANCE_PORT = 54321
36 INSTANCE_SECURE_PORT = 54322
37 INSTANCE_SERVERID = 'standalone'
38 #DEBUGGING = True
39
40 MAJOR, MINOR, _, _, _ = sys.version_info
41
42 CUSTOM_DIR = f'{os.getenv("PREFIX", "")}/var/lib/dirsrv_pytest_test_setup_ds_custom_db_dir'
43 CUSTOM_DB_DIR = f'{CUSTOM_DIR}/db'
44
45 class TopologyInstance(object):
46 def __init__(self, standalone):
47 # For these tests, we don't want to open the instance.
48 # instance.open()
49 self.standalone = standalone
50
51 # Need a teardown to destroy the instance.
52 @pytest.fixture
53 def topology(request):
54 instance = DirSrv(verbose=DEBUGGING)
55 instance.log.debug("Instance allocated")
56 args = {SER_PORT: INSTANCE_PORT,
57 SER_SERVERID_PROP: INSTANCE_SERVERID}
58 instance.allocate(args)
59 if instance.exists():
60 instance.delete()
61 # Cleanup custom dir
62 selinux_label_file(CUSTOM_DB_DIR, None)
63 rmtree(CUSTOM_DIR, ignore_errors=True)
64
65 def fin():
66 if not DEBUGGING:
67 if instance.exists():
68 instance.delete()
69 selinux_label_file(CUSTOM_DB_DIR, None)
70 rmtree(CUSTOM_DIR, ignore_errors=True)
71 request.addfinalizer(fin)
72
73 return TopologyInstance(instance)
74
75
76 def run_cmd(cmd):
77 result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
78 args = ' '.join(ensure_list_str(result.args))
79 stdout = ensure_str(result.stdout)
80 stderr = ensure_str(result.stderr)
81 log.info(f"CMD: {args} returned {result.returncode} STDOUT: {stdout} STDERR: {stderr}")
82 return stdout
83
84
85 def test_setup_ds_minimal_dry(topology):
86 """Test minimal DS setup - dry run
87
88 :id: 82637910-e279-11ec-a785-3497f624ea11
89 :setup: standalone instance
90 :steps:
91 1. Create the setupDS
92 2. Give it the right types
93 3. Get the dicts from Type2Base, as though they were from _validate_ds_2_config
94 4. Override instance name, root password, port and secure port
95 5. Assert we did not change the system
96 :expectedresults:
97 1. Success
98 2. Success
99 3. Success
100 4. Success
101 5. Success
102 """
103 # Unset PYTHONPATH to avoid mixing old CLI tools and new lib389
104 tmp_env = os.environ
105 if "PYTHONPATH" in tmp_env:
106 del tmp_env["PYTHONPATH"]
107
108 # Create the setupDs
109 lc = LogCapture()
110 # Give it the right types.
111 sds = SetupDs(verbose=DEBUGGING, dryrun=True, log=lc.log)
112
113 # Get the dicts from Type2Base, as though they were from _validate_ds_2_config
114 # IE get the defaults back just from Slapd2Base.collect
115 # Override instance name, root password, port and secure port.
116
117 general_options = General2Base(lc.log)
118 general_options.verify()
119 general = general_options.collect()
120
121 slapd_options = Slapd2Base(lc.log)
122 slapd_options.set('instance_name', INSTANCE_SERVERID)
123 slapd_options.set('port', INSTANCE_PORT)
124 slapd_options.set('secure_port', INSTANCE_SECURE_PORT)
125 slapd_options.set('root_password', PW_DM)
126 slapd_options.verify()
127 slapd = slapd_options.collect()
128
129 sds.create_from_args(general, slapd, {}, None)
130
131 insts = topology.standalone.list(serverid=INSTANCE_SERVERID)
132 # Assert we did not change the system.
133 assert(len(insts) == 0)
134
135 def test_setup_ds_minimal(topology):
136 """Test minimal DS setup
137
138 :id: 563c3ec4-e27b-11ec-970e-3497f624ea11
139 :setup: standalone instance
140 :steps:
141 1. Create the setupDS
142 2. Give it the right types
143 3. Get the dicts from Type2Base, as though they were from _validate_ds_2_config
144 4. Override instance name, root password, port and secure port
145 5. Assert we did change the system
146 6. Make sure we can connect
147 7. Make sure we can start stop.
148 8. Remove the instance
149 :expectedresults:
150 1. Success
151 2. Success
152 3. Success
153 4. Success
154 5. Success
155 6. Success
156 7. Success
157 8. Success
158 """
159 # Create the setupDs
160 lc = LogCapture()
161 # Give it the right types.
162 sds = SetupDs(verbose=DEBUGGING, dryrun=False, log=lc.log)
163
164 # Get the dicts from Type2Base, as though they were from _validate_ds_2_config
165 # IE get the defaults back just from Slapd2Base.collect
166 # Override instance name, root password, port and secure port.
167
168 general_options = General2Base(lc.log)
169 general_options.verify()
170 general = general_options.collect()
171
172 slapd_options = Slapd2Base(lc.log)
173 slapd_options.set('instance_name', INSTANCE_SERVERID)
174 slapd_options.set('port', INSTANCE_PORT)
175 slapd_options.set('secure_port', INSTANCE_SECURE_PORT)
176 slapd_options.set('root_password', PW_DM)
177 slapd_options.verify()
178 slapd = slapd_options.collect()
179
180 sds.create_from_args(general, slapd, {}, None)
181 insts = topology.standalone.list(serverid=INSTANCE_SERVERID)
182 # Assert we did change the system.
183 assert(len(insts) == 1)
184 # Make sure we can connect
185 topology.standalone.open()
186 # Make sure we can start stop.
187 topology.standalone.stop()
188 topology.standalone.start()
189 # Okay, actually remove the instance
190 remove_ds_instance(topology.standalone)
191
192
193 @pytest.mark.skipif(not os.path.exists('/usr/sbin/semanage'), reason="semanage is not installed. Please run dnf install policycoreutils-python-utils -y")
194 @pytest.mark.skipif(os.getuid()!=0, reason="pytest non run by root user")
195 def test_setup_ds_custom_db_dir(topology):
196 """Test DS setup using custom uid,gid and db_dir path
197
198 :id: 5a596887-cabb-4862-a91c-5eedafe222cd
199 :setup: standalone instance
200 :steps:
201 1. Create the user that will run ns-slapd
202 2. Create the setupDS
203 3. Give it the right types
204 4. Get the dicts from Type2Base, as though they were from _validate_ds_2_config
205 5. Override instance name, root password, port, secure port, user, group and dir_path
206 6. Assert we did change the system
207 7. Make sure we can connect
208 8. Make sure we can start stop.
209 9. Remove the instance
210 10. Check that there is not any dirsrv_* labels in in file local selinux customizations
211 11. Check that there is not any wldap_port_t labels in port local selinux customizations
212 :expectedresults:
213 1. Success
214 2. Success
215 3. Success
216 4. Success
217 5. Success
218 6. Success
219 7. Success
220 8. Success
221 9. Success
222 10. Success
223 11. Success
224 """
225 # Add linux user NON_ROOT_USER if it does not already exist
226 CUSTOM_USER='ldapsrv1'
227 try:
228 pwd_cu = pwd.getpwnam(CUSTOM_USER)
229 except KeyError:
230 subprocess.run(('/usr/sbin/useradd', CUSTOM_USER), check=True)
231 pwd_cu = pwd.getpwnam(CUSTOM_USER)
232 grp_cu = grp.getgrgid(pwd_cu.pw_gid)
233 log.info(f'Custom user: {pwd_cu} {grp_cu}')
234
235 # Create the setupDs
236 lc = LogCapture()
237 # Give it the right types.
238 sds = SetupDs(verbose=DEBUGGING, dryrun=False, log=lc.log)
239
240 # Get the dicts from Type2Base, as though they were from _validate_ds_2_config
241 # IE get the defaults back just from Slapd2Base.collect
242 # Override instance name, root password, port, secure port, user, group and db_dir.
243
244 general_options = General2Base(lc.log)
245 general_options.verify()
246 general = general_options.collect()
247
248 slapd_options = Slapd2Base(lc.log)
249 slapd_options.set('instance_name', INSTANCE_SERVERID)
250 slapd_options.set('port', INSTANCE_PORT)
251 slapd_options.set('secure_port', INSTANCE_SECURE_PORT)
252 slapd_options.set('root_password', PW_DM)
253 slapd_options.set('user', pwd_cu.pw_name)
254 slapd_options.set('group', grp_cu.gr_name)
255 slapd_options.set('db_dir', CUSTOM_DB_DIR)
256 slapd_options.verify()
257 slapd = slapd_options.collect()
258
259 sds.create_from_args(general, slapd, {}, None)
260 insts = topology.standalone.list(serverid=INSTANCE_SERVERID)
261 # Assert we did change the system.
262 assert(len(insts) == 1)
263 # Make sure we can connect
264 topology.standalone.open()
265 # Make sure we can start stop.
266 topology.standalone.stop()
267 topology.standalone.start()
268 # Okay, actually remove the instance
269 insts = topology.standalone.list(all=True)
270 remove_ds_instance(topology.standalone)
271 if (len(insts) == 1):
272 res = run_cmd(["semanage", "fcontext", "--list", "-C"])
273 assert not "dirsrv_" in res
274 res = run_cmd(["semanage", "port", "--list", "-C"])
275 assert not "ldap_port_t" in res
276
277
278 class UserEnv:
279 def __init__(self, user):
280 if os.geteuid() == 0:
281 try:
282 pw = pwd.getpwnam(user)
283 except KeyError:
284 subprocess.run(('/usr/sbin/useradd', user), check=True)
285 pw = pwd.getpwnam(user)
286 else:
287 pw = pwd.getpwuid(os.geteuid())
288 user = pw.pw_name
289 self.user = user
290 self.pw = pw
291 self.dir = None
292 self.runid = 1
293 self._instances = {}
294 self._dirs = []
295
296 def setdir(self, testname):
297 # Stop instance from current dir
298 if self.dir in self._instances:
299 lines = [ f'dsctl {i} stop' for i in self._instances[self.dir] ]
300 self.run(lines)
301 # Then create new dir
302 dir = f'{self.pw.pw_dir}/{testname}'
303 self.dir = dir
304 if os.path.isdir(dir):
305 rmtree(dir)
306 self._dirs.append(dir)
307 os.makedirs(dir)
308 if os.geteuid() == 0:
309 os.chown(dir, self.pw.pw_uid, self.pw.pw_gid)
310
311 def write_file(self, fname, is_runnable=False, lines=()):
312 log.debug(f'Creating file {fname} with:')
313 with open(fname, 'wt') as f:
314 for line in lines:
315 log.debug(line)
316 f.write(line+'\n')
317 os.chown(fname, self.pw.pw_uid, self.pw.pw_gid)
318 if (is_runnable):
319 os.chmod(fname, 0o755)
320 log.debug(f'End of file: {fname}')
321
322 def add_instance_for_cleanup(self, serverid):
323 if not self.dir in self._instances:
324 self._instances[self.dir] = []
325 self._instances[self.dir].append(serverid)
326
327 def run(self, lines, exit_on_error=True):
328 # Prepare test script
329 # Export current path and python path in test script
330 # to insure that right binary/libraries are used
331 path = os.environ['PATH']
332 pythonpath = os.getenv('PYTHONPATH', '')
333 if exit_on_error:
334 eoe = ( "set -e # Exit on error" , )
335 else:
336 eoe = ()
337 name = f'{self.dir}/run.{self.runid}'
338 self.runid = self.runid + 1
339 self.write_file(name, is_runnable=True, lines=(
340 '#!/usr/bin/bash',
341 'set -x',
342 f'export PATH="{self.dir}/bin:{path}"',
343 f'export PYTHONPATH="{pythonpath}:$PYTHONPATH"',
344 *eoe,
345 *lines,
346 'exit 0',
347 ))
348 # Run the script as self.user
349 log.debug(f'Run script {name} as user {self.user}')
350 if os.geteuid() == 0:
351 return subprocess.run(['/usr/bin/su', '-', self.user, name], capture_output=True, text=True)
352 else:
353 return subprocess.run([name], capture_output=True, text=True)
354
355 def cleanup(self):
356 path = os.environ['PATH']
357 lines = []
358 for k,v in self._instances.items():
359 lines.append(f'export PATH="{k}/bin:{path}"')
360 for i in v:
361 if DEBUGGING:
362 lines.append((f'dsctl {i} stop'))
363 else:
364 lines.append((f'dsctl {i} remove --do-it'))
365 log.debug(f'CLEANUP LINES: {lines}')
366 if lines:
367 self.run(list(lines))
368 if DEBUGGING:
369 log.debug(f'CLEANUP DIRS: {self._dirs}')
370 for d in self._dirs:
371 if os.path.isdir(d):
372 rmtree(d)
373
374
375 @pytest.fixture(scope="module")
376 def nru(request):
377 # Geberate a non root user
378 env = UserEnv('user1')
379
380 def fin():
381 # Should delete the UserEnv object and remove the temporary directory
382 if env:
383 env.cleanup()
384
385 request.addfinalizer(fin)
386 return env;
387
388
389 def test_setup_ds_as_non_root(nru, request):
390 """Test creating an instance as a non root user
391
392 :id: c727998e-a960-11ec-898e-482ae39447e5
393 :setup: no instance
394 :steps:
395 1. Create a dscreate template file
396 2. Create an run a test script that
397 Run dscreate ds-root
398 Run dscreate from-file
399 Add a backend
400 Search users in backend and store output in a file
401 Stop the instance
402 3. Check that pid file exists and kill the associated process
403 4. Check demo_user is in the search result
404 5. Check that test.sh returned 0
405
406
407 :expectedresults:
408 1. No error.
409 2. No error.
410 3. Should fail to kill the process (That is supposed to be stopped)
411 4. demo_user should be in search result
412 5. return code should be 0
413
414 """
415
416 nru.setdir(testname=request.node.name)
417 # Prepare dscreate template
418 nru.write_file(f'{nru.dir}/ds.tmpl', lines=(
419 '[general]',
420 '[slapd]',
421 f'port = {INSTANCE_PORT}',
422 f'instance_name = {INSTANCE_SERVERID}',
423 f'root_password = {PW_DM}',
424 f'secure_port = {INSTANCE_SECURE_PORT}',
425 '[backend-userroot]',
426 'create_suffix_entry = True',
427 'require_index = True',
428 'sample_entries = yes',
429 'suffix = dc=example,dc=com',
430 ))
431 # Create the test script and run it as nru.user
432 nru.add_instance_for_cleanup(INSTANCE_SERVERID)
433 result = nru.run((
434 'type dscreate',
435 f'dscreate ds-root {nru.dir}/root {nru.dir}/bin',
436 'hash -d dscreate # Remove dscreate from hash to use the new one',
437 'type dscreate',
438 f'dscreate from-file {nru.dir}/ds.tmpl',
439 f'dsconf {INSTANCE_SERVERID} backend create --suffix dc=foo,dc=bar --be-name=foo --create-entries',
440 f'ldapsearch -x -H ldap://localhost:{INSTANCE_PORT} -D "cn=directory manager" -w {PW_DM} -b dc=foo,dc=bar "uid=*" | tee {nru.dir}/search.out',
441 f'dsctl {INSTANCE_SERVERID} stop',
442 ))
443 log.info(f'test.sh stdout is: {str(result.stdout)}')
444 log.info(f'test.sh stderr is: {str(result.stderr)}')
445
446 # Let check that demo_user is in the search result
447 with open(f'{nru.dir}/search.out', 'rt') as f:
448 assert(re.findall('demo_user', f.read()))
449 log.debug(f'Check that test script finished successfully.')
450 assert(result.returncode == 0)
451
452 def test_setup_ds_as_non_root_with_non_canonic_paths(nru, request):
453 """Test creating an instance as a non root user
454
455 :id: db8e1ca0-98ce-11ed-89b9-482ae39447e5
456 :setup: no instance
457 :steps:
458 1. Create a dscreate template file
459 2. Create an run a test script that
460 Run dscreate ds-root using non canonic paths
461 Run dscreate from-file
462 Add a backend
463 Search users in backend and store output in a file
464 Stop the instance
465 3. Check that pid file exists and kill the associated process
466 4. Check demo_user is in the search result
467 5. Check that test.sh returned 0
468
469
470 :expectedresults:
471 1. No error.
472 2. No error.
473 3. Should fail to kill the process (That is supposed to be stopped)
474 4. demo_user should be in search result
475 5. return code should be 0
476
477 """
478
479 nru.setdir(testname=request.node.name)
480 # Prepare dscreate template
481 nru.write_file(f'{nru.dir}/ds.tmpl', lines=(
482 '[general]',
483 '[slapd]',
484 f'port = {INSTANCE_PORT}',
485 f'instance_name = {INSTANCE_SERVERID}',
486 f'root_password = {PW_DM}',
487 f'secure_port = {INSTANCE_SECURE_PORT}',
488 '[backend-userroot]',
489 'create_suffix_entry = True',
490 'require_index = True',
491 'sample_entries = yes',
492 'suffix = dc=example,dc=com',
493 ))
494 # Create the test script and run it as nru.user
495 nru.add_instance_for_cleanup(INSTANCE_SERVERID)
496 result = nru.run((
497 'type dscreate',
498 f'dscreate ds-root {nru.dir}/root/. {nru.dir}/bin/',
499 'hash -d dscreate # Remove dscreate from hash to use the new one',
500 'type dscreate',
501 f'dscreate from-file {nru.dir}/ds.tmpl',
502 f'dsconf {INSTANCE_SERVERID} backend create --suffix dc=foo,dc=bar --be-name=foo --create-entries',
503 f'ldapsearch -x -H ldap://localhost:{INSTANCE_PORT} -D "cn=directory manager" -w {PW_DM} -b dc=foo,dc=bar "uid=*" | tee {nru.dir}/search.out',
504 f'dsctl {INSTANCE_SERVERID} stop',
505 ))
506 log.info(f'test.sh stdout is: {str(result.stdout)}')
507 log.info(f'test.sh stderr is: {str(result.stderr)}')
508
509 # Let check that demo_user is in the search result
510 with open(f'{nru.dir}/search.out', 'rt') as f:
511 assert(re.findall('demo_user', f.read()))
512 log.debug(f'Check that test script finished successfully.')
513 assert(result.returncode == 0)
514
515 def test_setup_ds_as_non_root_with_default_options(nru, request):
516 """Test creating an instance as a non root user
517
518 :id: 160e3eaa-7cb9-11ed-9b2b-482ae39447e5
519 :setup: Create a non root user environment
520 :steps:
521 1. Create a dscreate template file
522 2. Create an run a test script that
523 Run dscreate ds-root
524 Run dscreate from-file without specifying any ports
525 Add a backend
526 Search users in backend and store output in a file
527 Stop the instance
528 3. Check demo_user is in the search result
529 4. Check that test.sh returned 0
530
531
532 :expectedresults:
533 1. No error.
534 2. No error.
535 3. Should fail to kill the process (That is supposed to be stopped)
536 4. demo_user should be in search result
537 5. return code should be 0
538
539 """
540
541 nru.setdir(testname=request.node.name)
542 # Prepare dscreate template
543 nru.write_file(f'{nru.dir}/ds.tmpl', lines=(
544 '[general]',
545 '[slapd]',
546 f'instance_name = {INSTANCE_SERVERID}',
547 f'root_password = {PW_DM}',
548 ))
549 # Remove instance if test fails
550 nru.add_instance_for_cleanup(INSTANCE_SERVERID)
551 # Create the test script and run it as nru.user
552 result = nru.run((
553 f'dscreate ds-root {nru.dir}/root {nru.dir}/bin',
554 'hash -d dscreate # Remove dscreate from hash to use the new one',
555 'type dscreate',
556 f'dscreate from-file {nru.dir}/ds.tmpl',
557 f'dsconf {INSTANCE_SERVERID} backend create --suffix dc=foo,dc=bar --be-name=foo --create-entries',
558 "port=`awk '/nsslapd-port/ { print $2; }' " + f"{nru.dir}/root/etc/dirsrv/slapd-{INSTANCE_SERVERID}/dse.ldif`",
559 f'ldapsearch -x -H ldap://localhost:$port -D "cn=directory manager" -w {PW_DM} -b dc=foo,dc=bar "uid=*" | tee {nru.dir}/search.out',
560 f'dsctl {INSTANCE_SERVERID} stop',
561 ))
562 log.info(f'test.sh stdout is: {str(result.stdout)}')
563 log.info(f'test.sh stderr is: {str(result.stderr)}')
564
565 # Let check that demo_user is in the search result
566 with open(f'{nru.dir}/search.out', 'rt') as f:
567 assert(re.findall('demo_user', f.read()))
568 log.debug(f'Check that test script finished successfully.')
569 assert(result.returncode == 0)
570
571 def test_dscreate_non_root_defaults(nru, request):
572 """Test creating an instance as a non root user
573
574 :id: 98174234-7cb9-11ed-9be5-482ae39447e5
575 :setup: Create a non root user environment
576 :steps:
577 1. Run dscreate create-template --advanced
578 2. Checks that we got expected default values
579
580
581 :expectedresults:
582 1. No error.
583 2. Check that:
584 selinux=False
585 systemd=False
586 port != 389
587 secure_port != 636
588
589 """
590
591 nru.setdir(testname=request.node.name)
592 # Prepare dscreate template
593 # Create the test script and run it as nru.user
594 result = nru.run(("dscreate create-template --advanced",))
595 stdout = ensure_str(result.stdout)
596 assert(result.returncode == 0)
597 log.debug(f"stdout={stdout}")
598 assert ";selinux = False" in stdout
599 assert ";systemd = False" in stdout
600 assert not ";secure_port = 636" in stdout
601 assert not ";port = 389" in stdout