1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#! /usr/bin/env python
# GrantFu - GRANT/REVOKE generator for Postgres
#
# Copyright (c) 2005 Marko Kreen
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""Generator for PostgreSQL permissions.
Loads config where roles, objects and their mapping is described
and generates grants based on them.
ConfigParser docs: http://docs.python.org/lib/module-ConfigParser.html
Example:
--------------------------------------------------------------------
[DEFAULT]
users = user1, user2 # users to handle
groups = group1, group2 # groups to handle
auto_seq = 0 # dont handle seqs (default)
# '!' after a table negates this setting for a table
seq_name = id # the name for serial field (default: id)
seq_usage = 0 # should we grant "usage" or "select, update"
# for automatically handled sequences
# section names can be random, but if you want to see them
# in same order as in config file, then order them alphabetically
[1.section]
on.tables = testtbl, testtbl_id_seq, # here we handle seq by hand
table_with_seq! # handle seq automatically
# (table_with_seq_id_seq)
user1 = select
group1 = select, insert, update
# instead of 'tables', you may use 'functions', 'languages',
# 'schemas', 'tablespaces'
---------------------------------------------------------------------
"""
import sys, os, getopt
from ConfigParser import SafeConfigParser
__version__ = "1.0"
R_NEW = 0x01
R_DEFS = 0x02
G_DEFS = 0x04
R_ONLY = 0x80
def usage(err):
sys.stderr.write("usage: %s [-r|-R] CONF_FILE\n" % sys.argv[0])
sys.stderr.write(" -r Generate also REVOKE commands\n")
sys.stderr.write(" -R Generate only REVOKE commands\n")
sys.stderr.write(" -d Also REVOKE default perms\n")
sys.stderr.write(" -D Only REVOKE default perms\n")
sys.stderr.write(" -o Generate default GRANTS\n")
sys.stderr.write(" -v Print program version\n")
sys.stderr.write(" -t Put everything in one big transaction\n")
sys.exit(err)
class PConf(SafeConfigParser):
"List support for ConfigParser"
def __init__(self, defaults = None):
SafeConfigParser.__init__(self, defaults)
def get_list(self, sect, key):
str = self.get(sect, key).strip()
res = []
if not str:
return res
for val in str.split(","):
res.append(val.strip())
return res
class GrantFu:
def __init__(self, cf, revoke):
self.cf = cf
self.revoke = revoke
# avoid putting grantfu vars into defaults, thus into every section
self.group_list = []
self.user_list = []
self.auto_seq = 0
self.seq_name = "id"
self.seq_usage = 0
if self.cf.has_option('GrantFu', 'groups'):
self.group_list = self.cf.get_list('GrantFu', 'groups')
if self.cf.has_option('GrantFu', 'users'):
self.user_list += self.cf.get_list('GrantFu', 'users')
if self.cf.has_option('GrantFu', 'roles'):
self.user_list += self.cf.get_list('GrantFu', 'roles')
if self.cf.has_option('GrantFu', 'auto_seq'):
self.auto_seq = self.cf.getint('GrantFu', 'auto_seq')
if self.cf.has_option('GrantFu', 'seq_name'):
self.seq_name = self.cf.get('GrantFu', 'seq_name')
if self.cf.has_option('GrantFu', 'seq_usage'):
self.seq_usage = self.cf.getint('GrantFu', 'seq_usage')
# make string of all subjects
tmp = []
for g in self.group_list:
tmp.append("group " + g)
for u in self.user_list:
tmp.append(u)
self.all_subjs = ", ".join(tmp)
# per-section vars
self.sect = None
self.seq_list = []
self.seq_allowed = []
def process(self):
if len(self.user_list) == 0 and len(self.group_list) == 0:
return
sect_list = self.cf.sections()
sect_list.sort()
for self.sect in sect_list:
if self.sect == "GrantFu":
continue
print "\n-- %s --" % self.sect
self.handle_tables()
self.handle_other('on.databases', 'DATABASE')
self.handle_other('on.functions', 'FUNCTION')
self.handle_other('on.languages', 'LANGUAGE')
self.handle_other('on.schemas', 'SCHEMA')
self.handle_other('on.tablespaces', 'TABLESPACE')
self.handle_other('on.sequences', 'SEQUENCE')
self.handle_other('on.types', 'TYPE')
self.handle_other('on.domains', 'DOMAIN')
def handle_other(self, listname, obj_type):
"""Handle grants for all objects except tables."""
if not self.sect_hasvar(listname):
return
# don't parse list, as in case of functions it may be complicated
obj_str = obj_type + " " + self.sect_var(listname)
if self.revoke & R_NEW:
self.gen_revoke(obj_str)
if self.revoke & R_DEFS:
self.gen_revoke_defs(obj_str, obj_type)
if not self.revoke & R_ONLY:
self.gen_one_type(obj_str)
if self.revoke & G_DEFS:
self.gen_defs(obj_str, obj_type)
def handle_tables(self):
"""Handle grants for tables and sequences.
The tricky part here is the automatic handling of sequences."""
if not self.sect_hasvar('on.tables'):
return
cleaned_list = []
table_list = self.sect_list('on.tables')
for table in table_list:
if table[-1] == '!':
table = table[:-1]
if not self.auto_seq:
self.seq_list.append("%s_%s_seq" % (table, self.seq_name))
else:
if self.auto_seq:
self.seq_list.append("%s_%s_seq" % (table, self.seq_name))
cleaned_list.append(table)
obj_str = "TABLE " + ", ".join(cleaned_list)
if self.revoke & R_NEW:
self.gen_revoke(obj_str)
if self.revoke & R_DEFS:
self.gen_revoke_defs(obj_str, "TABLE")
if not self.revoke & R_ONLY:
self.gen_one_type(obj_str)
if self.revoke & G_DEFS:
self.gen_defs(obj_str, "TABLE")
# cleanup
self.seq_list = []
self.seq_allowed = []
def gen_revoke(self, obj_str):
"Generate revoke for one section / subject type (user or group)"
if len(self.seq_list) > 0:
obj_str += ", " + ", ".join(self.seq_list)
obj_str = obj_str.strip().replace('\n', '\n ')
print "REVOKE ALL ON %s\n FROM %s CASCADE;" % (obj_str, self.all_subjs)
def gen_revoke_defs(self, obj_str, obj_type):
"Generate revoke defaults for one section"
# process only things that have default grants to public
if obj_type not in ('FUNCTION', 'DATABASE', 'LANGUAGE', 'TYPE', 'DOMAIN'):
return
defrole = 'public'
# if the sections contains grants to 'public', dont drop
if self.sect_hasvar(defrole):
return
obj_str = obj_str.strip().replace('\n', '\n ')
print "REVOKE ALL ON %s\n FROM %s CASCADE;" % (obj_str, defrole)
def gen_defs(self, obj_str, obj_type):
"Generate defaults grants for one section"
if obj_type == "FUNCTION":
defgrants = "execute"
elif obj_type == "DATABASE":
defgrants = "connect, temp"
elif obj_type in ("LANGUAGE", "TYPE", "DOMAIN"):
defgrants = "usage"
else:
return
defrole = 'public'
obj_str = obj_str.strip().replace('\n', '\n ')
print "GRANT %s ON %s\n TO %s;" % (defgrants, obj_str, defrole)
def gen_one_subj(self, subj, fqsubj, obj_str):
if not self.sect_hasvar(subj):
return
obj_str = obj_str.strip().replace('\n', '\n ')
perm = self.sect_var(subj).strip()
if perm:
print "GRANT %s ON %s\n TO %s;" % (perm, obj_str, fqsubj)
# check for seq perms
if len(self.seq_list) > 0:
loperm = perm.lower()
if loperm.find("insert") >= 0 or loperm.find("all") >= 0:
self.seq_allowed.append(fqsubj)
def gen_one_type(self, obj_str):
"Generate GRANT for one section / one object type in section"
for u in self.user_list:
self.gen_one_subj(u, u, obj_str)
for g in self.group_list:
self.gen_one_subj(g, "group " + g, obj_str)
# if there was any seq perms, generate grants
if len(self.seq_allowed) > 0:
seq_str = ", ".join(self.seq_list)
subj_str = ", ".join(self.seq_allowed)
if self.seq_usage:
cmd = "GRANT usage ON SEQUENCE %s\n TO %s;"
else:
cmd = "GRANT select, update ON %s\n TO %s;"
print cmd % (seq_str, subj_str)
def sect_var(self, name):
return self.cf.get(self.sect, name).strip()
def sect_list(self, name):
return self.cf.get_list(self.sect, name)
def sect_hasvar(self, name):
return self.cf.has_option(self.sect, name)
def main():
revoke = 0
tx = False
try:
opts, args = getopt.getopt(sys.argv[1:], "vhrRdDot")
except getopt.error, det:
print "getopt error:", det
usage(1)
for o, v in opts:
if o == "-h":
usage(0)
elif o == "-r":
revoke |= R_NEW
elif o == "-R":
revoke |= R_NEW | R_ONLY
elif o == "-d":
revoke |= R_DEFS
elif o == "-D":
revoke |= R_DEFS | R_ONLY
elif o == "-o":
revoke |= G_DEFS
elif o == "-t":
tx = True
elif o == "-v":
print "GrantFu version", __version__
sys.exit(0)
if len(args) != 1:
usage(1)
# load config
cf = PConf()
cf.read(args[0])
if not cf.has_section("GrantFu"):
print "Incorrect config file, GrantFu sction missing"
sys.exit(1)
if tx:
print "begin;\n"
# revokes and default grants
if revoke & (R_NEW | R_DEFS):
g = GrantFu(cf, revoke | R_ONLY)
g.process()
revoke = revoke & R_ONLY
# grants
if revoke & R_ONLY == 0:
g = GrantFu(cf, revoke & G_DEFS)
g.process()
if tx:
print "\ncommit;\n"
if __name__ == '__main__':
main()
|