source: fedd/exp_access_db.py @ 73102d1

axis_examplecompt_changesinfo-opsversion-1.30version-2.00version-3.01version-3.02
Last change on this file since 73102d1 was 73102d1, checked in by Ted Faber <faber@…>, 15 years ago

whoops!

  • Property mode set to 100755
File size: 2.2 KB
Line 
1#!/usr/local/bin/python
2
3import os, sys
4import MySQLdb
5import tempfile
6from optparse import OptionParser
7
8from fedid 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, g.pid,
55    CONCAT('-----BEGIN CERTIFICATE-----\\n',
56        s.cert,
57        '-----END CERTIFICATE-----\\n'),
58    encrypted
59FROM group_membership g INNER JOIN user_sslcerts s
60    ON g.uid = s.uid
61WHERE status = 'valid' AND g.pid = g.gid
62"""
63q_end ="""
64ORDER BY s.uid
65"""
66
67opts, args = opt_parser().parse_args()
68
69if not ( opts.project_access or opts.user_access):
70    sys.exit("No output if both --no-project-access and " +\
71            "--no-user-access are given")
72
73user_clause= add_list(opts.users, 'g.uid')
74project_clause= add_list(opts.projects, 'g.pid')
75
76query = q_start + user_clause + project_clause + q_end
77
78db = MySQLdb.connect(db='tbdb')
79c = db.cursor()
80c.execute(query)
81
82for u, p, c, e in c.fetchall():
83    fid = fids.get(c, None)
84
85    if not fid:
86        fid = cert_to_fid(c)
87        fids[c] = fid
88        if e: print "# %s (encrypted)" % u
89        else: print "# %s" % u
90        if opts.user_access:
91            print "fedid:%s->%s" % (fid, u)
92
93    if opts.project_access:
94        print "fedid:%s->(%s,%s)" % (fid, p, u)
Note: See TracBrowser for help on using the repository browser.