source: fedd/db_to_certs.py @ b15ecc6

compt_changes
Last change on this file since b15ecc6 was 6bedbdba, checked in by Ted Faber <faber@…>, 12 years ago

Split topdl and fedid out to different packages. Add differential
installs

  • Property mode set to 100755
File size: 2.5 KB
Line 
1#!/usr/bin/env python
2
3import os, sys
4import MySQLdb
5import tempfile
6import stat
7from optparse import OptionParser
8
9from deter import fedid
10
11class opt_parser(OptionParser):
12    def __init__(self):
13        OptionParser.__init__(self, usage="%prog [opts] (--help for details)",
14                version="0.1")
15        self.add_option('-u', '--user', dest='users', action='append',
16                default=[], help="Users to extract from DB")
17        self.add_option('-p', '--project', dest='projects', action='append',
18                default=[], help="Projects to extract from DB")
19        self.add_option('-d', '--cert-dir', dest='cert_dir', 
20                default=None, help='Directory to store copies of certificates')
21
22
23def cert_to_fid(cstr):
24    fd, path = tempfile.mkstemp('.pem')
25    try:
26        try:
27            f = os.fdopen(fd, "w")
28            print >>f, cstr
29            f.close()
30        except IOError, e:
31            print >>sys.stderr, "Error creating user %s" % u
32        return fedid(file=path)
33    finally:
34        os.remove(path)
35
36
37def add_list(l, field):
38    str = ""
39    for x in l:
40        if str: str += " OR "
41        else: str = " AND ("
42
43        str += "%s='%s'" % (field, x)
44    if str: str += ")"
45    return str
46
47
48fids = { }
49q_start = """
50SELECT
51    g.uid,
52    CASE g.gid
53        WHEN g.pid THEN g.pid
54        ELSE CONCAT(g.pid, '/', g.gid)
55    END,
56    CONCAT('-----BEGIN RSA PRIVATE KEY-----\\n',
57    s.privkey,
58    '-----END RSA PRIVATE KEY-----\\n',
59    '-----BEGIN CERTIFICATE-----\\n',
60        s.cert,
61        '-----END CERTIFICATE-----\\n'),
62    encrypted
63FROM group_membership g INNER JOIN user_sslcerts s
64    ON g.uid = s.uid
65WHERE revoked is NULL AND encrypted = 0
66"""
67q_end ="""
68ORDER BY s.uid
69"""
70
71opts, args = opt_parser().parse_args()
72
73if opts.cert_dir:
74    if not os.path.isdir(opts.cert_dir):
75        sys.exit('Not a directory: %s' % opts.cert_dir)
76    elif not os.access(opts.cert_dir, os.W_OK):
77        sys.exit('Cannot write %s' % opts.cert_dir)
78    else:
79        os.chmod(opts.cert_dir, stat.S_IRWXU)
80
81
82
83user_clause= add_list(opts.users, 'g.uid')
84project_clause= add_list(opts.projects, 'g.pid')
85
86query = q_start + user_clause + project_clause + q_end
87
88db = MySQLdb.connect(db='tbdb')
89c = db.cursor()
90c.execute(query)
91
92for u, p, c, e in c.fetchall():
93    fid = fids.get(c, None)
94
95    if not fid:
96        fid = cert_to_fid(c)
97        fids[c] = fid
98        if opts.cert_dir:
99            cf =os.path.join(opts.cert_dir, '%s.pem' % u)
100            if not os.access(cf, os.F_OK) or os.access(cf, os.W_OK):
101                try:
102                    f = open(cf, 'w')
103                    f.write(c)
104                    f.close()
105                    os.chmod(cf, stat.S_IREAD)
106                except EnvironmentError, e:
107                    print >>sys.stderr, 'Cannot write %s: %s' % \
108                            (e.filename, e.strerror)
Note: See TracBrowser for help on using the repository browser.