22set -euo pipefail
33
44# shellcheck disable=SC2034 # planned to be used in a future release
5- SCRIPT_VERSION=" 0.0.7 "
5+ SCRIPT_VERSION=" 0.0.8 "
66
77# === Load user configuration ===
88SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
@@ -30,24 +30,30 @@ fetch_key_file() {
3030 local METHOD=" $1 "
3131 local TARGET=" $2 "
3232 local OUTFILE=" $3 "
33+ local RETRIES=3
34+ local RETRY_DELAY=2
3335
34- if [[ " $METHOD " == " raw" ]]; then
35- curl -fsSL " $TARGET " -o " $OUTFILE "
36- return $?
37- elif [[ " $METHOD " == " api" ]]; then
38- : " ${GITHUB_TOKEN:? GITHUB_TOKEN is required for API access} "
39- curl -fsSL -H " Authorization: token $GITHUB_TOKEN " \
40- -H " Accept: application/vnd.github.v3.raw" \
41- " $TARGET " -o " $OUTFILE "
42- return $?
43- elif [[ " $METHOD " == " ghuser" ]]; then
44- # TARGET is the GitHub username
45- curl -fsSL " https://github.com/${TARGET} .keys" -o " $OUTFILE "
46- return $?
47- else
48- log_message " Error: Unsupported method '$METHOD ' encountered for URL '$TARGET '. Halting execution."
49- exit 2
50- fi
36+ for (( i= 1 ; i<= RETRIES; i++ )) ; do
37+ if [[ " $METHOD " == " raw" ]]; then
38+ curl -fsSL " $TARGET " -o " $OUTFILE " && return 0
39+ elif [[ " $METHOD " == " api" ]]; then
40+ : " ${GITHUB_TOKEN:? GITHUB_TOKEN is required for API access} "
41+ curl -fsSL -H " Authorization: token $GITHUB_TOKEN " \
42+ -H " Accept: application/vnd.github.v3.raw" \
43+ " $TARGET " -o " $OUTFILE " && return 0
44+ elif [[ " $METHOD " == " ghuser" ]]; then
45+ curl -fsSL " https://github.com/${TARGET} .keys" -o " $OUTFILE " && return 0
46+ else
47+ log_message " Error: Unsupported method '$METHOD ' encountered for URL '$TARGET '. Halting execution."
48+ exit 2
49+ fi
50+
51+ log_message " Attempt $i /$RETRIES failed for method '$METHOD ' and URL '$TARGET '. Retrying in $RETRY_DELAY seconds..."
52+ sleep " $RETRY_DELAY "
53+ done
54+
55+ log_message " Error: All $RETRIES attempts failed for method '$METHOD ' and URL '$TARGET '. Skipping."
56+ return 1
5157}
5258
5359TMP_FILES=()
@@ -58,16 +64,19 @@ for USER in "${!USER_KEYS[@]}"; do
5864 ENTRY=" ${USER_KEYS[$USER]} "
5965 METHOD=" ${ENTRY%%:* } "
6066 URL=" ${ENTRY#*: } "
67+
6168 # Ensure user exists
6269 if ! id " $USER " & > /dev/null; then
6370 log_message " User '$USER ' does not exist. Skipping."
6471 continue
6572 fi
73+
6674 USER_HOME=$( getent passwd " $USER " | cut -d: -f6)
6775 if [ -z " $USER_HOME " ]; then
6876 log_message " Failed to determine home directory for user '$USER '. Skipping."
6977 continue
7078 fi
79+
7180 AUTH_KEYS=" $USER_HOME /.ssh/authorized_keys"
7281 SSH_DIR=" $( dirname " $AUTH_KEYS " ) "
7382
@@ -76,21 +85,26 @@ for USER in "${!USER_KEYS[@]}"; do
7685 mkdir -p " $SSH_DIR "
7786 chown " $USER :$USER " " $SSH_DIR "
7887 chmod 700 " $SSH_DIR "
79- log_message " Created .ssh directory for user '$USER '"
88+ log_message " Created .ssh directory for user '$USER ' at $SSH_DIR . "
8089 fi
8190
8291 log_message " Fetching key file for $USER from $URL (method: $METHOD )"
8392 if ! fetch_key_file " $METHOD " " $URL " " $TMP_FILE " ; then
84- log_message " Failed to fetch key file for user '$USER ' from $URL . Skipping."
93+ log_message " Failed to fetch key file for user '$USER ' from $URL after multiple attempts . Skipping."
8594 continue
8695 fi
8796
88- if [ ! -f " $AUTH_KEYS " ] || ! cmp -s " $TMP_FILE " " $AUTH_KEYS " ; then
89- cp " $TMP_FILE " " $AUTH_KEYS "
90- chown " $USER :$USER " " $AUTH_KEYS "
91- chmod 600 " $AUTH_KEYS "
92- log_message " Updated authorized_keys for user '$USER '"
97+ if [ ! -f " $AUTH_KEYS " ]; then
98+ log_message " No existing authorized_keys file for user '$USER '. Creating a new one."
99+ elif ! cmp -s " $TMP_FILE " " $AUTH_KEYS " ; then
100+ log_message " Changes detected in authorized_keys for user '$USER '. Updating the file."
93101 else
94- log_message " No changes for user '$USER '"
102+ log_message " No changes detected in authorized_keys for user '$USER '."
103+ continue
95104 fi
105+
106+ cp " $TMP_FILE " " $AUTH_KEYS "
107+ chown " $USER :$USER " " $AUTH_KEYS "
108+ chmod 600 " $AUTH_KEYS "
109+ log_message " Updated authorized_keys for user '$USER ' at $AUTH_KEYS ."
96110done
0 commit comments