Skip to content

Commit 5f86ac0

Browse files
authored
fix: Do not integrate an invalid relation (#350)
1 parent f02e1fe commit 5f86ac0

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/charms/mongodb/v1/mongodb_provider.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
from charms.data_platform_libs.v0.data_interfaces import DatabaseProvides
1717
from charms.mongodb.v0.mongo import MongoConfiguration, MongoConnection
1818
from charms.mongodb.v1.helpers import generate_password
19-
from ops.charm import CharmBase, EventBase, RelationBrokenEvent, RelationChangedEvent
19+
from ops.charm import (
20+
CharmBase,
21+
EventBase,
22+
RelationBrokenEvent,
23+
RelationChangedEvent,
24+
RelationEvent,
25+
)
2026
from ops.framework import Object
2127
from ops.model import Relation
2228
from pymongo.errors import PyMongoError
@@ -31,7 +37,7 @@
3137

3238
# Increment this PATCH version before using `charmcraft publish-lib` or reset
3339
# to 0 if you are raising the major API version
34-
LIBPATCH = 13
40+
LIBPATCH = 15
3541

3642
logger = logging.getLogger(__name__)
3743
REL_NAME = "database"
@@ -88,6 +94,11 @@ def pass_sanity_hook_checks(self) -> bool:
8894
if not self.charm.db_initialised:
8995
return False
9096

97+
# Warning: the sanity_hook_checks can pass when the call is
98+
# issued by a config-sever because the config-server is allowed to manage the users
99+
# in MongoDB. This is not well named and does not protect integration of a config-server
100+
# to a client application. The mongos charm however doesn't care
101+
# because it supports only one integration that uses MongoDBProvider.
91102
if not self.charm.is_role(Config.Role.MONGOS) and not self.charm.is_relation_feasible(
92103
self.get_relation_name()
93104
):
@@ -99,8 +110,14 @@ def pass_sanity_hook_checks(self) -> bool:
99110

100111
return True
101112

102-
def pass_hook_checks(self, event: EventBase) -> bool:
113+
def pass_hook_checks(self, event: RelationEvent) -> bool:
103114
"""Runs the pre-hooks checks for MongoDBProvider, returns True if all pass."""
115+
# First, ensure that the relation is valid, useless to do anything else otherwise
116+
if not self.charm.is_role(Config.Role.MONGOS) and not self.charm.is_relation_feasible(
117+
event.relation.name
118+
):
119+
return False
120+
104121
if not self.pass_sanity_hook_checks():
105122
return False
106123

@@ -372,6 +389,7 @@ def _get_config(
372389
mongo_args["port"] = Config.MONGOS_PORT
373390
if self.substrate == Config.Substrate.K8S:
374391
mongo_args["hosts"] = self.charm.get_mongos_hosts_for_client()
392+
mongo_args["port"] = self.charm.get_mongos_port()
375393
else:
376394
mongo_args["replset"] = self.charm.app.name
377395

tests/unit/test_mongodb_provider.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ def setUp(self, *unused):
3939
self.charm = self.harness.charm
4040
self.addCleanup(self.harness.cleanup)
4141

42+
@patch("charms.mongodb.v0.set_status.get_charm_revision")
43+
@patch("charm.CrossAppVersionChecker.is_local_charm")
44+
@patch("charm.CrossAppVersionChecker.is_integrated_to_locally_built_charm")
4245
@patch("ops.framework.EventBase.defer")
4346
@patch("charm.MongoDBProvider.oversee_users")
44-
def test_relation_event_db_not_initialised(self, oversee_users, defer):
47+
def test_relation_event_db_not_initialised(self, oversee_users, defer, *unused):
4548
"""Tests no database relations are handled until the database is initialised.
4649
4750
Users should not be "overseen" until the database has been initialised, no matter the

0 commit comments

Comments
 (0)