Skip to content

Commit 59989f5

Browse files
authored
added check_tls_replication for checking replicas encrypted connection (#437)
1 parent ab6ed3e commit 59989f5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

tests/integration/helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,35 @@ async def check_tls(ops_test: OpsTest, unit_name: str, enabled: bool) -> bool:
736736
return False
737737

738738

739+
async def check_tls_replication(ops_test: OpsTest, unit_name: str, enabled: bool) -> bool:
740+
"""Returns whether TLS is enabled on the replica PostgreSQL instance.
741+
742+
Args:
743+
ops_test: The ops test framework instance.
744+
unit_name: The name of the replica of the PostgreSQL instance.
745+
enabled: check if TLS is enabled/disabled
746+
747+
Returns:
748+
Whether TLS is enabled/disabled.
749+
"""
750+
unit_address = get_unit_address(ops_test, unit_name)
751+
password = await get_password(ops_test, unit_name)
752+
753+
# Check for the all replicas using encrypted connection
754+
output = await execute_query_on_unit(
755+
unit_address,
756+
password,
757+
"SELECT pg_ssl.ssl, pg_sa.client_addr FROM pg_stat_ssl pg_ssl"
758+
" JOIN pg_stat_activity pg_sa ON pg_ssl.pid = pg_sa.pid"
759+
" AND pg_sa.usename = 'replication';",
760+
)
761+
762+
for i in range(0, len(output), 2):
763+
if output[i] != enabled:
764+
return False
765+
return True
766+
767+
739768
async def check_tls_patroni_api(ops_test: OpsTest, unit_name: str, enabled: bool) -> bool:
740769
"""Returns whether TLS is enabled on Patroni REST API.
741770

tests/integration/test_tls.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
change_primary_start_timeout,
1616
check_tls,
1717
check_tls_patroni_api,
18+
check_tls_replication,
1819
db_connect,
1920
get_password,
2021
get_primary,
@@ -85,6 +86,9 @@ async def test_tls_enabled(ops_test: OpsTest) -> None:
8586
if unit.name != primary
8687
][0]
8788

89+
# Check if TLS enabled for replication
90+
assert await check_tls_replication(ops_test, primary, enabled=True)
91+
8892
# Enable additional logs on the PostgreSQL instance to check TLS
8993
# being used in a later step and make the fail-over to happens faster.
9094
await ops_test.model.applications[DATABASE_APP_NAME].set_config({

0 commit comments

Comments
 (0)