forked from w2c/letsencrypt-esxi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renew.sh
108 lines (90 loc) · 3.69 KB
/
renew.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/sh
#
# Copyright (c) Johannes Feichtner <[email protected]>
# Released under the GNU GPLv3 License.
DOMAIN=$(hostname -f)
LOCALDIR=$(dirname "$(readlink -f "$0")")
LOCALSCRIPT=$(basename "$0")
ACMEDIR="$LOCALDIR/.well-known/acme-challenge"
DIRECTORY_URL="https://acme-v02.api.letsencrypt.org/directory"
SSL_CERT_FILE="$LOCALDIR/ca-certificates.crt"
RENEW_DAYS=30
ACCOUNTKEY="esxi_account.key"
KEY="esxi.key"
CSR="esxi.csr"
CRT="esxi.crt"
VMWARE_CRT="/etc/vmware/ssl/rui.crt"
VMWARE_KEY="/etc/vmware/ssl/rui.key"
if [ -r "$LOCALDIR/renew.cfg" ]; then
. "$LOCALDIR/renew.cfg"
fi
log() {
echo "$@"
logger -p daemon.info -t "$0" "$@"
}
log "Starting certificate renewal.";
# Preparation steps
if [ -z "$DOMAIN" ] || [ "$DOMAIN" = "${DOMAIN/.}" ]; then
log "Error: Hostname ${DOMAIN} is no FQDN."
exit
fi
# Add a cronjob for auto renewal. The script is run once a week on Sunday at 00:00
if ! grep -q "$LOCALDIR/$LOCALSCRIPT" /var/spool/cron/crontabs/root; then
kill -sighup "$(pidof crond)" 2>/dev/null
echo "0 0 * * 0 /bin/sh $LOCALDIR/$LOCALSCRIPT" >> /var/spool/cron/crontabs/root
crond
fi
# Check issuer and expiration date of existing cert
if [ -e "$VMWARE_CRT" ]; then
# If the cert is issued for a different hostname, request a new one
SAN=$(openssl x509 -in "$VMWARE_CRT" -text -noout | grep DNS: | sed 's/DNS://g' | xargs)
if [ "$SAN" != "$DOMAIN" ] ; then
log "Existing cert issued for ${SAN} but current domain name is ${DOMAIN}. Requesting a new one!"
# If the cert is issued by Let's Encrypt, check its expiration date, otherwise request a new one
elif openssl x509 -in "$VMWARE_CRT" -issuer -noout | grep -q "O=Let's Encrypt"; then
CERT_VALID=$(openssl x509 -enddate -noout -in "$VMWARE_CRT" | cut -d= -f2-)
log "Existing Let's Encrypt cert valid until: ${CERT_VALID}"
if openssl x509 -checkend $((RENEW_DAYS * 86400)) -noout -in "$VMWARE_CRT"; then
log "=> Longer than ${RENEW_DAYS} days. Aborting."
exit
else
log "=> Less than ${RENEW_DAYS} days. Renewing!"
fi
else
log "Existing cert for ${DOMAIN} not issued by Let's Encrypt. Requesting a new one!"
fi
fi
cd "$LOCALDIR" || exit
mkdir -p "$ACMEDIR"
# Route /.well-known/acme-challenge to port 8120
if ! grep -q "acme-challenge" /etc/vmware/rhttpproxy/endpoints.conf; then
echo "/.well-known/acme-challenge local 8120 redirect allow" >> /etc/vmware/rhttpproxy/endpoints.conf
/etc/init.d/rhttpproxy restart
fi
# Cert Request
[ ! -r "$ACCOUNTKEY" ] && openssl genrsa 4096 > "$ACCOUNTKEY"
openssl genrsa -out "$KEY" 4096
openssl req -new -sha256 -key "$KEY" -subj "/CN=$DOMAIN" -config "./openssl.cnf" > "$CSR"
chmod 0400 "$ACCOUNTKEY" "$KEY"
# Start HTTP server on port 8120 for HTTP validation
esxcli network firewall ruleset set -e true -r httpClient
python -m "http.server" 8120 &
HTTP_SERVER_PID=$!
# Retrieve the certificate
export SSL_CERT_FILE
CERT=$(python ./acme_tiny.py --account-key "$ACCOUNTKEY" --csr "$CSR" --acme-dir "$ACMEDIR" --directory-url "$DIRECTORY_URL")
kill -9 "$HTTP_SERVER_PID"
# If an error occurred during certificate issuance, $CERT will be empty
if [ -n "$CERT" ] ; then
echo "$CERT" > "$CRT"
# Provide the certificate to ESXi
cp -p "$LOCALDIR/$KEY" "$VMWARE_KEY"
cp -p "$LOCALDIR/$CRT" "$VMWARE_CRT"
log "Success: Obtained and installed a certificate from Let's Encrypt."
elif openssl x509 -checkend 86400 -noout -in "$VMWARE_CRT"; then
log "Warning: No cert obtained from Let's Encrypt. Keeping the existing one as it is still valid."
else
log "Error: No cert obtained from Let's Encrypt. Generating a self-signed certificate."
/sbin/generate-certificates
fi
for s in /etc/init.d/*; do if $s | grep ssl_reset > /dev/null; then $s ssl_reset; fi; done