Debian Codegraph @

dirsrvtests/tests/suites/upgrade/upgrade_bdb2mdb_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2025 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 os
11 import logging
12 import pwd
13 import pytest
14 import re
15 import socket
16 import subprocess
17 import tarfile
18 import time
19 from contextlib import suppress
20 from itertools import permutations
21 from lib389 import DirSrv
22 from lib389._constants import *
23 from lib389.cli_base import FakeArgs
24 from lib389.cli_ctl.dblib import get_bdb_impl_status, dblib_bdb2mdb
25 from lib389.config import Config
26 from lib389.dseldif import DSEldif
27 from lib389.idm.services import ServiceAccounts
28 from lib389.nss_ssl import NssSsl
29 from lib389.passwd import password_generate
30 from lib389.replica import ReplicationManager, Replicas
31 from test389.topologies import topology_m2 as topo_m2
32 from lib389.utils import (
33 get_default_db_lib,
34 escapeDNFiltValue,
35 resolve_selinux_path,
36 selinux_label_file,
37 selinux_label_port,
38 selinux_present,
39 )
40 from pathlib import Path
41
42 log = logging.getLogger(__name__)
43 DEBUGGING = os.getenv("DEBUGGING", default=False)
44
45 # Environmaent variable used to generate the tarball
46 GENTARBALL = "GENERATE_BDB_TARBALL"
47 CURRENT_FILE = os.path.realpath(__file__)
48 TARFILENAME = f'{Path(CURRENT_FILE).parents[2]}/data/bdb_instances/instances.tgz'
49
50 class MigrationHandler:
51 def __init__(self):
52 self.hostname = socket.gethostname()
53 self.uid = os.getuid()
54 self.gid = os.getgid()
55 self.idir = os.getenv("PREFIX", "/")
56 self.inst_list=[]
57 self.username = pwd.getpwuid(self.uid).pw_name
58 # Determine the instances names
59 with tarfile.open(name=TARFILENAME, mode='r:gz') as tar:
60 names = []
61 for member in tar:
62 match = re.match('.*/slapd-([^/]*)/dse.ldif$',member.path)
63 if match and match.group(1) not in names:
64 names.append(match.group(1))
65 self.names = names
66 log.info(f'MigrationHandler has following instances: {names}')
67
68
69 def replace_dse_line(self, line, old_hostname):
70 if old_hostname:
71 line = line.replace(old_hostname, self.hostname)
72 if self.idir != '/':
73 for name in ( '/etc/', '/var/l', '/run/', ):
74 line = line.replace(f' {name}', f' {self.idir}/{name}')
75 if line.startswith('nsslapd-localuser:'):
76 line = f'nsslapd-localuser: {self.username}\n'
77 return line
78
79 def remap_dse(self, path):
80 old_hostname = None
81 tmppath = f'{path}.tmp'
82 with open(path, 'rt') as fin:
83 with open(tmppath, 'wt') as fout:
84 for line in fin:
85 match = re.match(r'nsslapd-localhost:\s*(\S*)',line)
86 if match:
87 old_hostname = match.group(1)
88 fout.write(self.replace_dse_line(line, old_hostname))
89 os.remove(path)
90 os.rename(tmppath, path)
91
92 def tar_filter(self, member, path):
93 fullpath = f'{path}/{member.path}'
94 # Do not try to overwrite existing files except dse.ldif
95 if os.path.exists(fullpath) and not member.path.endswith('/dse.ldif'):
96 return None
97 # use the current user credentials
98 member.uid = self.uid
99 member.gid = self.gid
100 # Security: reject tricky files that may be in tar ball
101 return tarfile.tar_filter(member, path)
102
103 def relabel_selinux(self):
104 if not selinux_present():
105 return
106 for inst in self.inst_list:
107 log.info("Performing SELinux labeling on instance {inst.serverid} ...")
108 dse = DSEldif(inst)
109
110 selinux_attr_labels = {
111 (DN_CONFIG, 'nsslapd-bakdir'): 'dirsrv_var_lib_t',
112 (DN_CONFIG, 'nsslapd-certdir'): 'dirsrv_config_t',
113 (DN_CONFIG, 'nsslapd-ldifdir'): 'dirsrv_var_lib_t',
114 (DN_CONFIG, 'nsslapd-lockdir'): 'dirsrv_var_lock_t',
115 (DN_CONFIG, 'nsslapd-rundir'): 'dirsrv_var_run_t',
116 (DN_CONFIG, 'nsslapd-schemadir'): 'dirsrv_config_t',
117 (DN_CONFIG, 'nsslapd-tmpdir'): 'tmp_t',
118 (DN_CONFIG_LDBM, 'nsslapd-db-home-directory'): 'dirsrv_tmpfs_t',
119 (DN_CONFIG_LDBM, 'nsslapd-directory'): 'dirsrv_var_lib_t',
120 }
121 # Generates the path -> label dict
122 selinux_labels = { dse.get(pair[0], pair[1], single=True) : label for pair,label in selinux_attr_labels.items() }
123 log_dir = os.path.dirname(dse.get(DN_CONFIG, 'nsslapd-accesslog', single=True))
124 selinux_attr_labels[log_dir] = 'dirsrv_var_log_t'
125 if os.path.isdir('/run/dirsrv'):
126 selinux_attr_labels['/run/dirsrv'] = 'dirsrv_var_run_t'
127
128 for path, label in selinux_labels.items():
129 with suppress(ValueError):
130 selinux_label_file(resolve_selinux_path(str(path)), label)
131
132 for port_attr in ( 'nsslapd-port', 'nsslapd-securePort' ):
133 port = dse.get(DN_CONFIG, port_attr, single=True)
134 if port is None or port == '0':
135 continue
136 selinux_label_port(port)
137
138 def dirsrv_instances(self, ignore_errors=False):
139 inst_list = []
140 for name in self.names:
141 inst = DirSrv(verbose=DEBUGGING, external_log=log)
142 try:
143 inst.local_simple_allocate(name, binddn=DN_DM, password=PW_DM)
144 except FileNotFoundError as ex:
145 if ignore_errors is not True:
146 raise ex
147 continue
148 inst.setup_ldapi()
149 inst_list.append(inst)
150 nss = NssSsl(dirsrv=inst)
151 nss.openssl_rehash(nss._certdb)
152 return inst_list
153
154
155 def extract_instances(self):
156 with tarfile.open(name=TARFILENAME, mode='r:gz') as tar:
157 tar.extractall(path=self.idir, filter=self.tar_filter)
158
159 # Fix the dse.ldif
160 for name in self.names:
161 self.remap_dse(f'{self.idir}/etc/dirsrv/slapd-{name}/dse.ldif')
162
163 # Create missing directories
164 os.makedirs(f'{self.idir}/var/log/dirsrv/slapd-supplier1', mode=0o750, exist_ok=True)
165 os.makedirs(f'{self.idir}/var/log/dirsrv/slapd-supplier2', mode=0o750, exist_ok=True)
166 os.makedirs(f'{self.idir}/run/lock/dirsrv/slapd-supplier1', mode=0o750, exist_ok=True)
167 os.makedirs(f'{self.idir}/run/lock/dirsrv/slapd-supplier2', mode=0o750, exist_ok=True)
168 os.makedirs(f'{self.idir}/var/lib/dirsrv/slapd-supplier1/ldif', mode=0o750, exist_ok=True)
169 os.makedirs(f'{self.idir}/var/lib/dirsrv/slapd-supplier2/ldif', mode=0o750, exist_ok=True)
170 os.makedirs(f'{self.idir}/run/dirsrv', mode=0o770, exist_ok=True)
171
172 # Generate DirSrv instances
173 self.inst_list = self.dirsrv_instances()
174
175 # Relabel
176 if self.uid == 0:
177 self.relabel_selinux()
178
179
180 def remove_instances(self):
181 for inst in self.dirsrv_instances(ignore_errors=True):
182 inst.delete()
183
184
185 def migrate2mdb(self):
186 args = FakeArgs()
187 args.tmpdir = None
188 for inst in self.inst_list:
189 log.info(f'Migrating instance {inst.serverid} from bdb to mdb')
190 dblib_bdb2mdb(inst, log, args)
191 inst.start()
192 inst.open()
193 self.reset_agmt_passwords()
194
195
196 def reset_agmt_passwords(self):
197 pw = password_generate()
198
199 for inst in self.inst_list:
200 # For some reason inst.sslport is None although nsslapd-securePort is defined
201 for service in ServiceAccounts(inst, DEFAULT_SUFFIX).list():
202 service.replace('userPassword', pw)
203 replica = Replicas(inst).list()[0]
204 for agmt in replica.get_agreements().list():
205 agmt.replace('nsds5ReplicaCredentials', pw)
206 del pw
207
208
209 def strip_path(path):
210 return re.match(r'^/*([^/].*)$', path).group(1)
211
212
213 @pytest.mark.skipif(get_default_db_lib() != "bdb", reason = f'Requires bdb mode')
214 @pytest.mark.skipif(os.getenv(GENTARBALL) is None, reason = f'Requires setting {GENTARBALL} environment variable')
215 def test_generate_tarball(topo_m2):
216 """Test Generate the tarball for test_upgradefrombdb
217
218 :id: 0325c4fe-da66-11ef-98fc-482ae39447e5
219 :setup: two suppliers
220 :steps:
221 1. Check that replication is working
222 2. Generaters the tarball
223 :expectedresults:
224 1. Success
225 2. Success
226 """
227 idir = os.getenv("PREFIX", "/")
228 repl = ReplicationManager(DEFAULT_SUFFIX)
229 for i1,i2 in permutations(topo_m2, 2):
230 log.info(f'Testing replication {i1.serverid} --> {i2.serverid}')
231 repl.test_replication(i1, i2)
232 for inst in topo_m2:
233 inst.stop()
234 tardir = Path(TARFILENAME).parent
235 os.makedirs(tardir, mode = 0o755, exist_ok = True)
236 this_dir = os.getcwd()
237 with tarfile.open(name=TARFILENAME, mode='w:gz') as tar:
238 os.chdir(idir)
239 for inst in topo_m2:
240 name = inst.serverid
241 tar.add(strip_path(f'/etc/dirsrv/slapd-{name}'))
242 tar.add(strip_path(f'/var/lib/dirsrv/slapd-{name}/db'))
243 os.chdir(this_dir)
244 for inst in topo_m2:
245 inst.start()
246
247
248 @pytest.mark.skipif(get_bdb_impl_status() != BDB_IMPL_STATUS.READ_ONLY, reason = 'Already tested through clu/dsctl_dblib_test.py:test_dblib_migration')
249 def test_upgradefrombdb():
250 """Test upgrade from bdb to mdb
251
252 :id: f0f02d12-da4f-11ef-966f-482ae39447e5
253 :setup: None
254 :steps:
255 1. Extract bdb instances from the tar ball
256 2. Migrate the instances to mdb
257 3. Check that replication is still working
258 :expectedresults:
259 1. Success
260 2. Success
261 3. Success
262 """
263
264 handler = MigrationHandler()
265 handler.remove_instances()
266 handler.extract_instances()
267 handler.migrate2mdb()
268 repl = ReplicationManager(DEFAULT_SUFFIX)
269 for i1,i2 in permutations(handler.inst_list, 2):
270 log.info(f'Testing replication {i1.serverid} --> {i2.serverid}')
271 repl.test_replication(i1, i2)
272 handler.remove_instances()
273
274 if __name__ == '__main__':
275 # Run isolated
276 # -s for DEBUG mode
277 pytest.main(["-s", CURRENT_FILE])
278