f37 -> f39
[infrastructure.git] / scripts / get_genica_bundle.py
1 #!/usr/bin/env python
2 import os
3 import re
4 import urllib2
5 import subprocess
6
7 BUNDLE_URL="https://www.emulab.net/genica.bundle"
8
9 def get_bundle():
10     u = urllib2.urlopen(BUNDLE_URL)
11     upath = urllib2.urlparse.urlsplit(u.url)[2]
12     bundle_fname = os.path.basename(upath)
13     open(bundle_fname, 'w').write(u.read())
14     print "Downloaded:", bundle_fname
15     return bundle_fname
16
17 def get_CN(cert):
18     p = subprocess.Popen(['openssl', 'x509', '-issuer'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
19     p.stdin.write(cert)
20     m = re.search("(CN=)(.*)([\/ ])", p.stdout.readline())
21     if m:
22         return m.group(2)
23
24 def write_cert(cert):
25     cn = get_CN(cert)
26     if not cn: return
27     fname = '%s.gid' % cn
28     open(fname, 'w').write(cert)
29     print "Wrote:", fname
30
31 def extract(fname):
32     bundle = open(fname, 'r')
33     in_cert = False
34     cert = ""
35     for line in bundle:
36         if line.startswith("-----BEGIN CERTIFICATE-----"):
37             in_cert = True
38         elif line.startswith("-----END CERTIFICATE-----"):
39             in_cert = False
40             cert += line
41             write_cert(cert)
42             cert = ""
43
44         if in_cert:
45             cert += line
46
47 if __name__ == "__main__":
48     bundle = get_bundle()
49     extract(bundle)