From 664bdeaed40041297ada128951199e5fd2611c4d Mon Sep 17 00:00:00 2001 From: Justin Oros Date: Thu, 16 Apr 2026 13:14:27 -0700 Subject: [PATCH] fix function ordering, permission check chains, and known_hosts check timing in setup-spoke.sh --- spoke/setup-spoke.sh | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/spoke/setup-spoke.sh b/spoke/setup-spoke.sh index 3dee549..1abd2dc 100644 --- a/spoke/setup-spoke.sh +++ b/spoke/setup-spoke.sh @@ -15,6 +15,23 @@ YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' +info() { echo -e "${GREEN}[+]${NC} $*"; } +warn() { echo -e "${YELLOW}[!]${NC} $*"; } +die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; } +header() { echo -e "\n${CYAN}══════════════════════════════════════════${NC}"; echo -e "${CYAN} $*${NC}"; echo -e "${CYAN}══════════════════════════════════════════${NC}"; } + +check_deps() { + local missing=() + for cmd in "$@"; do + if ! command -v "$cmd" >/dev/null 2>&1; then + missing+=("$cmd") + fi + done + if [ ${#missing[@]} -gt 0 ]; then + die "Missing required dependencies: ${missing[*]}" + fi +} + retry_or_abort() { local test_cmd="$1" local fail_msg="$2" @@ -59,24 +76,6 @@ check_permissions() { fi } -info() { echo -e "${GREEN}[+]${NC} $*"; } -check_deps() { - local missing=() - for cmd in "$@"; do - if ! command -v "$cmd" >/dev/null 2>&1; then - missing+=("$cmd") - fi - done - if [ ${#missing[@]} -gt 0 ]; then - die "Missing required dependencies: ${missing[*]}" - fi -} - - -warn() { echo -e "${YELLOW}[!]${NC} $*"; } -die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; } -header() { echo -e "\n${CYAN}══════════════════════════════════════════${NC}"; echo -e "${CYAN} $*${NC}"; echo -e "${CYAN}══════════════════════════════════════════${NC}"; } - [ "$(id -u)" -eq 0 ] || die "Run as root" check_deps ip ssh ssh-keygen ssh-keyscan systemctl hostnamectl @@ -266,14 +265,16 @@ fi info "Checking SSH key permissions..." check_permissions "$KEY_PATH" "spoke SSH private key" -[ -f "$KEY_PATH.pub" ] && check_permissions "$KEY_PATH.pub" "spoke SSH public key" || true -check_permissions "$SSH_DIR/known_hosts" "known_hosts" || true +if [ -f "$KEY_PATH.pub" ]; then + check_permissions "$KEY_PATH.pub" "spoke SSH public key" +fi info "Scanning hub host key..." sudo -u "$SPOKE_USER" touch "$SSH_DIR/known_hosts" chown "$SPOKE_USER":"$SPOKE_USER" "$SSH_DIR/known_hosts" chmod 600 "$SSH_DIR/known_hosts" sudo -u "$SPOKE_USER" ssh-keyscan -H "$HUB_HOST" >> "$SSH_DIR/known_hosts" 2>/dev/null +check_permissions "$SSH_DIR/known_hosts" "known_hosts" header "Testing SSH Connection" info "Testing connection to $HUB_HOST..."