forked from gregtwallace/certificate-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lego-adguard.sh
65 lines (51 loc) · 2.13 KB
/
lego-adguard.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
#!/bin/bash
# Script updates AdGuard certchain and key. It also restarts
# the AdGuard service.
# This script should be securely placed with limited access
# (e.g. owned by root with permissions of 700) to avoid
# compromising the API Keys
# ECDSA Keys ARE supported
## Recommended cron -- run at boot (in case system was powered off
# during a renewal, and run weekly)
# Pick any time you like. This time was arbitrarily selected.
# sudo crontab -e
# @reboot sleep 15 && /script/path/here
# 5 4 * * 2 /script/path/here
## Set VARs in accord with environment
cert_apikey=<cert API key>
key_apikey=<key API key>
# server hosting key/cert
server=certdp.local:port
# name of the key/cert (as it is on server)
cert_name=adguard.example.com
# URL paths
api_cert_path=legocerthub/api/v1/download/certificates/$cert_name
api_key_path=legocerthub/api/v1/download/privatekeys/$cert_name
# local user who will own certs
cert_owner=root
# local cert storage
local_certs=/opt/AdGuardHome/certs
# path to store a timestamp to easily see when script last ran
time_stamp=/opt/AdGuardHome/certs/cert_timestamp.txt
# temp folder
temp_certs=/tmp/tempcerts
## Script
sudo rm -rf $temp_certs
sudo mkdir $temp_certs
sudo mkdir -p $local_certs
# Fetch certs, if curl returns anything other than 200 success, abort
http_statuscode=$(sudo curl https://$server/$api_cert_path -H "apiKey: $cert_apikey" --out $temp_certs/certchain.pem --write-out "%{http_code}")
if test $http_statuscode -ne 200; then exit "$http_statuscode"; fi
http_statuscode=$(sudo curl https://$server/$api_key_path -H "apiKey: $key_apikey" --out $temp_certs/key.pem --write-out "%{http_code}")
if test $http_statuscode -ne 200; then exit "$http_statuscode"; fi
# if different
if ( ! cmp -s "$temp_certs/certchain.pem" "$local_certs/certchain.pem" ) || ( ! cmp -s "$temp_certs/key.pem" "$local_certs/key.pem" ) ; then
sudo service AdGuardHome stop
sudo cp -rf $temp_certs/* $local_certs/
sudo chown $cert_owner:$cert_owner $local_certs/*
sudo chmod 600 $local_certs/key.pem
sudo chmod 644 $local_certs/certchain.pem
sudo service AdGuardHome start
fi
sudo rm -rf $local_certs/temp
echo "Last Run: $(date)" > $time_stamp