source: fedd/exp_access_db.py @ 661e857

Last change on this file since 661e857 was 406f3b5, checked in by Ted Faber <faber@…>, 12 years ago

fedid moved

  • Property mode set to 100755
File size: 2.3 KB
Line 
1#!/usr/bin/env python
2
3import os, sys
4import MySQLdb
5import tempfile
6from optparse import OptionParser
7
8from deter import fedid
9
10class opt_parser(OptionParser):
11    def __init__(self):
12        OptionParser.__init__(self, usage="%prog [opts] (--help for details)",
13                version="0.1")
14        self.add_option('-u', '--user', dest='users', action='append',
15                default=[], help="Users to extract from DB")
16        self.add_option('-p', '--project', dest='projects', action='append',
17                default=[], help="Projects to extract from DB")
18        self.add_option('-U', '--no-user-access', dest='user_access',
19                default=True, action='store_false',
20                help='do not output a user-only access entry')
21        self.add_option('-P', '--no-project-access', dest='project_access',
22                default=True, action='store_false',
23                help='do not output project-based access entries')
24
25
26def cert_to_fid(cstr):
27    fd, path = tempfile.mkstemp('.pem')
28    try:
29        try:
30            f = os.fdopen(fd, "w")
31            print >>f, cstr
32            f.close()
33        except IOError, e:
34            print >>sys.stderr, "Error creating user %s" % u
35        return fedid(file=path)
36    finally:
37        os.remove(path)
38
39
40def add_list(l, field):
41    str = ""
42    for x in l:
43        if str: str += " OR "
44        else: str = " AND ("
45
46        str += "%s='%s'" % (field, x)
47    if str: str += ")"
48    return str
49
50
51fids = { }
52q_start = """
53SELECT
54    g.uid,
55    CASE g.gid
56        WHEN g.pid THEN g.pid
57        ELSE CONCAT(g.pid, '/', g.gid)
58    END,
59    CONCAT('-----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
66"""
67q_end ="""
68ORDER BY s.uid
69"""
70
71opts, args = opt_parser().parse_args()
72
73if not ( opts.project_access or opts.user_access):
74    sys.exit("No output if both --no-project-access and " +\
75            "--no-user-access are given")
76
77user_clause= add_list(opts.users, 'g.uid')
78project_clause= add_list(opts.projects, 'g.pid')
79
80query = q_start + user_clause + project_clause + q_end
81
82db = MySQLdb.connect(db='tbdb')
83c = db.cursor()
84c.execute(query)
85
86for u, p, c, e in c.fetchall():
87    fid = fids.get(c, None)
88
89    if not fid:
90        fid = cert_to_fid(c)
91        fids[c] = fid
92        if e: print "# %s (encrypted)" % u
93        else: print "# %s" % u
94        if opts.user_access:
95            print "fedid:%s->%s" % (fid, u)
96
97    if opts.project_access:
98        print "fedid:%s->(%s,%s)" % (fid, p, u)
Note: See TracBrowser for help on using the repository browser.