-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_enrollment_string.sh
executable file
·69 lines (56 loc) · 2.2 KB
/
create_enrollment_string.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# Define file to hold persistent values
PERSISTENT_VALUES_FILE="enrollment_values.env"
# Function to save enrollment values
save_values() {
echo "serverUrl='$serverUrl'" > "$PERSISTENT_VALUES_FILE"
echo "hostname='$hostname'" >> "$PERSISTENT_VALUES_FILE"
# For certificate, read the file and save it as a string
certificate=$(cat "$certificatePath")
echo "certificate='$certificate'" >> "$PERSISTENT_VALUES_FILE"
echo "verifyServerCert=$verifyServerCert" >> "$PERSISTENT_VALUES_FILE"
echo "pinRootCert=$pinRootCert" >> "$PERSISTENT_VALUES_FILE"
}
# Encode and print the JSON string
encode_json() {
# Notify user about %DEVICENAME% placeholder
echo "Note: You can use %DEVICENAME% as a placeholder for the device's name."
# Create JSON string
json_string=$(jq -n \
--arg serverUrl "$serverUrl" \
--arg token "$token" \
--arg hostname "$hostname" \
--arg certificate "$certificate" \
--argjson verifyServerCert "$verifyServerCert" \
--argjson pinRootCert "$pinRootCert" \
'{serverUrl: $serverUrl, token: $token, hostname: $hostname, certificate: $certificate, verifyServerCert: $verifyServerCert, pinRootCert: $pinRootCert}')
# Encode to Base64
echo "Encoded Enrollment String:"
echo $(echo "$json_string" | base64 | tr -d "\n")
echo "ENROLLMENT_STRING=$(echo "$json_string" | base64 | tr -d "\n")" > .env
}
# Check for jq and curl
if ! command -v jq &> /dev/null || ! command -v curl &> /dev/null
then
echo "jq and curl are required to run this script. Please install them."
exit
fi
# Main script starts here
mode="$1"
# Fresh input or updating all values
echo "Enter fleet URL:"
read serverUrl
echo "Enter token:"
read token
echo "Enter hostname (you can use %DEVICENAME% as a placeholder):"
read hostname
echo "Enter path to certificate file:"
read certificatePath
echo "Verify server certificate? (true/false):"
read verifyServerCert
echo "Pin root certificate after enrollment? (true/false):"
read pinRootCert
# Save the entered enrollment values
save_values
# Encode and print the final JSON string
encode_json