Merge branch 'master' of git+ssh://git.cacert.org/var/lib/git/cacert-infradocs
[cacert-infradocs.git] / tools / ssh_host_keys.py
1 #!/usr/bin/env python
2
3 from glob import glob
4 import argparse
5 import os.path
6 import subprocess
7
8
9 SUPPORTED_SSH_KEYTYPES = ('RSA', 'DSA', 'ECDSA', 'ED25519')
10
11
12 if __name__ == '__main__':
13 parser = argparse.ArgumentParser(
14 description=(
15 'Convert a set of ssh host keys to the syntax expected by the '
16 'sshkeys directive of the CAcert infrastructur documentation'))
17 parser.add_argument(
18 'root', metavar='ROOT', type=str, help='root directory'
19 )
20 args = parser.parse_args()
21
22 keys = {}
23 for host_key in glob(os.path.join(
24 args.root, 'etc/ssh', 'ssh_host_*key.pub')
25 ):
26 fp = subprocess.check_output(
27 ['ssh-keygen', '-l', '-f', host_key]).strip().split()
28 keys[fp[3][1:-1]] = fp[1]
29
30 maxlen = max([len(key) for key in keys.keys() if key in SUPPORTED_SSH_KEYTYPES])
31
32 print ".. sshkeys::"
33 for typ, key in [
34 (typ, keys[typ]) for typ in SUPPORTED_SSH_KEYTYPES
35 if typ in keys
36 ]:
37 print " :%s:%s %s" % (typ, ' ' * (maxlen - len(typ)), key)