source: fedd/cert_to_fedid.py @ 62f3dd9

axis_examplecompt_changesinfo-ops
Last change on this file since 62f3dd9 was 62f3dd9, checked in by Ted Faber <faber@…>, 13 years ago

allow command line progams to expand tildes. Added a class derived from OptionParser? to make that easily available.

  • Property mode set to 100755
File size: 1.7 KB
Line 
1#!/usr/local/bin/python
2
3import sys, os
4import subprocess, tempfile
5import os.path
6
7from string import join
8from federation.util import abac_pem_type, abac_split_cert, file_expanding_opts
9
10class Parser(file_expanding_opts):
11    def __init__(self):
12        file_expanding_opts.__init__(self, usage='%prog [options]')
13        self.add_option('--out', dest='out', help='destination file',
14                action='callback', callback=self.expand_file, type='str',
15                default='./cert.pem')
16        self.add_option('--debug', dest='debug', action='store_true', 
17                default=False, help='Just print command')
18        self.add_option('--openssl', dest='openssl',
19                help='Path to openssl command', default='/usr/bin/openssl')
20
21parser = Parser()
22opts, args = parser.parse_args()
23delete_key = False
24
25if args:
26    key = args[0]
27
28ktype = abac_pem_type(key)
29if ktype == 'both':
30    key, cert = abac_split_cert(key)
31    os.unlink(cert)
32    delete_key = True
33elif ktype != 'key':
34    sys.exit('Cannot use %s as identity.  It is a %s ' % ktype + 
35        'and we were expecting a key')
36
37
38try:
39    tf, tn = tempfile.mkstemp(suffix=".pem")
40    cmd = [opts.openssl, 'req', '-new', '-nodes', '-subj', 
41            '/CN=users.isi.deterlab.net', '-x509', '-days',  '3650',
42            '-key', key, '-out', tn]
43    if opts.debug:
44        print join(cmd)
45        sys.exit(0)
46    else:
47        rv = subprocess.call(cmd)
48        if rv == 0:
49            try:
50                of = os.fdopen(os.open(opts.out, 
51                    os.O_WRONLY | os.O_CREAT, 0600), 'w')
52                for inf in (tn, key):
53                    f = open(inf, 'r')
54                    for line in f:
55                        print >>of, line,
56                    f.close()
57                of.close()
58            except EnvironmentError, e:
59                sys.exit("Cannot open %s: %s" % (efilename, e.strerror))
60        else:
61            sys.exit("%s failed: %d" % (opts.openssl, rv))
62finally:
63    if delete_key and key: os.unlink(key)
Note: See TracBrowser for help on using the repository browser.