Skip to content

Commit a4d8e22

Browse files
committed
crc-aws-fetch-secrets: try multiple times to get the secrets from the IMDS
1 parent 3191110 commit a4d8e22

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

systemd/crc-aws-fetch-secrets.sh

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,37 @@ if [[ -z "$PULL_SECRETS_KEY" || -z "$KUBEADM_PASS_KEY" || -z "$DEVELOPER_PASS_KE
2424
exit 1
2525
fi
2626

27+
DELAY=5
28+
TOTAL_PERIOD=$(( 3*60 ))
29+
ATTEMPTS=$(( TOTAL_PERIOD / DELAY))
30+
function retry_compact() {
31+
for i in $(seq 1 $ATTEMPTS); do
32+
# If the command succeeds (returns 0), exit the function with success.
33+
if "$@"; then
34+
echo "'$*' succeeded after $i attempts "
35+
return 0
36+
fi
37+
echo "'$*' still failing after $i/$ATTEMPTS attempts ..."
38+
sleep "$DELAY"
39+
done
40+
echo "'$*' didn't succeed after $i attempt ..."
41+
# If the loop finishes, the command never succeeded.
42+
return 1
43+
}
44+
45+
cleanup() {
46+
rm -f /tmp/aws-region /opt/crc/pull-secret.tmp /opt/crc/pass_kubeadmin.tmp /opt/crc/pass_developer.tmp
47+
echo "Temp files cleanup complete."
48+
}
49+
50+
# Cleanup happens automatically via trap on error or at script end
51+
trap cleanup ERR EXIT
52+
2753
SECONDS=0
2854
podman pull --quiet "$AWS_CLI_IMG"
2955
echo "Took $SECONDS seconds to pull the $AWS_CLI_IMG"
3056

31-
wait_imds_available_and_get_region() {
32-
total_timeout_minutes=5
33-
retry_interval_seconds=5
34-
57+
check_imds_available_and_get_region() {
3558
IMDS_TOKEN_COMMAND=(
3659
curl
3760
--connect-timeout 1
@@ -40,25 +63,9 @@ wait_imds_available_and_get_region() {
4063
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600"
4164
-Ssf
4265
)
43-
success=false
44-
deadline=$(( $(date +%s) + (total_timeout_minutes * 60) ))
45-
while [[ $(date +%s) -lt $deadline ]]; do
46-
# By placing the command in an 'if' condition, we can test its exit code
47-
# without triggering 'set -e'. The output is still captured.
48-
if TOKEN=$("${IMDS_TOKEN_COMMAND[@]}"); then
49-
# This block only runs if the curl command succeeds (exit code 0)
50-
success=true
51-
echo "Successfully fetched token." >&2
52-
break # Exit the loop on success
53-
fi
5466

55-
# This block runs if the curl command fails
56-
echo "Failed to connect. Retrying in $retry_interval_seconds seconds..." >&2
57-
sleep "$retry_interval_seconds"
58-
done
59-
60-
if [[ "$success" != "true" ]]; then
61-
echo "ERROR: Could not fetch token after $total_timeout_minutes minutes." >&2
67+
if ! TOKEN=$("${IMDS_TOKEN_COMMAND[@]}"); then
68+
echo "Couldn't fetch the token..." >&2
6269
return 1
6370
fi
6471

@@ -73,12 +80,10 @@ wait_imds_available_and_get_region() {
7380
set +x # disable the xtrace as the token would be leaked
7481
echo "Waiting for the AWS IMDS service to be available ..."
7582
SECONDS=0
76-
wait_imds_available_and_get_region
83+
retry_compact check_imds_available_and_get_region
7784
echo "Took $SECONDS for the IMDS service to become available."
7885
)
7986

80-
missing_secrets=0
81-
8287
save_secret() {
8388
name=$1
8489
key=$2
@@ -101,27 +106,23 @@ save_secret() {
101106
then
102107
rm -f "${dest}.tmp"
103108
echo "ERROR: failed to get the '$name' secret ... (fetched from $key)"
104-
((missing_secrets += 1))
105-
return
109+
return 1
106110
fi
107111
char_count=$(wc -c < "${dest}.tmp")
108112
if (( char_count < MIN_CHAR_COUNT )); then
109113
echo "ERROR: the content of the '$name' secret is too short ... (fetched from $key)"
110114
rm -f "${dest}.tmp"
111-
((missing_secrets += 1))
112-
return
115+
return 1
113116
fi
114117

115118
mv "${dest}.tmp" "${dest}" # atomic creation of the file
116-
}
117119

118-
save_secret "pull-secrets" "$PULL_SECRETS_KEY" /opt/crc/pull-secret
119-
save_secret "kubeadmin-pass" "$KUBEADM_PASS_KEY" /opt/crc/pass_kubeadmin
120-
save_secret "developer-pass" "$DEVELOPER_PASS_KEY" /opt/crc/pass_developer
120+
return 0
121+
}
121122

122-
if (( missing_secrets != 0 )); then
123-
echo "ERROR: failed to fetch $missing_secrets secrets ..."
124-
exit 1
125-
fi
123+
# execution will abort if 'retry_compact' fails.
124+
retry_compact save_secret "pull-secrets" "$PULL_SECRETS_KEY" /opt/crc/pull-secret
125+
retry_compact save_secret "kubeadmin-pass" "$KUBEADM_PASS_KEY" /opt/crc/pass_kubeadmin
126+
retry_compact save_secret "developer-pass" "$DEVELOPER_PASS_KEY" /opt/crc/pass_developer
126127

127128
exit 0

0 commit comments

Comments
 (0)