1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import os, sys |
---|
4 | import MySQLdb |
---|
5 | import tempfile |
---|
6 | from optparse import OptionParser |
---|
7 | |
---|
8 | from federation import fedid |
---|
9 | |
---|
10 | class 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 | |
---|
26 | def 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 | |
---|
40 | def 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 | |
---|
51 | fids = { } |
---|
52 | q_start = """ |
---|
53 | SELECT |
---|
54 | g.uid, g.pid, |
---|
55 | CONCAT('-----BEGIN CERTIFICATE-----\\n', |
---|
56 | s.cert, |
---|
57 | '-----END CERTIFICATE-----\\n'), |
---|
58 | encrypted |
---|
59 | FROM group_membership g INNER JOIN user_sslcerts s |
---|
60 | ON g.uid = s.uid |
---|
61 | WHERE status = 'valid' AND g.pid = g.gid |
---|
62 | """ |
---|
63 | q_end =""" |
---|
64 | ORDER BY s.uid |
---|
65 | """ |
---|
66 | |
---|
67 | opts, args = opt_parser().parse_args() |
---|
68 | |
---|
69 | if 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 | |
---|
73 | user_clause= add_list(opts.users, 'g.uid') |
---|
74 | project_clause= add_list(opts.projects, 'g.pid') |
---|
75 | |
---|
76 | query = q_start + user_clause + project_clause + q_end |
---|
77 | |
---|
78 | db = MySQLdb.connect(db='tbdb') |
---|
79 | c = db.cursor() |
---|
80 | c.execute(query) |
---|
81 | |
---|
82 | for 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) |
---|