Skip to content

Commit e7743f6

Browse files
authored
Merge pull request #39 from kolbe/master
Improved bash tests and quoting
2 parents dc23c27 + abe5d5f commit e7743f6

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

unifi_ssl_import.sh

+28-28
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ else
7070
printf "\nRunning in Standard Mode...\n"
7171
fi
7272

73-
if [ ${LE_MODE} == "true" ]; then
73+
if [[ ${LE_MODE} == "true" ]]; then
7474
# Check to see whether LE certificate has changed
7575
printf "\nInspecting current SSL certificate...\n"
76-
if md5sum -c ${LE_LIVE_DIR}/${UNIFI_HOSTNAME}/privkey.pem.md5 &>/dev/null; then
76+
if md5sum -c "${LE_LIVE_DIR}/${UNIFI_HOSTNAME}/privkey.pem.md5" &>/dev/null; then
7777
# MD5 remains unchanged, exit the script
7878
printf "\nCertificate is unchanged, no update is necessary.\n"
7979
exit 0
@@ -84,7 +84,7 @@ if [ ${LE_MODE} == "true" ]; then
8484
fi
8585

8686
# Verify required files exist
87-
if [ ! -f "${PRIV_KEY}" ] || [ ! -f "${CHAIN_FILE}" ]; then
87+
if [[ ! -f ${PRIV_KEY} ]] || [[ ! -f ${CHAIN_FILE} ]]; then
8888
printf "\nMissing one or more required files. Check your settings.\n"
8989
exit 1
9090
else
@@ -99,24 +99,24 @@ P12_TEMP=$(mktemp)
9999

100100
# Stop the UniFi Controller
101101
printf "\nStopping UniFi Controller...\n"
102-
service ${UNIFI_SERVICE} stop
102+
service "${UNIFI_SERVICE}" stop
103103

104-
if [ ${LE_MODE} == "true" ]; then
104+
if [[ ${LE_MODE} == "true" ]]; then
105105

106106
# Write a new MD5 checksum based on the updated certificate
107107
printf "\nUpdating certificate MD5 checksum...\n"
108108

109-
md5sum ${PRIV_KEY} > ${LE_LIVE_DIR}/${UNIFI_HOSTNAME}/privkey.pem.md5
109+
md5sum "${PRIV_KEY}" > "${LE_LIVE_DIR}/${UNIFI_HOSTNAME}/privkey.pem.md5"
110110

111111
fi
112112

113113
# Create double-safe keystore backup
114-
if [ -s "${KEYSTORE}.orig" ]; then
114+
if [[ -s "${KEYSTORE}.orig" ]]; then
115115
printf "\nBackup of original keystore exists!\n"
116116
printf "\nCreating non-destructive backup as keystore.bak...\n"
117-
cp ${KEYSTORE} ${KEYSTORE}.bak
117+
cp "${KEYSTORE}" "${KEYSTORE}.bak"
118118
else
119-
cp ${KEYSTORE} ${KEYSTORE}.orig
119+
cp "${KEYSTORE}" "${KEYSTORE}.orig"
120120
printf "\nNo original keystore backup found.\n"
121121
printf "\nCreating backup as keystore.orig...\n"
122122
fi
@@ -125,42 +125,42 @@ fi
125125
printf "\nExporting SSL certificate and key data into temporary PKCS12 file...\n"
126126

127127
#If there is a signed crt we should include this in the export
128-
if [ -f "${SIGNED_CRT}" ]; then
128+
if [[ -f ${SIGNED_CRT} ]]; then
129129
openssl pkcs12 -export \
130-
-in ${CHAIN_FILE} \
131-
-in ${SIGNED_CRT} \
132-
-inkey ${PRIV_KEY} \
133-
-out ${P12_TEMP} -passout pass:${PASSWORD} \
134-
-name ${ALIAS}
130+
-in "${CHAIN_FILE}" \
131+
-in "${SIGNED_CRT}" \
132+
-inkey "${PRIV_KEY}" \
133+
-out "${P12_TEMP}" -passout pass:"${PASSWORD}" \
134+
-name "${ALIAS}"
135135
else
136136
openssl pkcs12 -export \
137-
-in ${CHAIN_FILE} \
138-
-inkey ${PRIV_KEY} \
139-
-out ${P12_TEMP} -passout pass:${PASSWORD} \
140-
-name ${ALIAS}
137+
-in "${CHAIN_FILE}" \
138+
-inkey "${PRIV_KEY}" \
139+
-out "${P12_TEMP}" -passout pass:"${PASSWORD}" \
140+
-name "${ALIAS}"
141141
fi
142142

143143
# Delete the previous certificate data from keystore to avoid "already exists" message
144144
printf "\nRemoving previous certificate data from UniFi keystore...\n"
145-
keytool -delete -alias ${ALIAS} -keystore ${KEYSTORE} -deststorepass ${PASSWORD}
145+
keytool -delete -alias "${ALIAS}" -keystore "${KEYSTORE}" -deststorepass "${PASSWORD}"
146146

147147
# Import the temp PKCS12 file into the UniFi keystore
148148
printf "\nImporting SSL certificate into UniFi keystore...\n"
149149
keytool -importkeystore \
150-
-srckeystore ${P12_TEMP} -srcstoretype PKCS12 \
151-
-srcstorepass ${PASSWORD} \
152-
-destkeystore ${KEYSTORE} \
153-
-deststorepass ${PASSWORD} \
154-
-destkeypass ${PASSWORD} \
155-
-alias ${ALIAS} -trustcacerts
150+
-srckeystore "${P12_TEMP}" -srcstoretype PKCS12 \
151+
-srcstorepass "${PASSWORD}" \
152+
-destkeystore "${KEYSTORE}" \
153+
-deststorepass "${PASSWORD}" \
154+
-destkeypass "${PASSWORD}" \
155+
-alias "${ALIAS}" -trustcacerts
156156

157157
# Clean up temp files
158158
printf "\nRemoving temporary files...\n"
159-
rm -f ${P12_TEMP}
159+
rm -f "${P12_TEMP}"
160160

161161
# Restart the UniFi Controller to pick up the updated keystore
162162
printf "\nRestarting UniFi Controller to apply new Let's Encrypt SSL certificate...\n"
163-
service ${UNIFI_SERVICE} start
163+
service "${UNIFI_SERVICE}" start
164164

165165
# That's all, folks!
166166
printf "\nDone!\n"

0 commit comments

Comments
 (0)