Skip to content

Commit 7cc1507

Browse files
[DPE-7558] Fixes for predefined roles (backporting fixes from PgBouncer to PostgreSQL) (#958)
* Call RESET ROLE before doing some operations in the database Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Reset role when updating user password Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Quote identifiers Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Revoke privileges only once Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Quote IN ROLE value Signed-off-by: Marcelo Henrique Neppel <[email protected]> --------- Signed-off-by: Marcelo Henrique Neppel <[email protected]>
1 parent 04999ee commit 7cc1507

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/charms/postgresql_k8s/v1/postgresql.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def _configure_pgaudit(self, enable: bool) -> None:
164164
connection = self._connect_to_database()
165165
connection.autocommit = True
166166
with connection.cursor() as cursor:
167+
cursor.execute("RESET ROLE;")
167168
if enable:
168169
cursor.execute("ALTER SYSTEM SET pgaudit.log = 'ROLE,DDL,MISC,MISC_SET';")
169170
cursor.execute("ALTER SYSTEM SET pgaudit.log_client TO off;")
@@ -242,11 +243,11 @@ def create_database(
242243
if cursor.fetchone() is None:
243244
cursor.execute(SQL("SET ROLE charmed_databases_owner;"))
244245
cursor.execute(SQL("CREATE DATABASE {};").format(Identifier(database)))
245-
cursor.execute(
246-
SQL("REVOKE ALL PRIVILEGES ON DATABASE {} FROM PUBLIC;").format(
247-
Identifier(database)
246+
cursor.execute(
247+
SQL("REVOKE ALL PRIVILEGES ON DATABASE {} FROM PUBLIC;").format(
248+
Identifier(database)
249+
)
248250
)
249-
)
250251
with self._connect_to_database(database=database) as conn, conn.cursor() as curs:
251252
curs.execute(SQL("SELECT set_up_predefined_catalog_roles();"))
252253
except psycopg2.Error as e:
@@ -306,11 +307,12 @@ def create_user(
306307
f"WITH LOGIN{' SUPERUSER' if admin else ''} ENCRYPTED PASSWORD '{password}'"
307308
)
308309
if in_role:
309-
user_definition += f" IN ROLE {in_role}"
310+
user_definition += f" IN ROLE \"{in_role}\""
310311
if can_create_database:
311312
user_definition += " CREATEDB"
312313
if privileges:
313314
user_definition += f" {' '.join(privileges)}"
315+
cursor.execute(SQL("RESET ROLE;"))
314316
cursor.execute(SQL("BEGIN;"))
315317
cursor.execute(SQL("SET LOCAL log_statement = 'none';"))
316318
cursor.execute(SQL(f"{user_definition};").format(Identifier(user)))
@@ -976,8 +978,9 @@ def set_up_predefined_catalog_roles_function(self) -> None:
976978
BEGIN
977979
database := (SELECT current_database());
978980
current_session_user := (SELECT session_user);
979-
owner_user := database || '_owner';
980-
admin_user := database || '_admin';
981+
owner_user := quote_ident(database || '_owner');
982+
admin_user := quote_ident(database || '_admin');
983+
database := quote_ident(database);
981984
982985
IF (SELECT COUNT(rolname) FROM pg_roles WHERE rolname=admin_user) = 0 THEN
983986
statements := ARRAY[
@@ -1068,6 +1071,7 @@ def update_user_password(
10681071
with self._connect_to_database(
10691072
database_host=database_host
10701073
) as connection, connection.cursor() as cursor:
1074+
cursor.execute(SQL("RESET ROLE;"))
10711075
cursor.execute(SQL("BEGIN;"))
10721076
cursor.execute(SQL("SET LOCAL log_statement = 'none';"))
10731077
cursor.execute(

0 commit comments

Comments
 (0)