-
Notifications
You must be signed in to change notification settings - Fork 568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling encryption logic when only one non-root partition is encrypted (such as /home) #2575
base: master
Are you sure you want to change the base?
Changes from all commits
35c6311
91abe1f
7625c65
4322f3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,7 +191,16 @@ def lock(self): | |
|
||
self._mapper_dev = None | ||
|
||
def create_keyfile(self, target_path: Path, override: bool = False): | ||
def create_crypttab(self, target_path: Path, key_file: Path|None = None, options: List[str]|None = None, override: bool = False): | ||
crypttab_path = target_path / 'etc/crypttab' | ||
self._crypttab( | ||
crypttab_path, | ||
key_file=key_file, | ||
options=options or ["luks", "key-slot=1"], | ||
override=override | ||
) | ||
|
||
def create_keyfile(self, target_path: Path, override: bool = False) -> Path: | ||
""" | ||
Routine to create keyfiles, so it can be moved elsewhere | ||
""" | ||
|
@@ -202,12 +211,11 @@ def create_keyfile(self, target_path: Path, override: bool = False): | |
# automatically load this key if we name the device to "xyzloop" | ||
kf_path = Path(f'/etc/cryptsetup-keys.d/{self.mapper_name}.key') | ||
key_file = target_path / kf_path.relative_to(kf_path.root) | ||
crypttab_path = target_path / 'etc/crypttab' | ||
|
||
if key_file.exists(): | ||
if not override: | ||
info(f'Key file {key_file} already exists, keeping existing') | ||
return | ||
return kf_path | ||
else: | ||
info(f'Key file {key_file} already exists, overriding') | ||
|
||
|
@@ -219,7 +227,8 @@ def create_keyfile(self, target_path: Path, override: bool = False): | |
key_file.chmod(0o400) | ||
|
||
self._add_key(key_file) | ||
self._crypttab(crypttab_path, kf_path, options=["luks", "key-slot=1"]) | ||
|
||
return kf_path | ||
|
||
def _add_key(self, key_file: Path): | ||
debug(f'Adding additional key-file {key_file}') | ||
|
@@ -239,13 +248,33 @@ def _add_key(self, key_file: Path): | |
def _crypttab( | ||
self, | ||
crypttab_path: Path, | ||
key_file: Path, | ||
options: List[str] | ||
options: List[str], | ||
key_file: Path|None = None, | ||
override: bool = False | ||
) -> None: | ||
debug(f'Adding crypttab entry for key {key_file}') | ||
|
||
with open(crypttab_path, 'a') as crypttab: | ||
with open(crypttab_path, 'r+') as crypttab: | ||
existing_data = crypttab.readlines() | ||
opt = ','.join(options) | ||
uuid = self._get_luks_uuid() | ||
row = f"{self.mapper_name} UUID={uuid} {key_file} {opt}\n" | ||
crypttab.write(row) | ||
new_row = f"{self.mapper_name} UUID={uuid} {key_file or 'none'} {opt}\n" | ||
|
||
for index, existing_row in enumerate(existing_data): | ||
if uuid in existing_row: | ||
if override is False: | ||
debug(f"Found {uuid} in {existing_row}") | ||
return True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this return here right? If override is passed into the function should this terminate ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's checking if we don't want to override - but it found that we already have added the data - so it's happy and returns "done" :) |
||
else: | ||
existing_data.pop(index) | ||
|
||
# Make sure the last entry ends with \n | ||
# before inserting our new entry at the end | ||
if not existing_data[-1][-1:] == '\n': | ||
existing_data.append('\n') | ||
|
||
existing_data.append(new_row) | ||
|
||
crypttab.seek(0) | ||
crypttab.truncate() | ||
crypttab.write(''.join(existing_data)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just FYI there's a
part.is_root()
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check doesn't feel right here...on the caller side this is run for each partition now, but we can probably short circuit it
And on the caller
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silly me just copied the code from two rows down, but now that you mention it I do remember seeing that function, I'll change!