Skip to content

Commit 58bd895

Browse files
committed
Fixup import of certificates
Only import if the file exists and is not a directory. We still assume that the file content of the pki trust directories matches certificates and not random non certificate files. This is realted to #202
1 parent 5ddf30b commit 58bd895

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

suse_migration_services/units/prepare.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ def perform(self):
9595
cert_file = os.sep.join([root_trust_anchor, cert])
9696
if os.path.islink(cert_file):
9797
cert_file = os.sep.join([self.root_path, os.readlink(cert_file)])
98-
self.log.info('Importing certificate: %s', cert_file)
99-
try:
100-
shutil.copy(cert_file, trust_anchor)
101-
except FileNotFoundError as issue:
102-
self.log.warning('Import of {} failed with {}'.format(cert_file, issue))
98+
if os.path.exists(cert_file) and not os.path.isdir(cert_file):
99+
self.log.info('Importing certificate: {}'.format(cert_file))
100+
try:
101+
shutil.copy(cert_file, trust_anchor)
102+
except FileNotFoundError as issue:
103+
self.log.warning('Import of {} failed with {}'.format(cert_file, issue))
103104
self.log.info('Update certificate pool')
104105
Command.run(['update-ca-certificates'])
105106

test/unit/units/prepare_test.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,32 @@ def test_main(
209209
mock_logger_setup,
210210
mock_update_regionsrv_setup,
211211
):
212+
def isdir_side_effect(path):
213+
if path == '/system-root/usr/share/pki/trust/anchors/foo':
214+
return False
215+
elif path == '/system-root/usr/share/pki/trust/anchors/bar':
216+
return False
217+
elif path == '/system-root/etc/pki/trust/anchors/foo':
218+
return False
219+
elif path == '/system-root/link_target':
220+
return False
221+
return True
222+
223+
def exists_side_effect(path):
224+
if path == '/var/log/zypper.log':
225+
return False
226+
return True
212227

213228
mock_readlink.return_value = 'link_target'
214-
mock_path_isdir.side_effect = [True, True, True, True]
229+
mock_path_isdir.side_effect = isdir_side_effect
215230
migration_config = Mock()
216231
migration_config.is_zypper_migration_plugin_requested.return_value = True
217232
mock_MigrationConfig.return_value = migration_config
218233
fstab = Mock()
219234
mock_Fstab.return_value = fstab
220235
mock_os_listdir.side_effect = [['foo', 'bar'], ['foo', 'bar'], ['fooSMT'], ['fooSMT']]
221236
mock_os_path_islink.side_effect = [False, False, False, True]
222-
mock_os_path_exists.side_effect = [True, True, True, True, False, True, True, True]
237+
mock_os_path_exists.side_effect = exists_side_effect
223238
mock_is_registered.return_value = True
224239
mock_Command_run.side_effect = [
225240
MagicMock(),

0 commit comments

Comments
 (0)