diff --git a/vaultlocker/shell.py b/vaultlocker/shell.py index ffb0fa1..7d3d8e9 100644 --- a/vaultlocker/shell.py +++ b/vaultlocker/shell.py @@ -186,7 +186,16 @@ def encrypt(args, config): :param: args: argparser generated cli arguments :param: config: configparser object of vaultlocker config """ - _do_it_with_persistence(_encrypt_block_device, args, config) + if os.path.exists( + "/usr/lib/systemd/system/vaultlocker-decrypt@.service" + ) or os.path.exists( + "/etc/systemd/system/vaultlocker-decrypt@.service"): + _do_it_with_persistence(_encrypt_block_device, args, config) + else: + raise FileNotFoundError(""" + Systemd Unit vaultlocker-encrypt@.service not found + in /usr/lib/systemd/system/ or /etc/systemd/system/ + """) def decrypt(args, config): diff --git a/vaultlocker/tests/functional/test_keystorage.py b/vaultlocker/tests/functional/test_keystorage.py index bf84599..1087ac6 100644 --- a/vaultlocker/tests/functional/test_keystorage.py +++ b/vaultlocker/tests/functional/test_keystorage.py @@ -38,7 +38,17 @@ def test_encrypt(self, _luks_open, _luks_format, _systemd, args.block_device = ['/dev/sdb'] args.retry = -1 + def mock_path_exists_side_effect(arg): + if arg in ['/usr/lib/systemd/system/vaultlocker-decrypt@.service', + '/etc/systemd/system/vaultlocker-decrypt@.service']: + return True + else: + return False + + mock_pathexists = mock.patch('os.path.exists').start() + mock_pathexists.side_effect = mock_path_exists_side_effect shell.encrypt(args, self.config) + _luks_format.assert_called_once_with(mock.ANY, '/dev/sdb', 'passed-UUID')