-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert.sh
81 lines (76 loc) · 1.72 KB
/
cert.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
#!/bin/bash
POSITIONAL_ARGS=()
LIFETIME=45 # set default lifetime of 45 days
while [[ $# -gt 0 ]]; do
case $1 in
-c|--certfile)
CERTFILE="$2"
shift # past argument=value
shift # past vaule
;;
-k|--keyfile)
KEYFILE="$2"
shift # past argument
shift # past vaule
;;
-l|--lifetime)
LIFETIME="${2}"
shift
shift
;;
-d|--debug)
DEBUG="YES"
shift # past argument
;;
-p|--pem)
PEM="$2"
shift
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=($1)
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
#debug
LIFELEFT=$((LIFETIME * 86400))
if [ -x $(command -v tailscale) ] && [ -x $(command -v jq ) ];
then
CERTCN=$(tailscale status --self --peers=false --json | jq -r .CertDomains[]);
else
echo "Tailscale or jq not found or not in path";
exit 1
fi
if [ -z "$CERTFILE" ]; then
CERTFILE="${CERTCN}.crt"
fi
if [ -z "$KEYFILE" ]; then
KEYFILE="${CERTCN}.key"
fi
if [[ $DEBUG ]];
then
echo "Certfile = ${CERTFILE}"
echo "KEYFILE = ${KEYFILE}"
echo "LIFETIME = ${LIFETIME}"
echo "LIFELEFT = ${LIFELEFT}"
echo "PEM = ${PEM}"
fi
# script
if openssl x509 -in "${CERTFILE}" -checkend ${LIFELEFT} &>/dev/null;
then
echo "Certificate for ${CERTCN} is valid for ${LIFETIME} days or more."
exit 0
else
echo "Renewing Cert for ${CERTCN}";
tailscale cert --cert-file "${CERTFILE}" --key-file "${KEYFILE}" ${CERTCN}
fi
if [ -n "$PEM" ]; then
echo "Combine ${CERTFILE} and ${KEYFILE} into ${PEM}"
cat ${CERTFILE} ${KEYFILE} >> ${PEM}
fi