2
2
set -euo pipefail
3
3
4
4
# shellcheck disable=SC2034 # planned to be used in a future release
5
- SCRIPT_VERSION=" 0.0.7 "
5
+ SCRIPT_VERSION=" 0.0.8 "
6
6
7
7
# === Load user configuration ===
8
8
SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
@@ -30,24 +30,30 @@ fetch_key_file() {
30
30
local METHOD=" $1 "
31
31
local TARGET=" $2 "
32
32
local OUTFILE=" $3 "
33
+ local RETRIES=3
34
+ local RETRY_DELAY=2
33
35
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
51
57
}
52
58
53
59
TMP_FILES=()
@@ -58,16 +64,19 @@ for USER in "${!USER_KEYS[@]}"; do
58
64
ENTRY=" ${USER_KEYS[$USER]} "
59
65
METHOD=" ${ENTRY%%:* } "
60
66
URL=" ${ENTRY#*: } "
67
+
61
68
# Ensure user exists
62
69
if ! id " $USER " & > /dev/null; then
63
70
log_message " User '$USER ' does not exist. Skipping."
64
71
continue
65
72
fi
73
+
66
74
USER_HOME=$( getent passwd " $USER " | cut -d: -f6)
67
75
if [ -z " $USER_HOME " ]; then
68
76
log_message " Failed to determine home directory for user '$USER '. Skipping."
69
77
continue
70
78
fi
79
+
71
80
AUTH_KEYS=" $USER_HOME /.ssh/authorized_keys"
72
81
SSH_DIR=" $( dirname " $AUTH_KEYS " ) "
73
82
@@ -76,21 +85,26 @@ for USER in "${!USER_KEYS[@]}"; do
76
85
mkdir -p " $SSH_DIR "
77
86
chown " $USER :$USER " " $SSH_DIR "
78
87
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 . "
80
89
fi
81
90
82
91
log_message " Fetching key file for $USER from $URL (method: $METHOD )"
83
92
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."
85
94
continue
86
95
fi
87
96
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."
93
101
else
94
- log_message " No changes for user '$USER '"
102
+ log_message " No changes detected in authorized_keys for user '$USER '."
103
+ continue
95
104
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 ."
96
110
done
0 commit comments