-
Notifications
You must be signed in to change notification settings - Fork 4
/
renew_mit_certs.py
executable file
·106 lines (89 loc) · 3.35 KB
/
renew_mit_certs.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
#!/usr/bin/env python
from __future__ import print_function
import os
import socket
import subprocess
from datetime import datetime, timedelta
from email.mime.text import MIMEText
from paste.deploy import appconfig
import ldap
from scripts import cert
from scriptspony import vhosts
from scriptspony.config.environment import load_environment
NON_SCRIPTS_VHOSTS_ALIAS = ["sipb.mit.edu"]
def get_expiring_certs():
"""
Most of this function is from find_expiring_certs.py
"""
# load turbogears config, required for vhosts.connect to work
config = appconfig('config:' + os.path.abspath(os.path.dirname(__file__) + '/development.ini'))
load_environment(config.global_conf, config.local_conf)
now = datetime.utcnow()
vhosts.connect()
res = vhosts.conn.search_s(
"ou=VirtualHosts,dc=scripts,dc=mit,dc=edu",
ldap.SCOPE_ONELEVEL,
"(&(objectClass=scriptsVhost)(scriptsVhostCertificate=*))",
[
"scriptsVhostName", "scriptsVhostAlias", "uid",
"scriptsVhostCertificate"
],
)
expiring = []
for _, attrs in res:
vhost, = attrs["scriptsVhostName"]
aliases = attrs.get("scriptsVhostAlias", [])
uid, = attrs["uid"]
scripts, = attrs["scriptsVhostCertificate"]
chain = cert.scripts_to_chain(scripts)
expires = cert.chain_notAfter(chain) - now
if expires < timedelta(days=14):
expiring.append((expires, uid, [vhost] + aliases))
expiring.sort()
return expiring
def renew_expiring_mit_certs():
expiring = get_expiring_certs()
for _, uid, hostnames in expiring:
mit_hostnames = [
h for h in hostnames if '.' not in h or h.endswith('.mit.edu')
]
if 'mit.edu' in hostnames[0]:
try:
hostnames = request_cert(uid, mit_hostnames)
print("CSR sent for " + ", ".join(hostnames))
except (AssertionError, IOError, OSError) as err:
print("failed to send CSR for " + ", ".join(hostnames) + ": ",
err)
def request_cert(locker, hostnames):
"""
The code from send_mitcert_request.py, but as a function
"""
for i, hostname in enumerate(hostnames):
hostname = hostname.lower()
if not hostname.endswith(".mit.edu"):
hostname += ".mit.edu"
assert hostname.endswith(".mit.edu"), hostname
if hostname not in NON_SCRIPTS_VHOSTS_ALIAS:
assert socket.gethostbyname(hostname) == "18.4.86.46", hostname
hostnames[i] = hostname
hostnames = list(set(hostnames))
csr = subprocess.check_output(
["sudo", "/etc/pki/tls/gencsr-pony", locker] + hostnames)
assert csr.startswith("-----BEGIN CERTIFICATE REQUEST-----\n")
msg = MIMEText("""\
At your convenience, please sign this certificate for
{hostnames} (an alias of scripts-vhosts).
Thanks,
SIPB Scripts team
{csr}
""".format(hostnames=", ".join(hostnames), csr=csr))
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
msg["Cc"] = "[email protected]"
msg["Subject"] = "Certificate signing request for " + ", ".join(hostnames)
p = subprocess.Popen(["/usr/sbin/sendmail", "-t", "-oi"],
stdin=subprocess.PIPE)
p.communicate(msg.as_string())
return hostnames
if __name__ == "__main__":
renew_expiring_mit_certs()