]> git.decadent.org.uk Git - dak.git/blob - dak/acl.py
Merge remote-tracking branch 'jcristau/cs-set-log-suite'
[dak.git] / dak / acl.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import apt_pkg
20 import sys
21
22 from daklib.config import Config
23 from daklib.dbconn import DBConn, Fingerprint, Keyring, Uid, ACL
24
25 def usage():
26     print """Usage:
27   dak acl set-fingerprints <acl-name>
28   dak acl export-per-source <acl-name>
29
30   set-fingerprints:
31     Reads list of fingerprints from stdin and sets the ACL <acl-name> to these.
32     Accepted input formats are "uid:<uid>", "name:<name>" and
33     "fpr:<fingerprint>".
34
35   export-per-source:
36     Export per source upload rights for ACL <acl-name>.
37 """
38
39 def get_fingerprint(entry, session):
40     """get fingerprint for given ACL entry
41
42     The entry is a string in one of these formats::
43
44         uid:<uid>
45         name:<name>
46         fpr:<fingerprint>
47         keyring:<keyring-name>
48
49     @type  entry: string
50     @param entry: ACL entry
51
52     @param session: database session
53
54     @rtype:  L{daklib.dbconn.Fingerprint} or C{None}
55     @return: fingerprint for the entry
56     """
57     field, value = entry.split(":", 1)
58     q = session.query(Fingerprint).join(Fingerprint.keyring).filter(Keyring.active == True)
59
60     if field == 'uid':
61         q = q.join(Fingerprint.uid).filter(Uid.uid == value)
62     elif field == 'name':
63         q = q.join(Fingerprint.uid).filter(Uid.name == value)
64     elif field == 'fpr':
65         q = q.filter(Fingerprint.fingerprint == value)
66     elif field == 'keyring':
67         q = q.filter(Keyring.keyring_name == value)
68     else:
69         raise Exception('Unknown selector "{0}".'.format(field))
70
71     return q.all()
72
73 def acl_set_fingerprints(acl_name, entries):
74     session = DBConn().session()
75     acl = session.query(ACL).filter_by(name=acl_name).one()
76
77     acl.fingerprints.clear()
78     for entry in entries:
79         entry = entry.strip()
80         if entry.startswith('#') or len(entry) == 0:
81             continue
82
83         fps = get_fingerprint(entry, session)
84         if len(fps) == 0:
85             print "Unknown key for '{0}'".format(entry)
86         else:
87             acl.fingerprints.update(fps)
88
89     session.commit()
90
91 def acl_export_per_source(acl_name):
92     session = DBConn().session()
93     acl = session.query(ACL).filter_by(name=acl_name).one()
94
95     query = r"""
96       SELECT
97         f.fingerprint,
98         (SELECT COALESCE(u.name, '') || ' <' || u.uid || '>'
99            FROM uid u
100            JOIN fingerprint f2 ON u.id = f2.uid
101           WHERE f2.id = f.id) AS name,
102         STRING_AGG(
103           a.source
104           || COALESCE(' (' || (SELECT fingerprint FROM fingerprint WHERE id = a.created_by_id) || ')', ''),
105           E',\n ' ORDER BY a.source)
106       FROM acl_per_source a
107       JOIN fingerprint f ON a.fingerprint_id = f.id
108       LEFT JOIN uid u ON f.uid = u.id
109       WHERE a.acl_id = :acl_id
110       GROUP BY f.id, f.fingerprint
111       ORDER BY name
112       """
113
114     for row in session.execute(query, {'acl_id': acl.id}):
115         print "Fingerprint:", row[0]
116         print "Uid:", row[1]
117         print "Allow:", row[2]
118         print
119
120     session.rollback()
121     session.close()
122
123 def main(argv=None):
124     if argv is None:
125         argv = sys.argv
126
127     if len(argv) != 3:
128         usage()
129         sys.exit(1)
130
131     if argv[1] == 'set-fingerprints':
132         acl_set_fingerprints(argv[2], sys.stdin)
133     elif argv[1] == 'export-per-source':
134         acl_export_per_source(argv[2])
135     else:
136         usage()
137         sys.exit(1)