-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreplace_ca.py
executable file
·72 lines (60 loc) · 2.03 KB
/
replace_ca.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
#!/usr/bin/env python
from __future__ import print_function
from datetime import datetime, timedelta
import ldap
from OpenSSL import crypto
from scripts import cert, log, auth
from scriptspony import vhosts
import os
import sys
import logging
def pkey_to_pem(pk):
return crypto.dump_publickey(crypto.FILETYPE_PEM, pk)
@log.exceptions
def main():
pem = sys.stdin.read()
replacement_certs = cert.pem_to_certs(pem)
replacements = {pkey_to_pem(c.get_pubkey()): c for c in replacement_certs}
logging.info("Replacement certificates: %s", replacements)
vhosts.connect()
res = vhosts.conn.search_s(
"ou=VirtualHosts,dc=scripts,dc=mit,dc=edu",
ldap.SCOPE_ONELEVEL,
"(&(objectClass=scriptsVhost)(scriptsVhostCertificate=*))",
["scriptsVhostName", "scriptsVhostCertificate"],
)
for dn, attrs in res:
replace = 0
vhost, = attrs["scriptsVhostName"]
logging.info("Examining %s", vhost)
scripts, = attrs["scriptsVhostCertificate"]
chain = cert.scripts_to_chain(scripts)
for i, c in enumerate(chain):
new = replacements.get(pkey_to_pem(c.get_pubkey()))
if new:
chain[i] = new
replace += 1
if replace:
logging.info(
"Replacing %d certificates for %s"
% (replace, vhost)
)
try:
vhosts.conn.modify_s(
dn,
[
(
ldap.MOD_REPLACE,
"scriptsVhostCertificate",
cert.chain_to_scripts(chain),
),
],
)
except ldap.INSUFFICIENT_ACCESS as e:
logging.exception(e)
if __name__ == "__main__":
auth.set_user_from_parent_process()
from paste.deploy import loadapp
loadapp("config:development.ini", relative_to=os.path.dirname(__file__))
logging.basicConfig(level=logging.DEBUG)
main()