[f77a256] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import os, sys |
---|
| 4 | import MySQLdb |
---|
| 5 | import tempfile |
---|
| 6 | from optparse import OptionParser |
---|
| 7 | |
---|
[94a6661] | 8 | from deter import fedid |
---|
[f77a256] | 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('-d', '--cert-dir', dest='cert_dir', |
---|
| 19 | default=None, help='Directory to store copies of certificates') |
---|
| 20 | self.add_option('-t','--testbed', dest='tb', default=None, |
---|
| 21 | help='testbed from which attrs come (cert file)') |
---|
| 22 | |
---|
| 23 | def 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 | |
---|
| 37 | def add_list(l, field, prefix=''): |
---|
| 38 | str = "" |
---|
| 39 | for x in l: |
---|
| 40 | if str: str += " OR " |
---|
| 41 | else: str = " %s (" % prefix |
---|
| 42 | |
---|
| 43 | str += "%s='%s'" % (field, x) |
---|
| 44 | if str: str += ")" |
---|
| 45 | return str |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | fids = { } |
---|
| 49 | q_start = """ |
---|
| 50 | SELECT |
---|
| 51 | g.uid, |
---|
| 52 | CASE g.gid |
---|
| 53 | WHEN g.pid THEN g.pid |
---|
| 54 | ELSE CONCAT(g.pid, '/', g.gid) |
---|
| 55 | END |
---|
| 56 | FROM group_membership g |
---|
| 57 | """ |
---|
| 58 | q_end =""" |
---|
| 59 | ORDER BY g.uid |
---|
| 60 | """ |
---|
| 61 | |
---|
| 62 | opts, args = opt_parser().parse_args() |
---|
| 63 | |
---|
| 64 | if opts.users or opts.projects: q_start += '\nWHERE ' |
---|
| 65 | user_clause= add_list(opts.users, 'g.uid') |
---|
| 66 | if user_clause: prefix = 'AND' |
---|
| 67 | else: prefix = '' |
---|
| 68 | project_clause= add_list(opts.projects, 'g.pid', prefix) |
---|
| 69 | |
---|
| 70 | if opts.tb: |
---|
| 71 | try: |
---|
| 72 | tbc=fedid(file=opts.tb) |
---|
| 73 | except EnvironmentError, e: |
---|
| 74 | sys.exit('Cannot get testbed cert from %s: %s' % \ |
---|
| 75 | (e.filename, e.strerror)) |
---|
| 76 | else: |
---|
| 77 | sys.exit('need a testbed (--testbed)') |
---|
| 78 | |
---|
[94a6661] | 79 | if not opts.cert_dir: |
---|
| 80 | sys.exit('Need a certificate directory --cert-dir ') |
---|
| 81 | |
---|
[f77a256] | 82 | query = q_start + user_clause + project_clause + q_end |
---|
| 83 | |
---|
| 84 | db = MySQLdb.connect(db='tbdb') |
---|
| 85 | c = db.cursor() |
---|
| 86 | c.execute(query) |
---|
| 87 | |
---|
[41644eb] | 88 | print "# users %s" % ','.join(opts.users) |
---|
| 89 | print "# projects %s" % ','.join(opts.projects) |
---|
[f77a256] | 90 | for u, p, in c.fetchall(): |
---|
| 91 | print "(fedid:%s,%s,%s)-> access, (%s,%s,%s)" % \ |
---|
| 92 | (tbc, p, u, p, u, os.path.join(opts.cert_dir, "%s.pem" % u)) |
---|