File: haproxy-dump-certs

package info (click to toggle)
haproxy 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 24,584 kB
  • sloc: ansic: 275,085; sh: 3,607; xml: 1,756; python: 1,345; makefile: 1,162; perl: 168; cpp: 21
file content (235 lines) | stat: -rwxr-xr-x 5,574 bytes parent folder | download | duplicates (2)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
#
# Dump certificates from the HAProxy stats or master socket to the filesystem
# Experimental script
#

set -e

export BASEPATH=${BASEPATH:-/etc/haproxy}/
export SOCKET=${SOCKET:-/var/run/haproxy-master.sock}
export DRY_RUN=0
export DEBUG=
export VERBOSE=
export M="@1 "
export TMP

vecho() {

	[ -n "$VERBOSE" ] && echo "$@"
	return 0
}

read_certificate() {
	name=$1
	crt_filename=
	key_filename=

	OFS=$IFS
	IFS=":"

	while read -r key value; do
		case "$key" in
			"Crt filename")
				crt_filename="${value# }"
				key_filename="${value# }"
			;;
			"Key filename")
				key_filename="${value# }"
			;;
		esac
	done < <(echo "${M}show ssl cert ${name}" | socat "${SOCKET}" -)
	IFS=$OFS

	if [ -z "$crt_filename" ] || [ -z "$key_filename" ]; then
		return 1
	fi

	# handle fields without a crt-base/key-base
	[ "${crt_filename:0:1}" != "/" ] && crt_filename="${BASEPATH}${crt_filename}"
	[ "${key_filename:0:1}" != "/" ] && key_filename="${BASEPATH}${key_filename}"

	vecho "name:$name"
	vecho "crt:$crt_filename"
	vecho "key:$key_filename"

	export NAME="$name"
	export CRT_FILENAME="$crt_filename"
	export KEY_FILENAME="$key_filename"

	return 0
}

cmp_certkey() {
	prev=$1
	new=$2

	if [ ! -f "$prev" ]; then
		return 1;
	fi

	if ! cmp -s <(openssl x509 -in "$prev" -noout -fingerprint -sha256) <(openssl x509 -in "$new" -noout -fingerprint -sha256); then
		return 1
	fi

	return 0
}

dump_certificate() {
	name=$1
	prev_crt=$2
	prev_key=$3
	r="tmp.${RANDOM}"
	d="old.$(date +%s)"
	new_crt="$TMP/$(basename "$prev_crt").${r}"
	new_key="$TMP/$(basename "$prev_key").${r}"

	if ! touch "${new_crt}" || ! touch "${new_key}"; then
		echo "[ALERT] ($$) : can't dump \"$name\", can't create tmp files" >&2
		return 1
	fi

	echo "${M}dump ssl cert ${name}" | socat "${SOCKET}" - | openssl pkey >> "${new_key}"
	# use crl2pkcs7 as a way to dump multiple x509, storeutl could be used in modern versions of openssl
	echo "${M}dump ssl cert ${name}" | socat "${SOCKET}" - | openssl crl2pkcs7 -nocrl -certfile /dev/stdin | openssl pkcs7 -print_certs  >> "${new_crt}"

	if ! cmp -s <(openssl x509 -in "${new_crt}" -pubkey -noout) <(openssl pkey -in "${new_key}" -pubout); then
		echo "[ALERT] ($$) : Private key \"${new_key}\"  and public key \"${new_crt}\" don't match" >&2
		return 1
	fi

	if cmp_certkey "${prev_crt}" "${new_crt}"; then
		echo "[NOTICE] ($$) : ${crt_filename} is already up to date" >&2
		return 0
	fi

	# dry run will just return before trying to move the files
	if [ "${DRY_RUN}" != "0" ]; then
		return 0
	fi

	# move the current certificates to ".old.timestamp"
	if [ -f "${prev_crt}" ] && [ -f "${prev_key}" ]; then
		mv "${prev_crt}" "${prev_crt}.${d}"
		[ "${prev_crt}" != "${prev_key}" ] && mv "${prev_key}" "${prev_key}.${d}"
	fi

	# move the new certificates to old place
	mv "${new_crt}" "${prev_crt}"
	[ "${prev_crt}" != "${prev_key}" ] && mv "${new_key}" "${prev_key}"

	return 0
}

dump_all_certificates() {
	echo "${M}show ssl cert" | socat "${SOCKET}" - | grep -v '^#' | grep -v '^$' | while read -r line; do
		export NAME
		export CRT_FILENAME
		export KEY_FILENAME

		if read_certificate "$line"; then
			dump_certificate "$NAME" "$CRT_FILENAME" "$KEY_FILENAME"
		else
			echo "[WARNING] ($$) : can't dump \"$name\", crt/key filename details not found in \"show ssl cert\"" >&2
		fi

	done
}

usage() {
	echo "Usage:"
	echo " $0 [options]* [cert]*"
	echo ""
	echo " Dump certificates from the HAProxy stats or master socket to the filesystem"
	echo " Require socat and openssl"
	echo " EXPERIMENTAL script, backup your files!"
	echo " The script will move your previous files to FILE.old.unixtimestamp (ex: foo.com.pem.old.1759044998)"

	echo ""
	echo "Options:"
	echo "  -S, --master-socket <path>   Use the master socket at <path> (default: ${SOCKET})"
	echo "  -s, --socket <path>          Use the stats socket at <path>"
	echo "  -p, --path <path>            Specifiy a base path for relative files (default: ${BASEPATH})"
	echo "  -n, --dry-run                Read certificates on the socket but don't dump them"
	echo "  -d, --debug                  Debug mode, set -x"
	echo "  -v, --verbose                Verbose mode"
	echo "  -h, --help                   This help"
	echo "  --                           End of options"
	echo ""
	echo "Examples:"
	echo "  $0 -v -p ${BASEPATH} -S ${SOCKET}"
	echo "  $0 -v -p ${BASEPATH} -S ${SOCKET} bar.com.rsa.pem"
	echo "  $0 -v -p ${BASEPATH} -S ${SOCKET} -- foo.com.ecdsa.pem bar.com.rsa.pem"
}

main() {
	while [ -n "$1" ]; do
		case "$1" in
			-S|--master-socket)
				SOCKET="$2"
				M="@1 "
				shift 2
				;;
			-s|--socket)
				SOCKET="$2"
				M=
				shift 2
				;;
			-p|--path)
				BASEPATH="$2/"
				shift 2
				;;
			-n|--dry-run)
				DRY_RUN=1
				shift
				;;
			-d|--debug)
				DEBUG=1
				shift
				;;
			-v|--verbose)
				VERBOSE=1
				shift
				;;
			-h|--help)
				usage "$@"
				exit 0
				;;
			--)
				shift
				break
				;;
			-*)
				echo "[ALERT] ($$) : Unknown option '$1'" >&2
				usage "$@"
				exit 1
				;;
			*)
				break
				;;
		esac
	done

	if [ -n "$DEBUG" ]; then
		set -x
	fi

	TMP=${TMP:-$(mktemp -d)}

	if [ -z "$1" ]; then
		dump_all_certificates
	else
		# compute the certificates names at the end of the command
		while [ -n "$1" ]; do
			if ! read_certificate "$1"; then
				echo "[ALERT] ($$) : can't dump \"$1\", crt/key filename details not found in \"show ssl cert\"" >&2
				exit 1
			fi
			[ "${DRY_RUN}" = "0" ] && dump_certificate "$NAME" "$CRT_FILENAME" "$KEY_FILENAME"
			shift
		done
	fi
}

trap 'rm -rf -- "$TMP"' EXIT
main "$@"