diff --git a/scripts/ssh-client.sh b/scripts/ssh-client.sh new file mode 100755 index 0000000..095dc05 --- /dev/null +++ b/scripts/ssh-client.sh @@ -0,0 +1,89 @@ +#!/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) " -n 1 -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" + update_prompt "$@" +} + + +renew_user_cert() { + step ssh certificate --sign \ + --principal root --principal john \ + --provisioner admin \ + john@john-pc-ubuntu ~/.ssh/id_ed25519.pub +} + +# +# Run Process +# + +if [[ ! -e "$SSH_USER_KEY" ]]; then + prompt_user "SSH User" "Private key missing: ${SSH_USER_KEY}. Create?" + if [[ $REPLY =~ ^[Yy]$ ]]; then + update_prompt $YELLOW_BANG "SSH User" "Creating private key" + sleep 0.5 + reupdate_prompt $GREEN_CHECK "SSH User" "Created private key: ${SSH_USER_KEY}" + fi +fi + +prompt_user "SSH User" "Cert missing. Renew cert?" +if [[ $REPLY =~ ^[Yy]$ ]]; then + # update_prompt $YELLOW_BANG "SSH User" "Renewing cert" + # sleep 0.25 + renew_user_cert + if [[ $? -eq 0 ]]; then + update_prompt $GREEN_CHECK "SSH User" "Renewed cert" + else + update_prompt $RED_X "SSH User" "Failed to renew cert" + fi + echo +fi