-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenew_cert.py
executable file
·358 lines (305 loc) · 11.1 KB
/
renew_cert.py
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
# Acquires or renews a single certificate via Let's Encrypt and writes it out as a HAProxy compatible certificate.
# First the SSL certificate is checked for validity >= <expiry-days>. If it's still valid for more than <expiry-days>, we exit early.
# --force overrides that behaviour so the check isn't performed
# Uses the standalone webserver configuration in certbot, spawning at port 8888.
# WARNING: HAProxy has to be configured to redirect HTTP traffic to /.well-known/acme-challenge/ to the certbot standalone webserver
import os
import sys
import argparse
import subprocess
import logging
SECONDS_PER_DAY = 86400
# default values
ARG_SILENT = False
ARG_FORCE = False
ARG_NO_RELOAD = False
ARG_EXPIRY_DAYS = 30
ARG_CERT_DIR = "/etc/ssl/private"
ARG_LE_CERT_DIR = "/etc/letsencrypt/live"
ARG_CHALLENGE = "--standalone"
ARG_CERTBOT_ARGS = "--preferred-challenges http --http-01-port 8888"
ARG_SYSTEMD_UNIT = "haproxy.service"
ARG_NO_CONCAT = False
ARG_CERT_OWNER = None
ARG_CERT_GROUP = None
def main():
abs_folder_path = os.path.abspath(os.path.dirname(__file__))
parser = argparse.ArgumentParser(
description="Acquire/renew Let's Encrypt certificates via certbot",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-s",
"--silent",
action="store_true",
help="Suppress non-essential logging (so that it runs silently in a cron job)",
required=False,
default=ARG_SILENT,
)
parser.add_argument(
"-f",
"--force",
action="store_true",
help="Force a renewal even if cert is valid for longer than <expiry-days>",
required=False,
default=ARG_FORCE,
)
parser.add_argument(
"--no-reload",
action="store_true",
help="Don't reload systemd service after acquiring cert.",
required=False,
default=ARG_NO_RELOAD,
)
parser.add_argument(
"-m",
"--mail",
type=str,
help="Mail address to use for Let's Encrypt",
required=True,
)
parser.add_argument(
"-d", "--domain", type=str, help="Domain to renew cert for", required=True
)
parser.add_argument(
"--expiry-days",
type=int,
help="Minimum validity days before cert is renewed.",
required=False,
default=ARG_EXPIRY_DAYS,
)
parser.add_argument(
"--cert-dir",
type=str,
help="Base directory for certificates to be concatenated/written into.",
default=ARG_CERT_DIR,
required=False,
)
parser.add_argument(
"--le-cert-dir",
type=str,
help="Let's Encrypt base directory for acquired certificates.",
default=ARG_LE_CERT_DIR,
required=False,
)
parser.add_argument(
"--challenge",
type=str,
help="ACME challenge to perform (certbot arg).",
default=ARG_CHALLENGE,
)
parser.add_argument(
"--certbot-args",
type=str,
help="Additional args to pass to certbot, formatted as one string.",
default=ARG_CERTBOT_ARGS,
)
parser.add_argument(
"--systemd-unit",
type=str,
help="systemd unit to reload after running renewal (as long as --no-reload isn't passed)",
default=ARG_SYSTEMD_UNIT,
)
parser.add_argument(
"--no-concat",
help="Don't concatenate fullchain and key into one file, just copy to a subdirectory by the cert name",
action="store_true",
default=ARG_NO_CONCAT,
)
parser.add_argument(
"--cert-owner",
type=str,
help="Owner for the target certificate(s)",
default=ARG_CERT_OWNER,
)
parser.add_argument(
"--cert-group",
type=str,
help="Group for the target certificate(s)",
default=ARG_CERT_GROUP,
)
args = parser.parse_args()
run_renewal(**vars(args))
def reload_systemd_unit(unit_name: str):
logging.getLogger().debug(f"Reloading systemd service '{unit_name}'")
systemctl_result = subprocess.run(
["systemctl", "reload", unit_name], capture_output=True
)
systemctl_result.check_returncode()
def copy_certs(
certpath_target_root: str,
domain: str,
cert_owner: str,
cert_group: str,
certpath_le_fullchain: str,
certpath_le_privkey: str,
):
logger = logging.getLogger()
certpath_target_dir = os.path.join(certpath_target_root.rstrip("/"), domain)
from shutil import copyfile, chown
logger.info(
f"Copying generated fullchain and privkey to output folder '{certpath_target_dir}'"
)
if not os.path.isdir(certpath_target_dir):
logger.info(f"Folder '{certpath_target_dir}' doesn't exist yet, creating it")
os.mkdir(certpath_target_dir)
if cert_owner:
logger.debug(
f"Setting '{certpath_target_dir}' ownership to {cert_owner}{':' + cert_group if cert_group else ''}"
)
chown(certpath_target_dir, cert_owner, cert_group)
certpath_fullchain_target = os.path.join(certpath_target_dir, "fullchain.pem")
logger.debug(f"Copying '{certpath_le_fullchain}' to '{certpath_fullchain_target}'")
copyfile(certpath_le_fullchain, certpath_fullchain_target)
certpath_privkey_target = os.path.join(certpath_target_dir, "privkey.pem")
logger.debug(f"Copying '{certpath_le_privkey}' to '{certpath_privkey_target}'")
copyfile(certpath_le_privkey, certpath_privkey_target)
if cert_owner:
logger.debug(
f"Setting file ownership to {cert_owner}{':' + cert_group if cert_group else ''}"
)
chown(certpath_fullchain_target, cert_owner, cert_group)
chown(certpath_privkey_target, cert_owner, cert_group)
os.chmod(certpath_privkey_target, 0o600)
def concat_certs(
certpath_target_root: str,
domain: str,
cert_owner: str,
cert_group: str,
certpath_le_fullchain: str,
certpath_le_privkey: str,
):
logger = logging.getLogger()
certpath_target = os.path.join(certpath_target_root.rstrip("/"), f"{domain}.pem")
buf_fullchain: bytes = None
buf_privkey: bytes = None
logger.debug(
f"Concatenating '{certpath_le_fullchain}' and '{certpath_le_privkey}' to '{certpath_target}'"
)
# concatenate cert files for haproxy, this is required!
with open(certpath_le_fullchain, "rb") as f:
buf_fullchain = f.read()
with open(certpath_le_privkey, "rb") as f:
buf_privkey = f.read()
with open(certpath_target, "wb") as f:
written_bytes = f.write(buf_fullchain)
written_bytes += f.write(buf_privkey)
logger.debug(f"Wrote {written_bytes} byte to '{certpath_target}'")
if cert_owner:
from shutil import chown
logger.debug(
f"Setting '{certpath_target}' ownership to {cert_owner}{':' + cert_group if cert_group else ''}"
)
chown(certpath_target, cert_owner, cert_group)
def run_renewal(
mail: str,
domain: str,
silent: bool = ARG_SILENT,
force: bool = ARG_FORCE,
no_reload: bool = ARG_NO_RELOAD,
expiry_days: int = ARG_EXPIRY_DAYS,
cert_dir: str = ARG_CERT_DIR,
le_cert_dir: str = ARG_LE_CERT_DIR,
challenge: str = ARG_CHALLENGE,
certbot_args: str = ARG_CERTBOT_ARGS,
systemd_unit: str = ARG_SYSTEMD_UNIT,
no_concat: bool = ARG_NO_CONCAT,
cert_owner: str = ARG_CERT_OWNER,
cert_group: str = ARG_CERT_GROUP,
):
try:
logging.basicConfig(
format="[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s",
level=logging.DEBUG,
)
logger = logging.getLogger()
# Check if being run as root
if not os.geteuid() == 0:
logger.error("You need to execute this script as root")
sys.exit(3)
if silent:
logger.setLevel(logging.WARN)
logger.debug(f"Renewing cert for '{domain}'")
expiry_seconds = expiry_days * SECONDS_PER_DAY
le_certpath = os.path.join(le_cert_dir.rstrip("/"), domain, "fullchain.pem")
skip_certbot = False
assert isinstance(expiry_seconds, int)
# check whether the cert is valid for longer than the given expiry time (has to be given in seconds to openssl)
openssl_args = [
"openssl",
"x509",
"-checkend",
str(expiry_seconds),
"-noout",
"-in",
le_certpath,
]
logger.debug(f"Executing command {openssl_args}")
openssl_result = subprocess.run(openssl_args, capture_output=True)
logger.debug(f"stdout=\n{openssl_result.stdout.decode()}")
logger.debug(f"stderr=\n{openssl_result.stderr.decode()}")
try:
# if this returned with an exit code of 0, the cert is still valid. Nothing to do for now.
openssl_result.check_returncode()
skip_certbot = True
except subprocess.CalledProcessError as e:
pass
# only run certbot if forced or not skipped
if force or not skip_certbot:
certbot_args = [
"certbot",
"certonly",
"--force-renewal",
"--email",
mail,
"-d",
domain,
"--agree-tos",
"--no-eff-email",
challenge,
*(certbot_args.split(" ")),
]
logger.debug(f"Executing command {certbot_args}")
certbot_result = subprocess.run(certbot_args, capture_output=True)
certbot_result.check_returncode()
logger.debug(f"stdout=\n{certbot_result.stdout.decode()}")
logger.debug(f"stderr=\n{certbot_result.stderr.decode()}")
certpath_le_fullchain = os.path.join(
le_cert_dir.rstrip("/"), domain, "fullchain.pem"
)
certpath_le_privkey = os.path.join(
le_cert_dir.rstrip("/"), domain, "privkey.pem"
)
if no_concat:
copy_certs(
certpath_target_root=cert_dir,
domain=domain,
cert_owner=cert_owner,
cert_group=cert_group,
certpath_le_fullchain=certpath_le_fullchain,
certpath_le_privkey=certpath_le_privkey,
)
else:
concat_certs(
certpath_target_root=cert_dir,
domain=domain,
cert_owner=cert_owner,
cert_group=cert_group,
certpath_le_fullchain=certpath_le_fullchain,
certpath_le_privkey=certpath_le_privkey,
)
# Reload HAProxy service
if not no_reload: # double negatives are terrible
reload_systemd_unit(systemd_unit)
except subprocess.CalledProcessError as e:
logger.exception("Error while executing subprocess")
# echo out stdout and stderr from the failed command
sys.stdout.write(e.stdout.decode())
sys.stderr.write(e.stderr.decode())
sys.exit(2)
except Exception as e:
logger.exception("Generic exception occured")
sys.exit(1)
if __name__ == "__main__":
main()