source: fedd/cert_to_fedid.py @ 2d601b7

compt_changesinfo-ops
Last change on this file since 2d601b7 was 2d601b7, checked in by Ted Faber <faber@…>, 13 years ago

Add an option to specify a CN directly

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