Debian Codegraph @

admin/cli/haproxy-dump-certs

raw
1 #!/bin/bash
2 #
3 # Dump certificates from the HAProxy stats or master socket to the filesystem
4 # Experimental script
5 #
6
7 set -e
8
9 export BASEPATH=${BASEPATH:-/etc/haproxy}/
10 export SOCKET=${SOCKET:-/var/run/haproxy-master.sock}
11 export DRY_RUN=0
12 export DEBUG=
13 export VERBOSE=
14 export M="@1 "
15 export TMP
16
17 vecho() {
18
19 [ -n "$VERBOSE" ] && echo "$@"
20 return 0
21 }
22
23 read_certificate() {
24 name=$1
25 crt_filename=
26 key_filename=
27
28 OFS=$IFS
29 IFS=":"
30
31 while read -r key value; do
32 case "$key" in
33 "Crt filename")
34 crt_filename="${value# }"
35 key_filename="${value# }"
36 ;;
37 "Key filename")
38 key_filename="${value# }"
39 ;;
40 esac
41 done < <(echo "${M}show ssl cert ${name}" | socat "${SOCKET}" -)
42 IFS=$OFS
43
44 if [ -z "$crt_filename" ] || [ -z "$key_filename" ]; then
45 return 1
46 fi
47
48 # handle fields without a crt-base/key-base
49 [ "${crt_filename:0:1}" != "/" ] && crt_filename="${BASEPATH}${crt_filename}"
50 [ "${key_filename:0:1}" != "/" ] && key_filename="${BASEPATH}${key_filename}"
51
52 vecho "name:$name"
53 vecho "crt:$crt_filename"
54 vecho "key:$key_filename"
55
56 export NAME="$name"
57 export CRT_FILENAME="$crt_filename"
58 export KEY_FILENAME="$key_filename"
59
60 return 0
61 }
62
63 cmp_certkey() {
64 prev=$1
65 new=$2
66
67 if [ ! -f "$prev" ]; then
68 return 1;
69 fi
70
71 if ! cmp -s <(openssl x509 -in "$prev" -noout -fingerprint -sha256) <(openssl x509 -in "$new" -noout -fingerprint -sha256); then
72 return 1
73 fi
74
75 return 0
76 }
77
78 dump_certificate() {
79 name=$1
80 prev_crt=$2
81 prev_key=$3
82 r="tmp.${RANDOM}"
83 d="old.$(date +%s)"
84 new_crt="$TMP/$(basename "$prev_crt").${r}"
85 new_key="$TMP/$(basename "$prev_key").${r}"
86
87 if ! touch "${new_crt}" || ! touch "${new_key}"; then
88 echo "[ALERT] ($$) : can't dump \"$name\", can't create tmp files" >&2
89 return 1
90 fi
91
92 echo "${M}dump ssl cert ${name}" | socat "${SOCKET}" - | openssl pkey >> "${new_key}"
93 # use crl2pkcs7 as a way to dump multiple x509, storeutl could be used in modern versions of openssl
94 echo "${M}dump ssl cert ${name}" | socat "${SOCKET}" - | openssl crl2pkcs7 -nocrl -certfile /dev/stdin | openssl pkcs7 -print_certs >> "${new_crt}"
95
96 if ! cmp -s <(openssl x509 -in "${new_crt}" -pubkey -noout) <(openssl pkey -in "${new_key}" -pubout); then
97 echo "[ALERT] ($$) : Private key \"${new_key}\" and public key \"${new_crt}\" don't match" >&2
98 return 1
99 fi
100
101 if cmp_certkey "${prev_crt}" "${new_crt}"; then
102 echo "[NOTICE] ($$) : ${crt_filename} is already up to date" >&2
103 return 0
104 fi
105
106 # dry run will just return before trying to move the files
107 if [ "${DRY_RUN}" != "0" ]; then
108 return 0
109 fi
110
111 # move the current certificates to ".old.timestamp"
112 if [ -f "${prev_crt}" ] && [ -f "${prev_key}" ]; then
113 mv "${prev_crt}" "${prev_crt}.${d}"
114 [ "${prev_crt}" != "${prev_key}" ] && mv "${prev_key}" "${prev_key}.${d}"
115 fi
116
117 # move the new certificates to old place
118 mv "${new_crt}" "${prev_crt}"
119 [ "${prev_crt}" != "${prev_key}" ] && mv "${new_key}" "${prev_key}"
120
121 return 0
122 }
123
124 dump_all_certificates() {
125 echo "${M}show ssl cert" | socat "${SOCKET}" - | grep -v '^#' | grep -v '^$' | while read -r line; do
126 export NAME
127 export CRT_FILENAME
128 export KEY_FILENAME
129
130 if read_certificate "$line"; then
131 dump_certificate "$NAME" "$CRT_FILENAME" "$KEY_FILENAME"
132 else
133 echo "[WARNING] ($$) : can't dump \"$name\", crt/key filename details not found in \"show ssl cert\"" >&2
134 fi
135
136 done
137 }
138
139 usage() {
140 echo "Usage:"
141 echo " $0 [options]* [cert]*"
142 echo ""
143 echo " Dump certificates from the HAProxy stats or master socket to the filesystem"
144 echo " Require socat and openssl"
145 echo " EXPERIMENTAL script, backup your files!"
146 echo " The script will move your previous files to FILE.old.unixtimestamp (ex: foo.com.pem.old.1759044998)"
147
148 echo ""
149 echo "Options:"
150 echo " -S, --master-socket <path> Use the master socket at <path> (default: ${SOCKET})"
151 echo " -s, --socket <path> Use the stats socket at <path>"
152 echo " -p, --path <path> Specifiy a base path for relative files (default: ${BASEPATH})"
153 echo " -n, --dry-run Read certificates on the socket but don't dump them"
154 echo " -d, --debug Debug mode, set -x"
155 echo " -v, --verbose Verbose mode"
156 echo " -h, --help This help"
157 echo " -- End of options"
158 echo ""
159 echo "Examples:"
160 echo " $0 -v -p ${BASEPATH} -S ${SOCKET}"
161 echo " $0 -v -p ${BASEPATH} -S ${SOCKET} bar.com.rsa.pem"
162 echo " $0 -v -p ${BASEPATH} -S ${SOCKET} -- foo.com.ecdsa.pem bar.com.rsa.pem"
163 }
164
165 main() {
166 while [ -n "$1" ]; do
167 case "$1" in
168 -S|--master-socket)
169 SOCKET="$2"
170 M="@1 "
171 shift 2
172 ;;
173 -s|--socket)
174 SOCKET="$2"
175 M=
176 shift 2
177 ;;
178 -p|--path)
179 BASEPATH="$2/"
180 shift 2
181 ;;
182 -n|--dry-run)
183 DRY_RUN=1
184 shift
185 ;;
186 -d|--debug)
187 DEBUG=1
188 shift
189 ;;
190 -v|--verbose)
191 VERBOSE=1
192 shift
193 ;;
194 -h|--help)
195 usage "$@"
196 exit 0
197 ;;
198 --)
199 shift
200 break
201 ;;
202 -*)
203 echo "[ALERT] ($$) : Unknown option '$1'" >&2
204 usage "$@"
205 exit 1
206 ;;
207 *)
208 break
209 ;;
210 esac
211 done
212
213 if [ -n "$DEBUG" ]; then
214 set -x
215 fi
216
217 TMP=${TMP:-$(mktemp -d)}
218
219 if [ -z "$1" ]; then
220 dump_all_certificates
221 else
222 # compute the certificates names at the end of the command
223 while [ -n "$1" ]; do
224 if ! read_certificate "$1"; then
225 echo "[ALERT] ($$) : can't dump \"$1\", crt/key filename details not found in \"show ssl cert\"" >&2
226 exit 1
227 fi
228 [ "${DRY_RUN}" = "0" ] && dump_certificate "$NAME" "$CRT_FILENAME" "$KEY_FILENAME"
229 shift
230 done
231 fi
232 }
233
234 trap 'rm -rf -- "$TMP"' EXIT
235 main "$@"