65
65
class DbSchema(dict):
66
groups = None # List of groups defined in the db
67
users = None # List of users defined in the db
66
groups = None # List of groups defined in the db
67
users = None # List of users defined in the db
69
69
def __init__(self, con):
70
70
super(DbSchema, self).__init__()
134
134
def execute(self, cmd, params=None):
135
135
cmd = cmd.encode('utf8')
136
136
if params is None:
137
log.debug3('%s' % (cmd, ))
137
log.debug2('%s' % (cmd, ))
138
138
return self.__dict__['_cursor'].execute(cmd)
140
log.debug3('%s [%r]' % (cmd, params))
140
log.debug2('%s [%r]' % (cmd, params))
141
141
return self.__dict__['_cursor'].execute(cmd, params)
143
143
def __getattr__(self, key):
159
159
config.read([configfile_name])
161
161
con = connect(options.dbuser)
162
cur = CursorWrapper(con.cursor())
163
164
if options.cluster:
164
165
nodes = replication.helpers.get_nodes(con, 1)
171
172
node.nickname, node.connection_string))
172
173
reset_permissions(
173
174
psycopg2.connect(node.connection_string), config, options)
175
176
log.warning("--cluster requested, but not a Slony-I cluster.")
176
177
log.info("Resetting permissions on single database")
177
178
reset_permissions(con, config, options)
181
181
def list_identifiers(identifiers):
342
342
if username in schema.principals:
343
343
if type_ == 'group':
344
344
if options.revoke:
345
log.debug2("Revoking membership of %s role", username)
345
log.debug("Revoking membership of %s role", username)
346
346
cur.execute("REVOKE %s FROM %s" % (
347
347
quote_identifier(username), all_users))
349
349
# Note - we don't drop the user because it might own
350
350
# objects in other databases. We need to ensure they are
351
351
# not superusers though!
352
log.debug2("Resetting role options of %s role.", username)
352
log.debug("Resetting role options of %s role.", username)
354
354
"ALTER ROLE %s WITH %s" % (
355
355
quote_identifier(username),
380
380
if user.endswith('_ro'):
381
381
groups = ['%s_ro' % group for group in groups]
383
log.debug2("Adding %s to %s roles", user, ', '.join(groups))
383
log.debug("Adding %s to %s roles", user, ', '.join(groups))
384
384
for group in groups:
385
385
cur.execute(r"""ALTER GROUP %s ADD USER %s""" % (
386
386
quote_identifier(group), quote_identifier(user)))
388
log.debug2("%s not in any roles", user)
388
log.debug("%s not in any roles", user)
390
# Change ownership of all objects to OWNER
391
for obj in schema.values():
392
if obj.type in ("function", "sequence"):
393
pass # Can't change ownership of functions or sequences
395
if obj.owner != options.owner:
396
log.info("Resetting ownership of %s", obj.fullname)
397
cur.execute("ALTER TABLE %s OWNER TO %s" % (
398
obj.fullname, quote_identifier(options.owner)))
390
400
if options.revoke:
391
# Change ownership of all objects to OWNER.
392
# We skip this in --no-revoke mode as ownership changes may
393
# block on a live system.
394
for obj in schema.values():
395
if obj.type in ("function", "sequence"):
396
pass # Can't change ownership of functions or sequences
398
if obj.owner != options.owner:
399
log.info("Resetting ownership of %s", obj.fullname)
400
cur.execute("ALTER TABLE %s OWNER TO %s" % (
401
obj.fullname, quote_identifier(options.owner)))
403
401
# Revoke all privs from known groups. Don't revoke anything for
404
402
# users or groups not defined in our security.cfg.
405
403
table_revocations = PermissionGatherer("TABLE")
431
429
function_revocations.revoke(cur)
432
430
sequence_revocations.revoke(cur)
434
log.info("Not resetting ownership of database objects")
435
432
log.info("Not revoking permissions on database objects")
437
434
# Set of all tables we have granted permissions on. After we have assigned
468
465
who_ro = quote_identifier('%s_ro' % username)
471
468
"Granting %s on %s to %s", perm, obj.fullname, who)
472
469
if obj.type == 'function':
473
470
function_permissions.add(perm, obj.fullname, who)