Skip to content

Commit

Permalink
propogating passwords of internal db users happens automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Sep 22, 2023
1 parent 0a432ea commit 059356f
Showing 1 changed file with 1 addition and 26 deletions.
27 changes: 1 addition & 26 deletions lib/charms/mongodb/v0/shards_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
import logging

from charms.mongodb.v0.helpers import KEY_FILE
from charms.mongodb.v0.mongodb import MongoDBConnection, NotReadyError, PyMongoError
from charms.mongodb.v0.users import MongoDBUser, OperatorUser
from ops.charm import CharmBase
from ops.framework import Object
from ops.model import BlockedStatus, MaintenanceStatus
from tenacity import RetryError, Retrying, stop_after_delay, wait_fixed

from config import Config

Expand Down Expand Up @@ -139,12 +137,7 @@ def _on_relation_changed(self, event):
# shards rely on the config server for secrets
relation_data = event.relation.data[event.app]
self.update_keyfile(key_file_contents=relation_data.get(KEYFILE_KEY))

try:
self.update_operator_password(new_password=relation_data.get(OPERATOR_PASSWORD_KEY))
except RetryError:
self.charm.unit.status = BlockedStatus("Shard not added to config-server")
return
self.update_operator_password(new_password=relation_data.get(OPERATOR_PASSWORD_KEY))

self.charm.unit.status = MaintenanceStatus("Adding shard to config-server")

Expand All @@ -167,24 +160,6 @@ def update_operator_password(self, new_password: str) -> None:
if not new_password or new_password == current_password or not self.charm.unit.is_leader():
return

# updating operator password, usually comes after keyfile was updated, hence, the mongodb
# service was restarted. Sometimes this requires units getting insync again.
for attempt in Retrying(stop=stop_after_delay(60), wait=wait_fixed(3)):
with attempt:
# TODO, in the future use set_password from src/charm.py - this will require adding
# a library, for exceptions used in both charm code and lib code.
with MongoDBConnection(self.charm.mongodb_config) as mongo:
try:
mongo.set_user_password(OperatorUser.get_username(), new_password)
except NotReadyError:
logger.error(
"Failed changing the password: Not all members healthy or finished initial sync."
)
raise
except PyMongoError as e:
logger.error(f"Failed changing the password: {e}")
raise

self.charm.set_secret(
Config.Relations.APP_SCOPE,
MongoDBUser.get_password_key_name_for_user(OperatorUser.get_username()),
Expand Down

0 comments on commit 059356f

Please sign in to comment.