Skip to content

Commit 5f368bd

Browse files
authored
Merge pull request #2792 from OSInside/fix_use_disk_password_for_random_keys
Fix setup of use_disk_password for random secret
2 parents 7ff82b0 + 944998b commit 5f368bd

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

kiwi/builder/disk.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,8 +1805,10 @@ def _install_bootloader(
18051805
bootloader.install()
18061806
bootloader.secure_boot_install()
18071807

1808-
if self.use_disk_password and self.luks:
1809-
bootloader.set_disk_password(self.luks)
1808+
if self.use_disk_password and self.storage_map['luks_root']:
1809+
bootloader.set_disk_password(
1810+
self.storage_map['luks_root'].passphrase
1811+
)
18101812
else:
18111813
log.warning(
18121814
'No install of bootcode on read-only root possible'

kiwi/storage/luks_device.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#
1818
import os
1919
import logging
20+
import binascii
2021
from typing import Optional
2122

2223
# project
@@ -47,6 +48,7 @@ def __init__(self, storage_provider: DeviceProvider) -> None:
4748

4849
self.luks_device: Optional[str] = None
4950
self.luks_keyfile: str = ''
51+
self.passphrase: str = ''
5052
self.luks_name = 'luksRoot'
5153

5254
self.option_map = {
@@ -95,6 +97,7 @@ def create_crypto_luks(
9597
:param string root_dir: root dir path
9698
"""
9799
keyslot = '0'
100+
self.passphrase = passphrase
98101
if not options:
99102
options = []
100103
if osname:
@@ -116,7 +119,7 @@ def create_crypto_luks(
116119
keyfile_path = os.path.normpath(
117120
os.sep.join([root_dir, self.luks_keyfile])
118121
)
119-
LuksDevice.create_random_keyfile(keyfile_path)
122+
random_passphrase = LuksDevice.create_random_keyfile(keyfile_path)
120123

121124
if randomize:
122125
log.info('--> Randomizing...')
@@ -139,6 +142,7 @@ def create_crypto_luks(
139142
# initrd also gets protected, e.g through encryption
140143
# like it is done with the secure linux execution on
141144
# zSystems
145+
self.passphrase = random_passphrase
142146
passphrase_file = keyfile_path
143147
# Do not add an additional keyfile
144148
keyfile = ''
@@ -242,15 +246,19 @@ def is_loop(self) -> bool:
242246
return self.storage_provider.is_loop()
243247

244248
@staticmethod
245-
def create_random_keyfile(filename: str) -> None:
249+
def create_random_keyfile(filename: str) -> str:
246250
"""
247251
Create keyfile with random data
248252
249253
:param string filename: file path name
250254
"""
251-
with open(filename, 'wb') as keyfile:
252-
keyfile.write(os.urandom(Defaults.get_luks_key_length()))
255+
random_data = binascii.hexlify(
256+
os.urandom(Defaults.get_luks_key_length())
257+
).decode()
258+
with open(filename, 'w') as keyfile:
259+
keyfile.write(random_data)
253260
os.chmod(filename, 0o600)
261+
return random_data
254262

255263
def __exit__(self, exc_type, exc_value, traceback):
256264
if self.luks_device:

test/unit/builder/disk_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def side_effect(filename):
167167
return_value=self.integrity_root
168168
)
169169
self.luks_root = Mock()
170+
self.luks_root.passphrase = 'passphrase'
170171
kiwi.builder.disk.LuksDevice = Mock(
171172
return_value=self.luks_root
172173
)

test/unit/storage/luks_device_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_create_random_keyfile(self, mock_os_chmod, mock_os_urandom):
247247
mock_open.return_value = MagicMock(spec=io.IOBase)
248248
file_handle = mock_open.return_value.__enter__.return_value
249249
LuksDevice.create_random_keyfile('some-file')
250-
file_handle.write.assert_called_once_with(secret)
250+
file_handle.write.assert_called_once_with('736563726574')
251251
mock_os_chmod.assert_called_once_with('some-file', 0o600)
252252

253253
def test_is_loop(self):

0 commit comments

Comments
 (0)