Skip to content

Commit 4b2b442

Browse files
committed
Cleanup symlinks to non SSL enabled domains
1 parent 9570237 commit 4b2b442

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

app/letsencrypt_service

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,63 @@ create_links() {
4242
return $return_code
4343
}
4444

45+
function cleanup_links {
46+
local -a ENABLED_DOMAINS
47+
local -a SYMLINKED_DOMAINS
48+
local -a DISABLED_DOMAINS
49+
50+
# Create an array containing domains for which a
51+
# symlinked private key exists in /etc/nginx/certs.
52+
for symlinked_domain in /etc/nginx/certs/*.crt; do
53+
[[ -f "$symlinked_domain" ]] || continue
54+
symlinked_domain="${symlinked_domain##*/}"
55+
symlinked_domain="${symlinked_domain%*.crt}"
56+
SYMLINKED_DOMAINS+=("$symlinked_domain")
57+
done
58+
[[ $DEBUG == true ]] && echo "Symlinked domains: ${SYMLINKED_DOMAINS[*]}"
59+
60+
# Create an array containing domains that are considered
61+
# enabled (ie present on /app/letsencrypt_service_data).
62+
# shellcheck source=/dev/null
63+
source "$DIR"/letsencrypt_service_data
64+
for cid in "${LETSENCRYPT_CONTAINERS[@]}"; do
65+
host_varname="LETSENCRYPT_${cid}_HOST"
66+
hosts_array="${host_varname}[@]"
67+
for domain in "${!hosts_array}"; do
68+
# Add domain to the array storing currently enabled domains.
69+
ENABLED_DOMAINS+=("$domain")
70+
done
71+
done
72+
[[ $DEBUG == true ]] && echo "Enabled domains: ${ENABLED_DOMAINS[*]}"
73+
74+
# Create an array containing only domains for which a symlinked private key exists
75+
# in /etc/nginx/certs but that no longer have a corresponding LETSENCRYPT_HOST set
76+
# on an active container.
77+
if [[ ${#SYMLINKED_DOMAINS[@]} -gt 0 ]]; then
78+
mapfile -t DISABLED_DOMAINS < <(echo "${SYMLINKED_DOMAINS[@]}" \
79+
"${ENABLED_DOMAINS[@]}" \
80+
"${ENABLED_DOMAINS[@]}" \
81+
| tr ' ' '\n' | sort | uniq -u)
82+
fi
83+
[[ $DEBUG == true ]] && echo "Disabled domains: ${DISABLED_DOMAINS[*]}"
84+
85+
# Remove disabled domains symlinks if present.
86+
# Return 1 if nothing was removed and 0 otherwise.
87+
if [[ ${#DISABLED_DOMAINS[@]} -gt 0 ]]; then
88+
for disabled_domain in "${DISABLED_DOMAINS[@]}"; do
89+
for extension in .crt .key .dhparam.pem .chain.pem; do
90+
file="${disabled_domain}${extension}"
91+
if [[ -n "${file// }" ]] && [[ -f "/etc/nginx/certs/${file}" ]]; then
92+
rm -f "/etc/nginx/certs/${file}"
93+
fi
94+
done
95+
done
96+
return 0
97+
else
98+
return 1
99+
fi
100+
}
101+
45102
update_certs() {
46103

47104
check_two_containers_case && (check_nginx_proxy_container_run || return)
@@ -179,6 +236,8 @@ update_certs() {
179236
done
180237
done
181238

239+
cleanup_links && should_reload_nginx='true'
240+
182241
[[ "$should_reload_nginx" == 'true' ]] && reload_nginx
183242
}
184243

0 commit comments

Comments
 (0)