| 1 |
import logging |
| 2 |
import io |
| 3 |
from unittest.mock import ( |
| 4 |
patch, call, MagicMock, Mock |
| 5 |
) |
| 6 |
from pytest import ( |
| 7 |
raises, fixture |
| 8 |
) |
| 9 |
|
| 10 |
from kiwi.exceptions import KiwiLuksSetupError |
| 11 |
from kiwi.storage.luks_device import LuksDevice |
| 12 |
|
| 13 |
|
| 14 |
class TestLuksDevice: |
| 15 |
@fixture(autouse=True) |
| 16 |
def inject_fixtures(self, caplog): |
| 17 |
self._caplog = caplog |
| 18 |
|
| 19 |
def setup(self): |
| 20 |
storage_device = Mock() |
| 21 |
storage_device.get_byte_size = Mock( |
| 22 |
return_value=1048576 |
| 23 |
) |
| 24 |
storage_device.get_uuid = Mock( |
| 25 |
return_value='0815' |
| 26 |
) |
| 27 |
storage_device.get_device = Mock( |
| 28 |
return_value='/dev/some-device' |
| 29 |
) |
| 30 |
storage_device.is_loop = Mock( |
| 31 |
return_value=True |
| 32 |
) |
| 33 |
self.luks = LuksDevice(storage_device) |
| 34 |
self.runtime_config = Mock() |
| 35 |
self.shasum = self.runtime_config.get_checksum_handler.return_value |
| 36 |
self.luks.runtime_config = self.runtime_config |
| 37 |
|
| 38 |
def setup_method(self, cls): |
| 39 |
self.setup() |
| 40 |
|
| 41 |
def test_create_crypto_luks_unsupported_os_options(self): |
| 42 |
with raises(KiwiLuksSetupError): |
| 43 |
self.luks.create_crypto_luks('passphrase', 'some-os') |
| 44 |
|
| 45 |
@patch('os.path.exists') |
| 46 |
def test_get_device(self, mock_path): |
| 47 |
mock_path.return_value = True |
| 48 |
self.luks.luks_device = '/dev/mapper/luksRoot' |
| 49 |
assert self.luks.get_device().get_device() == '/dev/mapper/luksRoot' |
| 50 |
self.luks.luks_device = None |
| 51 |
|
| 52 |
@patch('os.path.exists') |
| 53 |
def test_get_device_none(self, mock_path): |
| 54 |
mock_path.return_value = True |
| 55 |
self.luks.luks_device = None |
| 56 |
assert self.luks.get_device() is None |
| 57 |
|
| 58 |
@patch('kiwi.storage.luks_device.Command.run') |
| 59 |
@patch('os.chmod') |
| 60 |
def test_create_crypto_luks_empty_passphrase( |
| 61 |
self, mock_os_chmod, mock_command |
| 62 |
): |
| 63 |
with patch('builtins.open', create=True): |
| 64 |
self.luks.create_crypto_luks( |
| 65 |
passphrase='', osname='sle12', |
| 66 |
keyfile='some-keyfile', root_dir='root' |
| 67 |
) |
| 68 |
assert mock_command.call_args_list == [ |
| 69 |
call( |
| 70 |
[ |
| 71 |
'dd', 'if=/dev/urandom', 'bs=1M', 'count=1', |
| 72 |
'of=/dev/some-device' |
| 73 |
] |
| 74 |
), |
| 75 |
call( |
| 76 |
[ |
| 77 |
'cryptsetup', '-q', '--key-file', '/dev/zero', |
| 78 |
'--cipher', 'aes-xts-plain64', |
| 79 |
'--key-size', '256', '--hash', 'sha1', |
| 80 |
'--keyfile-size', '32', |
| 81 |
'luksFormat', '/dev/some-device' |
| 82 |
] |
| 83 |
), |
| 84 |
call( |
| 85 |
[ |
| 86 |
'cryptsetup', '--key-file', '/dev/zero', |
| 87 |
'--keyfile-size', '32', |
| 88 |
'luksAddKey', '/dev/some-device', 'root/some-keyfile' |
| 89 |
] |
| 90 |
), |
| 91 |
call( |
| 92 |
[ |
| 93 |
'cryptsetup', '--key-file', '/dev/zero', |
| 94 |
'--keyfile-size', '32', |
| 95 |
'luksHeaderBackup', '/dev/some-device', |
| 96 |
'--header-backup-file', 'root/root/.luks.header' |
| 97 |
] |
| 98 |
), |
| 99 |
call( |
| 100 |
[ |
| 101 |
'cryptsetup', '--key-file', '/dev/zero', |
| 102 |
'--keyfile-size', '32', |
| 103 |
'luksOpen', '/dev/some-device', 'luksRoot' |
| 104 |
] |
| 105 |
) |
| 106 |
] |
| 107 |
self.shasum.digest.assert_called_once_with() |
| 108 |
self.luks.luks_device = None |
| 109 |
|
| 110 |
@patch('kiwi.storage.luks_device.LuksDevice') |
| 111 |
@patch('kiwi.storage.luks_device.Command.run') |
| 112 |
@patch('kiwi.storage.luks_device.Temporary.new_file') |
| 113 |
@patch('os.chmod') |
| 114 |
def test_create_crypto_luks_random_passphrase( |
| 115 |
self, mock_os_chmod, mock_tmpfile, |
| 116 |
mock_command, mock_LuksDevice |
| 117 |
): |
| 118 |
tmpfile = Mock() |
| 119 |
tmpfile.name = 'tmpfile' |
| 120 |
mock_tmpfile.return_value = tmpfile |
| 121 |
with patch('builtins.open', create=True): |
| 122 |
self.luks.create_crypto_luks( |
| 123 |
passphrase='random', osname='sle12', |
| 124 |
keyfile='some-keyfile', root_dir='root' |
| 125 |
) |
| 126 |
assert mock_command.call_args_list == [ |
| 127 |
call( |
| 128 |
[ |
| 129 |
'dd', 'if=/dev/urandom', 'bs=1M', 'count=1', |
| 130 |
'of=/dev/some-device' |
| 131 |
] |
| 132 |
), |
| 133 |
call( |
| 134 |
[ |
| 135 |
'cryptsetup', '-q', '--key-file', 'root/some-keyfile', |
| 136 |
'--cipher', 'aes-xts-plain64', |
| 137 |
'--key-size', '256', '--hash', 'sha1', |
| 138 |
'luksFormat', '/dev/some-device' |
| 139 |
] |
| 140 |
), |
| 141 |
call( |
| 142 |
[ |
| 143 |
'cryptsetup', '--key-file', 'root/some-keyfile', |
| 144 |
'luksHeaderBackup', '/dev/some-device', |
| 145 |
'--header-backup-file', 'root/root/.luks.header' |
| 146 |
] |
| 147 |
), |
| 148 |
call( |
| 149 |
[ |
| 150 |
'cryptsetup', '--key-file', 'root/some-keyfile', 'luksOpen', |
| 151 |
'/dev/some-device', 'luksRoot' |
| 152 |
] |
| 153 |
) |
| 154 |
] |
| 155 |
self.shasum.digest.assert_called_once_with() |
| 156 |
mock_LuksDevice.create_random_keyfile.assert_called_once_with( |
| 157 |
'root/some-keyfile' |
| 158 |
) |
| 159 |
assert self.luks.luks_keyfile == 'some-keyfile' |
| 160 |
self.luks.luks_device = '' |
| 161 |
|
| 162 |
@patch('kiwi.storage.luks_device.LuksDevice') |
| 163 |
@patch('kiwi.storage.luks_device.Command.run') |
| 164 |
@patch('kiwi.storage.luks_device.Temporary.new_file') |
| 165 |
@patch('os.chmod') |
| 166 |
def test_create_crypto_luks( |
| 167 |
self, mock_os_chmod, mock_tmpfile, |
| 168 |
mock_command, mock_LuksDevice |
| 169 |
): |
| 170 |
tmpfile = Mock() |
| 171 |
tmpfile.name = 'tmpfile' |
| 172 |
mock_tmpfile.return_value = tmpfile |
| 173 |
with patch('builtins.open', create=True): |
| 174 |
self.luks.create_crypto_luks( |
| 175 |
passphrase='passphrase', osname='sle12', |
| 176 |
keyfile='some-keyfile', root_dir='root' |
| 177 |
) |
| 178 |
assert mock_command.call_args_list == [ |
| 179 |
call( |
| 180 |
[ |
| 181 |
'dd', 'if=/dev/urandom', 'bs=1M', 'count=1', |
| 182 |
'of=/dev/some-device' |
| 183 |
] |
| 184 |
), |
| 185 |
call( |
| 186 |
[ |
| 187 |
'cryptsetup', '-q', '--key-file', 'tmpfile', |
| 188 |
'--cipher', 'aes-xts-plain64', |
| 189 |
'--key-size', '256', '--hash', 'sha1', |
| 190 |
'luksFormat', '/dev/some-device' |
| 191 |
] |
| 192 |
), |
| 193 |
call( |
| 194 |
[ |
| 195 |
'cryptsetup', '--key-file', 'tmpfile', 'luksAddKey', |
| 196 |
'/dev/some-device', 'root/some-keyfile' |
| 197 |
] |
| 198 |
), |
| 199 |
call( |
| 200 |
[ |
| 201 |
'cryptsetup', '--key-file', 'tmpfile', |
| 202 |
'luksHeaderBackup', '/dev/some-device', |
| 203 |
'--header-backup-file', 'root/root/.luks.header' |
| 204 |
] |
| 205 |
), |
| 206 |
call( |
| 207 |
[ |
| 208 |
'cryptsetup', '--key-file', 'tmpfile', 'luksOpen', |
| 209 |
'/dev/some-device', 'luksRoot' |
| 210 |
] |
| 211 |
) |
| 212 |
] |
| 213 |
self.shasum.digest.assert_called_once_with() |
| 214 |
mock_LuksDevice.create_random_keyfile.assert_called_once_with( |
| 215 |
'root/some-keyfile' |
| 216 |
) |
| 217 |
assert self.luks.luks_keyfile == 'some-keyfile' |
| 218 |
self.luks.luks_device = '' |
| 219 |
|
| 220 |
def test_create_crypttab(self): |
| 221 |
self.luks.luks_device = '/dev/mapper/luksRoot' |
| 222 |
self.luks.luks_keyfile = None |
| 223 |
with patch('builtins.open', create=True) as mock_open: |
| 224 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 225 |
file_handle = mock_open.return_value.__enter__.return_value |
| 226 |
self.luks.create_crypttab('crypttab') |
| 227 |
file_handle.write.assert_called_once_with( |
| 228 |
'luks UUID=0815\n' |
| 229 |
) |
| 230 |
self.luks.luks_device = None |
| 231 |
self.luks.luks_keyfile = 'some-keyfile' |
| 232 |
with patch('builtins.open', create=True) as mock_open: |
| 233 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 234 |
file_handle = mock_open.return_value.__enter__.return_value |
| 235 |
self.luks.create_crypttab('crypttab') |
| 236 |
file_handle.write.assert_called_once_with( |
| 237 |
'luks UUID=0815 /some-keyfile\n' |
| 238 |
) |
| 239 |
self.luks.luks_device = None |
| 240 |
|
| 241 |
@patch('os.urandom') |
| 242 |
@patch('os.chmod') |
| 243 |
def test_create_random_keyfile(self, mock_os_chmod, mock_os_urandom): |
| 244 |
secret = b'secret' |
| 245 |
mock_os_urandom.return_value = secret |
| 246 |
with patch('builtins.open', create=True) as mock_open: |
| 247 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 248 |
file_handle = mock_open.return_value.__enter__.return_value |
| 249 |
LuksDevice.create_random_keyfile('some-file') |
| 250 |
file_handle.write.assert_called_once_with('736563726574') |
| 251 |
mock_os_chmod.assert_called_once_with('some-file', 0o600) |
| 252 |
|
| 253 |
def test_is_loop(self): |
| 254 |
assert self.luks.is_loop() is True |
| 255 |
|
| 256 |
@patch('kiwi.storage.luks_device.Command.run') |
| 257 |
@patch('kiwi.storage.luks_device.log.warning') |
| 258 |
def test_context_manager_exit(self, mock_log_warn, mock_command): |
| 259 |
mock_command.side_effect = Exception |
| 260 |
with self._caplog.at_level(logging.ERROR): |
| 261 |
with LuksDevice(Mock()) as luks: |
| 262 |
luks.luks_device = '/dev/mapper/luksRoot' |
| 263 |
mock_command.assert_called_once_with( |
| 264 |
['cryptsetup', 'luksClose', 'luksRoot'] |
| 265 |
) |