From 9f6c75916cee8cb65b21b71c69f62d080818ad63 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Mon, 13 Apr 2026 00:07:46 -0400 Subject: refactor: unify get_{luks,zfs}_passphrase and get_root_password MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract the prompt/confirm/min-length loop into prompt_password() in lib/common.sh using a nameref for the output variable, so UI output stays on the terminal (no command-substitution capture) and the three callers collapse from ~30 lines each to a single helper call. - get_luks_passphrase() — min 8 chars - get_zfs_passphrase() — min 8 chars - get_root_password() — no min (was unchecked before; preserved) 5 bats tests added: match+min-ok path, length-retry loop, mismatch-retry loop, min_len=0 disables check, empty passphrase when min_len=0. make test: 58/58. --- installer/lib/common.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'installer/lib/common.sh') diff --git a/installer/lib/common.sh b/installer/lib/common.sh index 4acd7b9..dcaf071 100644 --- a/installer/lib/common.sh +++ b/installer/lib/common.sh @@ -56,6 +56,43 @@ require_command() { command_exists "$1" || error "Required command not found: $1" } +############################# +# Password / Passphrase Input +############################# + +# Prompt for a secret, require confirmation, enforce min length, loop +# until valid. Sets the named variable by nameref so UI output stays +# on the terminal and the caller doesn't command-substitute. +# +# Usage: prompt_password VAR_NAME "label for prompts" MIN_LEN +# min_len of 0 disables the length check. +prompt_password() { + local -n _out="$1" + local label="$2" + local min_len="${3:-0}" + local confirm + + while true; do + prompt "Enter $label:" + read -rs _out + echo "" + + prompt "Confirm $label:" + read -rs confirm + echo "" + + if [[ "$_out" != "$confirm" ]]; then + warn "Passphrases do not match. Try again." + continue + fi + if [[ $min_len -gt 0 && ${#_out} -lt $min_len ]]; then + warn "Passphrase must be at least $min_len characters. Try again." + continue + fi + break + done +} + ############################# # FZF Prompts ############################# -- cgit v1.2.3