@@ -320,6 +320,62 @@ do_install() {
320320 fi
321321}
322322
323+ # Enroll recovery keys for encrypted partitions and store them in Vault
324+ enroll_recovery_keys () {
325+ local vault_token=" $1 "
326+ local machine_id=" $( cat /etc/machine-id) "
327+ local found_any=0
328+
329+ # Find all LUKS-encrypted partitions
330+ local devices=($( lsblk -ln -o NAME,TYPE,FSTYPE | awk ' $2=="part" && $3=="crypto_LUKS" {print "/dev/"$1}' ) )
331+
332+ for device in " ${devices[@]} " ; do
333+ local partlabel=$( lsblk -n -o PARTLABEL " $device " 2> /dev/null | tr -d ' \n\r\t' )
334+
335+ # Skip if no valid partition label
336+ if [ -z " $partlabel " ]; then
337+ continue
338+ fi
339+
340+ # Skip if recovery key already exists in Vault
341+ if VAULT_TOKEN=" ${vault_token} " vault kv get " secrets/mangos/recovery-keys/${machine_id} /${partlabel} " > /dev/null 2>&1 ; then
342+ continue
343+ fi
344+
345+ found_any=1
346+ step " Enrolling recovery key for ${partlabel} "
347+
348+ # Generate and enroll recovery key (systemd-cryptenroll generates and prints the key)
349+ # Use TPM to unlock the device, then enroll a new recovery key
350+ local output=$( systemd-cryptenroll " ${device} " --recovery-key --unlock-tpm2-device=auto 2>&1 )
351+
352+ # Extract recovery key - format: 6 lowercase alphanumeric groups of 8, separated by dashes
353+ # Example: etklvner-lblhnbgl-kdtnujtk-ikjlgbur-lnlrjrrc-iuikkidg-feientnn-dkjeeuft
354+ LUKS_RECOVERY_KEY_REGEX=' [a-z0-9]{8}(-[a-z0-9]{8}){7}'
355+ local recovery_key=$( echo " $output " | grep -oE " ${LUKS_RECOVERY_KEY_REGEX} " | head -n 1)
356+
357+ if [ -n " $recovery_key " ] && [[ " $recovery_key " =~ ^${LUKS_RECOVERY_KEY_REGEX} $ ]]; then
358+ VAULT_TOKEN=" ${vault_token} " vault kv put " secrets/mangos/recovery-keys/${machine_id} /${partlabel} " \
359+ key=" ${recovery_key} " hostname=" ${HOSTNAME} " device=" ${device} " created=" $( date -u +%Y-%m-%dT%H:%M:%SZ) "
360+ if [ $? -eq 0 ]; then
361+ greenln Success
362+ else
363+ red " Failed to store in Vault"
364+ echo
365+ fi
366+ else
367+ red " Failed to enroll or extract recovery key"
368+ echo
369+ fi
370+ done
371+
372+ if [ $found_any -eq 0 ]; then
373+ echo " > All recovery keys already enrolled"
374+ else
375+ echo " > Recovery keys enrolled and stored in Vault"
376+ fi
377+ }
378+
323379do_enroll () {
324380 declare -A groups
325381
@@ -433,6 +489,11 @@ do_enroll() {
433489 entity_name=$( vault write -field=name identity/lookup/entity alias_name=${HOSTNAME} .mangos alias_mount_accessor=${node_auth_accessor} )
434490 echo $entity_name
435491
492+ step " Setting machine-id as entity metadata"
493+ machine_id=$( cat /etc/machine-id)
494+ vault write identity/entity/name/${entity_name} metadata=machine_id=" ${machine_id} "
495+ greenln Success
496+
436497 for group in ${! groups[@]}
437498 do
438499 do_step " Adding host to group '${group} '" chronic do_entity addgroup ${entity_name} ${group}
@@ -553,6 +614,8 @@ do_enroll() {
553614 greenln Success
554615
555616 do_step " Reloading confexts" chronic systemd-confext refresh --mutable=auto
617+
618+ do_step " Enrolling recovery keys for encrypted partitions" enroll_recovery_keys " ${NODE_VAULT_TOKEN} "
556619}
557620
558621do_group () {
@@ -987,6 +1050,12 @@ do_bootstrap() {
9871050 NOMAD_TOKEN=" ${nomad_mgmt_token} " \
9881051 CONSUL_HTTP_TOKEN=${consul_mgmt_token} \
9891052 do_step " Final Terraform run" run_terraform_apply
1053+
1054+ echo
1055+ echo " Bootstrap complete! Next steps:"
1056+ echo " 1. Run: mangosctl sudo enroll -g vault-server -g consul-server -g nomad-server 127.0.0.1"
1057+ echo " 2. This will enroll the bootstrap node's identity and recovery keys"
1058+ echo
9901059}
9911060
9921061set_agent_token () {
0 commit comments