@@ -5,22 +5,51 @@ mkdir -p /root/AzureCACertificates
5
5
# http://168.63.129.16 is a constant for the host's wireserver endpoint
6
6
WIRESERVER_ENDPOINT=" http://168.63.129.16"
7
7
8
+ # Function to make HTTP request with retry logic for rate limiting
9
+ make_request_with_retry () {
10
+ local url=" $1 "
11
+ local max_retries=10
12
+ local retry_delay=3
13
+ local attempt=1
14
+
15
+ local response
16
+ while [ $attempt -le $max_retries ]; do
17
+ response=$( curl -f --no-progress-meter " $url " )
18
+ local request_status=$?
19
+
20
+ if echo " $response " | grep -q " RequestRateLimitExceeded" ; then
21
+ sleep $retry_delay
22
+ retry_delay=$(( retry_delay * 2 ))
23
+ attempt=$(( attempt + 1 ))
24
+ elif [ $request_status -ne 0 ]; then
25
+ sleep $retry_delay
26
+ attempt=$(( attempt + 1 ))
27
+ else
28
+ echo " $response "
29
+ return 0
30
+ fi
31
+ done
32
+
33
+ echo " exhausted all retries, last response: $response "
34
+ return 1
35
+ }
36
+
8
37
# Function to process certificate operations from a given endpoint
9
38
process_cert_operations () {
10
39
local endpoint_type=" $1 "
11
40
local operation_response
12
41
13
42
echo " Retrieving certificate operations for type: $endpoint_type "
14
- operation_response=$( curl " ${WIRESERVER_ENDPOINT} /machine?comp=acmspackage&type=$endpoint_type &ext=json" )
15
-
16
- if [ -z " $operation_response " ]; then
17
- echo " Warning: No response received for $ endpoint_type"
43
+ operation_response=$( make_request_with_retry " ${WIRESERVER_ENDPOINT} /machine?comp=acmspackage&type=$endpoint_type &ext=json" )
44
+ local request_status= $?
45
+ if [ -z " $operation_response " ] || [ $request_status -ne 0 ] ; then
46
+ echo " Warning: No response received or request failed for: ${WIRESERVER_ENDPOINT} /machine?comp=acmspackage&type= $ endpoint_type&ext=json "
18
47
return
19
48
fi
20
49
21
50
# Extract ResourceFileName values from the JSON response
22
51
local cert_filenames
23
- cert_filenames=( $( echo " $operation_response " | grep -oP ' (?<="ResouceFileName": ")[^"]*' ) )
52
+ mapfile -t cert_filenames < <( echo " $operation_response " | grep -oP ' (?<="ResouceFileName": ")[^"]*' )
24
53
25
54
if [ ${# cert_filenames[@]} -eq 0 ]; then
26
55
echo " No certificate filenames found in response for $endpoint_type "
@@ -37,9 +66,14 @@ process_cert_operations() {
37
66
38
67
echo " Downloading certificate: filename=$filename , extension=$extension "
39
68
40
- # Retrieve the actual certificate content
69
+ # Retrieve the actual certificate content with retry logic
41
70
local cert_content
42
- cert_content=$( curl " ${WIRESERVER_ENDPOINT} /machine?comp=acmspackage&type=$filename &ext=$extension " )
71
+ cert_content=$( make_request_with_retry " ${WIRESERVER_ENDPOINT} /machine?comp=acmspackage&type=$filename &ext=$extension " )
72
+ local request_status=$?
73
+ if [ -z " $cert_content " ] || [ $request_status -ne 0 ]; then
74
+ echo " Warning: No response received or request failed for: ${WIRESERVER_ENDPOINT} /machine?comp=acmspackage&type=$filename &ext=$extension "
75
+ continue
76
+ fi
43
77
44
78
if [ -n " $cert_content " ]; then
45
79
# Save the certificate to the appropriate location
@@ -58,12 +92,7 @@ process_cert_operations "operationrequestsroot"
58
92
process_cert_operations " operationrequestsintermediate"
59
93
60
94
# Copy all certificate files to the system certificate directory
61
- if [ -n " $( find /root/AzureCACertificates -name ' *.crt' 2> /dev/null) " ]; then
62
- cp /root/AzureCACertificates/* .crt /usr/local/share/ca-certificates/
63
- echo " Copied certificate files to /usr/local/share/ca-certificates/"
64
- else
65
- echo " Warning: No .crt files found to copy"
66
- fi
95
+ cp /root/AzureCACertificates/* .crt /usr/local/share/ca-certificates/
67
96
68
97
# Update the system certificate store
69
98
/usr/sbin/update-ca-certificates
0 commit comments