[2e46f35] | 1 | #!/usr/bin/env python |
---|
[87c0fc1] | 2 | |
---|
| 3 | import sys |
---|
| 4 | import re |
---|
| 5 | import os |
---|
| 6 | |
---|
[62f3dd9] | 7 | from federation.util import abac_split_cert, abac_pem_type, file_expanding_opts |
---|
[87c0fc1] | 8 | |
---|
| 9 | # Options |
---|
[62f3dd9] | 10 | class Parser(file_expanding_opts): |
---|
[87c0fc1] | 11 | def __init__(self): |
---|
[62f3dd9] | 12 | file_expanding_opts.__init__(self, usage="%prog [options] file.pem") |
---|
[87c0fc1] | 13 | self.add_option('--cert', dest='cert', default='./cert.pem', |
---|
[62f3dd9] | 14 | action='callback', callback=self.expand_file, type='str', |
---|
[87c0fc1] | 15 | help='File to extract certificate into, default: [%default]') |
---|
| 16 | self.add_option('--key', dest='key', default='./key.pem', |
---|
[62f3dd9] | 17 | action='callback', callback=self.expand_file, type='str', |
---|
[87c0fc1] | 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 | |
---|
[353db8c] | 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) |
---|
[87c0fc1] | 38 | |
---|
| 39 | try: |
---|
[353db8c] | 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')); |
---|
[87c0fc1] | 45 | except EnvironmentError, e: |
---|
| 46 | sys.exit("%s: %s" % (e.strerror, e.filename or '?!')) |
---|