Skip to content

Commit ec98c4c

Browse files
committed
Reuse connection for pgaudit conf
1 parent cf635c4 commit ec98c4c

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

lib/charms/postgresql_k8s/v0/postgresql.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,17 @@ def __init__(
144144
self.database = database
145145
self.system_users = system_users if system_users else []
146146

147-
def _configure_pgaudit(self, enable: bool) -> None:
148-
connection = None
149-
try:
150-
connection = self._connect_to_database()
151-
connection.autocommit = True
152-
with connection.cursor() as cursor:
153-
if enable:
154-
cursor.execute("ALTER SYSTEM SET pgaudit.log = 'ROLE,DDL,MISC,MISC_SET';")
155-
cursor.execute("ALTER SYSTEM SET pgaudit.log_client TO off;")
156-
cursor.execute("ALTER SYSTEM SET pgaudit.log_parameter TO off;")
157-
else:
158-
cursor.execute("ALTER SYSTEM RESET pgaudit.log;")
159-
cursor.execute("ALTER SYSTEM RESET pgaudit.log_client;")
160-
cursor.execute("ALTER SYSTEM RESET pgaudit.log_parameter;")
161-
cursor.execute("SELECT pg_reload_conf();")
162-
finally:
163-
if connection is not None:
164-
connection.close()
147+
def _configure_pgaudit(self, enable: bool, connection) -> None:
148+
with connection.cursor() as cursor:
149+
if enable:
150+
cursor.execute("ALTER SYSTEM SET pgaudit.log = 'ROLE,DDL,MISC,MISC_SET';")
151+
cursor.execute("ALTER SYSTEM SET pgaudit.log_client TO off;")
152+
cursor.execute("ALTER SYSTEM SET pgaudit.log_parameter TO off;")
153+
else:
154+
cursor.execute("ALTER SYSTEM RESET pgaudit.log;")
155+
cursor.execute("ALTER SYSTEM RESET pgaudit.log_client;")
156+
cursor.execute("ALTER SYSTEM RESET pgaudit.log_parameter;")
157+
cursor.execute("SELECT pg_reload_conf();")
165158

166159
def _connect_to_database(
167160
self, database: Optional[str] = None, database_host: Optional[str] = None
@@ -435,7 +428,7 @@ def enable_disable_extensions(
435428
for extension, enable in extensions.items():
436429
ordered_extensions[extension] = enable
437430

438-
self._configure_pgaudit(False)
431+
self._configure_pgaudit(False, connection)
439432
# Enable/disabled the extension in each database.
440433
for database in databases:
441434
with self._connect_to_database(
@@ -454,8 +447,8 @@ def enable_disable_extensions(
454447
except psycopg2.Error as e:
455448
raise PostgreSQLEnableDisableExtensionError() from e
456449
finally:
457-
self._configure_pgaudit(extensions.get("pgaudit", False))
458450
if connection is not None:
451+
self._configure_pgaudit(extensions.get("pgaudit", False), connection)
459452
connection.close()
460453

461454
def _generate_database_privileges_statements(

tests/unit/test_postgresql.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright 2023 Canonical Ltd.
22
# See LICENSE file for licensing details.
3-
from unittest.mock import call, patch
3+
from unittest.mock import MagicMock, call, patch
44

55
import psycopg2
66
import pytest
@@ -460,30 +460,26 @@ def test_build_postgresql_parameters(harness):
460460

461461

462462
def test_configure_pgaudit(harness):
463-
with patch(
464-
"charms.postgresql_k8s.v0.postgresql.PostgreSQL._connect_to_database"
465-
) as _connect_to_database:
466-
# Test when pgAudit is enabled.
467-
execute = (
468-
_connect_to_database.return_value.cursor.return_value.__enter__.return_value.execute
469-
)
470-
harness.charm.postgresql._configure_pgaudit(True)
471-
execute.assert_has_calls([
472-
call("ALTER SYSTEM SET pgaudit.log = 'ROLE,DDL,MISC,MISC_SET';"),
473-
call("ALTER SYSTEM SET pgaudit.log_client TO off;"),
474-
call("ALTER SYSTEM SET pgaudit.log_parameter TO off;"),
475-
call("SELECT pg_reload_conf();"),
476-
])
477-
478-
# Test when pgAudit is disabled.
479-
execute.reset_mock()
480-
harness.charm.postgresql._configure_pgaudit(False)
481-
execute.assert_has_calls([
482-
call("ALTER SYSTEM RESET pgaudit.log;"),
483-
call("ALTER SYSTEM RESET pgaudit.log_client;"),
484-
call("ALTER SYSTEM RESET pgaudit.log_parameter;"),
485-
call("SELECT pg_reload_conf();"),
486-
])
463+
# Test when pgAudit is enabled.
464+
mock_conn = MagicMock()
465+
execute = mock_conn.cursor.return_value.__enter__.return_value.execute
466+
harness.charm.postgresql._configure_pgaudit(True, mock_conn)
467+
execute.assert_has_calls([
468+
call("ALTER SYSTEM SET pgaudit.log = 'ROLE,DDL,MISC,MISC_SET';"),
469+
call("ALTER SYSTEM SET pgaudit.log_client TO off;"),
470+
call("ALTER SYSTEM SET pgaudit.log_parameter TO off;"),
471+
call("SELECT pg_reload_conf();"),
472+
])
473+
474+
# Test when pgAudit is disabled.
475+
execute.reset_mock()
476+
harness.charm.postgresql._configure_pgaudit(False, mock_conn)
477+
execute.assert_has_calls([
478+
call("ALTER SYSTEM RESET pgaudit.log;"),
479+
call("ALTER SYSTEM RESET pgaudit.log_client;"),
480+
call("ALTER SYSTEM RESET pgaudit.log_parameter;"),
481+
call("SELECT pg_reload_conf();"),
482+
])
487483

488484

489485
def test_validate_group_map(harness):

0 commit comments

Comments
 (0)