Skip to content

Commit bdca5d4

Browse files
committed
Improve TLS status retrieval
1 parent d9ec362 commit bdca5d4

File tree

2 files changed

+12
-47
lines changed

2 files changed

+12
-47
lines changed

src/charm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ def _patroni(self) -> Patroni:
410410
self._peer_members_ips,
411411
self._get_password(),
412412
self._replication_password,
413+
bool(self.unit_peer_data.get("tls")),
413414
)
414415

415416
@property
@@ -821,7 +822,8 @@ def update_config(self) -> None:
821822
return
822823

823824
restart_postgresql = enable_tls != self.postgresql.is_tls_enabled()
824-
self._patroni.reload_patroni_configuration(restart_postgresql)
825+
self._patroni.reload_patroni_configuration()
826+
self.unit_peer_data.update({"tls": "enabled" if enable_tls else ""})
825827

826828
# Restart PostgreSQL if TLS configuration has changed
827829
# (so the both old and new connections use the configuration).

src/cluster.py

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(
6262
peers_ips: Set[str],
6363
superuser_password: str,
6464
replication_password: str,
65+
tls_enabled: bool,
6566
):
6667
"""Initialize the Patroni class.
6768
@@ -74,6 +75,7 @@ def __init__(
7475
planned_units: number of units planned for the cluster
7576
superuser_password: password for the operator user
7677
replication_password: password for the user used in the replication
78+
tls_enabled: whether TLS is enabled
7779
"""
7880
self.unit_ip = unit_ip
7981
self.storage_path = storage_path
@@ -83,52 +85,16 @@ def __init__(
8385
self.peers_ips = peers_ips
8486
self.superuser_password = superuser_password
8587
self.replication_password = replication_password
88+
self.tls_enabled = tls_enabled
8689
# Variable mapping to requests library verify parameter.
87-
self.verify = f"{self.storage_path}/{TLS_CA_FILE}" if self._tls_enabled else True
88-
89-
@property
90-
def _tls_enabled(self) -> bool:
91-
# return False
92-
def demote(user_uid, user_gid):
93-
def result():
94-
os.setgid(user_gid)
95-
os.setuid(user_uid)
96-
97-
return result
98-
99-
pw_record = pwd.getpwnam("postgres")
100-
user_uid = pw_record.pw_uid
101-
user_gid = pw_record.pw_gid
102-
103-
try:
104-
env = dict(os.environ, PGPASSWORD=self.superuser_password)
105-
ssl_query_result = subprocess.check_output(
106-
[
107-
"patronictl",
108-
"-c",
109-
f"{self.storage_path}/patroni.yml",
110-
"query",
111-
self.cluster_name,
112-
"--command",
113-
"SHOW ssl;",
114-
"--dbname",
115-
"postgres",
116-
"--username",
117-
USER,
118-
],
119-
env=env,
120-
preexec_fn=demote(user_uid, user_gid),
121-
timeout=10,
122-
).decode("UTF-8")
123-
# logger.warning(ssl_query_result)
124-
return "on" in ssl_query_result
125-
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
126-
return False
90+
# The CA bundle file is used to validate the server certificate when
91+
# TLS is enabled, otherwise True is set because it's the default value.
92+
self.verify = f"{self.storage_path}/{TLS_CA_FILE}" if tls_enabled else True
12793

12894
@property
12995
def _patroni_url(self) -> str:
13096
"""Patroni REST API URL."""
131-
return f"{'https' if self._tls_enabled else 'http'}://{self.unit_ip}:8008"
97+
return f"{'https' if self.tls_enabled else 'http'}://{self.unit_ip}:8008"
13298

13399
def bootstrap_cluster(self) -> bool:
134100
"""Bootstrap a PostgreSQL cluster using Patroni."""
@@ -395,12 +361,9 @@ def remove_raft_member(self, member_ip: str) -> None:
395361
raise RemoveRaftMemberFailedError()
396362

397363
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
398-
def reload_patroni_configuration(self, restart_postgresql: bool = False):
364+
def reload_patroni_configuration(self):
399365
"""Reload Patroni configuration after it was changed."""
400-
url = self._patroni_url
401-
if restart_postgresql:
402-
url.replace("https", "http")
403-
requests.post(f"{url}/reload", verify=self.verify)
366+
requests.post(f"{self._patroni_url}/reload", verify=self.verify)
404367

405368
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
406369
def restart_postgresql(self) -> None:

0 commit comments

Comments
 (0)