source: fedd/deter_exp_access_db.py @ b213b53

Last change on this file since b213b53 was 41644eb, checked in by Ted Faber <faber@…>, 12 years ago

Add comments to the output about how the file was generated

  • Property mode set to 100755
File size: 2.2 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('-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
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, 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
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
56FROM group_membership g
57"""
58q_end ="""
59ORDER BY g.uid
60"""
61
62opts, args = opt_parser().parse_args()
63
64if opts.users or opts.projects: q_start += '\nWHERE '
65user_clause= add_list(opts.users, 'g.uid')
66if user_clause: prefix = 'AND'
67else: prefix = ''
68project_clause= add_list(opts.projects, 'g.pid', prefix)
69
70if 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))
76else:
77    sys.exit('need a testbed (--testbed)')
78
79if not opts.cert_dir:
80    sys.exit('Need a certificate directory --cert-dir ')
81
82query = q_start + user_clause + project_clause + q_end
83
84db = MySQLdb.connect(db='tbdb')
85c = db.cursor()
86c.execute(query)
87
88print "# users %s" % ','.join(opts.users)
89print "# projects %s" % ','.join(opts.projects)
90for 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))
Note: See TracBrowser for help on using the repository browser.