1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import sys |
---|
4 | import re |
---|
5 | import os |
---|
6 | |
---|
7 | from optparse import OptionParser |
---|
8 | from federation.util import abac_split_cert, abac_pem_type |
---|
9 | |
---|
10 | # Options |
---|
11 | class Parser(OptionParser): |
---|
12 | def __init__(self): |
---|
13 | OptionParser.__init__(self, usage="%prog [options] file.pem") |
---|
14 | self.add_option('--cert', dest='cert', default='./cert.pem', |
---|
15 | help='File to extract certificate into, default: [%default]') |
---|
16 | self.add_option('--key', dest='key', default='./key.pem', |
---|
17 | help='File to extract key into, default: [%default]') |
---|
18 | self.add_option('--force', action='store_true', dest='force', |
---|
19 | default=False, |
---|
20 | help=('Overwite existing certificate and key files. ' + \ |
---|
21 | 'default: [%default]')) |
---|
22 | |
---|
23 | # Option validation |
---|
24 | parser = Parser() |
---|
25 | opts, args = parser.parse_args() |
---|
26 | |
---|
27 | if len(args) == 1: |
---|
28 | combo = args[0] |
---|
29 | else: |
---|
30 | parser.print_help() |
---|
31 | sys.exit('\nMust have one file argument') |
---|
32 | |
---|
33 | for fn in (opts.cert, opts.key): |
---|
34 | if os.access(fn, os.F_OK): |
---|
35 | if opts.force: os.unlink(fn) |
---|
36 | else: sys.exit('%s exists. --force to overwite it' % fn) |
---|
37 | |
---|
38 | try: |
---|
39 | type = abac_pem_type(combo) |
---|
40 | if type == 'both': |
---|
41 | abac_split_cert(combo, opts.key, opts.cert) |
---|
42 | else: |
---|
43 | sys.exit('Cannot split %s as it is a %s' % (combo, type or 'dunno')); |
---|
44 | except EnvironmentError, e: |
---|
45 | sys.exit("%s: %s" % (e.strerror, e.filename or '?!')) |
---|