Skip to content

Commit 26ad2c0

Browse files
committed
Enable pgAudit by default
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
1 parent fc7813e commit 26ad2c0

File tree

5 files changed

+44
-36
lines changed

5 files changed

+44
-36
lines changed

config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ options:
304304
type: boolean
305305
description: Enable timescaledb extension
306306
plugin_audit_enable:
307-
default: false
307+
default: true
308308
type: boolean
309309
description: Enable pgAudit extension
310310
profile:

tests/integration/test_audit.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,11 @@ async def test_audit_plugin(ops_test: OpsTest, charm) -> None:
3535
apps=[APPLICATION_NAME, DATABASE_APP_NAME], status="active"
3636
)
3737

38-
logger.info("Checking that the audit plugin is disabled")
38+
logger.info("Checking that the audit plugin is enabled")
3939
connection_string = await build_connection_string(
4040
ops_test, APPLICATION_NAME, RELATION_ENDPOINT
4141
)
4242
connection = None
43-
try:
44-
connection = psycopg2.connect(connection_string)
45-
with connection.cursor() as cursor:
46-
cursor.execute("CREATE TABLE test1(value TEXT);")
47-
cursor.execute("GRANT SELECT ON test1 TO PUBLIC;")
48-
cursor.execute("SET TIME ZONE 'Europe/Rome';")
49-
finally:
50-
if connection is not None:
51-
connection.close()
52-
try:
53-
logs = await run_command_on_unit(
54-
ops_test,
55-
"postgresql/0",
56-
"sudo grep AUDIT /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log",
57-
)
58-
except Exception:
59-
pass
60-
else:
61-
logger.info(f"Logs: {logs}")
62-
assert False, "Audit logs were found when the plugin is disabled."
63-
64-
logger.info("Enabling the audit plugin")
65-
await ops_test.model.applications[DATABASE_APP_NAME].set_config({
66-
"plugin_audit_enable": "True"
67-
})
68-
await ops_test.model.wait_for_idle(apps=[DATABASE_APP_NAME], status="active")
69-
70-
logger.info("Checking that the audit plugin is enabled")
7143
try:
7244
connection = psycopg2.connect(connection_string)
7345
with connection.cursor() as cursor:
@@ -77,12 +49,13 @@ async def test_audit_plugin(ops_test: OpsTest, charm) -> None:
7749
finally:
7850
if connection is not None:
7951
connection.close()
52+
unit_name = f"{DATABASE_APP_NAME}/0"
8053
for attempt in Retrying(stop=stop_after_delay(90), wait=wait_fixed(10), reraise=True):
8154
with attempt:
8255
try:
8356
logs = await run_command_on_unit(
8457
ops_test,
85-
"postgresql/0",
58+
unit_name,
8659
"sudo grep AUDIT /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log",
8760
)
8861
assert "MISC,BEGIN,,,BEGIN" in logs
@@ -93,3 +66,38 @@ async def test_audit_plugin(ops_test: OpsTest, charm) -> None:
9366
assert "MISC,SET,,,SET TIME ZONE 'Europe/Rome';" in logs
9467
except Exception:
9568
assert False, "Audit logs were not found when the plugin is enabled."
69+
70+
logger.info("Disabling the audit plugin")
71+
await ops_test.model.applications[DATABASE_APP_NAME].set_config({
72+
"plugin_audit_enable": "False"
73+
})
74+
await ops_test.model.wait_for_idle(apps=[DATABASE_APP_NAME], status="active")
75+
76+
logger.info("Removing the previous logs")
77+
await run_command_on_unit(
78+
ops_test,
79+
unit_name,
80+
"rm /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log",
81+
)
82+
83+
logger.info("Checking that the audit plugin is disabled")
84+
try:
85+
connection = psycopg2.connect(connection_string)
86+
with connection.cursor() as cursor:
87+
cursor.execute("CREATE TABLE test1(value TEXT);")
88+
cursor.execute("GRANT SELECT ON test1 TO PUBLIC;")
89+
cursor.execute("SET TIME ZONE 'Europe/Rome';")
90+
finally:
91+
if connection is not None:
92+
connection.close()
93+
try:
94+
logs = await run_command_on_unit(
95+
ops_test,
96+
unit_name,
97+
"sudo grep AUDIT /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log",
98+
)
99+
except Exception:
100+
pass
101+
else:
102+
logger.info(f"Logs: {logs}")
103+
assert False, "Audit logs were found when the plugin is disabled."

tests/unit/test_charm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2560,7 +2560,7 @@ def test_restart_services_after_reboot(harness):
25602560
def test_get_plugins(harness):
25612561
with patch("charm.PostgresqlOperatorCharm._on_config_changed"):
25622562
# Test when the charm has no plugins enabled.
2563-
assert harness.charm.get_plugins() == []
2563+
assert harness.charm.get_plugins() == ["pgaudit"]
25642564

25652565
# Test when the charm has some plugins enabled.
25662566
harness.update_config({

tests/unit/test_db.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def test_set_up_relation(harness):
228228
user = f"relation-{rel_id}"
229229
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
230230
postgresql_mock.create_database.assert_called_once_with(
231-
DATABASE, user, plugins=[], client_relations=[relation]
231+
DATABASE, user, plugins=["pgaudit"], client_relations=[relation]
232232
)
233233
_update_endpoints.assert_called_once()
234234
_update_unit_status.assert_called_once()
@@ -255,7 +255,7 @@ def test_set_up_relation(harness):
255255
assert harness.charm.legacy_db_relation.set_up_relation(relation)
256256
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
257257
postgresql_mock.create_database.assert_called_once_with(
258-
DATABASE, user, plugins=[], client_relations=[relation]
258+
DATABASE, user, plugins=["pgaudit"], client_relations=[relation]
259259
)
260260
_update_endpoints.assert_called_once()
261261
_update_unit_status.assert_called_once()
@@ -276,7 +276,7 @@ def test_set_up_relation(harness):
276276
assert harness.charm.legacy_db_relation.set_up_relation(relation)
277277
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
278278
postgresql_mock.create_database.assert_called_once_with(
279-
"application", user, plugins=[], client_relations=[relation]
279+
"application", user, plugins=["pgaudit"], client_relations=[relation]
280280
)
281281
_update_endpoints.assert_called_once()
282282
_update_unit_status.assert_called_once()

tests/unit/test_postgresql_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def test_on_database_requested(harness):
130130
database_relation = harness.model.get_relation(RELATION_NAME)
131131
client_relations = [database_relation]
132132
postgresql_mock.create_database.assert_called_once_with(
133-
DATABASE, user, plugins=[], client_relations=client_relations
133+
DATABASE, user, plugins=["pgaudit"], client_relations=client_relations
134134
)
135135
postgresql_mock.get_postgresql_version.assert_called_once()
136136
_update_endpoints.assert_called_once()

0 commit comments

Comments
 (0)