]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update16.py
Convert exception handling to Python3 syntax.
[dak.git] / dak / dakdb / update16.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Adding tables for key-based ACLs and blocks
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2009  Mark Hymers <mhy@debian.org>
9 @license: GNU General Public License version 2 or later
10 """
11
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.
16
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.
21
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
25
26 ################################################################################
27
28
29 ################################################################################
30
31 import psycopg2
32 import time
33 from daklib.dak_exceptions import DBUpdateError
34
35 ################################################################################
36
37 def do_update(self):
38     print "Adding tables for handling key-based ACLs and upload blocks"
39
40     try:
41         c = self.db.cursor()
42
43         # Fix up some older table permissions
44         c.execute("GRANT SELECT ON src_format TO public")
45         c.execute("GRANT ALL ON src_format TO ftpmaster")
46         c.execute("GRANT USAGE ON src_format_id_seq TO ftpmaster")
47
48         c.execute("GRANT SELECT ON suite_src_formats TO public")
49         c.execute("GRANT ALL ON suite_src_formats TO ftpmaster")
50
51         # Source ACLs table
52         print "Source ACLs table"
53         c.execute("""
54         CREATE TABLE source_acl (
55               id SERIAL PRIMARY KEY,
56               access_level TEXT UNIQUE NOT NULL
57         )
58         """)
59
60         ## Can upload all packages
61         c.execute("INSERT INTO source_acl (access_level) VALUES ('full')")
62         ## Can upload only packages marked as DM upload allowed
63         c.execute("INSERT INTO source_acl (access_level) VALUES ('dm')")
64
65         c.execute("GRANT SELECT ON source_acl TO public")
66         c.execute("GRANT ALL ON source_acl TO ftpmaster")
67         c.execute("GRANT USAGE ON source_acl_id_seq TO ftpmaster")
68
69         # Binary ACLs table
70         print "Binary ACLs table"
71         c.execute("""
72         CREATE TABLE binary_acl (
73               id SERIAL PRIMARY KEY,
74               access_level TEXT UNIQUE NOT NULL
75         )
76         """)
77
78         ## Can upload any architectures of binary packages
79         c.execute("INSERT INTO binary_acl (access_level) VALUES ('full')")
80         ## Can upload debs where architectures are based on the map table binary_acl_map
81         c.execute("INSERT INTO binary_acl (access_level) VALUES ('map')")
82
83         c.execute("GRANT SELECT ON binary_acl TO public")
84         c.execute("GRANT ALL ON binary_acl TO ftpmaster")
85         c.execute("GRANT USAGE ON binary_acl_id_seq TO ftpmaster")
86
87         # This is only used if binary_acl is 2 for the fingerprint concerned
88         c.execute("""
89         CREATE TABLE binary_acl_map (
90               id SERIAL PRIMARY KEY,
91               fingerprint_id INT4 REFERENCES fingerprint (id) NOT NULL,
92               architecture_id INT4 REFERENCES architecture (id) NOT NULL,
93
94               UNIQUE (fingerprint_id, architecture_id)
95         )""")
96
97         c.execute("GRANT SELECT ON binary_acl_map TO public")
98         c.execute("GRANT ALL ON binary_acl_map TO ftpmaster")
99         c.execute("GRANT USAGE ON binary_acl_map_id_seq TO ftpmaster")
100
101         ## NULL means no source upload access (i.e. any upload containing source
102         ## will be rejected)
103         c.execute("ALTER TABLE fingerprint ADD COLUMN source_acl_id INT4 REFERENCES source_acl(id) DEFAULT NULL")
104
105         ## NULL means no binary upload access
106         c.execute("ALTER TABLE fingerprint ADD COLUMN binary_acl_id INT4 REFERENCES binary_acl(id) DEFAULT NULL")
107
108         ## TRUE here means that if the person doesn't have binary upload permissions for
109         ## an architecture, we'll reject the .changes.  FALSE means that we'll simply
110         ## dispose of those particular binaries
111         c.execute("ALTER TABLE fingerprint ADD COLUMN binary_reject BOOLEAN NOT NULL DEFAULT TRUE")
112
113         # Blockage table (replaces the hard coded stuff we used to have in extensions)
114         print "Adding blockage table"
115         c.execute("""
116         CREATE TABLE upload_blocks (
117               id             SERIAL PRIMARY KEY,
118               source         TEXT NOT NULL,
119               version        TEXT DEFAULT NULL,
120               fingerprint_id INT4 REFERENCES fingerprint (id),
121               uid_id         INT4 REFERENCES uid (id),
122               reason         TEXT NOT NULL,
123
124               CHECK (fingerprint_id IS NOT NULL OR uid_id IS NOT NULL)
125         )""")
126
127         c.execute("GRANT SELECT ON upload_blocks TO public")
128         c.execute("GRANT ALL ON upload_blocks TO ftpmaster")
129         c.execute("GRANT USAGE ON upload_blocks_id_seq TO ftpmaster")
130
131         c.execute("ALTER TABLE keyrings ADD COLUMN default_source_acl_id INT4 REFERENCES source_acl (id) DEFAULT NULL")
132         c.execute("ALTER TABLE keyrings ADD COLUMN default_binary_acl_id INT4 REFERENCES binary_acl (id) DEFAULT NULL")
133         c.execute("ALTER TABLE keyrings ADD COLUMN default_binary_reject BOOLEAN NOT NULL DEFAULT TRUE")
134         # Set up keyring priorities
135         c.execute("ALTER TABLE keyrings ADD COLUMN priority INT4 NOT NULL DEFAULT 100")
136         # And then we don't need the DM stuff any more
137         c.execute("ALTER TABLE keyrings DROP COLUMN debian_maintainer")
138
139         # Default ACLs for keyrings
140         c.execute("""
141         CREATE TABLE keyring_acl_map (
142               id SERIAL PRIMARY KEY,
143               keyring_id      INT4 REFERENCES keyrings (id) NOT NULL,
144               architecture_id INT4 REFERENCES architecture (id) NOT NULL,
145
146               UNIQUE (keyring_id, architecture_id)
147         )""")
148
149         c.execute("GRANT SELECT ON keyring_acl_map TO public")
150         c.execute("GRANT ALL ON keyring_acl_map TO ftpmaster")
151         c.execute("GRANT USAGE ON keyring_acl_map_id_seq TO ftpmaster")
152
153         # Set up some default stuff; default to old behaviour
154         print "Setting up some defaults"
155
156         c.execute("""UPDATE keyrings SET default_source_acl_id = (SELECT id FROM source_acl WHERE access_level = 'full'),
157                                          default_binary_acl_id = (SELECT id FROM binary_acl WHERE access_level = 'full')""")
158
159         c.execute("""UPDATE keyrings SET default_source_acl_id = (SELECT id FROM source_acl WHERE access_level = 'dm'),
160                                          default_binary_acl_id = (SELECT id FROM binary_acl WHERE access_level = 'full')
161                                      WHERE name = 'debian-maintainers.gpg'""")
162
163         c.execute("""UPDATE keyrings SET priority = 90 WHERE name = 'debian-maintainers.gpg'""")
164
165         # Initialize the existing keys
166         c.execute("""UPDATE fingerprint SET binary_acl_id = (SELECT default_binary_acl_id FROM keyrings
167                                                               WHERE keyrings.id = fingerprint.keyring)""")
168
169         c.execute("""UPDATE fingerprint SET source_acl_id = (SELECT default_source_acl_id FROM keyrings
170                                                               WHERE keyrings.id = fingerprint.keyring)""")
171
172         print "Updating config version"
173         c.execute("UPDATE config SET value = '16' WHERE name = 'db_revision'")
174         self.db.commit()
175
176     except psycopg2.ProgrammingError as msg:
177         self.db.rollback()
178         raise DBUpdateError, "Unable to apply ACLs update (16), rollback issued. Error message : %s" % (str(msg))