Compare commits

..

6 Commits

Author SHA1 Message Date
John Lancaster
42b7ec401a readme update 2026-01-04 19:50:29 -06:00
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
John Lancaster
c020c481cf polish 2026-01-04 16:50:46 -06:00
John Lancaster
fe0c66d57a pruned 2026-01-04 16:45:09 -06:00
John Lancaster
b08ef16aec added NEEDS_RESTART 2026-01-04 16:44:12 -06:00
3 changed files with 134 additions and 21 deletions

View File

@@ -21,7 +21,7 @@ step ca init --ssh --acme
Install script: Install script:
```bash ```bash
curl -sL https://gitea.john-stream.com/john/janus/raw/branch/main/scripts/ssh-server-check.sh | bash bash <(curl -sL https://gitea.john-stream.com/john/janus/raw/branch/main/scripts/ssh-server-check.sh)
``` ```
### Server ### Server

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

View File

@@ -17,9 +17,11 @@ YELLOW_BANG="\e[33m!\e[0m"
CREATE_USER_CA=0 CREATE_USER_CA=0
CREATE_HOST_CERT=0 CREATE_HOST_CERT=0
NEEDS_RESTART=0
# #
# Function Definition # Function Definitions
# #
# This test loads the sshd config to see what values actually get parsed. # This test loads the sshd config to see what values actually get parsed.
@@ -38,7 +40,8 @@ ssh_config_val() {
title_msg() { title_msg() {
local title="\e[1m${1:-Title}:\e[0m" local title="\e[1m${1:-Title}:\e[0m"
local prompt="${2:-Prompt for the user}" local prompt="${2:-Prompt for the user}"
printf "%b %b" "$title" "$prompt" # printf "%b %b" "$title" "$prompt"
echo -e "$title $prompt"
} }
prompt_user() { prompt_user() {
@@ -60,14 +63,6 @@ update_prompt() {
echo -e "$icon $msg" echo -e "$icon $msg"
} }
auto_update_prompt() {
if [[ $REPLY =~ ^[Yy]$ ]]; then
update_prompt $GREEN_CHECK
elif [[ $REPLY =~ ^[Nn]$ ]]; then
update_prompt $RED_X
fi
}
sign_host_cert() { sign_host_cert() {
local if="eth0" local if="eth0"
local IP_ADDRESS=$(ip -4 addr show dev $if | awk '/inet /{print $2}' | cut -d/ -f1) && \ local IP_ADDRESS=$(ip -4 addr show dev $if | awk '/inet /{print $2}' | cut -d/ -f1) && \
@@ -177,14 +172,13 @@ EOF
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
install_cert_config install_cert_config
update_prompt $GREEN_CHECK "sshd" "Configured to use and accept certs" update_prompt $GREEN_CHECK "sshd" "Configured to use and accept certs"
NEEDS_RESTART=1
fi fi
fi fi
restart_sshd
echo
} }
restart_sshd() { restart_sshd() {
if [[ $NEEDS_RESTART -eq 0 ]]; then return; fi
echo -en "$YELLOW_BANG Restarting sshd..." echo -en "$YELLOW_BANG Restarting sshd..."
systemctl restart sshd systemctl restart sshd
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
@@ -194,34 +188,31 @@ restart_sshd() {
update_prompt $RED_X "sshd" "Failed to restart sshd.service" update_prompt $RED_X "sshd" "Failed to restart sshd.service"
exit 1 exit 1
fi fi
echo
} }
create_files() { create_files() {
local wrote_lines=0
if [[ $CREATE_HOST_CERT -eq 1 ]]; then if [[ $CREATE_HOST_CERT -eq 1 ]]; then
wrote_lines=1
prompt_user "SSH Host" "Cert missing. Sign the ssh host cert?" prompt_user "SSH Host" "Cert missing. Sign the ssh host cert?"
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
update_prompt $YELLOW_BANG "Signing ssh host cert" update_prompt $YELLOW_BANG "Signing ssh host cert"
sign_host_cert sign_host_cert
NEEDS_RESTART=1
else else
update_prompt $RED_X update_prompt $RED_X
fi fi
fi fi
if [[ $CREATE_USER_CA -eq 1 ]]; then if [[ $CREATE_USER_CA -eq 1 ]]; then
wrote_lines=1
prompt_user "SSH Host" "Create the trusted keys file?" prompt_user "SSH Host" "Create the trusted keys file?"
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
(step ssh config --roots > "$path") (step ssh config --roots > "$path")
update_prompt $GREEN_CHECK "SSH Host" "Created the trusted keys file for the SSH host." update_prompt $GREEN_CHECK "SSH Host" "Created the trusted keys file for the SSH host."
NEEDS_RESTART=1
else else
update_prompt $RED_X update_prompt $RED_X
fi fi
fi fi
if [[ $wrote_lines -eq 1 ]]; then echo; fi
} }
@@ -230,8 +221,9 @@ create_files() {
check_cert_config "certs.conf" check_cert_config "certs.conf"
check_ssh_config_files check_ssh_config_files
create_files create_files
restart_sshd
title_msg "SSH Host Cert" "$SSH_HOST_CERT\n" title_msg "SSH Host Cert" "$SSH_HOST_CERT"
CERT_INFO=$(ssh-keygen -Lf "$SSH_HOST_CERT") CERT_INFO=$(ssh-keygen -Lf "$SSH_HOST_CERT")
echo -e "$CERT_INFO" | grep "Public key" echo -e "$CERT_INFO" | grep "Public key"
echo -e "$CERT_INFO" | grep "Valid" echo -e "$CERT_INFO" | grep "Valid"