From 4aa7c0fcf55f33709a3ca530c2d2637eb6ec438c Mon Sep 17 00:00:00 2001 From: Mohammad Javad Naderi Date: Wed, 21 Aug 2024 13:10:59 +0330 Subject: [PATCH] Print timestamp in log messages --- client/entrypoint.sh | 35 +++++++++++++++++++++++------------ server/entrypoint.sh | 23 ++++++++++++++++------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/client/entrypoint.sh b/client/entrypoint.sh index 415af9f..7a66498 100644 --- a/client/entrypoint.sh +++ b/client/entrypoint.sh @@ -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 @@ -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 # ################################ @@ -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 @@ -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 @@ -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 diff --git a/server/entrypoint.sh b/server/entrypoint.sh index 2004dff..8be02b0 100644 --- a/server/entrypoint.sh +++ b/server/entrypoint.sh @@ -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 @@ -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 # ################################ @@ -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 @@ -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 @@ -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 }'