Skip to content

Commit 5fef9c7

Browse files
Lucas Gameiromarceloneppel
andauthored
[DPE-4369] SQL queries exposing passwords on postgresql logs (#506)
* add integration test * make create_user calls change log config * Pin redis-k8s charm revision * bump LIBPATCH * re-bump LIBPATCH * Revert "Pin redis-k8s charm revision" This reverts commit 2219fd6. * revert redis-k8s version 28 pinning --------- Co-authored-by: Marcelo Henrique Neppel <[email protected]>
1 parent 62c086a commit 5fef9c7

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

lib/charms/postgresql_k8s/v0/postgresql.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Increment this PATCH version before using `charmcraft publish-lib` or reset
3838
# to 0 if you are raising the major API version
39-
LIBPATCH = 27
39+
LIBPATCH = 28
4040

4141
INVALID_EXTRA_USER_ROLE_BLOCKING_MESSAGE = "invalid role(s) for extra user roles"
4242

@@ -230,7 +230,10 @@ def create_user(
230230
user_definition += f"WITH {'NOLOGIN' if user == 'admin' else 'LOGIN'}{' SUPERUSER' if admin else ''} ENCRYPTED PASSWORD '{password}'{'IN ROLE admin CREATEDB' if admin_role else ''}"
231231
if privileges:
232232
user_definition += f' {" ".join(privileges)}'
233+
cursor.execute(sql.SQL("BEGIN;"))
234+
cursor.execute(sql.SQL("SET LOCAL log_statement = 'none';"))
233235
cursor.execute(sql.SQL(f"{user_definition};").format(sql.Identifier(user)))
236+
cursor.execute(sql.SQL("COMMIT;"))
234237

235238
# Add extra user roles to the new user.
236239
if roles:
@@ -519,11 +522,14 @@ def update_user_password(
519522
with self._connect_to_database(
520523
database_host=database_host
521524
) as connection, connection.cursor() as cursor:
525+
cursor.execute(sql.SQL("BEGIN;"))
526+
cursor.execute(sql.SQL("SET LOCAL log_statement = 'none';"))
522527
cursor.execute(
523528
sql.SQL("ALTER USER {} WITH ENCRYPTED PASSWORD '" + password + "';").format(
524529
sql.Identifier(username)
525530
)
526531
)
532+
cursor.execute(sql.SQL("COMMIT;"))
527533
except psycopg2.Error as e:
528534
logger.error(f"Failed to update user password: {e}")
529535
raise PostgreSQLUpdateUserPasswordError()

tests/integration/new_relations/test_new_relations.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,8 @@ async def test_discourse(ops_test: OpsTest):
590590
# Deploy Discourse and Redis.
591591
await gather(
592592
ops_test.model.deploy(DISCOURSE_APP_NAME, application_name=DISCOURSE_APP_NAME),
593-
# Revision 28 is being used due to https://github.com/canonical/redis-k8s-operator/issues/87.
594593
ops_test.model.deploy(
595-
REDIS_APP_NAME,
596-
application_name=REDIS_APP_NAME,
597-
channel="latest/edge",
598-
revision=28,
599-
series="jammy",
594+
REDIS_APP_NAME, application_name=REDIS_APP_NAME, channel="latest/edge"
600595
),
601596
)
602597

tests/integration/test_password_rotation.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
get_primary,
2020
get_unit_address,
2121
restart_patroni,
22+
run_command_on_unit,
2223
set_password,
2324
)
2425

@@ -125,8 +126,8 @@ async def test_password_from_secret_same_as_cli(ops_test: OpsTest):
125126
I.e. we're manipulating the secret we think we're manipulating.
126127
"""
127128
#
128-
# No way to retrieve a secet by label for now (https://bugs.launchpad.net/juju/+bug/2037104)
129-
# Therefore we take advantage of the fact, that we only have ONE single secret a this point
129+
# No way to retrieve a secret by label for now (https://bugs.launchpad.net/juju/+bug/2037104)
130+
# Therefore we take advantage of the fact, that we only have ONE single secret at this point
130131
# So we take the single member of the list
131132
# NOTE: This would BREAK if for instance units had secrets at the start...
132133
#
@@ -176,3 +177,18 @@ async def test_no_password_change_on_invalid_password(ops_test: OpsTest) -> None
176177
password2 = await get_password(ops_test, username="replication")
177178
# The password didn't change
178179
assert password1 == password2
180+
181+
182+
@pytest.mark.group(1)
183+
async def test_no_password_exposed_on_logs(ops_test: OpsTest) -> None:
184+
"""Test that passwords don't get exposed on postgresql logs."""
185+
for unit in ops_test.model.applications[APP_NAME].units:
186+
try:
187+
logs = await run_command_on_unit(
188+
ops_test,
189+
unit.name,
190+
"grep PASSWORD /var/log/postgresql/postgresql-*.log",
191+
)
192+
except Exception:
193+
continue
194+
assert len(logs) == 0, f"Sensitive information detected on {unit.name} logs"

0 commit comments

Comments
 (0)