Skip to content

Commit

Permalink
Print timestamp in log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mjnaderi committed Aug 21, 2024
1 parent 289c6e9 commit 4aa7c0f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
35 changes: 23 additions & 12 deletions client/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/bin/sh

log_with_timestamp() {
echo -e "$1" | awk '{ print strftime("[%Y-%m-%d %H:%M:%S%z]"), $0 }'
}

# Ensure the script is not run by the "root" user.
if [ "$(id -u)" == "0" ]; then
echo "This image should not be run as the 'root' user. Exiting..."
exit 1
fi

if [ -z "${SSH_HOSTNAME}" ]; then
echo "SSH_HOSTNAME is not set. Exiting..."
log_with_timestamp "This image should not be run as the 'root' user. Exiting..."
exit 1
fi

Expand All @@ -23,6 +22,16 @@ export LD_PRELOAD=/usr/lib/libnss_wrapper.so NSS_WRAPPER_PASSWD=/tmp/passwd NSS_
mkdir -p "${HOME}/.ssh"
chmod -R 700 "${HOME}"

log_with_timestamp "\033[1;34mWelcome to docker-ssh/client!\033[0m"
log_with_timestamp "\033[1;32m Alpine: \033[0m $(cat /etc/alpine-release)"
log_with_timestamp "\033[1;32m OpenSSH: \033[0m $(ssh -V 2>&1)"
log_with_timestamp "\033[1;32m Rsync: \033[0m $(rsync --version | head -n 1)"

if [ -z "${SSH_HOSTNAME}" ]; then
log_with_timestamp "SSH_HOSTNAME is not set. Exiting..."
exit 1
fi

################################
# setup keys #
################################
Expand All @@ -31,18 +40,18 @@ if [ -n "${CLIENT_ED25519_PRIVATE_KEY_FILE}" ]; then
if [ "${CLIENT_ED25519_PRIVATE_KEY_FILE}" != "${HOME}/.ssh/id_ed25519" ]; then
cp "${CLIENT_ED25519_PRIVATE_KEY_FILE}" "${HOME}/.ssh/id_ed25519"
chmod 600 "${HOME}/.ssh/id_ed25519"
log_with_timestamp "Installed private key from key file."
fi
echo "Installed private key from key file."
else
echo "'${CLIENT_ED25519_PRIVATE_KEY_FILE}' is not readable. Exiting..."
log_with_timestamp "'${CLIENT_ED25519_PRIVATE_KEY_FILE}' is not readable. Exiting..."
exit 1
fi
elif [ -n "${CLIENT_ED25519_PRIVATE_KEY_BASE64}" ]; then
echo "${CLIENT_ED25519_PRIVATE_KEY_BASE64}" | base64 -d >"${HOME}/.ssh/id_ed25519"
chmod 600 "${HOME}/.ssh/id_ed25519"
echo "Installed private key from env var."
log_with_timestamp "Installed private key from env var."
else
echo "No private key provided. Exiting..."
log_with_timestamp "No private key provided. Exiting..."
exit 1
fi

Expand All @@ -54,7 +63,7 @@ if [ -n "${SERVER_ED25519_PUBLIC_KEY}" ]; then
fi
chmod 600 "${HOME}/.ssh/known_hosts"
else
echo "SERVER_ED25519_PUBLIC_KEY is not set. Exiting..."
log_with_timestamp "SERVER_ED25519_PUBLIC_KEY is not set. Exiting..."
exit 1
fi

Expand Down Expand Up @@ -93,8 +102,10 @@ export AUTOSSH_POLL="${AUTOSSH_POLL:-30}"
# run/schedule the command #
################################
if [ -n "${SCHEDULE}" ]; then
log_with_timestamp "Scheduling command..."
echo "${SCHEDULE} ${SCHEDULE_CMD}" >"${HOME}/crontab"
exec supercronic "${HOME}/crontab"
else
exec "$@"
log_with_timestamp "Running $1..."
exec "$@" 2>&1 | awk '{ print strftime("[%Y-%m-%d %H:%M:%S%z]"), $0 }'
fi
23 changes: 16 additions & 7 deletions server/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/bin/sh

log_with_timestamp() {
echo -e "$1" | awk '{ print strftime("[%Y-%m-%d %H:%M:%S%z]"), $0 }'
}

# Ensure the script is not run by the "root" user.
if [ "$(id -u)" == "0" ]; then
echo "This image should not be run as the 'root' user. Exiting..."
log_with_timestamp "This image should not be run as the 'root' user. Exiting..."
exit 1
fi

Expand All @@ -18,6 +22,11 @@ export LD_PRELOAD=/usr/lib/libnss_wrapper.so NSS_WRAPPER_PASSWD=/tmp/passwd NSS_
mkdir -p "${HOME}/sshd" "${HOME}/.ssh"
chmod -R 700 "${HOME}"

log_with_timestamp "\033[1;34mWelcome to docker-ssh/server!\033[0m"
log_with_timestamp "\033[1;32m Alpine: \033[0m $(cat /etc/alpine-release)"
log_with_timestamp "\033[1;32m OpenSSH: \033[0m $(sshd -V 2>&1)"
log_with_timestamp "\033[1;32m Rsync: \033[0m $(rsync --version | head -n 1)"

################################
# setup host key #
################################
Expand All @@ -26,18 +35,18 @@ if [ -n "${SERVER_ED25519_PRIVATE_KEY_FILE}" ]; then
if [ "${SERVER_ED25519_PRIVATE_KEY_FILE}" != "${HOME}/sshd/ssh_host_ed25519_key" ]; then
cp "${SERVER_ED25519_PRIVATE_KEY_FILE}" "${HOME}/sshd/ssh_host_ed25519_key"
chmod 600 "${HOME}/sshd/ssh_host_ed25519_key"
log_with_timestamp "Installed host key from key file."
fi
echo "Installed host key from key file."
else
echo "'${SERVER_ED25519_PRIVATE_KEY_FILE}' is not readable. Exiting..."
log_with_timestamp "'${SERVER_ED25519_PRIVATE_KEY_FILE}' is not readable. Exiting..."
exit 1
fi
elif [ -n "${SERVER_ED25519_PRIVATE_KEY_BASE64}" ]; then
echo "${SERVER_ED25519_PRIVATE_KEY_BASE64}" | base64 -d >"${HOME}/sshd/ssh_host_ed25519_key"
chmod 600 "${HOME}/sshd/ssh_host_ed25519_key"
echo "Installed host key from env var."
log_with_timestamp "Installed host key from env var."
else
echo "No private key provided. Exiting..."
log_with_timestamp "No private key provided. Exiting..."
exit 1
fi

Expand All @@ -50,7 +59,7 @@ fi
# configure authorized_keys #
################################
if [ -z "${CLIENT_AUTHORIZED_KEYS}" ]; then
echo "CLIENT_AUTHORIZED_KEYS is not set. Exiting..."
log_with_timestamp "CLIENT_AUTHORIZED_KEYS is not set. Exiting..."
exit 1
else
# Split the CLIENT_AUTHORIZED_KEYS variable by semicolon and add each to authorized_keys
Expand Down Expand Up @@ -92,4 +101,4 @@ AllowUsers sshuser
################################
# Start sshd #
################################
exec /usr/sbin/sshd -D -e -f "${HOME}/sshd/sshd.conf"
exec /usr/sbin/sshd -D -e -f "${HOME}/sshd/sshd.conf" 2>&1 | awk '{ print strftime("[%Y-%m-%d %H:%M:%S%z]"), $0 }'

0 comments on commit 4aa7c0f

Please sign in to comment.