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