Skip to content

Commit 2e492f8

Browse files
author
Lucas Gameiro
authored
add tls and tls-ca fields to databag (#666)
1 parent c5c916d commit 2e492f8

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/charm.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,9 @@ def update_config(self, is_creating_backup: bool = False) -> bool:
17091709
# in a bundle together with the TLS certificates operator. This flag is used to
17101710
# know when to call the Patroni API using HTTP or HTTPS.
17111711
self.unit_peer_data.update({"tls": "enabled" if enable_tls else ""})
1712+
self.postgresql_client_relation.update_tls_flag(
1713+
"True" if self.is_tls_enabled else "False"
1714+
)
17121715
logger.debug("Early exit update_config: Workload not started yet")
17131716
return True
17141717

@@ -1784,6 +1787,7 @@ def _handle_postgresql_restart_need(self, enable_tls: bool) -> None:
17841787
# Ignore the error, as it happens only to indicate that the configuration has not changed.
17851788
pass
17861789
self.unit_peer_data.update({"tls": "enabled" if enable_tls else ""})
1790+
self.postgresql_client_relation.update_tls_flag("True" if self.is_tls_enabled else "False")
17871791

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

src/relations/postgresql_provider.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
108108
# Set the database name
109109
self.database_provides.set_database(event.relation.id, database)
110110

111+
# Set TLS flag
112+
self.database_provides.set_tls(
113+
event.relation.id,
114+
"True" if self.charm.is_tls_enabled else "False",
115+
)
116+
117+
# Set TLS CA
118+
if self.charm.is_tls_enabled:
119+
_, ca, _ = self.charm.tls.get_tls_files()
120+
self.database_provides.set_tls_ca(event.relation.id, ca)
121+
111122
# Update the read/write and read-only endpoints.
112123
self.update_endpoints(event)
113124

@@ -215,6 +226,18 @@ def update_endpoints(self, event: DatabaseRequestedEvent = None) -> None:
215226
f"postgresql://{user}:{password}@{self.charm.primary_endpoint}:{DATABASE_PORT}/{database}",
216227
)
217228

229+
def update_tls_flag(self, tls: str) -> None:
230+
"""Update TLS flag and CA in relation databag."""
231+
relations = self.model.relations[self.relation_name]
232+
if tls == "True":
233+
_, ca, _ = self.charm.tls.get_tls_files()
234+
else:
235+
ca = ""
236+
237+
for relation in relations:
238+
self.database_provides.set_tls(relation.id, tls)
239+
self.database_provides.set_tls_ca(relation.id, ca)
240+
218241
def _check_multiple_endpoints(self) -> bool:
219242
"""Checks if there are relations with other endpoints."""
220243
relation_names = {relation.name for relation in self.charm.client_relations}

tests/unit/test_postgresql_provider.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def test_on_database_requested(harness):
142142
"password": "test-password",
143143
"version": POSTGRESQL_VERSION,
144144
"database": f"{DATABASE}",
145+
"tls": "False",
145146
}
146147

147148
# Assert no BlockedStatus was set.
@@ -153,6 +154,7 @@ def test_on_database_requested(harness):
153154
# No data is set in the databag by the database.
154155
assert harness.get_relation_data(rel_id, harness.charm.app.name) == {
155156
"data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}',
157+
"tls": "False",
156158
}
157159

158160
# BlockedStatus due to a PostgreSQLCreateDatabaseError.
@@ -161,6 +163,7 @@ def test_on_database_requested(harness):
161163
# No data is set in the databag by the database.
162164
assert harness.get_relation_data(rel_id, harness.charm.app.name) == {
163165
"data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}',
166+
"tls": "False",
164167
}
165168

166169
# BlockedStatus due to a PostgreSQLGetPostgreSQLVersionError.

0 commit comments

Comments
 (0)