-
Notifications
You must be signed in to change notification settings - Fork 2
Add LUKS recovery key to vault #59
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| #!/bin/sh | ||
| set -x | ||
|
|
||
| cp -r ${SRCDIR}/resources/mangosctl ${BUILDDIR} | ||
| cd ${BUILDDIR}/mangosctl | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
mkosi.images/base/mkosi.extra/usr/share/mangos/recovery_test.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
| set -x | ||
|
|
||
| machine_id="$(cat /etc/machine-id)" | ||
|
|
||
| test_recovery_key() { | ||
| test_device="$1" | ||
|
|
||
| test_partition="$(lsblk -nlo PARTLABEL ${test_device} | tr -d '\n')" | ||
| mapper_name="$(lsblk -nlo NAME ${test_device} | awk 'NR==2')" | ||
|
|
||
| echo "Testing recovery unlock for ${test_partition} (device: ${test_device}, mapper: ${mapper_name})..." | ||
| echo "" | ||
|
|
||
| # Get recovery key from Vault | ||
| recovery_key="$(mangosctl sudo -- vault kv get -field=key "secrets/mangos/recovery-keys/${machine_id}/${test_partition}")" | ||
|
|
||
| # Find TPM keyslot number | ||
| tpm_slot="$(cryptsetup luksDump ${test_device} | \ | ||
| awk '/Tokens:/,/Keyslots:/ {if (/systemd-tpm2/) found=1; if (found && /^ [0-9]+:/) {print $1; exit}}' | \ | ||
| tr -d ':')" | ||
|
|
||
| echo "Removing TPM keyslot ${tpm_slot} (simulating TPM failure)..." | ||
| # Provide the recovery key on stdin so systemd-cryptenroll does not prompt interactively. | ||
| # Use --unlock-key-file=/dev/stdin to read the key from stdin when wiping the TPM slot. | ||
| printf '%s' "$recovery_key" | systemd-cryptenroll --wipe-slot=tpm2 --unlock-key-file=/dev/stdin ${test_device} | ||
|
|
||
|
|
||
| if [ ! -b /dev/mapper/"${mapper_name}" ]; then | ||
| echo "ERROR: Device not found - /dev/mapper/${mapper_name}" | ||
| return 1 | ||
| fi | ||
|
|
||
| # Get mount point for this partition | ||
| mount_point="$(findmnt -n -o TARGET /dev/mapper/${mapper_name} || true)" | ||
|
|
||
| # Unmount and close | ||
| if [ -n "${mount_point}" ]; then | ||
| systemctl stop "$(systemd-escape -p --suffix=mount "${mount_point}")" | ||
| fi | ||
|
|
||
| cryptsetup close "${mapper_name}" | ||
|
|
||
| # THE CRITICAL TEST: Unlock with recovery key | ||
| echo "Unlocking with recovery key..." | ||
| echo -n "$recovery_key" | systemd-cryptsetup attach "${mapper_name}" "${test_device}" - | ||
| # Remount | ||
| if [ -n "${mount_point}" ]; then | ||
| mount /dev/mapper/"${mapper_name}" "${mount_point}" | ||
| fi | ||
|
|
||
| # Verify device is accessible | ||
| if [ ! -b /dev/mapper/"${mapper_name}" ]; then | ||
| echo "ERROR: Device not accessible after recovery" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Data accessible after recovery: OK" | ||
|
|
||
| # Re-enroll TPM (cleanup for future tests) | ||
| echo "Re-enrolling TPM keyslot..." | ||
| # Re-enroll by supplying the recovery key on stdin (non-interactive) | ||
| printf '%s' "${recovery_key}" | systemd-cryptenroll "${test_device}" \ | ||
| --tpm2-device=auto \ | ||
| --tpm2-pcrs=7 \ | ||
| --tpm2-public-key-pcrs=11 \ | ||
| --unlock-key-file=/dev/stdin | ||
|
|
||
| echo "Recovery test: PASSED" | ||
|
|
||
| } | ||
|
|
||
| # Auto-detect first LUKS partition for testing | ||
| devices="$(lsblk -ln -o NAME,TYPE,FSTYPE | awk '$2=="part" && $3=="crypto_LUKS" {print "/dev/"$1}' | tr '\n' ' ')" | ||
|
|
||
| echo "> LUKS-encrypted devices found: $devices" | ||
|
|
||
| for test_device in ${devices}; do | ||
| echo "> Testing device: ${test_device}" | ||
| test_recovery_key "${test_device}" | ||
| done | ||
|
|
||
| echo "> All recovery tests completed successfully." | ||
| echo "failing to see whats happening" | ||
| exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 1.21.2-1 | ||
| 1.21.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.