Compare commits

...

2 Commits

Author SHA1 Message Date
John Lancaster
d248af25a0 error message for cert renewal 2026-01-04 19:17:10 -06:00
John Lancaster
fd8ab31779 started ssh-client script 2026-01-04 18:33:17 -06:00

121
scripts/ssh-client.sh Executable file
View File

@@ -0,0 +1,121 @@
#!/usr/bin/env bash
#
# Env vars
#
SSH_CFG_PATH="$(readlink -f ~/.ssh)"
SSH_USER_KEY="$SSH_CFG_PATH/id_ed25519"
SSH_USER_PUBLIC_KEY="$SSH_USER_KEY.pub"
SSH_USER_CERT="$SSH_USER_KEY-cert.pub"
GREEN_CHECK="\e[32m✔\e[0m"
RED_X="\e[31m✗\e[0m"
YELLOW_BANG="\e[33m!\e[0m"
MAGENTA_QUESTION="\e[35m?\e[0m"
UP_ONE_LINE="\e[1A"
#
# Function Definitions
#
reset_line() {
echo -en "\r\e[K"
}
title_msg() {
local title="\e[1m${1:-Title}:\e[0m"
local prompt="${2:-Prompt for the user}"
# printf "%b %b" "$title" "$prompt"
echo -e "$title $prompt"
}
prompt_user() {
full_prompt_msg="$(title_msg "${1}" "${2}")"
echo -n -e "$MAGENTA_QUESTION $full_prompt_msg"
read -p " (y/n) " -r
}
update_prompt() {
local icon="$1"
case $# in
1) msg="$full_prompt_msg $REPLY";;
2) msg="$2";;
3) msg="$(title_msg "${2}" "${3}")";;
*) msg="Too many arguments";;
esac
reset_line
echo -e "$icon $msg"
}
reupdate_prompt() {
# echo -en "$UP_ONE_LINE"
echo -en "$UP_ONE_LINE"
update_prompt "$@"
}
icon_msg() {
local icon="$1"
echo -en "$icon "
title_msg "${@:2}"
}
success_msg() {
icon_msg "${GREEN_CHECK}" "$@"
}
warn_msg() {
icon_msg "${YELLOW_BANG}" "$@"
}
check_user_private_key() {
if [[ -e "$SSH_USER_KEY" ]]; then
success_msg "SSH User" "Private key: $SSH_USER_KEY"
else
prompt_user "SSH User" "Private key missing: ${SSH_USER_KEY}. Create?"
if [[ $REPLY =~ ^[Yy]$ ]]; then
reupdate_prompt $YELLOW_BANG "SSH User" "Creating private key"
ERROR_MSG=$(ssh-keygen -t ed25519 -f "$SSH_USER_KEY" -N "" 2>&1)
if [[ $? -eq 0 ]]; then
reupdate_prompt $GREEN_CHECK "SSH User" "Created private key: ${SSH_USER_KEY}"
else
reupdate_prompt $RED_X "SSH User" "Failed to create key: ${SSH_USER_KEY}"
echo -e "Error: $ERROR_MSG"
fi
elif [[ $REPLY =~ ^[Nn]$ ]]; then
reupdate_prompt $YELLOW_BANG "SSH User" "Continuing without private key"
fi
fi
}
check_user_cert() {
if [[ -e "$SSH_USER_CERT" ]]; then
success_msg "SSH User" "Certificate: $SSH_USER_CERT"
else
prompt_user "SSH User" "Cert missing. Renew cert?"
if [[ $REPLY =~ ^[Yy]$ ]]; then
if renew_user_cert; then
update_prompt $GREEN_CHECK "SSH User" "Renewed cert"
else
update_prompt $RED_X "SSH User" "Failed to renew cert"
fi
elif [[ $REPLY =~ ^[Nn]$ ]]; then
reupdate_prompt $RED_X "SSH User" "Declined to renew cert"
fi
fi
}
renew_user_cert() {
step ssh certificate --sign \
--principal root --principal john \
--provisioner admin \
john@john-pc-ubuntu ~/.ssh/id_ed25519.pub < /dev/tty
}
#
# Run Process
#
check_user_private_key
check_user_cert