5 switch to new ACL implementation and add pre-suite NEW
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2012 Ansgar Burchardt <ansgar@debian.org>
9 @license: GNU General Public License version 2 or later
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ################################################################################
29 from daklib.dak_exceptions import DBUpdateError
30 from daklib.config import Config
33 """ALTER TABLE suite ADD COLUMN new_queue_id INT REFERENCES policy_queue(id)""",
36 id SERIAL PRIMARY KEY NOT NULL,
38 is_global BOOLEAN NOT NULL DEFAULT 'f',
40 match_fingerprint BOOLEAN NOT NULL DEFAULT 'f',
41 match_keyring_id INTEGER REFERENCES keyrings(id),
43 allow_new BOOLEAN NOT NULL DEFAULT 'f',
44 allow_source BOOLEAN NOT NULL DEFAULT 'f',
45 allow_binary BOOLEAN NOT NULL DEFAULT 'f',
46 allow_binary_all BOOLEAN NOT NULL DEFAULT 'f',
47 allow_binary_only BOOLEAN NOT NULL DEFAULT 'f',
48 allow_hijack BOOLEAN NOT NULL DEFAULT 'f',
49 allow_per_source BOOLEAN NOT NULL DEFAULT 'f',
50 deny_per_source BOOLEAN NOT NULL DEFAULT 'f'
53 """CREATE TABLE acl_architecture_map (
54 acl_id INTEGER NOT NULL REFERENCES acl(id) ON DELETE CASCADE,
55 architecture_id INTEGER NOT NULL REFERENCES architecture(id) ON DELETE CASCADE,
56 PRIMARY KEY (acl_id, architecture_id)
59 """CREATE TABLE acl_fingerprint_map (
60 acl_id INTEGER NOT NULL REFERENCES acl(id) ON DELETE CASCADE,
61 fingerprint_id INTEGER NOT NULL REFERENCES fingerprint(id) ON DELETE CASCADE,
62 PRIMARY KEY (acl_id, fingerprint_id)
65 """CREATE TABLE acl_per_source (
66 acl_id INTEGER NOT NULL REFERENCES acl(id) ON DELETE CASCADE,
67 fingerprint_id INTEGER NOT NULL REFERENCES fingerprint(id) ON DELETE CASCADE,
70 PRIMARY KEY (acl_id, fingerprint_id, source)
73 """CREATE TABLE suite_acl_map (
74 suite_id INTEGER NOT NULL REFERENCES suite(id) ON DELETE CASCADE,
75 acl_id INTEGER NOT NULL REFERENCES acl(id),
76 PRIMARY KEY (suite_id, acl_id)
80 ################################################################################
82 def get_buildd_acl_id(c, keyring_id):
84 SELECT 'buildd-' || STRING_AGG(a.arch_string, '+' ORDER BY a.arch_string)
85 FROM keyring_acl_map kam
86 JOIN architecture a ON kam.architecture_id = a.id
87 WHERE kam.keyring_id = %(keyring_id)s
88 """, {'keyring_id': keyring_id})
89 acl_name, = c.fetchone()
91 c.execute('SELECT id FROM acl WHERE name = %(acl_name)s', {'acl_name': acl_name})
98 ( name, allow_new, allow_source, allow_binary, allow_binary_all, allow_binary_only, allow_hijack)
99 VALUES (%(acl_name)s, 't', 'f', 't', 'f', 't', 't')
100 RETURNING id""", {'acl_name': acl_name})
101 acl_id, = c.fetchone()
103 c.execute("""INSERT INTO acl_architecture_map (acl_id, architecture_id)
104 SELECT %(acl_id)s, architecture_id
106 WHERE keyring_id = %(keyring_id)s""",
107 {'acl_id': acl_id, 'keyring_id': keyring_id})
111 def get_acl_id(c, acl_dd, acl_dm, keyring_id, source_acl_id, binary_acl_id):
112 c.execute('SELECT access_level FROM source_acl WHERE id = %(source_acl_id)s', {'source_acl_id': source_acl_id})
119 c.execute('SELECT access_level FROM binary_acl WHERE id = %(binary_acl_id)s', {'binary_acl_id': binary_acl_id})
126 if source_acl == 'full' and binary_acl == 'full':
128 elif source_acl == 'dm' and binary_acl == 'full':
130 elif source_acl is None and binary_acl == 'map':
131 return get_buildd_acl_id(c, keyring_id)
133 raise Exception('Cannot convert ACL combination automatically: binary_acl={0}, source_acl={1}'.format(binary_acl, source_acl))
142 for stmt in statements:
147 (name, allow_new, allow_source, allow_binary, allow_binary_all, allow_binary_only, allow_hijack)
148 VALUES ('dd', 't', 't', 't', 't', 't', 't')
150 acl_dd, = c.fetchone()
154 (name, allow_new, allow_source, allow_binary, allow_binary_all, allow_binary_only, allow_per_source, allow_hijack)
155 VALUES ('dm', 'f', 't', 't', 't', 'f', 't', 'f')
157 acl_dm, = c.fetchone()
159 # convert per-fingerprint ACLs
161 c.execute('ALTER TABLE fingerprint ADD COLUMN acl_id INTEGER REFERENCES acl(id)')
162 c.execute("""SELECT id, keyring, source_acl_id, binary_acl_id
164 WHERE source_acl_id IS NOT NULL OR binary_acl_id IS NOT NULL""")
165 for fingerprint_id, keyring_id, source_acl_id, binary_acl_id in c.fetchall():
166 acl_id = get_acl_id(c, acl_dd, acl_dm, keyring_id, source_acl_id, binary_acl_id)
167 c.execute('UPDATE fingerprint SET acl_id = %(acl_id)s WHERE id = %(fingerprint_id)s',
168 {'acl_id': acl_id, 'fingerprint_id': fingerprint_id})
169 c.execute("""ALTER TABLE fingerprint
170 DROP COLUMN source_acl_id,
171 DROP COLUMN binary_acl_id,
172 DROP COLUMN binary_reject""")
174 # convert per-keyring ACLs
175 c.execute('ALTER TABLE keyrings ADD COLUMN acl_id INTEGER REFERENCES acl(id)')
176 c.execute('SELECT id, default_source_acl_id, default_binary_acl_id FROM keyrings')
177 for keyring_id, source_acl_id, binary_acl_id in c.fetchall():
178 acl_id = get_acl_id(c, acl_dd, acl_dm, keyring_id, source_acl_id, binary_acl_id)
179 c.execute('UPDATE keyrings SET acl_id = %(acl_id)s WHERE id = %(keyring_id)s',
180 {'acl_id': acl_id, 'keyring_id': keyring_id})
181 c.execute("""ALTER TABLE keyrings
182 DROP COLUMN default_source_acl_id,
183 DROP COLUMN default_binary_acl_id,
184 DROP COLUMN default_binary_reject""")
186 c.execute("DROP TABLE keyring_acl_map")
187 c.execute("DROP TABLE binary_acl_map")
188 c.execute("DROP TABLE binary_acl")
189 c.execute("DROP TABLE source_acl")
191 # convert upload blocks
194 ( name, is_global, allow_new, allow_source, allow_binary, allow_binary_all, allow_hijack, allow_binary_only, deny_per_source)
195 VALUES ('blocks', 't', 't', 't', 't', 't', 't', 't', 't')
197 acl_block, = c.fetchone()
198 c.execute("SELECT source, fingerprint_id, reason FROM upload_blocks")
199 for source, fingerprint_id, reason in c.fetchall():
200 if fingerprint_id is None:
202 "ERROR: upload blocks based on uid are no longer supported\n"
203 "=========================================================\n"
205 "dak now only supports upload blocks based on fingerprints. Please remove\n"
206 "any uid-specific block by running\n"
207 " DELETE FROM upload_blocks WHERE fingerprint_id IS NULL\n"
210 c.execute('INSERT INTO acl_match_source_map (acl_id, fingerprint_id, source, reason) VALUES (%(acl_id)s, %(fingerprint_id)s, %(source)s, %(reason)s)',
211 {'acl_id': acl_block, 'fingerprint_id': fingerprint_id, 'source': source, 'reason': reason})
212 c.execute("DROP TABLE upload_blocks")
214 c.execute("UPDATE config SET value = '83' WHERE name = 'db_revision'")
217 except psycopg2.ProgrammingError as msg:
219 raise DBUpdateError('Unable to apply sick update 83, rollback issued. Error message: {0}'.format(msg))