Skip to content

Commit

Permalink
[DPE-3180] Share secrets to host application (#326)
Browse files Browse the repository at this point in the history
## Issue
1. Mongos charm needs to be notified of updated username/password
2. URI construction in mongos should consider connections made via the
Unix Domain Socket

## Future PR
Most of the work for `DPE-3180` is done on the `mongos` charm. But the
`mongos` charm relies on the libraries hosted by `mongodb` charm to make
the necessary changes future work:
1. updates libraries
2. shares connection info to the application hosting the subordinate
`mongos` charm
3. integration tests verifying its functiondd
  • Loading branch information
MiaAltieri authored Jan 5, 2024
1 parent e9c6c7c commit 4e54b42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 15 additions & 2 deletions lib/charms/mongodb/v0/config_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 1
LIBPATCH = 2


class ClusterProvider(Object):
Expand Down Expand Up @@ -139,6 +139,7 @@ def __init__(
self.database_requires = DatabaseRequires(
self.charm,
relation_name=self.relation_name,
relations_aliases=[self.relation_name],
database_name=self.charm.database,
extra_user_roles=self.charm.extra_user_roles,
additional_secret_fields=[KEYFILE_KEY],
Expand All @@ -149,11 +150,24 @@ def __init__(
charm.on[self.relation_name].relation_created,
self.database_requires._on_relation_created_event,
)

self.framework.observe(
self.database_requires.on.database_created, self._on_database_created
)
self.framework.observe(
charm.on[self.relation_name].relation_changed, self._on_relation_changed
)
# TODO Future PRs handle scale down

def _on_database_created(self, event) -> None:
if not self.charm.unit.is_leader():
return

logger.info("Database and user created for mongos application")
self.charm.set_secret(Config.Relations.APP_SCOPE, Config.Secrets.USERNAME, event.username)
self.charm.set_secret(Config.Relations.APP_SCOPE, Config.Secrets.PASSWORD, event.password)
self.charm.share_connection_info()

def _on_relation_changed(self, event) -> None:
"""Starts/restarts monogs with config server information."""
key_file_contents = self.database_requires.fetch_relation_field(
Expand Down Expand Up @@ -186,7 +200,6 @@ def _on_relation_changed(self, event) -> None:
event.defer()
return

self.charm.share_uri()
self.charm.unit.status = ActiveStatus()

# BEGIN: helper functions
Expand Down
7 changes: 5 additions & 2 deletions lib/charms/mongodb/v1/mongos.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class MongosConfiguration:
@property
def uri(self):
"""Return URI concatenated from fields."""
hosts = [f"{host}:{self.port}" for host in self.hosts]
hosts = ",".join(hosts)
# mongos using Unix Domain Socket to communicate do not use port
if self.port:
self.hosts = [f"{host}:{self.port}" for host in self.hosts]

hosts = ",".join(self.hosts)
# Auth DB should be specified while user connects to application DB.
auth_source = ""
if self.database != "admin":
Expand Down

0 comments on commit 4e54b42

Please sign in to comment.