Skip to content

Commit 675e66e

Browse files
authored
[DPE-5215] Upgrade libraries to fix secret disclosure issue (#328)
1 parent 7911c38 commit 675e66e

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

lib/charms/mongodb/v0/config_server_interface.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
from charms.data_platform_libs.v0.data_interfaces import (
1313
DatabaseProvides,
14+
DatabaseRequestedEvent,
1415
DatabaseRequires,
1516
)
1617
from charms.mongodb.v1.mongos import MongosConnection
17-
from ops.charm import CharmBase, EventBase, RelationBrokenEvent
18+
from ops.charm import CharmBase, EventBase, RelationBrokenEvent, RelationChangedEvent
1819
from ops.framework import Object
1920
from ops.model import (
2021
ActiveStatus,
@@ -42,7 +43,7 @@
4243

4344
# Increment this PATCH version before using `charmcraft publish-lib` or reset
4445
# to 0 if you are raising the major API version
45-
LIBPATCH = 9
46+
LIBPATCH = 12
4647

4748

4849
class ClusterProvider(Object):
@@ -57,6 +58,9 @@ def __init__(
5758
self.database_provides = DatabaseProvides(self.charm, relation_name=self.relation_name)
5859

5960
super().__init__(charm, self.relation_name)
61+
self.framework.observe(
62+
self.database_provides.on.database_requested, self._on_database_requested
63+
)
6064
self.framework.observe(
6165
charm.on[self.relation_name].relation_changed, self._on_relation_changed
6266
)
@@ -105,8 +109,14 @@ def is_valid_mongos_integration(self) -> bool:
105109

106110
return True
107111

108-
def _on_relation_changed(self, event) -> None:
109-
"""Handles providing mongos with KeyFile and hosts."""
112+
def _on_database_requested(self, event: DatabaseRequestedEvent | RelationChangedEvent) -> None:
113+
"""Handles the database requested event.
114+
115+
The first time secrets are written to relations should be on this event.
116+
117+
Note: If secrets are written for the first time on other events we risk
118+
the chance of writing secrets in plain sight.
119+
"""
110120
if not self.pass_hook_checks(event):
111121
if not self.is_valid_mongos_integration():
112122
self.charm.status.set_and_share_status(
@@ -116,12 +126,9 @@ def _on_relation_changed(self, event) -> None:
116126
)
117127
logger.info("Skipping relation joined event: hook checks did not pass")
118128
return
119-
120129
config_server_db = self.generate_config_server_db()
121-
122130
# create user and set secrets for mongos relation
123131
self.charm.client_relations.oversee_users(None, None)
124-
125132
relation_data = {
126133
KEYFILE_KEY: self.charm.get_secret(
127134
Config.Relations.APP_SCOPE, Config.Secrets.SECRET_KEYFILE_NAME
@@ -135,9 +142,20 @@ def _on_relation_changed(self, event) -> None:
135142
)
136143
if int_tls_ca:
137144
relation_data[INT_TLS_CA_KEY] = int_tls_ca
138-
139145
self.database_provides.update_relation_data(event.relation.id, relation_data)
140146

147+
def _on_relation_changed(self, event: RelationChangedEvent) -> None:
148+
"""Handles providing mongos with KeyFile and hosts."""
149+
# First we need to ensure that the database requested event has run
150+
# otherwise we risk the chance of writing secrets in plain sight.
151+
if not self.database_provides.fetch_relation_field(event.relation.id, "database"):
152+
logger.info("Database Requested has not run yet, skipping.")
153+
event.defer()
154+
return
155+
156+
# TODO : This workflow is a fix until we have time for a better and complete fix (DPE-5513)
157+
self._on_database_requested(event)
158+
141159
def _on_relation_broken(self, event) -> None:
142160
if self.charm.upgrade_in_progress:
143161
logger.warning(
@@ -328,6 +346,8 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
328346
# K8s charm have a 1:Many client scheme and share connection info in a different manner.
329347
if self.substrate == Config.Substrate.VM:
330348
self.charm.remove_connection_info()
349+
else:
350+
self.db_initialised = False
331351

332352
# BEGIN: helper functions
333353
def pass_hook_checks(self, event):
@@ -371,7 +391,7 @@ def is_mongos_running(self) -> bool:
371391
connection_uri = f"mongodb://{self.charm.get_mongos_host()}"
372392

373393
# use the mongos port for k8s charms and external connections on VM
374-
if self.charm.is_external_client or self.substrate == Config.K8S_SUBSTRATE:
394+
if self.substrate == Config.Substrate.K8S or self.charm.is_external_client:
375395
connection_uri = connection_uri + f":{Config.MONGOS_PORT}"
376396

377397
with MongosConnection(None, connection_uri) as mongo:

tests/integration/sharding_tests/test_mongos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def test_connect_to_cluster_creates_user(ops_test: OpsTest) -> None:
9494
lambda: is_relation_joined(
9595
ops_test,
9696
CLUSTER_REL_NAME,
97-
CONFIG_SERVER_REL_NAME,
97+
CLUSTER_REL_NAME,
9898
)
9999
is True,
100100
timeout=600,

0 commit comments

Comments
 (0)